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