@agirails/sdk 2.0.4 → 2.2.1

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 (201) hide show
  1. package/README.md +536 -87
  2. package/dist/ACTPClient.d.ts +200 -0
  3. package/dist/ACTPClient.d.ts.map +1 -1
  4. package/dist/ACTPClient.js +266 -2
  5. package/dist/ACTPClient.js.map +1 -1
  6. package/dist/abi/ACTPKernel.json +16 -0
  7. package/dist/adapters/AdapterRegistry.d.ts +140 -0
  8. package/dist/adapters/AdapterRegistry.d.ts.map +1 -0
  9. package/dist/adapters/AdapterRegistry.js +166 -0
  10. package/dist/adapters/AdapterRegistry.js.map +1 -0
  11. package/dist/adapters/AdapterRouter.d.ts +165 -0
  12. package/dist/adapters/AdapterRouter.d.ts.map +1 -0
  13. package/dist/adapters/AdapterRouter.js +350 -0
  14. package/dist/adapters/AdapterRouter.js.map +1 -0
  15. package/dist/adapters/BaseAdapter.d.ts +17 -0
  16. package/dist/adapters/BaseAdapter.d.ts.map +1 -1
  17. package/dist/adapters/BaseAdapter.js +21 -0
  18. package/dist/adapters/BaseAdapter.js.map +1 -1
  19. package/dist/adapters/BasicAdapter.d.ts +72 -3
  20. package/dist/adapters/BasicAdapter.d.ts.map +1 -1
  21. package/dist/adapters/BasicAdapter.js +178 -2
  22. package/dist/adapters/BasicAdapter.js.map +1 -1
  23. package/dist/adapters/IAdapter.d.ts +230 -0
  24. package/dist/adapters/IAdapter.d.ts.map +1 -0
  25. package/dist/adapters/IAdapter.js +44 -0
  26. package/dist/adapters/IAdapter.js.map +1 -0
  27. package/dist/adapters/StandardAdapter.d.ts +80 -6
  28. package/dist/adapters/StandardAdapter.d.ts.map +1 -1
  29. package/dist/adapters/StandardAdapter.js +203 -6
  30. package/dist/adapters/StandardAdapter.js.map +1 -1
  31. package/dist/adapters/X402Adapter.d.ts +208 -0
  32. package/dist/adapters/X402Adapter.d.ts.map +1 -0
  33. package/dist/adapters/X402Adapter.js +423 -0
  34. package/dist/adapters/X402Adapter.js.map +1 -0
  35. package/dist/adapters/index.d.ts +8 -0
  36. package/dist/adapters/index.d.ts.map +1 -1
  37. package/dist/adapters/index.js +19 -1
  38. package/dist/adapters/index.js.map +1 -1
  39. package/dist/cli/commands/init.d.ts +4 -0
  40. package/dist/cli/commands/init.d.ts.map +1 -1
  41. package/dist/cli/commands/init.js +146 -4
  42. package/dist/cli/commands/init.js.map +1 -1
  43. package/dist/config/networks.d.ts +9 -0
  44. package/dist/config/networks.d.ts.map +1 -1
  45. package/dist/config/networks.js +27 -12
  46. package/dist/config/networks.js.map +1 -1
  47. package/dist/erc8004/ERC8004Bridge.d.ts +155 -0
  48. package/dist/erc8004/ERC8004Bridge.d.ts.map +1 -0
  49. package/dist/erc8004/ERC8004Bridge.js +325 -0
  50. package/dist/erc8004/ERC8004Bridge.js.map +1 -0
  51. package/dist/erc8004/ReputationReporter.d.ts +223 -0
  52. package/dist/erc8004/ReputationReporter.d.ts.map +1 -0
  53. package/dist/erc8004/ReputationReporter.js +266 -0
  54. package/dist/erc8004/ReputationReporter.js.map +1 -0
  55. package/dist/erc8004/index.d.ts +36 -0
  56. package/dist/erc8004/index.d.ts.map +1 -0
  57. package/dist/erc8004/index.js +46 -0
  58. package/dist/erc8004/index.js.map +1 -0
  59. package/dist/index.d.ts +11 -1
  60. package/dist/index.d.ts.map +1 -1
  61. package/dist/index.js +51 -2
  62. package/dist/index.js.map +1 -1
  63. package/dist/level0/provide.d.ts.map +1 -1
  64. package/dist/level0/provide.js +2 -1
  65. package/dist/level0/provide.js.map +1 -1
  66. package/dist/level1/Agent.d.ts.map +1 -1
  67. package/dist/level1/Agent.js +11 -3
  68. package/dist/level1/Agent.js.map +1 -1
  69. package/dist/protocol/ACTPKernel.d.ts +1 -1
  70. package/dist/protocol/ACTPKernel.d.ts.map +1 -1
  71. package/dist/protocol/ACTPKernel.js +23 -12
  72. package/dist/protocol/ACTPKernel.js.map +1 -1
  73. package/dist/protocol/DIDResolver.js +1 -1
  74. package/dist/protocol/DIDResolver.js.map +1 -1
  75. package/dist/protocol/EASHelper.d.ts.map +1 -1
  76. package/dist/protocol/EASHelper.js +2 -3
  77. package/dist/protocol/EASHelper.js.map +1 -1
  78. package/dist/protocol/MessageSigner.d.ts.map +1 -1
  79. package/dist/protocol/MessageSigner.js +8 -8
  80. package/dist/protocol/MessageSigner.js.map +1 -1
  81. package/dist/runtime/BlockchainRuntime.d.ts +7 -0
  82. package/dist/runtime/BlockchainRuntime.d.ts.map +1 -1
  83. package/dist/runtime/BlockchainRuntime.js +40 -22
  84. package/dist/runtime/BlockchainRuntime.js.map +1 -1
  85. package/dist/runtime/IACTPRuntime.d.ts +21 -0
  86. package/dist/runtime/IACTPRuntime.d.ts.map +1 -1
  87. package/dist/runtime/MockRuntime.d.ts +19 -0
  88. package/dist/runtime/MockRuntime.d.ts.map +1 -1
  89. package/dist/runtime/MockRuntime.js +56 -4
  90. package/dist/runtime/MockRuntime.js.map +1 -1
  91. package/dist/runtime/types/MockState.d.ts +11 -2
  92. package/dist/runtime/types/MockState.d.ts.map +1 -1
  93. package/dist/runtime/types/MockState.js.map +1 -1
  94. package/dist/storage/ArchiveBundleBuilder.d.ts +150 -0
  95. package/dist/storage/ArchiveBundleBuilder.d.ts.map +1 -0
  96. package/dist/storage/ArchiveBundleBuilder.js +468 -0
  97. package/dist/storage/ArchiveBundleBuilder.js.map +1 -0
  98. package/dist/storage/ArweaveClient.d.ts +271 -0
  99. package/dist/storage/ArweaveClient.d.ts.map +1 -0
  100. package/dist/storage/ArweaveClient.js +761 -0
  101. package/dist/storage/ArweaveClient.js.map +1 -0
  102. package/dist/storage/FilebaseClient.d.ts +193 -0
  103. package/dist/storage/FilebaseClient.d.ts.map +1 -0
  104. package/dist/storage/FilebaseClient.js +643 -0
  105. package/dist/storage/FilebaseClient.js.map +1 -0
  106. package/dist/storage/index.d.ts +47 -0
  107. package/dist/storage/index.d.ts.map +1 -0
  108. package/dist/storage/index.js +64 -0
  109. package/dist/storage/index.js.map +1 -0
  110. package/dist/storage/types.d.ts +291 -0
  111. package/dist/storage/types.d.ts.map +1 -0
  112. package/dist/storage/types.js +18 -0
  113. package/dist/storage/types.js.map +1 -0
  114. package/dist/types/adapter.d.ts +359 -0
  115. package/dist/types/adapter.d.ts.map +1 -0
  116. package/dist/types/adapter.js +115 -0
  117. package/dist/types/adapter.js.map +1 -0
  118. package/dist/types/erc8004.d.ts +184 -0
  119. package/dist/types/erc8004.d.ts.map +1 -0
  120. package/dist/types/erc8004.js +132 -0
  121. package/dist/types/erc8004.js.map +1 -0
  122. package/dist/types/index.d.ts +3 -0
  123. package/dist/types/index.d.ts.map +1 -1
  124. package/dist/types/index.js +3 -0
  125. package/dist/types/index.js.map +1 -1
  126. package/dist/types/state.d.ts +5 -4
  127. package/dist/types/state.d.ts.map +1 -1
  128. package/dist/types/state.js +10 -9
  129. package/dist/types/state.js.map +1 -1
  130. package/dist/types/transaction.d.ts +12 -0
  131. package/dist/types/transaction.d.ts.map +1 -1
  132. package/dist/types/x402.d.ts +162 -0
  133. package/dist/types/x402.d.ts.map +1 -0
  134. package/dist/types/x402.js +162 -0
  135. package/dist/types/x402.js.map +1 -0
  136. package/dist/utils/IPFSClient.d.ts.map +1 -1
  137. package/dist/utils/IPFSClient.js +5 -2
  138. package/dist/utils/IPFSClient.js.map +1 -1
  139. package/dist/utils/NonceManager.d.ts.map +1 -1
  140. package/dist/utils/NonceManager.js +3 -2
  141. package/dist/utils/NonceManager.js.map +1 -1
  142. package/dist/utils/UsedAttestationTracker.d.ts.map +1 -1
  143. package/dist/utils/UsedAttestationTracker.js +3 -3
  144. package/dist/utils/UsedAttestationTracker.js.map +1 -1
  145. package/dist/utils/circuitBreaker.d.ts +136 -0
  146. package/dist/utils/circuitBreaker.d.ts.map +1 -0
  147. package/dist/utils/circuitBreaker.js +253 -0
  148. package/dist/utils/circuitBreaker.js.map +1 -0
  149. package/dist/utils/retry.d.ts +120 -0
  150. package/dist/utils/retry.d.ts.map +1 -0
  151. package/dist/utils/retry.js +260 -0
  152. package/dist/utils/retry.js.map +1 -0
  153. package/dist/utils/validation.d.ts +100 -0
  154. package/dist/utils/validation.d.ts.map +1 -1
  155. package/dist/utils/validation.js +248 -1
  156. package/dist/utils/validation.js.map +1 -1
  157. package/package.json +16 -3
  158. package/src/ACTPClient.ts +318 -2
  159. package/src/abi/ACTPKernel.json +16 -0
  160. package/src/adapters/AdapterRegistry.ts +173 -0
  161. package/src/adapters/AdapterRouter.ts +417 -0
  162. package/src/adapters/BaseAdapter.ts +25 -0
  163. package/src/adapters/BasicAdapter.ts +210 -3
  164. package/src/adapters/IAdapter.ts +292 -0
  165. package/src/adapters/StandardAdapter.ts +246 -7
  166. package/src/adapters/X402Adapter.ts +653 -0
  167. package/src/adapters/index.ts +27 -0
  168. package/src/cli/commands/init.ts +166 -3
  169. package/src/config/networks.ts +36 -12
  170. package/src/erc8004/ERC8004Bridge.ts +461 -0
  171. package/src/erc8004/ReputationReporter.ts +472 -0
  172. package/src/erc8004/index.ts +61 -0
  173. package/src/index.ts +97 -0
  174. package/src/level0/provide.ts +2 -1
  175. package/src/level1/Agent.ts +13 -3
  176. package/src/protocol/ACTPKernel.ts +33 -12
  177. package/src/protocol/DIDResolver.ts +1 -1
  178. package/src/protocol/EASHelper.ts +2 -5
  179. package/src/protocol/MessageSigner.ts +8 -14
  180. package/src/runtime/BlockchainRuntime.ts +41 -45
  181. package/src/runtime/IACTPRuntime.ts +22 -0
  182. package/src/runtime/MockRuntime.ts +58 -4
  183. package/src/runtime/types/MockState.ts +12 -2
  184. package/src/storage/ArchiveBundleBuilder.ts +563 -0
  185. package/src/storage/ArweaveClient.ts +945 -0
  186. package/src/storage/FilebaseClient.ts +790 -0
  187. package/src/storage/index.ts +96 -0
  188. package/src/storage/types.ts +348 -0
  189. package/src/types/adapter.ts +296 -0
  190. package/src/types/erc8004.ts +293 -0
  191. package/src/types/index.ts +3 -0
  192. package/src/types/state.ts +10 -9
  193. package/src/types/transaction.ts +12 -0
  194. package/src/types/x402.ts +219 -0
  195. package/src/utils/IPFSClient.ts +5 -4
  196. package/src/utils/NonceManager.ts +3 -2
  197. package/src/utils/UsedAttestationTracker.ts +3 -5
  198. package/src/utils/circuitBreaker.ts +324 -0
  199. package/src/utils/fsSafe.ts +5 -0
  200. package/src/utils/retry.ts +365 -0
  201. package/src/utils/validation.ts +295 -1
@@ -0,0 +1,472 @@
1
+ /**
2
+ * ERC-8004 Reputation Reporter
3
+ *
4
+ * WRITES to ERC-8004 Reputation Registry after ACTP settlements.
5
+ *
6
+ * CRITICAL SECURITY NOTES:
7
+ * 1. Requires signer (pays gas) - typically the requester
8
+ * 2. Caller MUST NOT be agent owner (ERC-8004 restriction)
9
+ * 3. Uses feedbackHash = keccak256(txId) for replay protection
10
+ * 4. Failures are logged but NEVER block ACTP settlement
11
+ * 5. Local dedup prevents double-reporting in same session
12
+ *
13
+ * WHEN TO CALL:
14
+ * - reportSettlement(): After successful release() (state = SETTLED)
15
+ * - reportDispute(): After dispute resolution
16
+ *
17
+ * FAILURE HANDLING:
18
+ * - All methods return null on failure (never throw)
19
+ * - Settlement/dispute already succeeded - reputation is bonus
20
+ * - Log warnings for debugging
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * const reporter = new ReputationReporter({
25
+ * network: 'base-sepolia',
26
+ * signer: requesterWallet,
27
+ * });
28
+ *
29
+ * // After successful release
30
+ * const txHash = await reporter.reportSettlement({
31
+ * agentId: '12345',
32
+ * txId: result.txId,
33
+ * capability: 'code_generation',
34
+ * });
35
+ *
36
+ * if (txHash) {
37
+ * console.log('Reputation reported:', txHash);
38
+ * } else {
39
+ * console.warn('Report failed (settlement still succeeded)');
40
+ * }
41
+ * ```
42
+ *
43
+ * @module erc8004/ReputationReporter
44
+ */
45
+
46
+ import { ethers, Signer } from 'ethers';
47
+ import {
48
+ ERC8004Network,
49
+ ERC8004_REPUTATION_REGISTRY,
50
+ ERC8004_REPUTATION_ABI,
51
+ ACTP_FEEDBACK_TAGS,
52
+ } from '../types/erc8004';
53
+
54
+ // ============================================================================
55
+ // Types
56
+ // ============================================================================
57
+
58
+ /**
59
+ * Transaction receipt interface for reputation reports.
60
+ */
61
+ interface TransactionReceipt {
62
+ hash: string;
63
+ blockNumber: number;
64
+ gasUsed: bigint;
65
+ }
66
+
67
+ /**
68
+ * Transaction response interface.
69
+ */
70
+ interface TransactionResponse {
71
+ wait(): Promise<TransactionReceipt>;
72
+ }
73
+
74
+ /**
75
+ * Interface for ERC-8004 Reputation Registry contract.
76
+ * Used for testing with mock implementations.
77
+ */
78
+ export interface IERC8004ReputationRegistry {
79
+ giveFeedback(
80
+ agentId: string,
81
+ value: number,
82
+ valueDecimals: number,
83
+ tag1: string,
84
+ tag2: string,
85
+ endpoint: string,
86
+ feedbackURI: string,
87
+ feedbackHash: string,
88
+ txOptions?: { gasLimit?: bigint }
89
+ ): Promise<TransactionResponse>;
90
+
91
+ getSummary(
92
+ agentId: string,
93
+ clientAddresses: string[],
94
+ tag1: string,
95
+ tag2: string
96
+ ): Promise<[bigint, bigint, number]>;
97
+
98
+ revokeLatest(agentId: string, feedbackIndex: bigint): Promise<TransactionResponse>;
99
+ }
100
+
101
+ /**
102
+ * Configuration for ReputationReporter.
103
+ */
104
+ export interface ReputationReporterConfig {
105
+ /** Target network */
106
+ network: ERC8004Network;
107
+
108
+ /** Signer for transactions (pays gas) */
109
+ signer: Signer;
110
+
111
+ /** Override registry address (optional, for testing) */
112
+ registryAddress?: string;
113
+
114
+ /** Gas limit override (optional) */
115
+ gasLimit?: bigint;
116
+
117
+ /**
118
+ * Injected contract instance (optional, for testing).
119
+ * If provided, skips creating a real ethers Contract.
120
+ * @internal
121
+ */
122
+ _testContract?: IERC8004ReputationRegistry;
123
+ }
124
+
125
+ /**
126
+ * Parameters for reporting a successful settlement.
127
+ */
128
+ export interface ReportSettlementParams {
129
+ /** ERC-8004 agent ID */
130
+ agentId: string;
131
+
132
+ /** ACTP transaction ID (used for replay protection) */
133
+ txId: string;
134
+
135
+ /** Agent capability (e.g., 'code_generation', 'data_analysis') */
136
+ capability?: string;
137
+
138
+ /** Service endpoint (optional) */
139
+ endpoint?: string;
140
+
141
+ /** Link to transaction details (optional, IPFS or HTTPS) */
142
+ feedbackURI?: string;
143
+ }
144
+
145
+ /**
146
+ * Parameters for reporting a dispute resolution.
147
+ */
148
+ export interface ReportDisputeParams {
149
+ /** ERC-8004 agent ID */
150
+ agentId: string;
151
+
152
+ /** ACTP transaction ID */
153
+ txId: string;
154
+
155
+ /** true if agent won the dispute, false if requester won */
156
+ agentWon: boolean;
157
+
158
+ /** Agent capability (optional) */
159
+ capability?: string;
160
+
161
+ /** Dispute reason/details (optional, stored as feedbackURI) */
162
+ reason?: string;
163
+ }
164
+
165
+ /**
166
+ * Result of a reputation report.
167
+ */
168
+ export interface ReportResult {
169
+ /** Transaction hash */
170
+ txHash: string;
171
+
172
+ /** Block number */
173
+ blockNumber: number;
174
+
175
+ /** Gas used */
176
+ gasUsed: bigint;
177
+ }
178
+
179
+ // ============================================================================
180
+ // ReputationReporter
181
+ // ============================================================================
182
+
183
+ /**
184
+ * Reporter for submitting ACTP settlement outcomes to ERC-8004 Reputation.
185
+ *
186
+ * Designed to never block or fail the main ACTP flow.
187
+ * All errors are caught and logged, returning null on failure.
188
+ */
189
+ export class ReputationReporter {
190
+ private readonly registry: IERC8004ReputationRegistry;
191
+ private readonly signer: Signer;
192
+ private readonly network: ERC8004Network;
193
+ private readonly gasLimit?: bigint;
194
+
195
+ /**
196
+ * Track reported txIds to prevent double-reporting in same session.
197
+ * Note: This is in-memory only, so restarts allow re-reporting.
198
+ * On-chain, feedbackHash provides permanent dedup.
199
+ */
200
+ private readonly reportedTxIds: Set<string> = new Set();
201
+
202
+ constructor(config: ReputationReporterConfig) {
203
+ this.network = config.network;
204
+ this.signer = config.signer;
205
+ this.gasLimit = config.gasLimit;
206
+
207
+ // Use injected contract for testing
208
+ if (config._testContract) {
209
+ this.registry = config._testContract;
210
+ return;
211
+ }
212
+
213
+ const registryAddress =
214
+ config.registryAddress ?? ERC8004_REPUTATION_REGISTRY[config.network];
215
+
216
+ if (registryAddress === ethers.ZeroAddress) {
217
+ console.warn(
218
+ `[ERC8004] Reputation Registry not deployed on ${config.network}. ` +
219
+ 'Reports will fail.'
220
+ );
221
+ }
222
+
223
+ this.registry = new ethers.Contract(
224
+ registryAddress,
225
+ ERC8004_REPUTATION_ABI,
226
+ config.signer
227
+ ) as unknown as IERC8004ReputationRegistry;
228
+ }
229
+
230
+ // ==========================================================================
231
+ // Public Methods
232
+ // ==========================================================================
233
+
234
+ /**
235
+ * Report successful ACTP settlement to ERC-8004 Reputation.
236
+ *
237
+ * Call this AFTER release() succeeds. Failure here does NOT affect settlement.
238
+ *
239
+ * Reports:
240
+ * - value: 1 (positive feedback)
241
+ * - valueDecimals: 0 (binary)
242
+ * - tag1: 'actp_settled'
243
+ * - tag2: capability
244
+ * - feedbackHash: keccak256(txId)
245
+ *
246
+ * @param params - Settlement details
247
+ * @returns ReportResult if successful, null if failed
248
+ */
249
+ async reportSettlement(params: ReportSettlementParams): Promise<ReportResult | null> {
250
+ const {
251
+ agentId,
252
+ txId,
253
+ capability = '',
254
+ endpoint = '',
255
+ feedbackURI = '',
256
+ } = params;
257
+
258
+ // Local dedup check
259
+ if (this.reportedTxIds.has(txId)) {
260
+ console.warn(`[ERC8004] Already reported txId in this session: ${txId}`);
261
+ return null;
262
+ }
263
+
264
+ try {
265
+ // Compute feedbackHash for on-chain replay protection
266
+ const feedbackHash = ethers.keccak256(ethers.toUtf8Bytes(txId));
267
+
268
+ // Prepare transaction options
269
+ const txOptions: { gasLimit?: bigint } = {};
270
+ if (this.gasLimit) {
271
+ txOptions.gasLimit = this.gasLimit;
272
+ }
273
+
274
+ // Submit feedback
275
+ const tx = await this.registry.giveFeedback(
276
+ agentId,
277
+ 1, // value: 1 = success
278
+ 0, // decimals: 0 (binary)
279
+ ACTP_FEEDBACK_TAGS.SETTLED, // tag1: 'actp_settled'
280
+ capability, // tag2: capability
281
+ endpoint,
282
+ feedbackURI,
283
+ feedbackHash,
284
+ txOptions
285
+ );
286
+
287
+ // Wait for confirmation
288
+ const receipt = await tx.wait();
289
+
290
+ // Mark as reported
291
+ this.reportedTxIds.add(txId);
292
+
293
+ return {
294
+ txHash: receipt.hash,
295
+ blockNumber: receipt.blockNumber,
296
+ gasUsed: receipt.gasUsed,
297
+ };
298
+ } catch (error) {
299
+ // Log but NEVER throw - settlement already succeeded
300
+ this.logError('reportSettlement', agentId, txId, error);
301
+ return null;
302
+ }
303
+ }
304
+
305
+ /**
306
+ * Report ACTP dispute resolution to ERC-8004 Reputation.
307
+ *
308
+ * Call this after dispute is resolved (either party won).
309
+ *
310
+ * Reports:
311
+ * - value: 1 if agent won, -1 if requester won
312
+ * - valueDecimals: 0 (binary)
313
+ * - tag1: 'actp_dispute_won' or 'actp_dispute_lost'
314
+ * - tag2: capability
315
+ * - feedbackHash: keccak256(txId)
316
+ *
317
+ * @param params - Dispute details
318
+ * @returns ReportResult if successful, null if failed
319
+ */
320
+ async reportDispute(params: ReportDisputeParams): Promise<ReportResult | null> {
321
+ const { agentId, txId, agentWon, capability = '', reason = '' } = params;
322
+
323
+ // Local dedup check
324
+ if (this.reportedTxIds.has(txId)) {
325
+ console.warn(`[ERC8004] Already reported txId in this session: ${txId}`);
326
+ return null;
327
+ }
328
+
329
+ try {
330
+ const feedbackHash = ethers.keccak256(ethers.toUtf8Bytes(txId));
331
+
332
+ // Determine value and tag based on outcome
333
+ const value = agentWon ? 1 : -1;
334
+ const tag1 = agentWon
335
+ ? ACTP_FEEDBACK_TAGS.DISPUTE_WON
336
+ : ACTP_FEEDBACK_TAGS.DISPUTE_LOST;
337
+
338
+ const txOptions: { gasLimit?: bigint } = {};
339
+ if (this.gasLimit) {
340
+ txOptions.gasLimit = this.gasLimit;
341
+ }
342
+
343
+ const tx = await this.registry.giveFeedback(
344
+ agentId,
345
+ value,
346
+ 0, // decimals: 0 (binary)
347
+ tag1,
348
+ capability,
349
+ '', // endpoint
350
+ reason, // feedbackURI (contains dispute reason)
351
+ feedbackHash,
352
+ txOptions
353
+ );
354
+
355
+ const receipt = await tx.wait();
356
+ this.reportedTxIds.add(txId);
357
+
358
+ return {
359
+ txHash: receipt.hash,
360
+ blockNumber: receipt.blockNumber,
361
+ gasUsed: receipt.gasUsed,
362
+ };
363
+ } catch (error) {
364
+ this.logError('reportDispute', agentId, txId, error);
365
+ return null;
366
+ }
367
+ }
368
+
369
+ /**
370
+ * Get reputation summary for an agent.
371
+ *
372
+ * This is a READ operation (no gas cost).
373
+ *
374
+ * @param agentId - ERC-8004 agent ID
375
+ * @param tag1 - Filter by tag1 (optional, e.g., 'actp_settled')
376
+ * @returns Summary if successful, null if failed
377
+ */
378
+ async getAgentReputation(
379
+ agentId: string,
380
+ tag1?: string
381
+ ): Promise<{ count: number; score: number } | null> {
382
+ try {
383
+ const [count, summaryValue] = await this.registry.getSummary(
384
+ agentId,
385
+ [], // clientAddresses (empty = all)
386
+ tag1 ?? '',
387
+ '' // tag2
388
+ );
389
+
390
+ return {
391
+ count: Number(count),
392
+ score: Number(summaryValue),
393
+ };
394
+ } catch (error) {
395
+ console.error(
396
+ `[ERC8004] getAgentReputation failed for ${agentId}:`,
397
+ error instanceof Error ? error.message : error
398
+ );
399
+ return null;
400
+ }
401
+ }
402
+
403
+ /**
404
+ * Check if a transaction was already reported in this session.
405
+ *
406
+ * Note: This is local-only. For on-chain check, you'd need to
407
+ * query the feedbackHash mapping (not exposed in standard interface).
408
+ *
409
+ * @param txId - ACTP transaction ID
410
+ */
411
+ isReported(txId: string): boolean {
412
+ return this.reportedTxIds.has(txId);
413
+ }
414
+
415
+ /**
416
+ * Clear local dedup cache.
417
+ * Use when starting new session or for testing.
418
+ */
419
+ clearReportedCache(): void {
420
+ this.reportedTxIds.clear();
421
+ }
422
+
423
+ /**
424
+ * Get reporter statistics.
425
+ */
426
+ getStats(): { network: ERC8004Network; reportedCount: number } {
427
+ return {
428
+ network: this.network,
429
+ reportedCount: this.reportedTxIds.size,
430
+ };
431
+ }
432
+
433
+ // ==========================================================================
434
+ // Private Methods
435
+ // ==========================================================================
436
+
437
+ /**
438
+ * Log error in consistent format.
439
+ * NEVER throws - errors are informational only.
440
+ */
441
+ private logError(
442
+ method: string,
443
+ agentId: string,
444
+ txId: string,
445
+ error: unknown
446
+ ): void {
447
+ const errorMessage =
448
+ error instanceof Error ? error.message : String(error);
449
+
450
+ // Check for common error cases
451
+ if (errorMessage.includes('insufficient funds')) {
452
+ console.error(
453
+ `[ERC8004] ${method} failed for agent ${agentId}: ` +
454
+ 'Insufficient funds for gas. Signer needs ETH/native token.'
455
+ );
456
+ } else if (errorMessage.includes('cannot be the agent owner')) {
457
+ console.error(
458
+ `[ERC8004] ${method} failed for agent ${agentId}: ` +
459
+ 'Caller is agent owner. ERC-8004 requires different address.'
460
+ );
461
+ } else if (errorMessage.includes('user rejected')) {
462
+ console.warn(
463
+ `[ERC8004] ${method} cancelled by user for agent ${agentId}`
464
+ );
465
+ } else {
466
+ console.error(
467
+ `[ERC8004] ${method} failed for agent ${agentId} (tx: ${txId}): ` +
468
+ errorMessage
469
+ );
470
+ }
471
+ }
472
+ }
@@ -0,0 +1,61 @@
1
+ /**
2
+ * ERC-8004 Trustless Agents Integration
3
+ *
4
+ * Provides integration with ERC-8004 standard for:
5
+ * - Agent identity resolution (Identity Registry)
6
+ * - Reputation reporting (Reputation Registry)
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import {
11
+ * ERC8004Bridge,
12
+ * ReputationReporter,
13
+ * ERC8004Error,
14
+ * } from '@agirails/sdk/erc8004';
15
+ *
16
+ * // Read agent info
17
+ * const bridge = new ERC8004Bridge({ network: 'base-sepolia' });
18
+ * const agent = await bridge.resolveAgent('12345');
19
+ *
20
+ * // Report settlement
21
+ * const reporter = new ReputationReporter({
22
+ * network: 'base-sepolia',
23
+ * signer: wallet,
24
+ * });
25
+ * await reporter.reportSettlement({ agentId: '12345', txId });
26
+ * ```
27
+ *
28
+ * @module erc8004
29
+ */
30
+
31
+ // Bridge (read-only)
32
+ export { ERC8004Bridge } from './ERC8004Bridge';
33
+ export type { ERC8004BridgeConfig } from './ERC8004Bridge';
34
+
35
+ // Reporter (write)
36
+ export { ReputationReporter } from './ReputationReporter';
37
+ export type {
38
+ ReputationReporterConfig,
39
+ ReportSettlementParams,
40
+ ReportDisputeParams,
41
+ ReportResult,
42
+ } from './ReputationReporter';
43
+
44
+ // Re-export types from types/erc8004
45
+ export type {
46
+ ERC8004Network,
47
+ ERC8004Agent,
48
+ ERC8004AgentMetadata,
49
+ ReputationFeedback,
50
+ ReputationSummary,
51
+ ACTPFeedbackTag,
52
+ } from '../types/erc8004';
53
+
54
+ export {
55
+ ERC8004Error,
56
+ ERC8004ErrorCode,
57
+ ERC8004_IDENTITY_REGISTRY,
58
+ ERC8004_REPUTATION_REGISTRY,
59
+ ERC8004_DEFAULT_RPC,
60
+ ACTP_FEEDBACK_TAGS,
61
+ } from '../types/erc8004';
package/src/index.ts CHANGED
@@ -64,6 +64,20 @@ export {
64
64
  StandardTransactionParams,
65
65
  } from './adapters/StandardAdapter';
66
66
 
67
+ export { AdapterRegistry } from './adapters/AdapterRegistry';
68
+
69
+ export {
70
+ AdapterRouter,
71
+ AdapterSelectionResult,
72
+ } from './adapters/AdapterRouter';
73
+
74
+ export {
75
+ IAdapter,
76
+ TransactionStatus,
77
+ AdapterTransactionState,
78
+ isAdapter,
79
+ } from './adapters/IAdapter';
80
+
67
81
  // =============================================================================
68
82
  // Runtime Layer - Protocol Implementation
69
83
  // =============================================================================
@@ -217,6 +231,75 @@ export type {
217
231
  PriceCalculation,
218
232
  } from './level1';
219
233
 
234
+ // =============================================================================
235
+ // Storage Layer (AIP-7 §4) - Hybrid IPFS + Arweave
236
+ // =============================================================================
237
+
238
+ // Storage clients
239
+ export { FilebaseClient } from './storage/FilebaseClient';
240
+ export { ArweaveClient } from './storage/ArweaveClient';
241
+
242
+ // Archive bundle builder
243
+ export {
244
+ ArchiveBundleBuilder,
245
+ computeContentHash,
246
+ validateArchiveBundle,
247
+ } from './storage/ArchiveBundleBuilder';
248
+
249
+ // Storage types
250
+ export type {
251
+ FilebaseConfig,
252
+ ArweaveConfig,
253
+ IrysCurrency,
254
+ IrysNetwork,
255
+ ArchiveBundle,
256
+ ArchiveChainId,
257
+ ArchiveFinalState,
258
+ ArchiveParticipants,
259
+ ArchiveReferences,
260
+ ArchiveHashes,
261
+ ArchiveSignatures,
262
+ ArchiveAttestation,
263
+ ArchiveSettlement,
264
+ EscrowRelease,
265
+ ArchiveTags,
266
+ IPFSUploadResult,
267
+ ArweaveUploadResult,
268
+ DownloadResult,
269
+ } from './storage/types';
270
+
271
+ // Storage constants
272
+ export { ARCHIVE_BUNDLE_TYPE } from './storage/types';
273
+
274
+ // =============================================================================
275
+ // ERC-8004 Integration
276
+ // =============================================================================
277
+
278
+ export {
279
+ ERC8004Bridge,
280
+ ReputationReporter,
281
+ ERC8004Error,
282
+ ERC8004ErrorCode,
283
+ ERC8004_IDENTITY_REGISTRY,
284
+ ERC8004_REPUTATION_REGISTRY,
285
+ ERC8004_DEFAULT_RPC,
286
+ ACTP_FEEDBACK_TAGS,
287
+ } from './erc8004';
288
+
289
+ export type {
290
+ ERC8004BridgeConfig,
291
+ ReputationReporterConfig,
292
+ ReportSettlementParams,
293
+ ReportDisputeParams,
294
+ ReportResult,
295
+ ERC8004Network,
296
+ ERC8004Agent,
297
+ ERC8004AgentMetadata,
298
+ ReputationFeedback,
299
+ ReputationSummary,
300
+ ACTPFeedbackTag,
301
+ } from './erc8004';
302
+
220
303
  // =============================================================================
221
304
  // Enhanced Error Exports
222
305
  // =============================================================================
@@ -241,4 +324,18 @@ export {
241
324
  DisputeRaisedError,
242
325
  ServiceConfigError,
243
326
  AgentLifecycleError,
327
+ // Storage errors (AIP-7)
328
+ StorageError,
329
+ StorageAuthenticationError,
330
+ StorageRateLimitError,
331
+ UploadTimeoutError,
332
+ DownloadTimeoutError,
333
+ ContentNotFoundError,
334
+ FileSizeLimitExceededError,
335
+ InvalidCIDError,
336
+ ArweaveUploadError,
337
+ ArweaveDownloadError,
338
+ ArweaveTimeoutError,
339
+ InvalidArweaveTxIdError,
340
+ InsufficientBalanceError as StorageInsufficientBalanceError,
244
341
  } from './errors';
@@ -12,6 +12,7 @@ import { JobHandler } from '../level1/types/Job';
12
12
  import { ProvideOptions } from '../level1/types/Options';
13
13
  import { Provider } from './Provider';
14
14
  import { serviceDirectory } from './ServiceDirectory';
15
+ import { sdkLogger } from '../utils/Logger';
15
16
 
16
17
  /**
17
18
  * Provide a service
@@ -83,7 +84,7 @@ export function provide(
83
84
  // Register in service directory after agent has started and has an address
84
85
  serviceDirectory.register(service, agent.address);
85
86
  }).catch((error) => {
86
- console.error(`Failed to start provider for ${service}:`, error);
87
+ sdkLogger.error(`Failed to start provider for ${service}`, { error: error instanceof Error ? error.message : String(error) });
87
88
  });
88
89
 
89
90
  // Return Provider interface (adapter over Agent)
@@ -1232,7 +1232,7 @@ export class Agent extends EventEmitter {
1232
1232
  result, // Include original result for convenience
1233
1233
  });
1234
1234
 
1235
- // Transition transaction to DELIVERED state
1235
+ // Transition transaction through IN_PROGRESS → DELIVERED states
1236
1236
  if (this._client) {
1237
1237
  // Store delivery proof by directly accessing MockRuntime's state
1238
1238
  // This is a workaround - in production, we'd use a proper method
@@ -1246,8 +1246,18 @@ export class Agent extends EventEmitter {
1246
1246
  });
1247
1247
  }
1248
1248
 
1249
- // Transition to DELIVERED (escrow was already linked in pollForJobs)
1250
- await this._client.runtime.transitionState(job.id, 'DELIVERED');
1249
+ // AUDIT FIX (2026-02): Must transition through IN_PROGRESS before DELIVERED
1250
+ // Contract rejects COMMITTED → DELIVERED direct transition
1251
+ await this._client.runtime.transitionState(job.id, 'IN_PROGRESS');
1252
+
1253
+ // Encode dispute window proof for DELIVERED transition
1254
+ // Use transaction's disputeWindow from metadata, fallback to 2 days (172800s) per Options.ts default
1255
+ const disputeWindowSeconds = job.metadata?.disputeWindow || 172800;
1256
+ const abiCoder = ethers.AbiCoder.defaultAbiCoder();
1257
+ const disputeWindowProof = abiCoder.encode(['uint256'], [disputeWindowSeconds]);
1258
+
1259
+ // Transition to DELIVERED with dispute window proof
1260
+ await this._client.runtime.transitionState(job.id, 'DELIVERED', disputeWindowProof);
1251
1261
  }
1252
1262
 
1253
1263
  // SECURITY FIX (C-2): Remove from active jobs on SUCCESS