@veilo/sdk-core 0.4.0 → 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 (296) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +286 -1298
  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 +6 -938
  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 +21 -0
  32. package/dist/cjs/compactNote.js +31 -7
  33. package/dist/cjs/index.d.ts +2 -1
  34. package/dist/cjs/index.js +4 -1
  35. package/dist/cjs/notes/encryption.d.ts +41 -0
  36. package/dist/cjs/notes/encryption.js +75 -0
  37. package/dist/cjs/notes/index.d.ts +5 -0
  38. package/dist/cjs/notes/index.js +21 -0
  39. package/dist/cjs/notes/mailbox.d.ts +70 -0
  40. package/dist/cjs/notes/mailbox.js +164 -0
  41. package/dist/cjs/notes/model.d.ts +91 -0
  42. package/dist/cjs/notes/model.js +109 -0
  43. package/dist/cjs/notes/nullifier.d.ts +27 -0
  44. package/dist/cjs/notes/nullifier.js +45 -0
  45. package/dist/cjs/notes/recovery.d.ts +10 -0
  46. package/dist/cjs/notes/recovery.js +31 -0
  47. package/dist/cjs/proof.d.ts +1 -183
  48. package/dist/cjs/proof.js +16 -290
  49. package/dist/cjs/proofs/encoding.d.ts +17 -0
  50. package/dist/cjs/proofs/encoding.js +72 -0
  51. package/dist/cjs/proofs/formatting.d.ts +6 -0
  52. package/dist/cjs/proofs/formatting.js +34 -0
  53. package/dist/cjs/proofs/index.d.ts +5 -0
  54. package/dist/cjs/proofs/index.js +21 -0
  55. package/dist/cjs/proofs/swap.d.ts +87 -0
  56. package/dist/cjs/proofs/swap.js +144 -0
  57. package/dist/cjs/proofs/transaction.d.ts +28 -0
  58. package/dist/cjs/proofs/transaction.js +110 -0
  59. package/dist/cjs/proofs/types.d.ts +70 -0
  60. package/dist/cjs/prover.d.ts +4 -1
  61. package/dist/cjs/prover.js +12 -2
  62. package/dist/cjs/relayer/client.d.ts +61 -0
  63. package/dist/cjs/relayer/client.js +151 -0
  64. package/dist/cjs/relayer/crypto.d.ts +5 -0
  65. package/dist/cjs/relayer/crypto.js +26 -0
  66. package/dist/cjs/relayer/encoding.d.ts +2 -0
  67. package/dist/cjs/relayer/encoding.js +23 -0
  68. package/dist/cjs/relayer/errors.d.ts +7 -0
  69. package/dist/cjs/relayer/errors.js +17 -0
  70. package/dist/cjs/relayer/index.d.ts +3 -0
  71. package/dist/cjs/relayer/index.js +19 -0
  72. package/dist/cjs/relayer/transport.d.ts +17 -0
  73. package/dist/cjs/relayer/transport.js +63 -0
  74. package/dist/{src/relayer.d.ts → cjs/relayer/types.d.ts} +44 -86
  75. package/dist/cjs/relayer/types.js +5 -0
  76. package/dist/cjs/relayer.d.ts +1 -318
  77. package/dist/cjs/relayer.js +15 -254
  78. package/dist/cjs/shield/alt.js +8 -8
  79. package/dist/cjs/shield/errors.js +2 -2
  80. package/dist/cjs/shield/finalize.js +2 -2
  81. package/dist/cjs/shield/ix.d.ts +1 -1
  82. package/dist/cjs/shield/owner.js +9 -8
  83. package/dist/cjs/shield/ports.js +3 -3
  84. package/dist/cjs/shield/preflight.js +3 -3
  85. package/dist/cjs/shield/shield.js +20 -19
  86. package/dist/cjs/shield/types.d.ts +1 -1
  87. package/dist/cjs/transactions/deposit.d.ts +94 -0
  88. package/dist/cjs/transactions/deposit.js +234 -0
  89. package/dist/cjs/transactions/index.d.ts +7 -0
  90. package/dist/cjs/transactions/index.js +26 -0
  91. package/dist/cjs/transactions/swap.d.ts +71 -0
  92. package/dist/cjs/transactions/swap.js +184 -0
  93. package/dist/cjs/transactions/transact.d.ts +34 -0
  94. package/dist/cjs/transactions/transact.js +146 -0
  95. package/dist/cjs/transactions/transfer.d.ts +51 -0
  96. package/dist/cjs/transactions/transfer.js +106 -0
  97. package/dist/cjs/transactions/withdraw.d.ts +52 -0
  98. package/dist/cjs/transactions/withdraw.js +104 -0
  99. package/dist/cjs/utxo.d.ts +1 -235
  100. package/dist/cjs/utxo.js +15 -404
  101. package/dist/esm/accounts/admin.d.ts +84 -0
  102. package/dist/esm/accounts/admin.js +165 -0
  103. package/dist/esm/accounts/errors.d.ts +6 -0
  104. package/dist/esm/accounts/errors.js +95 -0
  105. package/dist/esm/accounts/index.d.ts +4 -0
  106. package/dist/esm/accounts/index.js +4 -0
  107. package/dist/esm/accounts/pdas.d.ts +23 -0
  108. package/dist/esm/accounts/pdas.js +38 -0
  109. package/dist/esm/accounts/queries.d.ts +41 -0
  110. package/dist/esm/accounts/queries.js +88 -0
  111. package/dist/esm/client.d.ts +2 -407
  112. package/dist/esm/client.js +3 -891
  113. package/dist/esm/cloak/client.d.ts +28 -0
  114. package/dist/esm/cloak/client.js +64 -0
  115. package/dist/esm/cloak/errors.d.ts +13 -0
  116. package/dist/esm/cloak/errors.js +21 -0
  117. package/dist/esm/cloak/helpers.d.ts +6 -0
  118. package/dist/esm/cloak/helpers.js +31 -0
  119. package/dist/esm/cloak/index.d.ts +6 -0
  120. package/dist/esm/cloak/index.js +6 -0
  121. package/dist/esm/cloak/polling.d.ts +7 -0
  122. package/dist/esm/cloak/polling.js +63 -0
  123. package/dist/esm/cloak/transport.d.ts +15 -0
  124. package/dist/esm/cloak/transport.js +115 -0
  125. package/dist/esm/cloak/types.d.ts +188 -0
  126. package/dist/esm/cloak/types.js +4 -0
  127. package/dist/esm/cloak.d.ts +1 -0
  128. package/dist/esm/cloak.js +2 -0
  129. package/dist/esm/compactNote.d.ts +21 -0
  130. package/dist/esm/compactNote.js +24 -1
  131. package/dist/esm/index.d.ts +2 -1
  132. package/dist/esm/index.js +3 -1
  133. package/dist/esm/notes/encryption.d.ts +41 -0
  134. package/dist/esm/notes/encryption.js +67 -0
  135. package/dist/esm/notes/index.d.ts +5 -0
  136. package/dist/esm/notes/index.js +5 -0
  137. package/dist/esm/notes/mailbox.d.ts +70 -0
  138. package/dist/esm/notes/mailbox.js +154 -0
  139. package/dist/esm/notes/model.d.ts +91 -0
  140. package/dist/esm/notes/model.js +99 -0
  141. package/dist/esm/notes/nullifier.d.ts +27 -0
  142. package/dist/esm/notes/nullifier.js +40 -0
  143. package/dist/esm/notes/recovery.d.ts +10 -0
  144. package/dist/esm/notes/recovery.js +28 -0
  145. package/dist/esm/proof.d.ts +1 -183
  146. package/dist/esm/proof.js +2 -281
  147. package/dist/esm/proofs/encoding.d.ts +17 -0
  148. package/dist/esm/proofs/encoding.js +68 -0
  149. package/dist/esm/proofs/formatting.d.ts +6 -0
  150. package/dist/esm/proofs/formatting.js +31 -0
  151. package/dist/esm/proofs/index.d.ts +5 -0
  152. package/dist/esm/proofs/index.js +5 -0
  153. package/dist/esm/proofs/swap.d.ts +87 -0
  154. package/dist/esm/proofs/swap.js +138 -0
  155. package/dist/esm/proofs/transaction.d.ts +28 -0
  156. package/dist/esm/proofs/transaction.js +105 -0
  157. package/dist/esm/proofs/types.d.ts +70 -0
  158. package/dist/esm/proofs/types.js +1 -0
  159. package/dist/esm/prover.d.ts +4 -1
  160. package/dist/esm/prover.js +10 -1
  161. package/dist/esm/relayer/client.d.ts +61 -0
  162. package/dist/esm/relayer/client.js +144 -0
  163. package/dist/esm/relayer/crypto.d.ts +5 -0
  164. package/dist/esm/relayer/crypto.js +20 -0
  165. package/dist/esm/relayer/encoding.d.ts +2 -0
  166. package/dist/esm/relayer/encoding.js +19 -0
  167. package/dist/esm/relayer/errors.d.ts +7 -0
  168. package/dist/esm/relayer/errors.js +13 -0
  169. package/dist/esm/relayer/index.d.ts +3 -0
  170. package/dist/esm/relayer/index.js +3 -0
  171. package/dist/esm/relayer/transport.d.ts +17 -0
  172. package/dist/esm/relayer/transport.js +59 -0
  173. package/dist/esm/relayer/types.d.ts +276 -0
  174. package/dist/esm/relayer/types.js +4 -0
  175. package/dist/esm/relayer.d.ts +1 -318
  176. package/dist/esm/relayer.js +2 -249
  177. package/dist/esm/shield/alt.js +1 -1
  178. package/dist/esm/shield/errors.js +1 -1
  179. package/dist/esm/shield/finalize.js +1 -1
  180. package/dist/esm/shield/ix.d.ts +1 -1
  181. package/dist/esm/shield/owner.js +2 -1
  182. package/dist/esm/shield/ports.js +1 -1
  183. package/dist/esm/shield/preflight.js +1 -1
  184. package/dist/esm/shield/shield.js +4 -3
  185. package/dist/esm/shield/types.d.ts +1 -1
  186. package/dist/esm/transactions/deposit.d.ts +94 -0
  187. package/dist/esm/transactions/deposit.js +198 -0
  188. package/dist/esm/transactions/index.d.ts +7 -0
  189. package/dist/esm/transactions/index.js +7 -0
  190. package/dist/esm/transactions/swap.d.ts +71 -0
  191. package/dist/esm/transactions/swap.js +144 -0
  192. package/dist/esm/transactions/transact.d.ts +34 -0
  193. package/dist/esm/transactions/transact.js +110 -0
  194. package/dist/esm/transactions/transfer.d.ts +51 -0
  195. package/dist/esm/transactions/transfer.js +103 -0
  196. package/dist/esm/transactions/withdraw.d.ts +52 -0
  197. package/dist/esm/transactions/withdraw.js +101 -0
  198. package/dist/esm/utxo.d.ts +1 -235
  199. package/dist/esm/utxo.js +2 -382
  200. package/package.json +54 -16
  201. package/SHIELD_INTEGRATION.md +0 -143
  202. package/config.d.ts +0 -2
  203. package/config.js +0 -4
  204. package/dist/src/client.d.ts +0 -407
  205. package/dist/src/client.js +0 -951
  206. package/dist/src/compactNote.d.ts +0 -107
  207. package/dist/src/compactNote.js +0 -167
  208. package/dist/src/config.d.ts +0 -82
  209. package/dist/src/config.js +0 -57
  210. package/dist/src/events.d.ts +0 -77
  211. package/dist/src/events.js +0 -167
  212. package/dist/src/idl/privacy_pool.d.ts +0 -5
  213. package/dist/src/idl/privacy_pool.js +0 -15218
  214. package/dist/src/index.d.ts +0 -16
  215. package/dist/src/index.js +0 -67
  216. package/dist/src/merkle.d.ts +0 -77
  217. package/dist/src/merkle.js +0 -156
  218. package/dist/src/poseidon.d.ts +0 -29
  219. package/dist/src/poseidon.js +0 -100
  220. package/dist/src/program.d.ts +0 -37
  221. package/dist/src/program.js +0 -61
  222. package/dist/src/proof.d.ts +0 -183
  223. package/dist/src/proof.js +0 -292
  224. package/dist/src/prover.d.ts +0 -54
  225. package/dist/src/prover.js +0 -112
  226. package/dist/src/random.d.ts +0 -16
  227. package/dist/src/random.js +0 -28
  228. package/dist/src/relayer.js +0 -257
  229. package/dist/src/retry.d.ts +0 -32
  230. package/dist/src/retry.js +0 -75
  231. package/dist/src/shield/alt.d.ts +0 -87
  232. package/dist/src/shield/alt.js +0 -194
  233. package/dist/src/shield/computeBudget.d.ts +0 -61
  234. package/dist/src/shield/computeBudget.js +0 -64
  235. package/dist/src/shield/errors.d.ts +0 -58
  236. package/dist/src/shield/errors.js +0 -121
  237. package/dist/src/shield/finalize.d.ts +0 -45
  238. package/dist/src/shield/finalize.js +0 -119
  239. package/dist/src/shield/index.d.ts +0 -35
  240. package/dist/src/shield/index.js +0 -68
  241. package/dist/src/shield/ix.d.ts +0 -54
  242. package/dist/src/shield/ix.js +0 -119
  243. package/dist/src/shield/owner.d.ts +0 -36
  244. package/dist/src/shield/owner.js +0 -126
  245. package/dist/src/shield/ports.d.ts +0 -43
  246. package/dist/src/shield/ports.js +0 -153
  247. package/dist/src/shield/preflight.d.ts +0 -30
  248. package/dist/src/shield/preflight.js +0 -154
  249. package/dist/src/shield/shield.d.ts +0 -68
  250. package/dist/src/shield/shield.js +0 -499
  251. package/dist/src/shield/types.d.ts +0 -202
  252. package/dist/src/utxo.d.ts +0 -235
  253. package/dist/src/utxo.js +0 -407
  254. package/dist/tests/compact-note.test.d.ts +0 -1
  255. package/dist/tests/compact-note.test.js +0 -173
  256. package/dist/tests/config.test.d.ts +0 -1
  257. package/dist/tests/config.test.js +0 -102
  258. package/dist/tests/edge-cases.test.d.ts +0 -1
  259. package/dist/tests/edge-cases.test.js +0 -220
  260. package/dist/tests/encryption.test.d.ts +0 -1
  261. package/dist/tests/encryption.test.js +0 -215
  262. package/dist/tests/events.test.d.ts +0 -1
  263. package/dist/tests/events.test.js +0 -78
  264. package/dist/tests/multi-tree.test.d.ts +0 -1
  265. package/dist/tests/multi-tree.test.js +0 -405
  266. package/dist/tests/pda.test.d.ts +0 -1
  267. package/dist/tests/pda.test.js +0 -229
  268. package/dist/tests/poseidon-builder-parity.test.d.ts +0 -1
  269. package/dist/tests/poseidon-builder-parity.test.js +0 -72
  270. package/dist/tests/poseidon.test.d.ts +0 -1
  271. package/dist/tests/poseidon.test.js +0 -142
  272. package/dist/tests/proof.test.d.ts +0 -1
  273. package/dist/tests/proof.test.js +0 -296
  274. package/dist/tests/relayer.test.d.ts +0 -1
  275. package/dist/tests/relayer.test.js +0 -271
  276. package/dist/tests/sdk.integration.test.d.ts +0 -1
  277. package/dist/tests/sdk.integration.test.js +0 -330
  278. package/dist/tests/shield-owner.test.d.ts +0 -1
  279. package/dist/tests/shield-owner.test.js +0 -89
  280. package/dist/tests/shield-preflight.test.d.ts +0 -1
  281. package/dist/tests/shield-preflight.test.js +0 -87
  282. package/dist/tests/shield-realproof.test.d.ts +0 -1
  283. package/dist/tests/shield-realproof.test.js +0 -272
  284. package/dist/tests/shield.test.d.ts +0 -1
  285. package/dist/tests/shield.test.js +0 -403
  286. package/dist/tests/utxo.test.d.ts +0 -1
  287. package/dist/tests/utxo.test.js +0 -140
  288. package/poseidon.d.ts +0 -2
  289. package/poseidon.js +0 -4
  290. package/proof.d.ts +0 -2
  291. package/proof.js +0 -4
  292. package/prover.d.ts +0 -2
  293. package/prover.js +0 -4
  294. package/shield.d.ts +0 -2
  295. package/shield.js +0 -4
  296. /package/dist/{src/shield → cjs/proofs}/types.js +0 -0
@@ -44,6 +44,21 @@ export type CompactNoteCipher = {
44
44
  /** 1-byte scan filter. */
45
45
  viewTag: number;
46
46
  };
47
+ /** The optional pair of compact note ciphers carried by `transact` instructions. */
48
+ export type NoteCiphers = {
49
+ note0EphemeralKey: Uint8Array;
50
+ note0Encrypted: Uint8Array;
51
+ note0ViewTag: number;
52
+ note1EphemeralKey: Uint8Array;
53
+ note1Encrypted: Uint8Array;
54
+ note1ViewTag: number;
55
+ };
56
+ /** Plaintext note material needed to create one compact on-chain cipher. */
57
+ export type NoteCipherInput = {
58
+ recipientWalletPubkey: Uint8Array;
59
+ blinding: Uint8Array;
60
+ amount: bigint;
61
+ };
47
62
  /**
48
63
  * Test-only determinism hook. NOT part of the public contract — it exists so
49
64
  * golden vectors can pin exact bytes against the sibling implementations, which
@@ -105,3 +120,9 @@ export declare function matchesViewTag(walletSecretKey: Uint8Array, ephemeralPub
105
120
  export declare function toX25519Private(walletSecretKey: Uint8Array): Uint8Array;
106
121
  /** The all-zero cipher the program treats as "no note here". */
107
122
  export declare function emptyNoteCipher(): CompactNoteCipher;
123
+ /**
124
+ * Build the two-note payload accepted by `transact` and `transact_swap`.
125
+ * A missing side is encoded with the program's all-zero sentinel. If both
126
+ * sides are missing, `null` is returned so Anchor encodes `Option::None`.
127
+ */
128
+ export declare function createNoteCiphers(note0: NoteCipherInput | null, note1: NoteCipherInput | null): NoteCiphers | null;
@@ -9,8 +9,9 @@ exports.decryptCompactNoteCipher = decryptCompactNoteCipher;
9
9
  exports.matchesViewTag = matchesViewTag;
10
10
  exports.toX25519Private = toX25519Private;
11
11
  exports.emptyNoteCipher = emptyNoteCipher;
12
+ exports.createNoteCiphers = createNoteCiphers;
12
13
  const tweetnacl_1 = __importDefault(require("tweetnacl"));
13
- const utxo_js_1 = require("./utxo.js");
14
+ const mailbox_js_1 = require("./notes/mailbox.js");
14
15
  const random_js_1 = require("./random.js");
15
16
  /**
16
17
  * Compact note ciphers — the 80-byte payload carried inside the on-chain
@@ -82,7 +83,7 @@ function createCompactNoteCipher(recipientWalletPubkey, blinding, amount,
82
83
  const ephemeralSeed = __det?.ephemeralSeed ?? (0, random_js_1.randomBytes)(32);
83
84
  const ephemeralPublicKey = tweetnacl_1.default.sign.keyPair.fromSeed(ephemeralSeed).publicKey;
84
85
  const sharedSecret = deriveSharedSecret(ephemeralSeed, recipientWalletPubkey);
85
- const key = (0, utxo_js_1.kdfSHA512_256)(sharedSecret);
86
+ const key = (0, mailbox_js_1.kdfSHA512_256)(sharedSecret);
86
87
  const plaintext = new Uint8Array(COMPACT_PLAINTEXT_LENGTH);
87
88
  plaintext.set(blinding.slice(0, 32), 0);
88
89
  let amt = amount;
@@ -114,9 +115,9 @@ function decryptCompactNoteCipher(walletSecretKey, ephemeralPublicKey, compactBl
114
115
  return null;
115
116
  const sharedSecret = precomputed?.sharedSecret ??
116
117
  (precomputed?.x25519Private
117
- ? tweetnacl_1.default.scalarMult(precomputed.x25519Private, (0, utxo_js_1.ed25519PublicKeyToX25519)(ephemeralPublicKey))
118
+ ? tweetnacl_1.default.scalarMult(precomputed.x25519Private, (0, mailbox_js_1.ed25519PublicKeyToX25519)(ephemeralPublicKey))
118
119
  : deriveSharedSecret(walletSecretKey, ephemeralPublicKey));
119
- const key = (0, utxo_js_1.kdfSHA512_256)(sharedSecret);
120
+ const key = (0, mailbox_js_1.kdfSHA512_256)(sharedSecret);
120
121
  const nonce = compactBlob.slice(0, NONCE_LENGTH);
121
122
  const encrypted = compactBlob.slice(NONCE_LENGTH);
122
123
  const plaintext = tweetnacl_1.default.secretbox.open(encrypted, nonce, key);
@@ -138,7 +139,7 @@ function decryptCompactNoteCipher(walletSecretKey, ephemeralPublicKey, compactBl
138
139
  */
139
140
  function matchesViewTag(walletSecretKey, ephemeralPublicKey, viewTag, precomputed) {
140
141
  const sharedSecret = precomputed?.x25519Private
141
- ? tweetnacl_1.default.scalarMult(precomputed.x25519Private, (0, utxo_js_1.ed25519PublicKeyToX25519)(ephemeralPublicKey))
142
+ ? tweetnacl_1.default.scalarMult(precomputed.x25519Private, (0, mailbox_js_1.ed25519PublicKeyToX25519)(ephemeralPublicKey))
142
143
  : deriveSharedSecret(walletSecretKey, ephemeralPublicKey);
143
144
  return { matches: tweetnacl_1.default.hash(sharedSecret)[0] === viewTag, sharedSecret };
144
145
  }
@@ -147,7 +148,7 @@ function matchesViewTag(walletSecretKey, ephemeralPublicKey, viewTag, precompute
147
148
  * across a scan loop.
148
149
  */
149
150
  function toX25519Private(walletSecretKey) {
150
- return (0, utxo_js_1.ed25519SeedToX25519Private)(seedOf(walletSecretKey));
151
+ return (0, mailbox_js_1.ed25519SeedToX25519Private)(seedOf(walletSecretKey));
151
152
  }
152
153
  /** The all-zero cipher the program treats as "no note here". */
153
154
  function emptyNoteCipher() {
@@ -157,11 +158,34 @@ function emptyNoteCipher() {
157
158
  viewTag: 0,
158
159
  };
159
160
  }
161
+ /**
162
+ * Build the two-note payload accepted by `transact` and `transact_swap`.
163
+ * A missing side is encoded with the program's all-zero sentinel. If both
164
+ * sides are missing, `null` is returned so Anchor encodes `Option::None`.
165
+ */
166
+ function createNoteCiphers(note0, note1) {
167
+ if (!note0 && !note1)
168
+ return null;
169
+ const cipher0 = note0
170
+ ? createCompactNoteCipher(note0.recipientWalletPubkey, note0.blinding, note0.amount)
171
+ : emptyNoteCipher();
172
+ const cipher1 = note1
173
+ ? createCompactNoteCipher(note1.recipientWalletPubkey, note1.blinding, note1.amount)
174
+ : emptyNoteCipher();
175
+ return {
176
+ note0EphemeralKey: cipher0.ephemeralPublicKey,
177
+ note0Encrypted: cipher0.compactBlob,
178
+ note0ViewTag: cipher0.viewTag,
179
+ note1EphemeralKey: cipher1.ephemeralPublicKey,
180
+ note1Encrypted: cipher1.compactBlob,
181
+ note1ViewTag: cipher1.viewTag,
182
+ };
183
+ }
160
184
  // -- internal -----------------------------------------------------------------
161
185
  /** Accept either a 32-byte Ed25519 seed or a 64-byte secret key, as the siblings do. */
162
186
  function seedOf(secretKey) {
163
187
  return secretKey.length === 64 ? secretKey.slice(0, 32) : secretKey;
164
188
  }
165
189
  function deriveSharedSecret(mySecretKey, theirEd25519PublicKey) {
166
- return tweetnacl_1.default.scalarMult((0, utxo_js_1.ed25519SeedToX25519Private)(seedOf(mySecretKey)), (0, utxo_js_1.ed25519PublicKeyToX25519)(theirEd25519PublicKey));
190
+ return tweetnacl_1.default.scalarMult((0, mailbox_js_1.ed25519SeedToX25519Private)(seedOf(mySecretKey)), (0, mailbox_js_1.ed25519PublicKeyToX25519)(theirEd25519PublicKey));
167
191
  }
@@ -6,11 +6,12 @@ export * from "./proof.js";
6
6
  export * from "./events.js";
7
7
  export { createVeiloProgram, createReadonlyVeiloProgram, PRIVACY_POOL_IDL, PRIVACY_POOL_PROGRAM_ID, } from "./program.js";
8
8
  export * from "./shield/index.js";
9
- export { createTransactionProver, verifyProof } from "./prover.js";
9
+ export { createTransactionProver, createSwapProver, verifyProof, } from "./prover.js";
10
10
  export type { CircuitArtifacts } from "./prover.js";
11
11
  export * from "./compactNote.js";
12
12
  export { randomBytes, randomBytes32 } from "./random.js";
13
13
  export * from "./relayer.js";
14
+ export * from "./cloak.js";
14
15
  export { withRpcRetry, DEFAULT_RPC_RETRY } from "./retry.js";
15
16
  export type { RpcRetryPolicy } from "./retry.js";
16
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,7 +14,7 @@ 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.randomBytes32 = exports.randomBytes = exports.verifyProof = exports.createTransactionProver = exports.PRIVACY_POOL_PROGRAM_ID = exports.PRIVACY_POOL_IDL = exports.createReadonlyVeiloProgram = 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
19
  __exportStar(require("./client.js"), exports);
20
20
  // UTXO types and functions (replaces note.ts)
@@ -38,6 +38,7 @@ __exportStar(require("./shield/index.js"), exports);
38
38
  // Proof generation (requires snarkjs peer dependency)
39
39
  var prover_js_1 = require("./prover.js");
40
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; } });
41
42
  Object.defineProperty(exports, "verifyProof", { enumerable: true, get: function () { return prover_js_1.verifyProof; } });
42
43
  // Compact note ciphers — the on-chain payload that makes notes recoverable
43
44
  // from chain data alone, without any relayer involvement.
@@ -48,6 +49,8 @@ Object.defineProperty(exports, "randomBytes", { enumerable: true, get: function
48
49
  Object.defineProperty(exports, "randomBytes32", { enumerable: true, get: function () { return random_js_1.randomBytes32; } });
49
50
  // Relayer API client
50
51
  __exportStar(require("./relayer.js"), exports);
52
+ // Veilo Cloak Partner API client and helpers
53
+ __exportStar(require("./cloak.js"), exports);
51
54
  // RPC retry / resilience
52
55
  var retry_js_1 = require("./retry.js");
53
56
  Object.defineProperty(exports, "withRpcRetry", { enumerable: true, get: function () { return retry_js_1.withRpcRetry; } });
@@ -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
+ }
@@ -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;