@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,38 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compute EIP-712 Type Hash for AIP-4 Delivery Proof
|
|
3
|
+
* Reference: AIP-4 §3.3, EIP-712 specification
|
|
4
|
+
*
|
|
5
|
+
* Run with: npx tsx src/utils/computeTypeHash.ts
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { TypedDataEncoder, keccak256, toUtf8Bytes } from 'ethers';
|
|
9
|
+
import { AIP4DeliveryProofTypes } from '../types/eip712';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Compute EIP-712 type hash for DeliveryProof
|
|
13
|
+
* @returns Type hash (bytes32)
|
|
14
|
+
*/
|
|
15
|
+
export function computeDeliveryProofTypeHash(): string {
|
|
16
|
+
const encoder = TypedDataEncoder.from(AIP4DeliveryProofTypes);
|
|
17
|
+
const typeString = encoder.encodeType('DeliveryProof');
|
|
18
|
+
|
|
19
|
+
// Type hash is keccak256 of the encoded type string
|
|
20
|
+
const typeHash = keccak256(toUtf8Bytes(typeString));
|
|
21
|
+
|
|
22
|
+
return typeHash;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Get encoded type string for DeliveryProof
|
|
27
|
+
* @returns Type string (for documentation)
|
|
28
|
+
*/
|
|
29
|
+
export function getDeliveryProofTypeString(): string {
|
|
30
|
+
return TypedDataEncoder.from(AIP4DeliveryProofTypes).encodeType('DeliveryProof');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// If run directly, compute and print type hash
|
|
34
|
+
if (require.main === module) {
|
|
35
|
+
console.log('=== AIP-4 Delivery Proof EIP-712 Type Hash ===\n');
|
|
36
|
+
|
|
37
|
+
const typeString = getDeliveryProofTypeString();
|
|
38
|
+
console.log('Type String:');
|
|
39
|
+
console.log(typeString);
|
|
40
|
+
console.log();
|
|
41
|
+
|
|
42
|
+
const typeHash = computeDeliveryProofTypeHash();
|
|
43
|
+
console.log('Type Hash (bytes32):');
|
|
44
|
+
console.log(typeHash);
|
|
45
|
+
console.log();
|
|
46
|
+
|
|
47
|
+
console.log('✅ Update this value in:');
|
|
48
|
+
console.log('- /Testnet/docs/AIP-4.md:204');
|
|
49
|
+
console.log('- /Testnet/docs/schemas/aip-4-delivery.eip712.json:11');
|
|
50
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { isAddress, getAddress } from 'ethers';
|
|
2
|
+
import {
|
|
3
|
+
InvalidAddressError,
|
|
4
|
+
InvalidAmountError,
|
|
5
|
+
ValidationError
|
|
6
|
+
} from '../errors';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Input validation utilities
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Validate Ethereum address
|
|
14
|
+
*/
|
|
15
|
+
export function validateAddress(address: string, fieldName: string = 'address'): void {
|
|
16
|
+
if (!address || !isAddress(address)) {
|
|
17
|
+
throw new InvalidAddressError(address);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (address === getAddress('0x0000000000000000000000000000000000000000')) {
|
|
21
|
+
throw new ValidationError(fieldName, 'Address cannot be zero address');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Validate amount (must be > 0)
|
|
27
|
+
*/
|
|
28
|
+
export function validateAmount(amount: bigint, _fieldName: string = 'amount'): void {
|
|
29
|
+
// Handle null/undefined before calling toString()
|
|
30
|
+
if (!amount) {
|
|
31
|
+
throw new InvalidAmountError(String(amount)); // Convert safely to string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (amount <= 0n) {
|
|
35
|
+
throw new InvalidAmountError(amount.toString());
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Validate deadline (must be future timestamp)
|
|
41
|
+
*/
|
|
42
|
+
export function validateDeadline(deadline: number, fieldName: string = 'deadline'): void {
|
|
43
|
+
const now = Math.floor(Date.now() / 1000);
|
|
44
|
+
|
|
45
|
+
if (deadline <= now) {
|
|
46
|
+
throw new ValidationError(
|
|
47
|
+
fieldName,
|
|
48
|
+
`Deadline must be in the future (now: ${now}, deadline: ${deadline})`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Validate dispute window (max 30 days per spec)
|
|
55
|
+
*/
|
|
56
|
+
export function validateDisputeWindow(
|
|
57
|
+
disputeWindow: number,
|
|
58
|
+
fieldName: string = 'disputeWindow'
|
|
59
|
+
): void {
|
|
60
|
+
const MAX_DISPUTE_WINDOW = 30 * 24 * 60 * 60; // 30 days in seconds
|
|
61
|
+
|
|
62
|
+
if (disputeWindow < 0) {
|
|
63
|
+
throw new ValidationError(fieldName, 'Dispute window cannot be negative');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (disputeWindow > MAX_DISPUTE_WINDOW) {
|
|
67
|
+
throw new ValidationError(
|
|
68
|
+
fieldName,
|
|
69
|
+
`Dispute window exceeds maximum (${MAX_DISPUTE_WINDOW}s = 30 days)`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Validate transaction ID format
|
|
76
|
+
*/
|
|
77
|
+
export function validateTxId(txId: string, fieldName: string = 'txId'): void {
|
|
78
|
+
if (!txId || !txId.match(/^0x[a-fA-F0-9]{64}$/)) {
|
|
79
|
+
throw new ValidationError(fieldName, 'Invalid transaction ID format (expected bytes32)');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|