@agirails/sdk 2.5.3 → 2.5.5

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 (169) hide show
  1. package/dist/ACTPClient.d.ts +18 -0
  2. package/dist/ACTPClient.d.ts.map +1 -1
  3. package/dist/ACTPClient.js +72 -23
  4. package/dist/ACTPClient.js.map +1 -1
  5. package/dist/adapters/BasicAdapter.d.ts +15 -0
  6. package/dist/adapters/BasicAdapter.d.ts.map +1 -1
  7. package/dist/adapters/BasicAdapter.js +33 -4
  8. package/dist/adapters/BasicAdapter.js.map +1 -1
  9. package/dist/adapters/StandardAdapter.d.ts +20 -3
  10. package/dist/adapters/StandardAdapter.d.ts.map +1 -1
  11. package/dist/adapters/StandardAdapter.js +90 -12
  12. package/dist/adapters/StandardAdapter.js.map +1 -1
  13. package/dist/cli/commands/publish.js +16 -4
  14. package/dist/cli/commands/publish.js.map +1 -1
  15. package/dist/cli/commands/register.js +16 -4
  16. package/dist/cli/commands/register.js.map +1 -1
  17. package/dist/cli/commands/tx.js +31 -3
  18. package/dist/cli/commands/tx.js.map +1 -1
  19. package/dist/config/networks.d.ts +10 -2
  20. package/dist/config/networks.d.ts.map +1 -1
  21. package/dist/config/networks.js +31 -22
  22. package/dist/config/networks.js.map +1 -1
  23. package/dist/level0/request.d.ts.map +1 -1
  24. package/dist/level0/request.js +2 -1
  25. package/dist/level0/request.js.map +1 -1
  26. package/dist/runtime/BlockchainRuntime.d.ts.map +1 -1
  27. package/dist/runtime/BlockchainRuntime.js +11 -5
  28. package/dist/runtime/BlockchainRuntime.js.map +1 -1
  29. package/dist/utils/IPFSClient.d.ts +3 -1
  30. package/dist/utils/IPFSClient.d.ts.map +1 -1
  31. package/dist/utils/IPFSClient.js +27 -7
  32. package/dist/utils/IPFSClient.js.map +1 -1
  33. package/dist/wallet/AutoWalletProvider.d.ts +11 -1
  34. package/dist/wallet/AutoWalletProvider.d.ts.map +1 -1
  35. package/dist/wallet/AutoWalletProvider.js +84 -19
  36. package/dist/wallet/AutoWalletProvider.js.map +1 -1
  37. package/dist/wallet/IWalletProvider.d.ts +34 -0
  38. package/dist/wallet/IWalletProvider.d.ts.map +1 -1
  39. package/dist/wallet/SmartWalletRouter.d.ts +128 -0
  40. package/dist/wallet/SmartWalletRouter.d.ts.map +1 -0
  41. package/dist/wallet/SmartWalletRouter.js +248 -0
  42. package/dist/wallet/SmartWalletRouter.js.map +1 -0
  43. package/dist/wallet/aa/DualNonceManager.d.ts +26 -1
  44. package/dist/wallet/aa/DualNonceManager.d.ts.map +1 -1
  45. package/dist/wallet/aa/DualNonceManager.js +140 -6
  46. package/dist/wallet/aa/DualNonceManager.js.map +1 -1
  47. package/package.json +3 -6
  48. package/src/ACTPClient.ts +0 -1579
  49. package/src/abi/ACTPKernel.json +0 -1356
  50. package/src/abi/AgentRegistry.json +0 -915
  51. package/src/abi/ERC20.json +0 -40
  52. package/src/abi/EscrowVault.json +0 -134
  53. package/src/abi/IdentityRegistry.json +0 -316
  54. package/src/adapters/AdapterRegistry.ts +0 -173
  55. package/src/adapters/AdapterRouter.ts +0 -416
  56. package/src/adapters/BaseAdapter.ts +0 -498
  57. package/src/adapters/BasicAdapter.ts +0 -514
  58. package/src/adapters/IAdapter.ts +0 -292
  59. package/src/adapters/StandardAdapter.ts +0 -555
  60. package/src/adapters/X402Adapter.ts +0 -731
  61. package/src/adapters/index.ts +0 -60
  62. package/src/builders/DeliveryProofBuilder.ts +0 -327
  63. package/src/builders/QuoteBuilder.ts +0 -483
  64. package/src/builders/index.ts +0 -17
  65. package/src/cli/commands/balance.ts +0 -110
  66. package/src/cli/commands/batch.ts +0 -487
  67. package/src/cli/commands/config.ts +0 -231
  68. package/src/cli/commands/deploy-check.ts +0 -364
  69. package/src/cli/commands/deploy-env.ts +0 -120
  70. package/src/cli/commands/diff.ts +0 -141
  71. package/src/cli/commands/init.ts +0 -469
  72. package/src/cli/commands/mint.ts +0 -116
  73. package/src/cli/commands/pay.ts +0 -113
  74. package/src/cli/commands/publish.ts +0 -475
  75. package/src/cli/commands/pull.ts +0 -124
  76. package/src/cli/commands/register.ts +0 -247
  77. package/src/cli/commands/simulate.ts +0 -345
  78. package/src/cli/commands/time.ts +0 -302
  79. package/src/cli/commands/tx.ts +0 -448
  80. package/src/cli/commands/watch.ts +0 -211
  81. package/src/cli/index.ts +0 -134
  82. package/src/cli/utils/client.ts +0 -252
  83. package/src/cli/utils/config.ts +0 -389
  84. package/src/cli/utils/output.ts +0 -465
  85. package/src/cli/utils/wallet.ts +0 -109
  86. package/src/config/agirailsmd.ts +0 -262
  87. package/src/config/networks.ts +0 -275
  88. package/src/config/pendingPublish.ts +0 -237
  89. package/src/config/publishPipeline.ts +0 -359
  90. package/src/config/syncOperations.ts +0 -279
  91. package/src/erc8004/ERC8004Bridge.ts +0 -462
  92. package/src/erc8004/ReputationReporter.ts +0 -468
  93. package/src/erc8004/index.ts +0 -61
  94. package/src/errors/index.ts +0 -427
  95. package/src/index.ts +0 -364
  96. package/src/level0/Provider.ts +0 -117
  97. package/src/level0/ServiceDirectory.ts +0 -131
  98. package/src/level0/index.ts +0 -10
  99. package/src/level0/provide.ts +0 -132
  100. package/src/level0/request.ts +0 -432
  101. package/src/level1/Agent.ts +0 -1426
  102. package/src/level1/index.ts +0 -10
  103. package/src/level1/pricing/PriceCalculator.ts +0 -255
  104. package/src/level1/pricing/PricingStrategy.ts +0 -198
  105. package/src/level1/types/Job.ts +0 -179
  106. package/src/level1/types/Options.ts +0 -291
  107. package/src/level1/types/index.ts +0 -8
  108. package/src/protocol/ACTPKernel.ts +0 -808
  109. package/src/protocol/AgentRegistry.ts +0 -559
  110. package/src/protocol/DIDManager.ts +0 -629
  111. package/src/protocol/DIDResolver.ts +0 -554
  112. package/src/protocol/EASHelper.ts +0 -378
  113. package/src/protocol/EscrowVault.ts +0 -255
  114. package/src/protocol/EventMonitor.ts +0 -204
  115. package/src/protocol/MessageSigner.ts +0 -510
  116. package/src/protocol/ProofGenerator.ts +0 -339
  117. package/src/protocol/QuoteBuilder.ts +0 -15
  118. package/src/registry/AgentRegistryClient.ts +0 -202
  119. package/src/runtime/BlockchainRuntime.ts +0 -1015
  120. package/src/runtime/IACTPRuntime.ts +0 -306
  121. package/src/runtime/MockRuntime.ts +0 -1298
  122. package/src/runtime/MockStateManager.ts +0 -577
  123. package/src/runtime/index.ts +0 -25
  124. package/src/runtime/types/MockState.ts +0 -237
  125. package/src/storage/ArchiveBundleBuilder.ts +0 -561
  126. package/src/storage/ArweaveClient.ts +0 -946
  127. package/src/storage/FilebaseClient.ts +0 -790
  128. package/src/storage/index.ts +0 -96
  129. package/src/storage/types.ts +0 -348
  130. package/src/types/adapter.ts +0 -310
  131. package/src/types/agent.ts +0 -79
  132. package/src/types/did.ts +0 -223
  133. package/src/types/eip712.ts +0 -175
  134. package/src/types/erc8004.ts +0 -293
  135. package/src/types/escrow.ts +0 -27
  136. package/src/types/index.ts +0 -17
  137. package/src/types/message.ts +0 -145
  138. package/src/types/state.ts +0 -87
  139. package/src/types/transaction.ts +0 -69
  140. package/src/types/x402.ts +0 -251
  141. package/src/utils/ErrorRecoveryGuide.ts +0 -676
  142. package/src/utils/Helpers.ts +0 -688
  143. package/src/utils/IPFSClient.ts +0 -368
  144. package/src/utils/Logger.ts +0 -484
  145. package/src/utils/NonceManager.ts +0 -591
  146. package/src/utils/RateLimiter.ts +0 -534
  147. package/src/utils/ReceivedNonceTracker.ts +0 -567
  148. package/src/utils/SDKLifecycle.ts +0 -416
  149. package/src/utils/SecureNonce.ts +0 -78
  150. package/src/utils/Semaphore.ts +0 -276
  151. package/src/utils/UsedAttestationTracker.ts +0 -385
  152. package/src/utils/canonicalJson.ts +0 -38
  153. package/src/utils/circuitBreaker.ts +0 -324
  154. package/src/utils/computeTypeHash.ts +0 -48
  155. package/src/utils/fsSafe.ts +0 -80
  156. package/src/utils/index.ts +0 -80
  157. package/src/utils/retry.ts +0 -364
  158. package/src/utils/security.ts +0 -418
  159. package/src/utils/validation.ts +0 -540
  160. package/src/wallet/AutoWalletProvider.ts +0 -299
  161. package/src/wallet/EOAWalletProvider.ts +0 -69
  162. package/src/wallet/IWalletProvider.ts +0 -135
  163. package/src/wallet/aa/BundlerClient.ts +0 -274
  164. package/src/wallet/aa/DualNonceManager.ts +0 -173
  165. package/src/wallet/aa/PaymasterClient.ts +0 -174
  166. package/src/wallet/aa/TransactionBatcher.ts +0 -353
  167. package/src/wallet/aa/UserOpBuilder.ts +0 -246
  168. package/src/wallet/aa/constants.ts +0 -60
  169. package/src/wallet/keystore.ts +0 -240
@@ -1,561 +0,0 @@
1
- /**
2
- * ArchiveBundleBuilder - Builder for Archive Bundles (AIP-7 §4.4)
3
- *
4
- * Provides a fluent builder pattern for constructing archive bundles
5
- * that will be stored permanently on Arweave.
6
- *
7
- * Archive bundles contain minimal metadata with cryptographic hashes
8
- * and references. Full content remains on IPFS.
9
- *
10
- * @module storage/ArchiveBundleBuilder
11
- */
12
-
13
- import { keccak256, toUtf8Bytes } from 'ethers';
14
- import { ValidationError } from '../errors';
15
- import {
16
- ArchiveBundle,
17
- ArchiveChainId,
18
- ArchiveParticipants,
19
- ArchiveReferences,
20
- ArchiveHashes,
21
- ArchiveSignatures,
22
- ArchiveAttestation,
23
- ArchiveSettlement,
24
- ARCHIVE_BUNDLE_TYPE
25
- } from './types';
26
-
27
- // ============================================================================
28
- // Constants
29
- // ============================================================================
30
-
31
- const DEFAULT_PROTOCOL_VERSION = '1.0.0';
32
- const DEFAULT_ARCHIVE_SCHEMA_VERSION = '1.0.0';
33
-
34
- // Address validation pattern
35
- const ADDRESS_PATTERN = /^0x[a-fA-F0-9]{40}$/;
36
-
37
- // Transaction ID validation pattern
38
- const TX_ID_PATTERN = /^0x[a-fA-F0-9]{64}$/;
39
-
40
- // Hash validation pattern
41
- const HASH_PATTERN = /^0x[a-fA-F0-9]{64}$/;
42
-
43
- // Signature validation pattern (65 bytes = 130 hex chars)
44
- const SIGNATURE_PATTERN = /^0x[a-fA-F0-9]{130}$/;
45
-
46
- // CID validation pattern (CIDv0 or CIDv1)
47
- const CID_PATTERN = /^(Qm[1-9A-HJ-NP-Za-km-z]{44}|b[a-z2-7]{58,})$/;
48
-
49
- // ============================================================================
50
- // ArchiveBundleBuilder Class
51
- // ============================================================================
52
-
53
- /**
54
- * ArchiveBundleBuilder - Fluent builder for archive bundles
55
- *
56
- * @example
57
- * ```typescript
58
- * const bundle = new ArchiveBundleBuilder()
59
- * .setTransactionId('0x1234...')
60
- * .setChainId(8453)
61
- * .setParticipants({
62
- * requester: '0xRequester...',
63
- * provider: '0xProvider...'
64
- * })
65
- * .setReferences({
66
- * requestCID: 'bafybei...',
67
- * deliveryCID: 'bafybei...'
68
- * })
69
- * .setHashes({
70
- * requestHash: '0xabc...',
71
- * deliveryHash: '0xdef...',
72
- * serviceHash: '0x123...'
73
- * })
74
- * .setSignatures({
75
- * providerDeliverySignature: '0xsig...'
76
- * })
77
- * .setAttestation({ easUID: '0xeas...' })
78
- * .setSettlement({
79
- * settledAt: Date.now() / 1000,
80
- * finalState: 'SETTLED',
81
- * escrowReleased: { to: '0xProvider...', amount: '100000000' },
82
- * platformFee: '1000000',
83
- * wasDisputed: false
84
- * })
85
- * .build();
86
- * ```
87
- */
88
- export class ArchiveBundleBuilder {
89
- private protocolVersion: string = DEFAULT_PROTOCOL_VERSION;
90
- private archiveSchemaVersion: string = DEFAULT_ARCHIVE_SCHEMA_VERSION;
91
- private txId?: string;
92
- private chainId?: ArchiveChainId;
93
- private archivedAt?: number;
94
- private participants?: ArchiveParticipants;
95
- private references?: ArchiveReferences;
96
- private hashes?: ArchiveHashes;
97
- private signatures?: ArchiveSignatures;
98
- private attestation?: ArchiveAttestation;
99
- private settlement?: ArchiveSettlement;
100
-
101
- /**
102
- * Create a new ArchiveBundleBuilder
103
- */
104
- constructor() {}
105
-
106
- // ==========================================================================
107
- // Version Setters
108
- // ==========================================================================
109
-
110
- /**
111
- * Set protocol version
112
- * @default '1.0.0'
113
- */
114
- setProtocolVersion(version: string): this {
115
- if (!/^\d+\.\d+\.\d+$/.test(version)) {
116
- throw new ValidationError('protocolVersion', 'Must be semver format (e.g., 1.0.0)');
117
- }
118
- this.protocolVersion = version;
119
- return this;
120
- }
121
-
122
- /**
123
- * Set archive schema version
124
- * @default '1.0.0'
125
- */
126
- setArchiveSchemaVersion(version: string): this {
127
- if (!/^\d+\.\d+\.\d+$/.test(version)) {
128
- throw new ValidationError('archiveSchemaVersion', 'Must be semver format (e.g., 1.0.0)');
129
- }
130
- this.archiveSchemaVersion = version;
131
- return this;
132
- }
133
-
134
- // ==========================================================================
135
- // Transaction Identity Setters
136
- // ==========================================================================
137
-
138
- /**
139
- * Set ACTP transaction ID (bytes32)
140
- */
141
- setTransactionId(txId: string): this {
142
- if (!TX_ID_PATTERN.test(txId)) {
143
- throw new ValidationError('txId', 'Must be bytes32 hex string (0x + 64 hex chars)');
144
- }
145
- this.txId = txId.toLowerCase();
146
- return this;
147
- }
148
-
149
- /**
150
- * Set blockchain chain ID
151
- * @param chainId - 8453 (Base Mainnet) or 84532 (Base Sepolia)
152
- */
153
- setChainId(chainId: ArchiveChainId): this {
154
- if (chainId !== 8453 && chainId !== 84532) {
155
- throw new ValidationError('chainId', 'Must be 8453 (Base Mainnet) or 84532 (Base Sepolia)');
156
- }
157
- this.chainId = chainId;
158
- return this;
159
- }
160
-
161
- /**
162
- * Set archive timestamp
163
- * @param timestamp - Unix timestamp in seconds (defaults to now)
164
- */
165
- setArchivedAt(timestamp?: number): this {
166
- const ts = timestamp ?? Math.floor(Date.now() / 1000);
167
- if (ts <= 0) {
168
- throw new ValidationError('archivedAt', 'Must be positive Unix timestamp');
169
- }
170
- this.archivedAt = ts;
171
- return this;
172
- }
173
-
174
- // ==========================================================================
175
- // Participant Setters
176
- // ==========================================================================
177
-
178
- /**
179
- * Set transaction participants
180
- */
181
- setParticipants(participants: ArchiveParticipants): this {
182
- if (!ADDRESS_PATTERN.test(participants.requester)) {
183
- throw new ValidationError('participants.requester', 'Invalid Ethereum address');
184
- }
185
- if (!ADDRESS_PATTERN.test(participants.provider)) {
186
- throw new ValidationError('participants.provider', 'Invalid Ethereum address');
187
- }
188
-
189
- this.participants = {
190
- requester: participants.requester.toLowerCase(),
191
- provider: participants.provider.toLowerCase()
192
- };
193
- return this;
194
- }
195
-
196
- // ==========================================================================
197
- // Reference Setters
198
- // ==========================================================================
199
-
200
- /**
201
- * Set IPFS CID references
202
- */
203
- setReferences(references: ArchiveReferences): this {
204
- if (!CID_PATTERN.test(references.requestCID)) {
205
- throw new ValidationError('references.requestCID', 'Invalid IPFS CID format');
206
- }
207
- if (!CID_PATTERN.test(references.deliveryCID)) {
208
- throw new ValidationError('references.deliveryCID', 'Invalid IPFS CID format');
209
- }
210
- if (references.resultCID && !CID_PATTERN.test(references.resultCID)) {
211
- throw new ValidationError('references.resultCID', 'Invalid IPFS CID format');
212
- }
213
-
214
- this.references = {
215
- requestCID: references.requestCID,
216
- deliveryCID: references.deliveryCID,
217
- ...(references.resultCID && { resultCID: references.resultCID })
218
- };
219
- return this;
220
- }
221
-
222
- // ==========================================================================
223
- // Hash Setters
224
- // ==========================================================================
225
-
226
- /**
227
- * Set cryptographic hashes
228
- */
229
- setHashes(hashes: ArchiveHashes): this {
230
- if (!HASH_PATTERN.test(hashes.requestHash)) {
231
- throw new ValidationError('hashes.requestHash', 'Invalid hash format (bytes32)');
232
- }
233
- if (!HASH_PATTERN.test(hashes.deliveryHash)) {
234
- throw new ValidationError('hashes.deliveryHash', 'Invalid hash format (bytes32)');
235
- }
236
- if (!HASH_PATTERN.test(hashes.serviceHash)) {
237
- throw new ValidationError('hashes.serviceHash', 'Invalid hash format (bytes32)');
238
- }
239
-
240
- this.hashes = {
241
- requestHash: hashes.requestHash.toLowerCase(),
242
- deliveryHash: hashes.deliveryHash.toLowerCase(),
243
- serviceHash: hashes.serviceHash.toLowerCase()
244
- };
245
- return this;
246
- }
247
-
248
- /**
249
- * Compute and set hashes from raw data
250
- *
251
- * @param request - Request metadata JSON
252
- * @param delivery - Delivery proof JSON
253
- * @param serviceHash - Service hash from transaction (already computed)
254
- */
255
- setHashesFromData(request: unknown, delivery: unknown, serviceHash: string): this {
256
- if (!HASH_PATTERN.test(serviceHash)) {
257
- throw new ValidationError('serviceHash', 'Invalid hash format (bytes32)');
258
- }
259
-
260
- // Compute canonical hashes (sorted keys JSON)
261
- const requestJson = JSON.stringify(this.sortObjectKeys(request));
262
- const deliveryJson = JSON.stringify(this.sortObjectKeys(delivery));
263
-
264
- this.hashes = {
265
- requestHash: keccak256(toUtf8Bytes(requestJson)),
266
- deliveryHash: keccak256(toUtf8Bytes(deliveryJson)),
267
- serviceHash: serviceHash.toLowerCase()
268
- };
269
-
270
- return this;
271
- }
272
-
273
- // ==========================================================================
274
- // Signature Setters
275
- // ==========================================================================
276
-
277
- /**
278
- * Set cryptographic signatures
279
- */
280
- setSignatures(signatures: ArchiveSignatures): this {
281
- if (!SIGNATURE_PATTERN.test(signatures.providerDeliverySignature)) {
282
- throw new ValidationError(
283
- 'signatures.providerDeliverySignature',
284
- 'Invalid signature format (65 bytes = 0x + 130 hex chars)'
285
- );
286
- }
287
- if (signatures.requesterSettlementSignature &&
288
- !SIGNATURE_PATTERN.test(signatures.requesterSettlementSignature)) {
289
- throw new ValidationError(
290
- 'signatures.requesterSettlementSignature',
291
- 'Invalid signature format'
292
- );
293
- }
294
-
295
- this.signatures = {
296
- providerDeliverySignature: signatures.providerDeliverySignature,
297
- ...(signatures.requesterSettlementSignature && {
298
- requesterSettlementSignature: signatures.requesterSettlementSignature
299
- })
300
- };
301
- return this;
302
- }
303
-
304
- // ==========================================================================
305
- // Attestation Setters
306
- // ==========================================================================
307
-
308
- /**
309
- * Set EAS attestation reference
310
- */
311
- setAttestation(attestation: ArchiveAttestation): this {
312
- if (!HASH_PATTERN.test(attestation.easUID)) {
313
- throw new ValidationError('attestation.easUID', 'Invalid EAS UID format (bytes32)');
314
- }
315
-
316
- this.attestation = {
317
- easUID: attestation.easUID.toLowerCase(),
318
- ...(attestation.schemaUID && { schemaUID: attestation.schemaUID.toLowerCase() })
319
- };
320
- return this;
321
- }
322
-
323
- // ==========================================================================
324
- // Settlement Setters
325
- // ==========================================================================
326
-
327
- /**
328
- * Set settlement information
329
- */
330
- setSettlement(settlement: ArchiveSettlement): this {
331
- // Validate settledAt
332
- if (settlement.settledAt <= 0) {
333
- throw new ValidationError('settlement.settledAt', 'Must be positive Unix timestamp');
334
- }
335
-
336
- // Validate finalState
337
- if (settlement.finalState !== 'SETTLED' && settlement.finalState !== 'CANCELLED') {
338
- throw new ValidationError('settlement.finalState', 'Must be SETTLED or CANCELLED');
339
- }
340
-
341
- // Validate escrowReleased
342
- if (!ADDRESS_PATTERN.test(settlement.escrowReleased.to)) {
343
- throw new ValidationError('settlement.escrowReleased.to', 'Invalid Ethereum address');
344
- }
345
- if (!/^\d+$/.test(settlement.escrowReleased.amount)) {
346
- throw new ValidationError(
347
- 'settlement.escrowReleased.amount',
348
- 'Must be numeric string (USDC base units)'
349
- );
350
- }
351
-
352
- // Validate platformFee
353
- if (!/^\d+$/.test(settlement.platformFee)) {
354
- throw new ValidationError('settlement.platformFee', 'Must be numeric string');
355
- }
356
-
357
- this.settlement = {
358
- settledAt: settlement.settledAt,
359
- finalState: settlement.finalState,
360
- escrowReleased: {
361
- to: settlement.escrowReleased.to.toLowerCase(),
362
- amount: settlement.escrowReleased.amount
363
- },
364
- platformFee: settlement.platformFee,
365
- wasDisputed: Boolean(settlement.wasDisputed)
366
- };
367
- return this;
368
- }
369
-
370
- // ==========================================================================
371
- // Build Method
372
- // ==========================================================================
373
-
374
- /**
375
- * Build the archive bundle
376
- *
377
- * @returns Complete archive bundle ready for Arweave upload
378
- * @throws {ValidationError} If required fields are missing
379
- */
380
- build(): ArchiveBundle {
381
- // Validate all required fields
382
- if (!this.txId) {
383
- throw new ValidationError('build', 'Transaction ID is required');
384
- }
385
- if (this.chainId === undefined) {
386
- throw new ValidationError('build', 'Chain ID is required');
387
- }
388
- if (!this.participants) {
389
- throw new ValidationError('build', 'Participants are required');
390
- }
391
- if (!this.references) {
392
- throw new ValidationError('build', 'References are required');
393
- }
394
- if (!this.hashes) {
395
- throw new ValidationError('build', 'Hashes are required');
396
- }
397
- if (!this.signatures) {
398
- throw new ValidationError('build', 'Signatures are required');
399
- }
400
- if (!this.settlement) {
401
- throw new ValidationError('build', 'Settlement info is required');
402
- }
403
-
404
- // Set archivedAt if not set
405
- const archivedAt = this.archivedAt ?? Math.floor(Date.now() / 1000);
406
-
407
- const bundle: ArchiveBundle = {
408
- protocolVersion: this.protocolVersion,
409
- archiveSchemaVersion: this.archiveSchemaVersion,
410
- type: ARCHIVE_BUNDLE_TYPE,
411
- txId: this.txId,
412
- chainId: this.chainId,
413
- archivedAt,
414
- participants: this.participants,
415
- references: this.references,
416
- hashes: this.hashes,
417
- signatures: this.signatures,
418
- settlement: this.settlement
419
- };
420
-
421
- // Add attestation if present (optional for CANCELLED transactions)
422
- if (this.attestation) {
423
- bundle.attestation = this.attestation;
424
- }
425
-
426
- return bundle;
427
- }
428
-
429
- // ==========================================================================
430
- // Static Factory Methods
431
- // ==========================================================================
432
-
433
- /**
434
- * Create builder from existing bundle (for modifications)
435
- */
436
- static fromBundle(bundle: ArchiveBundle): ArchiveBundleBuilder {
437
- const builder = new ArchiveBundleBuilder();
438
-
439
- builder.protocolVersion = bundle.protocolVersion;
440
- builder.archiveSchemaVersion = bundle.archiveSchemaVersion;
441
- builder.txId = bundle.txId;
442
- builder.chainId = bundle.chainId;
443
- builder.archivedAt = bundle.archivedAt;
444
- builder.participants = { ...bundle.participants };
445
- builder.references = { ...bundle.references };
446
- builder.hashes = { ...bundle.hashes };
447
- builder.signatures = { ...bundle.signatures };
448
- builder.settlement = { ...bundle.settlement };
449
-
450
- if (bundle.attestation) {
451
- builder.attestation = { ...bundle.attestation };
452
- }
453
-
454
- return builder;
455
- }
456
-
457
- // ==========================================================================
458
- // Private Helpers
459
- // ==========================================================================
460
-
461
- /**
462
- * Sort object keys recursively for canonical JSON
463
- */
464
- private sortObjectKeys(obj: unknown): unknown {
465
- if (obj === null || typeof obj !== 'object') {
466
- return obj;
467
- }
468
-
469
- if (Array.isArray(obj)) {
470
- return obj.map(item => this.sortObjectKeys(item));
471
- }
472
-
473
- const sorted: Record<string, unknown> = {};
474
- const keys = Object.keys(obj as Record<string, unknown>).sort();
475
-
476
- for (const key of keys) {
477
- sorted[key] = this.sortObjectKeys((obj as Record<string, unknown>)[key]);
478
- }
479
-
480
- return sorted;
481
- }
482
- }
483
-
484
- // ============================================================================
485
- // Utility Functions
486
- // ============================================================================
487
-
488
- /**
489
- * Compute keccak256 hash of canonical JSON
490
- *
491
- * @param data - Data to hash
492
- * @returns keccak256 hash (0x-prefixed)
493
- */
494
- export function computeContentHash(data: unknown): string {
495
- const sorted = sortObjectKeysRecursive(data);
496
- const json = JSON.stringify(sorted);
497
- return keccak256(toUtf8Bytes(json));
498
- }
499
-
500
- /**
501
- * Sort object keys recursively
502
- */
503
- function sortObjectKeysRecursive(obj: unknown): unknown {
504
- if (obj === null || typeof obj !== 'object') {
505
- return obj;
506
- }
507
-
508
- if (Array.isArray(obj)) {
509
- return obj.map(item => sortObjectKeysRecursive(item));
510
- }
511
-
512
- const sorted: Record<string, unknown> = {};
513
- const keys = Object.keys(obj as Record<string, unknown>).sort();
514
-
515
- for (const key of keys) {
516
- sorted[key] = sortObjectKeysRecursive((obj as Record<string, unknown>)[key]);
517
- }
518
-
519
- return sorted;
520
- }
521
-
522
- /**
523
- * Validate archive bundle structure
524
- *
525
- * @param bundle - Bundle to validate
526
- * @throws {ValidationError} If bundle is invalid
527
- */
528
- export function validateArchiveBundle(bundle: unknown): bundle is ArchiveBundle {
529
- if (!bundle || typeof bundle !== 'object') {
530
- throw new ValidationError('bundle', 'Archive bundle must be an object');
531
- }
532
-
533
- const b = bundle as Record<string, unknown>;
534
-
535
- // Type check
536
- if (b.type !== ARCHIVE_BUNDLE_TYPE) {
537
- throw new ValidationError('bundle.type', `Expected ${ARCHIVE_BUNDLE_TYPE}`);
538
- }
539
-
540
- // Required fields
541
- const required = [
542
- 'protocolVersion',
543
- 'archiveSchemaVersion',
544
- 'txId',
545
- 'chainId',
546
- 'archivedAt',
547
- 'participants',
548
- 'references',
549
- 'hashes',
550
- 'signatures',
551
- 'settlement'
552
- ];
553
-
554
- for (const field of required) {
555
- if (!(field in b)) {
556
- throw new ValidationError(`bundle.${field}`, 'Required field missing');
557
- }
558
- }
559
-
560
- return true;
561
- }