@veilo/sdk-core 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (195) hide show
  1. package/README.md +42 -3
  2. package/SHIELD_INTEGRATION.md +143 -0
  3. package/config.d.ts +2 -0
  4. package/config.js +4 -0
  5. package/dist/cjs/client.d.ts +5 -5
  6. package/dist/cjs/client.js +79 -42
  7. package/dist/cjs/compactNote.d.ts +107 -0
  8. package/dist/cjs/compactNote.js +167 -0
  9. package/dist/cjs/events.d.ts +1 -1
  10. package/dist/cjs/events.js +2 -2
  11. package/dist/cjs/idl/privacy_pool.d.ts +5 -0
  12. package/dist/cjs/idl/privacy_pool.js +15218 -0
  13. package/dist/cjs/index.d.ts +16 -13
  14. package/dist/cjs/index.js +40 -30
  15. package/dist/cjs/merkle.d.ts +13 -0
  16. package/dist/cjs/merkle.js +31 -8
  17. package/dist/cjs/program.d.ts +11 -0
  18. package/dist/cjs/program.js +26 -3
  19. package/dist/cjs/proof.d.ts +1 -1
  20. package/dist/cjs/proof.js +37 -37
  21. package/dist/cjs/prover.d.ts +1 -1
  22. package/dist/cjs/prover.js +2 -2
  23. package/dist/cjs/random.d.ts +16 -0
  24. package/dist/cjs/random.js +28 -0
  25. package/dist/cjs/relayer.d.ts +27 -4
  26. package/dist/cjs/relayer.js +16 -5
  27. package/dist/cjs/shield/alt.d.ts +87 -0
  28. package/dist/cjs/shield/alt.js +194 -0
  29. package/dist/cjs/shield/computeBudget.d.ts +61 -0
  30. package/dist/cjs/shield/computeBudget.js +64 -0
  31. package/dist/cjs/shield/errors.d.ts +58 -0
  32. package/dist/cjs/shield/errors.js +121 -0
  33. package/dist/cjs/shield/finalize.d.ts +45 -0
  34. package/dist/cjs/shield/finalize.js +119 -0
  35. package/dist/cjs/shield/index.d.ts +35 -0
  36. package/dist/cjs/shield/index.js +68 -0
  37. package/dist/cjs/shield/ix.d.ts +54 -0
  38. package/dist/cjs/shield/ix.js +119 -0
  39. package/dist/cjs/shield/owner.d.ts +36 -0
  40. package/dist/cjs/shield/owner.js +126 -0
  41. package/dist/cjs/shield/ports.d.ts +43 -0
  42. package/dist/cjs/shield/ports.js +153 -0
  43. package/dist/cjs/shield/preflight.d.ts +30 -0
  44. package/dist/cjs/shield/preflight.js +154 -0
  45. package/dist/cjs/shield/shield.d.ts +68 -0
  46. package/dist/cjs/shield/shield.js +499 -0
  47. package/dist/cjs/shield/types.d.ts +202 -0
  48. package/dist/cjs/shield/types.js +2 -0
  49. package/dist/cjs/utxo.d.ts +20 -0
  50. package/dist/cjs/utxo.js +41 -28
  51. package/dist/esm/client.d.ts +407 -0
  52. package/dist/esm/client.js +9 -5
  53. package/dist/esm/compactNote.d.ts +107 -0
  54. package/dist/esm/compactNote.js +156 -0
  55. package/dist/esm/config.d.ts +82 -0
  56. package/dist/esm/events.d.ts +77 -0
  57. package/dist/esm/events.js +1 -1
  58. package/dist/esm/idl/privacy_pool.d.ts +5 -0
  59. package/dist/esm/idl/privacy_pool.js +15216 -0
  60. package/dist/esm/index.d.ts +16 -0
  61. package/dist/esm/index.js +18 -11
  62. package/dist/esm/merkle.d.ts +77 -0
  63. package/dist/esm/merkle.js +23 -1
  64. package/dist/esm/package.json +1 -0
  65. package/dist/esm/poseidon.d.ts +29 -0
  66. package/dist/esm/program.d.ts +37 -0
  67. package/dist/esm/program.js +23 -1
  68. package/dist/esm/proof.d.ts +183 -0
  69. package/dist/esm/proof.js +2 -2
  70. package/dist/esm/prover.d.ts +54 -0
  71. package/dist/esm/prover.js +1 -1
  72. package/dist/esm/random.d.ts +16 -0
  73. package/dist/esm/random.js +21 -0
  74. package/dist/esm/relayer.d.ts +318 -0
  75. package/dist/esm/relayer.js +16 -5
  76. package/dist/esm/retry.d.ts +32 -0
  77. package/dist/esm/shield/alt.d.ts +87 -0
  78. package/dist/esm/shield/alt.js +186 -0
  79. package/dist/esm/shield/computeBudget.d.ts +61 -0
  80. package/dist/esm/shield/computeBudget.js +61 -0
  81. package/dist/esm/shield/errors.d.ts +58 -0
  82. package/dist/esm/shield/errors.js +115 -0
  83. package/dist/esm/shield/finalize.d.ts +45 -0
  84. package/dist/esm/shield/finalize.js +83 -0
  85. package/dist/esm/shield/index.d.ts +35 -0
  86. package/dist/esm/shield/index.js +32 -0
  87. package/dist/esm/shield/ix.d.ts +54 -0
  88. package/dist/esm/shield/ix.js +82 -0
  89. package/dist/esm/shield/owner.d.ts +36 -0
  90. package/dist/esm/shield/owner.js +122 -0
  91. package/dist/esm/shield/ports.d.ts +43 -0
  92. package/dist/esm/shield/ports.js +147 -0
  93. package/dist/esm/shield/preflight.d.ts +30 -0
  94. package/dist/esm/shield/preflight.js +151 -0
  95. package/dist/esm/shield/shield.d.ts +68 -0
  96. package/dist/esm/shield/shield.js +492 -0
  97. package/dist/esm/shield/types.d.ts +202 -0
  98. package/dist/esm/shield/types.js +1 -0
  99. package/dist/esm/utxo.d.ts +235 -0
  100. package/dist/esm/utxo.js +18 -8
  101. package/dist/src/client.d.ts +407 -0
  102. package/dist/src/client.js +951 -0
  103. package/dist/src/compactNote.d.ts +107 -0
  104. package/dist/src/compactNote.js +167 -0
  105. package/dist/src/config.d.ts +82 -0
  106. package/dist/src/config.js +57 -0
  107. package/dist/src/events.d.ts +77 -0
  108. package/dist/src/events.js +167 -0
  109. package/dist/src/idl/privacy_pool.d.ts +5 -0
  110. package/dist/src/idl/privacy_pool.js +15218 -0
  111. package/dist/src/index.d.ts +16 -0
  112. package/dist/src/index.js +67 -0
  113. package/dist/src/merkle.d.ts +77 -0
  114. package/dist/src/merkle.js +156 -0
  115. package/dist/src/poseidon.d.ts +29 -0
  116. package/dist/src/poseidon.js +100 -0
  117. package/dist/src/program.d.ts +37 -0
  118. package/dist/src/program.js +61 -0
  119. package/dist/src/proof.d.ts +183 -0
  120. package/dist/src/proof.js +292 -0
  121. package/dist/src/prover.d.ts +54 -0
  122. package/dist/src/prover.js +112 -0
  123. package/dist/src/random.d.ts +16 -0
  124. package/dist/src/random.js +28 -0
  125. package/dist/src/relayer.d.ts +318 -0
  126. package/dist/src/relayer.js +257 -0
  127. package/dist/src/retry.d.ts +32 -0
  128. package/dist/src/retry.js +75 -0
  129. package/dist/src/shield/alt.d.ts +87 -0
  130. package/dist/src/shield/alt.js +194 -0
  131. package/dist/src/shield/computeBudget.d.ts +61 -0
  132. package/dist/src/shield/computeBudget.js +64 -0
  133. package/dist/src/shield/errors.d.ts +58 -0
  134. package/dist/src/shield/errors.js +121 -0
  135. package/dist/src/shield/finalize.d.ts +45 -0
  136. package/dist/src/shield/finalize.js +119 -0
  137. package/dist/src/shield/index.d.ts +35 -0
  138. package/dist/src/shield/index.js +68 -0
  139. package/dist/src/shield/ix.d.ts +54 -0
  140. package/dist/src/shield/ix.js +119 -0
  141. package/dist/src/shield/owner.d.ts +36 -0
  142. package/dist/src/shield/owner.js +126 -0
  143. package/dist/src/shield/ports.d.ts +43 -0
  144. package/dist/src/shield/ports.js +153 -0
  145. package/dist/src/shield/preflight.d.ts +30 -0
  146. package/dist/src/shield/preflight.js +154 -0
  147. package/dist/src/shield/shield.d.ts +68 -0
  148. package/dist/src/shield/shield.js +499 -0
  149. package/dist/src/shield/types.d.ts +202 -0
  150. package/dist/src/shield/types.js +2 -0
  151. package/dist/src/utxo.d.ts +235 -0
  152. package/dist/src/utxo.js +407 -0
  153. package/dist/tests/compact-note.test.d.ts +1 -0
  154. package/dist/tests/compact-note.test.js +173 -0
  155. package/dist/tests/config.test.d.ts +1 -0
  156. package/dist/tests/config.test.js +102 -0
  157. package/dist/tests/edge-cases.test.d.ts +1 -0
  158. package/dist/tests/edge-cases.test.js +220 -0
  159. package/dist/tests/encryption.test.d.ts +1 -0
  160. package/dist/tests/encryption.test.js +215 -0
  161. package/dist/tests/events.test.d.ts +1 -0
  162. package/dist/tests/events.test.js +78 -0
  163. package/dist/tests/multi-tree.test.d.ts +1 -0
  164. package/dist/tests/multi-tree.test.js +405 -0
  165. package/dist/tests/pda.test.d.ts +1 -0
  166. package/dist/tests/pda.test.js +229 -0
  167. package/dist/tests/poseidon-builder-parity.test.d.ts +1 -0
  168. package/dist/tests/poseidon-builder-parity.test.js +72 -0
  169. package/dist/tests/poseidon.test.d.ts +1 -0
  170. package/dist/tests/poseidon.test.js +142 -0
  171. package/dist/tests/proof.test.d.ts +1 -0
  172. package/dist/tests/proof.test.js +296 -0
  173. package/dist/tests/relayer.test.d.ts +1 -0
  174. package/dist/tests/relayer.test.js +271 -0
  175. package/dist/tests/sdk.integration.test.d.ts +1 -0
  176. package/dist/tests/sdk.integration.test.js +330 -0
  177. package/dist/tests/shield-owner.test.d.ts +1 -0
  178. package/dist/tests/shield-owner.test.js +89 -0
  179. package/dist/tests/shield-preflight.test.d.ts +1 -0
  180. package/dist/tests/shield-preflight.test.js +87 -0
  181. package/dist/tests/shield-realproof.test.d.ts +1 -0
  182. package/dist/tests/shield-realproof.test.js +272 -0
  183. package/dist/tests/shield.test.d.ts +1 -0
  184. package/dist/tests/shield.test.js +403 -0
  185. package/dist/tests/utxo.test.d.ts +1 -0
  186. package/dist/tests/utxo.test.js +140 -0
  187. package/package.json +69 -11
  188. package/poseidon.d.ts +2 -0
  189. package/poseidon.js +4 -0
  190. package/proof.d.ts +2 -0
  191. package/proof.js +4 -0
  192. package/prover.d.ts +2 -0
  193. package/prover.js +4 -0
  194. package/shield.d.ts +2 -0
  195. package/shield.js +4 -0
@@ -0,0 +1,167 @@
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.COMPACT_BLOB_LENGTH = void 0;
7
+ exports.createCompactNoteCipher = createCompactNoteCipher;
8
+ exports.decryptCompactNoteCipher = decryptCompactNoteCipher;
9
+ exports.matchesViewTag = matchesViewTag;
10
+ exports.toX25519Private = toX25519Private;
11
+ exports.emptyNoteCipher = emptyNoteCipher;
12
+ const tweetnacl_1 = __importDefault(require("tweetnacl"));
13
+ const utxo_js_1 = require("./utxo.js");
14
+ const random_js_1 = require("./random.js");
15
+ /**
16
+ * Compact note ciphers — the 80-byte payload carried inside the on-chain
17
+ * `transact` instruction (`note_ciphers: Option<NoteCiphers>`) and re-emitted in
18
+ * `CommitmentEvent`.
19
+ *
20
+ * This is what makes a note recoverable from chain data alone: a recipient who
21
+ * was never told about a deposit can still find it by scanning `view_tag` and
22
+ * decrypting the blob. Without it, note delivery depends on someone calling the
23
+ * relayer, which a third-party integrator cannot be trusted to do.
24
+ *
25
+ * ## Byte-compatibility is load-bearing
26
+ *
27
+ * Four sibling implementations already exist (relayer-server, extension ×2,
28
+ * wallet-app). This is the canonical one, and it MUST agree with them byte for
29
+ * byte in both directions:
30
+ *
31
+ * - disagree on encrypt → new notes are invisible to every existing client
32
+ * - disagree on decrypt → the entire historical note corpus becomes unreadable
33
+ *
34
+ * Three details are easy to get wrong and are individually fatal:
35
+ *
36
+ * 1. The KDF is `nacl.hash(x).slice(0, 32)` — **truncated SHA-512**, despite the
37
+ * sibling helper being named `sha256ForNotes`. See `kdfSHA512_256` in utxo.ts.
38
+ * 2. The ephemeral key published on-chain is **Ed25519**; it is converted to
39
+ * X25519 only for the Diffie-Hellman. Scanners convert it back.
40
+ * 3. `viewTag` is the first byte of the hash of the **raw shared secret**, not of
41
+ * the derived key. A wrong view tag makes every note invisible to the scan
42
+ * loop, which rejects ~255/256 candidates on this byte alone.
43
+ *
44
+ * The ECDH primitives are imported from `utxo.ts` rather than reimplemented — a
45
+ * fifth independent Edwards→Montgomery implementation is exactly how the current
46
+ * drift risk between the four siblings arose.
47
+ *
48
+ * Verified against the siblings by `tests/compact-note.test.ts` (golden vectors)
49
+ * and `scripts/verify-compact-cipher-parity.mjs` (differential).
50
+ */
51
+ /** Size of the on-chain compact blob: nonce[24] || secretbox(40-byte plaintext)[56]. */
52
+ exports.COMPACT_BLOB_LENGTH = 80;
53
+ /** Plaintext layout: blinding[32] || amount as little-endian u64[8]. */
54
+ const COMPACT_PLAINTEXT_LENGTH = 40;
55
+ const NONCE_LENGTH = 24;
56
+ /**
57
+ * Encrypt a note's secret material to a recipient's Solana wallet key.
58
+ *
59
+ * The recipient does not need to be the signer of the transaction, or to have
60
+ * any prior relationship with the sender — this is what makes shielding to an
61
+ * arbitrary owner possible.
62
+ *
63
+ * @param recipientWalletPubkey 32-byte Ed25519 Solana wallet public key
64
+ * @param blinding 32-byte big-endian blinding factor
65
+ * @param amount note amount in base units
66
+ */
67
+ function createCompactNoteCipher(recipientWalletPubkey, blinding, amount,
68
+ /** @internal */ __det) {
69
+ if (recipientWalletPubkey.length !== 32) {
70
+ throw new Error(`recipientWalletPubkey must be 32 bytes, got ${recipientWalletPubkey.length}`);
71
+ }
72
+ if (blinding.length < 32) {
73
+ throw new Error(`blinding must be at least 32 bytes, got ${blinding.length}`);
74
+ }
75
+ if (amount < 0n || amount > 0xffffffffffffffffn) {
76
+ throw new Error(`amount ${amount} does not fit in a u64`);
77
+ }
78
+ // Ed25519 ephemeral keypair. `nacl.sign.keyPair.fromSeed(seed)` produces the
79
+ // same public key as web3.js `Keypair.generate()` for the same seed, which is
80
+ // what the sibling implementations use — but without pulling @solana/web3.js
81
+ // into the crypto path.
82
+ const ephemeralSeed = __det?.ephemeralSeed ?? (0, random_js_1.randomBytes)(32);
83
+ const ephemeralPublicKey = tweetnacl_1.default.sign.keyPair.fromSeed(ephemeralSeed).publicKey;
84
+ const sharedSecret = deriveSharedSecret(ephemeralSeed, recipientWalletPubkey);
85
+ const key = (0, utxo_js_1.kdfSHA512_256)(sharedSecret);
86
+ const plaintext = new Uint8Array(COMPACT_PLAINTEXT_LENGTH);
87
+ plaintext.set(blinding.slice(0, 32), 0);
88
+ let amt = amount;
89
+ for (let i = 0; i < 8; i++) {
90
+ plaintext[32 + i] = Number(amt & 0xffn);
91
+ amt >>= 8n;
92
+ }
93
+ const nonce = __det?.nonce ?? tweetnacl_1.default.randomBytes(NONCE_LENGTH);
94
+ const encrypted = tweetnacl_1.default.secretbox(plaintext, nonce, key);
95
+ const compactBlob = new Uint8Array(exports.COMPACT_BLOB_LENGTH);
96
+ compactBlob.set(nonce, 0);
97
+ compactBlob.set(encrypted, NONCE_LENGTH);
98
+ // First byte of the hash of the RAW shared secret — not of `key`.
99
+ const viewTag = tweetnacl_1.default.hash(sharedSecret)[0];
100
+ return { ephemeralPublicKey, compactBlob, viewTag };
101
+ }
102
+ /**
103
+ * Recover a note's blinding and amount from an on-chain compact cipher.
104
+ *
105
+ * Returns `null` when the blob was not encrypted to this key — the expected
106
+ * outcome for the overwhelming majority of candidates during a scan, so callers
107
+ * should filter on `viewTag` first (see {@link matchesViewTag}) and only attempt
108
+ * decryption on the ~1/256 that survive.
109
+ *
110
+ * @param walletSecretKey 32-byte Ed25519 seed or 64-byte secret key
111
+ */
112
+ function decryptCompactNoteCipher(walletSecretKey, ephemeralPublicKey, compactBlob, precomputed) {
113
+ if (compactBlob.length !== exports.COMPACT_BLOB_LENGTH)
114
+ return null;
115
+ const sharedSecret = precomputed?.sharedSecret ??
116
+ (precomputed?.x25519Private
117
+ ? tweetnacl_1.default.scalarMult(precomputed.x25519Private, (0, utxo_js_1.ed25519PublicKeyToX25519)(ephemeralPublicKey))
118
+ : deriveSharedSecret(walletSecretKey, ephemeralPublicKey));
119
+ const key = (0, utxo_js_1.kdfSHA512_256)(sharedSecret);
120
+ const nonce = compactBlob.slice(0, NONCE_LENGTH);
121
+ const encrypted = compactBlob.slice(NONCE_LENGTH);
122
+ const plaintext = tweetnacl_1.default.secretbox.open(encrypted, nonce, key);
123
+ if (!plaintext)
124
+ return null;
125
+ const blinding = new Uint8Array(plaintext.slice(0, 32));
126
+ let amount = 0n;
127
+ for (let i = 7; i >= 0; i--) {
128
+ amount = (amount << 8n) | BigInt(plaintext[32 + i]);
129
+ }
130
+ return { blinding, amount };
131
+ }
132
+ /**
133
+ * Cheap scan filter: does this on-chain cipher plausibly belong to us?
134
+ *
135
+ * Rejects ~255/256 candidates for the cost of one scalar multiplication and one
136
+ * hash, versus a full secretbox open. Hoist `x25519Private` across a scan loop
137
+ * with {@link toX25519Private}.
138
+ */
139
+ function matchesViewTag(walletSecretKey, ephemeralPublicKey, viewTag, precomputed) {
140
+ const sharedSecret = precomputed?.x25519Private
141
+ ? tweetnacl_1.default.scalarMult(precomputed.x25519Private, (0, utxo_js_1.ed25519PublicKeyToX25519)(ephemeralPublicKey))
142
+ : deriveSharedSecret(walletSecretKey, ephemeralPublicKey);
143
+ return { matches: tweetnacl_1.default.hash(sharedSecret)[0] === viewTag, sharedSecret };
144
+ }
145
+ /**
146
+ * Convert an Ed25519 wallet secret key to its X25519 scalar once, for reuse
147
+ * across a scan loop.
148
+ */
149
+ function toX25519Private(walletSecretKey) {
150
+ return (0, utxo_js_1.ed25519SeedToX25519Private)(seedOf(walletSecretKey));
151
+ }
152
+ /** The all-zero cipher the program treats as "no note here". */
153
+ function emptyNoteCipher() {
154
+ return {
155
+ ephemeralPublicKey: new Uint8Array(32),
156
+ compactBlob: new Uint8Array(exports.COMPACT_BLOB_LENGTH),
157
+ viewTag: 0,
158
+ };
159
+ }
160
+ // -- internal -----------------------------------------------------------------
161
+ /** Accept either a 32-byte Ed25519 seed or a 64-byte secret key, as the siblings do. */
162
+ function seedOf(secretKey) {
163
+ return secretKey.length === 64 ? secretKey.slice(0, 32) : secretKey;
164
+ }
165
+ function deriveSharedSecret(mySecretKey, theirEd25519PublicKey) {
166
+ return tweetnacl_1.default.scalarMult((0, utxo_js_1.ed25519SeedToX25519Private)(seedOf(mySecretKey)), (0, utxo_js_1.ed25519PublicKeyToX25519)(theirEd25519PublicKey));
167
+ }
@@ -1,6 +1,6 @@
1
1
  import { PublicKey } from "@solana/web3.js";
2
2
  import { Program, Idl } from "@coral-xyz/anchor";
3
- import { MerkleTree } from "./merkle";
3
+ import { MerkleTree } from "./merkle.js";
4
4
  /**
5
5
  * On-chain CommitmentEvent emitted by `transact` and `transact_swap`.
6
6
  * Mirrors the IDL CommitmentEvent struct.
@@ -37,7 +37,7 @@ exports.scanCommitmentEvents = scanCommitmentEvents;
37
37
  exports.scanNullifierEvents = scanNullifierEvents;
38
38
  exports.buildTreeFromEvents = buildTreeFromEvents;
39
39
  const anchor = __importStar(require("@coral-xyz/anchor"));
40
- const merkle_1 = require("./merkle");
40
+ const merkle_js_1 = require("./merkle.js");
41
41
  // -----------------------------------------------------------------------------
42
42
  // Event Scanning
43
43
  // -----------------------------------------------------------------------------
@@ -156,7 +156,7 @@ async function buildTreeFromEvents(params) {
156
156
  const treeEvents = allEvents
157
157
  .filter((e) => e.treeId === treeId)
158
158
  .sort((a, b) => a.leafIndex - b.leafIndex);
159
- const tree = new merkle_1.MerkleTree(depth);
159
+ const tree = new merkle_js_1.MerkleTree(depth);
160
160
  for (const ev of treeEvents) {
161
161
  tree.insert(ev.commitment);
162
162
  }
@@ -0,0 +1,5 @@
1
+ import type { Idl } from "@coral-xyz/anchor";
2
+ declare const IDL_JSON: Idl & {
3
+ address: string;
4
+ };
5
+ export default IDL_JSON;