@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.
- package/dist/ACTPClient.d.ts +18 -0
- package/dist/ACTPClient.d.ts.map +1 -1
- package/dist/ACTPClient.js +72 -23
- package/dist/ACTPClient.js.map +1 -1
- package/dist/adapters/BasicAdapter.d.ts +15 -0
- package/dist/adapters/BasicAdapter.d.ts.map +1 -1
- package/dist/adapters/BasicAdapter.js +33 -4
- package/dist/adapters/BasicAdapter.js.map +1 -1
- package/dist/adapters/StandardAdapter.d.ts +20 -3
- package/dist/adapters/StandardAdapter.d.ts.map +1 -1
- package/dist/adapters/StandardAdapter.js +90 -12
- package/dist/adapters/StandardAdapter.js.map +1 -1
- package/dist/cli/commands/publish.js +16 -4
- package/dist/cli/commands/publish.js.map +1 -1
- package/dist/cli/commands/register.js +16 -4
- package/dist/cli/commands/register.js.map +1 -1
- package/dist/cli/commands/tx.js +31 -3
- package/dist/cli/commands/tx.js.map +1 -1
- package/dist/config/networks.d.ts +10 -2
- package/dist/config/networks.d.ts.map +1 -1
- package/dist/config/networks.js +31 -22
- package/dist/config/networks.js.map +1 -1
- package/dist/level0/request.d.ts.map +1 -1
- package/dist/level0/request.js +2 -1
- package/dist/level0/request.js.map +1 -1
- package/dist/runtime/BlockchainRuntime.d.ts.map +1 -1
- package/dist/runtime/BlockchainRuntime.js +11 -5
- package/dist/runtime/BlockchainRuntime.js.map +1 -1
- package/dist/utils/IPFSClient.d.ts +3 -1
- package/dist/utils/IPFSClient.d.ts.map +1 -1
- package/dist/utils/IPFSClient.js +27 -7
- package/dist/utils/IPFSClient.js.map +1 -1
- package/dist/wallet/AutoWalletProvider.d.ts +11 -1
- package/dist/wallet/AutoWalletProvider.d.ts.map +1 -1
- package/dist/wallet/AutoWalletProvider.js +84 -19
- package/dist/wallet/AutoWalletProvider.js.map +1 -1
- package/dist/wallet/IWalletProvider.d.ts +34 -0
- package/dist/wallet/IWalletProvider.d.ts.map +1 -1
- package/dist/wallet/SmartWalletRouter.d.ts +128 -0
- package/dist/wallet/SmartWalletRouter.d.ts.map +1 -0
- package/dist/wallet/SmartWalletRouter.js +248 -0
- package/dist/wallet/SmartWalletRouter.js.map +1 -0
- package/dist/wallet/aa/DualNonceManager.d.ts +26 -1
- package/dist/wallet/aa/DualNonceManager.d.ts.map +1 -1
- package/dist/wallet/aa/DualNonceManager.js +140 -6
- package/dist/wallet/aa/DualNonceManager.js.map +1 -1
- package/package.json +3 -6
- package/src/ACTPClient.ts +0 -1579
- package/src/abi/ACTPKernel.json +0 -1356
- package/src/abi/AgentRegistry.json +0 -915
- package/src/abi/ERC20.json +0 -40
- package/src/abi/EscrowVault.json +0 -134
- package/src/abi/IdentityRegistry.json +0 -316
- package/src/adapters/AdapterRegistry.ts +0 -173
- package/src/adapters/AdapterRouter.ts +0 -416
- package/src/adapters/BaseAdapter.ts +0 -498
- package/src/adapters/BasicAdapter.ts +0 -514
- package/src/adapters/IAdapter.ts +0 -292
- package/src/adapters/StandardAdapter.ts +0 -555
- package/src/adapters/X402Adapter.ts +0 -731
- package/src/adapters/index.ts +0 -60
- package/src/builders/DeliveryProofBuilder.ts +0 -327
- package/src/builders/QuoteBuilder.ts +0 -483
- package/src/builders/index.ts +0 -17
- package/src/cli/commands/balance.ts +0 -110
- package/src/cli/commands/batch.ts +0 -487
- package/src/cli/commands/config.ts +0 -231
- package/src/cli/commands/deploy-check.ts +0 -364
- package/src/cli/commands/deploy-env.ts +0 -120
- package/src/cli/commands/diff.ts +0 -141
- package/src/cli/commands/init.ts +0 -469
- package/src/cli/commands/mint.ts +0 -116
- package/src/cli/commands/pay.ts +0 -113
- package/src/cli/commands/publish.ts +0 -475
- package/src/cli/commands/pull.ts +0 -124
- package/src/cli/commands/register.ts +0 -247
- package/src/cli/commands/simulate.ts +0 -345
- package/src/cli/commands/time.ts +0 -302
- package/src/cli/commands/tx.ts +0 -448
- package/src/cli/commands/watch.ts +0 -211
- package/src/cli/index.ts +0 -134
- package/src/cli/utils/client.ts +0 -252
- package/src/cli/utils/config.ts +0 -389
- package/src/cli/utils/output.ts +0 -465
- package/src/cli/utils/wallet.ts +0 -109
- package/src/config/agirailsmd.ts +0 -262
- package/src/config/networks.ts +0 -275
- package/src/config/pendingPublish.ts +0 -237
- package/src/config/publishPipeline.ts +0 -359
- package/src/config/syncOperations.ts +0 -279
- package/src/erc8004/ERC8004Bridge.ts +0 -462
- package/src/erc8004/ReputationReporter.ts +0 -468
- package/src/erc8004/index.ts +0 -61
- package/src/errors/index.ts +0 -427
- package/src/index.ts +0 -364
- package/src/level0/Provider.ts +0 -117
- package/src/level0/ServiceDirectory.ts +0 -131
- package/src/level0/index.ts +0 -10
- package/src/level0/provide.ts +0 -132
- package/src/level0/request.ts +0 -432
- package/src/level1/Agent.ts +0 -1426
- package/src/level1/index.ts +0 -10
- package/src/level1/pricing/PriceCalculator.ts +0 -255
- package/src/level1/pricing/PricingStrategy.ts +0 -198
- package/src/level1/types/Job.ts +0 -179
- package/src/level1/types/Options.ts +0 -291
- package/src/level1/types/index.ts +0 -8
- package/src/protocol/ACTPKernel.ts +0 -808
- package/src/protocol/AgentRegistry.ts +0 -559
- package/src/protocol/DIDManager.ts +0 -629
- package/src/protocol/DIDResolver.ts +0 -554
- package/src/protocol/EASHelper.ts +0 -378
- package/src/protocol/EscrowVault.ts +0 -255
- package/src/protocol/EventMonitor.ts +0 -204
- package/src/protocol/MessageSigner.ts +0 -510
- package/src/protocol/ProofGenerator.ts +0 -339
- package/src/protocol/QuoteBuilder.ts +0 -15
- package/src/registry/AgentRegistryClient.ts +0 -202
- package/src/runtime/BlockchainRuntime.ts +0 -1015
- package/src/runtime/IACTPRuntime.ts +0 -306
- package/src/runtime/MockRuntime.ts +0 -1298
- package/src/runtime/MockStateManager.ts +0 -577
- package/src/runtime/index.ts +0 -25
- package/src/runtime/types/MockState.ts +0 -237
- package/src/storage/ArchiveBundleBuilder.ts +0 -561
- package/src/storage/ArweaveClient.ts +0 -946
- package/src/storage/FilebaseClient.ts +0 -790
- package/src/storage/index.ts +0 -96
- package/src/storage/types.ts +0 -348
- package/src/types/adapter.ts +0 -310
- package/src/types/agent.ts +0 -79
- package/src/types/did.ts +0 -223
- package/src/types/eip712.ts +0 -175
- package/src/types/erc8004.ts +0 -293
- package/src/types/escrow.ts +0 -27
- package/src/types/index.ts +0 -17
- package/src/types/message.ts +0 -145
- package/src/types/state.ts +0 -87
- package/src/types/transaction.ts +0 -69
- package/src/types/x402.ts +0 -251
- package/src/utils/ErrorRecoveryGuide.ts +0 -676
- package/src/utils/Helpers.ts +0 -688
- package/src/utils/IPFSClient.ts +0 -368
- package/src/utils/Logger.ts +0 -484
- package/src/utils/NonceManager.ts +0 -591
- package/src/utils/RateLimiter.ts +0 -534
- package/src/utils/ReceivedNonceTracker.ts +0 -567
- package/src/utils/SDKLifecycle.ts +0 -416
- package/src/utils/SecureNonce.ts +0 -78
- package/src/utils/Semaphore.ts +0 -276
- package/src/utils/UsedAttestationTracker.ts +0 -385
- package/src/utils/canonicalJson.ts +0 -38
- package/src/utils/circuitBreaker.ts +0 -324
- package/src/utils/computeTypeHash.ts +0 -48
- package/src/utils/fsSafe.ts +0 -80
- package/src/utils/index.ts +0 -80
- package/src/utils/retry.ts +0 -364
- package/src/utils/security.ts +0 -418
- package/src/utils/validation.ts +0 -540
- package/src/wallet/AutoWalletProvider.ts +0 -299
- package/src/wallet/EOAWalletProvider.ts +0 -69
- package/src/wallet/IWalletProvider.ts +0 -135
- package/src/wallet/aa/BundlerClient.ts +0 -274
- package/src/wallet/aa/DualNonceManager.ts +0 -173
- package/src/wallet/aa/PaymasterClient.ts +0 -174
- package/src/wallet/aa/TransactionBatcher.ts +0 -353
- package/src/wallet/aa/UserOpBuilder.ts +0 -246
- package/src/wallet/aa/constants.ts +0 -60
- package/src/wallet/keystore.ts +0 -240
package/src/utils/Semaphore.ts
DELETED
|
@@ -1,276 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Semaphore - Concurrency limiter for ACTP SDK
|
|
3
|
-
*
|
|
4
|
-
* SECURITY FIX (MEDIUM-4): Prevents unbounded concurrent execution
|
|
5
|
-
* which could lead to resource exhaustion (memory/CPU DoS).
|
|
6
|
-
*
|
|
7
|
-
* @module utils/Semaphore
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Simple semaphore for limiting concurrent operations
|
|
12
|
-
*
|
|
13
|
-
* Uses a FIFO queue to ensure fair scheduling of waiting tasks.
|
|
14
|
-
*/
|
|
15
|
-
export class Semaphore {
|
|
16
|
-
private permits: number;
|
|
17
|
-
private readonly maxPermits: number;
|
|
18
|
-
private readonly waitQueue: Array<{
|
|
19
|
-
resolve: () => void;
|
|
20
|
-
reject: (error: Error) => void;
|
|
21
|
-
}> = [];
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Create a semaphore with specified concurrency limit
|
|
25
|
-
*
|
|
26
|
-
* @param maxPermits - Maximum concurrent permits (default: 10)
|
|
27
|
-
* @throws Error if maxPermits is not positive
|
|
28
|
-
*/
|
|
29
|
-
constructor(maxPermits: number = 10) {
|
|
30
|
-
if (maxPermits <= 0 || !Number.isInteger(maxPermits)) {
|
|
31
|
-
throw new Error(`maxPermits must be a positive integer, got: ${maxPermits}`);
|
|
32
|
-
}
|
|
33
|
-
this.maxPermits = maxPermits;
|
|
34
|
-
this.permits = maxPermits;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Acquire a permit, waiting if necessary
|
|
39
|
-
*
|
|
40
|
-
* @param timeoutMs - Optional timeout in milliseconds (0 = no timeout)
|
|
41
|
-
* @returns Promise that resolves when permit is acquired
|
|
42
|
-
* @throws Error if timeout is exceeded
|
|
43
|
-
*/
|
|
44
|
-
async acquire(timeoutMs: number = 0): Promise<void> {
|
|
45
|
-
if (this.permits > 0) {
|
|
46
|
-
this.permits--;
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// No permits available, queue the request
|
|
51
|
-
return new Promise<void>((resolve, reject) => {
|
|
52
|
-
const waiter = { resolve, reject };
|
|
53
|
-
this.waitQueue.push(waiter);
|
|
54
|
-
|
|
55
|
-
// Set up timeout if specified
|
|
56
|
-
if (timeoutMs > 0) {
|
|
57
|
-
const timeoutId = setTimeout(() => {
|
|
58
|
-
const index = this.waitQueue.indexOf(waiter);
|
|
59
|
-
if (index >= 0) {
|
|
60
|
-
this.waitQueue.splice(index, 1);
|
|
61
|
-
reject(new Error(`Semaphore acquire timeout after ${timeoutMs}ms`));
|
|
62
|
-
}
|
|
63
|
-
}, timeoutMs);
|
|
64
|
-
|
|
65
|
-
// Clear timeout when resolved
|
|
66
|
-
const originalResolve = waiter.resolve;
|
|
67
|
-
waiter.resolve = () => {
|
|
68
|
-
clearTimeout(timeoutId);
|
|
69
|
-
originalResolve();
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Try to acquire a permit without waiting
|
|
77
|
-
*
|
|
78
|
-
* @returns true if permit was acquired, false if none available
|
|
79
|
-
*/
|
|
80
|
-
tryAcquire(): boolean {
|
|
81
|
-
if (this.permits > 0) {
|
|
82
|
-
this.permits--;
|
|
83
|
-
return true;
|
|
84
|
-
}
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Release a permit
|
|
90
|
-
*
|
|
91
|
-
* @throws Error if releasing more permits than acquired
|
|
92
|
-
*/
|
|
93
|
-
release(): void {
|
|
94
|
-
if (this.permits >= this.maxPermits) {
|
|
95
|
-
throw new Error('Cannot release: no permits held');
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// If there are waiters, give permit to first in queue (FIFO)
|
|
99
|
-
if (this.waitQueue.length > 0) {
|
|
100
|
-
const waiter = this.waitQueue.shift()!;
|
|
101
|
-
waiter.resolve();
|
|
102
|
-
} else {
|
|
103
|
-
this.permits++;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Execute a function with semaphore protection
|
|
109
|
-
*
|
|
110
|
-
* Automatically acquires before execution and releases after,
|
|
111
|
-
* even if the function throws.
|
|
112
|
-
*
|
|
113
|
-
* @param fn - Function to execute
|
|
114
|
-
* @param timeoutMs - Optional timeout for acquiring permit
|
|
115
|
-
* @returns Result of the function
|
|
116
|
-
*/
|
|
117
|
-
async run<T>(fn: () => Promise<T> | T, timeoutMs: number = 0): Promise<T> {
|
|
118
|
-
await this.acquire(timeoutMs);
|
|
119
|
-
try {
|
|
120
|
-
return await fn();
|
|
121
|
-
} finally {
|
|
122
|
-
this.release();
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Get current available permits
|
|
128
|
-
*/
|
|
129
|
-
get availablePermits(): number {
|
|
130
|
-
return this.permits;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Get number of waiters in queue
|
|
135
|
-
*/
|
|
136
|
-
get queueLength(): number {
|
|
137
|
-
return this.waitQueue.length;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Get maximum permits
|
|
142
|
-
*/
|
|
143
|
-
get limit(): number {
|
|
144
|
-
return this.maxPermits;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Check if semaphore is fully utilized
|
|
149
|
-
*/
|
|
150
|
-
get isFull(): boolean {
|
|
151
|
-
return this.permits === 0;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Cancel all waiting tasks
|
|
156
|
-
*
|
|
157
|
-
* @param reason - Error message for rejected promises
|
|
158
|
-
*/
|
|
159
|
-
cancelAll(reason: string = 'Semaphore cancelled'): void {
|
|
160
|
-
while (this.waitQueue.length > 0) {
|
|
161
|
-
const waiter = this.waitQueue.shift()!;
|
|
162
|
-
waiter.reject(new Error(reason));
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Rate limiter using sliding window algorithm
|
|
169
|
-
*
|
|
170
|
-
* SECURITY FIX (MEDIUM-4): Complements semaphore for rate-based limiting
|
|
171
|
-
*/
|
|
172
|
-
export class RateLimiter {
|
|
173
|
-
private readonly timestamps: number[] = [];
|
|
174
|
-
private readonly windowMs: number;
|
|
175
|
-
private readonly maxRequests: number;
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Create a rate limiter
|
|
179
|
-
*
|
|
180
|
-
* @param maxRequests - Maximum requests per window
|
|
181
|
-
* @param windowMs - Time window in milliseconds
|
|
182
|
-
*/
|
|
183
|
-
constructor(maxRequests: number, windowMs: number) {
|
|
184
|
-
if (maxRequests <= 0) {
|
|
185
|
-
throw new Error('maxRequests must be positive');
|
|
186
|
-
}
|
|
187
|
-
if (windowMs <= 0) {
|
|
188
|
-
throw new Error('windowMs must be positive');
|
|
189
|
-
}
|
|
190
|
-
this.maxRequests = maxRequests;
|
|
191
|
-
this.windowMs = windowMs;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Check if a request is allowed and record it
|
|
196
|
-
*
|
|
197
|
-
* @returns true if request is allowed, false if rate limited
|
|
198
|
-
*/
|
|
199
|
-
tryAcquire(): boolean {
|
|
200
|
-
const now = Date.now();
|
|
201
|
-
const windowStart = now - this.windowMs;
|
|
202
|
-
|
|
203
|
-
// Remove timestamps outside the window
|
|
204
|
-
while (this.timestamps.length > 0 && this.timestamps[0] < windowStart) {
|
|
205
|
-
this.timestamps.shift();
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// Check if we're at the limit
|
|
209
|
-
if (this.timestamps.length >= this.maxRequests) {
|
|
210
|
-
return false;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// Record this request
|
|
214
|
-
this.timestamps.push(now);
|
|
215
|
-
return true;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Wait until a request is allowed
|
|
220
|
-
*
|
|
221
|
-
* @param timeoutMs - Optional timeout
|
|
222
|
-
* @returns Promise that resolves when request is allowed
|
|
223
|
-
*/
|
|
224
|
-
async acquire(timeoutMs: number = 0): Promise<void> {
|
|
225
|
-
const startTime = Date.now();
|
|
226
|
-
|
|
227
|
-
while (!this.tryAcquire()) {
|
|
228
|
-
if (timeoutMs > 0 && Date.now() - startTime >= timeoutMs) {
|
|
229
|
-
throw new Error(`Rate limiter timeout after ${timeoutMs}ms`);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// Wait a short time before retrying
|
|
233
|
-
const waitTime = Math.min(100, this.timeUntilNextSlot());
|
|
234
|
-
await new Promise((resolve) => setTimeout(resolve, waitTime));
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* Get time until next slot is available
|
|
240
|
-
*
|
|
241
|
-
* @returns Milliseconds until next slot, or 0 if slot available
|
|
242
|
-
*/
|
|
243
|
-
timeUntilNextSlot(): number {
|
|
244
|
-
if (this.timestamps.length < this.maxRequests) {
|
|
245
|
-
return 0;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
const now = Date.now();
|
|
249
|
-
const windowStart = now - this.windowMs;
|
|
250
|
-
const oldestTimestamp = this.timestamps[0];
|
|
251
|
-
|
|
252
|
-
if (oldestTimestamp <= windowStart) {
|
|
253
|
-
return 0;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
return oldestTimestamp - windowStart;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* Get current usage stats
|
|
261
|
-
*/
|
|
262
|
-
get stats(): { current: number; max: number; windowMs: number } {
|
|
263
|
-
// Clean up old timestamps
|
|
264
|
-
const now = Date.now();
|
|
265
|
-
const windowStart = now - this.windowMs;
|
|
266
|
-
while (this.timestamps.length > 0 && this.timestamps[0] < windowStart) {
|
|
267
|
-
this.timestamps.shift();
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
return {
|
|
271
|
-
current: this.timestamps.length,
|
|
272
|
-
max: this.maxRequests,
|
|
273
|
-
windowMs: this.windowMs,
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
}
|
|
@@ -1,385 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* UsedAttestationTracker - Prevents EAS Attestation Replay Attacks (C-1)
|
|
3
|
-
*
|
|
4
|
-
* Tracks which attestation UIDs have been used for which transaction IDs.
|
|
5
|
-
* This prevents a malicious provider from reusing an attestation from
|
|
6
|
-
* Transaction A to settle Transaction B.
|
|
7
|
-
*
|
|
8
|
-
* SECURITY: ACTPKernel V1 contract accepts any attestationUID without validation.
|
|
9
|
-
* This tracker provides SDK-side protection until contract is upgraded.
|
|
10
|
-
*
|
|
11
|
-
* @module utils/UsedAttestationTracker
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { assertSafeFileForRead, ensureSafeDir, ensureSafeFile } from './fsSafe';
|
|
15
|
-
import { sdkLogger } from './Logger';
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Interface for tracking used attestations
|
|
19
|
-
*/
|
|
20
|
-
export interface IUsedAttestationTracker {
|
|
21
|
-
/**
|
|
22
|
-
* Record that an attestation was used for a transaction
|
|
23
|
-
* @param attestationUID - EAS attestation UID (bytes32)
|
|
24
|
-
* @param txId - Transaction ID (bytes32)
|
|
25
|
-
* @returns true if recorded, false if already used for different transaction
|
|
26
|
-
*
|
|
27
|
-
* SECURITY FIX (HIGH-1): This method is now async to ensure persistence completes
|
|
28
|
-
* before returning. Use recordUsageSync() for fire-and-forget behavior.
|
|
29
|
-
*/
|
|
30
|
-
recordUsage(attestationUID: string, txId: string): Promise<boolean>;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Check if attestation has been used
|
|
34
|
-
* @param attestationUID - EAS attestation UID (bytes32)
|
|
35
|
-
* @returns Transaction ID if used, null if not used
|
|
36
|
-
*/
|
|
37
|
-
getUsageForAttestation(attestationUID: string): string | null;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Check if attestation is valid for transaction
|
|
41
|
-
* @param attestationUID - EAS attestation UID
|
|
42
|
-
* @param txId - Transaction ID
|
|
43
|
-
* @returns true if attestation is unused or already used for this txId
|
|
44
|
-
*/
|
|
45
|
-
isValidForTransaction(attestationUID: string, txId: string): boolean;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Clear all tracked attestations
|
|
49
|
-
*/
|
|
50
|
-
clear(): void;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* In-Memory Used Attestation Tracker
|
|
55
|
-
*
|
|
56
|
-
* SECURITY FIX (C-1): Prevents attestation replay attacks by tracking
|
|
57
|
-
* which attestation UIDs have been used for which transactions.
|
|
58
|
-
*
|
|
59
|
-
* SECURITY FIX (NEW-H-2): LRU-style cache with max size to prevent DoS
|
|
60
|
-
*
|
|
61
|
-
* WARNING: In-memory only. For production:
|
|
62
|
-
* - Use persistent storage (Redis, PostgreSQL, etc.)
|
|
63
|
-
* - Implement recovery from blockchain events
|
|
64
|
-
*/
|
|
65
|
-
export class InMemoryUsedAttestationTracker implements IUsedAttestationTracker {
|
|
66
|
-
// Map: attestationUID -> txId
|
|
67
|
-
private usedAttestations: Map<string, string> = new Map();
|
|
68
|
-
|
|
69
|
-
// SECURITY FIX (NEW-H-2): Maximum size to prevent memory exhaustion DoS
|
|
70
|
-
private readonly maxSize: number;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Create in-memory tracker with optional max size
|
|
74
|
-
* @param maxSize - Maximum entries to store (default: 100,000)
|
|
75
|
-
*/
|
|
76
|
-
constructor(maxSize: number = 100000) {
|
|
77
|
-
if (maxSize <= 0) {
|
|
78
|
-
throw new Error('maxSize must be positive');
|
|
79
|
-
}
|
|
80
|
-
this.maxSize = maxSize;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Record that an attestation was used for a transaction
|
|
85
|
-
* @param attestationUID - EAS attestation UID (bytes32)
|
|
86
|
-
* @param txId - Transaction ID (bytes32)
|
|
87
|
-
* @returns true if recorded, false if already used for different transaction
|
|
88
|
-
*
|
|
89
|
-
* SECURITY FIX (NEW-H-2): LRU eviction when max size reached
|
|
90
|
-
* SECURITY FIX (HIGH-1): Now async for interface consistency
|
|
91
|
-
*/
|
|
92
|
-
async recordUsage(attestationUID: string, txId: string): Promise<boolean> {
|
|
93
|
-
return this.recordUsageSync(attestationUID, txId);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Synchronous version of recordUsage (for backward compatibility)
|
|
98
|
-
* @param attestationUID - EAS attestation UID (bytes32)
|
|
99
|
-
* @param txId - Transaction ID (bytes32)
|
|
100
|
-
* @returns true if recorded, false if already used for different transaction
|
|
101
|
-
*/
|
|
102
|
-
recordUsageSync(attestationUID: string, txId: string): boolean {
|
|
103
|
-
const normalizedUID = attestationUID.toLowerCase();
|
|
104
|
-
const normalizedTxId = txId.toLowerCase();
|
|
105
|
-
|
|
106
|
-
const existingTxId = this.usedAttestations.get(normalizedUID);
|
|
107
|
-
|
|
108
|
-
// If attestation was already used for a different transaction, reject
|
|
109
|
-
if (existingTxId && existingTxId !== normalizedTxId) {
|
|
110
|
-
return false;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// SECURITY FIX (NEW-H-2): Enforce max size limit with true LRU behavior
|
|
114
|
-
if (this.usedAttestations.size >= this.maxSize && !existingTxId) {
|
|
115
|
-
// Remove oldest entry (first entry in Map)
|
|
116
|
-
const firstKey = this.usedAttestations.keys().next().value;
|
|
117
|
-
if (firstKey) {
|
|
118
|
-
this.usedAttestations.delete(firstKey);
|
|
119
|
-
}
|
|
120
|
-
} else if (existingTxId) {
|
|
121
|
-
// SECURITY FIX (M-3): True LRU - delete and re-add to move to end
|
|
122
|
-
this.usedAttestations.delete(normalizedUID);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// Record the usage (at end for LRU)
|
|
126
|
-
this.usedAttestations.set(normalizedUID, normalizedTxId);
|
|
127
|
-
return true;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Check if attestation has been used
|
|
132
|
-
* @param attestationUID - EAS attestation UID (bytes32)
|
|
133
|
-
* @returns Transaction ID if used, null if not used
|
|
134
|
-
*
|
|
135
|
-
* SECURITY FIX (MEDIUM-4): Updates access order for true LRU behavior
|
|
136
|
-
* Accessed items are moved to end of Map (most recently used)
|
|
137
|
-
*/
|
|
138
|
-
getUsageForAttestation(attestationUID: string): string | null {
|
|
139
|
-
const normalizedUID = attestationUID.toLowerCase();
|
|
140
|
-
const txId = this.usedAttestations.get(normalizedUID);
|
|
141
|
-
|
|
142
|
-
// SECURITY FIX (MEDIUM-4): True LRU - move accessed item to end
|
|
143
|
-
// Without this, eviction uses insertion order, not access order
|
|
144
|
-
if (txId !== undefined) {
|
|
145
|
-
this.usedAttestations.delete(normalizedUID);
|
|
146
|
-
this.usedAttestations.set(normalizedUID, txId);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
return txId || null;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Check if attestation is valid for transaction
|
|
154
|
-
* @param attestationUID - EAS attestation UID
|
|
155
|
-
* @param txId - Transaction ID
|
|
156
|
-
* @returns true if attestation is unused or already used for this txId
|
|
157
|
-
*
|
|
158
|
-
* SECURITY FIX (MEDIUM-4): Updates access order for true LRU behavior
|
|
159
|
-
*/
|
|
160
|
-
isValidForTransaction(attestationUID: string, txId: string): boolean {
|
|
161
|
-
const normalizedUID = attestationUID.toLowerCase();
|
|
162
|
-
const normalizedTxId = txId.toLowerCase();
|
|
163
|
-
|
|
164
|
-
const existingTxId = this.usedAttestations.get(normalizedUID);
|
|
165
|
-
|
|
166
|
-
// SECURITY FIX (MEDIUM-4): True LRU - move accessed item to end
|
|
167
|
-
if (existingTxId !== undefined) {
|
|
168
|
-
this.usedAttestations.delete(normalizedUID);
|
|
169
|
-
this.usedAttestations.set(normalizedUID, existingTxId);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// Valid if: not used OR used for same transaction
|
|
173
|
-
return !existingTxId || existingTxId === normalizedTxId;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Clear all tracked attestations
|
|
178
|
-
*/
|
|
179
|
-
clear(): void {
|
|
180
|
-
this.usedAttestations.clear();
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Get all tracked attestations (for debugging/persistence)
|
|
185
|
-
*/
|
|
186
|
-
getAllUsages(): Record<string, string> {
|
|
187
|
-
return Object.fromEntries(this.usedAttestations.entries());
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Get count of tracked attestations
|
|
192
|
-
*/
|
|
193
|
-
getCount(): number {
|
|
194
|
-
return this.usedAttestations.size;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Cleanup old entries based on timestamp (optional)
|
|
199
|
-
*
|
|
200
|
-
* SECURITY FIX (NEW-H-2): Manual cleanup for old entries
|
|
201
|
-
* Note: This requires external timestamp tracking. For automatic cleanup,
|
|
202
|
-
* use FileBasedUsedAttestationTracker with periodic cleanup.
|
|
203
|
-
*
|
|
204
|
-
* @param maxAgeHours - Remove entries older than this many hours
|
|
205
|
-
*/
|
|
206
|
-
cleanupOldEntries(_maxAgeHours: number): number {
|
|
207
|
-
// In-memory tracker doesn't track timestamps
|
|
208
|
-
// This is a placeholder for future enhancement
|
|
209
|
-
sdkLogger.warn('cleanupOldEntries not implemented for InMemoryUsedAttestationTracker - use FileBasedUsedAttestationTracker');
|
|
210
|
-
return 0;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* File-based Used Attestation Tracker for persistence
|
|
216
|
-
*
|
|
217
|
-
* SECURITY FIX (C-1): Persistent storage for attestation tracking
|
|
218
|
-
* SECURITY FIX (NEW-H-4): File locking to prevent concurrent write corruption
|
|
219
|
-
*
|
|
220
|
-
* Survives process restarts.
|
|
221
|
-
*/
|
|
222
|
-
export class FileBasedUsedAttestationTracker implements IUsedAttestationTracker {
|
|
223
|
-
private inMemory: InMemoryUsedAttestationTracker;
|
|
224
|
-
private filePath: string;
|
|
225
|
-
private fs: typeof import('fs');
|
|
226
|
-
private path: typeof import('path');
|
|
227
|
-
private lockfile: typeof import('proper-lockfile');
|
|
228
|
-
|
|
229
|
-
constructor(stateDirectory: string) {
|
|
230
|
-
this.inMemory = new InMemoryUsedAttestationTracker();
|
|
231
|
-
this.fs = require('fs');
|
|
232
|
-
this.path = require('path');
|
|
233
|
-
// SECURITY FIX (NEW-H-4): File locking to prevent race conditions
|
|
234
|
-
this.lockfile = require('proper-lockfile');
|
|
235
|
-
|
|
236
|
-
// Ensure directory exists
|
|
237
|
-
const actpDir = this.path.join(stateDirectory, '.actp');
|
|
238
|
-
ensureSafeDir(actpDir, 0o755);
|
|
239
|
-
|
|
240
|
-
this.filePath = this.path.join(actpDir, 'used-attestations.json');
|
|
241
|
-
|
|
242
|
-
// Load existing data
|
|
243
|
-
this.loadFromFile();
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
private loadFromFile(): void {
|
|
247
|
-
if (!this.fs.existsSync(this.filePath)) return;
|
|
248
|
-
|
|
249
|
-
// SECURITY: Refuse to read from symlinked tracker files
|
|
250
|
-
assertSafeFileForRead(this.filePath);
|
|
251
|
-
|
|
252
|
-
// Basic size limit to avoid memory DoS on parse
|
|
253
|
-
const MAX_TRACKER_FILE_SIZE = 10 * 1024 * 1024; // 10MB
|
|
254
|
-
const st = this.fs.statSync(this.filePath);
|
|
255
|
-
if (st.size > MAX_TRACKER_FILE_SIZE) {
|
|
256
|
-
throw new Error(
|
|
257
|
-
`used-attestations.json exceeds ${MAX_TRACKER_FILE_SIZE / 1024 / 1024}MB limit: ${this.filePath}`
|
|
258
|
-
);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
try {
|
|
262
|
-
const data = JSON.parse(this.fs.readFileSync(this.filePath, 'utf-8'));
|
|
263
|
-
for (const [uid, txId] of Object.entries(data)) {
|
|
264
|
-
this.inMemory.recordUsageSync(uid, txId as string);
|
|
265
|
-
}
|
|
266
|
-
} catch (e: any) {
|
|
267
|
-
// Fail closed: losing replay-protection state is a security issue.
|
|
268
|
-
throw new Error(
|
|
269
|
-
`Failed to parse used-attestations.json (replay protection would be disabled). ` +
|
|
270
|
-
`Fix/delete the file: ${this.filePath}. Error: ${e?.message || String(e)}`
|
|
271
|
-
);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Save data to file with file locking
|
|
277
|
-
*
|
|
278
|
-
* SECURITY FIX (NEW-H-4): File locking prevents concurrent write corruption
|
|
279
|
-
* SECURITY FIX (NEW-HIGH-1): Create file before locking if it doesn't exist
|
|
280
|
-
*/
|
|
281
|
-
private async saveToFile(): Promise<void> {
|
|
282
|
-
const data = this.inMemory.getAllUsages();
|
|
283
|
-
const tempPath = `${this.filePath}.tmp`;
|
|
284
|
-
|
|
285
|
-
// SECURITY FIX (NEW-HIGH-1): Ensure file exists before locking
|
|
286
|
-
// proper-lockfile.lock() fails on non-existent files
|
|
287
|
-
ensureSafeFile(this.filePath, '{}', 0o644);
|
|
288
|
-
|
|
289
|
-
// SECURITY FIX (NEW-H-4): Acquire file lock before writing
|
|
290
|
-
let release: (() => Promise<void>) | null = null;
|
|
291
|
-
try {
|
|
292
|
-
release = await this.lockfile.lock(this.filePath, {
|
|
293
|
-
stale: 10000, // Lock expires after 10 seconds if process crashes
|
|
294
|
-
retries: {
|
|
295
|
-
retries: 5,
|
|
296
|
-
minTimeout: 100,
|
|
297
|
-
maxTimeout: 500
|
|
298
|
-
}
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
// Atomic write: temp file + rename
|
|
302
|
-
if (this.fs.existsSync(tempPath)) {
|
|
303
|
-
this.fs.unlinkSync(tempPath);
|
|
304
|
-
}
|
|
305
|
-
this.fs.writeFileSync(tempPath, JSON.stringify(data, null, 2), {
|
|
306
|
-
encoding: 'utf-8',
|
|
307
|
-
mode: 0o644,
|
|
308
|
-
flag: 'wx'
|
|
309
|
-
});
|
|
310
|
-
this.fs.renameSync(tempPath, this.filePath);
|
|
311
|
-
} catch (error) {
|
|
312
|
-
// Clean up temp file on error
|
|
313
|
-
if (this.fs.existsSync(tempPath)) {
|
|
314
|
-
try {
|
|
315
|
-
this.fs.unlinkSync(tempPath);
|
|
316
|
-
} catch {
|
|
317
|
-
// Ignore cleanup errors
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
throw error;
|
|
321
|
-
} finally {
|
|
322
|
-
// Always release lock if acquired
|
|
323
|
-
if (release) {
|
|
324
|
-
await release();
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Record attestation usage with guaranteed persistence
|
|
331
|
-
*
|
|
332
|
-
* SECURITY FIX (HIGH-1): Now properly awaits persistence to prevent data loss
|
|
333
|
-
*/
|
|
334
|
-
async recordUsage(attestationUID: string, txId: string): Promise<boolean> {
|
|
335
|
-
const result = this.inMemory.recordUsageSync(attestationUID, txId);
|
|
336
|
-
if (result) {
|
|
337
|
-
// SECURITY FIX (HIGH-1): Await persistence to ensure data is saved
|
|
338
|
-
await this.saveToFile();
|
|
339
|
-
}
|
|
340
|
-
return result;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* Fire-and-forget version for backward compatibility
|
|
345
|
-
* WARNING: May lose data if process crashes before save completes
|
|
346
|
-
*/
|
|
347
|
-
recordUsageSync(attestationUID: string, txId: string): boolean {
|
|
348
|
-
const result = this.inMemory.recordUsageSync(attestationUID, txId);
|
|
349
|
-
if (result) {
|
|
350
|
-
this.saveToFile().catch((err) => {
|
|
351
|
-
sdkLogger.error('Failed to save attestation tracker state', { error: err instanceof Error ? err.message : String(err) });
|
|
352
|
-
});
|
|
353
|
-
}
|
|
354
|
-
return result;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
getUsageForAttestation(attestationUID: string): string | null {
|
|
358
|
-
return this.inMemory.getUsageForAttestation(attestationUID);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
isValidForTransaction(attestationUID: string, txId: string): boolean {
|
|
362
|
-
return this.inMemory.isValidForTransaction(attestationUID, txId);
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
clear(): void {
|
|
366
|
-
this.inMemory.clear();
|
|
367
|
-
if (this.fs.existsSync(this.filePath)) {
|
|
368
|
-
this.fs.unlinkSync(this.filePath);
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
/**
|
|
374
|
-
* Factory to create attestation tracker
|
|
375
|
-
* @param stateDirectory - Optional directory for persistent storage
|
|
376
|
-
* @returns IUsedAttestationTracker instance
|
|
377
|
-
*/
|
|
378
|
-
export function createUsedAttestationTracker(
|
|
379
|
-
stateDirectory?: string
|
|
380
|
-
): IUsedAttestationTracker {
|
|
381
|
-
if (stateDirectory) {
|
|
382
|
-
return new FileBasedUsedAttestationTracker(stateDirectory);
|
|
383
|
-
}
|
|
384
|
-
return new InMemoryUsedAttestationTracker();
|
|
385
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Canonical JSON Serialization for AIP-4
|
|
3
|
-
* Reference: AIP-4 §3.6
|
|
4
|
-
*
|
|
5
|
-
* CRITICAL: Uses fast-json-stable-stringify@^2.1.0 for deterministic hashing
|
|
6
|
-
* This ensures resultHash is identical across all implementations (JS, Python, Go, Rust)
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import stringify from 'fast-json-stable-stringify';
|
|
10
|
-
import { keccak256, toUtf8Bytes } from 'ethers';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Canonical JSON stringify (sorted keys, no whitespace)
|
|
14
|
-
* @param obj - Any JSON-serializable object
|
|
15
|
-
* @returns Canonical JSON string
|
|
16
|
-
*/
|
|
17
|
-
export function canonicalJsonStringify(obj: any): string {
|
|
18
|
-
return stringify(obj);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Compute keccak256 hash of canonical JSON
|
|
23
|
-
* @param obj - Any JSON-serializable object
|
|
24
|
-
* @returns Keccak256 hash (0x-prefixed hex string)
|
|
25
|
-
*/
|
|
26
|
-
export function computeCanonicalHash(obj: any): string {
|
|
27
|
-
const canonical = canonicalJsonStringify(obj);
|
|
28
|
-
return keccak256(toUtf8Bytes(canonical));
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Compute result hash for delivery proof (AIP-4)
|
|
33
|
-
* @param resultData - Service result data
|
|
34
|
-
* @returns Keccak256 hash of canonical result JSON
|
|
35
|
-
*/
|
|
36
|
-
export function computeResultHash(resultData: any): string {
|
|
37
|
-
return computeCanonicalHash(resultData);
|
|
38
|
-
}
|