@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,51 @@
|
|
|
1
|
+
import type { Program, Idl } from "@coral-xyz/anchor";
|
|
2
|
+
import { Keypair, PublicKey } from "@solana/web3.js";
|
|
3
|
+
import type { TransactionProofBuilder } from "../proofs/types.js";
|
|
4
|
+
import type { InputUTXO, SerializedUTXO } from "../notes/model.js";
|
|
5
|
+
import { MerkleTree } from "../merkle.js";
|
|
6
|
+
import { type NoteCiphers } from "../compactNote.js";
|
|
7
|
+
export type TransferResult = {
|
|
8
|
+
/** Output UTXOs created by the transfer */
|
|
9
|
+
outputUTXOs: [SerializedUTXO, SerializedUTXO];
|
|
10
|
+
/** Leaf indices where the UTXOs were inserted */
|
|
11
|
+
leafIndices: [number, number];
|
|
12
|
+
/** New Merkle root after insertion */
|
|
13
|
+
root: Uint8Array;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Execute a private transfer within the pool.
|
|
17
|
+
*
|
|
18
|
+
* For transfers, the publicAmount is 0 (no funds enter or leave the pool).
|
|
19
|
+
* Input amounts must equal output amounts.
|
|
20
|
+
*
|
|
21
|
+
* @param inputUTXOs - The UTXOs being spent
|
|
22
|
+
* @param outputPubkeys - UTXO public keys for the recipients
|
|
23
|
+
* @param outputAmounts - Amounts for each output (must sum to input total)
|
|
24
|
+
* @param inputTreeId - The tree ID where input UTXOs are located
|
|
25
|
+
* @param outputTreeId - The tree ID where output commitments will be inserted
|
|
26
|
+
*/
|
|
27
|
+
export declare function privateTransfer<T extends Idl>(params: {
|
|
28
|
+
program: Program<T>;
|
|
29
|
+
relayer: Keypair;
|
|
30
|
+
mintAddress: PublicKey;
|
|
31
|
+
inputUTXOs: [InputUTXO, InputUTXO];
|
|
32
|
+
outputPubkeys: [bigint, bigint];
|
|
33
|
+
outputAmounts: [bigint, bigint];
|
|
34
|
+
root: Uint8Array;
|
|
35
|
+
inputTreeId: number;
|
|
36
|
+
outputTreeId: number;
|
|
37
|
+
tree: MerkleTree;
|
|
38
|
+
proofBuilder: TransactionProofBuilder;
|
|
39
|
+
/** Must be zero: the program rejects fees on publicAmount=0 transfers. */
|
|
40
|
+
fee?: bigint;
|
|
41
|
+
deadline?: bigint;
|
|
42
|
+
/** Public recipient account committed in ExtData (no funds are sent to it). */
|
|
43
|
+
externalRecipient?: PublicKey;
|
|
44
|
+
/** Wallet keys used to encrypt output notes for on-chain recovery. */
|
|
45
|
+
noteRecipientWallets?: [
|
|
46
|
+
PublicKey | Uint8Array | null,
|
|
47
|
+
PublicKey | Uint8Array | null
|
|
48
|
+
];
|
|
49
|
+
/** Prebuilt note ciphers. Takes precedence over noteRecipientWallets. */
|
|
50
|
+
noteCiphers?: NoteCiphers | null;
|
|
51
|
+
}): Promise<TransferResult>;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Keypair, PublicKey, SystemProgram } from "@solana/web3.js";
|
|
2
|
+
import { prepareTransactionInputs } from "../proofs/transaction.js";
|
|
3
|
+
import { encodeSnarkjsProofToTransactionProof } from "../proofs/encoding.js";
|
|
4
|
+
import { createUTXO } from "../notes/model.js";
|
|
5
|
+
import { transact } from "./transact.js";
|
|
6
|
+
import { bigIntToBytesBE } from "../poseidon.js";
|
|
7
|
+
import { createNoteCiphers, } from "../compactNote.js";
|
|
8
|
+
/**
|
|
9
|
+
* Execute a private transfer within the pool.
|
|
10
|
+
*
|
|
11
|
+
* For transfers, the publicAmount is 0 (no funds enter or leave the pool).
|
|
12
|
+
* Input amounts must equal output amounts.
|
|
13
|
+
*
|
|
14
|
+
* @param inputUTXOs - The UTXOs being spent
|
|
15
|
+
* @param outputPubkeys - UTXO public keys for the recipients
|
|
16
|
+
* @param outputAmounts - Amounts for each output (must sum to input total)
|
|
17
|
+
* @param inputTreeId - The tree ID where input UTXOs are located
|
|
18
|
+
* @param outputTreeId - The tree ID where output commitments will be inserted
|
|
19
|
+
*/
|
|
20
|
+
export async function privateTransfer(params) {
|
|
21
|
+
const { program, relayer, mintAddress, inputUTXOs, outputPubkeys, outputAmounts, root, inputTreeId, outputTreeId, tree, proofBuilder, fee = 0n, deadline = BigInt(Math.floor(Date.now() / 1000) + 600), externalRecipient = Keypair.generate().publicKey, noteRecipientWallets, noteCiphers, } = params;
|
|
22
|
+
if (fee !== 0n) {
|
|
23
|
+
throw new Error("Private transact transfers require fee=0; represent any relayer fee in the output amounts");
|
|
24
|
+
}
|
|
25
|
+
const totalInput = inputUTXOs[0].amount + inputUTXOs[1].amount;
|
|
26
|
+
const totalOutput = outputAmounts[0] + outputAmounts[1];
|
|
27
|
+
if (totalInput !== totalOutput) {
|
|
28
|
+
throw new Error(`Balance mismatch: inputs=${totalInput}, outputs=${totalOutput}`);
|
|
29
|
+
}
|
|
30
|
+
const outputUTXO0 = createUTXO({
|
|
31
|
+
amount: outputAmounts[0],
|
|
32
|
+
pubkey: outputPubkeys[0],
|
|
33
|
+
mintAddress,
|
|
34
|
+
});
|
|
35
|
+
const outputUTXO1 = createUTXO({
|
|
36
|
+
amount: outputAmounts[1],
|
|
37
|
+
pubkey: outputPubkeys[1],
|
|
38
|
+
mintAddress,
|
|
39
|
+
});
|
|
40
|
+
const outputUTXOs = [
|
|
41
|
+
outputUTXO0,
|
|
42
|
+
outputUTXO1,
|
|
43
|
+
];
|
|
44
|
+
const extData = {
|
|
45
|
+
recipient: externalRecipient,
|
|
46
|
+
relayer: relayer.publicKey,
|
|
47
|
+
fee: 0n,
|
|
48
|
+
refund: 0n,
|
|
49
|
+
claimant: SystemProgram.programId,
|
|
50
|
+
};
|
|
51
|
+
// publicAmount is 0 for transfers
|
|
52
|
+
const publicAmount = 0n;
|
|
53
|
+
const circuitInputs = prepareTransactionInputs({
|
|
54
|
+
inputUTXOs,
|
|
55
|
+
outputUTXOs,
|
|
56
|
+
root,
|
|
57
|
+
publicAmount,
|
|
58
|
+
extData,
|
|
59
|
+
mintAddress,
|
|
60
|
+
});
|
|
61
|
+
const rawProof = await proofBuilder(circuitInputs);
|
|
62
|
+
const proof = encodeSnarkjsProofToTransactionProof(rawProof);
|
|
63
|
+
const walletBytes = (wallet) => wallet instanceof PublicKey ? wallet.toBytes() : wallet;
|
|
64
|
+
const transferNoteCiphers = noteCiphers ??
|
|
65
|
+
(noteRecipientWallets
|
|
66
|
+
? createNoteCiphers(noteRecipientWallets[0]
|
|
67
|
+
? {
|
|
68
|
+
recipientWalletPubkey: walletBytes(noteRecipientWallets[0]),
|
|
69
|
+
blinding: bigIntToBytesBE(outputUTXOs[0].blinding),
|
|
70
|
+
amount: outputUTXOs[0].amount,
|
|
71
|
+
}
|
|
72
|
+
: null, noteRecipientWallets[1]
|
|
73
|
+
? {
|
|
74
|
+
recipientWalletPubkey: walletBytes(noteRecipientWallets[1]),
|
|
75
|
+
blinding: bigIntToBytesBE(outputUTXOs[1].blinding),
|
|
76
|
+
amount: outputUTXOs[1].amount,
|
|
77
|
+
}
|
|
78
|
+
: null)
|
|
79
|
+
: null);
|
|
80
|
+
await transact({
|
|
81
|
+
program,
|
|
82
|
+
relayer,
|
|
83
|
+
recipient: externalRecipient,
|
|
84
|
+
mintAddress,
|
|
85
|
+
root,
|
|
86
|
+
inputTreeId,
|
|
87
|
+
outputTreeId,
|
|
88
|
+
publicAmount,
|
|
89
|
+
inputNullifiers: circuitInputs.inputNullifiers,
|
|
90
|
+
outputCommitments: circuitInputs.outputCommitments,
|
|
91
|
+
extData,
|
|
92
|
+
proof,
|
|
93
|
+
deadline,
|
|
94
|
+
noteCiphers: transferNoteCiphers,
|
|
95
|
+
});
|
|
96
|
+
const { index: outIdx0 } = tree.insert(outputUTXOs[0].commitment);
|
|
97
|
+
const { index: outIdx1, root: newRoot } = tree.insert(outputUTXOs[1].commitment);
|
|
98
|
+
return {
|
|
99
|
+
outputUTXOs,
|
|
100
|
+
leafIndices: [outIdx0, outIdx1],
|
|
101
|
+
root: newRoot,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Program, Idl } from "@coral-xyz/anchor";
|
|
2
|
+
import { Keypair, PublicKey } from "@solana/web3.js";
|
|
3
|
+
import type { TransactionProofBuilder } from "../proofs/types.js";
|
|
4
|
+
import type { InputUTXO, SerializedUTXO } from "../notes/model.js";
|
|
5
|
+
import { MerkleTree } from "../merkle.js";
|
|
6
|
+
import { type NoteCiphers } from "../compactNote.js";
|
|
7
|
+
export type WithdrawResult = {
|
|
8
|
+
/** Change UTXO (remaining balance) */
|
|
9
|
+
changeUTXO: SerializedUTXO;
|
|
10
|
+
/** Leaf index of the change UTXO */
|
|
11
|
+
leafIndex: number;
|
|
12
|
+
/** New Merkle root after insertion */
|
|
13
|
+
root: Uint8Array;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Withdraw funds from the privacy pool.
|
|
17
|
+
*
|
|
18
|
+
* For withdrawals, we spend input UTXOs and create change output UTXOs.
|
|
19
|
+
* The publicAmount is negative (funds flowing out).
|
|
20
|
+
*
|
|
21
|
+
* @param withdrawAmount - Amount to withdraw in lamports/token units
|
|
22
|
+
* @param inputUTXOs - The UTXOs being spent
|
|
23
|
+
* @param changePubkey - UTXO public key for the change output
|
|
24
|
+
* @param inputTreeId - The tree ID where input UTXOs are located
|
|
25
|
+
* @param outputTreeId - The tree ID where change outputs will be inserted
|
|
26
|
+
*/
|
|
27
|
+
export declare function withdraw<T extends Idl>(params: {
|
|
28
|
+
program: Program<T>;
|
|
29
|
+
relayer: Keypair;
|
|
30
|
+
recipient: PublicKey;
|
|
31
|
+
mintAddress: PublicKey;
|
|
32
|
+
inputUTXOs: [InputUTXO, InputUTXO];
|
|
33
|
+
changePubkey: bigint;
|
|
34
|
+
withdrawAmount: bigint;
|
|
35
|
+
fee: bigint;
|
|
36
|
+
refund: bigint;
|
|
37
|
+
root: Uint8Array;
|
|
38
|
+
inputTreeId: number;
|
|
39
|
+
outputTreeId: number;
|
|
40
|
+
tree: MerkleTree;
|
|
41
|
+
proofBuilder: TransactionProofBuilder;
|
|
42
|
+
/** When true, the fee is debited in addition to withdrawAmount. */
|
|
43
|
+
feeOnTop?: boolean;
|
|
44
|
+
deadline?: bigint;
|
|
45
|
+
/** Solana wallet key used to encrypt the change note for chain recovery. */
|
|
46
|
+
changeNoteRecipientWallet?: PublicKey | Uint8Array;
|
|
47
|
+
/** Prebuilt recovery payload. Takes precedence over changeNoteRecipientWallet. */
|
|
48
|
+
noteCiphers?: NoteCiphers | null;
|
|
49
|
+
}): Promise<WithdrawResult>;
|
|
50
|
+
/**
|
|
51
|
+
* Result of a private transfer operation.
|
|
52
|
+
*/
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { PublicKey, SystemProgram } from "@solana/web3.js";
|
|
2
|
+
import { prepareTransactionInputs } from "../proofs/transaction.js";
|
|
3
|
+
import { encodeSnarkjsProofToTransactionProof } from "../proofs/encoding.js";
|
|
4
|
+
import { createUTXO, generateKeypair } from "../notes/model.js";
|
|
5
|
+
import { transact } from "./transact.js";
|
|
6
|
+
import { bigIntToBytesBE } from "../poseidon.js";
|
|
7
|
+
import { createNoteCiphers, } from "../compactNote.js";
|
|
8
|
+
/**
|
|
9
|
+
* Withdraw funds from the privacy pool.
|
|
10
|
+
*
|
|
11
|
+
* For withdrawals, we spend input UTXOs and create change output UTXOs.
|
|
12
|
+
* The publicAmount is negative (funds flowing out).
|
|
13
|
+
*
|
|
14
|
+
* @param withdrawAmount - Amount to withdraw in lamports/token units
|
|
15
|
+
* @param inputUTXOs - The UTXOs being spent
|
|
16
|
+
* @param changePubkey - UTXO public key for the change output
|
|
17
|
+
* @param inputTreeId - The tree ID where input UTXOs are located
|
|
18
|
+
* @param outputTreeId - The tree ID where change outputs will be inserted
|
|
19
|
+
*/
|
|
20
|
+
export async function withdraw(params) {
|
|
21
|
+
const { program, relayer, recipient, mintAddress, inputUTXOs, changePubkey, withdrawAmount, fee, refund, root, inputTreeId, outputTreeId, tree, proofBuilder, feeOnTop = false, deadline = BigInt(Math.floor(Date.now() / 1000) + 600), changeNoteRecipientWallet, noteCiphers, } = params;
|
|
22
|
+
const totalInput = inputUTXOs[0].amount + inputUTXOs[1].amount;
|
|
23
|
+
const outflowAmount = feeOnTop ? withdrawAmount + fee : withdrawAmount;
|
|
24
|
+
const changeAmount = totalInput - outflowAmount;
|
|
25
|
+
if (withdrawAmount <= 0n) {
|
|
26
|
+
throw new Error("Withdrawal amount must be positive");
|
|
27
|
+
}
|
|
28
|
+
if (fee < 0n || refund < 0n || fee + refund > outflowAmount) {
|
|
29
|
+
throw new Error("Withdrawal fee and refund must fit within the public outflow");
|
|
30
|
+
}
|
|
31
|
+
if (changeAmount < 0n) {
|
|
32
|
+
throw new Error("Insufficient input balance for withdrawal");
|
|
33
|
+
}
|
|
34
|
+
const changeUTXO = createUTXO({
|
|
35
|
+
amount: changeAmount,
|
|
36
|
+
pubkey: changePubkey,
|
|
37
|
+
mintAddress,
|
|
38
|
+
});
|
|
39
|
+
const zeroUTXO = createUTXO({
|
|
40
|
+
amount: 0n,
|
|
41
|
+
pubkey: generateKeypair().publicKey,
|
|
42
|
+
mintAddress,
|
|
43
|
+
});
|
|
44
|
+
const outputUTXOs = [changeUTXO, zeroUTXO];
|
|
45
|
+
const extData = {
|
|
46
|
+
recipient,
|
|
47
|
+
relayer: relayer.publicKey,
|
|
48
|
+
fee,
|
|
49
|
+
refund,
|
|
50
|
+
claimant: SystemProgram.programId,
|
|
51
|
+
};
|
|
52
|
+
// publicAmount is negative for withdrawals
|
|
53
|
+
const publicAmount = -outflowAmount;
|
|
54
|
+
const circuitInputs = prepareTransactionInputs({
|
|
55
|
+
inputUTXOs,
|
|
56
|
+
outputUTXOs,
|
|
57
|
+
root,
|
|
58
|
+
publicAmount,
|
|
59
|
+
extData,
|
|
60
|
+
mintAddress,
|
|
61
|
+
});
|
|
62
|
+
const rawProof = await proofBuilder(circuitInputs);
|
|
63
|
+
const proof = encodeSnarkjsProofToTransactionProof(rawProof);
|
|
64
|
+
const withdrawNoteCiphers = noteCiphers !== undefined
|
|
65
|
+
? noteCiphers
|
|
66
|
+
: changeNoteRecipientWallet && changeAmount > 0n
|
|
67
|
+
? createNoteCiphers({
|
|
68
|
+
recipientWalletPubkey: changeNoteRecipientWallet instanceof PublicKey
|
|
69
|
+
? changeNoteRecipientWallet.toBytes()
|
|
70
|
+
: changeNoteRecipientWallet,
|
|
71
|
+
blinding: bigIntToBytesBE(changeUTXO.blinding),
|
|
72
|
+
amount: changeAmount,
|
|
73
|
+
}, null)
|
|
74
|
+
: null;
|
|
75
|
+
await transact({
|
|
76
|
+
program,
|
|
77
|
+
relayer,
|
|
78
|
+
recipient,
|
|
79
|
+
mintAddress,
|
|
80
|
+
root,
|
|
81
|
+
inputTreeId,
|
|
82
|
+
outputTreeId,
|
|
83
|
+
publicAmount,
|
|
84
|
+
inputNullifiers: circuitInputs.inputNullifiers,
|
|
85
|
+
outputCommitments: circuitInputs.outputCommitments,
|
|
86
|
+
extData,
|
|
87
|
+
proof,
|
|
88
|
+
deadline,
|
|
89
|
+
noteCiphers: withdrawNoteCiphers,
|
|
90
|
+
});
|
|
91
|
+
const { index: changeIdx } = tree.insert(changeUTXO.commitment);
|
|
92
|
+
const { root: newRoot } = tree.insert(zeroUTXO.commitment);
|
|
93
|
+
return {
|
|
94
|
+
changeUTXO,
|
|
95
|
+
leafIndex: changeIdx,
|
|
96
|
+
root: newRoot,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Result of a private transfer operation.
|
|
101
|
+
*/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./notes/index.js";
|
package/dist/esm/utxo.js
CHANGED
|
@@ -1,372 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { sha256 } from "@noble/hashes/sha256";
|
|
4
|
-
import { BN254_FR_MODULUS, bigIntToBytesBE, bytesToBigIntBE, poseidon1, poseidon3, poseidon4, pubkeyToField, } from "./poseidon";
|
|
5
|
-
// -----------------------------------------------------------------------------
|
|
6
|
-
// Keypair Functions
|
|
7
|
-
// -----------------------------------------------------------------------------
|
|
8
|
-
/**
|
|
9
|
-
* Generate a random keypair.
|
|
10
|
-
* Private key is a random 32-byte scalar.
|
|
11
|
-
* Public key is derived via Poseidon: pubkey = Poseidon(privateKey)
|
|
12
|
-
*/
|
|
13
|
-
export function generateKeypair() {
|
|
14
|
-
const privateKeyBytes = new Uint8Array(randomBytes(32));
|
|
15
|
-
const privateKey = bytesToBigIntBE(privateKeyBytes) % BN254_FR_MODULUS;
|
|
16
|
-
const publicKey = derivePublicKey(privateKey);
|
|
17
|
-
return { privateKey, publicKey };
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Derive public key from private key.
|
|
21
|
-
* Matches circuit's Keypair template: publicKey = Poseidon(privateKey)
|
|
22
|
-
*/
|
|
23
|
-
export function derivePublicKey(privateKey) {
|
|
24
|
-
return poseidon1(privateKey);
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Create keypair from a known private key.
|
|
28
|
-
*/
|
|
29
|
-
export function keypairFromPrivateKey(privateKey) {
|
|
30
|
-
return {
|
|
31
|
-
privateKey: privateKey % BN254_FR_MODULUS,
|
|
32
|
-
publicKey: derivePublicKey(privateKey % BN254_FR_MODULUS),
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
// -----------------------------------------------------------------------------
|
|
36
|
-
// UTXO Commitment Functions
|
|
37
|
-
// -----------------------------------------------------------------------------
|
|
38
|
-
/**
|
|
39
|
-
* Compute UTXO commitment.
|
|
40
|
-
* Matches circuit's UTXOCommitment template:
|
|
41
|
-
* commitment = Poseidon(amount, pubkey, blinding, mintAddress)
|
|
42
|
-
*/
|
|
43
|
-
export function commitUTXO(utxo) {
|
|
44
|
-
const commitment = poseidon4(utxo.amount, utxo.pubkey, utxo.blinding, utxo.mintAddress);
|
|
45
|
-
return bigIntToBytesBE(commitment);
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Create a random UTXO with the given parameters.
|
|
49
|
-
*/
|
|
50
|
-
export function createUTXO(params) {
|
|
51
|
-
const blindingBytes = new Uint8Array(randomBytes(32));
|
|
52
|
-
const blinding = bytesToBigIntBE(blindingBytes) % BN254_FR_MODULUS;
|
|
53
|
-
const utxo = {
|
|
54
|
-
amount: params.amount,
|
|
55
|
-
pubkey: params.pubkey,
|
|
56
|
-
blinding,
|
|
57
|
-
mintAddress: pubkeyToField(params.mintAddress),
|
|
58
|
-
};
|
|
59
|
-
const commitment = commitUTXO(utxo);
|
|
60
|
-
return {
|
|
61
|
-
...utxo,
|
|
62
|
-
commitment,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Create a UTXO with a specified private key (for owned UTXOs).
|
|
67
|
-
*/
|
|
68
|
-
export function createOwnedUTXO(params) {
|
|
69
|
-
const keypair = keypairFromPrivateKey(params.privateKey);
|
|
70
|
-
const utxo = createUTXO({
|
|
71
|
-
amount: params.amount,
|
|
72
|
-
pubkey: keypair.publicKey,
|
|
73
|
-
mintAddress: params.mintAddress,
|
|
74
|
-
});
|
|
75
|
-
return {
|
|
76
|
-
...utxo,
|
|
77
|
-
privateKey: keypair.privateKey,
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Create a zero UTXO (for deposits where no input exists).
|
|
82
|
-
* Zero UTXOs have amount=0 and are used as placeholders in 2-in-2-out transactions.
|
|
83
|
-
*/
|
|
84
|
-
export function createZeroUTXO(pubkey, mintAddress) {
|
|
85
|
-
return createUTXO({
|
|
86
|
-
amount: 0n,
|
|
87
|
-
pubkey,
|
|
88
|
-
mintAddress,
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Create a zero UTXO with private key (for owned zero UTXOs).
|
|
93
|
-
*/
|
|
94
|
-
export function createOwnedZeroUTXO(privateKey, mintAddress) {
|
|
95
|
-
const keypair = keypairFromPrivateKey(privateKey);
|
|
96
|
-
const utxo = createZeroUTXO(keypair.publicKey, mintAddress);
|
|
97
|
-
return {
|
|
98
|
-
...utxo,
|
|
99
|
-
privateKey: keypair.privateKey,
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
// -----------------------------------------------------------------------------
|
|
103
|
-
// Nullifier Functions
|
|
104
|
-
// -----------------------------------------------------------------------------
|
|
105
|
-
/**
|
|
106
|
-
* Compute signature for nullifier derivation.
|
|
107
|
-
* Matches circuit's Signature template:
|
|
108
|
-
* signature = Poseidon(privateKey, commitment, pathIndex)
|
|
109
|
-
*/
|
|
110
|
-
export function computeSignature(privateKey, commitment, pathIndex) {
|
|
111
|
-
return poseidon3(privateKey, commitment, pathIndex);
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Derive nullifier for a UTXO.
|
|
115
|
-
* Matches circuit's UTXONullifier template:
|
|
116
|
-
* signature = Poseidon(privateKey, commitment, pathIndex)
|
|
117
|
-
* nullifier = Poseidon(commitment, pathIndex, signature)
|
|
118
|
-
*
|
|
119
|
-
* The signature-based nullifier ensures only the private key holder can spend.
|
|
120
|
-
*/
|
|
121
|
-
export function deriveNullifier(utxo, pathIndex, privateKey) {
|
|
122
|
-
const commitment = bytesToBigIntBE(utxo.commitment) % BN254_FR_MODULUS;
|
|
123
|
-
const pathIndexField = BigInt(pathIndex) % BN254_FR_MODULUS;
|
|
124
|
-
const signature = computeSignature(privateKey, commitment, pathIndexField);
|
|
125
|
-
const nullifier = poseidon3(commitment, pathIndexField, signature);
|
|
126
|
-
return bigIntToBytesBE(nullifier);
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Convert InputUTXO to the format expected by the circuit.
|
|
130
|
-
*/
|
|
131
|
-
export function inputUTXOToCircuitFormat(input) {
|
|
132
|
-
return {
|
|
133
|
-
amount: input.amount,
|
|
134
|
-
pubkey: input.pubkey,
|
|
135
|
-
blinding: input.blinding,
|
|
136
|
-
pathElements: input.pathElements.map((e) => bytesToBigIntBE(e) % BN254_FR_MODULUS),
|
|
137
|
-
pathIndex: input.pathIndex,
|
|
138
|
-
privateKey: input.privateKey,
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Derive a NaCl X25519 encryption keypair from a UTXO private key.
|
|
143
|
-
* The keypair is deterministic: same privateKey always yields the same pair.
|
|
144
|
-
*/
|
|
145
|
-
export function deriveEncryptionKeypair(privateKey) {
|
|
146
|
-
const seed = sha256(bigIntToBytesBE(privateKey));
|
|
147
|
-
return nacl.box.keyPair.fromSecretKey(seed);
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Encrypt a UTXO so the recipient can recover it from on-chain events.
|
|
151
|
-
*
|
|
152
|
-
* Plaintext layout (72 bytes):
|
|
153
|
-
* [0..8] amount — 8-byte little-endian u64
|
|
154
|
-
* [8..40] blinding — 32-byte big-endian field element
|
|
155
|
-
* [40..72] privateKey — 32-byte big-endian field element
|
|
156
|
-
*
|
|
157
|
-
* Uses ephemeral-key NaCl box so the sender key is single-use.
|
|
158
|
-
*/
|
|
159
|
-
export function encryptUTXONote(utxo, recipientEncPubkey) {
|
|
160
|
-
const nonce = nacl.randomBytes(nacl.box.nonceLength); // 24 bytes
|
|
161
|
-
const ephemeral = nacl.box.keyPair();
|
|
162
|
-
const amountBuf = new Uint8Array(8);
|
|
163
|
-
let v = utxo.amount;
|
|
164
|
-
for (let i = 0; i < 8; i++) {
|
|
165
|
-
amountBuf[i] = Number(v & 0xffn);
|
|
166
|
-
v >>= 8n;
|
|
167
|
-
}
|
|
168
|
-
const plaintext = new Uint8Array(72);
|
|
169
|
-
plaintext.set(amountBuf, 0);
|
|
170
|
-
plaintext.set(bigIntToBytesBE(utxo.blinding), 8);
|
|
171
|
-
plaintext.set(bigIntToBytesBE(utxo.privateKey), 40);
|
|
172
|
-
const sharedKey = nacl.box.before(recipientEncPubkey, ephemeral.secretKey);
|
|
173
|
-
const ciphertext = nacl.box.after(plaintext, nonce, sharedKey);
|
|
174
|
-
return { nonce, ciphertext, senderEphemeralPubkey: ephemeral.publicKey };
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Try to decrypt an encrypted UTXO note using the recipient's private key.
|
|
178
|
-
* Returns the SerializedUTXO if decryption succeeds and the commitment
|
|
179
|
-
* matches, or `null` if the note was not intended for this key.
|
|
180
|
-
*/
|
|
181
|
-
export function decryptUTXONote(note, recipientPrivateKey, commitment, mintAddress) {
|
|
182
|
-
const { secretKey } = deriveEncryptionKeypair(recipientPrivateKey);
|
|
183
|
-
const sharedKey = nacl.box.before(note.senderEphemeralPubkey, secretKey);
|
|
184
|
-
const plaintext = nacl.box.open.after(note.ciphertext, note.nonce, sharedKey);
|
|
185
|
-
if (!plaintext || plaintext.length < 72)
|
|
186
|
-
return null;
|
|
187
|
-
let amount = 0n;
|
|
188
|
-
for (let i = 7; i >= 0; i--)
|
|
189
|
-
amount = (amount << 8n) | BigInt(plaintext[i]);
|
|
190
|
-
const blinding = bytesToBigIntBE(plaintext.slice(8, 40)) % BN254_FR_MODULUS;
|
|
191
|
-
const privateKey = bytesToBigIntBE(plaintext.slice(40, 72)) % BN254_FR_MODULUS;
|
|
192
|
-
const pubkey = derivePublicKey(privateKey);
|
|
193
|
-
const utxo = {
|
|
194
|
-
amount,
|
|
195
|
-
pubkey,
|
|
196
|
-
blinding,
|
|
197
|
-
mintAddress: pubkeyToField(mintAddress),
|
|
198
|
-
};
|
|
199
|
-
const expectedCommitment = commitUTXO(utxo);
|
|
200
|
-
if (!expectedCommitment.every((b, i) => b === commitment[i]))
|
|
201
|
-
return null;
|
|
202
|
-
return { ...utxo, commitment, privateKey };
|
|
203
|
-
}
|
|
204
|
-
// -- Internal crypto helpers (no extra dependencies) --------------------------
|
|
205
|
-
/** Derive 32-byte encryption key from a shared secret using SHA-512 truncated.
|
|
206
|
-
* Must match the production "sha256" helper which uses nacl.hash (SHA-512) [0:32]. */
|
|
207
|
-
function kdfSHA512_256(secret) {
|
|
208
|
-
return nacl.hash(secret).slice(0, 32);
|
|
209
|
-
}
|
|
210
|
-
/** Convert an Ed25519 private seed (32 bytes) to an X25519 scalar.
|
|
211
|
-
* Equivalent to ed2curve.convertSecretKey(seed). */
|
|
212
|
-
function ed25519SeedToX25519Private(seed) {
|
|
213
|
-
const h = nacl.hash(seed); // SHA-512
|
|
214
|
-
const scalar = h.slice(0, 32);
|
|
215
|
-
scalar[0] &= 248;
|
|
216
|
-
scalar[31] &= 127;
|
|
217
|
-
scalar[31] |= 64;
|
|
218
|
-
return scalar;
|
|
219
|
-
}
|
|
220
|
-
const P25519 = (1n << 255n) - 19n;
|
|
221
|
-
function modpow25519(b, e) {
|
|
222
|
-
let r = 1n;
|
|
223
|
-
b = b % P25519;
|
|
224
|
-
while (e > 0n) {
|
|
225
|
-
if (e & 1n)
|
|
226
|
-
r = (r * b) % P25519;
|
|
227
|
-
e >>= 1n;
|
|
228
|
-
b = (b * b) % P25519;
|
|
229
|
-
}
|
|
230
|
-
return r;
|
|
231
|
-
}
|
|
232
|
-
/** Convert an Ed25519 compressed public key (32 bytes) to X25519.
|
|
233
|
-
* Equivalent to ed2curve.convertPublicKey(pubkey).
|
|
234
|
-
* Edwards y-coord → Montgomery u via: u = (1 + y) / (1 - y) (mod p). */
|
|
235
|
-
function ed25519PublicKeyToX25519(pubkey) {
|
|
236
|
-
const pk = new Uint8Array(pubkey);
|
|
237
|
-
pk[31] &= 0x7f; // clear sign bit to get y as LE integer
|
|
238
|
-
// Decode little-endian y
|
|
239
|
-
let y = 0n;
|
|
240
|
-
for (let i = 31; i >= 0; i--)
|
|
241
|
-
y = (y << 8n) | BigInt(pk[i]);
|
|
242
|
-
// u = (1 + y) / (1 - y) mod p
|
|
243
|
-
const num = (1n + y) % P25519;
|
|
244
|
-
const den = (((1n - y) % P25519) + P25519) % P25519;
|
|
245
|
-
const u = (num * modpow25519(den, P25519 - 2n)) % P25519;
|
|
246
|
-
// Encode as 32-byte LE
|
|
247
|
-
const out = new Uint8Array(32);
|
|
248
|
-
let v = u;
|
|
249
|
-
for (let i = 0; i < 32; i++) {
|
|
250
|
-
out[i] = Number(v & 0xffn);
|
|
251
|
-
v >>= 8n;
|
|
252
|
-
}
|
|
253
|
-
return out;
|
|
254
|
-
}
|
|
255
|
-
// -- Public API ---------------------------------------------------------------
|
|
256
|
-
/**
|
|
257
|
-
* Encrypt a UTXO note for a recipient identified by their Solana wallet public key.
|
|
258
|
-
* Produces a blob the relayer will store and serve via POST /notes/save.
|
|
259
|
-
*
|
|
260
|
-
* @param recipientWalletPubkey 32-byte Ed25519 public key (wallet.publicKey.toBytes())
|
|
261
|
-
* @param noteData UTXO details to encrypt
|
|
262
|
-
*/
|
|
263
|
-
export function encryptBlindMailboxNote(recipientWalletPubkey, noteData) {
|
|
264
|
-
// 1. Generate ephemeral Ed25519 keypair seed
|
|
265
|
-
const ephemeralSeed = randomBytes(32);
|
|
266
|
-
// Ed25519 public key stored by relayer; dapp recovers X25519 via ed2curve.convertPublicKey()
|
|
267
|
-
const ephemeralEdPublicKey = nacl.sign.keyPair.fromSeed(new Uint8Array(ephemeralSeed)).publicKey;
|
|
268
|
-
// X25519 scalar (ed2curve.convertSecretKey equivalent)
|
|
269
|
-
const ephemeralX25519Private = ed25519SeedToX25519Private(new Uint8Array(ephemeralSeed));
|
|
270
|
-
// X25519 public key (ed2curve.convertPublicKey equivalent)
|
|
271
|
-
const recipientX25519Public = ed25519PublicKeyToX25519(recipientWalletPubkey);
|
|
272
|
-
const sharedSecret = nacl.scalarMult(ephemeralX25519Private, recipientX25519Public);
|
|
273
|
-
const key = kdfSHA512_256(sharedSecret);
|
|
274
|
-
// JSON format matches production relayer notes schema
|
|
275
|
-
const hexArr = (b) => Array.from(b)
|
|
276
|
-
.map((x) => x.toString(16).padStart(2, "0"))
|
|
277
|
-
.join("");
|
|
278
|
-
const noteJson = JSON.stringify({
|
|
279
|
-
blinding: hexArr(noteData.blinding),
|
|
280
|
-
leafIndex: noteData.leafIndex,
|
|
281
|
-
commitment: hexArr(noteData.commitment),
|
|
282
|
-
amount: noteData.amount.toString(),
|
|
283
|
-
mintAddress: noteData.mintAddress,
|
|
284
|
-
timestamp: Date.now(),
|
|
285
|
-
treeId: noteData.treeId,
|
|
286
|
-
});
|
|
287
|
-
const nonce = nacl.randomBytes(24);
|
|
288
|
-
const plaintext = new TextEncoder().encode(noteJson);
|
|
289
|
-
const encrypted = nacl.secretbox(plaintext, nonce, key);
|
|
290
|
-
const combined = new Uint8Array(nonce.length + encrypted.length);
|
|
291
|
-
combined.set(nonce);
|
|
292
|
-
combined.set(encrypted, nonce.length);
|
|
293
|
-
const encryptedBlob = Buffer.from(combined).toString("base64");
|
|
294
|
-
// Return Ed25519 public key (dapp calls ed2curve.convertPublicKey() to recover X25519)
|
|
295
|
-
return { ephemeralPublicKey: ephemeralEdPublicKey, encryptedBlob };
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* Decrypt a Blind Mailbox note using the recipient's Solana wallet private seed.
|
|
299
|
-
*
|
|
300
|
-
* @param walletSecretKey Full 64-byte wallet.secretKey — the first 32 bytes are the seed
|
|
301
|
-
* @param ephemeralPublicKey 32-byte raw public key stored alongside the encrypted blob.
|
|
302
|
-
* This may be either an Ed25519 pubkey (from Keypair.generate())
|
|
303
|
-
* or a raw X25519 pubkey — the decryption path handles both:
|
|
304
|
-
* if it is an Ed25519 key it is converted; otherwise used directly.
|
|
305
|
-
* @param encryptedBlob Base64 string as stored by the relayer
|
|
306
|
-
*/
|
|
307
|
-
export function decryptBlindMailboxNote(walletSecretKey, ephemeralPublicKey, encryptedBlob) {
|
|
308
|
-
// 1. Derive recipient X25519 from Ed25519 seed
|
|
309
|
-
const seed = walletSecretKey.slice(0, 32);
|
|
310
|
-
const myX25519Private = ed25519SeedToX25519Private(seed);
|
|
311
|
-
// ephemeralPublicKey may be an Ed25519 pubkey (dapp's Keypair.generate()) or raw X25519;
|
|
312
|
-
// try Ed25519→X25519 conversion first, fall back to using it directly.
|
|
313
|
-
let theirPublic;
|
|
314
|
-
try {
|
|
315
|
-
theirPublic = ed25519PublicKeyToX25519(ephemeralPublicKey);
|
|
316
|
-
}
|
|
317
|
-
catch {
|
|
318
|
-
theirPublic = ephemeralPublicKey; // already X25519
|
|
319
|
-
}
|
|
320
|
-
const sharedSecret = nacl.scalarMult(myX25519Private, theirPublic);
|
|
321
|
-
const key = kdfSHA512_256(sharedSecret);
|
|
322
|
-
const combined = Buffer.from(encryptedBlob, "base64");
|
|
323
|
-
const nonce = combined.slice(0, 24);
|
|
324
|
-
const ciphertext = combined.slice(24);
|
|
325
|
-
const decrypted = nacl.secretbox.open(new Uint8Array(ciphertext), new Uint8Array(nonce), key);
|
|
326
|
-
if (!decrypted) {
|
|
327
|
-
throw new Error("decryptBlindMailboxNote: decryption failed — wrong key or corrupted blob");
|
|
328
|
-
}
|
|
329
|
-
const parsed = JSON.parse(new TextDecoder().decode(decrypted));
|
|
330
|
-
const hexToBytes = (hex) => {
|
|
331
|
-
const out = new Uint8Array(hex.length / 2);
|
|
332
|
-
for (let i = 0; i < hex.length; i += 2)
|
|
333
|
-
out[i / 2] = parseInt(hex.slice(i, i + 2), 16);
|
|
334
|
-
return out;
|
|
335
|
-
};
|
|
336
|
-
return {
|
|
337
|
-
blinding: hexToBytes(parsed.blinding),
|
|
338
|
-
leafIndex: parsed.leafIndex,
|
|
339
|
-
commitment: hexToBytes(parsed.commitment),
|
|
340
|
-
amount: BigInt(parsed.amount),
|
|
341
|
-
timestamp: parsed.timestamp ?? 0,
|
|
342
|
-
mintAddress: parsed.mintAddress,
|
|
343
|
-
treeId: parsed.treeId,
|
|
344
|
-
};
|
|
345
|
-
}
|
|
346
|
-
/**
|
|
347
|
-
* Fetch all encrypted notes for a wallet from the relayer's Blind Mailbox endpoint
|
|
348
|
-
* and decrypt them using the wallet's secret key.
|
|
349
|
-
*
|
|
350
|
-
* @param walletPublicKey Base58 wallet address used to query the relayer
|
|
351
|
-
* @param walletSecretKey Full 64-byte keypair.secretKey for decryption
|
|
352
|
-
* @param relayerUrl Relayer base URL (e.g. "https://relay.veilo.app")
|
|
353
|
-
*/
|
|
354
|
-
export async function fetchAndDecryptNotes(walletPublicKey, walletSecretKey, relayerUrl) {
|
|
355
|
-
const resp = await fetch(`${relayerUrl}/api/notes?recipientPublicKey=${walletPublicKey}`);
|
|
356
|
-
if (!resp.ok) {
|
|
357
|
-
throw new Error(`fetchAndDecryptNotes: ${resp.status} ${resp.statusText}`);
|
|
358
|
-
}
|
|
359
|
-
const { notes } = (await resp.json());
|
|
360
|
-
const results = [];
|
|
361
|
-
for (const note of notes) {
|
|
362
|
-
try {
|
|
363
|
-
const epk = Buffer.from(note.ephemeralPublicKey, "base64");
|
|
364
|
-
const decrypted = decryptBlindMailboxNote(walletSecretKey, new Uint8Array(epk), note.encryptedBlob);
|
|
365
|
-
results.push(decrypted);
|
|
366
|
-
}
|
|
367
|
-
catch {
|
|
368
|
-
// Note not for this key or corrupted — skip silently
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
return results;
|
|
372
|
-
}
|
|
1
|
+
// Backward-compatible entry point. Note responsibilities live in src/notes.
|
|
2
|
+
export * from "./notes/index.js";
|