Quickstart
Give something you control an identity, from TypeScript with viem, on Sepolia. Four calls.
Set up
Node 18+, npm install viem, a Sepolia RPC URL, and a wallet with a little test ETH.
import { createPublicClient, createWalletClient, http, parseAbi, toHex } from "viem"; import { privateKeyToAccount } from "viem/accounts"; import { sepolia } from "viem/chains"; const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`); const rpc = http(process.env.RPC_URL); const publicClient = createPublicClient({ chain: sepolia, transport: rpc }); const wallet = createWalletClient({ chain: sepolia, transport: rpc, account }); const ADAPTER = "0x7621630cB63a73a194f45A3E6801B8C6A7eC2f92"; const abi = parseAbi([ "function hashBinding(uint8 standard, address boundAddress, uint256 tokenId) view returns (bytes32)", "function counterfactualRegister(uint8 standard, address boundAddress, uint256 tokenId, string agentURI) returns (bytes32)", "function counterfactualSetMetadata(uint8 standard, address boundAddress, uint256 tokenId, string key, bytes value) returns (bytes32)", "function counterfactualSetAgentWalletAndUBID(uint8 standard, address boundAddress, uint256 tokenId) returns (bytes32)", "function attest(uint8 attestationType, bytes32 ubid, bytes32 variant, bytes data)", "function register(uint8 standard, address boundAddress, uint256 tokenId, string agentURI) returns (uint256)", ]); // Standards: 0 ERC721, 1 ERC1155, 2 ERC6909, 3 ERC1155F, 4 ERC6909F, 5 ACCOUNT, 6 CONTRACT_OWNABLE, 7 CONTRACT_ADMIN const ERC721 = 0;
1. Know your UBID before you do anything
const NFT = "0xYourCollection"; const TOKEN_ID = 7n; const ubid = await publicClient.readContract({ address: ADAPTER, abi, functionName: "hashBinding", args: [ERC721, NFT, TOKEN_ID], }); // Same value before and after claiming or registering. This is the identity's permanent name.
2. Claim it
One event, no storage. The caller must control the token under the standard's rule - here, own it.
await wallet.writeContract({ address: ADAPTER, abi, functionName: "counterfactualRegister", args: [ERC721, NFT, TOKEN_ID, "ipfs://…/agent.json"], }); // Optional: a name the explorer will show. await wallet.writeContract({ address: ADAPTER, abi, functionName: "counterfactualSetMetadata", args: [ERC721, NFT, TOKEN_ID, "name", toHex("PunkBot")], });
For your own address instead of a token, the standard is 5 (ACCOUNT), the bound address is your address, and the token id is 0.
3. Link the wallet the agent signs from
If the agent signs from the same address that controls the token, one call does both halves:
await wallet.writeContract({ address: ADAPTER, abi, functionName: "counterfactualSetAgentWalletAndUBID", args: [ERC721, NFT, TOKEN_ID], });
If it's a different address, the controller names it and that wallet confirms - see Operating wallets.
4. Say something about an agent
import { concatHex } from "viem"; const ZERO = "0x" + "00".repeat(32) as `0x${string}`; // your rating of the agent: a score, optionally followed by your review - one call, one live per wallet await wallet.writeContract({ address: ADAPTER, abi, functionName: "attest", args: [3, ubid, ZERO, concatHex([toHex(90, { size: 1 }), toHex("Fast, fair, would deal again.")])], }); // a record of one transaction: score ‖ tx hash ‖ optional note, with the hash in the variant slot too await wallet.writeContract({ address: ADAPTER, abi, functionName: "attest", args: [5, ubid, txHash, concatHex([toHex(95, { size: 1 }), txHash, toHex("Settled in one block.")])], });
Later: register fully
Mints an ERC-8004 agent bound to the same coordinates. Everything attached to the UBID carries over.
await wallet.writeContract({ address: ADAPTER, abi, functionName: "register", args: [ERC721, NFT, TOKEN_ID, "ipfs://…/agent.json"] });
Every write is authority-checked by the contract. Simulating the call first (viem's simulateContract) tells you whether you'd pass before you sign - that is exactly what the explorer's create flow does.