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