@agirails/sdk 2.2.0 → 2.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/dist/ACTPClient.d.ts +200 -0
  2. package/dist/ACTPClient.d.ts.map +1 -1
  3. package/dist/ACTPClient.js +266 -2
  4. package/dist/ACTPClient.js.map +1 -1
  5. package/dist/abi/ACTPKernel.json +16 -0
  6. package/dist/adapters/AdapterRegistry.d.ts +140 -0
  7. package/dist/adapters/AdapterRegistry.d.ts.map +1 -0
  8. package/dist/adapters/AdapterRegistry.js +166 -0
  9. package/dist/adapters/AdapterRegistry.js.map +1 -0
  10. package/dist/adapters/AdapterRouter.d.ts +165 -0
  11. package/dist/adapters/AdapterRouter.d.ts.map +1 -0
  12. package/dist/adapters/AdapterRouter.js +350 -0
  13. package/dist/adapters/AdapterRouter.js.map +1 -0
  14. package/dist/adapters/BaseAdapter.d.ts +17 -0
  15. package/dist/adapters/BaseAdapter.d.ts.map +1 -1
  16. package/dist/adapters/BaseAdapter.js +21 -0
  17. package/dist/adapters/BaseAdapter.js.map +1 -1
  18. package/dist/adapters/BasicAdapter.d.ts +72 -3
  19. package/dist/adapters/BasicAdapter.d.ts.map +1 -1
  20. package/dist/adapters/BasicAdapter.js +170 -2
  21. package/dist/adapters/BasicAdapter.js.map +1 -1
  22. package/dist/adapters/IAdapter.d.ts +230 -0
  23. package/dist/adapters/IAdapter.d.ts.map +1 -0
  24. package/dist/adapters/IAdapter.js +44 -0
  25. package/dist/adapters/IAdapter.js.map +1 -0
  26. package/dist/adapters/StandardAdapter.d.ts +70 -1
  27. package/dist/adapters/StandardAdapter.d.ts.map +1 -1
  28. package/dist/adapters/StandardAdapter.js +184 -0
  29. package/dist/adapters/StandardAdapter.js.map +1 -1
  30. package/dist/adapters/X402Adapter.d.ts +208 -0
  31. package/dist/adapters/X402Adapter.d.ts.map +1 -0
  32. package/dist/adapters/X402Adapter.js +423 -0
  33. package/dist/adapters/X402Adapter.js.map +1 -0
  34. package/dist/adapters/index.d.ts +8 -0
  35. package/dist/adapters/index.d.ts.map +1 -1
  36. package/dist/adapters/index.js +19 -1
  37. package/dist/adapters/index.js.map +1 -1
  38. package/dist/cli/commands/init.d.ts +4 -0
  39. package/dist/cli/commands/init.d.ts.map +1 -1
  40. package/dist/cli/commands/init.js +184 -4
  41. package/dist/cli/commands/init.js.map +1 -1
  42. package/dist/config/networks.js +3 -3
  43. package/dist/config/networks.js.map +1 -1
  44. package/dist/erc8004/ERC8004Bridge.d.ts +155 -0
  45. package/dist/erc8004/ERC8004Bridge.d.ts.map +1 -0
  46. package/dist/erc8004/ERC8004Bridge.js +325 -0
  47. package/dist/erc8004/ERC8004Bridge.js.map +1 -0
  48. package/dist/erc8004/ReputationReporter.d.ts +223 -0
  49. package/dist/erc8004/ReputationReporter.d.ts.map +1 -0
  50. package/dist/erc8004/ReputationReporter.js +266 -0
  51. package/dist/erc8004/ReputationReporter.js.map +1 -0
  52. package/dist/erc8004/index.d.ts +36 -0
  53. package/dist/erc8004/index.d.ts.map +1 -0
  54. package/dist/erc8004/index.js +46 -0
  55. package/dist/erc8004/index.js.map +1 -0
  56. package/dist/index.d.ts +5 -0
  57. package/dist/index.d.ts.map +1 -1
  58. package/dist/index.js +21 -2
  59. package/dist/index.js.map +1 -1
  60. package/dist/protocol/ACTPKernel.d.ts +1 -1
  61. package/dist/protocol/ACTPKernel.d.ts.map +1 -1
  62. package/dist/protocol/ACTPKernel.js +16 -7
  63. package/dist/protocol/ACTPKernel.js.map +1 -1
  64. package/dist/runtime/BlockchainRuntime.d.ts.map +1 -1
  65. package/dist/runtime/BlockchainRuntime.js +2 -0
  66. package/dist/runtime/BlockchainRuntime.js.map +1 -1
  67. package/dist/runtime/IACTPRuntime.d.ts +6 -0
  68. package/dist/runtime/IACTPRuntime.d.ts.map +1 -1
  69. package/dist/runtime/MockRuntime.d.ts +12 -0
  70. package/dist/runtime/MockRuntime.d.ts.map +1 -1
  71. package/dist/runtime/MockRuntime.js +41 -0
  72. package/dist/runtime/MockRuntime.js.map +1 -1
  73. package/dist/runtime/types/MockState.d.ts +6 -0
  74. package/dist/runtime/types/MockState.d.ts.map +1 -1
  75. package/dist/runtime/types/MockState.js.map +1 -1
  76. package/dist/types/adapter.d.ts +359 -0
  77. package/dist/types/adapter.d.ts.map +1 -0
  78. package/dist/types/adapter.js +115 -0
  79. package/dist/types/adapter.js.map +1 -0
  80. package/dist/types/erc8004.d.ts +184 -0
  81. package/dist/types/erc8004.d.ts.map +1 -0
  82. package/dist/types/erc8004.js +132 -0
  83. package/dist/types/erc8004.js.map +1 -0
  84. package/dist/types/index.d.ts +3 -0
  85. package/dist/types/index.d.ts.map +1 -1
  86. package/dist/types/index.js +3 -0
  87. package/dist/types/index.js.map +1 -1
  88. package/dist/types/transaction.d.ts +12 -0
  89. package/dist/types/transaction.d.ts.map +1 -1
  90. package/dist/types/x402.d.ts +162 -0
  91. package/dist/types/x402.d.ts.map +1 -0
  92. package/dist/types/x402.js +162 -0
  93. package/dist/types/x402.js.map +1 -0
  94. package/package.json +3 -2
  95. package/src/ACTPClient.ts +318 -2
  96. package/src/abi/ACTPKernel.json +16 -0
  97. package/src/adapters/AdapterRegistry.ts +173 -0
  98. package/src/adapters/AdapterRouter.ts +417 -0
  99. package/src/adapters/BaseAdapter.ts +25 -0
  100. package/src/adapters/BasicAdapter.ts +199 -3
  101. package/src/adapters/IAdapter.ts +292 -0
  102. package/src/adapters/StandardAdapter.ts +220 -1
  103. package/src/adapters/X402Adapter.ts +653 -0
  104. package/src/adapters/index.ts +27 -0
  105. package/src/cli/commands/init.ts +208 -3
  106. package/src/config/networks.ts +3 -3
  107. package/src/erc8004/ERC8004Bridge.ts +461 -0
  108. package/src/erc8004/ReputationReporter.ts +472 -0
  109. package/src/erc8004/index.ts +61 -0
  110. package/src/index.ts +43 -0
  111. package/src/protocol/ACTPKernel.ts +26 -7
  112. package/src/runtime/BlockchainRuntime.ts +2 -0
  113. package/src/runtime/IACTPRuntime.ts +6 -0
  114. package/src/runtime/MockRuntime.ts +42 -0
  115. package/src/runtime/types/MockState.ts +7 -0
  116. package/src/types/adapter.ts +296 -0
  117. package/src/types/erc8004.ts +293 -0
  118. package/src/types/index.ts +3 -0
  119. package/src/types/transaction.ts +12 -0
  120. package/src/types/x402.ts +219 -0
@@ -8,6 +8,8 @@
8
8
  */
9
9
 
10
10
  import * as crypto from 'crypto';
11
+ import * as fs from 'fs';
12
+ import * as path from 'path';
11
13
  import { Command } from 'commander';
12
14
  import {
13
15
  saveConfig,
@@ -30,6 +32,10 @@ export function createInitCommand(): Command {
30
32
  .option('-m, --mode <mode>', 'Operating mode: mock, testnet, mainnet', 'mock')
31
33
  .option('-a, --address <address>', 'Your Ethereum address')
32
34
  .option('-f, --force', 'Overwrite existing configuration')
35
+ .option('--scaffold', 'Generate a starter agent.ts file')
36
+ .option('--intent <intent>', 'Agent intent: earn, pay, or both (default: earn)')
37
+ .option('--service <name>', 'Service name (default: my-service)')
38
+ .option('--price <usdc>', 'Base price in USDC (default: 1)')
33
39
  .option('--json', 'Output as JSON')
34
40
  .option('-q, --quiet', 'Minimal output')
35
41
  .action(async (options) => {
@@ -55,10 +61,16 @@ export function createInitCommand(): Command {
55
61
  // Implementation
56
62
  // ============================================================================
57
63
 
64
+ type ScaffoldIntent = 'earn' | 'pay' | 'both';
65
+
58
66
  interface InitOptions {
59
67
  mode: string;
60
68
  address?: string;
61
69
  force?: boolean;
70
+ scaffold?: boolean;
71
+ intent?: string;
72
+ service?: string;
73
+ price?: string;
62
74
  }
63
75
 
64
76
  async function runInit(options: InitOptions, output: Output): Promise<void> {
@@ -151,11 +163,204 @@ async function runInit(options: InitOptions, output: Output): Promise<void> {
151
163
  { quietKey: 'address' }
152
164
  );
153
165
 
166
+ // Generate scaffold if requested
167
+ if (options.scaffold) {
168
+ await runScaffold(options, mode, output);
169
+ } else {
170
+ output.blank();
171
+ output.print('Next steps:');
172
+ output.print(' 1. Create a payment: actp pay <provider> <amount>');
173
+ output.print(' 2. Check your balance: actp balance');
174
+ output.print(' 3. List transactions: actp tx list');
175
+ output.print('');
176
+ output.print('Tip: Use --scaffold to generate a starter agent.ts');
177
+ }
178
+ }
179
+
180
+ // ============================================================================
181
+ // Scaffold
182
+ // ============================================================================
183
+
184
+ async function runScaffold(
185
+ options: InitOptions,
186
+ mode: CLIMode,
187
+ output: Output,
188
+ ): Promise<void> {
189
+ const validIntents: ScaffoldIntent[] = ['earn', 'pay', 'both'];
190
+ const intent: ScaffoldIntent = (options.intent as ScaffoldIntent) || 'earn';
191
+
192
+ if (!validIntents.includes(intent)) {
193
+ throw new Error(
194
+ `Invalid intent: "${options.intent}". Valid intents: ${validIntents.join(', ')}`
195
+ );
196
+ }
197
+
198
+ const service = options.service || 'my-service';
199
+ const price = options.price || '1';
200
+ const agentFile = path.join(process.cwd(), 'agent.ts');
201
+
202
+ // Check if file already exists
203
+ if (fs.existsSync(agentFile) && !options.force) {
204
+ output.warning('agent.ts already exists. Use --force to overwrite.');
205
+ return;
206
+ }
207
+
208
+ // Derive agent name from directory
209
+ const agentName = path.basename(process.cwd());
210
+
211
+ // Get template and substitute variables
212
+ const template = getTemplate(intent);
213
+ const content = template
214
+ .replace(/\{\{service\}\}/g, service)
215
+ .replace(/\{\{mode\}\}/g, mode)
216
+ .replace(/\{\{price\}\}/g, price)
217
+ .replace(/\{\{name\}\}/g, agentName);
218
+
219
+ // Atomic write
220
+ const tempFile = `${agentFile}.tmp`;
221
+ try {
222
+ fs.writeFileSync(tempFile, content, 'utf-8');
223
+ fs.renameSync(tempFile, agentFile);
224
+ } catch (error) {
225
+ if (fs.existsSync(tempFile)) {
226
+ try { fs.unlinkSync(tempFile); } catch { /* ignore */ }
227
+ }
228
+ throw error;
229
+ }
230
+
231
+ output.success(`Generated agent.ts (intent: ${intent})`);
232
+
233
+ // Generate tsconfig.json if it doesn't exist
234
+ const tsconfigFile = path.join(process.cwd(), 'tsconfig.json');
235
+ if (!fs.existsSync(tsconfigFile)) {
236
+ const tsconfigContent = JSON.stringify({
237
+ compilerOptions: {
238
+ target: 'ES2022',
239
+ module: 'ES2022',
240
+ moduleResolution: 'bundler',
241
+ esModuleInterop: true,
242
+ strict: true,
243
+ outDir: 'dist',
244
+ skipLibCheck: true,
245
+ },
246
+ include: ['*.ts'],
247
+ }, null, 2);
248
+
249
+ const tsconfigTemp = `${tsconfigFile}.tmp`;
250
+ try {
251
+ fs.writeFileSync(tsconfigTemp, tsconfigContent, 'utf-8');
252
+ fs.renameSync(tsconfigTemp, tsconfigFile);
253
+ output.success('Generated tsconfig.json');
254
+ } catch {
255
+ output.warning('Could not generate tsconfig.json');
256
+ }
257
+ }
258
+
259
+ // Check package.json for type: module
260
+ const pkgFile = path.join(process.cwd(), 'package.json');
261
+ if (fs.existsSync(pkgFile)) {
262
+ try {
263
+ const pkg = JSON.parse(fs.readFileSync(pkgFile, 'utf-8'));
264
+ if (pkg.type !== 'module') {
265
+ output.warning(
266
+ 'package.json has type: "' + (pkg.type || 'commonjs') + '". ' +
267
+ 'Set "type": "module" for ESM support, or run with: npx ts-node --esm agent.ts'
268
+ );
269
+ }
270
+ } catch { /* ignore parse errors */ }
271
+ }
272
+
154
273
  output.blank();
155
274
  output.print('Next steps:');
156
- output.print(' 1. Create a payment: actp pay <provider> <amount>');
157
- output.print(' 2. Check your balance: actp balance');
158
- output.print(' 3. List transactions: actp tx list');
275
+ output.print(' 1. Edit agent.ts with your logic');
276
+ output.print(' 2. Run: npx ts-node --esm agent.ts');
277
+ output.print(' 3. Check balance: actp balance');
278
+ }
279
+
280
+ function getTemplate(intent: ScaffoldIntent): string {
281
+ switch (intent) {
282
+ case 'earn':
283
+ return TEMPLATE_EARN;
284
+ case 'pay':
285
+ return TEMPLATE_PAY;
286
+ case 'both':
287
+ return TEMPLATE_BOTH;
288
+ }
289
+ }
290
+
291
+ // ============================================================================
292
+ // Templates
293
+ // ============================================================================
294
+
295
+ const TEMPLATE_EARN = `import { provide } from '@agirails/sdk';
296
+
297
+ const provider = provide('{{service}}', async (job) => {
298
+ console.log(\`Job received: \${job.id} (\${job.budget} USDC)\`);
299
+
300
+ // TODO: Replace with your actual work
301
+ const result = await processJob(job.input);
302
+
303
+ return result;
304
+ }, {
305
+ network: '{{mode}}',
306
+ filter: { minBudget: {{price}} },
307
+ });
308
+
309
+ async function processJob(input: any): Promise<any> {
310
+ // Your logic here
311
+ return { status: 'completed', output: input };
312
+ }
313
+
314
+ console.log(\`Provider listening for '{{service}}' jobs...\`);
315
+ `;
316
+
317
+ const TEMPLATE_PAY = `import { request } from '@agirails/sdk';
318
+
319
+ async function main() {
320
+ const { result, transaction } = await request('{{service}}', {
321
+ provider: '0xPROVIDER_ADDRESS', // replace with the provider's address
322
+ input: { /* your data here */ },
323
+ budget: {{price}},
324
+ network: '{{mode}}',
325
+ });
326
+
327
+ console.log('Result:', result);
328
+ console.log('Transaction:', transaction.id);
329
+ console.log('Fee:', transaction.fee, 'USDC');
330
+ }
331
+
332
+ main().catch(console.error);
333
+ `;
334
+
335
+ const TEMPLATE_BOTH = `import { Agent } from '@agirails/sdk';
336
+
337
+ async function main() {
338
+ const agent = new Agent({
339
+ name: '{{name}}',
340
+ network: '{{mode}}',
341
+ behavior: {
342
+ autoAccept: true,
343
+ concurrency: 10,
344
+ },
345
+ });
346
+
347
+ // Provide a service
348
+ agent.provide('{{service}}', async (job, ctx) => {
349
+ ctx.progress(50, 'Working...');
350
+
351
+ // TODO: Replace with your actual work
352
+ return { status: 'completed', output: job.input };
353
+ });
354
+
355
+ agent.on('payment:received', (data) => {
356
+ console.log(\`Earned \${data.amount} USDC\`);
357
+ });
358
+
359
+ await agent.start();
360
+ console.log(\`Agent '{{name}}' running on {{mode}}\`);
159
361
  }
160
362
 
363
+ main().catch(console.error);
364
+ `;
365
+
161
366
  export { runInit };
@@ -58,9 +58,9 @@ export const BASE_SEPOLIA: NetworkConfig = {
58
58
  rpcUrl: BASE_SEPOLIA_RPC_URL,
59
59
  blockExplorer: 'https://sepolia.basescan.org',
60
60
  contracts: {
61
- // Redeployed 2025-12-10
62
- actpKernel: '0xD199070F8e9FB9a127F6Fe730Bc13300B4b3d962',
63
- escrowVault: '0x948b9Ea081C4Cec1E112Af2e539224c531d4d585',
61
+ // Redeployed 2026-02-06 with agentId support
62
+ actpKernel: '0x469CBADbACFFE096270594F0a31f0EEC53753411',
63
+ escrowVault: '0x57f888261b629bB380dfb983f5DA6c70Ff2D49E5',
64
64
  usdc: '0x444b4e1A65949AB2ac75979D5d0166Eb7A248Ccb', // MockUSDC
65
65
  // EAS contracts (Base native deployment)
66
66
  eas: '0x4200000000000000000000000000000000000021',
@@ -0,0 +1,461 @@
1
+ /**
2
+ * ERC-8004 Identity Bridge
3
+ *
4
+ * READ-ONLY access to ERC-8004 Identity Registry.
5
+ * Resolves agent IDs to wallet addresses for ACTP payments.
6
+ *
7
+ * SECURITY NOTES:
8
+ * - All operations are view functions (no gas costs)
9
+ * - Safe to call without signer
10
+ * - Caches results to minimize RPC calls
11
+ * - Handles network errors gracefully
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const bridge = new ERC8004Bridge({ network: 'base-sepolia' });
16
+ *
17
+ * // Check if agent exists
18
+ * const exists = await bridge.verifyAgent('12345');
19
+ *
20
+ * // Get wallet for payment
21
+ * const wallet = await bridge.getAgentWallet('12345');
22
+ *
23
+ * // Get full agent info
24
+ * const agent = await bridge.resolveAgent('12345');
25
+ * ```
26
+ *
27
+ * @module erc8004/ERC8004Bridge
28
+ */
29
+
30
+ import { ethers } from 'ethers';
31
+ import {
32
+ ERC8004Agent,
33
+ ERC8004AgentMetadata,
34
+ ERC8004Network,
35
+ ERC8004Error,
36
+ ERC8004ErrorCode,
37
+ ERC8004_IDENTITY_REGISTRY,
38
+ ERC8004_IDENTITY_ABI,
39
+ ERC8004_DEFAULT_RPC,
40
+ } from '../types/erc8004';
41
+
42
+ // ============================================================================
43
+ // Types
44
+ // ============================================================================
45
+
46
+ /**
47
+ * Interface for ERC-8004 Identity Registry contract.
48
+ * Used for testing with mock implementations.
49
+ */
50
+ export interface IERC8004IdentityRegistry {
51
+ ownerOf(agentId: string): Promise<string>;
52
+ getAgentURI(agentId: string): Promise<string>;
53
+ balanceOf(owner: string): Promise<bigint>;
54
+ tokenOfOwnerByIndex(owner: string, index: number): Promise<bigint>;
55
+ }
56
+
57
+ /**
58
+ * Configuration for ERC8004Bridge.
59
+ */
60
+ export interface ERC8004BridgeConfig {
61
+ /** Target network */
62
+ network: ERC8004Network;
63
+
64
+ /** Custom RPC URL (optional, uses default if not provided) */
65
+ rpcUrl?: string;
66
+
67
+ /** Override registry address (optional, for testing) */
68
+ registryAddress?: string;
69
+
70
+ /** Custom fetch function (optional, for testing) */
71
+ fetchFn?: typeof fetch;
72
+
73
+ /** Cache TTL in milliseconds (default: 60000 = 1 minute) */
74
+ cacheTimeMs?: number;
75
+
76
+ /** Metadata fetch timeout in milliseconds (default: 10000 = 10 seconds) */
77
+ metadataTimeoutMs?: number;
78
+
79
+ /**
80
+ * Injected contract instance (optional, for testing).
81
+ * If provided, skips creating a real ethers Contract.
82
+ * @internal
83
+ */
84
+ _testContract?: IERC8004IdentityRegistry;
85
+ }
86
+
87
+ /**
88
+ * Cached agent entry.
89
+ */
90
+ interface CachedAgent {
91
+ agent: ERC8004Agent;
92
+ expiresAt: number;
93
+ }
94
+
95
+ // ============================================================================
96
+ // ERC8004Bridge
97
+ // ============================================================================
98
+
99
+ /**
100
+ * Bridge for reading from ERC-8004 Identity Registry.
101
+ *
102
+ * Provides methods to:
103
+ * - Verify agent existence
104
+ * - Get agent wallet address for payments
105
+ * - Resolve full agent info including metadata
106
+ * - List agents owned by an address
107
+ */
108
+ export class ERC8004Bridge {
109
+ private readonly provider: ethers.JsonRpcProvider | null;
110
+ private readonly registry: IERC8004IdentityRegistry;
111
+ private readonly cache: Map<string, CachedAgent>;
112
+ private readonly fetchFn: typeof fetch;
113
+ private readonly cacheTimeMs: number;
114
+ private readonly metadataTimeoutMs: number;
115
+ private readonly network: ERC8004Network;
116
+
117
+ constructor(config: ERC8004BridgeConfig) {
118
+ this.network = config.network;
119
+ this.fetchFn = config.fetchFn ?? fetch;
120
+ this.cacheTimeMs = config.cacheTimeMs ?? 60000;
121
+ this.metadataTimeoutMs = config.metadataTimeoutMs ?? 10000;
122
+ this.cache = new Map();
123
+
124
+ // Use injected contract for testing
125
+ if (config._testContract) {
126
+ this.provider = null;
127
+ this.registry = config._testContract;
128
+ return;
129
+ }
130
+
131
+ // Setup provider
132
+ const rpcUrl = config.rpcUrl ?? ERC8004_DEFAULT_RPC[config.network];
133
+ this.provider = new ethers.JsonRpcProvider(rpcUrl);
134
+
135
+ // Setup registry contract
136
+ const registryAddress =
137
+ config.registryAddress ?? ERC8004_IDENTITY_REGISTRY[config.network];
138
+
139
+ if (registryAddress === ethers.ZeroAddress) {
140
+ console.warn(
141
+ `[ERC8004] Registry not deployed on ${config.network}. Using zero address.`
142
+ );
143
+ }
144
+
145
+ this.registry = new ethers.Contract(
146
+ registryAddress,
147
+ ERC8004_IDENTITY_ABI,
148
+ this.provider
149
+ ) as unknown as IERC8004IdentityRegistry;
150
+ }
151
+
152
+ // ==========================================================================
153
+ // Public Methods
154
+ // ==========================================================================
155
+
156
+ /**
157
+ * Verify agent exists in ERC-8004 Identity Registry.
158
+ *
159
+ * Safe to call frequently - uses cache and is a view function.
160
+ *
161
+ * @param agentId - ERC-8004 agent ID (uint256 as string)
162
+ * @returns true if agent exists, false otherwise
163
+ */
164
+ async verifyAgent(agentId: string): Promise<boolean> {
165
+ // Validate format first
166
+ if (!this.isValidAgentId(agentId)) {
167
+ return false;
168
+ }
169
+
170
+ // Check cache
171
+ const cached = this.cache.get(agentId);
172
+ if (cached && cached.expiresAt > Date.now()) {
173
+ return true;
174
+ }
175
+
176
+ try {
177
+ const owner = await this.registry.ownerOf(agentId);
178
+ return owner !== ethers.ZeroAddress;
179
+ } catch {
180
+ // ownerOf reverts for non-existent tokens (ERC-721 standard)
181
+ return false;
182
+ }
183
+ }
184
+
185
+ /**
186
+ * Get wallet address for receiving payments.
187
+ *
188
+ * Wallet priority:
189
+ * 1. metadata.paymentAddress (explicit payment destination)
190
+ * 2. metadata.wallet (alternative field name)
191
+ * 3. owner address (fallback)
192
+ *
193
+ * @param agentId - ERC-8004 agent ID
194
+ * @returns Checksummed wallet address
195
+ * @throws ERC8004Error if agent not found
196
+ */
197
+ async getAgentWallet(agentId: string): Promise<string> {
198
+ const agent = await this.resolveAgent(agentId);
199
+ return agent.wallet;
200
+ }
201
+
202
+ /**
203
+ * Resolve full agent info from ERC-8004.
204
+ *
205
+ * Fetches on-chain data (owner, agentURI) and off-chain metadata.
206
+ * Results are cached for cacheTimeMs.
207
+ *
208
+ * @param agentId - ERC-8004 agent ID
209
+ * @returns Full agent info including metadata
210
+ * @throws ERC8004Error if agent not found or invalid ID
211
+ */
212
+ async resolveAgent(agentId: string): Promise<ERC8004Agent> {
213
+ // Check cache first
214
+ const cached = this.cache.get(agentId);
215
+ if (cached && cached.expiresAt > Date.now()) {
216
+ return cached.agent;
217
+ }
218
+
219
+ // Validate format
220
+ if (!this.isValidAgentId(agentId)) {
221
+ throw new ERC8004Error(
222
+ `Invalid agent ID format: "${agentId}"`,
223
+ ERC8004ErrorCode.INVALID_AGENT_ID,
224
+ agentId
225
+ );
226
+ }
227
+
228
+ // Fetch on-chain data
229
+ let owner: string;
230
+ let agentURI: string;
231
+
232
+ try {
233
+ [owner, agentURI] = await Promise.all([
234
+ this.registry.ownerOf(agentId),
235
+ this.registry.getAgentURI(agentId),
236
+ ]);
237
+ } catch (error) {
238
+ // Check if it's a "nonexistent token" error
239
+ const isNotFound =
240
+ error instanceof Error &&
241
+ (error.message.includes('nonexistent') ||
242
+ error.message.includes('ERC721') ||
243
+ error.message.includes('invalid token'));
244
+
245
+ if (isNotFound) {
246
+ throw new ERC8004Error(
247
+ `Agent ${agentId} not found in ERC-8004 registry`,
248
+ ERC8004ErrorCode.AGENT_NOT_FOUND,
249
+ agentId
250
+ );
251
+ }
252
+
253
+ throw new ERC8004Error(
254
+ `Failed to fetch agent ${agentId}: ${error instanceof Error ? error.message : 'Unknown error'}`,
255
+ ERC8004ErrorCode.NETWORK_ERROR,
256
+ agentId,
257
+ error instanceof Error ? error : undefined
258
+ );
259
+ }
260
+
261
+ // Check owner is valid
262
+ if (owner === ethers.ZeroAddress) {
263
+ throw new ERC8004Error(
264
+ `Agent ${agentId} not found in ERC-8004 registry`,
265
+ ERC8004ErrorCode.AGENT_NOT_FOUND,
266
+ agentId
267
+ );
268
+ }
269
+
270
+ // Fetch metadata (may return undefined if fetch fails)
271
+ const metadata = await this.fetchMetadata(agentURI, agentId);
272
+
273
+ // Determine wallet address
274
+ // Priority: paymentAddress > wallet > owner
275
+ let walletAddress = owner;
276
+
277
+ if (metadata?.paymentAddress && this.isValidAddress(metadata.paymentAddress)) {
278
+ walletAddress = metadata.paymentAddress;
279
+ } else if (metadata?.wallet && this.isValidAddress(metadata.wallet)) {
280
+ walletAddress = metadata.wallet;
281
+ }
282
+
283
+ // Build agent object
284
+ const agent: ERC8004Agent = {
285
+ agentId,
286
+ owner: ethers.getAddress(owner), // Checksummed
287
+ wallet: ethers.getAddress(walletAddress), // Checksummed
288
+ agentURI,
289
+ metadata,
290
+ network: this.network,
291
+ };
292
+
293
+ // Cache result
294
+ this.cache.set(agentId, {
295
+ agent,
296
+ expiresAt: Date.now() + this.cacheTimeMs,
297
+ });
298
+
299
+ return agent;
300
+ }
301
+
302
+ /**
303
+ * Get all agent IDs owned by an address.
304
+ *
305
+ * @param owner - Owner address to query
306
+ * @returns Array of agent IDs (may be empty)
307
+ */
308
+ async getAgentsByOwner(owner: string): Promise<string[]> {
309
+ if (!this.isValidAddress(owner)) {
310
+ return [];
311
+ }
312
+
313
+ try {
314
+ const balance = await this.registry.balanceOf(owner);
315
+ const balanceNum = Number(balance);
316
+
317
+ if (balanceNum === 0) {
318
+ return [];
319
+ }
320
+
321
+ const agentIds: string[] = [];
322
+
323
+ for (let i = 0; i < balanceNum; i++) {
324
+ const agentId = await this.registry.tokenOfOwnerByIndex(owner, i);
325
+ agentIds.push(agentId.toString());
326
+ }
327
+
328
+ return agentIds;
329
+ } catch (error) {
330
+ console.warn(`[ERC8004] Failed to get agents for owner ${owner}:`, error);
331
+ return [];
332
+ }
333
+ }
334
+
335
+ /**
336
+ * Clear all cached entries.
337
+ * Useful for testing or forcing fresh data.
338
+ */
339
+ clearCache(): void {
340
+ this.cache.clear();
341
+ }
342
+
343
+ /**
344
+ * Get cache statistics (for debugging).
345
+ */
346
+ getCacheStats(): { size: number; network: ERC8004Network } {
347
+ return {
348
+ size: this.cache.size,
349
+ network: this.network,
350
+ };
351
+ }
352
+
353
+ // ==========================================================================
354
+ // Private Methods
355
+ // ==========================================================================
356
+
357
+ /**
358
+ * Validate agent ID format.
359
+ * Agent IDs are uint256 values (0 to 2^256-1).
360
+ */
361
+ private isValidAgentId(agentId: string): boolean {
362
+ if (!agentId || typeof agentId !== 'string') {
363
+ return false;
364
+ }
365
+
366
+ // Must not look like an Ethereum address or URL
367
+ if (agentId.startsWith('0x') || agentId.includes('://')) {
368
+ return false;
369
+ }
370
+
371
+ try {
372
+ const bn = BigInt(agentId);
373
+ return bn >= 0n && bn < 2n ** 256n;
374
+ } catch {
375
+ return false;
376
+ }
377
+ }
378
+
379
+ /**
380
+ * Validate Ethereum address format.
381
+ */
382
+ private isValidAddress(address: string): boolean {
383
+ try {
384
+ ethers.getAddress(address);
385
+ return true;
386
+ } catch {
387
+ return false;
388
+ }
389
+ }
390
+
391
+ /**
392
+ * Fetch and parse metadata from agentURI.
393
+ *
394
+ * Handles:
395
+ * - IPFS URIs (ipfs://...)
396
+ * - HTTPS URIs
397
+ * - Timeout protection
398
+ * - Parse errors
399
+ *
400
+ * Returns undefined on any failure (never throws).
401
+ */
402
+ private async fetchMetadata(
403
+ agentURI: string,
404
+ agentId: string
405
+ ): Promise<ERC8004AgentMetadata | undefined> {
406
+ if (!agentURI) {
407
+ return undefined;
408
+ }
409
+
410
+ try {
411
+ // Convert IPFS URI to HTTP gateway
412
+ let url = agentURI;
413
+ if (url.startsWith('ipfs://')) {
414
+ const cid = url.slice(7);
415
+ url = `https://ipfs.io/ipfs/${cid}`;
416
+ }
417
+
418
+ // Validate URL
419
+ if (!url.startsWith('http://') && !url.startsWith('https://')) {
420
+ console.warn(`[ERC8004] Invalid agentURI scheme for ${agentId}: ${url}`);
421
+ return undefined;
422
+ }
423
+
424
+ // Fetch with timeout
425
+ const controller = new AbortController();
426
+ const timeoutId = setTimeout(() => controller.abort(), this.metadataTimeoutMs);
427
+
428
+ try {
429
+ const response = await this.fetchFn(url, {
430
+ headers: { Accept: 'application/json' },
431
+ signal: controller.signal,
432
+ });
433
+
434
+ clearTimeout(timeoutId);
435
+
436
+ if (!response.ok) {
437
+ console.warn(
438
+ `[ERC8004] Metadata fetch failed for ${agentId}: HTTP ${response.status}`
439
+ );
440
+ return undefined;
441
+ }
442
+
443
+ const data = await response.json();
444
+ return data as ERC8004AgentMetadata;
445
+ } finally {
446
+ clearTimeout(timeoutId);
447
+ }
448
+ } catch (error) {
449
+ // Log but don't throw - metadata is optional
450
+ const errorMessage =
451
+ error instanceof Error
452
+ ? error.name === 'AbortError'
453
+ ? 'timeout'
454
+ : error.message
455
+ : 'unknown error';
456
+
457
+ console.warn(`[ERC8004] Metadata fetch failed for ${agentId}: ${errorMessage}`);
458
+ return undefined;
459
+ }
460
+ }
461
+ }