@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.
Files changed (154) hide show
  1. package/README.md +183 -0
  2. package/dist/ACTPClient.d.ts +52 -0
  3. package/dist/ACTPClient.d.ts.map +1 -0
  4. package/dist/ACTPClient.js +120 -0
  5. package/dist/ACTPClient.js.map +1 -0
  6. package/dist/abi/ACTPKernel.json +1340 -0
  7. package/dist/abi/ERC20.json +38 -0
  8. package/dist/abi/EscrowVault.json +64 -0
  9. package/dist/builders/DeliveryProofBuilder.d.ts +37 -0
  10. package/dist/builders/DeliveryProofBuilder.d.ts.map +1 -0
  11. package/dist/builders/DeliveryProofBuilder.js +165 -0
  12. package/dist/builders/DeliveryProofBuilder.js.map +1 -0
  13. package/dist/builders/QuoteBuilder.d.ts +68 -0
  14. package/dist/builders/QuoteBuilder.d.ts.map +1 -0
  15. package/dist/builders/QuoteBuilder.js +255 -0
  16. package/dist/builders/QuoteBuilder.js.map +1 -0
  17. package/dist/builders/index.d.ts +3 -0
  18. package/dist/builders/index.d.ts.map +1 -0
  19. package/dist/builders/index.js +10 -0
  20. package/dist/builders/index.js.map +1 -0
  21. package/dist/config/networks.d.ts +27 -0
  22. package/dist/config/networks.d.ts.map +1 -0
  23. package/dist/config/networks.js +103 -0
  24. package/dist/config/networks.js.map +1 -0
  25. package/dist/errors/index.d.ts +38 -0
  26. package/dist/errors/index.d.ts.map +1 -0
  27. package/dist/errors/index.js +87 -0
  28. package/dist/errors/index.js.map +1 -0
  29. package/dist/index.d.ts +19 -0
  30. package/dist/index.d.ts.map +1 -0
  31. package/dist/index.js +68 -0
  32. package/dist/index.js.map +1 -0
  33. package/dist/protocol/ACTPKernel.d.ts +30 -0
  34. package/dist/protocol/ACTPKernel.d.ts.map +1 -0
  35. package/dist/protocol/ACTPKernel.js +261 -0
  36. package/dist/protocol/ACTPKernel.js.map +1 -0
  37. package/dist/protocol/EASHelper.d.ts +23 -0
  38. package/dist/protocol/EASHelper.d.ts.map +1 -0
  39. package/dist/protocol/EASHelper.js +106 -0
  40. package/dist/protocol/EASHelper.js.map +1 -0
  41. package/dist/protocol/EscrowVault.d.ts +24 -0
  42. package/dist/protocol/EscrowVault.d.ts.map +1 -0
  43. package/dist/protocol/EscrowVault.js +114 -0
  44. package/dist/protocol/EscrowVault.js.map +1 -0
  45. package/dist/protocol/EventMonitor.d.ts +18 -0
  46. package/dist/protocol/EventMonitor.d.ts.map +1 -0
  47. package/dist/protocol/EventMonitor.js +92 -0
  48. package/dist/protocol/EventMonitor.js.map +1 -0
  49. package/dist/protocol/MessageSigner.d.ts +23 -0
  50. package/dist/protocol/MessageSigner.d.ts.map +1 -0
  51. package/dist/protocol/MessageSigner.js +178 -0
  52. package/dist/protocol/MessageSigner.js.map +1 -0
  53. package/dist/protocol/ProofGenerator.d.ts +22 -0
  54. package/dist/protocol/ProofGenerator.d.ts.map +1 -0
  55. package/dist/protocol/ProofGenerator.js +64 -0
  56. package/dist/protocol/ProofGenerator.js.map +1 -0
  57. package/dist/protocol/QuoteBuilder.d.ts +2 -0
  58. package/dist/protocol/QuoteBuilder.d.ts.map +1 -0
  59. package/dist/protocol/QuoteBuilder.js +7 -0
  60. package/dist/protocol/QuoteBuilder.js.map +1 -0
  61. package/dist/types/eip712.d.ts +106 -0
  62. package/dist/types/eip712.d.ts.map +1 -0
  63. package/dist/types/eip712.js +84 -0
  64. package/dist/types/eip712.js.map +1 -0
  65. package/dist/types/escrow.d.ts +18 -0
  66. package/dist/types/escrow.d.ts.map +1 -0
  67. package/dist/types/escrow.js +3 -0
  68. package/dist/types/escrow.js.map +1 -0
  69. package/dist/types/index.d.ts +6 -0
  70. package/dist/types/index.d.ts.map +1 -0
  71. package/dist/types/index.js +22 -0
  72. package/dist/types/index.js.map +1 -0
  73. package/dist/types/message.d.ts +109 -0
  74. package/dist/types/message.d.ts.map +1 -0
  75. package/dist/types/message.js +3 -0
  76. package/dist/types/message.js.map +1 -0
  77. package/dist/types/state.d.ts +19 -0
  78. package/dist/types/state.d.ts.map +1 -0
  79. package/dist/types/state.js +49 -0
  80. package/dist/types/state.js.map +1 -0
  81. package/dist/types/transaction.d.ts +36 -0
  82. package/dist/types/transaction.d.ts.map +1 -0
  83. package/dist/types/transaction.js +3 -0
  84. package/dist/types/transaction.js.map +1 -0
  85. package/dist/utils/IPFSClient.d.ts +37 -0
  86. package/dist/utils/IPFSClient.d.ts.map +1 -0
  87. package/dist/utils/IPFSClient.js +128 -0
  88. package/dist/utils/IPFSClient.js.map +1 -0
  89. package/dist/utils/NonceManager.d.ts +34 -0
  90. package/dist/utils/NonceManager.d.ts.map +1 -0
  91. package/dist/utils/NonceManager.js +114 -0
  92. package/dist/utils/NonceManager.js.map +1 -0
  93. package/dist/utils/ReceivedNonceTracker.d.ts +35 -0
  94. package/dist/utils/ReceivedNonceTracker.d.ts.map +1 -0
  95. package/dist/utils/ReceivedNonceTracker.js +196 -0
  96. package/dist/utils/ReceivedNonceTracker.js.map +1 -0
  97. package/dist/utils/canonicalJson.d.ts +4 -0
  98. package/dist/utils/canonicalJson.d.ts.map +1 -0
  99. package/dist/utils/canonicalJson.js +21 -0
  100. package/dist/utils/canonicalJson.js.map +1 -0
  101. package/dist/utils/computeTypeHash.d.ts +3 -0
  102. package/dist/utils/computeTypeHash.d.ts.map +1 -0
  103. package/dist/utils/computeTypeHash.js +30 -0
  104. package/dist/utils/computeTypeHash.js.map +1 -0
  105. package/dist/utils/validation.d.ts +6 -0
  106. package/dist/utils/validation.d.ts.map +1 -0
  107. package/dist/utils/validation.js +46 -0
  108. package/dist/utils/validation.js.map +1 -0
  109. package/package.json +73 -0
  110. package/src/ACTPClient.ts +276 -0
  111. package/src/__tests__/ProofGenerator.test.ts +124 -0
  112. package/src/__tests__/QuoteBuilder.test.ts +516 -0
  113. package/src/__tests__/StateMachine.test.ts +82 -0
  114. package/src/__tests__/builders/DeliveryProofBuilder.test.ts +581 -0
  115. package/src/__tests__/integration/ACTPClient.test.ts +263 -0
  116. package/src/__tests__/integration.test.ts +289 -0
  117. package/src/__tests__/protocol/EASHelper.test.ts +472 -0
  118. package/src/__tests__/protocol/EventMonitor.test.ts +382 -0
  119. package/src/__tests__/security/ACTPKernel.security.test.ts +1167 -0
  120. package/src/__tests__/security/EscrowVault.security.test.ts +570 -0
  121. package/src/__tests__/security/MessageSigner.security.test.ts +286 -0
  122. package/src/__tests__/security/NonceReplay.security.test.ts +501 -0
  123. package/src/__tests__/security/validation.security.test.ts +376 -0
  124. package/src/__tests__/utils/IPFSClient.test.ts +262 -0
  125. package/src/__tests__/utils/NonceManager.test.ts +205 -0
  126. package/src/__tests__/utils/canonicalJson.test.ts +153 -0
  127. package/src/abi/ACTPKernel.json +1340 -0
  128. package/src/abi/ERC20.json +40 -0
  129. package/src/abi/EscrowVault.json +66 -0
  130. package/src/builders/DeliveryProofBuilder.ts +326 -0
  131. package/src/builders/QuoteBuilder.ts +483 -0
  132. package/src/builders/index.ts +17 -0
  133. package/src/config/networks.ts +165 -0
  134. package/src/errors/index.ts +130 -0
  135. package/src/index.ts +108 -0
  136. package/src/protocol/ACTPKernel.ts +625 -0
  137. package/src/protocol/EASHelper.ts +197 -0
  138. package/src/protocol/EscrowVault.ts +237 -0
  139. package/src/protocol/EventMonitor.ts +161 -0
  140. package/src/protocol/MessageSigner.ts +336 -0
  141. package/src/protocol/ProofGenerator.ts +119 -0
  142. package/src/protocol/QuoteBuilder.ts +15 -0
  143. package/src/types/eip712.ts +175 -0
  144. package/src/types/escrow.ts +26 -0
  145. package/src/types/index.ts +10 -0
  146. package/src/types/message.ts +145 -0
  147. package/src/types/state.ts +77 -0
  148. package/src/types/transaction.ts +54 -0
  149. package/src/utils/IPFSClient.ts +248 -0
  150. package/src/utils/NonceManager.ts +293 -0
  151. package/src/utils/ReceivedNonceTracker.ts +397 -0
  152. package/src/utils/canonicalJson.ts +38 -0
  153. package/src/utils/computeTypeHash.ts +50 -0
  154. 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
+