@agirails/sdk 2.0.3 → 2.2.0

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 (151) hide show
  1. package/README.md +536 -87
  2. package/dist/adapters/BaseAdapter.js +2 -2
  3. package/dist/adapters/BaseAdapter.js.map +1 -1
  4. package/dist/adapters/BasicAdapter.d.ts.map +1 -1
  5. package/dist/adapters/BasicAdapter.js +8 -0
  6. package/dist/adapters/BasicAdapter.js.map +1 -1
  7. package/dist/adapters/StandardAdapter.d.ts +10 -5
  8. package/dist/adapters/StandardAdapter.d.ts.map +1 -1
  9. package/dist/adapters/StandardAdapter.js +19 -6
  10. package/dist/adapters/StandardAdapter.js.map +1 -1
  11. package/dist/builders/QuoteBuilder.js +1 -1
  12. package/dist/builders/QuoteBuilder.js.map +1 -1
  13. package/dist/cli/commands/config.js +1 -1
  14. package/dist/cli/commands/config.js.map +1 -1
  15. package/dist/cli/commands/simulate.js.map +1 -1
  16. package/dist/cli/commands/time.d.ts.map +1 -1
  17. package/dist/cli/commands/time.js.map +1 -1
  18. package/dist/config/networks.d.ts +9 -0
  19. package/dist/config/networks.d.ts.map +1 -1
  20. package/dist/config/networks.js +25 -10
  21. package/dist/config/networks.js.map +1 -1
  22. package/dist/index.d.ts +6 -1
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +31 -1
  25. package/dist/index.js.map +1 -1
  26. package/dist/level0/provide.d.ts.map +1 -1
  27. package/dist/level0/provide.js +2 -1
  28. package/dist/level0/provide.js.map +1 -1
  29. package/dist/level0/request.d.ts.map +1 -1
  30. package/dist/level0/request.js +1 -2
  31. package/dist/level0/request.js.map +1 -1
  32. package/dist/level1/Agent.d.ts.map +1 -1
  33. package/dist/level1/Agent.js +11 -3
  34. package/dist/level1/Agent.js.map +1 -1
  35. package/dist/level1/pricing/PriceCalculator.js +1 -1
  36. package/dist/level1/pricing/PriceCalculator.js.map +1 -1
  37. package/dist/level1/types/Options.d.ts.map +1 -1
  38. package/dist/protocol/ACTPKernel.d.ts.map +1 -1
  39. package/dist/protocol/ACTPKernel.js +7 -5
  40. package/dist/protocol/ACTPKernel.js.map +1 -1
  41. package/dist/protocol/DIDResolver.js +1 -1
  42. package/dist/protocol/DIDResolver.js.map +1 -1
  43. package/dist/protocol/EASHelper.d.ts.map +1 -1
  44. package/dist/protocol/EASHelper.js +2 -3
  45. package/dist/protocol/EASHelper.js.map +1 -1
  46. package/dist/protocol/MessageSigner.d.ts.map +1 -1
  47. package/dist/protocol/MessageSigner.js +9 -9
  48. package/dist/protocol/MessageSigner.js.map +1 -1
  49. package/dist/protocol/ProofGenerator.d.ts.map +1 -1
  50. package/dist/protocol/ProofGenerator.js +1 -0
  51. package/dist/protocol/ProofGenerator.js.map +1 -1
  52. package/dist/runtime/BlockchainRuntime.d.ts +10 -3
  53. package/dist/runtime/BlockchainRuntime.d.ts.map +1 -1
  54. package/dist/runtime/BlockchainRuntime.js +41 -25
  55. package/dist/runtime/BlockchainRuntime.js.map +1 -1
  56. package/dist/runtime/IACTPRuntime.d.ts +15 -0
  57. package/dist/runtime/IACTPRuntime.d.ts.map +1 -1
  58. package/dist/runtime/MockRuntime.d.ts +7 -0
  59. package/dist/runtime/MockRuntime.d.ts.map +1 -1
  60. package/dist/runtime/MockRuntime.js +15 -4
  61. package/dist/runtime/MockRuntime.js.map +1 -1
  62. package/dist/runtime/types/MockState.d.ts +5 -2
  63. package/dist/runtime/types/MockState.d.ts.map +1 -1
  64. package/dist/runtime/types/MockState.js.map +1 -1
  65. package/dist/storage/ArchiveBundleBuilder.d.ts +150 -0
  66. package/dist/storage/ArchiveBundleBuilder.d.ts.map +1 -0
  67. package/dist/storage/ArchiveBundleBuilder.js +468 -0
  68. package/dist/storage/ArchiveBundleBuilder.js.map +1 -0
  69. package/dist/storage/ArweaveClient.d.ts +271 -0
  70. package/dist/storage/ArweaveClient.d.ts.map +1 -0
  71. package/dist/storage/ArweaveClient.js +761 -0
  72. package/dist/storage/ArweaveClient.js.map +1 -0
  73. package/dist/storage/FilebaseClient.d.ts +193 -0
  74. package/dist/storage/FilebaseClient.d.ts.map +1 -0
  75. package/dist/storage/FilebaseClient.js +643 -0
  76. package/dist/storage/FilebaseClient.js.map +1 -0
  77. package/dist/storage/index.d.ts +47 -0
  78. package/dist/storage/index.d.ts.map +1 -0
  79. package/dist/storage/index.js +64 -0
  80. package/dist/storage/index.js.map +1 -0
  81. package/dist/storage/types.d.ts +291 -0
  82. package/dist/storage/types.d.ts.map +1 -0
  83. package/dist/storage/types.js +18 -0
  84. package/dist/storage/types.js.map +1 -0
  85. package/dist/types/state.d.ts +5 -4
  86. package/dist/types/state.d.ts.map +1 -1
  87. package/dist/types/state.js +10 -9
  88. package/dist/types/state.js.map +1 -1
  89. package/dist/utils/ErrorRecoveryGuide.d.ts.map +1 -1
  90. package/dist/utils/ErrorRecoveryGuide.js +1 -2
  91. package/dist/utils/ErrorRecoveryGuide.js.map +1 -1
  92. package/dist/utils/IPFSClient.d.ts.map +1 -1
  93. package/dist/utils/IPFSClient.js +5 -2
  94. package/dist/utils/IPFSClient.js.map +1 -1
  95. package/dist/utils/NonceManager.d.ts.map +1 -1
  96. package/dist/utils/NonceManager.js +3 -2
  97. package/dist/utils/NonceManager.js.map +1 -1
  98. package/dist/utils/UsedAttestationTracker.d.ts +1 -1
  99. package/dist/utils/UsedAttestationTracker.d.ts.map +1 -1
  100. package/dist/utils/UsedAttestationTracker.js +4 -4
  101. package/dist/utils/UsedAttestationTracker.js.map +1 -1
  102. package/dist/utils/circuitBreaker.d.ts +136 -0
  103. package/dist/utils/circuitBreaker.d.ts.map +1 -0
  104. package/dist/utils/circuitBreaker.js +253 -0
  105. package/dist/utils/circuitBreaker.js.map +1 -0
  106. package/dist/utils/retry.d.ts +120 -0
  107. package/dist/utils/retry.d.ts.map +1 -0
  108. package/dist/utils/retry.js +260 -0
  109. package/dist/utils/retry.js.map +1 -0
  110. package/dist/utils/validation.d.ts +100 -0
  111. package/dist/utils/validation.d.ts.map +1 -1
  112. package/dist/utils/validation.js +248 -1
  113. package/dist/utils/validation.js.map +1 -1
  114. package/package.json +14 -2
  115. package/src/adapters/BaseAdapter.ts +2 -2
  116. package/src/adapters/BasicAdapter.ts +12 -2
  117. package/src/adapters/StandardAdapter.ts +27 -7
  118. package/src/builders/QuoteBuilder.ts +1 -1
  119. package/src/cli/commands/config.ts +1 -1
  120. package/src/cli/commands/simulate.ts +1 -1
  121. package/src/cli/commands/time.ts +1 -2
  122. package/src/config/networks.ts +34 -10
  123. package/src/index.ts +54 -0
  124. package/src/level0/provide.ts +2 -1
  125. package/src/level0/request.ts +1 -2
  126. package/src/level1/Agent.ts +15 -5
  127. package/src/level1/pricing/PriceCalculator.ts +1 -1
  128. package/src/level1/types/Options.ts +1 -1
  129. package/src/protocol/ACTPKernel.ts +7 -5
  130. package/src/protocol/DIDResolver.ts +1 -1
  131. package/src/protocol/EASHelper.ts +2 -5
  132. package/src/protocol/MessageSigner.ts +9 -15
  133. package/src/protocol/ProofGenerator.ts +1 -0
  134. package/src/runtime/BlockchainRuntime.ts +42 -48
  135. package/src/runtime/IACTPRuntime.ts +16 -0
  136. package/src/runtime/MockRuntime.ts +16 -6
  137. package/src/runtime/types/MockState.ts +5 -2
  138. package/src/storage/ArchiveBundleBuilder.ts +563 -0
  139. package/src/storage/ArweaveClient.ts +945 -0
  140. package/src/storage/FilebaseClient.ts +790 -0
  141. package/src/storage/index.ts +96 -0
  142. package/src/storage/types.ts +348 -0
  143. package/src/types/state.ts +10 -9
  144. package/src/utils/ErrorRecoveryGuide.ts +1 -2
  145. package/src/utils/IPFSClient.ts +5 -4
  146. package/src/utils/NonceManager.ts +3 -2
  147. package/src/utils/UsedAttestationTracker.ts +4 -6
  148. package/src/utils/circuitBreaker.ts +324 -0
  149. package/src/utils/fsSafe.ts +5 -0
  150. package/src/utils/retry.ts +365 -0
  151. package/src/utils/validation.ts +295 -1
@@ -0,0 +1,271 @@
1
+ /**
2
+ * ArweaveClient - Permanent Storage via Irys (AIP-7 §4.3)
3
+ *
4
+ * Provides permanent storage on Arweave via Irys (formerly Bundlr).
5
+ * Used for archiving settled transaction bundles for compliance.
6
+ *
7
+ * Security Features (Post-Audit):
8
+ * - Gateway URL whitelist (SSRF protection)
9
+ * - Download size limits (DoS protection)
10
+ * - Credential sanitization in errors
11
+ * - Retry with exponential backoff
12
+ * - Circuit breaker for gateway health tracking
13
+ *
14
+ * @module storage/ArweaveClient
15
+ *
16
+ * Key Principle: Arweave-First Write Order
17
+ * 1. Write to Arweave FIRST -> Get Arweave TX ID
18
+ * 2. THEN anchor TX ID on-chain
19
+ *
20
+ * @see AIP-7 §4.1 for invariant explanation
21
+ */
22
+ import { ArweaveConfig, IrysCurrency, IrysNetwork, ArchiveBundle, ArweaveUploadResult, DownloadResult } from './types';
23
+ /**
24
+ * ArweaveClient - Permanent storage on Arweave via Irys
25
+ *
26
+ * Used for:
27
+ * - Archiving settled transaction bundles (compliance)
28
+ * - 7-year retention requirement (Arweave guarantees 200+ years)
29
+ *
30
+ * IMPORTANT: Uses Base ETH for payments (no bridging required)
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * const client = await ArweaveClient.create({
35
+ * privateKey: process.env.ARCHIVE_UPLOADER_KEY!,
36
+ * rpcUrl: process.env.BASE_RPC_URL!
37
+ * });
38
+ *
39
+ * // Check balance
40
+ * const balance = await client.getBalance();
41
+ * console.log('Irys balance:', balance);
42
+ *
43
+ * // Fund if needed
44
+ * if (balance < 10000n) {
45
+ * await client.fund(100000n);
46
+ * }
47
+ *
48
+ * // Upload archive bundle
49
+ * const result = await client.uploadBundle(archiveBundle);
50
+ * console.log('Arweave TX ID:', result.txId);
51
+ *
52
+ * // Download archive bundle
53
+ * const downloaded = await client.downloadBundle(result.txId);
54
+ * ```
55
+ */
56
+ export declare class ArweaveClient {
57
+ private _irys;
58
+ private readonly config;
59
+ private readonly maxDownloadSize;
60
+ private readonly retryOptions;
61
+ private readonly circuitBreaker;
62
+ private readonly circuitBreakerEnabled;
63
+ private initialized;
64
+ /**
65
+ * Get initialized Irys instance (throws if not initialized)
66
+ */
67
+ private get irys();
68
+ /**
69
+ * Private constructor - use ArweaveClient.create() factory
70
+ */
71
+ private constructor();
72
+ /**
73
+ * Create and initialize ArweaveClient
74
+ *
75
+ * @param config - Arweave configuration
76
+ * @returns Initialized ArweaveClient
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * const client = await ArweaveClient.create({
81
+ * privateKey: process.env.ARCHIVE_UPLOADER_KEY!,
82
+ * rpcUrl: 'https://mainnet.base.org'
83
+ * });
84
+ * ```
85
+ */
86
+ static create(config: ArweaveConfig): Promise<ArweaveClient>;
87
+ /**
88
+ * Initialize Irys SDK connection
89
+ */
90
+ private initialize;
91
+ /**
92
+ * Fund the Irys node (required before uploading)
93
+ *
94
+ * @param amount - Amount to fund in payment currency (wei for ETH)
95
+ * @throws {ValidationError} If amount is invalid
96
+ * @throws {StorageError} If funding fails
97
+ *
98
+ * @example
99
+ * ```typescript
100
+ * // Fund with 0.001 ETH (1e15 wei)
101
+ * await client.fund(1000000000000000n);
102
+ * ```
103
+ */
104
+ fund(amount: bigint): Promise<void>;
105
+ /**
106
+ * Get current Irys balance
107
+ *
108
+ * @returns Balance in payment currency (wei for ETH)
109
+ *
110
+ * @example
111
+ * ```typescript
112
+ * const balance = await client.getBalance();
113
+ * console.log('Balance:', balance, 'wei');
114
+ * ```
115
+ */
116
+ getBalance(): Promise<bigint>;
117
+ /**
118
+ * Estimate cost of uploading data
119
+ *
120
+ * @param sizeBytes - Size of data to upload in bytes
121
+ * @returns Cost in payment currency (wei for ETH)
122
+ *
123
+ * @example
124
+ * ```typescript
125
+ * const cost = await client.estimateCost(50 * 1024); // 50KB
126
+ * console.log('Upload cost:', cost, 'wei');
127
+ * ```
128
+ */
129
+ estimateCost(sizeBytes: number): Promise<bigint>;
130
+ /**
131
+ * Upload archive bundle to Arweave (permanent storage)
132
+ *
133
+ * IMPORTANT: This is the first step in the archive flow.
134
+ * After getting the TX ID, anchor it on-chain via ArchiveTreasury.anchorArchive()
135
+ *
136
+ * @param bundle - Archive bundle to upload
137
+ * @returns Upload result with Arweave TX ID
138
+ * @throws {InsufficientBalanceError} If Irys balance is too low
139
+ * @throws {ArweaveUploadError} If upload fails
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * const result = await client.uploadBundle(bundle);
144
+ * console.log('Arweave TX ID:', result.txId);
145
+ *
146
+ * // Then anchor on-chain
147
+ * await archiveTreasury.anchorArchive(bundle.txId, result.txId);
148
+ * ```
149
+ */
150
+ uploadBundle(bundle: ArchiveBundle): Promise<ArweaveUploadResult>;
151
+ /**
152
+ * Upload arbitrary JSON to Arweave
153
+ *
154
+ * For general-purpose permanent storage (not archive bundles).
155
+ *
156
+ * @param data - JSON data to upload
157
+ * @param tags - Optional tags for discoverability
158
+ * @returns Upload result with Arweave TX ID
159
+ */
160
+ uploadJSON(data: unknown, tags?: Array<{
161
+ name: string;
162
+ value: string;
163
+ }>): Promise<ArweaveUploadResult>;
164
+ /**
165
+ * Download archive bundle from Arweave
166
+ *
167
+ * Security Features:
168
+ * - Size limit enforcement (P1-1: DoS protection)
169
+ * - Retry with exponential backoff (P1-2)
170
+ * - Centralized TX ID validation (P1-3)
171
+ * - Credential sanitization in errors (P0-2)
172
+ *
173
+ * @param txId - Arweave transaction ID
174
+ * @returns Downloaded archive bundle
175
+ * @throws {InvalidArweaveTxIdError} If TX ID format is invalid
176
+ * @throws {FileSizeLimitExceededError} If content exceeds size limit
177
+ * @throws {ArweaveDownloadError} If download fails
178
+ *
179
+ * @example
180
+ * ```typescript
181
+ * const result = await client.downloadBundle('h7Xk2...');
182
+ * console.log('Bundle:', result.data);
183
+ * ```
184
+ */
185
+ downloadBundle(txId: string): Promise<DownloadResult<ArchiveBundle>>;
186
+ /**
187
+ * Download arbitrary JSON from Arweave
188
+ *
189
+ * Security Features:
190
+ * - Size limit enforcement (P1-1: DoS protection)
191
+ * - Retry with exponential backoff (P1-2)
192
+ * - Centralized TX ID validation (P1-3)
193
+ *
194
+ * @param txId - Arweave transaction ID
195
+ * @returns Downloaded JSON data
196
+ */
197
+ downloadJSON<T = unknown>(txId: string): Promise<DownloadResult<T>>;
198
+ /**
199
+ * Check if content exists on Arweave
200
+ *
201
+ * @param txId - Arweave transaction ID
202
+ * @returns True if content exists
203
+ */
204
+ exists(txId: string): Promise<boolean>;
205
+ /**
206
+ * Get configured currency
207
+ */
208
+ getCurrency(): IrysCurrency;
209
+ /**
210
+ * Get configured network
211
+ */
212
+ getNetwork(): IrysNetwork;
213
+ /**
214
+ * Get wallet address
215
+ */
216
+ getAddress(): Promise<string>;
217
+ /**
218
+ * Validate archive bundle structure
219
+ */
220
+ private validateBundle;
221
+ /**
222
+ * Wrap promise with timeout
223
+ */
224
+ private withTimeout;
225
+ /**
226
+ * Determine if an error represents a gateway failure vs content-specific error
227
+ *
228
+ * Gateway failures (should trigger circuit breaker):
229
+ * - 5xx HTTP errors (server errors)
230
+ * - Network timeouts
231
+ * - Connection refused
232
+ * - DNS resolution failures
233
+ *
234
+ * Content-specific errors (should NOT trigger circuit breaker):
235
+ * - 404 (content not found - not gateway's fault)
236
+ * - 400 (bad request - client's fault)
237
+ * - SyntaxError (invalid JSON - content issue)
238
+ * - Invalid bundle type (content validation failed)
239
+ */
240
+ private isGatewayFailure;
241
+ /**
242
+ * Get circuit breaker status for Arweave gateway
243
+ *
244
+ * @returns Circuit breaker status or null if disabled
245
+ *
246
+ * @example
247
+ * ```typescript
248
+ * const status = client.getCircuitBreakerStatus();
249
+ * if (status && status.state === 'OPEN') {
250
+ * console.log('Gateway unhealthy, failures:', status.failures);
251
+ * }
252
+ * ```
253
+ */
254
+ getCircuitBreakerStatus(): {
255
+ state: string;
256
+ failures: number;
257
+ } | null;
258
+ /**
259
+ * Reset circuit breaker for Arweave gateway
260
+ *
261
+ * Use with caution - only reset when you're confident the gateway is healthy.
262
+ * Useful for testing or after manual verification.
263
+ *
264
+ * @example
265
+ * ```typescript
266
+ * client.resetCircuitBreaker();
267
+ * ```
268
+ */
269
+ resetCircuitBreaker(): void;
270
+ }
271
+ //# sourceMappingURL=ArweaveClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ArweaveClient.d.ts","sourceRoot":"","sources":["../../src/storage/ArweaveClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAYH,OAAO,EACL,aAAa,EACb,YAAY,EACZ,WAAW,EACX,aAAa,EACb,mBAAmB,EACnB,cAAc,EAEf,MAAM,SAAS,CAAC;AA8BjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,KAAK,CAAqB;IAClC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAkD;IACzE,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAS;IACzC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAe;IAC5C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAA+B;IAC9D,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAAU;IAChD,OAAO,CAAC,WAAW,CAAS;IAE5B;;OAEG;IACH,OAAO,KAAK,IAAI,GAKf;IAED;;OAEG;IACH,OAAO;IA6CP;;;;;;;;;;;;;OAaG;WACU,MAAM,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;IAMlE;;OAEG;YACW,UAAU;IA+BxB;;;;;;;;;;;;OAYG;IACG,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA2BzC;;;;;;;;;;OAUG;IACG,UAAU,IAAI,OAAO,CAAC,MAAM,CAAC;IAYnC;;;;;;;;;;;OAWG;IACG,YAAY,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAsBtD;;;;;;;;;;;;;;;;;;;OAmBG;IACG,YAAY,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,mBAAmB,CAAC;IAyDvE;;;;;;;;OAQG;IACG,UAAU,CACd,IAAI,EAAE,OAAO,EACb,IAAI,CAAC,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,GAC5C,OAAO,CAAC,mBAAmB,CAAC;IAoD/B;;;;;;;;;;;;;;;;;;;;OAoBG;IACG,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,aAAa,CAAC,CAAC;IA0H1E;;;;;;;;;;OAUG;IACG,YAAY,CAAC,CAAC,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;IAkHzE;;;;;OAKG;IACG,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IA2B5C;;OAEG;IACH,WAAW,IAAI,YAAY;IAI3B;;OAEG;IACH,UAAU,IAAI,WAAW;IAIzB;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,MAAM,CAAC;IAUnC;;OAEG;IACH,OAAO,CAAC,cAAc;IAwCtB;;OAEG;YACW,WAAW;IAoBzB;;;;;;;;;;;;;;OAcG;IACH,OAAO,CAAC,gBAAgB;IAqCxB;;;;;;;;;;;;OAYG;IACH,uBAAuB,IAAI;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI;IASrE;;;;;;;;;;OAUG;IACH,mBAAmB,IAAI,IAAI;CAG5B"}