@veilo/sdk-core 0.3.3 → 0.5.0

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 (251) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +289 -1262
  3. package/dist/cjs/accounts/admin.d.ts +84 -0
  4. package/dist/cjs/accounts/admin.js +208 -0
  5. package/dist/cjs/accounts/errors.d.ts +6 -0
  6. package/dist/cjs/accounts/errors.js +98 -0
  7. package/dist/cjs/accounts/index.d.ts +4 -0
  8. package/dist/cjs/accounts/index.js +20 -0
  9. package/dist/cjs/accounts/pdas.d.ts +23 -0
  10. package/dist/cjs/accounts/pdas.js +44 -0
  11. package/dist/cjs/accounts/queries.d.ts +41 -0
  12. package/dist/cjs/accounts/queries.js +95 -0
  13. package/dist/cjs/client.d.ts +2 -407
  14. package/dist/cjs/client.js +17 -912
  15. package/dist/cjs/cloak/client.d.ts +28 -0
  16. package/dist/cjs/cloak/client.js +68 -0
  17. package/dist/cjs/cloak/errors.d.ts +13 -0
  18. package/dist/cjs/cloak/errors.js +25 -0
  19. package/dist/cjs/cloak/helpers.d.ts +6 -0
  20. package/dist/cjs/cloak/helpers.js +35 -0
  21. package/dist/cjs/cloak/index.d.ts +6 -0
  22. package/dist/cjs/cloak/index.js +24 -0
  23. package/dist/cjs/cloak/polling.d.ts +7 -0
  24. package/dist/cjs/cloak/polling.js +66 -0
  25. package/dist/cjs/cloak/transport.d.ts +15 -0
  26. package/dist/cjs/cloak/transport.js +119 -0
  27. package/dist/cjs/cloak/types.d.ts +188 -0
  28. package/dist/cjs/cloak/types.js +5 -0
  29. package/dist/cjs/cloak.d.ts +1 -0
  30. package/dist/cjs/cloak.js +18 -0
  31. package/dist/cjs/compactNote.d.ts +128 -0
  32. package/dist/cjs/compactNote.js +191 -0
  33. package/dist/cjs/events.d.ts +1 -1
  34. package/dist/cjs/events.js +2 -2
  35. package/dist/cjs/idl/privacy_pool.d.ts +5 -0
  36. package/dist/cjs/idl/privacy_pool.js +15218 -0
  37. package/dist/cjs/index.d.ts +17 -13
  38. package/dist/cjs/index.js +43 -30
  39. package/dist/cjs/merkle.d.ts +13 -0
  40. package/dist/cjs/merkle.js +31 -8
  41. package/dist/cjs/notes/encryption.d.ts +41 -0
  42. package/dist/cjs/notes/encryption.js +75 -0
  43. package/dist/cjs/notes/index.d.ts +5 -0
  44. package/dist/cjs/notes/index.js +21 -0
  45. package/dist/cjs/notes/mailbox.d.ts +70 -0
  46. package/dist/cjs/notes/mailbox.js +164 -0
  47. package/dist/cjs/notes/model.d.ts +91 -0
  48. package/dist/cjs/notes/model.js +109 -0
  49. package/dist/cjs/notes/nullifier.d.ts +27 -0
  50. package/dist/cjs/notes/nullifier.js +45 -0
  51. package/dist/cjs/notes/recovery.d.ts +10 -0
  52. package/dist/cjs/notes/recovery.js +31 -0
  53. package/dist/cjs/program.d.ts +11 -0
  54. package/dist/cjs/program.js +26 -3
  55. package/dist/cjs/proof.d.ts +1 -183
  56. package/dist/cjs/proof.js +16 -290
  57. package/dist/cjs/proofs/encoding.d.ts +17 -0
  58. package/dist/cjs/proofs/encoding.js +72 -0
  59. package/dist/cjs/proofs/formatting.d.ts +6 -0
  60. package/dist/cjs/proofs/formatting.js +34 -0
  61. package/dist/cjs/proofs/index.d.ts +5 -0
  62. package/dist/cjs/proofs/index.js +21 -0
  63. package/dist/cjs/proofs/swap.d.ts +87 -0
  64. package/dist/cjs/proofs/swap.js +144 -0
  65. package/dist/cjs/proofs/transaction.d.ts +28 -0
  66. package/dist/cjs/proofs/transaction.js +110 -0
  67. package/dist/cjs/proofs/types.d.ts +70 -0
  68. package/dist/cjs/proofs/types.js +2 -0
  69. package/dist/cjs/prover.d.ts +4 -1
  70. package/dist/cjs/prover.js +12 -2
  71. package/dist/cjs/random.d.ts +16 -0
  72. package/dist/cjs/random.js +28 -0
  73. package/dist/cjs/relayer/client.d.ts +61 -0
  74. package/dist/cjs/relayer/client.js +151 -0
  75. package/dist/cjs/relayer/crypto.d.ts +5 -0
  76. package/dist/cjs/relayer/crypto.js +26 -0
  77. package/dist/cjs/relayer/encoding.d.ts +2 -0
  78. package/dist/cjs/relayer/encoding.js +23 -0
  79. package/dist/cjs/relayer/errors.d.ts +7 -0
  80. package/dist/cjs/relayer/errors.js +17 -0
  81. package/dist/cjs/relayer/index.d.ts +3 -0
  82. package/dist/cjs/relayer/index.js +19 -0
  83. package/dist/cjs/relayer/transport.d.ts +17 -0
  84. package/dist/cjs/relayer/transport.js +63 -0
  85. package/dist/cjs/relayer/types.d.ts +276 -0
  86. package/dist/cjs/relayer/types.js +5 -0
  87. package/dist/cjs/relayer.d.ts +1 -295
  88. package/dist/cjs/relayer.js +15 -243
  89. package/dist/cjs/shield/alt.d.ts +87 -0
  90. package/dist/cjs/shield/alt.js +194 -0
  91. package/dist/cjs/shield/computeBudget.d.ts +61 -0
  92. package/dist/cjs/shield/computeBudget.js +64 -0
  93. package/dist/cjs/shield/errors.d.ts +58 -0
  94. package/dist/cjs/shield/errors.js +121 -0
  95. package/dist/cjs/shield/finalize.d.ts +45 -0
  96. package/dist/cjs/shield/finalize.js +119 -0
  97. package/dist/cjs/shield/index.d.ts +35 -0
  98. package/dist/cjs/shield/index.js +68 -0
  99. package/dist/cjs/shield/ix.d.ts +54 -0
  100. package/dist/cjs/shield/ix.js +119 -0
  101. package/dist/cjs/shield/owner.d.ts +36 -0
  102. package/dist/cjs/shield/owner.js +127 -0
  103. package/dist/cjs/shield/ports.d.ts +43 -0
  104. package/dist/cjs/shield/ports.js +153 -0
  105. package/dist/cjs/shield/preflight.d.ts +30 -0
  106. package/dist/cjs/shield/preflight.js +154 -0
  107. package/dist/cjs/shield/shield.d.ts +68 -0
  108. package/dist/cjs/shield/shield.js +500 -0
  109. package/dist/cjs/shield/types.d.ts +202 -0
  110. package/dist/cjs/shield/types.js +2 -0
  111. package/dist/cjs/transactions/deposit.d.ts +94 -0
  112. package/dist/cjs/transactions/deposit.js +234 -0
  113. package/dist/cjs/transactions/index.d.ts +7 -0
  114. package/dist/cjs/transactions/index.js +26 -0
  115. package/dist/cjs/transactions/swap.d.ts +71 -0
  116. package/dist/cjs/transactions/swap.js +184 -0
  117. package/dist/cjs/transactions/transact.d.ts +34 -0
  118. package/dist/cjs/transactions/transact.js +146 -0
  119. package/dist/cjs/transactions/transfer.d.ts +51 -0
  120. package/dist/cjs/transactions/transfer.js +106 -0
  121. package/dist/cjs/transactions/withdraw.d.ts +52 -0
  122. package/dist/cjs/transactions/withdraw.js +104 -0
  123. package/dist/cjs/utxo.d.ts +1 -215
  124. package/dist/cjs/utxo.js +15 -391
  125. package/dist/esm/accounts/admin.d.ts +84 -0
  126. package/dist/esm/accounts/admin.js +165 -0
  127. package/dist/esm/accounts/errors.d.ts +6 -0
  128. package/dist/esm/accounts/errors.js +95 -0
  129. package/dist/esm/accounts/index.d.ts +4 -0
  130. package/dist/esm/accounts/index.js +4 -0
  131. package/dist/esm/accounts/pdas.d.ts +23 -0
  132. package/dist/esm/accounts/pdas.js +38 -0
  133. package/dist/esm/accounts/queries.d.ts +41 -0
  134. package/dist/esm/accounts/queries.js +88 -0
  135. package/dist/esm/client.d.ts +2 -0
  136. package/dist/esm/client.js +3 -887
  137. package/dist/esm/cloak/client.d.ts +28 -0
  138. package/dist/esm/cloak/client.js +64 -0
  139. package/dist/esm/cloak/errors.d.ts +13 -0
  140. package/dist/esm/cloak/errors.js +21 -0
  141. package/dist/esm/cloak/helpers.d.ts +6 -0
  142. package/dist/esm/cloak/helpers.js +31 -0
  143. package/dist/esm/cloak/index.d.ts +6 -0
  144. package/dist/esm/cloak/index.js +6 -0
  145. package/dist/esm/cloak/polling.d.ts +7 -0
  146. package/dist/esm/cloak/polling.js +63 -0
  147. package/dist/esm/cloak/transport.d.ts +15 -0
  148. package/dist/esm/cloak/transport.js +115 -0
  149. package/dist/esm/cloak/types.d.ts +188 -0
  150. package/dist/esm/cloak/types.js +4 -0
  151. package/dist/esm/cloak.d.ts +1 -0
  152. package/dist/esm/cloak.js +2 -0
  153. package/dist/esm/compactNote.d.ts +128 -0
  154. package/dist/esm/compactNote.js +179 -0
  155. package/dist/esm/config.d.ts +82 -0
  156. package/dist/esm/events.d.ts +77 -0
  157. package/dist/esm/events.js +1 -1
  158. package/dist/esm/idl/privacy_pool.d.ts +5 -0
  159. package/dist/esm/idl/privacy_pool.js +15216 -0
  160. package/dist/esm/index.d.ts +17 -0
  161. package/dist/esm/index.js +20 -11
  162. package/dist/esm/merkle.d.ts +77 -0
  163. package/dist/esm/merkle.js +23 -1
  164. package/dist/esm/notes/encryption.d.ts +41 -0
  165. package/dist/esm/notes/encryption.js +67 -0
  166. package/dist/esm/notes/index.d.ts +5 -0
  167. package/dist/esm/notes/index.js +5 -0
  168. package/dist/esm/notes/mailbox.d.ts +70 -0
  169. package/dist/esm/notes/mailbox.js +154 -0
  170. package/dist/esm/notes/model.d.ts +91 -0
  171. package/dist/esm/notes/model.js +99 -0
  172. package/dist/esm/notes/nullifier.d.ts +27 -0
  173. package/dist/esm/notes/nullifier.js +40 -0
  174. package/dist/esm/notes/recovery.d.ts +10 -0
  175. package/dist/esm/notes/recovery.js +28 -0
  176. package/dist/esm/package.json +1 -0
  177. package/dist/esm/poseidon.d.ts +29 -0
  178. package/dist/esm/program.d.ts +37 -0
  179. package/dist/esm/program.js +23 -1
  180. package/dist/esm/proof.d.ts +1 -0
  181. package/dist/esm/proof.js +2 -281
  182. package/dist/esm/proofs/encoding.d.ts +17 -0
  183. package/dist/esm/proofs/encoding.js +68 -0
  184. package/dist/esm/proofs/formatting.d.ts +6 -0
  185. package/dist/esm/proofs/formatting.js +31 -0
  186. package/dist/esm/proofs/index.d.ts +5 -0
  187. package/dist/esm/proofs/index.js +5 -0
  188. package/dist/esm/proofs/swap.d.ts +87 -0
  189. package/dist/esm/proofs/swap.js +138 -0
  190. package/dist/esm/proofs/transaction.d.ts +28 -0
  191. package/dist/esm/proofs/transaction.js +105 -0
  192. package/dist/esm/proofs/types.d.ts +70 -0
  193. package/dist/esm/proofs/types.js +1 -0
  194. package/dist/esm/prover.d.ts +57 -0
  195. package/dist/esm/prover.js +10 -1
  196. package/dist/esm/random.d.ts +16 -0
  197. package/dist/esm/random.js +21 -0
  198. package/dist/esm/relayer/client.d.ts +61 -0
  199. package/dist/esm/relayer/client.js +144 -0
  200. package/dist/esm/relayer/crypto.d.ts +5 -0
  201. package/dist/esm/relayer/crypto.js +20 -0
  202. package/dist/esm/relayer/encoding.d.ts +2 -0
  203. package/dist/esm/relayer/encoding.js +19 -0
  204. package/dist/esm/relayer/errors.d.ts +7 -0
  205. package/dist/esm/relayer/errors.js +13 -0
  206. package/dist/esm/relayer/index.d.ts +3 -0
  207. package/dist/esm/relayer/index.js +3 -0
  208. package/dist/esm/relayer/transport.d.ts +17 -0
  209. package/dist/esm/relayer/transport.js +59 -0
  210. package/dist/esm/relayer/types.d.ts +276 -0
  211. package/dist/esm/relayer/types.js +4 -0
  212. package/dist/esm/relayer.d.ts +1 -0
  213. package/dist/esm/relayer.js +2 -238
  214. package/dist/esm/retry.d.ts +32 -0
  215. package/dist/esm/shield/alt.d.ts +87 -0
  216. package/dist/esm/shield/alt.js +186 -0
  217. package/dist/esm/shield/computeBudget.d.ts +61 -0
  218. package/dist/esm/shield/computeBudget.js +61 -0
  219. package/dist/esm/shield/errors.d.ts +58 -0
  220. package/dist/esm/shield/errors.js +115 -0
  221. package/dist/esm/shield/finalize.d.ts +45 -0
  222. package/dist/esm/shield/finalize.js +83 -0
  223. package/dist/esm/shield/index.d.ts +35 -0
  224. package/dist/esm/shield/index.js +32 -0
  225. package/dist/esm/shield/ix.d.ts +54 -0
  226. package/dist/esm/shield/ix.js +82 -0
  227. package/dist/esm/shield/owner.d.ts +36 -0
  228. package/dist/esm/shield/owner.js +123 -0
  229. package/dist/esm/shield/ports.d.ts +43 -0
  230. package/dist/esm/shield/ports.js +147 -0
  231. package/dist/esm/shield/preflight.d.ts +30 -0
  232. package/dist/esm/shield/preflight.js +151 -0
  233. package/dist/esm/shield/shield.d.ts +68 -0
  234. package/dist/esm/shield/shield.js +493 -0
  235. package/dist/esm/shield/types.d.ts +202 -0
  236. package/dist/esm/shield/types.js +1 -0
  237. package/dist/esm/transactions/deposit.d.ts +94 -0
  238. package/dist/esm/transactions/deposit.js +198 -0
  239. package/dist/esm/transactions/index.d.ts +7 -0
  240. package/dist/esm/transactions/index.js +7 -0
  241. package/dist/esm/transactions/swap.d.ts +71 -0
  242. package/dist/esm/transactions/swap.js +144 -0
  243. package/dist/esm/transactions/transact.d.ts +34 -0
  244. package/dist/esm/transactions/transact.js +110 -0
  245. package/dist/esm/transactions/transfer.d.ts +51 -0
  246. package/dist/esm/transactions/transfer.js +103 -0
  247. package/dist/esm/transactions/withdraw.d.ts +52 -0
  248. package/dist/esm/transactions/withdraw.js +101 -0
  249. package/dist/esm/utxo.d.ts +1 -0
  250. package/dist/esm/utxo.js +2 -372
  251. package/package.json +111 -15
package/dist/cjs/proof.js CHANGED
@@ -1,292 +1,18 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.computeExtDataHash = computeExtDataHash;
4
- exports.i64ToField = i64ToField;
5
- exports.prepareTransactionInputs = prepareTransactionInputs;
6
- exports.packProofToBytes = packProofToBytes;
7
- exports.encodeSnarkjsProofToTransactionProof = encodeSnarkjsProofToTransactionProof;
8
- exports.formatInputsForSnarkjs = formatInputsForSnarkjs;
9
- exports.formatSwapInputsForSnarkjs = formatSwapInputsForSnarkjs;
10
- exports.computeSwapParamsHash = computeSwapParamsHash;
11
- exports.computeSwapDataHash = computeSwapDataHash;
12
- const sha256_1 = require("@noble/hashes/sha256");
13
- const poseidon_1 = require("./poseidon");
14
- const utxo_1 = require("./utxo");
15
- // -----------------------------------------------------------------------------
16
- // ExtData Hash Computation
17
- // -----------------------------------------------------------------------------
18
- /**
19
- * Compute the external data hash.
20
- * Matches on-chain computation:
21
- * hash1 = Poseidon(recipient, relayer)
22
- * hash2 = Poseidon(fee, refund)
23
- * extDataHash = Poseidon(hash1, hash2, claimant)
24
- */
25
- function computeExtDataHash(extData) {
26
- const recipientField = (0, poseidon_1.pubkeyToField)(extData.recipient);
27
- const relayerField = (0, poseidon_1.pubkeyToField)(extData.relayer);
28
- const feeField = extData.fee % poseidon_1.BN254_FR_MODULUS;
29
- const refundField = extData.refund % poseidon_1.BN254_FR_MODULUS;
30
- const claimantField = (0, poseidon_1.pubkeyToField)(extData.claimant);
31
- const hash1 = (0, poseidon_1.poseidon2)(recipientField, relayerField);
32
- const hash2 = (0, poseidon_1.poseidon2)(feeField, refundField);
33
- const finalHash = (0, poseidon_1.poseidon3)(hash1, hash2, claimantField);
34
- return (0, poseidon_1.bigIntToBytesBE)(finalHash);
35
- }
36
- // -----------------------------------------------------------------------------
37
- // Circuit Input Preparation
38
- // -----------------------------------------------------------------------------
39
- /**
40
- * Convert i64 to field element (handles negative via modular arithmetic).
41
- * For negative values, we add the field modulus to get the equivalent positive representation.
42
- */
43
- function i64ToField(value) {
44
- if (value >= 0n) {
45
- return value % poseidon_1.BN254_FR_MODULUS;
46
- }
47
- return (poseidon_1.BN254_FR_MODULUS + value) % poseidon_1.BN254_FR_MODULUS;
48
- }
49
- /**
50
- * Prepare transaction circuit inputs from high-level parameters.
51
- * This converts the SDK types into the format expected by the circuit.
52
- */
53
- function prepareTransactionInputs(params) {
54
- const { inputUTXOs, outputUTXOs, root, publicAmount, extData, mintAddress } = params;
55
- const extDataHash = computeExtDataHash(extData);
56
- const mintField = (0, poseidon_1.pubkeyToField)(mintAddress);
57
- // Prepare input UTXO data
58
- const inAmount = [
59
- inputUTXOs[0].amount,
60
- inputUTXOs[1].amount,
61
- ];
62
- const inPubkey = [
63
- inputUTXOs[0].pubkey,
64
- inputUTXOs[1].pubkey,
65
- ];
66
- const inBlinding = [
67
- inputUTXOs[0].blinding,
68
- inputUTXOs[1].blinding,
69
- ];
70
- const inPathIndex = [
71
- inputUTXOs[0].pathIndex,
72
- inputUTXOs[1].pathIndex,
73
- ];
74
- const inPathElements = [
75
- inputUTXOs[0].pathElements.map((e) => (0, poseidon_1.bytesToBigIntBE)(e) % poseidon_1.BN254_FR_MODULUS),
76
- inputUTXOs[1].pathElements.map((e) => (0, poseidon_1.bytesToBigIntBE)(e) % poseidon_1.BN254_FR_MODULUS),
77
- ];
78
- const inPrivateKey = [
79
- inputUTXOs[0].privateKey,
80
- inputUTXOs[1].privateKey,
81
- ];
82
- // Prepare output UTXO data
83
- const outAmount = [
84
- outputUTXOs[0].amount,
85
- outputUTXOs[1].amount,
86
- ];
87
- const outPubkey = [
88
- outputUTXOs[0].pubkey,
89
- outputUTXOs[1].pubkey,
90
- ];
91
- const outBlinding = [
92
- outputUTXOs[0].blinding,
93
- outputUTXOs[1].blinding,
94
- ];
95
- // Derive nullifiers for input UTXOs
96
- const inputNullifiers = [
97
- (0, utxo_1.deriveNullifier)(inputUTXOs[0], inputUTXOs[0].pathIndex, inputUTXOs[0].privateKey),
98
- (0, utxo_1.deriveNullifier)(inputUTXOs[1], inputUTXOs[1].pathIndex, inputUTXOs[1].privateKey),
99
- ];
100
- return {
101
- root,
102
- publicAmount,
103
- extDataHash,
104
- mintAddress,
105
- inputNullifiers,
106
- outputCommitments: [outputUTXOs[0].commitment, outputUTXOs[1].commitment],
107
- inAmount,
108
- inPubkey,
109
- inBlinding,
110
- inPathIndex,
111
- inPathElements,
112
- inPrivateKey,
113
- outAmount,
114
- outPubkey,
115
- outBlinding,
116
- };
117
- }
118
- // -----------------------------------------------------------------------------
119
- // Proof Packing
120
- // -----------------------------------------------------------------------------
121
- /**
122
- * Convert bigint to 32-byte big-endian representation.
123
- * The alt_bn128 Solana precompile (and ark-works via change_endianness) expect BE.
124
- */
125
- function bigintTo32BytesBE(x) {
126
- const out = new Uint8Array(32);
127
- let v = x;
128
- for (let i = 31; i >= 0; i--) {
129
- out[i] = Number(v & 0xffn);
130
- v >>= 8n;
131
- }
132
- return out;
133
- }
134
- /**
135
- * Turn a snarkjs Groth16 proof into 256 bytes:
136
- * A(G1) | B(G2) | C(G1)
137
- * where:
138
- * A = (ax, ay) -> 64 bytes
139
- * B = (bx1, bx0, by1, by0) -> 128 bytes (swapped pairs for alt_bn128)
140
- * C = (cx, cy) -> 64 bytes
141
- *
142
- * Each field element is encoded as 32-byte BE to match the on-chain verifier.
143
- * We ignore the extra projective/z components snarkjs keeps.
144
- */
145
- function packProofToBytes(proof) {
146
- const chunks = [];
147
- const ax = BigInt(proof.pi_a[0]);
148
- const ay = BigInt(proof.pi_a[1]);
149
- chunks.push(bigintTo32BytesBE(ax), bigintTo32BytesBE(ay));
150
- const bx = proof.pi_b[0];
151
- const by = proof.pi_b[1];
152
- const bx0 = BigInt(bx[0]);
153
- const bx1 = BigInt(bx[1]);
154
- const by0 = BigInt(by[0]);
155
- const by1 = BigInt(by[1]);
156
- // alt_bn128 expects the G2 pairs swapped: bx1 before bx0, by1 before by0
157
- chunks.push(bigintTo32BytesBE(bx1), bigintTo32BytesBE(bx0), bigintTo32BytesBE(by1), bigintTo32BytesBE(by0));
158
- const cx = BigInt(proof.pi_c[0]);
159
- const cy = BigInt(proof.pi_c[1]);
160
- chunks.push(bigintTo32BytesBE(cx), bigintTo32BytesBE(cy));
161
- const out = new Uint8Array(32 * (2 + 4 + 2)); // 256
162
- let offset = 0;
163
- for (const c of chunks) {
164
- out.set(c, offset);
165
- offset += c.length;
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
166
7
  }
167
- return out;
168
- }
169
- /**
170
- * Encode a snarkjs proof into the TransactionProofStruct format expected by the on-chain program.
171
- */
172
- function encodeSnarkjsProofToTransactionProof(proof) {
173
- const full = packProofToBytes(proof);
174
- if (full.length !== 256) {
175
- throw new Error(`Expected 256 proof bytes, got ${full.length}`);
176
- }
177
- const proofA = full.slice(0, 64);
178
- const proofB = full.slice(64, 192);
179
- const proofC = full.slice(192, 256);
180
- return {
181
- proofA: Array.from(proofA),
182
- proofB: Array.from(proofB),
183
- proofC: Array.from(proofC),
184
- };
185
- }
186
- // -----------------------------------------------------------------------------
187
- // Snarkjs Input Formatting
188
- // -----------------------------------------------------------------------------
189
- /**
190
- * Convert TransactionCircuitInputs to the format expected by snarkjs.groth16.fullProve().
191
- * All values are converted to strings (snarkjs expects string inputs for field elements).
192
- */
193
- function formatInputsForSnarkjs(inputs) {
194
- return {
195
- // Public inputs
196
- root: (0, poseidon_1.bytesToBigIntBE)(inputs.root).toString(),
197
- publicAmount: i64ToField(inputs.publicAmount).toString(),
198
- extDataHash: (0, poseidon_1.bytesToBigIntBE)(inputs.extDataHash).toString(),
199
- mintAddress: (0, poseidon_1.pubkeyToField)(inputs.mintAddress).toString(),
200
- inputNullifier: inputs.inputNullifiers.map((n) => (0, poseidon_1.bytesToBigIntBE)(n).toString()),
201
- outputCommitment: inputs.outputCommitments.map((c) => (0, poseidon_1.bytesToBigIntBE)(c).toString()),
202
- // Private inputs - Input UTXOs
203
- inAmount: inputs.inAmount.map((a) => a.toString()),
204
- inPubkey: inputs.inPubkey.map((p) => p.toString()),
205
- inBlinding: inputs.inBlinding.map((b) => b.toString()),
206
- inPathIndex: inputs.inPathIndex.map((i) => i.toString()),
207
- inPathElements: inputs.inPathElements.map((path) => path.map((e) => e.toString())),
208
- inPrivateKey: inputs.inPrivateKey.map((k) => k.toString()),
209
- // Private inputs - Output UTXOs
210
- outAmount: inputs.outAmount.map((a) => a.toString()),
211
- outPubkey: inputs.outPubkey.map((p) => p.toString()),
212
- outBlinding: inputs.outBlinding.map((b) => b.toString()),
213
- };
214
- }
215
- /**
216
- * Convert SwapCircuitInputs into the shape expected by
217
- * snarkjs.groth16.fullProve() for swap.circom. Every value is emitted as a
218
- * decimal string (or string array) because snarkjs only accepts field
219
- * elements as strings.
220
- *
221
- * Mirrors `generateSwapProof` in
222
- * relayer-server/src/controllers/swap.helpers.ts so SDK-side swap proofs
223
- * verify against the same circuit the relayer uses.
224
- */
225
- function formatSwapInputsForSnarkjs(inputs) {
226
- return {
227
- // Public inputs
228
- sourceRoot: (0, poseidon_1.bytesToBigIntBE)(inputs.sourceRoot).toString(),
229
- swapParamsHash: (0, poseidon_1.bytesToBigIntBE)(inputs.swapParamsHash).toString(),
230
- extDataHash: (0, poseidon_1.bytesToBigIntBE)(inputs.extDataHash).toString(),
231
- sourceMint: (0, poseidon_1.pubkeyToField)(inputs.sourceMint).toString(),
232
- destMint: (0, poseidon_1.pubkeyToField)(inputs.destMint).toString(),
233
- inputNullifier: inputs.inputNullifiers.map((n) => (0, poseidon_1.bytesToBigIntBE)(n).toString()),
234
- changeCommitment: (0, poseidon_1.bytesToBigIntBE)(inputs.changeCommitment).toString(),
235
- destCommitment: (0, poseidon_1.bytesToBigIntBE)(inputs.destCommitment).toString(),
236
- swapAmount: inputs.swapAmount.toString(),
237
- // Private inputs - Input UTXOs
238
- inAmount: inputs.inAmount.map((a) => a.toString()),
239
- inPubkey: inputs.inPubkey.map((p) => p.toString()),
240
- inBlinding: inputs.inBlinding.map((b) => b.toString()),
241
- inPathIndex: inputs.inPathIndex.map((i) => i.toString()),
242
- inPathElements: inputs.inPathElements.map((path) => path.map((e) => e.toString())),
243
- inPrivateKey: inputs.inPrivateKey.map((k) => k.toString()),
244
- // Private inputs - Change UTXO
245
- changeAmount: inputs.changeAmount.toString(),
246
- changePubkey: inputs.changePubkey.toString(),
247
- changeBlinding: inputs.changeBlinding.toString(),
248
- // Private inputs - Destination UTXO
249
- destAmount: inputs.destAmount.toString(),
250
- destPubkey: inputs.destPubkey.toString(),
251
- destBlinding: inputs.destBlinding.toString(),
252
- // Swap params
253
- minAmountOut: inputs.minAmountOut.toString(),
254
- deadline: inputs.deadline.toString(),
255
- };
256
- }
257
- // -----------------------------------------------------------------------------
258
- // Swap Parameter Hashing
259
- // -----------------------------------------------------------------------------
260
- /**
261
- * Compute the swap params hash that the swap circuit and on-chain program verify.
262
- * Mirrors relayer-server/src/controllers/swap.helpers.ts `computeSwapParamsHash`.
263
- *
264
- * Circuit formula (swap.circom lines 293-307):
265
- * mintPairHash = Poseidon(sourceMint, destMint)
266
- * swapTermsHash = Poseidon(minAmountOut, deadline, destAmount)
267
- * swapParamsHash = Poseidon(mintPairHash, swapTermsHash)
268
- *
269
- * @param sourceMint Source pool mint
270
- * @param destMint Destination pool mint
271
- * @param minAmountOut Minimum accepted output amount (slippage protected)
272
- * @param deadline Unix timestamp deadline (i64, 0 = no deadline)
273
- * @param destAmount Actual amount committed to destination UTXO (≥ minAmountOut -
274
- * relayerFee). If omitted, defaults to minAmountOut.
275
- */
276
- function computeSwapParamsHash(sourceMint, destMint, minAmountOut, deadline, destAmount) {
277
- const srcField = (0, poseidon_1.pubkeyToField)(sourceMint);
278
- const dstField = (0, poseidon_1.pubkeyToField)(destMint);
279
- const mintPairHash = (0, poseidon_1.poseidon2)(srcField, dstField);
280
- const dest = destAmount ?? minAmountOut;
281
- const swapTermsHash = (0, poseidon_1.poseidon3)(minAmountOut, deadline, dest);
282
- const paramsHash = (0, poseidon_1.poseidon2)(mintPairHash, swapTermsHash);
283
- return (0, poseidon_1.bigIntToBytesBE)(paramsHash);
284
- }
285
- /**
286
- * Compute the swap data hash committed on-chain for Jupiter instruction integrity.
287
- * SHA-256 of the raw Jupiter swap instruction data bytes.
288
- * Mirrors relayer-server/src/controllers/swap.helpers.ts `computeSwapDataHash`.
289
- */
290
- function computeSwapDataHash(swapData) {
291
- return (0, sha256_1.sha256)(swapData instanceof Buffer ? new Uint8Array(swapData) : swapData);
292
- }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ // Backward-compatible entry point. Proof responsibilities live in src/proofs.
18
+ __exportStar(require("./proofs/index.js"), exports);
@@ -0,0 +1,17 @@
1
+ import type { RawProof, TransactionProofStruct } from "./types.js";
2
+ /**
3
+ * Turn a snarkjs Groth16 proof into 256 bytes:
4
+ * A(G1) | B(G2) | C(G1)
5
+ * where:
6
+ * A = (ax, ay) -> 64 bytes
7
+ * B = (bx1, bx0, by1, by0) -> 128 bytes (swapped pairs for alt_bn128)
8
+ * C = (cx, cy) -> 64 bytes
9
+ *
10
+ * Each field element is encoded as 32-byte BE to match the on-chain verifier.
11
+ * We ignore the extra projective/z components snarkjs keeps.
12
+ */
13
+ export declare function packProofToBytes(proof: RawProof): Uint8Array;
14
+ /**
15
+ * Encode a snarkjs proof into the TransactionProofStruct format expected by the on-chain program.
16
+ */
17
+ export declare function encodeSnarkjsProofToTransactionProof(proof: RawProof): TransactionProofStruct;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.packProofToBytes = packProofToBytes;
4
+ exports.encodeSnarkjsProofToTransactionProof = encodeSnarkjsProofToTransactionProof;
5
+ // -----------------------------------------------------------------------------
6
+ // Proof Packing
7
+ // -----------------------------------------------------------------------------
8
+ /**
9
+ * Convert bigint to 32-byte big-endian representation.
10
+ * The alt_bn128 Solana precompile (and ark-works via change_endianness) expect BE.
11
+ */
12
+ function bigintTo32BytesBE(x) {
13
+ const out = new Uint8Array(32);
14
+ let v = x;
15
+ for (let i = 31; i >= 0; i--) {
16
+ out[i] = Number(v & 0xffn);
17
+ v >>= 8n;
18
+ }
19
+ return out;
20
+ }
21
+ /**
22
+ * Turn a snarkjs Groth16 proof into 256 bytes:
23
+ * A(G1) | B(G2) | C(G1)
24
+ * where:
25
+ * A = (ax, ay) -> 64 bytes
26
+ * B = (bx1, bx0, by1, by0) -> 128 bytes (swapped pairs for alt_bn128)
27
+ * C = (cx, cy) -> 64 bytes
28
+ *
29
+ * Each field element is encoded as 32-byte BE to match the on-chain verifier.
30
+ * We ignore the extra projective/z components snarkjs keeps.
31
+ */
32
+ function packProofToBytes(proof) {
33
+ const chunks = [];
34
+ const ax = BigInt(proof.pi_a[0]);
35
+ const ay = BigInt(proof.pi_a[1]);
36
+ chunks.push(bigintTo32BytesBE(ax), bigintTo32BytesBE(ay));
37
+ const bx = proof.pi_b[0];
38
+ const by = proof.pi_b[1];
39
+ const bx0 = BigInt(bx[0]);
40
+ const bx1 = BigInt(bx[1]);
41
+ const by0 = BigInt(by[0]);
42
+ const by1 = BigInt(by[1]);
43
+ // alt_bn128 expects the G2 pairs swapped: bx1 before bx0, by1 before by0
44
+ chunks.push(bigintTo32BytesBE(bx1), bigintTo32BytesBE(bx0), bigintTo32BytesBE(by1), bigintTo32BytesBE(by0));
45
+ const cx = BigInt(proof.pi_c[0]);
46
+ const cy = BigInt(proof.pi_c[1]);
47
+ chunks.push(bigintTo32BytesBE(cx), bigintTo32BytesBE(cy));
48
+ const out = new Uint8Array(32 * (2 + 4 + 2)); // 256
49
+ let offset = 0;
50
+ for (const c of chunks) {
51
+ out.set(c, offset);
52
+ offset += c.length;
53
+ }
54
+ return out;
55
+ }
56
+ /**
57
+ * Encode a snarkjs proof into the TransactionProofStruct format expected by the on-chain program.
58
+ */
59
+ function encodeSnarkjsProofToTransactionProof(proof) {
60
+ const full = packProofToBytes(proof);
61
+ if (full.length !== 256) {
62
+ throw new Error(`Expected 256 proof bytes, got ${full.length}`);
63
+ }
64
+ const proofA = full.slice(0, 64);
65
+ const proofB = full.slice(64, 192);
66
+ const proofC = full.slice(192, 256);
67
+ return {
68
+ proofA: Array.from(proofA),
69
+ proofB: Array.from(proofB),
70
+ proofC: Array.from(proofC),
71
+ };
72
+ }
@@ -0,0 +1,6 @@
1
+ import type { TransactionCircuitInputs } from "./types.js";
2
+ /**
3
+ * Convert TransactionCircuitInputs to the format expected by snarkjs.groth16.fullProve().
4
+ * All values are converted to strings (snarkjs expects string inputs for field elements).
5
+ */
6
+ export declare function formatInputsForSnarkjs(inputs: TransactionCircuitInputs): Record<string, any>;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatInputsForSnarkjs = formatInputsForSnarkjs;
4
+ const poseidon_js_1 = require("../poseidon.js");
5
+ const transaction_js_1 = require("./transaction.js");
6
+ // -----------------------------------------------------------------------------
7
+ // Snarkjs Input Formatting
8
+ // -----------------------------------------------------------------------------
9
+ /**
10
+ * Convert TransactionCircuitInputs to the format expected by snarkjs.groth16.fullProve().
11
+ * All values are converted to strings (snarkjs expects string inputs for field elements).
12
+ */
13
+ function formatInputsForSnarkjs(inputs) {
14
+ return {
15
+ // Public inputs
16
+ root: (0, poseidon_js_1.bytesToBigIntBE)(inputs.root).toString(),
17
+ publicAmount: (0, transaction_js_1.i64ToField)(inputs.publicAmount).toString(),
18
+ extDataHash: (0, poseidon_js_1.bytesToBigIntBE)(inputs.extDataHash).toString(),
19
+ mintAddress: (0, poseidon_js_1.pubkeyToField)(inputs.mintAddress).toString(),
20
+ inputNullifier: inputs.inputNullifiers.map((n) => (0, poseidon_js_1.bytesToBigIntBE)(n).toString()),
21
+ outputCommitment: inputs.outputCommitments.map((c) => (0, poseidon_js_1.bytesToBigIntBE)(c).toString()),
22
+ // Private inputs - Input UTXOs
23
+ inAmount: inputs.inAmount.map((a) => a.toString()),
24
+ inPubkey: inputs.inPubkey.map((p) => p.toString()),
25
+ inBlinding: inputs.inBlinding.map((b) => b.toString()),
26
+ inPathIndex: inputs.inPathIndex.map((i) => i.toString()),
27
+ inPathElements: inputs.inPathElements.map((path) => path.map((e) => e.toString())),
28
+ inPrivateKey: inputs.inPrivateKey.map((k) => k.toString()),
29
+ // Private inputs - Output UTXOs
30
+ outAmount: inputs.outAmount.map((a) => a.toString()),
31
+ outPubkey: inputs.outPubkey.map((p) => p.toString()),
32
+ outBlinding: inputs.outBlinding.map((b) => b.toString()),
33
+ };
34
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./types.js";
2
+ export * from "./transaction.js";
3
+ export * from "./encoding.js";
4
+ export * from "./formatting.js";
5
+ export * from "./swap.js";
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./types.js"), exports);
18
+ __exportStar(require("./transaction.js"), exports);
19
+ __exportStar(require("./encoding.js"), exports);
20
+ __exportStar(require("./formatting.js"), exports);
21
+ __exportStar(require("./swap.js"), exports);
@@ -0,0 +1,87 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ import type { InputUTXO, SerializedUTXO } from "../notes/model.js";
3
+ import type { ExtData, RawProof } from "./types.js";
4
+ /**
5
+ * Swap circuit inputs. Matches the signal shape declared by swap.circom's
6
+ * main component (see zk-circuits/swap.circom lines 208-238). Unlike the
7
+ * transaction circuit, the swap circuit distinguishes a "change" output
8
+ * (same mint as source) from a "dest" output (dest mint) and takes a
9
+ * swap-specific params hash and swapAmount alongside the usual inputs.
10
+ */
11
+ export type SwapCircuitInputs = {
12
+ sourceRoot: Uint8Array;
13
+ swapParamsHash: Uint8Array;
14
+ extDataHash: Uint8Array;
15
+ sourceMint: PublicKey;
16
+ destMint: PublicKey;
17
+ inputNullifiers: [Uint8Array, Uint8Array];
18
+ changeCommitment: Uint8Array;
19
+ destCommitment: Uint8Array;
20
+ swapAmount: bigint;
21
+ inAmount: [bigint, bigint];
22
+ inPubkey: [bigint, bigint];
23
+ inBlinding: [bigint, bigint];
24
+ inPathIndex: [number, number];
25
+ inPathElements: [bigint[], bigint[]];
26
+ inPrivateKey: [bigint, bigint];
27
+ changeAmount: bigint;
28
+ changePubkey: bigint;
29
+ changeBlinding: bigint;
30
+ destAmount: bigint;
31
+ destPubkey: bigint;
32
+ destBlinding: bigint;
33
+ minAmountOut: bigint;
34
+ deadline: bigint;
35
+ };
36
+ /** Proof builder accepted by SDK-side private swap flows. */
37
+ export type SwapProofBuilder = (inputs: SwapCircuitInputs) => Promise<RawProof>;
38
+ /**
39
+ * Prepare the exact witness/public inputs used by the relayer's swap circuit.
40
+ * Output 0 must be source-mint change and output 1 the destination-mint note.
41
+ */
42
+ export declare function prepareSwapInputs(params: {
43
+ inputUTXOs: [InputUTXO, InputUTXO];
44
+ changeUTXO: SerializedUTXO;
45
+ destUTXO: SerializedUTXO;
46
+ sourceRoot: Uint8Array;
47
+ sourceMint: PublicKey;
48
+ destMint: PublicKey;
49
+ swapAmount: bigint;
50
+ minAmountOut: bigint;
51
+ deadline: bigint;
52
+ extData: ExtData;
53
+ }): SwapCircuitInputs;
54
+ /**
55
+ * Convert SwapCircuitInputs into the shape expected by
56
+ * snarkjs.groth16.fullProve() for swap.circom. Every value is emitted as a
57
+ * decimal string (or string array) because snarkjs only accepts field
58
+ * elements as strings.
59
+ *
60
+ * Mirrors `generateSwapProof` in
61
+ * relayer-server/src/controllers/swap.helpers.ts so SDK-side swap proofs
62
+ * verify against the same circuit the relayer uses.
63
+ */
64
+ export declare function formatSwapInputsForSnarkjs(inputs: SwapCircuitInputs): Record<string, any>;
65
+ /**
66
+ * Compute the swap params hash that the swap circuit and on-chain program verify.
67
+ * Mirrors relayer-server/src/controllers/swap.helpers.ts `computeSwapParamsHash`.
68
+ *
69
+ * Circuit formula (swap.circom lines 293-307):
70
+ * mintPairHash = Poseidon(sourceMint, destMint)
71
+ * swapTermsHash = Poseidon(minAmountOut, deadline, destAmount)
72
+ * swapParamsHash = Poseidon(mintPairHash, swapTermsHash)
73
+ *
74
+ * @param sourceMint Source pool mint
75
+ * @param destMint Destination pool mint
76
+ * @param minAmountOut Minimum accepted output amount (slippage protected)
77
+ * @param deadline Positive Unix timestamp deadline (the program rejects 0)
78
+ * @param destAmount Actual amount committed to destination UTXO (≥ minAmountOut -
79
+ * relayerFee). If omitted, defaults to minAmountOut.
80
+ */
81
+ export declare function computeSwapParamsHash(sourceMint: PublicKey, destMint: PublicKey, minAmountOut: bigint, deadline: bigint, destAmount?: bigint): Uint8Array;
82
+ /**
83
+ * Compute the swap data hash committed on-chain for Jupiter instruction integrity.
84
+ * SHA-256 of the raw Jupiter swap instruction data bytes.
85
+ * Mirrors relayer-server/src/controllers/swap.helpers.ts `computeSwapDataHash`.
86
+ */
87
+ export declare function computeSwapDataHash(swapData: Uint8Array | Buffer): Uint8Array;