@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
@@ -1,68 +0,0 @@
1
- import { AddressLookupTableAccount, Connection } from "@solana/web3.js";
2
- import { bytesToBigIntBE, BN254_FR_MODULUS } from "../poseidon.js";
3
- import type { ShieldParams, ShieldProofCache, ShieldResult } from "./types.js";
4
- /**
5
- * Build an unsigned transaction that shields public funds into the privacy pool.
6
- *
7
- * ## The contract
8
- *
9
- * This function builds. It does not sign, submit, confirm, or retry — those
10
- * belong to whoever holds the key, exactly as with Jupiter's `/swap`. The
11
- * returned `transaction` is unsigned; the caller signs it with whatever wallet
12
- * they have and broadcasts it themselves.
13
- *
14
- * ## Why this can be handed to a third party
15
- *
16
- * Three properties make it safe to build a shield on behalf of a signer you do
17
- * not control, and to shield to an owner who is not the signer:
18
- *
19
- * 1. **Deposits are permissionless.** The program gates its relayer allowlist
20
- * to `public_amount <= 0`, so any wallet can sign a deposit.
21
- * 2. **No secrets are involved in building.** The proof for a deposit has no
22
- * real inputs, and the output commitment binds only the recipient's *public*
23
- * keys.
24
- * 3. **The note rides on-chain.** `note_ciphers` travels inside the instruction,
25
- * so the recipient can find the note by scanning even if the platform that
26
- * submitted it never says a word.
27
- *
28
- * ## What the signer unavoidably pays
29
- *
30
- * The signer is the fee payer, the funding source, and the payer of two
31
- * `NullifierMarker` rents (~0.00192 SOL, permanently burned — the program
32
- * hardcodes `payer = relayer` on both). A wallet with zero SOL cannot shield
33
- * through this path no matter who submits the transaction. Fee sponsorship is
34
- * not achievable here: a second signature costs ~96 bytes against a budget with
35
- * ~69 to spare, and would not cover the rent anyway.
36
- */
37
- export declare function shield(params: ShieldParams): Promise<ShieldResult>;
38
- /**
39
- * Re-assemble a shield with a fresh blockhash and deadline, reusing the proof.
40
- *
41
- * The Groth16 proof commits to the root, the public amount, `ext_data_hash`, the
42
- * mint, the nullifiers and the commitments — none of which is time-dependent.
43
- * `deadline` is a positional instruction argument, outside `ext_data_hash` and
44
- * therefore outside the proof, so it can be refreshed too. That last part
45
- * matters: a shield built at T and signed at T+59min needs a new deadline, not
46
- * just a new blockhash.
47
- *
48
- * Costs one `getLatestBlockhash` and a message compile — no proving, no
49
- * Poseidon, no account resolution.
50
- *
51
- * Does NOT recover from `ROOT_STALE`: a stale root invalidates the proof, so
52
- * that case needs a fresh `shield()` call.
53
- */
54
- export declare function rebuild(result: ShieldResult, opts?: {
55
- connection?: Connection;
56
- alt?: AddressLookupTableAccount;
57
- blockhash?: {
58
- blockhash: string;
59
- lastValidBlockHeight: number;
60
- };
61
- deadlineSeconds?: number;
62
- computeUnitPriceMicroLamports?: number;
63
- }): Promise<ShieldResult>;
64
- /** Serialize a proof cache for transport between processes. Contains no secrets. */
65
- export declare function serializeProofCache(cache: ShieldProofCache): string;
66
- export declare function deserializeProofCache(json: string): ShieldProofCache;
67
- /** Re-exported for callers that want the raw field arithmetic. */
68
- export { bytesToBigIntBE, BN254_FR_MODULUS };
@@ -1,499 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BN254_FR_MODULUS = exports.bytesToBigIntBE = void 0;
4
- exports.shield = shield;
5
- exports.rebuild = rebuild;
6
- exports.serializeProofCache = serializeProofCache;
7
- exports.deserializeProofCache = deserializeProofCache;
8
- const web3_js_1 = require("@solana/web3.js");
9
- const spl_token_1 = require("@solana/spl-token");
10
- const program_js_1 = require("../program.js");
11
- const client_js_1 = require("../client.js");
12
- const config_js_1 = require("../config.js");
13
- const poseidon_js_1 = require("../poseidon.js");
14
- Object.defineProperty(exports, "bytesToBigIntBE", { enumerable: true, get: function () { return poseidon_js_1.bytesToBigIntBE; } });
15
- Object.defineProperty(exports, "BN254_FR_MODULUS", { enumerable: true, get: function () { return poseidon_js_1.BN254_FR_MODULUS; } });
16
- const merkle_js_1 = require("../merkle.js");
17
- const utxo_js_1 = require("../utxo.js");
18
- const proof_js_1 = require("../proof.js");
19
- const compactNote_js_1 = require("../compactNote.js");
20
- const ix_js_1 = require("./ix.js");
21
- const alt_js_1 = require("./alt.js");
22
- const ports_js_1 = require("./ports.js");
23
- const preflight_js_1 = require("./preflight.js");
24
- const errors_js_1 = require("./errors.js");
25
- const computeBudget_js_1 = require("./computeBudget.js");
26
- /**
27
- * Build an unsigned transaction that shields public funds into the privacy pool.
28
- *
29
- * ## The contract
30
- *
31
- * This function builds. It does not sign, submit, confirm, or retry — those
32
- * belong to whoever holds the key, exactly as with Jupiter's `/swap`. The
33
- * returned `transaction` is unsigned; the caller signs it with whatever wallet
34
- * they have and broadcasts it themselves.
35
- *
36
- * ## Why this can be handed to a third party
37
- *
38
- * Three properties make it safe to build a shield on behalf of a signer you do
39
- * not control, and to shield to an owner who is not the signer:
40
- *
41
- * 1. **Deposits are permissionless.** The program gates its relayer allowlist
42
- * to `public_amount <= 0`, so any wallet can sign a deposit.
43
- * 2. **No secrets are involved in building.** The proof for a deposit has no
44
- * real inputs, and the output commitment binds only the recipient's *public*
45
- * keys.
46
- * 3. **The note rides on-chain.** `note_ciphers` travels inside the instruction,
47
- * so the recipient can find the note by scanning even if the platform that
48
- * submitted it never says a word.
49
- *
50
- * ## What the signer unavoidably pays
51
- *
52
- * The signer is the fee payer, the funding source, and the payer of two
53
- * `NullifierMarker` rents (~0.00192 SOL, permanently burned — the program
54
- * hardcodes `payer = relayer` on both). A wallet with zero SOL cannot shield
55
- * through this path no matter who submits the transaction. Fee sponsorship is
56
- * not achievable here: a second signature costs ~96 bytes against a budget with
57
- * ~69 to spare, and would not cover the rent anyway.
58
- */
59
- async function shield(params) {
60
- const { connection, amount, owner, signer, prover, mint = config_js_1.NATIVE_SOL_MINT, programId = program_js_1.PRIVACY_POOL_PROGRAM_ID, computeUnitLimit = computeBudget_js_1.SHIELD_COMPUTE_UNIT_LIMIT, computeUnitPriceMicroLamports = computeBudget_js_1.SHIELD_COMPUTE_UNIT_PRICE_MICRO_LAMPORTS, deadlineSeconds = computeBudget_js_1.DEFAULT_DEADLINE_SECONDS, } = params;
61
- if (amount <= 0n) {
62
- throw new errors_js_1.ShieldError("UNKNOWN", `shield amount must be positive, got ${amount}`);
63
- }
64
- // The owner invariant cannot be verified by anything on-chain or in the type
65
- // system: if `veiloPublicKey` and `noteViewingKey` belong to different people
66
- // the note is spendable-but-invisible and the funds are gone from the owner's
67
- // view with no error anywhere. `resolveShieldOwner()` sets this flag because
68
- // it fetches both halves from one response and can therefore guarantee they
69
- // match. Anyone hand-building an owner is asserting they did the same.
70
- if (!owner.acknowledgeOwnerInvariant) {
71
- throw new errors_js_1.ShieldError("OWNER_INVARIANT_UNACKNOWLEDGED", "ShieldOwner was constructed by hand. `veiloPublicKey` (spend authority) and " +
72
- "`noteViewingKey` (detection authority) MUST belong to the same account — if " +
73
- "they do not, the note is spendable but permanently invisible to its owner. " +
74
- "Use resolveShieldOwner() to obtain both from a single source, or set " +
75
- "`acknowledgeOwnerInvariant: true` if you have verified them yourself.");
76
- }
77
- const cache = params.cache ?? (0, ports_js_1.memoryCache)();
78
- const treeSource = params.treeSource ?? (0, ports_js_1.onChainTreeSource)(connection, programId);
79
- const altSource = params.alt ?? (0, alt_js_1.sharedDepositAlt)(undefined, { cache });
80
- const isToken = !mint.equals(config_js_1.NATIVE_SOL_MINT);
81
- const userTokenAccount = isToken
82
- ? (signer.tokenAccount ?? (await (0, spl_token_1.getAssociatedTokenAddress)(mint, signer.publicKey)))
83
- : signer.publicKey;
84
- // --- 1. Pre-flight ---------------------------------------------------------
85
- // Before the prover, which costs 10-30 seconds. Every check here is one RPC
86
- // call, and each one pre-empts an on-chain failure whose error message would
87
- // tell the user nothing actionable.
88
- if (params.preflight !== false) {
89
- await (0, preflight_js_1.preflightShield)({
90
- connection,
91
- amount,
92
- mint,
93
- signer: { publicKey: signer.publicKey, tokenAccount: userTokenAccount },
94
- programId,
95
- options: typeof params.preflight === "object" ? params.preflight : {},
96
- cache,
97
- computeUnitLimit,
98
- computeUnitPriceMicroLamports,
99
- });
100
- }
101
- // --- 2. Target tree and root ----------------------------------------------
102
- const target = await treeSource.getDepositTarget(mint);
103
- const treeCapacity = 2 ** config_js_1.MERKLE_TREE_DEPTH;
104
- if (target.outputNextIndex >= treeCapacity) {
105
- throw new errors_js_1.ShieldError("TREE_FULL", `Merkle tree ${target.outputTreeId} is full (${target.outputNextIndex}/${treeCapacity} leaves).`, {
106
- retryable: "reshield",
107
- context: {
108
- treeId: target.outputTreeId,
109
- nextIndex: target.outputNextIndex,
110
- capacity: treeCapacity,
111
- },
112
- });
113
- }
114
- // --- 3. Outputs ------------------------------------------------------------
115
- // Output 0 carries the value and is owned by the recipient. Output 1 is a
116
- // zero-value dummy the 2-in-2-out circuit requires.
117
- const outputNote = (0, utxo_js_1.createUTXO)({
118
- amount,
119
- pubkey: owner.veiloPublicKey,
120
- mintAddress: mint,
121
- });
122
- const dummyOutput = (0, utxo_js_1.createUTXO)({
123
- amount: 0n,
124
- pubkey: (0, utxo_js_1.generateKeypair)().publicKey,
125
- mintAddress: mint,
126
- });
127
- // --- 4. Dummy inputs -------------------------------------------------------
128
- // Two INDEPENDENT keypairs. Reusing one (as the stale client.ts `deposit()`
129
- // does) only avoids the program's DuplicateNullifiers check by accident of
130
- // blinding randomness; two keys make it structural.
131
- const zeroChain = (0, merkle_js_1.poseidonZeroChain)(22).slice(0, 22);
132
- const dummyInputs = [
133
- toDummyInput((0, utxo_js_1.createOwnedZeroUTXO)((0, utxo_js_1.generateKeypair)().privateKey, mint), zeroChain),
134
- toDummyInput((0, utxo_js_1.createOwnedZeroUTXO)((0, utxo_js_1.generateKeypair)().privateKey, mint), zeroChain),
135
- ];
136
- // --- 5. ExtData ------------------------------------------------------------
137
- // For a deposit these slots carry no payment semantics — no fee is taken and
138
- // nothing is withdrawn — but they are bound into the proof via ext_data_hash,
139
- // so they must match exactly what goes on-chain.
140
- const extData = {
141
- recipient: signer.publicKey,
142
- relayer: signer.publicKey,
143
- fee: 0n,
144
- refund: 0n,
145
- claimant: web3_js_1.SystemProgram.programId,
146
- };
147
- const extDataHash = (0, proof_js_1.computeExtDataHash)(extData);
148
- // --- 6. Prove --------------------------------------------------------------
149
- const circuitInputs = (0, proof_js_1.prepareTransactionInputs)({
150
- inputUTXOs: dummyInputs,
151
- outputUTXOs: [outputNote, dummyOutput],
152
- root: target.inputRoot,
153
- publicAmount: amount,
154
- extData,
155
- mintAddress: mint,
156
- });
157
- let proof;
158
- try {
159
- proof = (0, proof_js_1.encodeSnarkjsProofToTransactionProof)(await prover(circuitInputs));
160
- }
161
- catch (e) {
162
- throw (0, errors_js_1.mapShieldError)(e);
163
- }
164
- // --- 7. Encrypt the note to its owner --------------------------------------
165
- // Encrypted to `noteViewingKey`, which may be a completely different party
166
- // from the signer. This is what makes shield-to-someone-else work.
167
- const compact = (0, compactNote_js_1.createCompactNoteCipher)(owner.noteViewingKey.toBytes(), (0, poseidon_js_1.bigIntToBytesBE)(outputNote.blinding), amount);
168
- const empty = (0, compactNote_js_1.emptyNoteCipher)();
169
- // --- 8. Resolve accounts ---------------------------------------------------
170
- const accounts = resolveTransactAccounts({
171
- programId,
172
- mint,
173
- signer: signer.publicKey,
174
- userTokenAccount,
175
- inputTreeId: target.inputTreeId,
176
- outputTreeId: target.outputTreeId,
177
- inputNullifiers: circuitInputs.inputNullifiers,
178
- vaultTokenAccount: isToken
179
- ? await (0, spl_token_1.getAssociatedTokenAddress)(mint, (0, client_js_1.getPoolPdas)(programId, mint).vault, true)
180
- : (0, client_js_1.getPoolPdas)(programId, mint).vault,
181
- isToken,
182
- });
183
- // --- 9. Cache everything needed to rebuild ---------------------------------
184
- const alt = await altSource.resolve({
185
- connection,
186
- mint,
187
- outputTreeId: target.outputTreeId,
188
- programId,
189
- });
190
- const proofCache = {
191
- version: 1,
192
- programId: programId.toBase58(),
193
- mint: mint.toBase58(),
194
- signer: signer.publicKey.toBase58(),
195
- altAddress: alt.key.toBase58(),
196
- inputTreeId: target.inputTreeId,
197
- outputTreeId: target.outputTreeId,
198
- root: hex(target.inputRoot),
199
- publicAmount: amount.toString(),
200
- extData: {
201
- recipient: extData.recipient.toBase58(),
202
- relayer: extData.relayer.toBase58(),
203
- fee: extData.fee.toString(),
204
- refund: extData.refund.toString(),
205
- claimant: extData.claimant.toBase58(),
206
- },
207
- extDataHash: hex(extDataHash),
208
- inputNullifiers: [
209
- hex(circuitInputs.inputNullifiers[0]),
210
- hex(circuitInputs.inputNullifiers[1]),
211
- ],
212
- outputCommitments: [hex(outputNote.commitment), hex(dummyOutput.commitment)],
213
- proof,
214
- noteCiphers: {
215
- note0EphemeralKey: hex(compact.ephemeralPublicKey),
216
- note0Encrypted: hex(compact.compactBlob),
217
- note0ViewTag: compact.viewTag,
218
- note1EphemeralKey: hex(empty.ephemeralPublicKey),
219
- note1Encrypted: hex(empty.compactBlob),
220
- note1ViewTag: empty.viewTag,
221
- },
222
- accounts: Object.fromEntries(Object.entries(accounts).map(([k, v]) => [k, v.toBase58()])),
223
- computeUnitLimit,
224
- };
225
- const note = {
226
- commitment: outputNote.commitment,
227
- blinding: outputNote.blinding,
228
- amount,
229
- ownerVeiloPublicKey: owner.veiloPublicKey,
230
- mint,
231
- treeId: target.outputTreeId,
232
- compact,
233
- };
234
- // --- 10. Assemble ----------------------------------------------------------
235
- const blockhash = params.blockhash ?? (await connection.getLatestBlockhash("confirmed"));
236
- const deadline = BigInt(Math.floor(Date.now() / 1000) + deadlineSeconds);
237
- const built = buildFromCache({
238
- cache: proofCache,
239
- alt,
240
- blockhash,
241
- deadline,
242
- computeUnitPriceMicroLamports,
243
- });
244
- const estimatedRentLamports = await rentForNullifierMarkers(connection, cache);
245
- return {
246
- transaction: built.transaction,
247
- blockhash: blockhash.blockhash,
248
- lastValidBlockHeight: blockhash.lastValidBlockHeight,
249
- deadline,
250
- note,
251
- predictedLeafIndex: target.outputNextIndex,
252
- quote: {
253
- serializedSize: built.serializedSize,
254
- computeUnitLimit,
255
- computeUnitPriceMicroLamports,
256
- estimatedFeeLamports: 5000n +
257
- (BigInt(computeUnitLimit) * BigInt(computeUnitPriceMicroLamports)) / 1000000n,
258
- estimatedRentLamports,
259
- },
260
- proofCache,
261
- };
262
- }
263
- /**
264
- * Re-assemble a shield with a fresh blockhash and deadline, reusing the proof.
265
- *
266
- * The Groth16 proof commits to the root, the public amount, `ext_data_hash`, the
267
- * mint, the nullifiers and the commitments — none of which is time-dependent.
268
- * `deadline` is a positional instruction argument, outside `ext_data_hash` and
269
- * therefore outside the proof, so it can be refreshed too. That last part
270
- * matters: a shield built at T and signed at T+59min needs a new deadline, not
271
- * just a new blockhash.
272
- *
273
- * Costs one `getLatestBlockhash` and a message compile — no proving, no
274
- * Poseidon, no account resolution.
275
- *
276
- * Does NOT recover from `ROOT_STALE`: a stale root invalidates the proof, so
277
- * that case needs a fresh `shield()` call.
278
- */
279
- async function rebuild(result, opts = {}) {
280
- const cache = result.proofCache;
281
- const blockhash = opts.blockhash ??
282
- (opts.connection
283
- ? await opts.connection.getLatestBlockhash("confirmed")
284
- : (() => {
285
- throw new errors_js_1.ShieldError("UNKNOWN", "rebuild() needs either a `connection` or an explicit `blockhash`");
286
- })());
287
- const alt = opts.alt ??
288
- (opts.connection
289
- ? await (0, alt_js_1.sharedDepositAlt)(new web3_js_1.PublicKey(cache.altAddress), {
290
- verifyCoverage: false,
291
- }).resolve({
292
- connection: opts.connection,
293
- mint: new web3_js_1.PublicKey(cache.mint),
294
- outputTreeId: cache.outputTreeId,
295
- programId: new web3_js_1.PublicKey(cache.programId),
296
- })
297
- : (() => {
298
- throw new errors_js_1.ShieldError("UNKNOWN", "rebuild() needs either a `connection` or an explicit `alt`");
299
- })());
300
- const deadline = BigInt(Math.floor(Date.now() / 1000) + (opts.deadlineSeconds ?? computeBudget_js_1.DEFAULT_DEADLINE_SECONDS));
301
- const priceMicroLamports = opts.computeUnitPriceMicroLamports ?? result.quote.computeUnitPriceMicroLamports;
302
- const built = buildFromCache({
303
- cache,
304
- alt,
305
- blockhash,
306
- deadline,
307
- computeUnitPriceMicroLamports: priceMicroLamports,
308
- });
309
- return {
310
- ...result,
311
- transaction: built.transaction,
312
- blockhash: blockhash.blockhash,
313
- lastValidBlockHeight: blockhash.lastValidBlockHeight,
314
- deadline,
315
- quote: {
316
- ...result.quote,
317
- serializedSize: built.serializedSize,
318
- computeUnitPriceMicroLamports: priceMicroLamports,
319
- },
320
- };
321
- }
322
- /**
323
- * The single place a shield transaction is assembled and size-checked.
324
- *
325
- * Both `shield()` and `rebuild()` route through here so there is exactly one
326
- * message layout and exactly one size assertion — the failure mode this guards
327
- * against (a transaction that is 40 bytes too large) is otherwise diagnosed as
328
- * an opaque wallet error with no indication of which account caused it.
329
- */
330
- function buildFromCache(params) {
331
- const { cache, alt, blockhash, deadline, computeUnitPriceMicroLamports } = params;
332
- const programId = new web3_js_1.PublicKey(cache.programId);
333
- const signer = new web3_js_1.PublicKey(cache.signer);
334
- const accounts = Object.fromEntries(Object.entries(cache.accounts).map(([k, v]) => [k, new web3_js_1.PublicKey(v)]));
335
- const transactIx = (0, ix_js_1.buildTransactInstruction)(accounts, {
336
- root: unhex(cache.root),
337
- inputTreeId: cache.inputTreeId,
338
- outputTreeId: cache.outputTreeId,
339
- publicAmount: BigInt(cache.publicAmount),
340
- extDataHash: unhex(cache.extDataHash),
341
- mintAddress: new web3_js_1.PublicKey(cache.mint),
342
- inputNullifiers: [unhex(cache.inputNullifiers[0]), unhex(cache.inputNullifiers[1])],
343
- outputCommitments: [
344
- unhex(cache.outputCommitments[0]),
345
- unhex(cache.outputCommitments[1]),
346
- ],
347
- deadline,
348
- extData: {
349
- recipient: new web3_js_1.PublicKey(cache.extData.recipient),
350
- relayer: new web3_js_1.PublicKey(cache.extData.relayer),
351
- fee: BigInt(cache.extData.fee),
352
- refund: BigInt(cache.extData.refund),
353
- claimant: new web3_js_1.PublicKey(cache.extData.claimant),
354
- },
355
- proof: cache.proof,
356
- noteCiphers: {
357
- note0EphemeralKey: unhex(cache.noteCiphers.note0EphemeralKey),
358
- note0Encrypted: unhex(cache.noteCiphers.note0Encrypted),
359
- note0ViewTag: cache.noteCiphers.note0ViewTag,
360
- note1EphemeralKey: unhex(cache.noteCiphers.note1EphemeralKey),
361
- note1Encrypted: unhex(cache.noteCiphers.note1Encrypted),
362
- note1ViewTag: cache.noteCiphers.note1ViewTag,
363
- },
364
- }, programId);
365
- const instructions = [
366
- web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ units: cache.computeUnitLimit }),
367
- ];
368
- // A zero price omits the instruction entirely — worth ~40 bytes when the
369
- // budget is tight (the gasless path relies on exactly this).
370
- if (computeUnitPriceMicroLamports > 0) {
371
- instructions.push(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
372
- microLamports: computeUnitPriceMicroLamports,
373
- }));
374
- }
375
- instructions.push(transactIx);
376
- const message = new web3_js_1.TransactionMessage({
377
- payerKey: signer,
378
- recentBlockhash: blockhash.blockhash,
379
- instructions,
380
- }).compileToV0Message([alt]);
381
- const transaction = new web3_js_1.VersionedTransaction(message);
382
- // Measure the message separately and add the one-signature wire section.
383
- // VersionedTransaction.serialize() cannot be used for diagnostics here: it
384
- // serializes through a fixed-size buffer and can throw `encoding overruns`
385
- // before we get a chance to report which ALT keys were missing.
386
- const serializedSize = serializedMessageSize(message) + computeBudget_js_1.SIGNATURE_BYTES;
387
- if (serializedSize > computeBudget_js_1.MAX_TRANSACTION_BYTES) {
388
- const staticKeys = message.staticAccountKeys.map((k) => k.toBase58());
389
- const inAlt = new Set(alt.state.addresses.map((a) => a.toBase58()));
390
- throw new errors_js_1.ShieldError("TX_TOO_LARGE", `Shield transaction is ${serializedSize} bytes, over the ${computeBudget_js_1.MAX_TRANSACTION_BYTES} ` +
391
- `limit by ${serializedSize - computeBudget_js_1.MAX_TRANSACTION_BYTES}. ` +
392
- `${staticKeys.length} account(s) could not be compressed into the lookup table.`, {
393
- context: {
394
- serializedSize,
395
- limit: computeBudget_js_1.MAX_TRANSACTION_BYTES,
396
- alt: alt.key.toBase58(),
397
- uncompressedKeys: staticKeys.filter((k) => !inAlt.has(k)),
398
- },
399
- });
400
- }
401
- return { transaction, serializedSize };
402
- }
403
- /** Exact v0 message wire size without serializing into web3.js' 1232-byte buffer. */
404
- function serializedMessageSize(message) {
405
- const staticKeys = message.staticAccountKeys.length;
406
- const instructions = message.compiledInstructions;
407
- const lookups = message.addressTableLookups;
408
- return (1 + // v0 prefix
409
- 3 + // message header
410
- shortVecSize(staticKeys) +
411
- staticKeys * 32 +
412
- 32 + // recent blockhash
413
- shortVecSize(instructions.length) +
414
- instructions.reduce((sum, ix) => sum +
415
- 1 + // program id index
416
- shortVecSize(ix.accountKeyIndexes.length) +
417
- ix.accountKeyIndexes.length +
418
- shortVecSize(ix.data.length) +
419
- ix.data.length, 0) +
420
- shortVecSize(lookups.length) +
421
- lookups.reduce((sum, lookup) => sum +
422
- 32 +
423
- shortVecSize(lookup.writableIndexes.length) +
424
- lookup.writableIndexes.length +
425
- shortVecSize(lookup.readonlyIndexes.length) +
426
- lookup.readonlyIndexes.length, 0));
427
- }
428
- /** Number of bytes used by Solana's short-vector length prefix. */
429
- function shortVecSize(value) {
430
- let size = 1;
431
- while (value >= 0x80) {
432
- value >>>= 7;
433
- size++;
434
- }
435
- return size;
436
- }
437
- // -- helpers ------------------------------------------------------------------
438
- /**
439
- * Wrap a zero-value UTXO as a circuit input.
440
- *
441
- * The path elements are the Poseidon zero chain and the index is 0. Neither is
442
- * constrained — the circuit disables its Merkle check for zero-amount inputs —
443
- * but the witness still has to be well-formed and the right length.
444
- */
445
- function toDummyInput(utxo, zeroChain) {
446
- return {
447
- ...utxo,
448
- pathIndex: 0,
449
- pathElements: zeroChain,
450
- privateKey: utxo.privateKey,
451
- };
452
- }
453
- function resolveTransactAccounts(params) {
454
- const { programId, mint, signer } = params;
455
- const { config, vault, nullifiers } = (0, client_js_1.getPoolPdas)(programId, mint);
456
- return {
457
- config,
458
- global_config: (0, client_js_1.getGlobalConfigPda)(programId),
459
- vault,
460
- input_tree: (0, client_js_1.getNoteTreePda)(programId, mint, params.inputTreeId),
461
- output_tree: (0, client_js_1.getNoteTreePda)(programId, mint, params.outputTreeId),
462
- nullifiers,
463
- nullifier_marker_0: (0, client_js_1.getNullifierMarkerPda)(programId, mint, params.inputNullifiers[0]),
464
- nullifier_marker_1: (0, client_js_1.getNullifierMarkerPda)(programId, mint, params.inputNullifiers[1]),
465
- relayer: signer,
466
- // For a deposit nothing is withdrawn, so `recipient` is just the signer.
467
- // Pointing the unused token slots at accounts already in the transaction
468
- // keeps them from costing 32 static bytes each.
469
- recipient: signer,
470
- vault_token_account: params.vaultTokenAccount,
471
- user_token_account: params.userTokenAccount,
472
- recipient_token_account: params.userTokenAccount,
473
- relayer_token_account: params.userTokenAccount,
474
- token_program: params.isToken ? spl_token_1.TOKEN_PROGRAM_ID : web3_js_1.SystemProgram.programId,
475
- system_program: web3_js_1.SystemProgram.programId,
476
- };
477
- }
478
- async function rentForNullifierMarkers(connection, cache) {
479
- const key = `rent:${computeBudget_js_1.NULLIFIER_MARKER_ACCOUNT_BYTES}`;
480
- let per = await cache.get(key);
481
- if (per === undefined) {
482
- per = await connection.getMinimumBalanceForRentExemption(computeBudget_js_1.NULLIFIER_MARKER_ACCOUNT_BYTES);
483
- await cache.set(key, per, 60 * 60000);
484
- }
485
- return BigInt(per) * BigInt(computeBudget_js_1.NULLIFIER_MARKERS_PER_TRANSACT);
486
- }
487
- const hex = (u) => Buffer.from(u).toString("hex");
488
- const unhex = (s) => new Uint8Array(Buffer.from(s, "hex"));
489
- /** Serialize a proof cache for transport between processes. Contains no secrets. */
490
- function serializeProofCache(cache) {
491
- return JSON.stringify(cache);
492
- }
493
- function deserializeProofCache(json) {
494
- const parsed = JSON.parse(json);
495
- if (parsed?.version !== 1) {
496
- throw new errors_js_1.ShieldError("UNKNOWN", `unsupported ShieldProofCache version: ${parsed?.version}`);
497
- }
498
- return parsed;
499
- }