@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 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { Program, Idl } from "@coral-xyz/anchor";
|
|
2
|
+
import { PublicKey, Transaction } from "@solana/web3.js";
|
|
3
|
+
import type { ExtData, TransactionProofBuilder } from "../proofs/types.js";
|
|
4
|
+
import type { SerializedUTXO } from "../notes/model.js";
|
|
5
|
+
import { MerkleTree } from "../merkle.js";
|
|
6
|
+
import { type NoteCiphers } from "../compactNote.js";
|
|
7
|
+
/**
|
|
8
|
+
* Result of a deposit operation, produced by `DepositBuild.commit()` after the
|
|
9
|
+
* caller has successfully submitted the deposit transaction on-chain.
|
|
10
|
+
*/
|
|
11
|
+
export type DepositResult = {
|
|
12
|
+
/** The output UTXOs created by the deposit */
|
|
13
|
+
outputUTXOs: [SerializedUTXO, SerializedUTXO];
|
|
14
|
+
/** The leaf indices where the UTXOs were inserted */
|
|
15
|
+
leafIndices: [number, number];
|
|
16
|
+
/** The new Merkle root after insertion */
|
|
17
|
+
root: Uint8Array;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* The return of `deposit()`. Instead of submitting the transaction itself,
|
|
21
|
+
* `deposit()` hands back an unsigned {@link Transaction} so the caller can
|
|
22
|
+
* sign it with whatever wallet they control (Solana wallet-adapter, Ledger,
|
|
23
|
+
* anchor NodeWallet, etc.) and submit it on their own terms.
|
|
24
|
+
*
|
|
25
|
+
* Usage pattern (browser / wallet-adapter):
|
|
26
|
+
*
|
|
27
|
+
* const { transaction, commit } = await deposit({ ... });
|
|
28
|
+
* const signed = await wallet.signTransaction(transaction);
|
|
29
|
+
* const sig = await connection.sendRawTransaction(signed.serialize());
|
|
30
|
+
* await connection.confirmTransaction(sig, "confirmed");
|
|
31
|
+
* const result = commit(); // updates the local tree, returns DepositResult
|
|
32
|
+
*/
|
|
33
|
+
export type DepositBuild = {
|
|
34
|
+
/**
|
|
35
|
+
* Unsigned {@link Transaction} for the deposit. `feePayer` and
|
|
36
|
+
* `recentBlockhash` are already populated, so the caller only needs to sign
|
|
37
|
+
* and send.
|
|
38
|
+
*/
|
|
39
|
+
transaction: Transaction;
|
|
40
|
+
/**
|
|
41
|
+
* Output UTXOs created by this deposit. Safe to read before submission —
|
|
42
|
+
* the commitment values are deterministic from the circuit inputs and do
|
|
43
|
+
* not depend on the on-chain result.
|
|
44
|
+
*/
|
|
45
|
+
outputUTXOs: [SerializedUTXO, SerializedUTXO];
|
|
46
|
+
/**
|
|
47
|
+
* Inserts the two output commitments into the local {@link MerkleTree} and
|
|
48
|
+
* returns the receipt. Call this **only after** the on-chain transaction
|
|
49
|
+
* has been confirmed — deferring the insert keeps the local tree
|
|
50
|
+
* consistent with chain state if the submission fails.
|
|
51
|
+
*
|
|
52
|
+
* Calling `commit()` more than once is unsupported and will corrupt the
|
|
53
|
+
* local tree.
|
|
54
|
+
*/
|
|
55
|
+
commit: () => DepositResult;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Build an unsigned deposit {@link Transaction} for the privacy pool.
|
|
59
|
+
*
|
|
60
|
+
* For deposits, we use zero input UTXOs (amount=0) and create output UTXOs
|
|
61
|
+
* with the deposit amount. `publicAmount` is positive. The caller is
|
|
62
|
+
* responsible for signing and submitting the returned transaction, and then
|
|
63
|
+
* calling `commit()` once it confirms.
|
|
64
|
+
*
|
|
65
|
+
* @param amount - Amount to deposit in lamports/token units
|
|
66
|
+
* @param recipientPubkey - UTXO public key that will own the deposited funds
|
|
67
|
+
* @param treeId - The tree ID to deposit into (defaults to 0)
|
|
68
|
+
*/
|
|
69
|
+
export declare function deposit<T extends Idl>(params: {
|
|
70
|
+
program: Program<T>;
|
|
71
|
+
/**
|
|
72
|
+
* Account that will pay for and sign the deposit transaction. Only
|
|
73
|
+
* `publicKey` is read — `deposit()` never touches a secret key. A browser
|
|
74
|
+
* wallet-adapter wallet, a Node `anchor.Wallet`, or a raw `Keypair` are all
|
|
75
|
+
* structurally acceptable.
|
|
76
|
+
*/
|
|
77
|
+
depositor: {
|
|
78
|
+
publicKey: PublicKey;
|
|
79
|
+
};
|
|
80
|
+
amount: bigint;
|
|
81
|
+
mintAddress: PublicKey;
|
|
82
|
+
recipientPubkey: bigint;
|
|
83
|
+
tree: MerkleTree;
|
|
84
|
+
proofBuilder: TransactionProofBuilder;
|
|
85
|
+
treeId?: number;
|
|
86
|
+
extData?: Partial<ExtData>;
|
|
87
|
+
/** Solana wallet key used to make output note 0 recoverable from chain events. */
|
|
88
|
+
noteRecipientWallet?: PublicKey | Uint8Array;
|
|
89
|
+
/** Prebuilt recovery payload. Takes precedence over noteRecipientWallet. */
|
|
90
|
+
noteCiphers?: NoteCiphers | null;
|
|
91
|
+
}): Promise<DepositBuild>;
|
|
92
|
+
/**
|
|
93
|
+
* Result of a withdrawal operation.
|
|
94
|
+
*/
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import * as anchor from "@coral-xyz/anchor";
|
|
2
|
+
import { ComputeBudgetProgram, PublicKey, SystemProgram, } from "@solana/web3.js";
|
|
3
|
+
import { TOKEN_PROGRAM_ID, getAssociatedTokenAddress } from "@solana/spl-token";
|
|
4
|
+
import { computeExtDataHash, prepareTransactionInputs, } from "../proofs/transaction.js";
|
|
5
|
+
import { encodeSnarkjsProofToTransactionProof } from "../proofs/encoding.js";
|
|
6
|
+
import { createOwnedZeroUTXO, createUTXO, generateKeypair, } from "../notes/model.js";
|
|
7
|
+
import { NATIVE_SOL_MINT } from "../config.js";
|
|
8
|
+
import { bigIntToBytesBE } from "../poseidon.js";
|
|
9
|
+
import { createNoteCiphers, } from "../compactNote.js";
|
|
10
|
+
import { getGlobalConfigPda, getNoteTreePda, getNullifierMarkerPda, getPoolPdas, } from "../accounts/pdas.js";
|
|
11
|
+
// Anchor is CJS and does not expose BN as a detectable Node ESM named export.
|
|
12
|
+
const { BN } = anchor;
|
|
13
|
+
/**
|
|
14
|
+
* Build an unsigned deposit {@link Transaction} for the privacy pool.
|
|
15
|
+
*
|
|
16
|
+
* For deposits, we use zero input UTXOs (amount=0) and create output UTXOs
|
|
17
|
+
* with the deposit amount. `publicAmount` is positive. The caller is
|
|
18
|
+
* responsible for signing and submitting the returned transaction, and then
|
|
19
|
+
* calling `commit()` once it confirms.
|
|
20
|
+
*
|
|
21
|
+
* @param amount - Amount to deposit in lamports/token units
|
|
22
|
+
* @param recipientPubkey - UTXO public key that will own the deposited funds
|
|
23
|
+
* @param treeId - The tree ID to deposit into (defaults to 0)
|
|
24
|
+
*/
|
|
25
|
+
export async function deposit(params) {
|
|
26
|
+
const { program, depositor, amount, mintAddress, recipientPubkey, tree, proofBuilder, treeId = 0, extData: partialExtData, noteRecipientWallet, noteCiphers, } = params;
|
|
27
|
+
if (amount <= 0n) {
|
|
28
|
+
throw new Error("Deposit amount must be positive");
|
|
29
|
+
}
|
|
30
|
+
// Create zero input UTXOs (for deposits, we don't spend any existing UTXOs)
|
|
31
|
+
const zeroKeypair0 = generateKeypair();
|
|
32
|
+
const zeroKeypair1 = generateKeypair();
|
|
33
|
+
const zeroInput0 = createOwnedZeroUTXO(zeroKeypair0.privateKey, mintAddress);
|
|
34
|
+
const zeroInput1 = createOwnedZeroUTXO(zeroKeypair1.privateKey, mintAddress);
|
|
35
|
+
// The circuit skips the Merkle proof check for zero-amount inputs (amount == 0
|
|
36
|
+
// causes `enabled = 0` in MerkleProofIfEnabled). So we provide a dummy path using
|
|
37
|
+
// the tree's pre-computed zero chain without inserting anything into the tree.
|
|
38
|
+
const dummyPath = tree.zeroes.slice(0, tree.depth);
|
|
39
|
+
const currentRoot = tree.root;
|
|
40
|
+
const inputUTXOs = [
|
|
41
|
+
{
|
|
42
|
+
...zeroInput0,
|
|
43
|
+
pathIndex: 0,
|
|
44
|
+
pathElements: dummyPath,
|
|
45
|
+
privateKey: zeroKeypair0.privateKey,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
...zeroInput1,
|
|
49
|
+
pathIndex: 0,
|
|
50
|
+
pathElements: dummyPath,
|
|
51
|
+
privateKey: zeroKeypair1.privateKey,
|
|
52
|
+
},
|
|
53
|
+
];
|
|
54
|
+
const outputUTXO0 = createUTXO({
|
|
55
|
+
amount: amount,
|
|
56
|
+
pubkey: recipientPubkey,
|
|
57
|
+
mintAddress,
|
|
58
|
+
});
|
|
59
|
+
const outputUTXO1 = createUTXO({
|
|
60
|
+
amount: 0n,
|
|
61
|
+
pubkey: generateKeypair().publicKey,
|
|
62
|
+
mintAddress,
|
|
63
|
+
});
|
|
64
|
+
const outputUTXOs = [
|
|
65
|
+
outputUTXO0,
|
|
66
|
+
outputUTXO1,
|
|
67
|
+
];
|
|
68
|
+
const depositorPubkey = depositor.publicKey;
|
|
69
|
+
const extData = {
|
|
70
|
+
recipient: partialExtData?.recipient ?? depositorPubkey,
|
|
71
|
+
relayer: partialExtData?.relayer ?? depositorPubkey,
|
|
72
|
+
fee: partialExtData?.fee ?? 0n,
|
|
73
|
+
refund: partialExtData?.refund ?? 0n,
|
|
74
|
+
claimant: partialExtData?.claimant ?? SystemProgram.programId,
|
|
75
|
+
};
|
|
76
|
+
if (!extData.relayer.equals(depositorPubkey)) {
|
|
77
|
+
throw new Error("Deposit extData.relayer must be the signing depositor");
|
|
78
|
+
}
|
|
79
|
+
if (extData.fee !== 0n || extData.refund !== 0n) {
|
|
80
|
+
throw new Error("Deposits require extData fee and refund to be zero");
|
|
81
|
+
}
|
|
82
|
+
const circuitInputs = prepareTransactionInputs({
|
|
83
|
+
inputUTXOs,
|
|
84
|
+
outputUTXOs,
|
|
85
|
+
root: currentRoot,
|
|
86
|
+
publicAmount: amount, // Positive for deposits
|
|
87
|
+
extData,
|
|
88
|
+
mintAddress,
|
|
89
|
+
});
|
|
90
|
+
const rawProof = await proofBuilder(circuitInputs);
|
|
91
|
+
const proof = encodeSnarkjsProofToTransactionProof(rawProof);
|
|
92
|
+
const depositNoteCiphers = noteCiphers !== undefined
|
|
93
|
+
? noteCiphers
|
|
94
|
+
: noteRecipientWallet
|
|
95
|
+
? createNoteCiphers({
|
|
96
|
+
recipientWalletPubkey: noteRecipientWallet instanceof PublicKey
|
|
97
|
+
? noteRecipientWallet.toBytes()
|
|
98
|
+
: noteRecipientWallet,
|
|
99
|
+
blinding: bigIntToBytesBE(outputUTXO0.blinding),
|
|
100
|
+
amount: outputUTXO0.amount,
|
|
101
|
+
}, null)
|
|
102
|
+
: null;
|
|
103
|
+
// -----------------------------------------------------------------------
|
|
104
|
+
// Build the transact instruction inline because deposits return an unsigned
|
|
105
|
+
// wallet-signable transaction while `transact()` submits with a Keypair.
|
|
106
|
+
//
|
|
107
|
+
// We intentionally don't reuse the `transact()` helper: it submits via
|
|
108
|
+
// anchor's `.rpc()` with `.signers([relayer])`, which assumes the caller
|
|
109
|
+
// holds a real Keypair. For deposit, we want to return an unsigned
|
|
110
|
+
// Transaction so the caller (typically a browser wallet) can sign it.
|
|
111
|
+
// -----------------------------------------------------------------------
|
|
112
|
+
const { config, vault, nullifiers } = getPoolPdas(program.programId, mintAddress);
|
|
113
|
+
const globalConfig = getGlobalConfigPda(program.programId);
|
|
114
|
+
const inputTree = getNoteTreePda(program.programId, mintAddress, treeId);
|
|
115
|
+
const outputTree = getNoteTreePda(program.programId, mintAddress, treeId);
|
|
116
|
+
const nullifierMarker0 = getNullifierMarkerPda(program.programId, mintAddress, circuitInputs.inputNullifiers[0]);
|
|
117
|
+
const nullifierMarker1 = getNullifierMarkerPda(program.programId, mintAddress, circuitInputs.inputNullifiers[1]);
|
|
118
|
+
const extDataHash = computeExtDataHash(extData);
|
|
119
|
+
const isNativeSol = mintAddress.equals(NATIVE_SOL_MINT);
|
|
120
|
+
let vaultTA;
|
|
121
|
+
let userTA;
|
|
122
|
+
let recipientTA;
|
|
123
|
+
let relayerTA;
|
|
124
|
+
if (!isNativeSol) {
|
|
125
|
+
vaultTA = await getAssociatedTokenAddress(mintAddress, vault, true);
|
|
126
|
+
userTA = await getAssociatedTokenAddress(mintAddress, depositorPubkey);
|
|
127
|
+
recipientTA = await getAssociatedTokenAddress(mintAddress, extData.recipient);
|
|
128
|
+
relayerTA = await getAssociatedTokenAddress(mintAddress, depositorPubkey);
|
|
129
|
+
}
|
|
130
|
+
const builtTx = await program.methods
|
|
131
|
+
.transact(Array.from(currentRoot), treeId, treeId, new BN(amount.toString()), Array.from(extDataHash), mintAddress, Array.from(circuitInputs.inputNullifiers[0]), Array.from(circuitInputs.inputNullifiers[1]), Array.from(circuitInputs.outputCommitments[0]), Array.from(circuitInputs.outputCommitments[1]), new BN(Math.floor(Date.now() / 1000) + 3600), // 1-hour window from build time
|
|
132
|
+
{
|
|
133
|
+
recipient: extData.recipient,
|
|
134
|
+
relayer: extData.relayer,
|
|
135
|
+
fee: new BN(extData.fee.toString()),
|
|
136
|
+
refund: new BN(extData.refund.toString()),
|
|
137
|
+
claimant: extData.claimant,
|
|
138
|
+
}, {
|
|
139
|
+
proofA: proof.proofA,
|
|
140
|
+
proofB: proof.proofB,
|
|
141
|
+
proofC: proof.proofC,
|
|
142
|
+
}, depositNoteCiphers
|
|
143
|
+
? {
|
|
144
|
+
note0EphemeralKey: Array.from(depositNoteCiphers.note0EphemeralKey),
|
|
145
|
+
note0Encrypted: Array.from(depositNoteCiphers.note0Encrypted),
|
|
146
|
+
note0ViewTag: depositNoteCiphers.note0ViewTag,
|
|
147
|
+
note1EphemeralKey: Array.from(depositNoteCiphers.note1EphemeralKey),
|
|
148
|
+
note1Encrypted: Array.from(depositNoteCiphers.note1Encrypted),
|
|
149
|
+
note1ViewTag: depositNoteCiphers.note1ViewTag,
|
|
150
|
+
}
|
|
151
|
+
: null)
|
|
152
|
+
.accountsStrict({
|
|
153
|
+
config,
|
|
154
|
+
globalConfig,
|
|
155
|
+
vault,
|
|
156
|
+
inputTree,
|
|
157
|
+
outputTree,
|
|
158
|
+
nullifiers,
|
|
159
|
+
nullifierMarker0,
|
|
160
|
+
nullifierMarker1,
|
|
161
|
+
relayer: depositorPubkey,
|
|
162
|
+
recipient: extData.recipient,
|
|
163
|
+
vaultTokenAccount: vaultTA ?? depositorPubkey,
|
|
164
|
+
userTokenAccount: userTA ?? depositorPubkey,
|
|
165
|
+
recipientTokenAccount: recipientTA ?? extData.recipient,
|
|
166
|
+
relayerTokenAccount: relayerTA ?? depositorPubkey,
|
|
167
|
+
tokenProgram: isNativeSol ? SystemProgram.programId : TOKEN_PROGRAM_ID,
|
|
168
|
+
systemProgram: SystemProgram.programId,
|
|
169
|
+
})
|
|
170
|
+
.transaction();
|
|
171
|
+
// Add compute budget instruction for higher CU limit
|
|
172
|
+
const computeBudgetIx = ComputeBudgetProgram.setComputeUnitLimit({
|
|
173
|
+
units: 1000000,
|
|
174
|
+
});
|
|
175
|
+
builtTx.instructions.unshift(computeBudgetIx);
|
|
176
|
+
// Populate feePayer + recentBlockhash so the wallet only has to sign.
|
|
177
|
+
const provider = program.provider;
|
|
178
|
+
builtTx.feePayer = depositorPubkey;
|
|
179
|
+
const { blockhash } = await provider.connection.getLatestBlockhash();
|
|
180
|
+
builtTx.recentBlockhash = blockhash;
|
|
181
|
+
const commit = () => {
|
|
182
|
+
const { index: outIdx0 } = tree.insert(outputUTXOs[0].commitment);
|
|
183
|
+
const { index: outIdx1, root: newRoot } = tree.insert(outputUTXOs[1].commitment);
|
|
184
|
+
return {
|
|
185
|
+
outputUTXOs,
|
|
186
|
+
leafIndices: [outIdx0, outIdx1],
|
|
187
|
+
root: newRoot,
|
|
188
|
+
};
|
|
189
|
+
};
|
|
190
|
+
return {
|
|
191
|
+
transaction: builtTx,
|
|
192
|
+
outputUTXOs,
|
|
193
|
+
commit,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Result of a withdrawal operation.
|
|
198
|
+
*/
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./transact.js";
|
|
2
|
+
export * from "./deposit.js";
|
|
3
|
+
export * from "./withdraw.js";
|
|
4
|
+
export * from "./transfer.js";
|
|
5
|
+
export * from "./swap.js";
|
|
6
|
+
export { buildTransactInstruction as buildRawTransactInstruction, TRANSACT_ACCOUNT_NAMES, } from "../shield/ix.js";
|
|
7
|
+
export type { TransactAccounts as RawTransactAccounts, TransactArgs as RawTransactArgs, } from "../shield/ix.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./transact.js";
|
|
2
|
+
export * from "./deposit.js";
|
|
3
|
+
export * from "./withdraw.js";
|
|
4
|
+
export * from "./transfer.js";
|
|
5
|
+
export * from "./swap.js";
|
|
6
|
+
// Raw IDL-correct builder for custom v0/ALT transaction composition.
|
|
7
|
+
export { buildTransactInstruction as buildRawTransactInstruction, TRANSACT_ACCOUNT_NAMES, } from "../shield/ix.js";
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { Idl, Program } from "@coral-xyz/anchor";
|
|
2
|
+
import { Keypair, PublicKey, type AccountMeta, type TransactionInstruction } from "@solana/web3.js";
|
|
3
|
+
import type { ExtData, TransactionProofStruct } from "../proofs/types.js";
|
|
4
|
+
import type { NoteCiphers } from "../compactNote.js";
|
|
5
|
+
/** Seeds: ["swap_executor", source_mint, dest_mint, nullifier_0, relayer]. */
|
|
6
|
+
export declare function getSwapExecutorPda(programId: PublicKey, sourceMint: PublicKey, destMint: PublicKey, inputNullifier0: Uint8Array, relayer: PublicKey): PublicKey;
|
|
7
|
+
/** Build `fund_native_source`. It must immediately precede `transact_swap`. */
|
|
8
|
+
export declare function fundNativeSource<T extends Idl>(params: {
|
|
9
|
+
program: Program<T>;
|
|
10
|
+
relayer: {
|
|
11
|
+
publicKey: PublicKey;
|
|
12
|
+
};
|
|
13
|
+
sourceMint: PublicKey;
|
|
14
|
+
destMint: PublicKey;
|
|
15
|
+
inputNullifier0: Uint8Array;
|
|
16
|
+
swapAmount: bigint;
|
|
17
|
+
/** Wrapped-native mint used by the DEX. Defaults to canonical WSOL. */
|
|
18
|
+
sourceMintAccount?: PublicKey;
|
|
19
|
+
}): Promise<TransactionInstruction>;
|
|
20
|
+
export type SwapProofStruct = TransactionProofStruct;
|
|
21
|
+
/** Fields in the current IDL's `SwapParams` struct. */
|
|
22
|
+
export type SwapParams = {
|
|
23
|
+
minAmountOut: bigint;
|
|
24
|
+
deadline: bigint;
|
|
25
|
+
destAmount: bigint;
|
|
26
|
+
/** SHA-256 of the exact DEX instruction bytes passed as `swapData`. */
|
|
27
|
+
swapDataHash: Uint8Array;
|
|
28
|
+
};
|
|
29
|
+
export type TransactSwapParams<T extends Idl = Idl> = {
|
|
30
|
+
program: Program<T>;
|
|
31
|
+
relayer: {
|
|
32
|
+
publicKey: PublicKey;
|
|
33
|
+
};
|
|
34
|
+
sourceMint: PublicKey;
|
|
35
|
+
destMint: PublicKey;
|
|
36
|
+
sourceRoot: Uint8Array;
|
|
37
|
+
sourceTreeId: number;
|
|
38
|
+
destTreeId: number;
|
|
39
|
+
inputNullifiers: [Uint8Array, Uint8Array];
|
|
40
|
+
/** Output 0 is source-mint change; output 1 is the destination note. */
|
|
41
|
+
outputCommitments: [Uint8Array, Uint8Array];
|
|
42
|
+
proof: SwapProofStruct;
|
|
43
|
+
swapParams: SwapParams;
|
|
44
|
+
swapAmount: bigint;
|
|
45
|
+
/** Exact raw data from the DEX/Jupiter instruction. */
|
|
46
|
+
swapData: Uint8Array | Buffer;
|
|
47
|
+
extData: ExtData;
|
|
48
|
+
noteCiphers?: NoteCiphers | null;
|
|
49
|
+
sourceVaultTokenAccount: PublicKey;
|
|
50
|
+
/** Actual DEX source mint (WSOL for the native-SOL pool sentinel). */
|
|
51
|
+
sourceMintAccount: PublicKey;
|
|
52
|
+
destVaultTokenAccount: PublicKey;
|
|
53
|
+
/** Actual DEX destination mint (WSOL for the native-SOL pool sentinel). */
|
|
54
|
+
destMintAccount: PublicKey;
|
|
55
|
+
relayerTokenAccount: PublicKey;
|
|
56
|
+
swapProgram: PublicKey;
|
|
57
|
+
jupiterEventAuthority: PublicKey;
|
|
58
|
+
/** Preserve the DEX instruction's account order and duplicates exactly. */
|
|
59
|
+
remainingAccounts?: AccountMeta[];
|
|
60
|
+
};
|
|
61
|
+
/** Build the current IDL's `transact_swap` instruction without submitting it. */
|
|
62
|
+
export declare function buildTransactSwapInstruction<T extends Idl>(params: TransactSwapParams<T>): Promise<TransactionInstruction>;
|
|
63
|
+
/** Build the atomic instruction sequence, including native-source funding. */
|
|
64
|
+
export declare function buildPrivateSwapInstructions<T extends Idl>(params: TransactSwapParams<T>): Promise<TransactionInstruction[]>;
|
|
65
|
+
/**
|
|
66
|
+
* Submit the parity-correct instructions as a legacy transaction. For Jupiter
|
|
67
|
+
* routes, prefer `buildPrivateSwapInstructions` and compile v0 with route ALTs.
|
|
68
|
+
*/
|
|
69
|
+
export declare function transactSwap<T extends Idl>(params: TransactSwapParams<T> & {
|
|
70
|
+
relayer: Keypair;
|
|
71
|
+
}): Promise<string>;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import * as anchor from "@coral-xyz/anchor";
|
|
2
|
+
import { PublicKey, SYSVAR_INSTRUCTIONS_PUBKEY, SystemProgram, Transaction, } from "@solana/web3.js";
|
|
3
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, NATIVE_MINT, TOKEN_PROGRAM_ID, getAssociatedTokenAddress, } from "@solana/spl-token";
|
|
4
|
+
import { NATIVE_SOL_MINT } from "../config.js";
|
|
5
|
+
import { withRpcRetry } from "../retry.js";
|
|
6
|
+
import { getGlobalConfigPda, getNoteTreePda, getNullifierMarkerPda, getPoolPdas, } from "../accounts/pdas.js";
|
|
7
|
+
const { BN } = anchor;
|
|
8
|
+
/** Seeds: ["swap_executor", source_mint, dest_mint, nullifier_0, relayer]. */
|
|
9
|
+
export function getSwapExecutorPda(programId, sourceMint, destMint, inputNullifier0, relayer) {
|
|
10
|
+
const [executor] = PublicKey.findProgramAddressSync([
|
|
11
|
+
Buffer.from("swap_executor"),
|
|
12
|
+
sourceMint.toBuffer(),
|
|
13
|
+
destMint.toBuffer(),
|
|
14
|
+
inputNullifier0,
|
|
15
|
+
relayer.toBuffer(),
|
|
16
|
+
], programId);
|
|
17
|
+
return executor;
|
|
18
|
+
}
|
|
19
|
+
/** Build `fund_native_source`. It must immediately precede `transact_swap`. */
|
|
20
|
+
export async function fundNativeSource(params) {
|
|
21
|
+
const { program, relayer, sourceMint, destMint, inputNullifier0, swapAmount, sourceMintAccount = NATIVE_MINT, } = params;
|
|
22
|
+
const { vault: sourceVault, config: sourceConfig } = getPoolPdas(program.programId, sourceMint);
|
|
23
|
+
const executor = getSwapExecutorPda(program.programId, sourceMint, destMint, inputNullifier0, relayer.publicKey);
|
|
24
|
+
const executorSourceToken = await getAssociatedTokenAddress(sourceMintAccount, executor, true);
|
|
25
|
+
return program.methods
|
|
26
|
+
.fundNativeSource(sourceMint, destMint, Array.from(inputNullifier0), new BN(swapAmount.toString()))
|
|
27
|
+
.accountsStrict({
|
|
28
|
+
executor,
|
|
29
|
+
executorSourceToken,
|
|
30
|
+
sourceVault,
|
|
31
|
+
sourceConfig,
|
|
32
|
+
sourceMintAccount,
|
|
33
|
+
relayer: relayer.publicKey,
|
|
34
|
+
instructionsSysvar: SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
35
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
36
|
+
systemProgram: SystemProgram.programId,
|
|
37
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
38
|
+
})
|
|
39
|
+
.instruction();
|
|
40
|
+
}
|
|
41
|
+
function encodeNoteCiphers(noteCiphers) {
|
|
42
|
+
return noteCiphers
|
|
43
|
+
? {
|
|
44
|
+
note0EphemeralKey: Array.from(noteCiphers.note0EphemeralKey),
|
|
45
|
+
note0Encrypted: Array.from(noteCiphers.note0Encrypted),
|
|
46
|
+
note0ViewTag: noteCiphers.note0ViewTag,
|
|
47
|
+
note1EphemeralKey: Array.from(noteCiphers.note1EphemeralKey),
|
|
48
|
+
note1Encrypted: Array.from(noteCiphers.note1Encrypted),
|
|
49
|
+
note1ViewTag: noteCiphers.note1ViewTag,
|
|
50
|
+
}
|
|
51
|
+
: null;
|
|
52
|
+
}
|
|
53
|
+
/** Build the current IDL's `transact_swap` instruction without submitting it. */
|
|
54
|
+
export async function buildTransactSwapInstruction(params) {
|
|
55
|
+
const { program, relayer, sourceMint, destMint, sourceRoot, sourceTreeId, destTreeId, inputNullifiers, outputCommitments, proof, swapParams, swapAmount, swapData, extData, noteCiphers = null, sourceVaultTokenAccount, sourceMintAccount, destVaultTokenAccount, destMintAccount, relayerTokenAccount, swapProgram, jupiterEventAuthority, remainingAccounts = [], } = params;
|
|
56
|
+
if (!extData.relayer.equals(relayer.publicKey)) {
|
|
57
|
+
throw new Error("extData.relayer must match the relayer account");
|
|
58
|
+
}
|
|
59
|
+
if (swapParams.deadline <= 0n) {
|
|
60
|
+
throw new Error("swapParams.deadline must be a positive Unix timestamp");
|
|
61
|
+
}
|
|
62
|
+
if (swapParams.swapDataHash.length !== 32) {
|
|
63
|
+
throw new Error("swapParams.swapDataHash must be 32 bytes");
|
|
64
|
+
}
|
|
65
|
+
const { config: sourceConfig, vault: sourceVault, nullifiers: sourceNullifiers, } = getPoolPdas(program.programId, sourceMint);
|
|
66
|
+
const { config: destConfig, vault: destVault } = getPoolPdas(program.programId, destMint);
|
|
67
|
+
const globalConfig = getGlobalConfigPda(program.programId);
|
|
68
|
+
const sourceTree = getNoteTreePda(program.programId, sourceMint, sourceTreeId);
|
|
69
|
+
const destTree = getNoteTreePda(program.programId, destMint, destTreeId);
|
|
70
|
+
const sourceNullifierMarker0 = getNullifierMarkerPda(program.programId, sourceMint, inputNullifiers[0]);
|
|
71
|
+
const sourceNullifierMarker1 = getNullifierMarkerPda(program.programId, sourceMint, inputNullifiers[1]);
|
|
72
|
+
const executor = getSwapExecutorPda(program.programId, sourceMint, destMint, inputNullifiers[0], relayer.publicKey);
|
|
73
|
+
const executorSourceToken = await getAssociatedTokenAddress(sourceMintAccount, executor, true);
|
|
74
|
+
const executorDestToken = await getAssociatedTokenAddress(destMintAccount, executor, true);
|
|
75
|
+
let method = program.methods
|
|
76
|
+
.transactSwap(sourceTreeId, sourceMint, Array.from(inputNullifiers[0]), Array.from(inputNullifiers[1]), destTreeId, destMint, { proofA: proof.proofA, proofB: proof.proofB, proofC: proof.proofC }, Array.from(sourceRoot), Array.from(outputCommitments[0]), Array.from(outputCommitments[1]), {
|
|
77
|
+
minAmountOut: new BN(swapParams.minAmountOut.toString()),
|
|
78
|
+
deadline: new BN(swapParams.deadline.toString()),
|
|
79
|
+
destAmount: new BN(swapParams.destAmount.toString()),
|
|
80
|
+
swapDataHash: Array.from(swapParams.swapDataHash),
|
|
81
|
+
}, new BN(swapAmount.toString()), Buffer.from(swapData), {
|
|
82
|
+
recipient: extData.recipient,
|
|
83
|
+
relayer: extData.relayer,
|
|
84
|
+
fee: new BN(extData.fee.toString()),
|
|
85
|
+
refund: new BN(extData.refund.toString()),
|
|
86
|
+
claimant: extData.claimant,
|
|
87
|
+
}, encodeNoteCiphers(noteCiphers))
|
|
88
|
+
.accountsStrict({
|
|
89
|
+
sourceConfig,
|
|
90
|
+
globalConfig,
|
|
91
|
+
sourceVault,
|
|
92
|
+
sourceTree,
|
|
93
|
+
sourceNullifiers,
|
|
94
|
+
sourceNullifierMarker0,
|
|
95
|
+
sourceNullifierMarker1,
|
|
96
|
+
sourceVaultTokenAccount,
|
|
97
|
+
sourceMintAccount,
|
|
98
|
+
destConfig,
|
|
99
|
+
destVault,
|
|
100
|
+
destTree,
|
|
101
|
+
destVaultTokenAccount,
|
|
102
|
+
destMintAccount,
|
|
103
|
+
executor,
|
|
104
|
+
executorSourceToken,
|
|
105
|
+
executorDestToken,
|
|
106
|
+
relayer: relayer.publicKey,
|
|
107
|
+
relayerTokenAccount,
|
|
108
|
+
swapProgram,
|
|
109
|
+
jupiterEventAuthority,
|
|
110
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
111
|
+
systemProgram: SystemProgram.programId,
|
|
112
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
113
|
+
});
|
|
114
|
+
// Jupiter account order and duplicates are part of the CPI contract.
|
|
115
|
+
method = method.remainingAccounts(remainingAccounts);
|
|
116
|
+
return method.instruction();
|
|
117
|
+
}
|
|
118
|
+
/** Build the atomic instruction sequence, including native-source funding. */
|
|
119
|
+
export async function buildPrivateSwapInstructions(params) {
|
|
120
|
+
const instructions = [];
|
|
121
|
+
if (params.sourceMint.equals(NATIVE_SOL_MINT)) {
|
|
122
|
+
instructions.push(await fundNativeSource({
|
|
123
|
+
program: params.program,
|
|
124
|
+
relayer: params.relayer,
|
|
125
|
+
sourceMint: params.sourceMint,
|
|
126
|
+
destMint: params.destMint,
|
|
127
|
+
inputNullifier0: params.inputNullifiers[0],
|
|
128
|
+
swapAmount: params.swapAmount,
|
|
129
|
+
sourceMintAccount: params.sourceMintAccount,
|
|
130
|
+
}));
|
|
131
|
+
}
|
|
132
|
+
instructions.push(await buildTransactSwapInstruction(params));
|
|
133
|
+
return instructions;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Submit the parity-correct instructions as a legacy transaction. For Jupiter
|
|
137
|
+
* routes, prefer `buildPrivateSwapInstructions` and compile v0 with route ALTs.
|
|
138
|
+
*/
|
|
139
|
+
export async function transactSwap(params) {
|
|
140
|
+
const instructions = await buildPrivateSwapInstructions(params);
|
|
141
|
+
const transaction = new Transaction().add(...instructions);
|
|
142
|
+
const provider = params.program.provider;
|
|
143
|
+
return withRpcRetry(() => provider.sendAndConfirm(transaction, [params.relayer]));
|
|
144
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Program, Idl } from "@coral-xyz/anchor";
|
|
2
|
+
import { Keypair, PublicKey } from "@solana/web3.js";
|
|
3
|
+
import type { ExtData, TransactionProofStruct } from "../proofs/types.js";
|
|
4
|
+
import type { NoteCiphers } from "../compactNote.js";
|
|
5
|
+
/**
|
|
6
|
+
* Execute a transaction on the privacy pool.
|
|
7
|
+
* This is the low-level function that calls the on-chain `transact` instruction.
|
|
8
|
+
*
|
|
9
|
+
* @param publicAmount - Positive for deposits, negative for withdrawals, 0 for transfers
|
|
10
|
+
* @param inputTreeId - The tree ID where input UTXOs are located
|
|
11
|
+
* @param outputTreeId - The tree ID where output commitments will be inserted
|
|
12
|
+
*/
|
|
13
|
+
export declare function transact<T extends Idl>(params: {
|
|
14
|
+
program: Program<T>;
|
|
15
|
+
relayer: Keypair;
|
|
16
|
+
recipient: PublicKey;
|
|
17
|
+
mintAddress: PublicKey;
|
|
18
|
+
root: Uint8Array;
|
|
19
|
+
inputTreeId: number;
|
|
20
|
+
outputTreeId: number;
|
|
21
|
+
publicAmount: bigint;
|
|
22
|
+
inputNullifiers: [Uint8Array, Uint8Array];
|
|
23
|
+
outputCommitments: [Uint8Array, Uint8Array];
|
|
24
|
+
extData: ExtData;
|
|
25
|
+
proof: TransactionProofStruct;
|
|
26
|
+
/** Unix timestamp after which the tx is invalid. Defaults to 10 minutes from build time. */
|
|
27
|
+
deadline?: bigint;
|
|
28
|
+
/** Optional compact recovery payload emitted with the two output commitments. */
|
|
29
|
+
noteCiphers?: NoteCiphers | null;
|
|
30
|
+
vaultTokenAccount?: PublicKey;
|
|
31
|
+
userTokenAccount?: PublicKey;
|
|
32
|
+
recipientTokenAccount?: PublicKey;
|
|
33
|
+
relayerTokenAccount?: PublicKey;
|
|
34
|
+
}): Promise<string>;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import * as anchor from "@coral-xyz/anchor";
|
|
2
|
+
import { SystemProgram } from "@solana/web3.js";
|
|
3
|
+
import { TOKEN_PROGRAM_ID, getAssociatedTokenAddress } from "@solana/spl-token";
|
|
4
|
+
import { computeExtDataHash } from "../proofs/transaction.js";
|
|
5
|
+
import { NATIVE_SOL_MINT } from "../config.js";
|
|
6
|
+
import { withRpcRetry } from "../retry.js";
|
|
7
|
+
import { getGlobalConfigPda, getNoteTreePda, getNullifierMarkerPda, getPoolPdas, } from "../accounts/pdas.js";
|
|
8
|
+
// Anchor is CJS and does not expose BN as a detectable Node ESM named export.
|
|
9
|
+
const { BN } = anchor;
|
|
10
|
+
// -----------------------------------------------------------------------------
|
|
11
|
+
// Main Transaction Instruction
|
|
12
|
+
// -----------------------------------------------------------------------------
|
|
13
|
+
/**
|
|
14
|
+
* Execute a transaction on the privacy pool.
|
|
15
|
+
* This is the low-level function that calls the on-chain `transact` instruction.
|
|
16
|
+
*
|
|
17
|
+
* @param publicAmount - Positive for deposits, negative for withdrawals, 0 for transfers
|
|
18
|
+
* @param inputTreeId - The tree ID where input UTXOs are located
|
|
19
|
+
* @param outputTreeId - The tree ID where output commitments will be inserted
|
|
20
|
+
*/
|
|
21
|
+
export async function transact(params) {
|
|
22
|
+
const { program, relayer, recipient, mintAddress, root, inputTreeId, outputTreeId, publicAmount, inputNullifiers, outputCommitments, extData, proof, deadline = BigInt(Math.floor(Date.now() / 1000) + 600), noteCiphers = null, vaultTokenAccount, userTokenAccount, recipientTokenAccount, relayerTokenAccount, } = params;
|
|
23
|
+
if (!recipient.equals(extData.recipient)) {
|
|
24
|
+
throw new Error("recipient account must match extData.recipient");
|
|
25
|
+
}
|
|
26
|
+
if (!relayer.publicKey.equals(extData.relayer)) {
|
|
27
|
+
throw new Error("relayer account must match extData.relayer");
|
|
28
|
+
}
|
|
29
|
+
if (deadline <= 0n) {
|
|
30
|
+
throw new Error("deadline must be a positive Unix timestamp");
|
|
31
|
+
}
|
|
32
|
+
if (publicAmount === 0n && (extData.fee !== 0n || extData.refund !== 0n)) {
|
|
33
|
+
throw new Error("Private transfers require extData fee and refund to be zero");
|
|
34
|
+
}
|
|
35
|
+
const { config, vault, nullifiers } = getPoolPdas(program.programId, mintAddress);
|
|
36
|
+
const globalConfig = getGlobalConfigPda(program.programId);
|
|
37
|
+
const inputTree = getNoteTreePda(program.programId, mintAddress, inputTreeId);
|
|
38
|
+
const outputTree = getNoteTreePda(program.programId, mintAddress, outputTreeId);
|
|
39
|
+
// Nullifier markers are global (no tree_id) to prevent cross-tree double-spend
|
|
40
|
+
const nullifierMarker0 = getNullifierMarkerPda(program.programId, mintAddress, inputNullifiers[0]);
|
|
41
|
+
const nullifierMarker1 = getNullifierMarkerPda(program.programId, mintAddress, inputNullifiers[1]);
|
|
42
|
+
const extDataHash = computeExtDataHash(extData);
|
|
43
|
+
const isNativeSol = mintAddress.equals(NATIVE_SOL_MINT);
|
|
44
|
+
// For SPL tokens, resolve token accounts
|
|
45
|
+
let vaultTA = vaultTokenAccount;
|
|
46
|
+
let userTA = userTokenAccount;
|
|
47
|
+
let recipientTA = recipientTokenAccount;
|
|
48
|
+
let relayerTA = relayerTokenAccount;
|
|
49
|
+
if (!isNativeSol) {
|
|
50
|
+
if (!vaultTA) {
|
|
51
|
+
vaultTA = await getAssociatedTokenAddress(mintAddress, vault, true);
|
|
52
|
+
}
|
|
53
|
+
if (!userTA) {
|
|
54
|
+
userTA = await getAssociatedTokenAddress(mintAddress, relayer.publicKey);
|
|
55
|
+
}
|
|
56
|
+
if (!recipientTA) {
|
|
57
|
+
recipientTA = await getAssociatedTokenAddress(mintAddress, recipient);
|
|
58
|
+
}
|
|
59
|
+
if (!relayerTA) {
|
|
60
|
+
relayerTA = await getAssociatedTokenAddress(mintAddress, relayer.publicKey);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
vaultTA ?? (vaultTA = relayer.publicKey);
|
|
65
|
+
userTA ?? (userTA = relayer.publicKey);
|
|
66
|
+
recipientTA ?? (recipientTA = relayer.publicKey);
|
|
67
|
+
relayerTA ?? (relayerTA = relayer.publicKey);
|
|
68
|
+
}
|
|
69
|
+
const tx = program.methods
|
|
70
|
+
.transact(Array.from(root), inputTreeId, outputTreeId, new BN(publicAmount.toString()), Array.from(extDataHash), mintAddress, Array.from(inputNullifiers[0]), Array.from(inputNullifiers[1]), Array.from(outputCommitments[0]), Array.from(outputCommitments[1]), new BN(deadline.toString()), {
|
|
71
|
+
recipient: extData.recipient,
|
|
72
|
+
relayer: extData.relayer,
|
|
73
|
+
fee: new BN(extData.fee.toString()),
|
|
74
|
+
refund: new BN(extData.refund.toString()),
|
|
75
|
+
claimant: extData.claimant,
|
|
76
|
+
}, {
|
|
77
|
+
proofA: proof.proofA,
|
|
78
|
+
proofB: proof.proofB,
|
|
79
|
+
proofC: proof.proofC,
|
|
80
|
+
}, noteCiphers
|
|
81
|
+
? {
|
|
82
|
+
note0EphemeralKey: Array.from(noteCiphers.note0EphemeralKey),
|
|
83
|
+
note0Encrypted: Array.from(noteCiphers.note0Encrypted),
|
|
84
|
+
note0ViewTag: noteCiphers.note0ViewTag,
|
|
85
|
+
note1EphemeralKey: Array.from(noteCiphers.note1EphemeralKey),
|
|
86
|
+
note1Encrypted: Array.from(noteCiphers.note1Encrypted),
|
|
87
|
+
note1ViewTag: noteCiphers.note1ViewTag,
|
|
88
|
+
}
|
|
89
|
+
: null)
|
|
90
|
+
.accountsStrict({
|
|
91
|
+
config,
|
|
92
|
+
globalConfig,
|
|
93
|
+
vault,
|
|
94
|
+
inputTree,
|
|
95
|
+
outputTree,
|
|
96
|
+
nullifiers,
|
|
97
|
+
nullifierMarker0,
|
|
98
|
+
nullifierMarker1,
|
|
99
|
+
relayer: relayer.publicKey,
|
|
100
|
+
recipient,
|
|
101
|
+
vaultTokenAccount: vaultTA,
|
|
102
|
+
userTokenAccount: userTA,
|
|
103
|
+
recipientTokenAccount: recipientTA,
|
|
104
|
+
relayerTokenAccount: relayerTA,
|
|
105
|
+
tokenProgram: isNativeSol ? SystemProgram.programId : TOKEN_PROGRAM_ID,
|
|
106
|
+
systemProgram: SystemProgram.programId,
|
|
107
|
+
})
|
|
108
|
+
.signers([relayer]);
|
|
109
|
+
return withRpcRetry(() => tx.rpc());
|
|
110
|
+
}
|