@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
@@ -1,13 +1,17 @@
1
- export * from "./client";
2
- export * from "./utxo";
3
- export * from "./config";
4
- export * from "./merkle";
5
- export * from "./proof";
6
- export * from "./events";
7
- export { createVeiloProgram, PRIVACY_POOL_IDL, PRIVACY_POOL_PROGRAM_ID, } from "./program";
8
- export { createTransactionProver, verifyProof } from "./prover";
9
- export type { CircuitArtifacts } from "./prover";
10
- export * from "./relayer";
11
- export { withRpcRetry, DEFAULT_RPC_RETRY } from "./retry";
12
- export type { RpcRetryPolicy } from "./retry";
13
- export { initPoseidon, getPoseidon, BN254_FR_MODULUS, bytesToBigIntBE, bigIntToBytesBE, frToBigInt, poseidon1, poseidon2, poseidon3, poseidon4, pubkeyToField, } from "./poseidon";
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/cjs/index.js CHANGED
@@ -14,44 +14,57 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.pubkeyToField = exports.poseidon4 = exports.poseidon3 = exports.poseidon2 = exports.poseidon1 = exports.frToBigInt = exports.bigIntToBytesBE = exports.bytesToBigIntBE = exports.BN254_FR_MODULUS = exports.getPoseidon = exports.initPoseidon = exports.DEFAULT_RPC_RETRY = exports.withRpcRetry = exports.verifyProof = exports.createTransactionProver = exports.PRIVACY_POOL_PROGRAM_ID = exports.PRIVACY_POOL_IDL = exports.createVeiloProgram = void 0;
17
+ exports.pubkeyToField = exports.poseidon4 = exports.poseidon3 = exports.poseidon2 = exports.poseidon1 = exports.frToBigInt = exports.bigIntToBytesBE = exports.bytesToBigIntBE = exports.BN254_FR_MODULUS = exports.getPoseidon = exports.initPoseidon = exports.DEFAULT_RPC_RETRY = exports.withRpcRetry = exports.randomBytes32 = exports.randomBytes = exports.verifyProof = exports.createSwapProver = exports.createTransactionProver = exports.PRIVACY_POOL_PROGRAM_ID = exports.PRIVACY_POOL_IDL = exports.createReadonlyVeiloProgram = exports.createVeiloProgram = void 0;
18
18
  // Core client functions
19
- __exportStar(require("./client"), exports);
19
+ __exportStar(require("./client.js"), exports);
20
20
  // UTXO types and functions (replaces note.ts)
21
- __exportStar(require("./utxo"), exports);
21
+ __exportStar(require("./utxo.js"), exports);
22
22
  // Configuration constants and types
23
- __exportStar(require("./config"), exports);
23
+ __exportStar(require("./config.js"), exports);
24
24
  // Merkle tree implementation
25
- __exportStar(require("./merkle"), exports);
25
+ __exportStar(require("./merkle.js"), exports);
26
26
  // Proof types and helpers
27
- __exportStar(require("./proof"), exports);
27
+ __exportStar(require("./proof.js"), exports);
28
28
  // On-chain event scanning and tree reconstruction
29
- __exportStar(require("./events"), exports);
29
+ __exportStar(require("./events.js"), exports);
30
30
  // Program factory and IDL
31
- var program_1 = require("./program");
32
- Object.defineProperty(exports, "createVeiloProgram", { enumerable: true, get: function () { return program_1.createVeiloProgram; } });
33
- Object.defineProperty(exports, "PRIVACY_POOL_IDL", { enumerable: true, get: function () { return program_1.PRIVACY_POOL_IDL; } });
34
- Object.defineProperty(exports, "PRIVACY_POOL_PROGRAM_ID", { enumerable: true, get: function () { return program_1.PRIVACY_POOL_PROGRAM_ID; } });
31
+ var program_js_1 = require("./program.js");
32
+ Object.defineProperty(exports, "createVeiloProgram", { enumerable: true, get: function () { return program_js_1.createVeiloProgram; } });
33
+ Object.defineProperty(exports, "createReadonlyVeiloProgram", { enumerable: true, get: function () { return program_js_1.createReadonlyVeiloProgram; } });
34
+ Object.defineProperty(exports, "PRIVACY_POOL_IDL", { enumerable: true, get: function () { return program_js_1.PRIVACY_POOL_IDL; } });
35
+ Object.defineProperty(exports, "PRIVACY_POOL_PROGRAM_ID", { enumerable: true, get: function () { return program_js_1.PRIVACY_POOL_PROGRAM_ID; } });
36
+ // Wallet-agnostic shielding — build an unsigned deposit for any external wallet
37
+ __exportStar(require("./shield/index.js"), exports);
35
38
  // Proof generation (requires snarkjs peer dependency)
36
- var prover_1 = require("./prover");
37
- Object.defineProperty(exports, "createTransactionProver", { enumerable: true, get: function () { return prover_1.createTransactionProver; } });
38
- Object.defineProperty(exports, "verifyProof", { enumerable: true, get: function () { return prover_1.verifyProof; } });
39
+ var prover_js_1 = require("./prover.js");
40
+ Object.defineProperty(exports, "createTransactionProver", { enumerable: true, get: function () { return prover_js_1.createTransactionProver; } });
41
+ Object.defineProperty(exports, "createSwapProver", { enumerable: true, get: function () { return prover_js_1.createSwapProver; } });
42
+ Object.defineProperty(exports, "verifyProof", { enumerable: true, get: function () { return prover_js_1.verifyProof; } });
43
+ // Compact note ciphers — the on-chain payload that makes notes recoverable
44
+ // from chain data alone, without any relayer involvement.
45
+ __exportStar(require("./compactNote.js"), exports);
46
+ // Cross-platform CSPRNG (no node:crypto import — safe in browser bundles)
47
+ var random_js_1 = require("./random.js");
48
+ Object.defineProperty(exports, "randomBytes", { enumerable: true, get: function () { return random_js_1.randomBytes; } });
49
+ Object.defineProperty(exports, "randomBytes32", { enumerable: true, get: function () { return random_js_1.randomBytes32; } });
39
50
  // Relayer API client
40
- __exportStar(require("./relayer"), exports);
51
+ __exportStar(require("./relayer.js"), exports);
52
+ // Veilo Cloak Partner API client and helpers
53
+ __exportStar(require("./cloak.js"), exports);
41
54
  // RPC retry / resilience
42
- var retry_1 = require("./retry");
43
- Object.defineProperty(exports, "withRpcRetry", { enumerable: true, get: function () { return retry_1.withRpcRetry; } });
44
- Object.defineProperty(exports, "DEFAULT_RPC_RETRY", { enumerable: true, get: function () { return retry_1.DEFAULT_RPC_RETRY; } });
55
+ var retry_js_1 = require("./retry.js");
56
+ Object.defineProperty(exports, "withRpcRetry", { enumerable: true, get: function () { return retry_js_1.withRpcRetry; } });
57
+ Object.defineProperty(exports, "DEFAULT_RPC_RETRY", { enumerable: true, get: function () { return retry_js_1.DEFAULT_RPC_RETRY; } });
45
58
  // Poseidon hash functions
46
- var poseidon_1 = require("./poseidon");
47
- Object.defineProperty(exports, "initPoseidon", { enumerable: true, get: function () { return poseidon_1.initPoseidon; } });
48
- Object.defineProperty(exports, "getPoseidon", { enumerable: true, get: function () { return poseidon_1.getPoseidon; } });
49
- Object.defineProperty(exports, "BN254_FR_MODULUS", { enumerable: true, get: function () { return poseidon_1.BN254_FR_MODULUS; } });
50
- Object.defineProperty(exports, "bytesToBigIntBE", { enumerable: true, get: function () { return poseidon_1.bytesToBigIntBE; } });
51
- Object.defineProperty(exports, "bigIntToBytesBE", { enumerable: true, get: function () { return poseidon_1.bigIntToBytesBE; } });
52
- Object.defineProperty(exports, "frToBigInt", { enumerable: true, get: function () { return poseidon_1.frToBigInt; } });
53
- Object.defineProperty(exports, "poseidon1", { enumerable: true, get: function () { return poseidon_1.poseidon1; } });
54
- Object.defineProperty(exports, "poseidon2", { enumerable: true, get: function () { return poseidon_1.poseidon2; } });
55
- Object.defineProperty(exports, "poseidon3", { enumerable: true, get: function () { return poseidon_1.poseidon3; } });
56
- Object.defineProperty(exports, "poseidon4", { enumerable: true, get: function () { return poseidon_1.poseidon4; } });
57
- Object.defineProperty(exports, "pubkeyToField", { enumerable: true, get: function () { return poseidon_1.pubkeyToField; } });
59
+ var poseidon_js_1 = require("./poseidon.js");
60
+ Object.defineProperty(exports, "initPoseidon", { enumerable: true, get: function () { return poseidon_js_1.initPoseidon; } });
61
+ Object.defineProperty(exports, "getPoseidon", { enumerable: true, get: function () { return poseidon_js_1.getPoseidon; } });
62
+ Object.defineProperty(exports, "BN254_FR_MODULUS", { enumerable: true, get: function () { return poseidon_js_1.BN254_FR_MODULUS; } });
63
+ Object.defineProperty(exports, "bytesToBigIntBE", { enumerable: true, get: function () { return poseidon_js_1.bytesToBigIntBE; } });
64
+ Object.defineProperty(exports, "bigIntToBytesBE", { enumerable: true, get: function () { return poseidon_js_1.bigIntToBytesBE; } });
65
+ Object.defineProperty(exports, "frToBigInt", { enumerable: true, get: function () { return poseidon_js_1.frToBigInt; } });
66
+ Object.defineProperty(exports, "poseidon1", { enumerable: true, get: function () { return poseidon_js_1.poseidon1; } });
67
+ Object.defineProperty(exports, "poseidon2", { enumerable: true, get: function () { return poseidon_js_1.poseidon2; } });
68
+ Object.defineProperty(exports, "poseidon3", { enumerable: true, get: function () { return poseidon_js_1.poseidon3; } });
69
+ Object.defineProperty(exports, "poseidon4", { enumerable: true, get: function () { return poseidon_js_1.poseidon4; } });
70
+ Object.defineProperty(exports, "pubkeyToField", { enumerable: true, get: function () { return poseidon_js_1.pubkeyToField; } });
@@ -62,3 +62,16 @@ export declare class MerkleTree {
62
62
  */
63
63
  getCircuitPath(index: number): CircuitMerklePath;
64
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,18 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MerkleTree = void 0;
4
- const poseidon_1 = require("./poseidon");
4
+ exports.poseidonZeroChain = poseidonZeroChain;
5
+ const poseidon_js_1 = require("./poseidon.js");
5
6
  // Poseidon-based hash of two 32-byte field elements.
6
7
  function hashPair(left, right) {
7
8
  if (left.length !== 32 || right.length !== 32) {
8
9
  throw new Error("hashPair expects 32-byte inputs");
9
10
  }
10
- const poseidon = (0, poseidon_1.getPoseidon)();
11
- const lf = (0, poseidon_1.bytesToBigIntBE)(left) % poseidon_1.BN254_FR_MODULUS;
12
- const rf = (0, poseidon_1.bytesToBigIntBE)(right) % poseidon_1.BN254_FR_MODULUS;
11
+ const poseidon = (0, poseidon_js_1.getPoseidon)();
12
+ const lf = (0, poseidon_js_1.bytesToBigIntBE)(left) % poseidon_js_1.BN254_FR_MODULUS;
13
+ const rf = (0, poseidon_js_1.bytesToBigIntBE)(right) % poseidon_js_1.BN254_FR_MODULUS;
13
14
  const fe = poseidon([lf, rf]);
14
- const outBig = (0, poseidon_1.frToBigInt)(fe) % poseidon_1.BN254_FR_MODULUS;
15
- return (0, poseidon_1.bigIntToBytesBE)(outBig);
15
+ const outBig = (0, poseidon_js_1.frToBigInt)(fe) % poseidon_js_1.BN254_FR_MODULUS;
16
+ return (0, poseidon_js_1.bigIntToBytesBE)(outBig);
16
17
  }
17
18
  /**
18
19
  * Fixed-depth binary Merkle tree over BN254 Fr:
@@ -32,7 +33,7 @@ class MerkleTree {
32
33
  this.zeroes = [];
33
34
  this.layers = [];
34
35
  // zero[0] = 0 (Fr), then zero[i+1] = Poseidon(zero[i], zero[i])
35
- let zero = (0, poseidon_1.bigIntToBytesBE)(0n);
36
+ let zero = (0, poseidon_js_1.bigIntToBytesBE)(0n);
36
37
  this.zeroes.push(zero);
37
38
  for (let level = 1; level <= depth; level++) {
38
39
  zero = hashPair(zero, zero);
@@ -126,8 +127,30 @@ class MerkleTree {
126
127
  const merklePath = this.getPath(index);
127
128
  return {
128
129
  pathIndex: index,
129
- pathElements: merklePath.path.map((p) => (0, poseidon_1.bytesToBigIntBE)(p) % poseidon_1.BN254_FR_MODULUS),
130
+ pathElements: merklePath.path.map((p) => (0, poseidon_js_1.bytesToBigIntBE)(p) % poseidon_js_1.BN254_FR_MODULUS),
130
131
  };
131
132
  }
132
133
  }
133
134
  exports.MerkleTree = MerkleTree;
135
+ /**
136
+ * The Poseidon zero chain: `z[0] = 0`, `z[i+1] = Poseidon(z[i], z[i])`.
137
+ *
138
+ * This is the `zeroes` array a `MerkleTree` builds in its constructor, without
139
+ * the tree. Deposits need a Merkle path for their dummy inputs, but the circuit
140
+ * disables the path check when a input amount is zero (`enabled <== 1 - IsZero(amount)`
141
+ * in transaction.circom), so the values are never constrained — only their
142
+ * count is.
143
+ *
144
+ * Allocating a full depth-22 `MerkleTree` for this costs ~130 MB and several
145
+ * seconds. This costs 22 hashes. Any deposit-shaped transaction should use it.
146
+ */
147
+ function poseidonZeroChain(depth = 22) {
148
+ const zeroes = [];
149
+ let zero = (0, poseidon_js_1.bigIntToBytesBE)(0n);
150
+ zeroes.push(zero);
151
+ for (let level = 1; level <= depth; level++) {
152
+ zero = hashPair(zero, zero);
153
+ zeroes.push(zero);
154
+ }
155
+ return zeroes;
156
+ }
@@ -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,75 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.deriveEncryptionKeypair = deriveEncryptionKeypair;
7
+ exports.encryptUTXONote = encryptUTXONote;
8
+ exports.decryptUTXONote = decryptUTXONote;
9
+ const sha256_1 = require("@noble/hashes/sha256");
10
+ const tweetnacl_1 = __importDefault(require("tweetnacl"));
11
+ const poseidon_js_1 = require("../poseidon.js");
12
+ const model_js_1 = require("./model.js");
13
+ /**
14
+ * Derive a NaCl X25519 encryption keypair from a UTXO private key.
15
+ * The keypair is deterministic: same privateKey always yields the same pair.
16
+ */
17
+ function deriveEncryptionKeypair(privateKey) {
18
+ const seed = (0, sha256_1.sha256)((0, poseidon_js_1.bigIntToBytesBE)(privateKey));
19
+ return tweetnacl_1.default.box.keyPair.fromSecretKey(seed);
20
+ }
21
+ /**
22
+ * Encrypt a UTXO so the recipient can recover it from on-chain events.
23
+ *
24
+ * Plaintext layout (72 bytes):
25
+ * [0..8] amount — 8-byte little-endian u64
26
+ * [8..40] blinding — 32-byte big-endian field element
27
+ * [40..72] privateKey — 32-byte big-endian field element
28
+ *
29
+ * Uses ephemeral-key NaCl box so the sender key is single-use.
30
+ */
31
+ function encryptUTXONote(utxo, recipientEncPubkey) {
32
+ const nonce = tweetnacl_1.default.randomBytes(tweetnacl_1.default.box.nonceLength); // 24 bytes
33
+ const ephemeral = tweetnacl_1.default.box.keyPair();
34
+ const amountBuf = new Uint8Array(8);
35
+ let v = utxo.amount;
36
+ for (let i = 0; i < 8; i++) {
37
+ amountBuf[i] = Number(v & 0xffn);
38
+ v >>= 8n;
39
+ }
40
+ const plaintext = new Uint8Array(72);
41
+ plaintext.set(amountBuf, 0);
42
+ plaintext.set((0, poseidon_js_1.bigIntToBytesBE)(utxo.blinding), 8);
43
+ plaintext.set((0, poseidon_js_1.bigIntToBytesBE)(utxo.privateKey), 40);
44
+ const sharedKey = tweetnacl_1.default.box.before(recipientEncPubkey, ephemeral.secretKey);
45
+ const ciphertext = tweetnacl_1.default.box.after(plaintext, nonce, sharedKey);
46
+ return { nonce, ciphertext, senderEphemeralPubkey: ephemeral.publicKey };
47
+ }
48
+ /**
49
+ * Try to decrypt an encrypted UTXO note using the recipient's private key.
50
+ * Returns the SerializedUTXO if decryption succeeds and the commitment
51
+ * matches, or `null` if the note was not intended for this key.
52
+ */
53
+ function decryptUTXONote(note, recipientPrivateKey, commitment, mintAddress) {
54
+ const { secretKey } = deriveEncryptionKeypair(recipientPrivateKey);
55
+ const sharedKey = tweetnacl_1.default.box.before(note.senderEphemeralPubkey, secretKey);
56
+ const plaintext = tweetnacl_1.default.box.open.after(note.ciphertext, note.nonce, sharedKey);
57
+ if (!plaintext || plaintext.length < 72)
58
+ return null;
59
+ let amount = 0n;
60
+ for (let i = 7; i >= 0; i--)
61
+ amount = (amount << 8n) | BigInt(plaintext[i]);
62
+ const blinding = (0, poseidon_js_1.bytesToBigIntBE)(plaintext.slice(8, 40)) % poseidon_js_1.BN254_FR_MODULUS;
63
+ const privateKey = (0, poseidon_js_1.bytesToBigIntBE)(plaintext.slice(40, 72)) % poseidon_js_1.BN254_FR_MODULUS;
64
+ const pubkey = (0, model_js_1.derivePublicKey)(privateKey);
65
+ const utxo = {
66
+ amount,
67
+ pubkey,
68
+ blinding,
69
+ mintAddress: (0, poseidon_js_1.pubkeyToField)(mintAddress),
70
+ };
71
+ const expectedCommitment = (0, model_js_1.commitUTXO)(utxo);
72
+ if (!expectedCommitment.every((b, i) => b === commitment[i]))
73
+ return null;
74
+ return { ...utxo, commitment, privateKey };
75
+ }
@@ -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,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("./model.js"), exports);
18
+ __exportStar(require("./nullifier.js"), exports);
19
+ __exportStar(require("./encryption.js"), exports);
20
+ __exportStar(require("./mailbox.js"), exports);
21
+ __exportStar(require("./recovery.js"), exports);
@@ -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,164 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.kdfSHA512_256 = kdfSHA512_256;
7
+ exports.ed25519SeedToX25519Private = ed25519SeedToX25519Private;
8
+ exports.ed25519PublicKeyToX25519 = ed25519PublicKeyToX25519;
9
+ exports.encryptBlindMailboxNote = encryptBlindMailboxNote;
10
+ exports.decryptBlindMailboxNote = decryptBlindMailboxNote;
11
+ const tweetnacl_1 = __importDefault(require("tweetnacl"));
12
+ const random_js_1 = require("../random.js");
13
+ // -- Internal crypto helpers (no extra dependencies) --------------------------
14
+ /** Derive 32-byte encryption key from a shared secret using SHA-512 truncated.
15
+ * Must match the production "sha256" helper which uses nacl.hash (SHA-512) [0:32].
16
+ *
17
+ * DO NOT "fix" this to real SHA-256. The name in the sibling implementations
18
+ * (`sha256ForNotes`) is a misnomer, but the bytes are load-bearing: changing the
19
+ * KDF makes every historical note undecryptable.
20
+ *
21
+ * @internal Exported for `compactNote.ts`. Not part of the stable public API. */
22
+ function kdfSHA512_256(secret) {
23
+ return tweetnacl_1.default.hash(secret).slice(0, 32);
24
+ }
25
+ /** Convert an Ed25519 private seed (32 bytes) to an X25519 scalar.
26
+ * Equivalent to ed2curve.convertSecretKey(seed).
27
+ *
28
+ * @internal Exported for `compactNote.ts`. Not part of the stable public API. */
29
+ function ed25519SeedToX25519Private(seed) {
30
+ const h = tweetnacl_1.default.hash(seed); // SHA-512
31
+ const scalar = h.slice(0, 32);
32
+ scalar[0] &= 248;
33
+ scalar[31] &= 127;
34
+ scalar[31] |= 64;
35
+ return scalar;
36
+ }
37
+ const P25519 = (1n << 255n) - 19n;
38
+ function modpow25519(b, e) {
39
+ let r = 1n;
40
+ b = b % P25519;
41
+ while (e > 0n) {
42
+ if (e & 1n)
43
+ r = (r * b) % P25519;
44
+ e >>= 1n;
45
+ b = (b * b) % P25519;
46
+ }
47
+ return r;
48
+ }
49
+ /** Convert an Ed25519 compressed public key (32 bytes) to X25519.
50
+ * Equivalent to ed2curve.convertPublicKey(pubkey).
51
+ * Edwards y-coord → Montgomery u via: u = (1 + y) / (1 - y) (mod p).
52
+ *
53
+ * @internal Exported for `compactNote.ts`. Not part of the stable public API. */
54
+ function ed25519PublicKeyToX25519(pubkey) {
55
+ const pk = new Uint8Array(pubkey);
56
+ pk[31] &= 0x7f; // clear sign bit to get y as LE integer
57
+ // Decode little-endian y
58
+ let y = 0n;
59
+ for (let i = 31; i >= 0; i--)
60
+ y = (y << 8n) | BigInt(pk[i]);
61
+ // u = (1 + y) / (1 - y) mod p
62
+ const num = (1n + y) % P25519;
63
+ const den = (((1n - y) % P25519) + P25519) % P25519;
64
+ const u = (num * modpow25519(den, P25519 - 2n)) % P25519;
65
+ // Encode as 32-byte LE
66
+ const out = new Uint8Array(32);
67
+ let v = u;
68
+ for (let i = 0; i < 32; i++) {
69
+ out[i] = Number(v & 0xffn);
70
+ v >>= 8n;
71
+ }
72
+ return out;
73
+ }
74
+ // -- Public API ---------------------------------------------------------------
75
+ /**
76
+ * Encrypt a UTXO note for a recipient identified by their Solana wallet public key.
77
+ * Produces a blob the relayer will store and serve via POST /notes/save.
78
+ *
79
+ * @param recipientWalletPubkey 32-byte Ed25519 public key (wallet.publicKey.toBytes())
80
+ * @param noteData UTXO details to encrypt
81
+ */
82
+ function encryptBlindMailboxNote(recipientWalletPubkey, noteData) {
83
+ // 1. Generate ephemeral Ed25519 keypair seed
84
+ const ephemeralSeed = (0, random_js_1.randomBytes)(32);
85
+ // Ed25519 public key stored by relayer; dapp recovers X25519 via ed2curve.convertPublicKey()
86
+ const ephemeralEdPublicKey = tweetnacl_1.default.sign.keyPair.fromSeed(new Uint8Array(ephemeralSeed)).publicKey;
87
+ // X25519 scalar (ed2curve.convertSecretKey equivalent)
88
+ const ephemeralX25519Private = ed25519SeedToX25519Private(new Uint8Array(ephemeralSeed));
89
+ // X25519 public key (ed2curve.convertPublicKey equivalent)
90
+ const recipientX25519Public = ed25519PublicKeyToX25519(recipientWalletPubkey);
91
+ const sharedSecret = tweetnacl_1.default.scalarMult(ephemeralX25519Private, recipientX25519Public);
92
+ const key = kdfSHA512_256(sharedSecret);
93
+ // JSON format matches production relayer notes schema
94
+ const hexArr = (b) => Array.from(b)
95
+ .map((x) => x.toString(16).padStart(2, "0"))
96
+ .join("");
97
+ const noteJson = JSON.stringify({
98
+ blinding: hexArr(noteData.blinding),
99
+ leafIndex: noteData.leafIndex,
100
+ commitment: hexArr(noteData.commitment),
101
+ amount: noteData.amount.toString(),
102
+ mintAddress: noteData.mintAddress,
103
+ timestamp: Date.now(),
104
+ treeId: noteData.treeId,
105
+ });
106
+ const nonce = tweetnacl_1.default.randomBytes(24);
107
+ const plaintext = new TextEncoder().encode(noteJson);
108
+ const encrypted = tweetnacl_1.default.secretbox(plaintext, nonce, key);
109
+ const combined = new Uint8Array(nonce.length + encrypted.length);
110
+ combined.set(nonce);
111
+ combined.set(encrypted, nonce.length);
112
+ const encryptedBlob = Buffer.from(combined).toString("base64");
113
+ // Return Ed25519 public key (dapp calls ed2curve.convertPublicKey() to recover X25519)
114
+ return { ephemeralPublicKey: ephemeralEdPublicKey, encryptedBlob };
115
+ }
116
+ /**
117
+ * Decrypt a Blind Mailbox note using the recipient's Solana wallet private seed.
118
+ *
119
+ * @param walletSecretKey Full 64-byte wallet.secretKey — the first 32 bytes are the seed
120
+ * @param ephemeralPublicKey 32-byte raw public key stored alongside the encrypted blob.
121
+ * This may be either an Ed25519 pubkey (from Keypair.generate())
122
+ * or a raw X25519 pubkey — the decryption path handles both:
123
+ * if it is an Ed25519 key it is converted; otherwise used directly.
124
+ * @param encryptedBlob Base64 string as stored by the relayer
125
+ */
126
+ function decryptBlindMailboxNote(walletSecretKey, ephemeralPublicKey, encryptedBlob) {
127
+ // 1. Derive recipient X25519 from Ed25519 seed
128
+ const seed = walletSecretKey.slice(0, 32);
129
+ const myX25519Private = ed25519SeedToX25519Private(seed);
130
+ // ephemeralPublicKey may be an Ed25519 pubkey (dapp's Keypair.generate()) or raw X25519;
131
+ // try Ed25519→X25519 conversion first, fall back to using it directly.
132
+ let theirPublic;
133
+ try {
134
+ theirPublic = ed25519PublicKeyToX25519(ephemeralPublicKey);
135
+ }
136
+ catch {
137
+ theirPublic = ephemeralPublicKey; // already X25519
138
+ }
139
+ const sharedSecret = tweetnacl_1.default.scalarMult(myX25519Private, theirPublic);
140
+ const key = kdfSHA512_256(sharedSecret);
141
+ const combined = Buffer.from(encryptedBlob, "base64");
142
+ const nonce = combined.slice(0, 24);
143
+ const ciphertext = combined.slice(24);
144
+ const decrypted = tweetnacl_1.default.secretbox.open(new Uint8Array(ciphertext), new Uint8Array(nonce), key);
145
+ if (!decrypted) {
146
+ throw new Error("decryptBlindMailboxNote: decryption failed — wrong key or corrupted blob");
147
+ }
148
+ const parsed = JSON.parse(new TextDecoder().decode(decrypted));
149
+ const hexToBytes = (hex) => {
150
+ const out = new Uint8Array(hex.length / 2);
151
+ for (let i = 0; i < hex.length; i += 2)
152
+ out[i / 2] = parseInt(hex.slice(i, i + 2), 16);
153
+ return out;
154
+ };
155
+ return {
156
+ blinding: hexToBytes(parsed.blinding),
157
+ leafIndex: parsed.leafIndex,
158
+ commitment: hexToBytes(parsed.commitment),
159
+ amount: BigInt(parsed.amount),
160
+ timestamp: parsed.timestamp ?? 0,
161
+ mintAddress: parsed.mintAddress,
162
+ treeId: parsed.treeId,
163
+ };
164
+ }