@veilo/sdk-core 0.1.17 → 0.4.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 (218) hide show
  1. package/README.md +1296 -272
  2. package/SHIELD_INTEGRATION.md +143 -0
  3. package/config.d.ts +2 -0
  4. package/config.js +4 -0
  5. package/dist/cjs/client.d.ts +407 -0
  6. package/dist/cjs/client.js +951 -0
  7. package/dist/cjs/compactNote.d.ts +107 -0
  8. package/dist/cjs/compactNote.js +167 -0
  9. package/dist/cjs/config.d.ts +82 -0
  10. package/dist/cjs/config.js +57 -0
  11. package/dist/cjs/events.d.ts +77 -0
  12. package/dist/cjs/events.js +167 -0
  13. package/dist/cjs/idl/privacy_pool.d.ts +5 -0
  14. package/dist/cjs/idl/privacy_pool.js +15218 -0
  15. package/dist/cjs/idl/privacy_pool.json +10313 -0
  16. package/dist/cjs/index.d.ts +16 -0
  17. package/dist/cjs/index.js +67 -0
  18. package/dist/cjs/merkle.d.ts +77 -0
  19. package/dist/cjs/merkle.js +156 -0
  20. package/dist/cjs/poseidon.d.ts +29 -0
  21. package/dist/cjs/poseidon.js +100 -0
  22. package/dist/cjs/program.d.ts +37 -0
  23. package/dist/cjs/program.js +61 -0
  24. package/dist/cjs/proof.d.ts +183 -0
  25. package/dist/cjs/proof.js +292 -0
  26. package/dist/cjs/prover.d.ts +54 -0
  27. package/dist/cjs/prover.js +112 -0
  28. package/dist/cjs/random.d.ts +16 -0
  29. package/dist/cjs/random.js +28 -0
  30. package/dist/cjs/relayer.d.ts +318 -0
  31. package/dist/cjs/relayer.js +257 -0
  32. package/dist/cjs/retry.d.ts +32 -0
  33. package/dist/cjs/retry.js +75 -0
  34. package/dist/cjs/shield/alt.d.ts +87 -0
  35. package/dist/cjs/shield/alt.js +194 -0
  36. package/dist/cjs/shield/computeBudget.d.ts +61 -0
  37. package/dist/cjs/shield/computeBudget.js +64 -0
  38. package/dist/cjs/shield/errors.d.ts +58 -0
  39. package/dist/cjs/shield/errors.js +121 -0
  40. package/dist/cjs/shield/finalize.d.ts +45 -0
  41. package/dist/cjs/shield/finalize.js +119 -0
  42. package/dist/cjs/shield/index.d.ts +35 -0
  43. package/dist/cjs/shield/index.js +68 -0
  44. package/dist/cjs/shield/ix.d.ts +54 -0
  45. package/dist/cjs/shield/ix.js +119 -0
  46. package/dist/cjs/shield/owner.d.ts +36 -0
  47. package/dist/cjs/shield/owner.js +126 -0
  48. package/dist/cjs/shield/ports.d.ts +43 -0
  49. package/dist/cjs/shield/ports.js +153 -0
  50. package/dist/cjs/shield/preflight.d.ts +30 -0
  51. package/dist/cjs/shield/preflight.js +154 -0
  52. package/dist/cjs/shield/shield.d.ts +68 -0
  53. package/dist/cjs/shield/shield.js +499 -0
  54. package/dist/cjs/shield/types.d.ts +202 -0
  55. package/dist/cjs/shield/types.js +2 -0
  56. package/dist/cjs/utxo.d.ts +235 -0
  57. package/dist/cjs/utxo.js +407 -0
  58. package/dist/esm/client.d.ts +407 -0
  59. package/dist/esm/client.js +891 -0
  60. package/dist/esm/compactNote.d.ts +107 -0
  61. package/dist/esm/compactNote.js +156 -0
  62. package/dist/esm/config.d.ts +82 -0
  63. package/dist/esm/config.js +51 -0
  64. package/dist/esm/events.d.ts +77 -0
  65. package/dist/esm/events.js +129 -0
  66. package/dist/esm/idl/privacy_pool.d.ts +5 -0
  67. package/dist/esm/idl/privacy_pool.js +15216 -0
  68. package/dist/esm/idl/privacy_pool.json +10313 -0
  69. package/dist/esm/index.d.ts +16 -0
  70. package/dist/esm/index.js +29 -0
  71. package/dist/esm/merkle.d.ts +77 -0
  72. package/dist/esm/merkle.js +151 -0
  73. package/dist/esm/package.json +1 -0
  74. package/dist/esm/poseidon.d.ts +29 -0
  75. package/dist/esm/poseidon.js +87 -0
  76. package/dist/esm/program.d.ts +37 -0
  77. package/dist/esm/program.js +53 -0
  78. package/dist/esm/proof.d.ts +183 -0
  79. package/dist/esm/proof.js +281 -0
  80. package/dist/esm/prover.d.ts +54 -0
  81. package/dist/esm/prover.js +75 -0
  82. package/dist/esm/random.d.ts +16 -0
  83. package/dist/esm/random.js +21 -0
  84. package/dist/esm/relayer.d.ts +318 -0
  85. package/dist/esm/relayer.js +249 -0
  86. package/dist/esm/retry.d.ts +32 -0
  87. package/dist/esm/retry.js +71 -0
  88. package/dist/esm/shield/alt.d.ts +87 -0
  89. package/dist/esm/shield/alt.js +186 -0
  90. package/dist/esm/shield/computeBudget.d.ts +61 -0
  91. package/dist/esm/shield/computeBudget.js +61 -0
  92. package/dist/esm/shield/errors.d.ts +58 -0
  93. package/dist/esm/shield/errors.js +115 -0
  94. package/dist/esm/shield/finalize.d.ts +45 -0
  95. package/dist/esm/shield/finalize.js +83 -0
  96. package/dist/esm/shield/index.d.ts +35 -0
  97. package/dist/esm/shield/index.js +32 -0
  98. package/dist/esm/shield/ix.d.ts +54 -0
  99. package/dist/esm/shield/ix.js +82 -0
  100. package/dist/esm/shield/owner.d.ts +36 -0
  101. package/dist/esm/shield/owner.js +122 -0
  102. package/dist/esm/shield/ports.d.ts +43 -0
  103. package/dist/esm/shield/ports.js +147 -0
  104. package/dist/esm/shield/preflight.d.ts +30 -0
  105. package/dist/esm/shield/preflight.js +151 -0
  106. package/dist/esm/shield/shield.d.ts +68 -0
  107. package/dist/esm/shield/shield.js +492 -0
  108. package/dist/esm/shield/types.d.ts +202 -0
  109. package/dist/esm/shield/types.js +1 -0
  110. package/dist/esm/utxo.d.ts +235 -0
  111. package/dist/esm/utxo.js +382 -0
  112. package/dist/src/client.d.ts +407 -0
  113. package/dist/src/client.js +951 -0
  114. package/dist/src/compactNote.d.ts +107 -0
  115. package/dist/src/compactNote.js +167 -0
  116. package/dist/src/config.d.ts +82 -0
  117. package/dist/src/config.js +57 -0
  118. package/dist/src/events.d.ts +77 -0
  119. package/dist/src/events.js +167 -0
  120. package/dist/src/idl/privacy_pool.d.ts +5 -0
  121. package/dist/src/idl/privacy_pool.js +15218 -0
  122. package/dist/src/index.d.ts +16 -0
  123. package/dist/src/index.js +67 -0
  124. package/dist/src/merkle.d.ts +77 -0
  125. package/dist/src/merkle.js +156 -0
  126. package/dist/src/poseidon.d.ts +29 -0
  127. package/dist/src/poseidon.js +100 -0
  128. package/dist/src/program.d.ts +37 -0
  129. package/dist/src/program.js +61 -0
  130. package/dist/src/proof.d.ts +183 -0
  131. package/dist/src/proof.js +292 -0
  132. package/dist/src/prover.d.ts +54 -0
  133. package/dist/src/prover.js +112 -0
  134. package/dist/src/random.d.ts +16 -0
  135. package/dist/src/random.js +28 -0
  136. package/dist/src/relayer.d.ts +318 -0
  137. package/dist/src/relayer.js +257 -0
  138. package/dist/src/retry.d.ts +32 -0
  139. package/dist/src/retry.js +75 -0
  140. package/dist/src/shield/alt.d.ts +87 -0
  141. package/dist/src/shield/alt.js +194 -0
  142. package/dist/src/shield/computeBudget.d.ts +61 -0
  143. package/dist/src/shield/computeBudget.js +64 -0
  144. package/dist/src/shield/errors.d.ts +58 -0
  145. package/dist/src/shield/errors.js +121 -0
  146. package/dist/src/shield/finalize.d.ts +45 -0
  147. package/dist/src/shield/finalize.js +119 -0
  148. package/dist/src/shield/index.d.ts +35 -0
  149. package/dist/src/shield/index.js +68 -0
  150. package/dist/src/shield/ix.d.ts +54 -0
  151. package/dist/src/shield/ix.js +119 -0
  152. package/dist/src/shield/owner.d.ts +36 -0
  153. package/dist/src/shield/owner.js +126 -0
  154. package/dist/src/shield/ports.d.ts +43 -0
  155. package/dist/src/shield/ports.js +153 -0
  156. package/dist/src/shield/preflight.d.ts +30 -0
  157. package/dist/src/shield/preflight.js +154 -0
  158. package/dist/src/shield/shield.d.ts +68 -0
  159. package/dist/src/shield/shield.js +499 -0
  160. package/dist/src/shield/types.d.ts +202 -0
  161. package/dist/src/shield/types.js +2 -0
  162. package/dist/src/utxo.d.ts +235 -0
  163. package/dist/src/utxo.js +407 -0
  164. package/dist/tests/compact-note.test.d.ts +1 -0
  165. package/dist/tests/compact-note.test.js +173 -0
  166. package/dist/tests/config.test.d.ts +1 -0
  167. package/dist/tests/config.test.js +102 -0
  168. package/dist/tests/edge-cases.test.d.ts +1 -0
  169. package/dist/tests/edge-cases.test.js +220 -0
  170. package/dist/tests/encryption.test.d.ts +1 -0
  171. package/dist/tests/encryption.test.js +215 -0
  172. package/dist/tests/events.test.d.ts +1 -0
  173. package/dist/tests/events.test.js +78 -0
  174. package/dist/tests/multi-tree.test.d.ts +1 -0
  175. package/dist/tests/multi-tree.test.js +405 -0
  176. package/dist/tests/pda.test.d.ts +1 -0
  177. package/dist/tests/pda.test.js +229 -0
  178. package/dist/tests/poseidon-builder-parity.test.d.ts +1 -0
  179. package/dist/tests/poseidon-builder-parity.test.js +72 -0
  180. package/dist/tests/poseidon.test.d.ts +1 -0
  181. package/dist/tests/poseidon.test.js +142 -0
  182. package/dist/tests/proof.test.d.ts +1 -0
  183. package/dist/tests/proof.test.js +296 -0
  184. package/dist/tests/relayer.test.d.ts +1 -0
  185. package/dist/tests/relayer.test.js +271 -0
  186. package/dist/tests/sdk.integration.test.d.ts +1 -0
  187. package/dist/tests/sdk.integration.test.js +330 -0
  188. package/dist/tests/shield-owner.test.d.ts +1 -0
  189. package/dist/tests/shield-owner.test.js +89 -0
  190. package/dist/tests/shield-preflight.test.d.ts +1 -0
  191. package/dist/tests/shield-preflight.test.js +87 -0
  192. package/dist/tests/shield-realproof.test.d.ts +1 -0
  193. package/dist/tests/shield-realproof.test.js +272 -0
  194. package/dist/tests/shield.test.d.ts +1 -0
  195. package/dist/tests/shield.test.js +403 -0
  196. package/dist/tests/utxo.test.d.ts +1 -0
  197. package/dist/tests/utxo.test.js +140 -0
  198. package/package.json +105 -11
  199. package/poseidon.d.ts +2 -0
  200. package/poseidon.js +4 -0
  201. package/proof.d.ts +2 -0
  202. package/proof.js +4 -0
  203. package/prover.d.ts +2 -0
  204. package/prover.js +4 -0
  205. package/shield.d.ts +2 -0
  206. package/shield.js +4 -0
  207. package/src/client.ts +0 -352
  208. package/src/config.ts +0 -13
  209. package/src/index.ts +0 -6
  210. package/src/merkle.ts +0 -178
  211. package/src/note.ts +0 -193
  212. package/src/poseidon.ts +0 -62
  213. package/src/proof.ts +0 -170
  214. package/test/script.js +0 -0
  215. package/test-tsconfig.json +0 -19
  216. package/tests/note.test.ts +0 -50
  217. package/tests/sdk.integration.test.ts +0 -210
  218. package/tsconfig.json +0 -18
@@ -0,0 +1,281 @@
1
+ import { sha256 } from "@noble/hashes/sha256";
2
+ import { BN254_FR_MODULUS, bigIntToBytesBE, bytesToBigIntBE, poseidon2, poseidon3, pubkeyToField, } from "./poseidon.js";
3
+ import { deriveNullifier } from "./utxo.js";
4
+ // -----------------------------------------------------------------------------
5
+ // ExtData Hash Computation
6
+ // -----------------------------------------------------------------------------
7
+ /**
8
+ * Compute the external data hash.
9
+ * Matches on-chain computation:
10
+ * hash1 = Poseidon(recipient, relayer)
11
+ * hash2 = Poseidon(fee, refund)
12
+ * extDataHash = Poseidon(hash1, hash2, claimant)
13
+ */
14
+ export function computeExtDataHash(extData) {
15
+ const recipientField = pubkeyToField(extData.recipient);
16
+ const relayerField = pubkeyToField(extData.relayer);
17
+ const feeField = extData.fee % BN254_FR_MODULUS;
18
+ const refundField = extData.refund % BN254_FR_MODULUS;
19
+ const claimantField = pubkeyToField(extData.claimant);
20
+ const hash1 = poseidon2(recipientField, relayerField);
21
+ const hash2 = poseidon2(feeField, refundField);
22
+ const finalHash = poseidon3(hash1, hash2, claimantField);
23
+ return bigIntToBytesBE(finalHash);
24
+ }
25
+ // -----------------------------------------------------------------------------
26
+ // Circuit Input Preparation
27
+ // -----------------------------------------------------------------------------
28
+ /**
29
+ * Convert i64 to field element (handles negative via modular arithmetic).
30
+ * For negative values, we add the field modulus to get the equivalent positive representation.
31
+ */
32
+ export function i64ToField(value) {
33
+ if (value >= 0n) {
34
+ return value % BN254_FR_MODULUS;
35
+ }
36
+ return (BN254_FR_MODULUS + value) % BN254_FR_MODULUS;
37
+ }
38
+ /**
39
+ * Prepare transaction circuit inputs from high-level parameters.
40
+ * This converts the SDK types into the format expected by the circuit.
41
+ */
42
+ export function prepareTransactionInputs(params) {
43
+ const { inputUTXOs, outputUTXOs, root, publicAmount, extData, mintAddress } = params;
44
+ const extDataHash = computeExtDataHash(extData);
45
+ const mintField = pubkeyToField(mintAddress);
46
+ // Prepare input UTXO data
47
+ const inAmount = [
48
+ inputUTXOs[0].amount,
49
+ inputUTXOs[1].amount,
50
+ ];
51
+ const inPubkey = [
52
+ inputUTXOs[0].pubkey,
53
+ inputUTXOs[1].pubkey,
54
+ ];
55
+ const inBlinding = [
56
+ inputUTXOs[0].blinding,
57
+ inputUTXOs[1].blinding,
58
+ ];
59
+ const inPathIndex = [
60
+ inputUTXOs[0].pathIndex,
61
+ inputUTXOs[1].pathIndex,
62
+ ];
63
+ const inPathElements = [
64
+ inputUTXOs[0].pathElements.map((e) => bytesToBigIntBE(e) % BN254_FR_MODULUS),
65
+ inputUTXOs[1].pathElements.map((e) => bytesToBigIntBE(e) % BN254_FR_MODULUS),
66
+ ];
67
+ const inPrivateKey = [
68
+ inputUTXOs[0].privateKey,
69
+ inputUTXOs[1].privateKey,
70
+ ];
71
+ // Prepare output UTXO data
72
+ const outAmount = [
73
+ outputUTXOs[0].amount,
74
+ outputUTXOs[1].amount,
75
+ ];
76
+ const outPubkey = [
77
+ outputUTXOs[0].pubkey,
78
+ outputUTXOs[1].pubkey,
79
+ ];
80
+ const outBlinding = [
81
+ outputUTXOs[0].blinding,
82
+ outputUTXOs[1].blinding,
83
+ ];
84
+ // Derive nullifiers for input UTXOs
85
+ const inputNullifiers = [
86
+ deriveNullifier(inputUTXOs[0], inputUTXOs[0].pathIndex, inputUTXOs[0].privateKey),
87
+ deriveNullifier(inputUTXOs[1], inputUTXOs[1].pathIndex, inputUTXOs[1].privateKey),
88
+ ];
89
+ return {
90
+ root,
91
+ publicAmount,
92
+ extDataHash,
93
+ mintAddress,
94
+ inputNullifiers,
95
+ outputCommitments: [outputUTXOs[0].commitment, outputUTXOs[1].commitment],
96
+ inAmount,
97
+ inPubkey,
98
+ inBlinding,
99
+ inPathIndex,
100
+ inPathElements,
101
+ inPrivateKey,
102
+ outAmount,
103
+ outPubkey,
104
+ outBlinding,
105
+ };
106
+ }
107
+ // -----------------------------------------------------------------------------
108
+ // Proof Packing
109
+ // -----------------------------------------------------------------------------
110
+ /**
111
+ * Convert bigint to 32-byte big-endian representation.
112
+ * The alt_bn128 Solana precompile (and ark-works via change_endianness) expect BE.
113
+ */
114
+ function bigintTo32BytesBE(x) {
115
+ const out = new Uint8Array(32);
116
+ let v = x;
117
+ for (let i = 31; i >= 0; i--) {
118
+ out[i] = Number(v & 0xffn);
119
+ v >>= 8n;
120
+ }
121
+ return out;
122
+ }
123
+ /**
124
+ * Turn a snarkjs Groth16 proof into 256 bytes:
125
+ * A(G1) | B(G2) | C(G1)
126
+ * where:
127
+ * A = (ax, ay) -> 64 bytes
128
+ * B = (bx1, bx0, by1, by0) -> 128 bytes (swapped pairs for alt_bn128)
129
+ * C = (cx, cy) -> 64 bytes
130
+ *
131
+ * Each field element is encoded as 32-byte BE to match the on-chain verifier.
132
+ * We ignore the extra projective/z components snarkjs keeps.
133
+ */
134
+ export function packProofToBytes(proof) {
135
+ const chunks = [];
136
+ const ax = BigInt(proof.pi_a[0]);
137
+ const ay = BigInt(proof.pi_a[1]);
138
+ chunks.push(bigintTo32BytesBE(ax), bigintTo32BytesBE(ay));
139
+ const bx = proof.pi_b[0];
140
+ const by = proof.pi_b[1];
141
+ const bx0 = BigInt(bx[0]);
142
+ const bx1 = BigInt(bx[1]);
143
+ const by0 = BigInt(by[0]);
144
+ const by1 = BigInt(by[1]);
145
+ // alt_bn128 expects the G2 pairs swapped: bx1 before bx0, by1 before by0
146
+ chunks.push(bigintTo32BytesBE(bx1), bigintTo32BytesBE(bx0), bigintTo32BytesBE(by1), bigintTo32BytesBE(by0));
147
+ const cx = BigInt(proof.pi_c[0]);
148
+ const cy = BigInt(proof.pi_c[1]);
149
+ chunks.push(bigintTo32BytesBE(cx), bigintTo32BytesBE(cy));
150
+ const out = new Uint8Array(32 * (2 + 4 + 2)); // 256
151
+ let offset = 0;
152
+ for (const c of chunks) {
153
+ out.set(c, offset);
154
+ offset += c.length;
155
+ }
156
+ return out;
157
+ }
158
+ /**
159
+ * Encode a snarkjs proof into the TransactionProofStruct format expected by the on-chain program.
160
+ */
161
+ export function encodeSnarkjsProofToTransactionProof(proof) {
162
+ const full = packProofToBytes(proof);
163
+ if (full.length !== 256) {
164
+ throw new Error(`Expected 256 proof bytes, got ${full.length}`);
165
+ }
166
+ const proofA = full.slice(0, 64);
167
+ const proofB = full.slice(64, 192);
168
+ const proofC = full.slice(192, 256);
169
+ return {
170
+ proofA: Array.from(proofA),
171
+ proofB: Array.from(proofB),
172
+ proofC: Array.from(proofC),
173
+ };
174
+ }
175
+ // -----------------------------------------------------------------------------
176
+ // Snarkjs Input Formatting
177
+ // -----------------------------------------------------------------------------
178
+ /**
179
+ * Convert TransactionCircuitInputs to the format expected by snarkjs.groth16.fullProve().
180
+ * All values are converted to strings (snarkjs expects string inputs for field elements).
181
+ */
182
+ export function formatInputsForSnarkjs(inputs) {
183
+ return {
184
+ // Public inputs
185
+ root: bytesToBigIntBE(inputs.root).toString(),
186
+ publicAmount: i64ToField(inputs.publicAmount).toString(),
187
+ extDataHash: bytesToBigIntBE(inputs.extDataHash).toString(),
188
+ mintAddress: pubkeyToField(inputs.mintAddress).toString(),
189
+ inputNullifier: inputs.inputNullifiers.map((n) => bytesToBigIntBE(n).toString()),
190
+ outputCommitment: inputs.outputCommitments.map((c) => bytesToBigIntBE(c).toString()),
191
+ // Private inputs - Input UTXOs
192
+ inAmount: inputs.inAmount.map((a) => a.toString()),
193
+ inPubkey: inputs.inPubkey.map((p) => p.toString()),
194
+ inBlinding: inputs.inBlinding.map((b) => b.toString()),
195
+ inPathIndex: inputs.inPathIndex.map((i) => i.toString()),
196
+ inPathElements: inputs.inPathElements.map((path) => path.map((e) => e.toString())),
197
+ inPrivateKey: inputs.inPrivateKey.map((k) => k.toString()),
198
+ // Private inputs - Output UTXOs
199
+ outAmount: inputs.outAmount.map((a) => a.toString()),
200
+ outPubkey: inputs.outPubkey.map((p) => p.toString()),
201
+ outBlinding: inputs.outBlinding.map((b) => b.toString()),
202
+ };
203
+ }
204
+ /**
205
+ * Convert SwapCircuitInputs into the shape expected by
206
+ * snarkjs.groth16.fullProve() for swap.circom. Every value is emitted as a
207
+ * decimal string (or string array) because snarkjs only accepts field
208
+ * elements as strings.
209
+ *
210
+ * Mirrors `generateSwapProof` in
211
+ * relayer-server/src/controllers/swap.helpers.ts so SDK-side swap proofs
212
+ * verify against the same circuit the relayer uses.
213
+ */
214
+ export function formatSwapInputsForSnarkjs(inputs) {
215
+ return {
216
+ // Public inputs
217
+ sourceRoot: bytesToBigIntBE(inputs.sourceRoot).toString(),
218
+ swapParamsHash: bytesToBigIntBE(inputs.swapParamsHash).toString(),
219
+ extDataHash: bytesToBigIntBE(inputs.extDataHash).toString(),
220
+ sourceMint: pubkeyToField(inputs.sourceMint).toString(),
221
+ destMint: pubkeyToField(inputs.destMint).toString(),
222
+ inputNullifier: inputs.inputNullifiers.map((n) => bytesToBigIntBE(n).toString()),
223
+ changeCommitment: bytesToBigIntBE(inputs.changeCommitment).toString(),
224
+ destCommitment: bytesToBigIntBE(inputs.destCommitment).toString(),
225
+ swapAmount: inputs.swapAmount.toString(),
226
+ // Private inputs - Input UTXOs
227
+ inAmount: inputs.inAmount.map((a) => a.toString()),
228
+ inPubkey: inputs.inPubkey.map((p) => p.toString()),
229
+ inBlinding: inputs.inBlinding.map((b) => b.toString()),
230
+ inPathIndex: inputs.inPathIndex.map((i) => i.toString()),
231
+ inPathElements: inputs.inPathElements.map((path) => path.map((e) => e.toString())),
232
+ inPrivateKey: inputs.inPrivateKey.map((k) => k.toString()),
233
+ // Private inputs - Change UTXO
234
+ changeAmount: inputs.changeAmount.toString(),
235
+ changePubkey: inputs.changePubkey.toString(),
236
+ changeBlinding: inputs.changeBlinding.toString(),
237
+ // Private inputs - Destination UTXO
238
+ destAmount: inputs.destAmount.toString(),
239
+ destPubkey: inputs.destPubkey.toString(),
240
+ destBlinding: inputs.destBlinding.toString(),
241
+ // Swap params
242
+ minAmountOut: inputs.minAmountOut.toString(),
243
+ deadline: inputs.deadline.toString(),
244
+ };
245
+ }
246
+ // -----------------------------------------------------------------------------
247
+ // Swap Parameter Hashing
248
+ // -----------------------------------------------------------------------------
249
+ /**
250
+ * Compute the swap params hash that the swap circuit and on-chain program verify.
251
+ * Mirrors relayer-server/src/controllers/swap.helpers.ts `computeSwapParamsHash`.
252
+ *
253
+ * Circuit formula (swap.circom lines 293-307):
254
+ * mintPairHash = Poseidon(sourceMint, destMint)
255
+ * swapTermsHash = Poseidon(minAmountOut, deadline, destAmount)
256
+ * swapParamsHash = Poseidon(mintPairHash, swapTermsHash)
257
+ *
258
+ * @param sourceMint Source pool mint
259
+ * @param destMint Destination pool mint
260
+ * @param minAmountOut Minimum accepted output amount (slippage protected)
261
+ * @param deadline Unix timestamp deadline (i64, 0 = no deadline)
262
+ * @param destAmount Actual amount committed to destination UTXO (≥ minAmountOut -
263
+ * relayerFee). If omitted, defaults to minAmountOut.
264
+ */
265
+ export function computeSwapParamsHash(sourceMint, destMint, minAmountOut, deadline, destAmount) {
266
+ const srcField = pubkeyToField(sourceMint);
267
+ const dstField = pubkeyToField(destMint);
268
+ const mintPairHash = poseidon2(srcField, dstField);
269
+ const dest = destAmount ?? minAmountOut;
270
+ const swapTermsHash = poseidon3(minAmountOut, deadline, dest);
271
+ const paramsHash = poseidon2(mintPairHash, swapTermsHash);
272
+ return bigIntToBytesBE(paramsHash);
273
+ }
274
+ /**
275
+ * Compute the swap data hash committed on-chain for Jupiter instruction integrity.
276
+ * SHA-256 of the raw Jupiter swap instruction data bytes.
277
+ * Mirrors relayer-server/src/controllers/swap.helpers.ts `computeSwapDataHash`.
278
+ */
279
+ export function computeSwapDataHash(swapData) {
280
+ return sha256(swapData instanceof Buffer ? new Uint8Array(swapData) : swapData);
281
+ }
@@ -0,0 +1,54 @@
1
+ import { TransactionProofBuilder, RawProof } from "./proof.js";
2
+ /**
3
+ * Circuit artifact paths required by snarkjs.
4
+ *
5
+ * These files are large (10-100 MB) and should NOT be bundled in npm.
6
+ * Host them on GitHub Releases, IPFS, or a CDN and download at runtime.
7
+ *
8
+ * For Node.js: pass file system paths (e.g. "./circuits/transaction.wasm").
9
+ * For browsers: pass Uint8Array buffers or URLs that fetch returns as ArrayBuffer.
10
+ */
11
+ export interface CircuitArtifacts {
12
+ /** Path or buffer for the compiled WASM witness generator */
13
+ wasmPath: string | Uint8Array;
14
+ /** Path or buffer for the Groth16 proving key (.zkey) */
15
+ zkeyPath: string | Uint8Array;
16
+ }
17
+ /**
18
+ * Create a proof builder function for the transaction circuit.
19
+ *
20
+ * Uses dynamic `import("snarkjs")` so the dependency is only loaded when
21
+ * proof generation is actually called. Install snarkjs as a peer dependency:
22
+ *
23
+ * ```
24
+ * npm install snarkjs
25
+ * ```
26
+ *
27
+ * @param artifacts Paths or buffers to the circuit WASM and zkey files
28
+ * @returns A `TransactionProofBuilder` callback compatible with the SDK's client functions
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * import { createTransactionProver } from "@veilo/sdk-core";
33
+ *
34
+ * const prover = createTransactionProver({
35
+ * wasmPath: "./circuits/transaction.wasm",
36
+ * zkeyPath: "./circuits/transaction_final.zkey",
37
+ * });
38
+ *
39
+ * // Pass to SDK functions that accept a TransactionProofBuilder
40
+ * const proof = await prover(circuitInputs);
41
+ * ```
42
+ */
43
+ export declare function createTransactionProver(artifacts: CircuitArtifacts): TransactionProofBuilder;
44
+ /**
45
+ * Verify a Groth16 proof against public signals and a verification key.
46
+ *
47
+ * Useful for local verification before submitting to the relayer.
48
+ *
49
+ * @param proof The raw snarkjs proof
50
+ * @param publicSignals Array of public signal strings
51
+ * @param vkeyPath Path to the verification key JSON file, or the parsed JSON object
52
+ * @returns true if the proof is valid
53
+ */
54
+ export declare function verifyProof(proof: RawProof, publicSignals: string[], vkey: string | object): Promise<boolean>;
@@ -0,0 +1,75 @@
1
+ import { formatInputsForSnarkjs, } from "./proof.js";
2
+ /** Lazy-load snarkjs — works in both CJS and ESM contexts. */
3
+ async function loadSnarkjs() {
4
+ try {
5
+ // Dynamic import works in both CJS (Node 12+) and ESM
6
+ return await Function('return import("snarkjs")')();
7
+ }
8
+ catch {
9
+ throw new Error("snarkjs is required for proof generation. Install it: npm install snarkjs");
10
+ }
11
+ }
12
+ /**
13
+ * Create a proof builder function for the transaction circuit.
14
+ *
15
+ * Uses dynamic `import("snarkjs")` so the dependency is only loaded when
16
+ * proof generation is actually called. Install snarkjs as a peer dependency:
17
+ *
18
+ * ```
19
+ * npm install snarkjs
20
+ * ```
21
+ *
22
+ * @param artifacts Paths or buffers to the circuit WASM and zkey files
23
+ * @returns A `TransactionProofBuilder` callback compatible with the SDK's client functions
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * import { createTransactionProver } from "@veilo/sdk-core";
28
+ *
29
+ * const prover = createTransactionProver({
30
+ * wasmPath: "./circuits/transaction.wasm",
31
+ * zkeyPath: "./circuits/transaction_final.zkey",
32
+ * });
33
+ *
34
+ * // Pass to SDK functions that accept a TransactionProofBuilder
35
+ * const proof = await prover(circuitInputs);
36
+ * ```
37
+ */
38
+ export function createTransactionProver(artifacts) {
39
+ return async (inputs) => {
40
+ const snarkjs = await loadSnarkjs();
41
+ const formattedInputs = formatInputsForSnarkjs(inputs);
42
+ const { proof } = await snarkjs.groth16.fullProve(formattedInputs, artifacts.wasmPath, artifacts.zkeyPath);
43
+ return proof;
44
+ };
45
+ }
46
+ /**
47
+ * Verify a Groth16 proof against public signals and a verification key.
48
+ *
49
+ * Useful for local verification before submitting to the relayer.
50
+ *
51
+ * @param proof The raw snarkjs proof
52
+ * @param publicSignals Array of public signal strings
53
+ * @param vkeyPath Path to the verification key JSON file, or the parsed JSON object
54
+ * @returns true if the proof is valid
55
+ */
56
+ export async function verifyProof(proof, publicSignals, vkey) {
57
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
58
+ let snarkjs;
59
+ try {
60
+ snarkjs = require("snarkjs");
61
+ }
62
+ catch {
63
+ throw new Error('snarkjs is required for proof verification. Install it: npm install snarkjs');
64
+ }
65
+ let vkeyObj;
66
+ if (typeof vkey === "string") {
67
+ // Node.js file path — read and parse
68
+ const fs = await import("fs");
69
+ vkeyObj = JSON.parse(fs.readFileSync(vkey, "utf-8"));
70
+ }
71
+ else {
72
+ vkeyObj = vkey;
73
+ }
74
+ return snarkjs.groth16.verify(vkeyObj, publicSignals, proof);
75
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Cross-platform CSPRNG.
3
+ *
4
+ * Deliberately backed by `nacl.randomBytes` rather than Node's `crypto.randomBytes`:
5
+ * tweetnacl resolves to `crypto.getRandomValues` in browsers and to Node's CSPRNG
6
+ * under Node, so this module carries no `node:crypto` import. That matters because
7
+ * every deposit path reaches this code via `createUTXO`/`generateKeypair`, and a
8
+ * bare `import { randomBytes } from "crypto"` throws at bundle time in browser
9
+ * builds that have no Node polyfill.
10
+ *
11
+ * tweetnacl is already a hard dependency, so this adds no install weight.
12
+ */
13
+ /** `length` cryptographically-secure random bytes. */
14
+ export declare function randomBytes(length: number): Uint8Array;
15
+ /** 32 cryptographically-secure random bytes — the common case for blindings and seeds. */
16
+ export declare function randomBytes32(): Uint8Array;
@@ -0,0 +1,21 @@
1
+ import nacl from "tweetnacl";
2
+ /**
3
+ * Cross-platform CSPRNG.
4
+ *
5
+ * Deliberately backed by `nacl.randomBytes` rather than Node's `crypto.randomBytes`:
6
+ * tweetnacl resolves to `crypto.getRandomValues` in browsers and to Node's CSPRNG
7
+ * under Node, so this module carries no `node:crypto` import. That matters because
8
+ * every deposit path reaches this code via `createUTXO`/`generateKeypair`, and a
9
+ * bare `import { randomBytes } from "crypto"` throws at bundle time in browser
10
+ * builds that have no Node polyfill.
11
+ *
12
+ * tweetnacl is already a hard dependency, so this adds no install weight.
13
+ */
14
+ /** `length` cryptographically-secure random bytes. */
15
+ export function randomBytes(length) {
16
+ return nacl.randomBytes(length);
17
+ }
18
+ /** 32 cryptographically-secure random bytes — the common case for blindings and seeds. */
19
+ export function randomBytes32() {
20
+ return nacl.randomBytes(32);
21
+ }