@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
@@ -0,0 +1,17 @@
1
+ export * from "./client.js";
2
+ export * from "./utxo.js";
3
+ export * from "./config.js";
4
+ export * from "./merkle.js";
5
+ export * from "./proof.js";
6
+ export * from "./events.js";
7
+ export { createVeiloProgram, createReadonlyVeiloProgram, PRIVACY_POOL_IDL, PRIVACY_POOL_PROGRAM_ID, } from "./program.js";
8
+ export * from "./shield/index.js";
9
+ export { createTransactionProver, createSwapProver, verifyProof, } from "./prover.js";
10
+ export type { CircuitArtifacts } from "./prover.js";
11
+ export * from "./compactNote.js";
12
+ export { randomBytes, randomBytes32 } from "./random.js";
13
+ export * from "./relayer.js";
14
+ export * from "./cloak.js";
15
+ export { withRpcRetry, DEFAULT_RPC_RETRY } from "./retry.js";
16
+ export type { RpcRetryPolicy } from "./retry.js";
17
+ export { initPoseidon, getPoseidon, BN254_FR_MODULUS, bytesToBigIntBE, bigIntToBytesBE, frToBigInt, poseidon1, poseidon2, poseidon3, poseidon4, pubkeyToField, } from "./poseidon.js";
package/dist/esm/index.js CHANGED
@@ -1,22 +1,31 @@
1
1
  // Core client functions
2
- export * from "./client";
2
+ export * from "./client.js";
3
3
  // UTXO types and functions (replaces note.ts)
4
- export * from "./utxo";
4
+ export * from "./utxo.js";
5
5
  // Configuration constants and types
6
- export * from "./config";
6
+ export * from "./config.js";
7
7
  // Merkle tree implementation
8
- export * from "./merkle";
8
+ export * from "./merkle.js";
9
9
  // Proof types and helpers
10
- export * from "./proof";
10
+ export * from "./proof.js";
11
11
  // On-chain event scanning and tree reconstruction
12
- export * from "./events";
12
+ export * from "./events.js";
13
13
  // Program factory and IDL
14
- export { createVeiloProgram, PRIVACY_POOL_IDL, PRIVACY_POOL_PROGRAM_ID, } from "./program";
14
+ export { createVeiloProgram, createReadonlyVeiloProgram, PRIVACY_POOL_IDL, PRIVACY_POOL_PROGRAM_ID, } from "./program.js";
15
+ // Wallet-agnostic shielding — build an unsigned deposit for any external wallet
16
+ export * from "./shield/index.js";
15
17
  // Proof generation (requires snarkjs peer dependency)
16
- export { createTransactionProver, verifyProof } from "./prover";
18
+ export { createTransactionProver, createSwapProver, verifyProof, } from "./prover.js";
19
+ // Compact note ciphers — the on-chain payload that makes notes recoverable
20
+ // from chain data alone, without any relayer involvement.
21
+ export * from "./compactNote.js";
22
+ // Cross-platform CSPRNG (no node:crypto import — safe in browser bundles)
23
+ export { randomBytes, randomBytes32 } from "./random.js";
17
24
  // Relayer API client
18
- export * from "./relayer";
25
+ export * from "./relayer.js";
26
+ // Veilo Cloak Partner API client and helpers
27
+ export * from "./cloak.js";
19
28
  // RPC retry / resilience
20
- export { withRpcRetry, DEFAULT_RPC_RETRY } from "./retry";
29
+ export { withRpcRetry, DEFAULT_RPC_RETRY } from "./retry.js";
21
30
  // Poseidon hash functions
22
- export { initPoseidon, getPoseidon, BN254_FR_MODULUS, bytesToBigIntBE, bigIntToBytesBE, frToBigInt, poseidon1, poseidon2, poseidon3, poseidon4, pubkeyToField, } from "./poseidon";
31
+ export { initPoseidon, getPoseidon, BN254_FR_MODULUS, bytesToBigIntBE, bigIntToBytesBE, frToBigInt, poseidon1, poseidon2, poseidon3, poseidon4, pubkeyToField, } from "./poseidon.js";
@@ -0,0 +1,77 @@
1
+ export type MerklePath = {
2
+ /** Index of the leaf in the tree (0-based) */
3
+ leafIndex: number;
4
+ /** Sibling hashes from leaf level up to (but not including) the root */
5
+ path: Uint8Array[];
6
+ /**
7
+ * 0 = current node is left child, sibling is right
8
+ * 1 = current node is right child, sibling is left
9
+ * (matches your Circom `pathIndices` semantics)
10
+ */
11
+ indices: number[];
12
+ };
13
+ /**
14
+ * Circuit-compatible Merkle path format.
15
+ * The pathIndex is used directly (circuit converts to bits internally via Num2Bits).
16
+ */
17
+ export type CircuitMerklePath = {
18
+ /** Leaf index (circuit converts to bits internally) */
19
+ pathIndex: number;
20
+ /** Sibling hashes as field elements */
21
+ pathElements: bigint[];
22
+ };
23
+ export type Bytes32 = Uint8Array;
24
+ /**
25
+ * Fixed-depth binary Merkle tree over BN254 Fr:
26
+ * - Leaves and internal nodes are 32-byte big-endian Fr elements.
27
+ * - Hash is Poseidon(2)(left, right).
28
+ * - Unfilled leaves use the Poseidon zero chain:
29
+ * zero[0] = 0
30
+ * zero[i+1] = Poseidon(zero[i], zero[i])
31
+ *
32
+ * Depth must match:
33
+ * - Circom WithdrawCircuit(depth)
34
+ * - Rust MERKLE_TREE_HEIGHT
35
+ */
36
+ export declare class MerkleTree {
37
+ readonly depth: number;
38
+ readonly zeroes: Uint8Array[];
39
+ readonly layers: Uint8Array[][];
40
+ nextIndex: number;
41
+ constructor(depth?: number);
42
+ get capacity(): number;
43
+ get root(): Uint8Array;
44
+ /**
45
+ * Insert a leaf and recompute the path up to the root.
46
+ * Returns the leaf index and the new root.
47
+ */
48
+ insert(leaf: Uint8Array): {
49
+ index: number;
50
+ root: Uint8Array;
51
+ };
52
+ /**
53
+ * Return Merkle proof (path + indices) for a given leaf index.
54
+ * This feeds directly into the Circom `MerklePathVerifier(depth)`:
55
+ * - `pathElements[i]` = sibling
56
+ * - `pathIndices[i]` = 0/1 as defined above
57
+ */
58
+ getPath(index: number): MerklePath;
59
+ /**
60
+ * Return Merkle proof in circuit-compatible format.
61
+ * The circuit uses pathIndex directly and converts to bits via Num2Bits(levels).
62
+ */
63
+ getCircuitPath(index: number): CircuitMerklePath;
64
+ }
65
+ /**
66
+ * The Poseidon zero chain: `z[0] = 0`, `z[i+1] = Poseidon(z[i], z[i])`.
67
+ *
68
+ * This is the `zeroes` array a `MerkleTree` builds in its constructor, without
69
+ * the tree. Deposits need a Merkle path for their dummy inputs, but the circuit
70
+ * disables the path check when a input amount is zero (`enabled <== 1 - IsZero(amount)`
71
+ * in transaction.circom), so the values are never constrained — only their
72
+ * count is.
73
+ *
74
+ * Allocating a full depth-22 `MerkleTree` for this costs ~130 MB and several
75
+ * seconds. This costs 22 hashes. Any deposit-shaped transaction should use it.
76
+ */
77
+ export declare function poseidonZeroChain(depth?: number): Uint8Array[];
@@ -1,4 +1,4 @@
1
- import { BN254_FR_MODULUS, bytesToBigIntBE, bigIntToBytesBE, getPoseidon, frToBigInt, } from "./poseidon";
1
+ import { BN254_FR_MODULUS, bytesToBigIntBE, bigIntToBytesBE, getPoseidon, frToBigInt, } from "./poseidon.js";
2
2
  // Poseidon-based hash of two 32-byte field elements.
3
3
  function hashPair(left, right) {
4
4
  if (left.length !== 32 || right.length !== 32) {
@@ -127,3 +127,25 @@ export class MerkleTree {
127
127
  };
128
128
  }
129
129
  }
130
+ /**
131
+ * The Poseidon zero chain: `z[0] = 0`, `z[i+1] = Poseidon(z[i], z[i])`.
132
+ *
133
+ * This is the `zeroes` array a `MerkleTree` builds in its constructor, without
134
+ * the tree. Deposits need a Merkle path for their dummy inputs, but the circuit
135
+ * disables the path check when a input amount is zero (`enabled <== 1 - IsZero(amount)`
136
+ * in transaction.circom), so the values are never constrained — only their
137
+ * count is.
138
+ *
139
+ * Allocating a full depth-22 `MerkleTree` for this costs ~130 MB and several
140
+ * seconds. This costs 22 hashes. Any deposit-shaped transaction should use it.
141
+ */
142
+ export function poseidonZeroChain(depth = 22) {
143
+ const zeroes = [];
144
+ let zero = bigIntToBytesBE(0n);
145
+ zeroes.push(zero);
146
+ for (let level = 1; level <= depth; level++) {
147
+ zero = hashPair(zero, zero);
148
+ zeroes.push(zero);
149
+ }
150
+ return zeroes;
151
+ }
@@ -0,0 +1,41 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ import type { SerializedUTXO } from "./model.js";
3
+ /**
4
+ * Encrypted UTXO note — can be broadcast alongside a transaction so the
5
+ * recipient can recover their note without a server.
6
+ */
7
+ export type EncryptedNote = {
8
+ /** 24-byte NaCl box nonce */
9
+ nonce: Uint8Array;
10
+ /** Ciphertext containing amount + blinding + privateKey */
11
+ ciphertext: Uint8Array;
12
+ /** Sender's ephemeral 32-byte X25519 public key */
13
+ senderEphemeralPubkey: Uint8Array;
14
+ };
15
+ /**
16
+ * Derive a NaCl X25519 encryption keypair from a UTXO private key.
17
+ * The keypair is deterministic: same privateKey always yields the same pair.
18
+ */
19
+ export declare function deriveEncryptionKeypair(privateKey: bigint): {
20
+ publicKey: Uint8Array;
21
+ secretKey: Uint8Array;
22
+ };
23
+ /**
24
+ * Encrypt a UTXO so the recipient can recover it from on-chain events.
25
+ *
26
+ * Plaintext layout (72 bytes):
27
+ * [0..8] amount — 8-byte little-endian u64
28
+ * [8..40] blinding — 32-byte big-endian field element
29
+ * [40..72] privateKey — 32-byte big-endian field element
30
+ *
31
+ * Uses ephemeral-key NaCl box so the sender key is single-use.
32
+ */
33
+ export declare function encryptUTXONote(utxo: SerializedUTXO & {
34
+ privateKey: bigint;
35
+ }, recipientEncPubkey: Uint8Array): EncryptedNote;
36
+ /**
37
+ * Try to decrypt an encrypted UTXO note using the recipient's private key.
38
+ * Returns the SerializedUTXO if decryption succeeds and the commitment
39
+ * matches, or `null` if the note was not intended for this key.
40
+ */
41
+ export declare function decryptUTXONote(note: EncryptedNote, recipientPrivateKey: bigint, commitment: Uint8Array, mintAddress: PublicKey): SerializedUTXO | null;
@@ -0,0 +1,67 @@
1
+ import { sha256 } from "@noble/hashes/sha256";
2
+ import nacl from "tweetnacl";
3
+ import { BN254_FR_MODULUS, bigIntToBytesBE, bytesToBigIntBE, pubkeyToField, } from "../poseidon.js";
4
+ import { commitUTXO, derivePublicKey } from "./model.js";
5
+ /**
6
+ * Derive a NaCl X25519 encryption keypair from a UTXO private key.
7
+ * The keypair is deterministic: same privateKey always yields the same pair.
8
+ */
9
+ export function deriveEncryptionKeypair(privateKey) {
10
+ const seed = sha256(bigIntToBytesBE(privateKey));
11
+ return nacl.box.keyPair.fromSecretKey(seed);
12
+ }
13
+ /**
14
+ * Encrypt a UTXO so the recipient can recover it from on-chain events.
15
+ *
16
+ * Plaintext layout (72 bytes):
17
+ * [0..8] amount — 8-byte little-endian u64
18
+ * [8..40] blinding — 32-byte big-endian field element
19
+ * [40..72] privateKey — 32-byte big-endian field element
20
+ *
21
+ * Uses ephemeral-key NaCl box so the sender key is single-use.
22
+ */
23
+ export function encryptUTXONote(utxo, recipientEncPubkey) {
24
+ const nonce = nacl.randomBytes(nacl.box.nonceLength); // 24 bytes
25
+ const ephemeral = nacl.box.keyPair();
26
+ const amountBuf = new Uint8Array(8);
27
+ let v = utxo.amount;
28
+ for (let i = 0; i < 8; i++) {
29
+ amountBuf[i] = Number(v & 0xffn);
30
+ v >>= 8n;
31
+ }
32
+ const plaintext = new Uint8Array(72);
33
+ plaintext.set(amountBuf, 0);
34
+ plaintext.set(bigIntToBytesBE(utxo.blinding), 8);
35
+ plaintext.set(bigIntToBytesBE(utxo.privateKey), 40);
36
+ const sharedKey = nacl.box.before(recipientEncPubkey, ephemeral.secretKey);
37
+ const ciphertext = nacl.box.after(plaintext, nonce, sharedKey);
38
+ return { nonce, ciphertext, senderEphemeralPubkey: ephemeral.publicKey };
39
+ }
40
+ /**
41
+ * Try to decrypt an encrypted UTXO note using the recipient's private key.
42
+ * Returns the SerializedUTXO if decryption succeeds and the commitment
43
+ * matches, or `null` if the note was not intended for this key.
44
+ */
45
+ export function decryptUTXONote(note, recipientPrivateKey, commitment, mintAddress) {
46
+ const { secretKey } = deriveEncryptionKeypair(recipientPrivateKey);
47
+ const sharedKey = nacl.box.before(note.senderEphemeralPubkey, secretKey);
48
+ const plaintext = nacl.box.open.after(note.ciphertext, note.nonce, sharedKey);
49
+ if (!plaintext || plaintext.length < 72)
50
+ return null;
51
+ let amount = 0n;
52
+ for (let i = 7; i >= 0; i--)
53
+ amount = (amount << 8n) | BigInt(plaintext[i]);
54
+ const blinding = bytesToBigIntBE(plaintext.slice(8, 40)) % BN254_FR_MODULUS;
55
+ const privateKey = bytesToBigIntBE(plaintext.slice(40, 72)) % BN254_FR_MODULUS;
56
+ const pubkey = derivePublicKey(privateKey);
57
+ const utxo = {
58
+ amount,
59
+ pubkey,
60
+ blinding,
61
+ mintAddress: pubkeyToField(mintAddress),
62
+ };
63
+ const expectedCommitment = commitUTXO(utxo);
64
+ if (!expectedCommitment.every((b, i) => b === commitment[i]))
65
+ return null;
66
+ return { ...utxo, commitment, privateKey };
67
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./model.js";
2
+ export * from "./nullifier.js";
3
+ export * from "./encryption.js";
4
+ export * from "./mailbox.js";
5
+ export * from "./recovery.js";
@@ -0,0 +1,5 @@
1
+ export * from "./model.js";
2
+ export * from "./nullifier.js";
3
+ export * from "./encryption.js";
4
+ export * from "./mailbox.js";
5
+ export * from "./recovery.js";
@@ -0,0 +1,70 @@
1
+ /** Note data stored inside a Blind Mailbox encrypted blob */
2
+ export type BlindMailboxNoteData = {
3
+ blinding: Uint8Array;
4
+ leafIndex: number;
5
+ commitment: Uint8Array;
6
+ amount: bigint;
7
+ mintAddress: string;
8
+ treeId: number;
9
+ };
10
+ /** Encrypted note as returned by the relayer's /api/notes endpoint */
11
+ export type BlindMailboxNote = {
12
+ commitment: string;
13
+ ephemeralPublicKey: string;
14
+ encryptedBlob: string;
15
+ timestamp?: number;
16
+ blockHeight?: number;
17
+ txSignature?: string;
18
+ };
19
+ /** Decrypted note data after calling decryptBlindMailboxNote */
20
+ export type DecryptedNote = {
21
+ blinding: Uint8Array;
22
+ leafIndex: number;
23
+ commitment: Uint8Array;
24
+ amount: bigint;
25
+ timestamp: number;
26
+ mintAddress?: string;
27
+ treeId: number;
28
+ };
29
+ /** Derive 32-byte encryption key from a shared secret using SHA-512 truncated.
30
+ * Must match the production "sha256" helper which uses nacl.hash (SHA-512) [0:32].
31
+ *
32
+ * DO NOT "fix" this to real SHA-256. The name in the sibling implementations
33
+ * (`sha256ForNotes`) is a misnomer, but the bytes are load-bearing: changing the
34
+ * KDF makes every historical note undecryptable.
35
+ *
36
+ * @internal Exported for `compactNote.ts`. Not part of the stable public API. */
37
+ export declare function kdfSHA512_256(secret: Uint8Array): Uint8Array;
38
+ /** Convert an Ed25519 private seed (32 bytes) to an X25519 scalar.
39
+ * Equivalent to ed2curve.convertSecretKey(seed).
40
+ *
41
+ * @internal Exported for `compactNote.ts`. Not part of the stable public API. */
42
+ export declare function ed25519SeedToX25519Private(seed: Uint8Array): Uint8Array;
43
+ /** Convert an Ed25519 compressed public key (32 bytes) to X25519.
44
+ * Equivalent to ed2curve.convertPublicKey(pubkey).
45
+ * Edwards y-coord → Montgomery u via: u = (1 + y) / (1 - y) (mod p).
46
+ *
47
+ * @internal Exported for `compactNote.ts`. Not part of the stable public API. */
48
+ export declare function ed25519PublicKeyToX25519(pubkey: Uint8Array): Uint8Array;
49
+ /**
50
+ * Encrypt a UTXO note for a recipient identified by their Solana wallet public key.
51
+ * Produces a blob the relayer will store and serve via POST /notes/save.
52
+ *
53
+ * @param recipientWalletPubkey 32-byte Ed25519 public key (wallet.publicKey.toBytes())
54
+ * @param noteData UTXO details to encrypt
55
+ */
56
+ export declare function encryptBlindMailboxNote(recipientWalletPubkey: Uint8Array, noteData: BlindMailboxNoteData): {
57
+ ephemeralPublicKey: Uint8Array;
58
+ encryptedBlob: string;
59
+ };
60
+ /**
61
+ * Decrypt a Blind Mailbox note using the recipient's Solana wallet private seed.
62
+ *
63
+ * @param walletSecretKey Full 64-byte wallet.secretKey — the first 32 bytes are the seed
64
+ * @param ephemeralPublicKey 32-byte raw public key stored alongside the encrypted blob.
65
+ * This may be either an Ed25519 pubkey (from Keypair.generate())
66
+ * or a raw X25519 pubkey — the decryption path handles both:
67
+ * if it is an Ed25519 key it is converted; otherwise used directly.
68
+ * @param encryptedBlob Base64 string as stored by the relayer
69
+ */
70
+ export declare function decryptBlindMailboxNote(walletSecretKey: Uint8Array, ephemeralPublicKey: Uint8Array, encryptedBlob: string): DecryptedNote;
@@ -0,0 +1,154 @@
1
+ import nacl from "tweetnacl";
2
+ import { randomBytes } from "../random.js";
3
+ // -- Internal crypto helpers (no extra dependencies) --------------------------
4
+ /** Derive 32-byte encryption key from a shared secret using SHA-512 truncated.
5
+ * Must match the production "sha256" helper which uses nacl.hash (SHA-512) [0:32].
6
+ *
7
+ * DO NOT "fix" this to real SHA-256. The name in the sibling implementations
8
+ * (`sha256ForNotes`) is a misnomer, but the bytes are load-bearing: changing the
9
+ * KDF makes every historical note undecryptable.
10
+ *
11
+ * @internal Exported for `compactNote.ts`. Not part of the stable public API. */
12
+ export function kdfSHA512_256(secret) {
13
+ return nacl.hash(secret).slice(0, 32);
14
+ }
15
+ /** Convert an Ed25519 private seed (32 bytes) to an X25519 scalar.
16
+ * Equivalent to ed2curve.convertSecretKey(seed).
17
+ *
18
+ * @internal Exported for `compactNote.ts`. Not part of the stable public API. */
19
+ export function ed25519SeedToX25519Private(seed) {
20
+ const h = nacl.hash(seed); // SHA-512
21
+ const scalar = h.slice(0, 32);
22
+ scalar[0] &= 248;
23
+ scalar[31] &= 127;
24
+ scalar[31] |= 64;
25
+ return scalar;
26
+ }
27
+ const P25519 = (1n << 255n) - 19n;
28
+ function modpow25519(b, e) {
29
+ let r = 1n;
30
+ b = b % P25519;
31
+ while (e > 0n) {
32
+ if (e & 1n)
33
+ r = (r * b) % P25519;
34
+ e >>= 1n;
35
+ b = (b * b) % P25519;
36
+ }
37
+ return r;
38
+ }
39
+ /** Convert an Ed25519 compressed public key (32 bytes) to X25519.
40
+ * Equivalent to ed2curve.convertPublicKey(pubkey).
41
+ * Edwards y-coord → Montgomery u via: u = (1 + y) / (1 - y) (mod p).
42
+ *
43
+ * @internal Exported for `compactNote.ts`. Not part of the stable public API. */
44
+ export function ed25519PublicKeyToX25519(pubkey) {
45
+ const pk = new Uint8Array(pubkey);
46
+ pk[31] &= 0x7f; // clear sign bit to get y as LE integer
47
+ // Decode little-endian y
48
+ let y = 0n;
49
+ for (let i = 31; i >= 0; i--)
50
+ y = (y << 8n) | BigInt(pk[i]);
51
+ // u = (1 + y) / (1 - y) mod p
52
+ const num = (1n + y) % P25519;
53
+ const den = (((1n - y) % P25519) + P25519) % P25519;
54
+ const u = (num * modpow25519(den, P25519 - 2n)) % P25519;
55
+ // Encode as 32-byte LE
56
+ const out = new Uint8Array(32);
57
+ let v = u;
58
+ for (let i = 0; i < 32; i++) {
59
+ out[i] = Number(v & 0xffn);
60
+ v >>= 8n;
61
+ }
62
+ return out;
63
+ }
64
+ // -- Public API ---------------------------------------------------------------
65
+ /**
66
+ * Encrypt a UTXO note for a recipient identified by their Solana wallet public key.
67
+ * Produces a blob the relayer will store and serve via POST /notes/save.
68
+ *
69
+ * @param recipientWalletPubkey 32-byte Ed25519 public key (wallet.publicKey.toBytes())
70
+ * @param noteData UTXO details to encrypt
71
+ */
72
+ export function encryptBlindMailboxNote(recipientWalletPubkey, noteData) {
73
+ // 1. Generate ephemeral Ed25519 keypair seed
74
+ const ephemeralSeed = randomBytes(32);
75
+ // Ed25519 public key stored by relayer; dapp recovers X25519 via ed2curve.convertPublicKey()
76
+ const ephemeralEdPublicKey = nacl.sign.keyPair.fromSeed(new Uint8Array(ephemeralSeed)).publicKey;
77
+ // X25519 scalar (ed2curve.convertSecretKey equivalent)
78
+ const ephemeralX25519Private = ed25519SeedToX25519Private(new Uint8Array(ephemeralSeed));
79
+ // X25519 public key (ed2curve.convertPublicKey equivalent)
80
+ const recipientX25519Public = ed25519PublicKeyToX25519(recipientWalletPubkey);
81
+ const sharedSecret = nacl.scalarMult(ephemeralX25519Private, recipientX25519Public);
82
+ const key = kdfSHA512_256(sharedSecret);
83
+ // JSON format matches production relayer notes schema
84
+ const hexArr = (b) => Array.from(b)
85
+ .map((x) => x.toString(16).padStart(2, "0"))
86
+ .join("");
87
+ const noteJson = JSON.stringify({
88
+ blinding: hexArr(noteData.blinding),
89
+ leafIndex: noteData.leafIndex,
90
+ commitment: hexArr(noteData.commitment),
91
+ amount: noteData.amount.toString(),
92
+ mintAddress: noteData.mintAddress,
93
+ timestamp: Date.now(),
94
+ treeId: noteData.treeId,
95
+ });
96
+ const nonce = nacl.randomBytes(24);
97
+ const plaintext = new TextEncoder().encode(noteJson);
98
+ const encrypted = nacl.secretbox(plaintext, nonce, key);
99
+ const combined = new Uint8Array(nonce.length + encrypted.length);
100
+ combined.set(nonce);
101
+ combined.set(encrypted, nonce.length);
102
+ const encryptedBlob = Buffer.from(combined).toString("base64");
103
+ // Return Ed25519 public key (dapp calls ed2curve.convertPublicKey() to recover X25519)
104
+ return { ephemeralPublicKey: ephemeralEdPublicKey, encryptedBlob };
105
+ }
106
+ /**
107
+ * Decrypt a Blind Mailbox note using the recipient's Solana wallet private seed.
108
+ *
109
+ * @param walletSecretKey Full 64-byte wallet.secretKey — the first 32 bytes are the seed
110
+ * @param ephemeralPublicKey 32-byte raw public key stored alongside the encrypted blob.
111
+ * This may be either an Ed25519 pubkey (from Keypair.generate())
112
+ * or a raw X25519 pubkey — the decryption path handles both:
113
+ * if it is an Ed25519 key it is converted; otherwise used directly.
114
+ * @param encryptedBlob Base64 string as stored by the relayer
115
+ */
116
+ export function decryptBlindMailboxNote(walletSecretKey, ephemeralPublicKey, encryptedBlob) {
117
+ // 1. Derive recipient X25519 from Ed25519 seed
118
+ const seed = walletSecretKey.slice(0, 32);
119
+ const myX25519Private = ed25519SeedToX25519Private(seed);
120
+ // ephemeralPublicKey may be an Ed25519 pubkey (dapp's Keypair.generate()) or raw X25519;
121
+ // try Ed25519→X25519 conversion first, fall back to using it directly.
122
+ let theirPublic;
123
+ try {
124
+ theirPublic = ed25519PublicKeyToX25519(ephemeralPublicKey);
125
+ }
126
+ catch {
127
+ theirPublic = ephemeralPublicKey; // already X25519
128
+ }
129
+ const sharedSecret = nacl.scalarMult(myX25519Private, theirPublic);
130
+ const key = kdfSHA512_256(sharedSecret);
131
+ const combined = Buffer.from(encryptedBlob, "base64");
132
+ const nonce = combined.slice(0, 24);
133
+ const ciphertext = combined.slice(24);
134
+ const decrypted = nacl.secretbox.open(new Uint8Array(ciphertext), new Uint8Array(nonce), key);
135
+ if (!decrypted) {
136
+ throw new Error("decryptBlindMailboxNote: decryption failed — wrong key or corrupted blob");
137
+ }
138
+ const parsed = JSON.parse(new TextDecoder().decode(decrypted));
139
+ const hexToBytes = (hex) => {
140
+ const out = new Uint8Array(hex.length / 2);
141
+ for (let i = 0; i < hex.length; i += 2)
142
+ out[i / 2] = parseInt(hex.slice(i, i + 2), 16);
143
+ return out;
144
+ };
145
+ return {
146
+ blinding: hexToBytes(parsed.blinding),
147
+ leafIndex: parsed.leafIndex,
148
+ commitment: hexToBytes(parsed.commitment),
149
+ amount: BigInt(parsed.amount),
150
+ timestamp: parsed.timestamp ?? 0,
151
+ mintAddress: parsed.mintAddress,
152
+ treeId: parsed.treeId,
153
+ };
154
+ }
@@ -0,0 +1,91 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ /**
3
+ * Keypair for UTXO ownership.
4
+ * Public key is derived via Poseidon: pubkey = Poseidon(privateKey)
5
+ */
6
+ export type Keypair = {
7
+ privateKey: bigint;
8
+ publicKey: bigint;
9
+ };
10
+ /**
11
+ * UTXO structure matching the circuit's UTXOCommitment template.
12
+ * commitment = Poseidon(amount, pubkey, blinding, mintAddress)
13
+ */
14
+ export type UTXO = {
15
+ /** Amount in lamports/token units */
16
+ amount: bigint;
17
+ /** Owner's public key (derived from private key via Poseidon) */
18
+ pubkey: bigint;
19
+ /** Random blinding factor for commitment privacy */
20
+ blinding: bigint;
21
+ /** Token mint address as field element */
22
+ mintAddress: bigint;
23
+ };
24
+ /**
25
+ * Serialized UTXO with precomputed commitment and optional ownership data.
26
+ */
27
+ export type SerializedUTXO = UTXO & {
28
+ /** 32-byte commitment: Poseidon(amount, pubkey, blinding, mintAddress) */
29
+ commitment: Uint8Array;
30
+ /** Private key for spending (only present for owned UTXOs) */
31
+ privateKey?: bigint;
32
+ /** Leaf index in the Merkle tree (set after insertion) */
33
+ pathIndex?: number;
34
+ };
35
+ /**
36
+ * Input UTXO with Merkle proof data for spending.
37
+ */
38
+ export type InputUTXO = SerializedUTXO & {
39
+ /** Leaf index in the Merkle tree */
40
+ pathIndex: number;
41
+ /** Merkle path elements (sibling hashes from leaf to root) */
42
+ pathElements: Uint8Array[];
43
+ /** Private key for nullifier derivation */
44
+ privateKey: bigint;
45
+ };
46
+ /**
47
+ * Generate a random keypair.
48
+ * Private key is a random 32-byte scalar.
49
+ * Public key is derived via Poseidon: pubkey = Poseidon(privateKey)
50
+ */
51
+ export declare function generateKeypair(): Keypair;
52
+ /**
53
+ * Derive public key from private key.
54
+ * Matches circuit's Keypair template: publicKey = Poseidon(privateKey)
55
+ */
56
+ export declare function derivePublicKey(privateKey: bigint): bigint;
57
+ /**
58
+ * Create keypair from a known private key.
59
+ */
60
+ export declare function keypairFromPrivateKey(privateKey: bigint): Keypair;
61
+ /**
62
+ * Compute UTXO commitment.
63
+ * Matches circuit's UTXOCommitment template:
64
+ * commitment = Poseidon(amount, pubkey, blinding, mintAddress)
65
+ */
66
+ export declare function commitUTXO(utxo: UTXO): Uint8Array;
67
+ /**
68
+ * Create a random UTXO with the given parameters.
69
+ */
70
+ export declare function createUTXO(params: {
71
+ amount: bigint;
72
+ pubkey: bigint;
73
+ mintAddress: PublicKey;
74
+ }): SerializedUTXO;
75
+ /**
76
+ * Create a UTXO with a specified private key (for owned UTXOs).
77
+ */
78
+ export declare function createOwnedUTXO(params: {
79
+ amount: bigint;
80
+ privateKey: bigint;
81
+ mintAddress: PublicKey;
82
+ }): SerializedUTXO;
83
+ /**
84
+ * Create a zero UTXO (for deposits where no input exists).
85
+ * Zero UTXOs have amount=0 and are used as placeholders in 2-in-2-out transactions.
86
+ */
87
+ export declare function createZeroUTXO(pubkey: bigint, mintAddress: PublicKey): SerializedUTXO;
88
+ /**
89
+ * Create a zero UTXO with private key (for owned zero UTXOs).
90
+ */
91
+ export declare function createOwnedZeroUTXO(privateKey: bigint, mintAddress: PublicKey): SerializedUTXO;