@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.
- package/README.md +536 -87
- package/dist/ACTPClient.d.ts +200 -0
- package/dist/ACTPClient.d.ts.map +1 -1
- package/dist/ACTPClient.js +266 -2
- package/dist/ACTPClient.js.map +1 -1
- package/dist/abi/ACTPKernel.json +16 -0
- package/dist/adapters/AdapterRegistry.d.ts +140 -0
- package/dist/adapters/AdapterRegistry.d.ts.map +1 -0
- package/dist/adapters/AdapterRegistry.js +166 -0
- package/dist/adapters/AdapterRegistry.js.map +1 -0
- package/dist/adapters/AdapterRouter.d.ts +165 -0
- package/dist/adapters/AdapterRouter.d.ts.map +1 -0
- package/dist/adapters/AdapterRouter.js +350 -0
- package/dist/adapters/AdapterRouter.js.map +1 -0
- package/dist/adapters/BaseAdapter.d.ts +17 -0
- package/dist/adapters/BaseAdapter.d.ts.map +1 -1
- package/dist/adapters/BaseAdapter.js +21 -0
- package/dist/adapters/BaseAdapter.js.map +1 -1
- package/dist/adapters/BasicAdapter.d.ts +72 -3
- package/dist/adapters/BasicAdapter.d.ts.map +1 -1
- package/dist/adapters/BasicAdapter.js +178 -2
- package/dist/adapters/BasicAdapter.js.map +1 -1
- package/dist/adapters/IAdapter.d.ts +230 -0
- package/dist/adapters/IAdapter.d.ts.map +1 -0
- package/dist/adapters/IAdapter.js +44 -0
- package/dist/adapters/IAdapter.js.map +1 -0
- package/dist/adapters/StandardAdapter.d.ts +80 -6
- package/dist/adapters/StandardAdapter.d.ts.map +1 -1
- package/dist/adapters/StandardAdapter.js +203 -6
- package/dist/adapters/StandardAdapter.js.map +1 -1
- package/dist/adapters/X402Adapter.d.ts +208 -0
- package/dist/adapters/X402Adapter.d.ts.map +1 -0
- package/dist/adapters/X402Adapter.js +423 -0
- package/dist/adapters/X402Adapter.js.map +1 -0
- package/dist/adapters/index.d.ts +8 -0
- package/dist/adapters/index.d.ts.map +1 -1
- package/dist/adapters/index.js +19 -1
- package/dist/adapters/index.js.map +1 -1
- package/dist/cli/commands/init.d.ts +4 -0
- package/dist/cli/commands/init.d.ts.map +1 -1
- package/dist/cli/commands/init.js +146 -4
- package/dist/cli/commands/init.js.map +1 -1
- package/dist/config/networks.d.ts +9 -0
- package/dist/config/networks.d.ts.map +1 -1
- package/dist/config/networks.js +27 -12
- package/dist/config/networks.js.map +1 -1
- package/dist/erc8004/ERC8004Bridge.d.ts +155 -0
- package/dist/erc8004/ERC8004Bridge.d.ts.map +1 -0
- package/dist/erc8004/ERC8004Bridge.js +325 -0
- package/dist/erc8004/ERC8004Bridge.js.map +1 -0
- package/dist/erc8004/ReputationReporter.d.ts +223 -0
- package/dist/erc8004/ReputationReporter.d.ts.map +1 -0
- package/dist/erc8004/ReputationReporter.js +266 -0
- package/dist/erc8004/ReputationReporter.js.map +1 -0
- package/dist/erc8004/index.d.ts +36 -0
- package/dist/erc8004/index.d.ts.map +1 -0
- package/dist/erc8004/index.js +46 -0
- package/dist/erc8004/index.js.map +1 -0
- package/dist/index.d.ts +11 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +51 -2
- package/dist/index.js.map +1 -1
- package/dist/level0/provide.d.ts.map +1 -1
- package/dist/level0/provide.js +2 -1
- package/dist/level0/provide.js.map +1 -1
- package/dist/level1/Agent.d.ts.map +1 -1
- package/dist/level1/Agent.js +11 -3
- package/dist/level1/Agent.js.map +1 -1
- package/dist/protocol/ACTPKernel.d.ts +1 -1
- package/dist/protocol/ACTPKernel.d.ts.map +1 -1
- package/dist/protocol/ACTPKernel.js +23 -12
- package/dist/protocol/ACTPKernel.js.map +1 -1
- package/dist/protocol/DIDResolver.js +1 -1
- package/dist/protocol/DIDResolver.js.map +1 -1
- package/dist/protocol/EASHelper.d.ts.map +1 -1
- package/dist/protocol/EASHelper.js +2 -3
- package/dist/protocol/EASHelper.js.map +1 -1
- package/dist/protocol/MessageSigner.d.ts.map +1 -1
- package/dist/protocol/MessageSigner.js +8 -8
- package/dist/protocol/MessageSigner.js.map +1 -1
- package/dist/runtime/BlockchainRuntime.d.ts +7 -0
- package/dist/runtime/BlockchainRuntime.d.ts.map +1 -1
- package/dist/runtime/BlockchainRuntime.js +40 -22
- package/dist/runtime/BlockchainRuntime.js.map +1 -1
- package/dist/runtime/IACTPRuntime.d.ts +21 -0
- package/dist/runtime/IACTPRuntime.d.ts.map +1 -1
- package/dist/runtime/MockRuntime.d.ts +19 -0
- package/dist/runtime/MockRuntime.d.ts.map +1 -1
- package/dist/runtime/MockRuntime.js +56 -4
- package/dist/runtime/MockRuntime.js.map +1 -1
- package/dist/runtime/types/MockState.d.ts +11 -2
- package/dist/runtime/types/MockState.d.ts.map +1 -1
- package/dist/runtime/types/MockState.js.map +1 -1
- package/dist/storage/ArchiveBundleBuilder.d.ts +150 -0
- package/dist/storage/ArchiveBundleBuilder.d.ts.map +1 -0
- package/dist/storage/ArchiveBundleBuilder.js +468 -0
- package/dist/storage/ArchiveBundleBuilder.js.map +1 -0
- package/dist/storage/ArweaveClient.d.ts +271 -0
- package/dist/storage/ArweaveClient.d.ts.map +1 -0
- package/dist/storage/ArweaveClient.js +761 -0
- package/dist/storage/ArweaveClient.js.map +1 -0
- package/dist/storage/FilebaseClient.d.ts +193 -0
- package/dist/storage/FilebaseClient.d.ts.map +1 -0
- package/dist/storage/FilebaseClient.js +643 -0
- package/dist/storage/FilebaseClient.js.map +1 -0
- package/dist/storage/index.d.ts +47 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +64 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/types.d.ts +291 -0
- package/dist/storage/types.d.ts.map +1 -0
- package/dist/storage/types.js +18 -0
- package/dist/storage/types.js.map +1 -0
- package/dist/types/adapter.d.ts +359 -0
- package/dist/types/adapter.d.ts.map +1 -0
- package/dist/types/adapter.js +115 -0
- package/dist/types/adapter.js.map +1 -0
- package/dist/types/erc8004.d.ts +184 -0
- package/dist/types/erc8004.d.ts.map +1 -0
- package/dist/types/erc8004.js +132 -0
- package/dist/types/erc8004.js.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -1
- package/dist/types/state.d.ts +5 -4
- package/dist/types/state.d.ts.map +1 -1
- package/dist/types/state.js +10 -9
- package/dist/types/state.js.map +1 -1
- package/dist/types/transaction.d.ts +12 -0
- package/dist/types/transaction.d.ts.map +1 -1
- package/dist/types/x402.d.ts +162 -0
- package/dist/types/x402.d.ts.map +1 -0
- package/dist/types/x402.js +162 -0
- package/dist/types/x402.js.map +1 -0
- package/dist/utils/IPFSClient.d.ts.map +1 -1
- package/dist/utils/IPFSClient.js +5 -2
- package/dist/utils/IPFSClient.js.map +1 -1
- package/dist/utils/NonceManager.d.ts.map +1 -1
- package/dist/utils/NonceManager.js +3 -2
- package/dist/utils/NonceManager.js.map +1 -1
- package/dist/utils/UsedAttestationTracker.d.ts.map +1 -1
- package/dist/utils/UsedAttestationTracker.js +3 -3
- package/dist/utils/UsedAttestationTracker.js.map +1 -1
- package/dist/utils/circuitBreaker.d.ts +136 -0
- package/dist/utils/circuitBreaker.d.ts.map +1 -0
- package/dist/utils/circuitBreaker.js +253 -0
- package/dist/utils/circuitBreaker.js.map +1 -0
- package/dist/utils/retry.d.ts +120 -0
- package/dist/utils/retry.d.ts.map +1 -0
- package/dist/utils/retry.js +260 -0
- package/dist/utils/retry.js.map +1 -0
- package/dist/utils/validation.d.ts +100 -0
- package/dist/utils/validation.d.ts.map +1 -1
- package/dist/utils/validation.js +248 -1
- package/dist/utils/validation.js.map +1 -1
- package/package.json +16 -3
- package/src/ACTPClient.ts +318 -2
- package/src/abi/ACTPKernel.json +16 -0
- package/src/adapters/AdapterRegistry.ts +173 -0
- package/src/adapters/AdapterRouter.ts +417 -0
- package/src/adapters/BaseAdapter.ts +25 -0
- package/src/adapters/BasicAdapter.ts +210 -3
- package/src/adapters/IAdapter.ts +292 -0
- package/src/adapters/StandardAdapter.ts +246 -7
- package/src/adapters/X402Adapter.ts +653 -0
- package/src/adapters/index.ts +27 -0
- package/src/cli/commands/init.ts +166 -3
- package/src/config/networks.ts +36 -12
- package/src/erc8004/ERC8004Bridge.ts +461 -0
- package/src/erc8004/ReputationReporter.ts +472 -0
- package/src/erc8004/index.ts +61 -0
- package/src/index.ts +97 -0
- package/src/level0/provide.ts +2 -1
- package/src/level1/Agent.ts +13 -3
- package/src/protocol/ACTPKernel.ts +33 -12
- package/src/protocol/DIDResolver.ts +1 -1
- package/src/protocol/EASHelper.ts +2 -5
- package/src/protocol/MessageSigner.ts +8 -14
- package/src/runtime/BlockchainRuntime.ts +41 -45
- package/src/runtime/IACTPRuntime.ts +22 -0
- package/src/runtime/MockRuntime.ts +58 -4
- package/src/runtime/types/MockState.ts +12 -2
- package/src/storage/ArchiveBundleBuilder.ts +563 -0
- package/src/storage/ArweaveClient.ts +945 -0
- package/src/storage/FilebaseClient.ts +790 -0
- package/src/storage/index.ts +96 -0
- package/src/storage/types.ts +348 -0
- package/src/types/adapter.ts +296 -0
- package/src/types/erc8004.ts +293 -0
- package/src/types/index.ts +3 -0
- package/src/types/state.ts +10 -9
- package/src/types/transaction.ts +12 -0
- package/src/types/x402.ts +219 -0
- package/src/utils/IPFSClient.ts +5 -4
- package/src/utils/NonceManager.ts +3 -2
- package/src/utils/UsedAttestationTracker.ts +3 -5
- package/src/utils/circuitBreaker.ts +324 -0
- package/src/utils/fsSafe.ts +5 -0
- package/src/utils/retry.ts +365 -0
- 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
|
+
}
|