Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

feat: add README and tests for unified on-chain deploy stack

nirholasbd111d8aa7fa
diff --git a/src/onchain/README.md b/src/onchain/README.md
new file mode 100644
index 000000000..609e9699c
--- /dev/null
+++ b/src/onchain/README.md
@@ -0,0 +1,144 @@
+# src/onchain
+
+Unified, family-agnostic on-chain deploy stack. Parallel to (and intended to
+eventually supersede) `src/erc8004/deploy-button.js` + `src/erc8004/solana-deploy.js`.
+
+## Why this exists
+
+The legacy stack branches on chain family (`if isSolana ...`) at every layer:
+deploy button, register-prep payload, agent_identities `meta` shape. Every new
+chain family doubles the branching surface. This module collapses all of that
+behind two abstractions:
+
+- **`ChainRef`** — a CAIP-2-shaped value: `{ family, chainId | cluster }`. Every
+ helper takes a `ChainRef`. Family-specific knowledge does not leak into the
+ UI.
+- **`WalletAdapter`** — `{ connect, switchTo, signAndSend }`. EVM and Solana
+ adapters implement the same interface; the deploy orchestrator never sees
+ `window.ethereum` or `window.phantom` directly.
+
+## Layout
+
+```
+src/onchain/
+├── chain-ref.js ChainRef + CAIP-2 + chain registry (pure)
+├── adapters/
+│ ├── base.js WalletAdapter abstract class + isUserRejection()
+│ ├── evm.js ethers + window.ethereum
+│ ├── solana.js @solana/web3.js + Phantom/Backpack/Solflare + inline SIWS
+│ └── index.js getAdapter(family) factory with cache
+├── deploy.js Unified prep → sign → confirm orchestrator
+└── deploy-button.js Mountable UI component (drop-in for legacy)
+```
+
+## API surface (frontend)
+
+```js
+import { OnchainDeployButton } from 'src/onchain/deploy-button.js';
+
+const btn = new OnchainDeployButton({
+ agent, // { id, name, description?, avatarId?, onchain? }
+ container: document.querySelector('#deploy'),
+ preferredChain: 'eip155:8453', // CAIP-2; default Base mainnet
+});
+btn.mount();
+```
+
+Or, headless:
+
+```js
+import { deployAgent } from 'src/onchain/deploy.js';
+import { evm, solana } from 'src/onchain/chain-ref.js';
+
+const result = await deployAgent({
+ agent,
+ ref: solana('devnet'),
+ onProgress: (step) => console.log(step), // 'connect'|'prep'|'sign'|'confirm'|'save'
+});
+// → { ref, txHash, onchainId, contractOrMint, agent }
+```
+
+## API surface (server)
+
+Two endpoints — replace the four legacy ones:
+
+| Endpoint | Purpose |
+| --------------------------------- | ------------------------------------------------------- |
+| `POST /api/agents/onchain/prep` | Pin manifest, build family-specific tx prep, return prepId |
+| `POST /api/agents/onchain/confirm`| Verify tx on-chain, link wallet, write `meta.onchain` |
+
+Request shape (prep):
+
+```json
+{
+ "agent_id": "uuid",
+ "chain": "eip155:8453",
+ "wallet_address": "0x...",
+ "name": "...",
+ "description": "...",
+ "avatar_id": "uuid|null",
+ "skills": ["..."]
+}
+```
+
+Response (prep): `{ prepId, chain, metadataUri, cid, contractAddress | { assetPubkey, txBase64, cluster } }`
+
+Request shape (confirm):
+
+```json
+{
+ "prep_id": "...",
+ "tx_hash": "0x... | <base58 sig>",
+ "onchain_id": "<EVM agentId> | null",
+ "wallet_address": "..."
+}
+```
+
+Confirm writes a single canonical block to `agent_identities.meta.onchain`:
+
+```json
+{
+ "onchain": {
+ "chain": "eip155:8453",
+ "family": "evm",
+ "tx_hash": "0x...",
+ "onchain_id": "42",
+ "contract_or_mint": "0x8004...",
+ "wallet": "0x...",
+ "metadata_uri": "ipfs://...",
+ "confirmed_at": "2026-..."
+ }
+}
+```
+
+Frontend reads `agent.onchain` only — no family-specific fields.
+
+## Migration
+
+`api/_lib/migrations/2026-04-29-onchain-unified.sql` backfills legacy rows
+(both EVM and Solana shapes) into `meta.onchain`. Idempotent. Apply via:
+
+```bash
+psql $DATABASE_URL -f api/_lib/migrations/2026-04-29-onchain-unified.sql
+```
+
+The legacy endpoints (`register-prep`, `register-confirm`,
+`solana-register-prep`, `solana-register-confirm`) are left intact. Deprecate
+once all callers move to `/api/agents/onchain/*`.
+
+## Adding a new chain family
+
+1. Add a `ChainRef` constructor + CAIP-2 namespace handling in `chain-ref.js`.
+2. Add an entry helper to `buildRegistry()` so it appears in the dropdown.
+3. Implement `WalletAdapter` for the family in `adapters/<name>.js`.
+4. Wire the adapter into `getAdapter()` in `adapters/index.js`.
+5. Add a `prep<Family>()` branch in `api/agents/onchain/prep.js` and a
+ `verify<Family>()` branch in `api/agents/onchain/confirm.js`.
+
+No UI changes required.
+
+## Tests
+
+`tests/src/onchain.test.js` — pure-logic coverage of `chain-ref` + adapter
+factory dispatch. Wallet I/O is integration-tested via Playwright (not yet
+written for this module — TODO).
diff --git a/tests/src/onchain.test.js b/tests/src/onchain.test.js
new file mode 100644
index 000000000..db96c6953
--- /dev/null
+++ b/tests/src/onchain.test.js
@@ -0,0 +1,121 @@
+/**
+ * src/onchain — chain-ref + adapter factory smoke tests.
+ *
+ * Pure-logic only — no wallet I/O, no fetch. The deploy orchestrator and
+ * adapters are integration-tested via Playwright (see test:pages).
+ */
+
+import { describe, it, expect } from 'vitest';
+import {
+ evm,
+ solana,
+ toCaip2,
+ fromCaip2,
+ eqRef,
+ buildRegistry,
+ groupRegistry,
+ entryByCaip2,
+} from '../../src/onchain/chain-ref.js';
+
+describe('chain-ref', () => {
+ it('round-trips EVM through CAIP-2', () => {
+ const r = evm(8453);
+ expect(toCaip2(r)).toBe('eip155:8453');
+ expect(eqRef(r, fromCaip2('eip155:8453'))).toBe(true);
+ });
+
+ it('round-trips Solana mainnet through CAIP-2', () => {
+ const r = solana('mainnet');
+ expect(toCaip2(r)).toBe('solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp');
+ expect(fromCaip2(toCaip2(r))).toEqual(r);
+ });
+
+ it('round-trips Solana devnet through CAIP-2', () => {
+ const r = solana('devnet');
+ expect(toCaip2(r)).toBe('solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1');
+ });
+
+ it('rejects bad inputs', () => {
+ expect(() => evm(0)).toThrow();
+ expect(() => evm(-1)).toThrow();
+ expect(() => evm(1.5)).toThrow();
+ expect(() => solana('testnet')).toThrow();
+ expect(() => fromCaip2('eip155:0')).toThrow();
+ expect(() => fromCaip2('cosmos:1')).toThrow();
+ expect(() => fromCaip2('solana:nope')).toThrow();
+ });
+
+ it('eqRef compares by canonical CAIP-2', () => {
+ expect(eqRef(evm(1), evm(1))).toBe(true);
+ expect(eqRef(evm(1), evm(8453))).toBe(false);
+ expect(eqRef(solana('devnet'), solana('mainnet'))).toBe(false);
+ expect(eqRef(null, evm(1))).toBe(false);
+ });
+
+ it('buildRegistry includes EVM with deployments + both Solana clusters', () => {
+ const meta = {
+ 8453: { name: 'Base', shortName: 'Base', explorer: 'https://basescan.org', testnet: false },
+ 84532: {
+ name: 'Base Sepolia',
+ shortName: 'BaseSep',
+ explorer: 'https://sepolia.basescan.org',
+ testnet: true,
+ },
+ 999: { name: 'NoDeploy', shortName: 'X', explorer: '', testnet: false },
+ };
+ const deployments = { 8453: { identityRegistry: '0xabc' }, 84532: { identityRegistry: '0xdef' } };
+ const reg = buildRegistry(meta, deployments);
+
+ const caip2s = reg.map((e) => toCaip2(e.ref));
+ expect(caip2s).toContain('eip155:8453');
+ expect(caip2s).toContain('eip155:84532');
+ expect(caip2s).toContain('solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp');
+ expect(caip2s).toContain('solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1');
+ expect(caip2s).not.toContain('eip155:999'); // gated by deployments
+ });
+
+ it('groupRegistry splits mainnet vs testnet', () => {
+ const reg = buildRegistry(
+ { 8453: { name: 'Base', shortName: 'B', explorer: '', testnet: false } },
+ { 8453: { identityRegistry: '0xabc' } },
+ );
+ const { mainnets, testnets } = groupRegistry(reg);
+ expect(mainnets.some((e) => toCaip2(e.ref) === 'eip155:8453')).toBe(true);
+ expect(mainnets.some((e) => toCaip2(e.ref) === 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp')).toBe(true);
+ expect(testnets.some((e) => toCaip2(e.ref) === 'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1')).toBe(true);
+ });
+
+ it('entryByCaip2 finds and returns null', () => {
+ const reg = buildRegistry({}, {});
+ expect(entryByCaip2(reg, 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp')?.name).toBe('Solana');
+ expect(entryByCaip2(reg, 'eip155:1')).toBeNull();
+ });
+
+ it('explorer URLs differ between EVM and Solana', () => {
+ const reg = buildRegistry(
+ { 8453: { name: 'Base', shortName: 'B', explorer: 'https://basescan.org', testnet: false } },
+ { 8453: { identityRegistry: '0xabc' } },
+ );
+ const evmEntry = entryByCaip2(reg, 'eip155:8453');
+ const solEntry = entryByCaip2(reg, 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp');
+ expect(evmEntry.explorerTx('0xdead')).toBe('https://basescan.org/tx/0xdead');
+ expect(solEntry.explorerTx('sigZ')).toBe('https://solscan.io/tx/sigZ');
+ });
+});
+
+describe('adapter factory', () => {
+ it('returns the correct family adapter and caches', async () => {
+ const { getAdapter, _resetAdapters } = await import('../../src/onchain/adapters/index.js');
+ _resetAdapters();
+ const e1 = getAdapter('evm');
+ const e2 = getAdapter('evm');
+ expect(e1).toBe(e2);
+ expect(e1.family).toBe('evm');
+
+ const s1 = getAdapter('solana');
+ expect(s1.family).toBe('solana');
+ expect(s1).not.toBe(e1);
+
+ expect(() => getAdapter('cosmos')).toThrow();
+ });
+});