Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 10 additions & 32 deletions agentkit/src/agent-book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,10 @@ import { extractEVMChainId } from './evm'
import { getPublicClient } from './viem-client'

const WORLD_MAINNET = 'eip155:480' as const
const BASE_MAINNET = 'eip155:8453' as const
const BASE_SEPOLIA = 'eip155:84532' as const

type KnownChainId = typeof WORLD_MAINNET | typeof BASE_MAINNET | typeof BASE_SEPOLIA
const AGENT_BOOK_ADDRESS: `0x${string}` = '0xA23aB2712eA7BBa896930544C7d6636a96b944dA'

/** Known AgentBook deployments keyed by CAIP-2 network identifier. */
const KNOWN_DEPLOYMENTS: Record<KnownChainId, `0x${string}`> = {
[WORLD_MAINNET]: '0xA23aB2712eA7BBa896930544C7d6636a96b944dA',
[BASE_MAINNET]: '0xE1D1D3526A6FAa37eb36bD10B933C1b77f4561a4',
[BASE_SEPOLIA]: '0xA23aB2712eA7BBa896930544C7d6636a96b944dA',
}


export type AgentBookNetwork = 'world' | 'base' | 'base-sepolia'
export type AgentBookNetwork = 'world'

const AGENT_BOOK_ABI = [
{
Expand All @@ -35,53 +25,41 @@ export interface AgentBookOptions {
contractAddress?: `0x${string}`
/** Custom RPC URL. Defaults to the chain's default RPC. */
rpcUrl?: string
/** Pin lookup to the built-in Base or Base Sepolia AgentBook deployment. */
/** Pin lookup to the built-in World Chain AgentBook deployment. */
network?: AgentBookNetwork
}

export function createAgentBookVerifier(options: AgentBookOptions = {}) {
function resolveLookupChainId(chainId: string): KnownChainId {
if (options.network === 'base') return BASE_MAINNET
if (options.network === 'world') return WORLD_MAINNET
if (options.network === 'base-sepolia') return BASE_SEPOLIA

if (chainId === WORLD_MAINNET || chainId === BASE_MAINNET || chainId === BASE_SEPOLIA) {
return chainId
}

// AgentBook is only deployed on WorldChain, Base mainnet and Base Sepolia.
// Default Worldchain mainnet unless explicitly pinned.
function resolveLookupChainId(): typeof WORLD_MAINNET {
return WORLD_MAINNET
}

function getClient(chainId: string): PublicClient {
if (options.client) return options.client

const lookupChainId = resolveLookupChainId(chainId)

const numericId =
options.contractAddress && options.rpcUrl && !options.network
? extractEVMChainId(chainId)
: extractEVMChainId(lookupChainId)
: extractEVMChainId(resolveLookupChainId())

return getPublicClient(numericId, options.rpcUrl)
}

function getContractAddress(chainId: string): `0x${string}` {
function getContractAddress(): `0x${string}` {
if (options.contractAddress) return options.contractAddress
return KNOWN_DEPLOYMENTS[resolveLookupChainId(chainId)]
return AGENT_BOOK_ADDRESS
}

return {
/**
* Look up the anonymous human identifier for an agent's wallet address.
* @param address The agent's wallet address.
* @param chainId CAIP-2 chain identifier (e.g. "eip155:480"). Built-in
* lookup resolves to Base mainnet or Base Sepolia only.
* @param chainId CAIP-2 chain identifier (e.g. "eip155:480"). Lookup always
* resolves to the World Chain AgentBook deployment.
* @returns The human ID (hex string) if registered, or null.
*/
async lookupHuman(address: string, chainId: string): Promise<string | null> {
const contractAddress = getContractAddress(chainId)
const contractAddress = getContractAddress()
const client = getClient(chainId)

try {
Expand Down
14 changes: 1 addition & 13 deletions agentkit/tests/agent-book.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('createAgentBookVerifier', () => {
expect(calls).toEqual([{ address: '0xA23aB2712eA7BBa896930544C7d6636a96b944dA' }])
})

it('uses Base Sepolia when the request chain is Base Sepolia', async () => {
it('defaults to World Chain for unknown chains', async () => {
const calls: Array<{ address: string }> = []
const verifier = createAgentBookVerifier({
client: createMockClient(1n, calls),
Expand All @@ -35,18 +35,6 @@ describe('createAgentBookVerifier', () => {
expect(calls).toEqual([{ address: '0xA23aB2712eA7BBa896930544C7d6636a96b944dA' }])
})

it('allows pinning lookup to Base Sepolia explicitly', async () => {
const calls: Array<{ address: string }> = []
const verifier = createAgentBookVerifier({
client: createMockClient(1n, calls),
network: 'base-sepolia',
})

await verifier.lookupHuman('0x1234567890abcdef1234567890abcdef12345678', 'eip155:480')

expect(calls).toEqual([{ address: '0xA23aB2712eA7BBa896930544C7d6636a96b944dA' }])
})

it('still honors custom contract deployments', async () => {
const calls: Array<{ address: string }> = []
const verifier = createAgentBookVerifier({
Expand Down
16 changes: 4 additions & 12 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import './polyfill.js'
import { Cli, z } from 'incur'
import { createPublicClient, http, decodeAbiParameters } from 'viem'
import type { Hex } from 'viem'
import { base, baseSepolia, worldchain } from 'viem/chains'
import { worldchain } from 'viem/chains'
import { createWorldBridgeStore } from '@worldcoin/idkit-core'
import type { ISuccessResult } from '@worldcoin/idkit-core'
import { solidityEncode } from '@worldcoin/idkit-core/hashing'
Expand All @@ -12,9 +12,7 @@ import qrcode from 'qrcode-terminal'
// ─── Config ──────────────────────────────────────────────────────────────────

const NETWORKS = {
base: { chain: base, address: '0xE1D1D3526A6FAa37eb36bD10B933C1b77f4561a4' as const },
world: { chain: worldchain, address: '0xA23aB2712eA7BBa896930544C7d6636a96b944dA' as const},
'base-sepolia': { chain: baseSepolia, address: '0xA23aB2712eA7BBa896930544C7d6636a96b944dA' as const },
world: { chain: worldchain, address: '0xA23aB2712eA7BBa896930544C7d6636a96b944dA' as const },
} as const

const NETWORK_NAMES = Object.keys(NETWORKS) as [keyof typeof NETWORKS, ...Array<keyof typeof NETWORKS>]
Expand All @@ -32,7 +30,6 @@ const AGENT_BOOK_ABI = [
const APP_ID = 'app_a7c3e2b6b83927251a0db5345bd7146a'
const ACTION = 'agentbook-registration'
const DEFAULT_AUTO_API_URLS: Partial<Record<keyof typeof NETWORKS, string>> = {
base: 'https://x402-worldchain.vercel.app',
world: 'https://x402-worldchain.vercel.app',
}

Expand All @@ -58,7 +55,7 @@ cli.command('register', {
API_URL: z
.string()
.optional()
.describe('Override API base URL for registration relay; base mainnet defaults to https://x402-worldchain.vercel.app'),
.describe('Override API base URL for registration relay; defaults to https://x402-worldchain.vercel.app'),
}),
output: z.object({
agent: z.string(),
Expand All @@ -71,12 +68,7 @@ cli.command('register', {
txHash: z.string().optional(),
}),
examples: [
{ args: { address: '0x1234567890abcdef1234567890abcdef12345678' }, description: 'Register on Base mainnet' },
{
args: { address: '0x1234567890abcdef1234567890abcdef12345678' },
options: { network: 'base-sepolia' },
description: 'Register on Sepolia',
},
{ args: { address: '0x1234567890abcdef1234567890abcdef12345678' }, description: 'Register on World Chain' },
],
async run(c) {
const agentAddress = c.args.address as `0x${string}`
Expand Down
Loading