@agirails/sdk 2.0.0-beta
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 +183 -0
- package/dist/ACTPClient.d.ts +52 -0
- package/dist/ACTPClient.d.ts.map +1 -0
- package/dist/ACTPClient.js +120 -0
- package/dist/ACTPClient.js.map +1 -0
- package/dist/abi/ACTPKernel.json +1340 -0
- package/dist/abi/ERC20.json +38 -0
- package/dist/abi/EscrowVault.json +64 -0
- package/dist/builders/DeliveryProofBuilder.d.ts +37 -0
- package/dist/builders/DeliveryProofBuilder.d.ts.map +1 -0
- package/dist/builders/DeliveryProofBuilder.js +165 -0
- package/dist/builders/DeliveryProofBuilder.js.map +1 -0
- package/dist/builders/QuoteBuilder.d.ts +68 -0
- package/dist/builders/QuoteBuilder.d.ts.map +1 -0
- package/dist/builders/QuoteBuilder.js +255 -0
- package/dist/builders/QuoteBuilder.js.map +1 -0
- package/dist/builders/index.d.ts +3 -0
- package/dist/builders/index.d.ts.map +1 -0
- package/dist/builders/index.js +10 -0
- package/dist/builders/index.js.map +1 -0
- package/dist/config/networks.d.ts +27 -0
- package/dist/config/networks.d.ts.map +1 -0
- package/dist/config/networks.js +103 -0
- package/dist/config/networks.js.map +1 -0
- package/dist/errors/index.d.ts +38 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +87 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +68 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol/ACTPKernel.d.ts +30 -0
- package/dist/protocol/ACTPKernel.d.ts.map +1 -0
- package/dist/protocol/ACTPKernel.js +261 -0
- package/dist/protocol/ACTPKernel.js.map +1 -0
- package/dist/protocol/EASHelper.d.ts +23 -0
- package/dist/protocol/EASHelper.d.ts.map +1 -0
- package/dist/protocol/EASHelper.js +106 -0
- package/dist/protocol/EASHelper.js.map +1 -0
- package/dist/protocol/EscrowVault.d.ts +24 -0
- package/dist/protocol/EscrowVault.d.ts.map +1 -0
- package/dist/protocol/EscrowVault.js +114 -0
- package/dist/protocol/EscrowVault.js.map +1 -0
- package/dist/protocol/EventMonitor.d.ts +18 -0
- package/dist/protocol/EventMonitor.d.ts.map +1 -0
- package/dist/protocol/EventMonitor.js +92 -0
- package/dist/protocol/EventMonitor.js.map +1 -0
- package/dist/protocol/MessageSigner.d.ts +23 -0
- package/dist/protocol/MessageSigner.d.ts.map +1 -0
- package/dist/protocol/MessageSigner.js +178 -0
- package/dist/protocol/MessageSigner.js.map +1 -0
- package/dist/protocol/ProofGenerator.d.ts +22 -0
- package/dist/protocol/ProofGenerator.d.ts.map +1 -0
- package/dist/protocol/ProofGenerator.js +64 -0
- package/dist/protocol/ProofGenerator.js.map +1 -0
- package/dist/protocol/QuoteBuilder.d.ts +2 -0
- package/dist/protocol/QuoteBuilder.d.ts.map +1 -0
- package/dist/protocol/QuoteBuilder.js +7 -0
- package/dist/protocol/QuoteBuilder.js.map +1 -0
- package/dist/types/eip712.d.ts +106 -0
- package/dist/types/eip712.d.ts.map +1 -0
- package/dist/types/eip712.js +84 -0
- package/dist/types/eip712.js.map +1 -0
- package/dist/types/escrow.d.ts +18 -0
- package/dist/types/escrow.d.ts.map +1 -0
- package/dist/types/escrow.js +3 -0
- package/dist/types/escrow.js.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +22 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/message.d.ts +109 -0
- package/dist/types/message.d.ts.map +1 -0
- package/dist/types/message.js +3 -0
- package/dist/types/message.js.map +1 -0
- package/dist/types/state.d.ts +19 -0
- package/dist/types/state.d.ts.map +1 -0
- package/dist/types/state.js +49 -0
- package/dist/types/state.js.map +1 -0
- package/dist/types/transaction.d.ts +36 -0
- package/dist/types/transaction.d.ts.map +1 -0
- package/dist/types/transaction.js +3 -0
- package/dist/types/transaction.js.map +1 -0
- package/dist/utils/IPFSClient.d.ts +37 -0
- package/dist/utils/IPFSClient.d.ts.map +1 -0
- package/dist/utils/IPFSClient.js +128 -0
- package/dist/utils/IPFSClient.js.map +1 -0
- package/dist/utils/NonceManager.d.ts +34 -0
- package/dist/utils/NonceManager.d.ts.map +1 -0
- package/dist/utils/NonceManager.js +114 -0
- package/dist/utils/NonceManager.js.map +1 -0
- package/dist/utils/ReceivedNonceTracker.d.ts +35 -0
- package/dist/utils/ReceivedNonceTracker.d.ts.map +1 -0
- package/dist/utils/ReceivedNonceTracker.js +196 -0
- package/dist/utils/ReceivedNonceTracker.js.map +1 -0
- package/dist/utils/canonicalJson.d.ts +4 -0
- package/dist/utils/canonicalJson.d.ts.map +1 -0
- package/dist/utils/canonicalJson.js +21 -0
- package/dist/utils/canonicalJson.js.map +1 -0
- package/dist/utils/computeTypeHash.d.ts +3 -0
- package/dist/utils/computeTypeHash.d.ts.map +1 -0
- package/dist/utils/computeTypeHash.js +30 -0
- package/dist/utils/computeTypeHash.js.map +1 -0
- package/dist/utils/validation.d.ts +6 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +46 -0
- package/dist/utils/validation.js.map +1 -0
- package/package.json +73 -0
- package/src/ACTPClient.ts +276 -0
- package/src/__tests__/ProofGenerator.test.ts +124 -0
- package/src/__tests__/QuoteBuilder.test.ts +516 -0
- package/src/__tests__/StateMachine.test.ts +82 -0
- package/src/__tests__/builders/DeliveryProofBuilder.test.ts +581 -0
- package/src/__tests__/integration/ACTPClient.test.ts +263 -0
- package/src/__tests__/integration.test.ts +289 -0
- package/src/__tests__/protocol/EASHelper.test.ts +472 -0
- package/src/__tests__/protocol/EventMonitor.test.ts +382 -0
- package/src/__tests__/security/ACTPKernel.security.test.ts +1167 -0
- package/src/__tests__/security/EscrowVault.security.test.ts +570 -0
- package/src/__tests__/security/MessageSigner.security.test.ts +286 -0
- package/src/__tests__/security/NonceReplay.security.test.ts +501 -0
- package/src/__tests__/security/validation.security.test.ts +376 -0
- package/src/__tests__/utils/IPFSClient.test.ts +262 -0
- package/src/__tests__/utils/NonceManager.test.ts +205 -0
- package/src/__tests__/utils/canonicalJson.test.ts +153 -0
- package/src/abi/ACTPKernel.json +1340 -0
- package/src/abi/ERC20.json +40 -0
- package/src/abi/EscrowVault.json +66 -0
- package/src/builders/DeliveryProofBuilder.ts +326 -0
- package/src/builders/QuoteBuilder.ts +483 -0
- package/src/builders/index.ts +17 -0
- package/src/config/networks.ts +165 -0
- package/src/errors/index.ts +130 -0
- package/src/index.ts +108 -0
- package/src/protocol/ACTPKernel.ts +625 -0
- package/src/protocol/EASHelper.ts +197 -0
- package/src/protocol/EscrowVault.ts +237 -0
- package/src/protocol/EventMonitor.ts +161 -0
- package/src/protocol/MessageSigner.ts +336 -0
- package/src/protocol/ProofGenerator.ts +119 -0
- package/src/protocol/QuoteBuilder.ts +15 -0
- package/src/types/eip712.ts +175 -0
- package/src/types/escrow.ts +26 -0
- package/src/types/index.ts +10 -0
- package/src/types/message.ts +145 -0
- package/src/types/state.ts +77 -0
- package/src/types/transaction.ts +54 -0
- package/src/utils/IPFSClient.ts +248 -0
- package/src/utils/NonceManager.ts +293 -0
- package/src/utils/ReceivedNonceTracker.ts +397 -0
- package/src/utils/canonicalJson.ts +38 -0
- package/src/utils/computeTypeHash.ts +50 -0
- package/src/utils/validation.ts +82 -0
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QuoteBuilder - AIP-2 Price Quote Construction
|
|
3
|
+
* Reference: AIP-2 §6.1
|
|
4
|
+
*
|
|
5
|
+
* Builds price quotes with:
|
|
6
|
+
* - Amount validation (≥ originalAmount, ≤ maxPrice)
|
|
7
|
+
* - EIP-712 signature
|
|
8
|
+
* - Canonical JSON hashing
|
|
9
|
+
* - Optional IPFS upload
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { Signer, keccak256, toUtf8Bytes, verifyTypedData } from 'ethers';
|
|
13
|
+
import { canonicalJsonStringify } from '../utils/canonicalJson';
|
|
14
|
+
import { IPFSClient } from '../utils/IPFSClient';
|
|
15
|
+
import { NonceManager } from '../utils/NonceManager';
|
|
16
|
+
import { EIP712Domain } from '../types/eip712';
|
|
17
|
+
import { SignatureVerificationError } from '../errors';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Quote message interface (AIP-2)
|
|
21
|
+
* Reference: AIP-2 §2.1
|
|
22
|
+
*/
|
|
23
|
+
export interface QuoteMessage {
|
|
24
|
+
type: 'agirails.quote.v1';
|
|
25
|
+
version: '1.0.0';
|
|
26
|
+
txId: string; // bytes32 (0x-prefixed)
|
|
27
|
+
provider: string; // DID (e.g., "did:ethr:84532:0x...")
|
|
28
|
+
consumer: string; // DID
|
|
29
|
+
quotedAmount: string; // Provider's quoted price (base units, string to avoid JS overflow)
|
|
30
|
+
originalAmount: string; // Consumer's original offer
|
|
31
|
+
maxPrice: string; // Consumer's maximum acceptable price
|
|
32
|
+
currency: string; // Currently "USDC" only
|
|
33
|
+
decimals: number; // Token decimals (6 for USDC)
|
|
34
|
+
quotedAt: number; // Unix timestamp (seconds)
|
|
35
|
+
expiresAt: number; // Unix timestamp (seconds)
|
|
36
|
+
justification?: {
|
|
37
|
+
reason?: string;
|
|
38
|
+
estimatedTime?: number;
|
|
39
|
+
computeCost?: number;
|
|
40
|
+
breakdown?: Record<string, any>;
|
|
41
|
+
};
|
|
42
|
+
chainId: number; // 84532 (Base Sepolia) or 8453 (Base Mainnet)
|
|
43
|
+
nonce: number; // Monotonically increasing per provider + message type
|
|
44
|
+
signature: string; // EIP-712 signature (0x-prefixed, 130 chars)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Quote build parameters
|
|
49
|
+
* Reference: AIP-2 §6.1
|
|
50
|
+
*/
|
|
51
|
+
export interface QuoteParams {
|
|
52
|
+
txId: string;
|
|
53
|
+
provider: string; // DID
|
|
54
|
+
consumer: string; // DID
|
|
55
|
+
quotedAmount: string; // Base units (e.g., "7500000" for $7.50 USDC)
|
|
56
|
+
originalAmount: string; // From AIP-1 request
|
|
57
|
+
maxPrice: string; // From AIP-1 request
|
|
58
|
+
currency?: string; // Default: "USDC"
|
|
59
|
+
decimals?: number; // Default: 6
|
|
60
|
+
expiresAt?: number; // Optional, defaults to +1 hour
|
|
61
|
+
justification?: {
|
|
62
|
+
reason?: string;
|
|
63
|
+
estimatedTime?: number;
|
|
64
|
+
computeCost?: number;
|
|
65
|
+
breakdown?: Record<string, any>;
|
|
66
|
+
};
|
|
67
|
+
chainId: number;
|
|
68
|
+
kernelAddress: string; // ACTPKernel contract address for EIP-712 domain
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* EIP-712 types for AIP-2 quote messages
|
|
73
|
+
* Reference: AIP-2 §3.1
|
|
74
|
+
*/
|
|
75
|
+
export const AIP2QuoteTypes = {
|
|
76
|
+
PriceQuote: [
|
|
77
|
+
{ name: 'txId', type: 'bytes32' },
|
|
78
|
+
{ name: 'provider', type: 'string' },
|
|
79
|
+
{ name: 'consumer', type: 'string' },
|
|
80
|
+
{ name: 'quotedAmount', type: 'string' },
|
|
81
|
+
{ name: 'originalAmount', type: 'string' },
|
|
82
|
+
{ name: 'maxPrice', type: 'string' },
|
|
83
|
+
{ name: 'currency', type: 'string' },
|
|
84
|
+
{ name: 'decimals', type: 'uint8' },
|
|
85
|
+
{ name: 'quotedAt', type: 'uint256' },
|
|
86
|
+
{ name: 'expiresAt', type: 'uint256' },
|
|
87
|
+
{ name: 'justificationHash', type: 'bytes32' },
|
|
88
|
+
{ name: 'chainId', type: 'uint256' },
|
|
89
|
+
{ name: 'nonce', type: 'uint256' }
|
|
90
|
+
]
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* QuoteBuilder - Main Builder Class
|
|
95
|
+
* Reference: AIP-2 §6.1
|
|
96
|
+
*/
|
|
97
|
+
export class QuoteBuilder {
|
|
98
|
+
constructor(
|
|
99
|
+
private signer: Signer,
|
|
100
|
+
private nonceManager: NonceManager,
|
|
101
|
+
private ipfs?: IPFSClient
|
|
102
|
+
) {}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Build and sign a quote message
|
|
106
|
+
* Reference: AIP-2 §4.1 (Provider workflow)
|
|
107
|
+
*
|
|
108
|
+
* @param params - Quote parameters
|
|
109
|
+
* @returns Signed quote message
|
|
110
|
+
*/
|
|
111
|
+
async build(params: QuoteParams): Promise<QuoteMessage> {
|
|
112
|
+
// Validate parameters
|
|
113
|
+
this.validateParams(params);
|
|
114
|
+
|
|
115
|
+
const quotedAt = Math.floor(Date.now() / 1000);
|
|
116
|
+
const expiresAt = params.expiresAt || (quotedAt + 3600); // Default 1 hour
|
|
117
|
+
|
|
118
|
+
// Validate expiry
|
|
119
|
+
if (expiresAt <= quotedAt) {
|
|
120
|
+
throw new Error('expiresAt must be after quotedAt');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (expiresAt > quotedAt + 86400) {
|
|
124
|
+
throw new Error('expiresAt cannot exceed 24 hours from quotedAt');
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Construct quote message (unsigned)
|
|
128
|
+
const quote: QuoteMessage = {
|
|
129
|
+
type: 'agirails.quote.v1',
|
|
130
|
+
version: '1.0.0',
|
|
131
|
+
txId: params.txId,
|
|
132
|
+
provider: params.provider,
|
|
133
|
+
consumer: params.consumer,
|
|
134
|
+
quotedAmount: params.quotedAmount,
|
|
135
|
+
originalAmount: params.originalAmount,
|
|
136
|
+
maxPrice: params.maxPrice,
|
|
137
|
+
currency: params.currency || 'USDC',
|
|
138
|
+
decimals: params.decimals !== undefined ? params.decimals : 6,
|
|
139
|
+
quotedAt,
|
|
140
|
+
expiresAt,
|
|
141
|
+
justification: params.justification,
|
|
142
|
+
chainId: params.chainId,
|
|
143
|
+
nonce: this.nonceManager.getNextNonce('agirails.quote.v1'),
|
|
144
|
+
signature: '' // Filled in next step
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
// Sign with EIP-712
|
|
148
|
+
const signature = await this.signQuote(quote, params.kernelAddress);
|
|
149
|
+
quote.signature = signature;
|
|
150
|
+
|
|
151
|
+
// Record nonce usage
|
|
152
|
+
this.nonceManager.recordNonce('agirails.quote.v1', quote.nonce);
|
|
153
|
+
|
|
154
|
+
return quote;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Verify quote signature and business rules
|
|
159
|
+
* Reference: AIP-2 §5.2, §5.3
|
|
160
|
+
*
|
|
161
|
+
* @param quote - Quote message to verify
|
|
162
|
+
* @param kernelAddress - ACTPKernel contract address
|
|
163
|
+
* @returns true if valid, throws error otherwise
|
|
164
|
+
*/
|
|
165
|
+
async verify(quote: QuoteMessage, kernelAddress: string): Promise<boolean> {
|
|
166
|
+
// 1. Validate schema
|
|
167
|
+
this.validateQuoteSchema(quote);
|
|
168
|
+
|
|
169
|
+
// 2. Verify signature
|
|
170
|
+
const recoveredAddress = this.recoverQuoteSigner(quote, kernelAddress);
|
|
171
|
+
const expectedAddress = this.extractAddressFromDID(quote.provider);
|
|
172
|
+
|
|
173
|
+
if (recoveredAddress.toLowerCase() !== expectedAddress.toLowerCase()) {
|
|
174
|
+
throw new Error('Invalid signature: recovered address does not match provider');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// 3. Validate business rules
|
|
178
|
+
const quotedAmount = BigInt(quote.quotedAmount);
|
|
179
|
+
const originalAmount = BigInt(quote.originalAmount);
|
|
180
|
+
const maxPrice = BigInt(quote.maxPrice);
|
|
181
|
+
|
|
182
|
+
if (quotedAmount < originalAmount) {
|
|
183
|
+
throw new Error('Quoted amount below original amount');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (quotedAmount > maxPrice) {
|
|
187
|
+
throw new Error('Quoted amount exceeds maxPrice');
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Platform minimum: $0.05 = 50000 base units (6 decimals)
|
|
191
|
+
if (quotedAmount < 50000n) {
|
|
192
|
+
throw new Error('Quoted amount below platform minimum ($0.05)');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// 4. Check expiry
|
|
196
|
+
const now = Math.floor(Date.now() / 1000);
|
|
197
|
+
if (quote.expiresAt < now) {
|
|
198
|
+
throw new Error('Quote expired');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// 5. Timestamp freshness check
|
|
202
|
+
if (Math.abs(now - quote.quotedAt) > 300) {
|
|
203
|
+
throw new Error('Quote timestamp outside 5-minute tolerance');
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Upload quote to IPFS and return CID
|
|
211
|
+
* Reference: AIP-2 §6 (optional IPFS storage)
|
|
212
|
+
*
|
|
213
|
+
* @param quote - Quote message
|
|
214
|
+
* @returns IPFS CID
|
|
215
|
+
*/
|
|
216
|
+
async uploadToIPFS(quote: QuoteMessage): Promise<string> {
|
|
217
|
+
if (!this.ipfs) {
|
|
218
|
+
throw new Error('IPFS client not configured');
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const cid = await this.ipfs.add(JSON.stringify(quote));
|
|
222
|
+
await this.ipfs.pin(cid); // Pin for dispute window
|
|
223
|
+
return cid;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Compute quote hash (canonical JSON + keccak256)
|
|
228
|
+
* Used for on-chain storage in transaction metadata
|
|
229
|
+
* Reference: AIP-2 §4.1 (Step 6)
|
|
230
|
+
*
|
|
231
|
+
* @param quote - Quote message
|
|
232
|
+
* @returns Keccak256 hash (0x-prefixed)
|
|
233
|
+
*/
|
|
234
|
+
computeHash(quote: QuoteMessage): string {
|
|
235
|
+
// Remove signature field for hashing
|
|
236
|
+
const { signature, ...quoteWithoutSig } = quote;
|
|
237
|
+
return keccak256(toUtf8Bytes(canonicalJsonStringify(quoteWithoutSig)));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Sign quote message with EIP-712
|
|
242
|
+
* Reference: AIP-2 §3.1, §3.2
|
|
243
|
+
*
|
|
244
|
+
* @param quote - Quote message (unsigned)
|
|
245
|
+
* @param kernelAddress - ACTPKernel contract address
|
|
246
|
+
* @returns EIP-712 signature (0x-prefixed, 130 chars)
|
|
247
|
+
*/
|
|
248
|
+
private async signQuote(quote: QuoteMessage, kernelAddress: string): Promise<string> {
|
|
249
|
+
const domain: EIP712Domain = {
|
|
250
|
+
name: 'AGIRAILS',
|
|
251
|
+
version: '1',
|
|
252
|
+
chainId: quote.chainId,
|
|
253
|
+
verifyingContract: kernelAddress
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
// Compute justification hash
|
|
257
|
+
const justificationHash = this.computeJustificationHash(quote.justification);
|
|
258
|
+
|
|
259
|
+
const message = {
|
|
260
|
+
txId: quote.txId,
|
|
261
|
+
provider: quote.provider,
|
|
262
|
+
consumer: quote.consumer,
|
|
263
|
+
quotedAmount: quote.quotedAmount,
|
|
264
|
+
originalAmount: quote.originalAmount,
|
|
265
|
+
maxPrice: quote.maxPrice,
|
|
266
|
+
currency: quote.currency,
|
|
267
|
+
decimals: quote.decimals,
|
|
268
|
+
quotedAt: quote.quotedAt,
|
|
269
|
+
expiresAt: quote.expiresAt,
|
|
270
|
+
justificationHash,
|
|
271
|
+
chainId: quote.chainId,
|
|
272
|
+
nonce: quote.nonce
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
// Sign using ethers.js signTypedData (v6 API)
|
|
276
|
+
if ('signTypedData' in this.signer && typeof (this.signer as any).signTypedData === 'function') {
|
|
277
|
+
const signature = await (this.signer as any).signTypedData(domain, AIP2QuoteTypes, message);
|
|
278
|
+
return signature;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
throw new Error('Signer does not support EIP-712 typed data signing');
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Recover signer address from quote signature
|
|
286
|
+
* Reference: AIP-2 §5.3
|
|
287
|
+
*
|
|
288
|
+
* @param quote - Quote message
|
|
289
|
+
* @param kernelAddress - ACTPKernel contract address
|
|
290
|
+
* @returns Recovered Ethereum address
|
|
291
|
+
*/
|
|
292
|
+
private recoverQuoteSigner(quote: QuoteMessage, kernelAddress: string): string {
|
|
293
|
+
const domain: EIP712Domain = {
|
|
294
|
+
name: 'AGIRAILS',
|
|
295
|
+
version: '1',
|
|
296
|
+
chainId: quote.chainId,
|
|
297
|
+
verifyingContract: kernelAddress
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
const justificationHash = this.computeJustificationHash(quote.justification);
|
|
301
|
+
|
|
302
|
+
const message = {
|
|
303
|
+
txId: quote.txId,
|
|
304
|
+
provider: quote.provider,
|
|
305
|
+
consumer: quote.consumer,
|
|
306
|
+
quotedAmount: quote.quotedAmount,
|
|
307
|
+
originalAmount: quote.originalAmount,
|
|
308
|
+
maxPrice: quote.maxPrice,
|
|
309
|
+
currency: quote.currency,
|
|
310
|
+
decimals: quote.decimals,
|
|
311
|
+
quotedAt: quote.quotedAt,
|
|
312
|
+
expiresAt: quote.expiresAt,
|
|
313
|
+
justificationHash,
|
|
314
|
+
chainId: quote.chainId,
|
|
315
|
+
nonce: quote.nonce
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
// Recover address using ethers.js verifyTypedData
|
|
319
|
+
// Wrap in try-catch to handle low-level cryptographic errors gracefully
|
|
320
|
+
try {
|
|
321
|
+
const recoveredAddress = verifyTypedData(domain, AIP2QuoteTypes, message, quote.signature);
|
|
322
|
+
return recoveredAddress;
|
|
323
|
+
} catch (error: any) {
|
|
324
|
+
// Wrap low-level cryptographic errors in SignatureVerificationError
|
|
325
|
+
throw new SignatureVerificationError(
|
|
326
|
+
quote.provider,
|
|
327
|
+
'unknown',
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Compute justification hash for EIP-712 signature
|
|
334
|
+
* Reference: AIP-2 §3.2
|
|
335
|
+
*
|
|
336
|
+
* @param justification - Optional justification object
|
|
337
|
+
* @returns Keccak256 hash (0x-prefixed), or zero hash if omitted
|
|
338
|
+
*/
|
|
339
|
+
private computeJustificationHash(justification: object | undefined): string {
|
|
340
|
+
if (!justification || Object.keys(justification).length === 0) {
|
|
341
|
+
return '0x0000000000000000000000000000000000000000000000000000000000000000';
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return keccak256(toUtf8Bytes(canonicalJsonStringify(justification)));
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Validate quote parameters
|
|
349
|
+
* Reference: AIP-2 §5.1, §5.2
|
|
350
|
+
*
|
|
351
|
+
* @param params - Quote parameters
|
|
352
|
+
* @throws Error if validation fails
|
|
353
|
+
*/
|
|
354
|
+
private validateParams(params: QuoteParams): void {
|
|
355
|
+
// Amount validation
|
|
356
|
+
const quotedAmount = BigInt(params.quotedAmount);
|
|
357
|
+
const originalAmount = BigInt(params.originalAmount);
|
|
358
|
+
const maxPrice = BigInt(params.maxPrice);
|
|
359
|
+
|
|
360
|
+
if (quotedAmount < originalAmount) {
|
|
361
|
+
throw new Error('quotedAmount must be >= originalAmount');
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (quotedAmount > maxPrice) {
|
|
365
|
+
throw new Error('quotedAmount must be <= maxPrice');
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// Platform minimum: $0.05 = 50000 base units (6 decimals)
|
|
369
|
+
if (quotedAmount < 50000n) {
|
|
370
|
+
throw new Error('quotedAmount must be >= $0.05 (50000 base units)');
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Expiry validation
|
|
374
|
+
if (params.expiresAt) {
|
|
375
|
+
const now = Math.floor(Date.now() / 1000);
|
|
376
|
+
if (params.expiresAt <= now) {
|
|
377
|
+
throw new Error('expiresAt must be in the future');
|
|
378
|
+
}
|
|
379
|
+
if (params.expiresAt > now + 86400) {
|
|
380
|
+
throw new Error('expiresAt cannot be more than 24 hours in future');
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// DID format validation
|
|
385
|
+
if (!params.provider.startsWith('did:ethr:')) {
|
|
386
|
+
throw new Error('provider must be valid did:ethr format');
|
|
387
|
+
}
|
|
388
|
+
if (!params.consumer.startsWith('did:ethr:')) {
|
|
389
|
+
throw new Error('consumer must be valid did:ethr format');
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// Transaction ID format
|
|
393
|
+
if (!/^0x[a-fA-F0-9]{64}$/.test(params.txId)) {
|
|
394
|
+
throw new Error('txId must be valid bytes32 hex string');
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// Kernel address format
|
|
398
|
+
if (!/^0x[a-fA-F0-9]{40}$/.test(params.kernelAddress)) {
|
|
399
|
+
throw new Error('kernelAddress must be valid Ethereum address');
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// ChainId validation
|
|
403
|
+
if (params.chainId !== 84532 && params.chainId !== 8453) {
|
|
404
|
+
throw new Error('chainId must be 84532 (Base Sepolia) or 8453 (Base Mainnet)');
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Validate quote message schema
|
|
410
|
+
* Reference: AIP-2 §2.1, §5.1
|
|
411
|
+
*
|
|
412
|
+
* @param quote - Quote message
|
|
413
|
+
* @throws Error if validation fails
|
|
414
|
+
*/
|
|
415
|
+
private validateQuoteSchema(quote: QuoteMessage): void {
|
|
416
|
+
if (quote.type !== 'agirails.quote.v1') {
|
|
417
|
+
throw new Error('Invalid message type');
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (!/^\d+\.\d+\.\d+$/.test(quote.version)) {
|
|
421
|
+
throw new Error('Invalid version format');
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
if (!/^0x[a-fA-F0-9]{64}$/.test(quote.txId)) {
|
|
425
|
+
throw new Error('Invalid txId format');
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (!quote.provider.startsWith('did:ethr:')) {
|
|
429
|
+
throw new Error('Invalid provider DID format');
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
if (!quote.consumer.startsWith('did:ethr:')) {
|
|
433
|
+
throw new Error('Invalid consumer DID format');
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
if (!/^\d+$/.test(quote.quotedAmount)) {
|
|
437
|
+
throw new Error('Invalid quotedAmount format (must be numeric string)');
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
if (!/^\d+$/.test(quote.originalAmount)) {
|
|
441
|
+
throw new Error('Invalid originalAmount format (must be numeric string)');
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
if (!/^\d+$/.test(quote.maxPrice)) {
|
|
445
|
+
throw new Error('Invalid maxPrice format (must be numeric string)');
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
if (quote.currency !== 'USDC') {
|
|
449
|
+
throw new Error('Only USDC currency is supported');
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
if (quote.decimals !== 6) {
|
|
453
|
+
throw new Error('USDC must use 6 decimals');
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (quote.chainId !== 84532 && quote.chainId !== 8453) {
|
|
457
|
+
throw new Error('Invalid chainId');
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
if (!/^0x[a-fA-F0-9]{130}$/.test(quote.signature)) {
|
|
461
|
+
throw new Error('Invalid signature format');
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Extract Ethereum address from DID
|
|
467
|
+
* Supports: did:ethr:0x... and did:ethr:84532:0x...
|
|
468
|
+
* Reference: AIP-0 §2.1 (DID format)
|
|
469
|
+
*
|
|
470
|
+
* @param did - DID string
|
|
471
|
+
* @returns Ethereum address (0x-prefixed)
|
|
472
|
+
*/
|
|
473
|
+
private extractAddressFromDID(did: string): string {
|
|
474
|
+
const parts = did.replace('did:ethr:', '').split(':');
|
|
475
|
+
const address = parts.length === 2 ? parts[1] : parts[0];
|
|
476
|
+
|
|
477
|
+
if (!address.startsWith('0x') || address.length !== 42) {
|
|
478
|
+
throw new Error(`Invalid DID format: ${did}`);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
return address;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ACTP SDK Builders
|
|
3
|
+
* High-level builder classes for constructing ACTP messages
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export {
|
|
7
|
+
DeliveryProofBuilder,
|
|
8
|
+
DeliveryProofParams,
|
|
9
|
+
AGIRAILS_DELIVERY_SCHEMA_UID
|
|
10
|
+
} from './DeliveryProofBuilder';
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
QuoteBuilder,
|
|
14
|
+
QuoteParams,
|
|
15
|
+
QuoteMessage,
|
|
16
|
+
AIP2QuoteTypes
|
|
17
|
+
} from './QuoteBuilder';
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Network configuration
|
|
5
|
+
*/
|
|
6
|
+
export interface NetworkConfig {
|
|
7
|
+
name: string;
|
|
8
|
+
chainId: number;
|
|
9
|
+
rpcUrl: string;
|
|
10
|
+
blockExplorer: string;
|
|
11
|
+
contracts: {
|
|
12
|
+
actpKernel: string;
|
|
13
|
+
escrowVault: string;
|
|
14
|
+
usdc: string;
|
|
15
|
+
eas: string; // EAS contract address
|
|
16
|
+
easSchemaRegistry: string; // EAS SchemaRegistry contract
|
|
17
|
+
};
|
|
18
|
+
eas: {
|
|
19
|
+
deliverySchemaUID: string; // AIP-4 delivery proof schema
|
|
20
|
+
};
|
|
21
|
+
gasSettings: {
|
|
22
|
+
maxFeePerGas: bigint;
|
|
23
|
+
maxPriorityFeePerGas: bigint;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Base Sepolia Testnet Configuration
|
|
29
|
+
*/
|
|
30
|
+
export const BASE_SEPOLIA: NetworkConfig = {
|
|
31
|
+
name: 'Base Sepolia',
|
|
32
|
+
chainId: 84532,
|
|
33
|
+
rpcUrl: 'https://sepolia.base.org',
|
|
34
|
+
blockExplorer: 'https://sepolia.basescan.org',
|
|
35
|
+
contracts: {
|
|
36
|
+
// Redeployed 2025-11-25 by Arha (fixed auto-transition in linkEscrow, optimizer-runs 200)
|
|
37
|
+
actpKernel: '0x6aDB650e185b0ee77981AC5279271f0Fa6CFe7ba',
|
|
38
|
+
escrowVault: '0x921edE340770db5DB6059B5B866be987d1b7311F',
|
|
39
|
+
usdc: '0x444b4e1A65949AB2ac75979D5d0166Eb7A248Ccb', // MockUSDC
|
|
40
|
+
// EAS contracts (Base native deployment)
|
|
41
|
+
eas: '0x4200000000000000000000000000000000000021',
|
|
42
|
+
easSchemaRegistry: '0x4200000000000000000000000000000000000020'
|
|
43
|
+
},
|
|
44
|
+
eas: {
|
|
45
|
+
// Deployed 2025-11-23 - AIP-4 delivery proof schema
|
|
46
|
+
deliverySchemaUID: '0x1b0ebdf0bd20c28ec9d5362571ce8715a55f46e81c3de2f9b0d8e1b95fb5ffce'
|
|
47
|
+
},
|
|
48
|
+
gasSettings: {
|
|
49
|
+
maxFeePerGas: ethers.parseUnits('2', 'gwei'),
|
|
50
|
+
maxPriorityFeePerGas: ethers.parseUnits('1', 'gwei')
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Base Mainnet Configuration
|
|
56
|
+
*/
|
|
57
|
+
export const BASE_MAINNET: NetworkConfig = {
|
|
58
|
+
name: 'Base Mainnet',
|
|
59
|
+
chainId: 8453,
|
|
60
|
+
rpcUrl: 'https://mainnet.base.org',
|
|
61
|
+
blockExplorer: 'https://basescan.org',
|
|
62
|
+
contracts: {
|
|
63
|
+
// TODO: Update after mainnet deployment
|
|
64
|
+
actpKernel: '0x0000000000000000000000000000000000000000',
|
|
65
|
+
escrowVault: '0x0000000000000000000000000000000000000000',
|
|
66
|
+
usdc: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', // Official USDC on Base
|
|
67
|
+
// EAS contracts (Base native deployment)
|
|
68
|
+
eas: '0x4200000000000000000000000000000000000021',
|
|
69
|
+
easSchemaRegistry: '0x4200000000000000000000000000000000000020'
|
|
70
|
+
},
|
|
71
|
+
eas: {
|
|
72
|
+
// TODO: Deploy delivery schema to mainnet
|
|
73
|
+
deliverySchemaUID: '0x0000000000000000000000000000000000000000000000000000000000000000'
|
|
74
|
+
},
|
|
75
|
+
gasSettings: {
|
|
76
|
+
maxFeePerGas: ethers.parseUnits('0.5', 'gwei'),
|
|
77
|
+
maxPriorityFeePerGas: ethers.parseUnits('0.1', 'gwei')
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* All supported networks
|
|
83
|
+
*/
|
|
84
|
+
export const NETWORKS: Record<string, NetworkConfig> = {
|
|
85
|
+
'base-sepolia': BASE_SEPOLIA,
|
|
86
|
+
'base-mainnet': BASE_MAINNET
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Get network configuration by name (returns deep clone to prevent mutation)
|
|
91
|
+
*/
|
|
92
|
+
export function getNetwork(network: string): NetworkConfig {
|
|
93
|
+
const config = NETWORKS[network];
|
|
94
|
+
if (!config) {
|
|
95
|
+
throw new Error(
|
|
96
|
+
`Unknown network: ${network}. Supported networks: ${Object.keys(NETWORKS).join(', ')}`
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Validate that contracts are deployed (not zero addresses)
|
|
101
|
+
validateNetworkConfig(config);
|
|
102
|
+
|
|
103
|
+
// Deep clone to prevent global mutation
|
|
104
|
+
return {
|
|
105
|
+
name: config.name,
|
|
106
|
+
chainId: config.chainId,
|
|
107
|
+
rpcUrl: config.rpcUrl,
|
|
108
|
+
blockExplorer: config.blockExplorer,
|
|
109
|
+
contracts: {
|
|
110
|
+
actpKernel: config.contracts.actpKernel,
|
|
111
|
+
escrowVault: config.contracts.escrowVault,
|
|
112
|
+
usdc: config.contracts.usdc,
|
|
113
|
+
eas: config.contracts.eas,
|
|
114
|
+
easSchemaRegistry: config.contracts.easSchemaRegistry
|
|
115
|
+
},
|
|
116
|
+
eas: {
|
|
117
|
+
deliverySchemaUID: config.eas.deliverySchemaUID
|
|
118
|
+
},
|
|
119
|
+
gasSettings: {
|
|
120
|
+
maxFeePerGas: config.gasSettings.maxFeePerGas,
|
|
121
|
+
maxPriorityFeePerGas: config.gasSettings.maxPriorityFeePerGas
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Validate network name
|
|
128
|
+
*/
|
|
129
|
+
export function isValidNetwork(network: string): boolean {
|
|
130
|
+
return network in NETWORKS;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Validate that contract addresses are deployed (not zero addresses)
|
|
135
|
+
*
|
|
136
|
+
* @throws Error if any contract address is zero (0x000...000)
|
|
137
|
+
*/
|
|
138
|
+
export function validateNetworkConfig(config: NetworkConfig): void {
|
|
139
|
+
const zeroAddress = '0x0000000000000000000000000000000000000000';
|
|
140
|
+
const errors: string[] = [];
|
|
141
|
+
|
|
142
|
+
if (config.contracts.actpKernel === zeroAddress) {
|
|
143
|
+
errors.push('ACTPKernel address is zero - contracts not yet deployed to this network');
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (config.contracts.escrowVault === zeroAddress) {
|
|
147
|
+
errors.push('EscrowVault address is zero - contracts not yet deployed to this network');
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (config.contracts.usdc === zeroAddress) {
|
|
151
|
+
errors.push('USDC address is zero - token contract not configured for this network');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (errors.length > 0) {
|
|
155
|
+
throw new Error(
|
|
156
|
+
`Network configuration error for ${config.name} (chainId: ${config.chainId}):\n\n` +
|
|
157
|
+
errors.map(e => ` ✗ ${e}`).join('\n') +
|
|
158
|
+
`\n\nContracts must be deployed before using the SDK. Please:\n` +
|
|
159
|
+
` 1. Deploy contracts to ${config.name}\n` +
|
|
160
|
+
` 2. Update src/config/networks.ts with deployed addresses\n` +
|
|
161
|
+
` 3. Rebuild the SDK: npm run build`
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|