@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,68 @@
|
|
|
1
|
+
// -----------------------------------------------------------------------------
|
|
2
|
+
// Proof Packing
|
|
3
|
+
// -----------------------------------------------------------------------------
|
|
4
|
+
/**
|
|
5
|
+
* Convert bigint to 32-byte big-endian representation.
|
|
6
|
+
* The alt_bn128 Solana precompile (and ark-works via change_endianness) expect BE.
|
|
7
|
+
*/
|
|
8
|
+
function bigintTo32BytesBE(x) {
|
|
9
|
+
const out = new Uint8Array(32);
|
|
10
|
+
let v = x;
|
|
11
|
+
for (let i = 31; i >= 0; i--) {
|
|
12
|
+
out[i] = Number(v & 0xffn);
|
|
13
|
+
v >>= 8n;
|
|
14
|
+
}
|
|
15
|
+
return out;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Turn a snarkjs Groth16 proof into 256 bytes:
|
|
19
|
+
* A(G1) | B(G2) | C(G1)
|
|
20
|
+
* where:
|
|
21
|
+
* A = (ax, ay) -> 64 bytes
|
|
22
|
+
* B = (bx1, bx0, by1, by0) -> 128 bytes (swapped pairs for alt_bn128)
|
|
23
|
+
* C = (cx, cy) -> 64 bytes
|
|
24
|
+
*
|
|
25
|
+
* Each field element is encoded as 32-byte BE to match the on-chain verifier.
|
|
26
|
+
* We ignore the extra projective/z components snarkjs keeps.
|
|
27
|
+
*/
|
|
28
|
+
export function packProofToBytes(proof) {
|
|
29
|
+
const chunks = [];
|
|
30
|
+
const ax = BigInt(proof.pi_a[0]);
|
|
31
|
+
const ay = BigInt(proof.pi_a[1]);
|
|
32
|
+
chunks.push(bigintTo32BytesBE(ax), bigintTo32BytesBE(ay));
|
|
33
|
+
const bx = proof.pi_b[0];
|
|
34
|
+
const by = proof.pi_b[1];
|
|
35
|
+
const bx0 = BigInt(bx[0]);
|
|
36
|
+
const bx1 = BigInt(bx[1]);
|
|
37
|
+
const by0 = BigInt(by[0]);
|
|
38
|
+
const by1 = BigInt(by[1]);
|
|
39
|
+
// alt_bn128 expects the G2 pairs swapped: bx1 before bx0, by1 before by0
|
|
40
|
+
chunks.push(bigintTo32BytesBE(bx1), bigintTo32BytesBE(bx0), bigintTo32BytesBE(by1), bigintTo32BytesBE(by0));
|
|
41
|
+
const cx = BigInt(proof.pi_c[0]);
|
|
42
|
+
const cy = BigInt(proof.pi_c[1]);
|
|
43
|
+
chunks.push(bigintTo32BytesBE(cx), bigintTo32BytesBE(cy));
|
|
44
|
+
const out = new Uint8Array(32 * (2 + 4 + 2)); // 256
|
|
45
|
+
let offset = 0;
|
|
46
|
+
for (const c of chunks) {
|
|
47
|
+
out.set(c, offset);
|
|
48
|
+
offset += c.length;
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Encode a snarkjs proof into the TransactionProofStruct format expected by the on-chain program.
|
|
54
|
+
*/
|
|
55
|
+
export function encodeSnarkjsProofToTransactionProof(proof) {
|
|
56
|
+
const full = packProofToBytes(proof);
|
|
57
|
+
if (full.length !== 256) {
|
|
58
|
+
throw new Error(`Expected 256 proof bytes, got ${full.length}`);
|
|
59
|
+
}
|
|
60
|
+
const proofA = full.slice(0, 64);
|
|
61
|
+
const proofB = full.slice(64, 192);
|
|
62
|
+
const proofC = full.slice(192, 256);
|
|
63
|
+
return {
|
|
64
|
+
proofA: Array.from(proofA),
|
|
65
|
+
proofB: Array.from(proofB),
|
|
66
|
+
proofC: Array.from(proofC),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { TransactionCircuitInputs } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Convert TransactionCircuitInputs to the format expected by snarkjs.groth16.fullProve().
|
|
4
|
+
* All values are converted to strings (snarkjs expects string inputs for field elements).
|
|
5
|
+
*/
|
|
6
|
+
export declare function formatInputsForSnarkjs(inputs: TransactionCircuitInputs): Record<string, any>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { bytesToBigIntBE, pubkeyToField } from "../poseidon.js";
|
|
2
|
+
import { i64ToField } from "./transaction.js";
|
|
3
|
+
// -----------------------------------------------------------------------------
|
|
4
|
+
// Snarkjs Input Formatting
|
|
5
|
+
// -----------------------------------------------------------------------------
|
|
6
|
+
/**
|
|
7
|
+
* Convert TransactionCircuitInputs to the format expected by snarkjs.groth16.fullProve().
|
|
8
|
+
* All values are converted to strings (snarkjs expects string inputs for field elements).
|
|
9
|
+
*/
|
|
10
|
+
export function formatInputsForSnarkjs(inputs) {
|
|
11
|
+
return {
|
|
12
|
+
// Public inputs
|
|
13
|
+
root: bytesToBigIntBE(inputs.root).toString(),
|
|
14
|
+
publicAmount: i64ToField(inputs.publicAmount).toString(),
|
|
15
|
+
extDataHash: bytesToBigIntBE(inputs.extDataHash).toString(),
|
|
16
|
+
mintAddress: pubkeyToField(inputs.mintAddress).toString(),
|
|
17
|
+
inputNullifier: inputs.inputNullifiers.map((n) => bytesToBigIntBE(n).toString()),
|
|
18
|
+
outputCommitment: inputs.outputCommitments.map((c) => bytesToBigIntBE(c).toString()),
|
|
19
|
+
// Private inputs - Input UTXOs
|
|
20
|
+
inAmount: inputs.inAmount.map((a) => a.toString()),
|
|
21
|
+
inPubkey: inputs.inPubkey.map((p) => p.toString()),
|
|
22
|
+
inBlinding: inputs.inBlinding.map((b) => b.toString()),
|
|
23
|
+
inPathIndex: inputs.inPathIndex.map((i) => i.toString()),
|
|
24
|
+
inPathElements: inputs.inPathElements.map((path) => path.map((e) => e.toString())),
|
|
25
|
+
inPrivateKey: inputs.inPrivateKey.map((k) => k.toString()),
|
|
26
|
+
// Private inputs - Output UTXOs
|
|
27
|
+
outAmount: inputs.outAmount.map((a) => a.toString()),
|
|
28
|
+
outPubkey: inputs.outPubkey.map((p) => p.toString()),
|
|
29
|
+
outBlinding: inputs.outBlinding.map((b) => b.toString()),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
|
2
|
+
import type { InputUTXO, SerializedUTXO } from "../notes/model.js";
|
|
3
|
+
import type { ExtData, RawProof } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Swap circuit inputs. Matches the signal shape declared by swap.circom's
|
|
6
|
+
* main component (see zk-circuits/swap.circom lines 208-238). Unlike the
|
|
7
|
+
* transaction circuit, the swap circuit distinguishes a "change" output
|
|
8
|
+
* (same mint as source) from a "dest" output (dest mint) and takes a
|
|
9
|
+
* swap-specific params hash and swapAmount alongside the usual inputs.
|
|
10
|
+
*/
|
|
11
|
+
export type SwapCircuitInputs = {
|
|
12
|
+
sourceRoot: Uint8Array;
|
|
13
|
+
swapParamsHash: Uint8Array;
|
|
14
|
+
extDataHash: Uint8Array;
|
|
15
|
+
sourceMint: PublicKey;
|
|
16
|
+
destMint: PublicKey;
|
|
17
|
+
inputNullifiers: [Uint8Array, Uint8Array];
|
|
18
|
+
changeCommitment: Uint8Array;
|
|
19
|
+
destCommitment: Uint8Array;
|
|
20
|
+
swapAmount: bigint;
|
|
21
|
+
inAmount: [bigint, bigint];
|
|
22
|
+
inPubkey: [bigint, bigint];
|
|
23
|
+
inBlinding: [bigint, bigint];
|
|
24
|
+
inPathIndex: [number, number];
|
|
25
|
+
inPathElements: [bigint[], bigint[]];
|
|
26
|
+
inPrivateKey: [bigint, bigint];
|
|
27
|
+
changeAmount: bigint;
|
|
28
|
+
changePubkey: bigint;
|
|
29
|
+
changeBlinding: bigint;
|
|
30
|
+
destAmount: bigint;
|
|
31
|
+
destPubkey: bigint;
|
|
32
|
+
destBlinding: bigint;
|
|
33
|
+
minAmountOut: bigint;
|
|
34
|
+
deadline: bigint;
|
|
35
|
+
};
|
|
36
|
+
/** Proof builder accepted by SDK-side private swap flows. */
|
|
37
|
+
export type SwapProofBuilder = (inputs: SwapCircuitInputs) => Promise<RawProof>;
|
|
38
|
+
/**
|
|
39
|
+
* Prepare the exact witness/public inputs used by the relayer's swap circuit.
|
|
40
|
+
* Output 0 must be source-mint change and output 1 the destination-mint note.
|
|
41
|
+
*/
|
|
42
|
+
export declare function prepareSwapInputs(params: {
|
|
43
|
+
inputUTXOs: [InputUTXO, InputUTXO];
|
|
44
|
+
changeUTXO: SerializedUTXO;
|
|
45
|
+
destUTXO: SerializedUTXO;
|
|
46
|
+
sourceRoot: Uint8Array;
|
|
47
|
+
sourceMint: PublicKey;
|
|
48
|
+
destMint: PublicKey;
|
|
49
|
+
swapAmount: bigint;
|
|
50
|
+
minAmountOut: bigint;
|
|
51
|
+
deadline: bigint;
|
|
52
|
+
extData: ExtData;
|
|
53
|
+
}): SwapCircuitInputs;
|
|
54
|
+
/**
|
|
55
|
+
* Convert SwapCircuitInputs into the shape expected by
|
|
56
|
+
* snarkjs.groth16.fullProve() for swap.circom. Every value is emitted as a
|
|
57
|
+
* decimal string (or string array) because snarkjs only accepts field
|
|
58
|
+
* elements as strings.
|
|
59
|
+
*
|
|
60
|
+
* Mirrors `generateSwapProof` in
|
|
61
|
+
* relayer-server/src/controllers/swap.helpers.ts so SDK-side swap proofs
|
|
62
|
+
* verify against the same circuit the relayer uses.
|
|
63
|
+
*/
|
|
64
|
+
export declare function formatSwapInputsForSnarkjs(inputs: SwapCircuitInputs): Record<string, any>;
|
|
65
|
+
/**
|
|
66
|
+
* Compute the swap params hash that the swap circuit and on-chain program verify.
|
|
67
|
+
* Mirrors relayer-server/src/controllers/swap.helpers.ts `computeSwapParamsHash`.
|
|
68
|
+
*
|
|
69
|
+
* Circuit formula (swap.circom lines 293-307):
|
|
70
|
+
* mintPairHash = Poseidon(sourceMint, destMint)
|
|
71
|
+
* swapTermsHash = Poseidon(minAmountOut, deadline, destAmount)
|
|
72
|
+
* swapParamsHash = Poseidon(mintPairHash, swapTermsHash)
|
|
73
|
+
*
|
|
74
|
+
* @param sourceMint Source pool mint
|
|
75
|
+
* @param destMint Destination pool mint
|
|
76
|
+
* @param minAmountOut Minimum accepted output amount (slippage protected)
|
|
77
|
+
* @param deadline Positive Unix timestamp deadline (the program rejects 0)
|
|
78
|
+
* @param destAmount Actual amount committed to destination UTXO (≥ minAmountOut -
|
|
79
|
+
* relayerFee). If omitted, defaults to minAmountOut.
|
|
80
|
+
*/
|
|
81
|
+
export declare function computeSwapParamsHash(sourceMint: PublicKey, destMint: PublicKey, minAmountOut: bigint, deadline: bigint, destAmount?: bigint): Uint8Array;
|
|
82
|
+
/**
|
|
83
|
+
* Compute the swap data hash committed on-chain for Jupiter instruction integrity.
|
|
84
|
+
* SHA-256 of the raw Jupiter swap instruction data bytes.
|
|
85
|
+
* Mirrors relayer-server/src/controllers/swap.helpers.ts `computeSwapDataHash`.
|
|
86
|
+
*/
|
|
87
|
+
export declare function computeSwapDataHash(swapData: Uint8Array | Buffer): Uint8Array;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { sha256 } from "@noble/hashes/sha256";
|
|
2
|
+
import { BN254_FR_MODULUS, bigIntToBytesBE, bytesToBigIntBE, poseidon2, poseidon3, pubkeyToField, } from "../poseidon.js";
|
|
3
|
+
import { deriveNullifier } from "../notes/nullifier.js";
|
|
4
|
+
import { computeExtDataHash } from "./transaction.js";
|
|
5
|
+
/**
|
|
6
|
+
* Prepare the exact witness/public inputs used by the relayer's swap circuit.
|
|
7
|
+
* Output 0 must be source-mint change and output 1 the destination-mint note.
|
|
8
|
+
*/
|
|
9
|
+
export function prepareSwapInputs(params) {
|
|
10
|
+
const { inputUTXOs, changeUTXO, destUTXO, sourceRoot, sourceMint, destMint, swapAmount, minAmountOut, deadline, extData, } = params;
|
|
11
|
+
const totalInput = inputUTXOs[0].amount + inputUTXOs[1].amount;
|
|
12
|
+
if (swapAmount <= 0n)
|
|
13
|
+
throw new Error("swapAmount must be positive");
|
|
14
|
+
if (deadline <= 0n) {
|
|
15
|
+
throw new Error("deadline must be a positive Unix timestamp");
|
|
16
|
+
}
|
|
17
|
+
if (totalInput !== swapAmount + changeUTXO.amount) {
|
|
18
|
+
throw new Error(`Swap balance mismatch: inputs=${totalInput}, swap=${swapAmount}, change=${changeUTXO.amount}`);
|
|
19
|
+
}
|
|
20
|
+
if (destUTXO.amount < minAmountOut) {
|
|
21
|
+
throw new Error("Destination note amount is below minAmountOut");
|
|
22
|
+
}
|
|
23
|
+
if (changeUTXO.mintAddress !== pubkeyToField(sourceMint)) {
|
|
24
|
+
throw new Error("Change UTXO must use the source mint");
|
|
25
|
+
}
|
|
26
|
+
if (destUTXO.mintAddress !== pubkeyToField(destMint)) {
|
|
27
|
+
throw new Error("Destination UTXO must use the destination mint");
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
sourceRoot,
|
|
31
|
+
swapParamsHash: computeSwapParamsHash(sourceMint, destMint, minAmountOut, deadline, destUTXO.amount),
|
|
32
|
+
extDataHash: computeExtDataHash(extData),
|
|
33
|
+
sourceMint,
|
|
34
|
+
destMint,
|
|
35
|
+
inputNullifiers: [
|
|
36
|
+
deriveNullifier(inputUTXOs[0], inputUTXOs[0].pathIndex, inputUTXOs[0].privateKey),
|
|
37
|
+
deriveNullifier(inputUTXOs[1], inputUTXOs[1].pathIndex, inputUTXOs[1].privateKey),
|
|
38
|
+
],
|
|
39
|
+
changeCommitment: changeUTXO.commitment,
|
|
40
|
+
destCommitment: destUTXO.commitment,
|
|
41
|
+
swapAmount,
|
|
42
|
+
inAmount: [inputUTXOs[0].amount, inputUTXOs[1].amount],
|
|
43
|
+
inPubkey: [inputUTXOs[0].pubkey, inputUTXOs[1].pubkey],
|
|
44
|
+
inBlinding: [inputUTXOs[0].blinding, inputUTXOs[1].blinding],
|
|
45
|
+
inPathIndex: [inputUTXOs[0].pathIndex, inputUTXOs[1].pathIndex],
|
|
46
|
+
inPathElements: [
|
|
47
|
+
inputUTXOs[0].pathElements.map((element) => bytesToBigIntBE(element) % BN254_FR_MODULUS),
|
|
48
|
+
inputUTXOs[1].pathElements.map((element) => bytesToBigIntBE(element) % BN254_FR_MODULUS),
|
|
49
|
+
],
|
|
50
|
+
inPrivateKey: [inputUTXOs[0].privateKey, inputUTXOs[1].privateKey],
|
|
51
|
+
changeAmount: changeUTXO.amount,
|
|
52
|
+
changePubkey: changeUTXO.pubkey,
|
|
53
|
+
changeBlinding: changeUTXO.blinding,
|
|
54
|
+
destAmount: destUTXO.amount,
|
|
55
|
+
destPubkey: destUTXO.pubkey,
|
|
56
|
+
destBlinding: destUTXO.blinding,
|
|
57
|
+
minAmountOut,
|
|
58
|
+
deadline,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Convert SwapCircuitInputs into the shape expected by
|
|
63
|
+
* snarkjs.groth16.fullProve() for swap.circom. Every value is emitted as a
|
|
64
|
+
* decimal string (or string array) because snarkjs only accepts field
|
|
65
|
+
* elements as strings.
|
|
66
|
+
*
|
|
67
|
+
* Mirrors `generateSwapProof` in
|
|
68
|
+
* relayer-server/src/controllers/swap.helpers.ts so SDK-side swap proofs
|
|
69
|
+
* verify against the same circuit the relayer uses.
|
|
70
|
+
*/
|
|
71
|
+
export function formatSwapInputsForSnarkjs(inputs) {
|
|
72
|
+
return {
|
|
73
|
+
// Public inputs
|
|
74
|
+
sourceRoot: bytesToBigIntBE(inputs.sourceRoot).toString(),
|
|
75
|
+
swapParamsHash: bytesToBigIntBE(inputs.swapParamsHash).toString(),
|
|
76
|
+
extDataHash: bytesToBigIntBE(inputs.extDataHash).toString(),
|
|
77
|
+
sourceMint: pubkeyToField(inputs.sourceMint).toString(),
|
|
78
|
+
destMint: pubkeyToField(inputs.destMint).toString(),
|
|
79
|
+
inputNullifier: inputs.inputNullifiers.map((n) => bytesToBigIntBE(n).toString()),
|
|
80
|
+
changeCommitment: bytesToBigIntBE(inputs.changeCommitment).toString(),
|
|
81
|
+
destCommitment: bytesToBigIntBE(inputs.destCommitment).toString(),
|
|
82
|
+
swapAmount: inputs.swapAmount.toString(),
|
|
83
|
+
// Private inputs - Input UTXOs
|
|
84
|
+
inAmount: inputs.inAmount.map((a) => a.toString()),
|
|
85
|
+
inPubkey: inputs.inPubkey.map((p) => p.toString()),
|
|
86
|
+
inBlinding: inputs.inBlinding.map((b) => b.toString()),
|
|
87
|
+
inPathIndex: inputs.inPathIndex.map((i) => i.toString()),
|
|
88
|
+
inPathElements: inputs.inPathElements.map((path) => path.map((e) => e.toString())),
|
|
89
|
+
inPrivateKey: inputs.inPrivateKey.map((k) => k.toString()),
|
|
90
|
+
// Private inputs - Change UTXO
|
|
91
|
+
changeAmount: inputs.changeAmount.toString(),
|
|
92
|
+
changePubkey: inputs.changePubkey.toString(),
|
|
93
|
+
changeBlinding: inputs.changeBlinding.toString(),
|
|
94
|
+
// Private inputs - Destination UTXO
|
|
95
|
+
destAmount: inputs.destAmount.toString(),
|
|
96
|
+
destPubkey: inputs.destPubkey.toString(),
|
|
97
|
+
destBlinding: inputs.destBlinding.toString(),
|
|
98
|
+
// Swap params
|
|
99
|
+
minAmountOut: inputs.minAmountOut.toString(),
|
|
100
|
+
deadline: inputs.deadline.toString(),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
// -----------------------------------------------------------------------------
|
|
104
|
+
// Swap Parameter Hashing
|
|
105
|
+
// -----------------------------------------------------------------------------
|
|
106
|
+
/**
|
|
107
|
+
* Compute the swap params hash that the swap circuit and on-chain program verify.
|
|
108
|
+
* Mirrors relayer-server/src/controllers/swap.helpers.ts `computeSwapParamsHash`.
|
|
109
|
+
*
|
|
110
|
+
* Circuit formula (swap.circom lines 293-307):
|
|
111
|
+
* mintPairHash = Poseidon(sourceMint, destMint)
|
|
112
|
+
* swapTermsHash = Poseidon(minAmountOut, deadline, destAmount)
|
|
113
|
+
* swapParamsHash = Poseidon(mintPairHash, swapTermsHash)
|
|
114
|
+
*
|
|
115
|
+
* @param sourceMint Source pool mint
|
|
116
|
+
* @param destMint Destination pool mint
|
|
117
|
+
* @param minAmountOut Minimum accepted output amount (slippage protected)
|
|
118
|
+
* @param deadline Positive Unix timestamp deadline (the program rejects 0)
|
|
119
|
+
* @param destAmount Actual amount committed to destination UTXO (≥ minAmountOut -
|
|
120
|
+
* relayerFee). If omitted, defaults to minAmountOut.
|
|
121
|
+
*/
|
|
122
|
+
export function computeSwapParamsHash(sourceMint, destMint, minAmountOut, deadline, destAmount) {
|
|
123
|
+
const srcField = pubkeyToField(sourceMint);
|
|
124
|
+
const dstField = pubkeyToField(destMint);
|
|
125
|
+
const mintPairHash = poseidon2(srcField, dstField);
|
|
126
|
+
const dest = destAmount ?? minAmountOut;
|
|
127
|
+
const swapTermsHash = poseidon3(minAmountOut, deadline, dest);
|
|
128
|
+
const paramsHash = poseidon2(mintPairHash, swapTermsHash);
|
|
129
|
+
return bigIntToBytesBE(paramsHash);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Compute the swap data hash committed on-chain for Jupiter instruction integrity.
|
|
133
|
+
* SHA-256 of the raw Jupiter swap instruction data bytes.
|
|
134
|
+
* Mirrors relayer-server/src/controllers/swap.helpers.ts `computeSwapDataHash`.
|
|
135
|
+
*/
|
|
136
|
+
export function computeSwapDataHash(swapData) {
|
|
137
|
+
return sha256(swapData instanceof Buffer ? new Uint8Array(swapData) : swapData);
|
|
138
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
|
2
|
+
import type { InputUTXO, SerializedUTXO } from "../notes/model.js";
|
|
3
|
+
import type { ExtData, TransactionCircuitInputs } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Compute the external data hash.
|
|
6
|
+
* Matches on-chain computation:
|
|
7
|
+
* hash1 = Poseidon(recipient, relayer)
|
|
8
|
+
* hash2 = Poseidon(fee, refund)
|
|
9
|
+
* extDataHash = Poseidon(hash1, hash2, claimant)
|
|
10
|
+
*/
|
|
11
|
+
export declare function computeExtDataHash(extData: ExtData): Uint8Array;
|
|
12
|
+
/**
|
|
13
|
+
* Convert i64 to field element (handles negative via modular arithmetic).
|
|
14
|
+
* For negative values, we add the field modulus to get the equivalent positive representation.
|
|
15
|
+
*/
|
|
16
|
+
export declare function i64ToField(value: bigint): bigint;
|
|
17
|
+
/**
|
|
18
|
+
* Prepare transaction circuit inputs from high-level parameters.
|
|
19
|
+
* This converts the SDK types into the format expected by the circuit.
|
|
20
|
+
*/
|
|
21
|
+
export declare function prepareTransactionInputs(params: {
|
|
22
|
+
inputUTXOs: [InputUTXO, InputUTXO];
|
|
23
|
+
outputUTXOs: [SerializedUTXO, SerializedUTXO];
|
|
24
|
+
root: Uint8Array;
|
|
25
|
+
publicAmount: bigint;
|
|
26
|
+
extData: ExtData;
|
|
27
|
+
mintAddress: PublicKey;
|
|
28
|
+
}): TransactionCircuitInputs;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { BN254_FR_MODULUS, bigIntToBytesBE, bytesToBigIntBE, poseidon2, poseidon3, pubkeyToField, } from "../poseidon.js";
|
|
2
|
+
import { deriveNullifier } from "../notes/nullifier.js";
|
|
3
|
+
// -----------------------------------------------------------------------------
|
|
4
|
+
// ExtData Hash Computation
|
|
5
|
+
// -----------------------------------------------------------------------------
|
|
6
|
+
/**
|
|
7
|
+
* Compute the external data hash.
|
|
8
|
+
* Matches on-chain computation:
|
|
9
|
+
* hash1 = Poseidon(recipient, relayer)
|
|
10
|
+
* hash2 = Poseidon(fee, refund)
|
|
11
|
+
* extDataHash = Poseidon(hash1, hash2, claimant)
|
|
12
|
+
*/
|
|
13
|
+
export function computeExtDataHash(extData) {
|
|
14
|
+
const recipientField = pubkeyToField(extData.recipient);
|
|
15
|
+
const relayerField = pubkeyToField(extData.relayer);
|
|
16
|
+
const feeField = extData.fee % BN254_FR_MODULUS;
|
|
17
|
+
const refundField = extData.refund % BN254_FR_MODULUS;
|
|
18
|
+
const claimantField = pubkeyToField(extData.claimant);
|
|
19
|
+
const hash1 = poseidon2(recipientField, relayerField);
|
|
20
|
+
const hash2 = poseidon2(feeField, refundField);
|
|
21
|
+
const finalHash = poseidon3(hash1, hash2, claimantField);
|
|
22
|
+
return bigIntToBytesBE(finalHash);
|
|
23
|
+
}
|
|
24
|
+
// -----------------------------------------------------------------------------
|
|
25
|
+
// Circuit Input Preparation
|
|
26
|
+
// -----------------------------------------------------------------------------
|
|
27
|
+
/**
|
|
28
|
+
* Convert i64 to field element (handles negative via modular arithmetic).
|
|
29
|
+
* For negative values, we add the field modulus to get the equivalent positive representation.
|
|
30
|
+
*/
|
|
31
|
+
export function i64ToField(value) {
|
|
32
|
+
if (value >= 0n) {
|
|
33
|
+
return value % BN254_FR_MODULUS;
|
|
34
|
+
}
|
|
35
|
+
return (BN254_FR_MODULUS + value) % BN254_FR_MODULUS;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Prepare transaction circuit inputs from high-level parameters.
|
|
39
|
+
* This converts the SDK types into the format expected by the circuit.
|
|
40
|
+
*/
|
|
41
|
+
export function prepareTransactionInputs(params) {
|
|
42
|
+
const { inputUTXOs, outputUTXOs, root, publicAmount, extData, mintAddress } = params;
|
|
43
|
+
const extDataHash = computeExtDataHash(extData);
|
|
44
|
+
const mintField = pubkeyToField(mintAddress);
|
|
45
|
+
// Prepare input UTXO data
|
|
46
|
+
const inAmount = [
|
|
47
|
+
inputUTXOs[0].amount,
|
|
48
|
+
inputUTXOs[1].amount,
|
|
49
|
+
];
|
|
50
|
+
const inPubkey = [
|
|
51
|
+
inputUTXOs[0].pubkey,
|
|
52
|
+
inputUTXOs[1].pubkey,
|
|
53
|
+
];
|
|
54
|
+
const inBlinding = [
|
|
55
|
+
inputUTXOs[0].blinding,
|
|
56
|
+
inputUTXOs[1].blinding,
|
|
57
|
+
];
|
|
58
|
+
const inPathIndex = [
|
|
59
|
+
inputUTXOs[0].pathIndex,
|
|
60
|
+
inputUTXOs[1].pathIndex,
|
|
61
|
+
];
|
|
62
|
+
const inPathElements = [
|
|
63
|
+
inputUTXOs[0].pathElements.map((e) => bytesToBigIntBE(e) % BN254_FR_MODULUS),
|
|
64
|
+
inputUTXOs[1].pathElements.map((e) => bytesToBigIntBE(e) % BN254_FR_MODULUS),
|
|
65
|
+
];
|
|
66
|
+
const inPrivateKey = [
|
|
67
|
+
inputUTXOs[0].privateKey,
|
|
68
|
+
inputUTXOs[1].privateKey,
|
|
69
|
+
];
|
|
70
|
+
// Prepare output UTXO data
|
|
71
|
+
const outAmount = [
|
|
72
|
+
outputUTXOs[0].amount,
|
|
73
|
+
outputUTXOs[1].amount,
|
|
74
|
+
];
|
|
75
|
+
const outPubkey = [
|
|
76
|
+
outputUTXOs[0].pubkey,
|
|
77
|
+
outputUTXOs[1].pubkey,
|
|
78
|
+
];
|
|
79
|
+
const outBlinding = [
|
|
80
|
+
outputUTXOs[0].blinding,
|
|
81
|
+
outputUTXOs[1].blinding,
|
|
82
|
+
];
|
|
83
|
+
// Derive nullifiers for input UTXOs
|
|
84
|
+
const inputNullifiers = [
|
|
85
|
+
deriveNullifier(inputUTXOs[0], inputUTXOs[0].pathIndex, inputUTXOs[0].privateKey),
|
|
86
|
+
deriveNullifier(inputUTXOs[1], inputUTXOs[1].pathIndex, inputUTXOs[1].privateKey),
|
|
87
|
+
];
|
|
88
|
+
return {
|
|
89
|
+
root,
|
|
90
|
+
publicAmount,
|
|
91
|
+
extDataHash,
|
|
92
|
+
mintAddress,
|
|
93
|
+
inputNullifiers,
|
|
94
|
+
outputCommitments: [outputUTXOs[0].commitment, outputUTXOs[1].commitment],
|
|
95
|
+
inAmount,
|
|
96
|
+
inPubkey,
|
|
97
|
+
inBlinding,
|
|
98
|
+
inPathIndex,
|
|
99
|
+
inPathElements,
|
|
100
|
+
inPrivateKey,
|
|
101
|
+
outAmount,
|
|
102
|
+
outPubkey,
|
|
103
|
+
outBlinding,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
|
2
|
+
/**
|
|
3
|
+
* External data that gets hashed into ext_data_hash.
|
|
4
|
+
* These are public parameters that affect financial flows.
|
|
5
|
+
*/
|
|
6
|
+
export type ExtData = {
|
|
7
|
+
/** Who receives withdrawal (for withdrawals) */
|
|
8
|
+
recipient: PublicKey;
|
|
9
|
+
/** Who submits tx and gets fee */
|
|
10
|
+
relayer: PublicKey;
|
|
11
|
+
/** Fee to relayer in lamports/token units */
|
|
12
|
+
fee: bigint;
|
|
13
|
+
/** Refund to user in lamports/token units (for gas compensation) */
|
|
14
|
+
refund: bigint;
|
|
15
|
+
/** Claimant key committed into the proof */
|
|
16
|
+
claimant: PublicKey;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Transaction circuit inputs (8 public inputs, many private inputs).
|
|
20
|
+
* This structure is used to prepare inputs for the snarkjs prover.
|
|
21
|
+
*/
|
|
22
|
+
export type TransactionCircuitInputs = {
|
|
23
|
+
/** Merkle root of UTXO tree */
|
|
24
|
+
root: Uint8Array;
|
|
25
|
+
/** Net deposit/withdrawal amount (positive = deposit, negative = withdrawal) */
|
|
26
|
+
publicAmount: bigint;
|
|
27
|
+
/** Hash of external data (recipient, relayer, fee, refund) */
|
|
28
|
+
extDataHash: Uint8Array;
|
|
29
|
+
/** Token mint address */
|
|
30
|
+
mintAddress: PublicKey;
|
|
31
|
+
/** Nullifiers for the two input UTXOs */
|
|
32
|
+
inputNullifiers: [Uint8Array, Uint8Array];
|
|
33
|
+
/** Commitments for the two output UTXOs */
|
|
34
|
+
outputCommitments: [Uint8Array, Uint8Array];
|
|
35
|
+
inAmount: [bigint, bigint];
|
|
36
|
+
inPubkey: [bigint, bigint];
|
|
37
|
+
inBlinding: [bigint, bigint];
|
|
38
|
+
inPathIndex: [number, number];
|
|
39
|
+
inPathElements: [bigint[], bigint[]];
|
|
40
|
+
inPrivateKey: [bigint, bigint];
|
|
41
|
+
outAmount: [bigint, bigint];
|
|
42
|
+
outPubkey: [bigint, bigint];
|
|
43
|
+
outBlinding: [bigint, bigint];
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Proof structure matching the on-chain TransactionProof struct.
|
|
47
|
+
* Total size: 256 bytes (64 + 128 + 64)
|
|
48
|
+
*/
|
|
49
|
+
export type TransactionProofStruct = {
|
|
50
|
+
/** G1 point A (64 bytes) */
|
|
51
|
+
proofA: number[];
|
|
52
|
+
/** G2 point B (128 bytes) */
|
|
53
|
+
proofB: number[];
|
|
54
|
+
/** G1 point C (64 bytes) */
|
|
55
|
+
proofC: number[];
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Shape of a Groth16 proof produced by snarkjs.
|
|
59
|
+
*/
|
|
60
|
+
export type RawProof = {
|
|
61
|
+
pi_a: [string, string, string];
|
|
62
|
+
pi_b: [[string, string], [string, string], [string, string]];
|
|
63
|
+
pi_c: [string, string, string];
|
|
64
|
+
protocol: string;
|
|
65
|
+
curve: string;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Proof builder function type - takes circuit inputs and returns a raw snarkjs proof.
|
|
69
|
+
*/
|
|
70
|
+
export type TransactionProofBuilder = (inputs: TransactionCircuitInputs) => Promise<RawProof>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { TransactionProofBuilder, RawProof } from "./proofs/types.js";
|
|
2
|
+
import { type SwapProofBuilder } from "./proofs/swap.js";
|
|
3
|
+
/**
|
|
4
|
+
* Circuit artifact paths required by snarkjs.
|
|
5
|
+
*
|
|
6
|
+
* These files are large (10-100 MB) and should NOT be bundled in npm.
|
|
7
|
+
* Host them on GitHub Releases, IPFS, or a CDN and download at runtime.
|
|
8
|
+
*
|
|
9
|
+
* For Node.js: pass file system paths (e.g. "./circuits/transaction.wasm").
|
|
10
|
+
* For browsers: pass Uint8Array buffers or URLs that fetch returns as ArrayBuffer.
|
|
11
|
+
*/
|
|
12
|
+
export interface CircuitArtifacts {
|
|
13
|
+
/** Path or buffer for the compiled WASM witness generator */
|
|
14
|
+
wasmPath: string | Uint8Array;
|
|
15
|
+
/** Path or buffer for the Groth16 proving key (.zkey) */
|
|
16
|
+
zkeyPath: string | Uint8Array;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Create a proof builder function for the transaction circuit.
|
|
20
|
+
*
|
|
21
|
+
* Uses dynamic `import("snarkjs")` so the dependency is only loaded when
|
|
22
|
+
* proof generation is actually called. Install snarkjs as a peer dependency:
|
|
23
|
+
*
|
|
24
|
+
* ```
|
|
25
|
+
* npm install snarkjs
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @param artifacts Paths or buffers to the circuit WASM and zkey files
|
|
29
|
+
* @returns A `TransactionProofBuilder` callback compatible with the SDK's client functions
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { createTransactionProver } from "@veilo/sdk-core";
|
|
34
|
+
*
|
|
35
|
+
* const prover = createTransactionProver({
|
|
36
|
+
* wasmPath: "./circuits/transaction.wasm",
|
|
37
|
+
* zkeyPath: "./circuits/transaction_final.zkey",
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* // Pass to SDK functions that accept a TransactionProofBuilder
|
|
41
|
+
* const proof = await prover(circuitInputs);
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function createTransactionProver(artifacts: CircuitArtifacts): TransactionProofBuilder;
|
|
45
|
+
/** Create a proof builder for the current `swap.circom` witness shape. */
|
|
46
|
+
export declare function createSwapProver(artifacts: CircuitArtifacts): SwapProofBuilder;
|
|
47
|
+
/**
|
|
48
|
+
* Verify a Groth16 proof against public signals and a verification key.
|
|
49
|
+
*
|
|
50
|
+
* Useful for local verification before submitting to the relayer.
|
|
51
|
+
*
|
|
52
|
+
* @param proof The raw snarkjs proof
|
|
53
|
+
* @param publicSignals Array of public signal strings
|
|
54
|
+
* @param vkeyPath Path to the verification key JSON file, or the parsed JSON object
|
|
55
|
+
* @returns true if the proof is valid
|
|
56
|
+
*/
|
|
57
|
+
export declare function verifyProof(proof: RawProof, publicSignals: string[], vkey: string | object): Promise<boolean>;
|
package/dist/esm/prover.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { formatInputsForSnarkjs
|
|
1
|
+
import { formatInputsForSnarkjs } from "./proofs/formatting.js";
|
|
2
|
+
import { formatSwapInputsForSnarkjs, } from "./proofs/swap.js";
|
|
2
3
|
/** Lazy-load snarkjs — works in both CJS and ESM contexts. */
|
|
3
4
|
async function loadSnarkjs() {
|
|
4
5
|
try {
|
|
@@ -43,6 +44,14 @@ export function createTransactionProver(artifacts) {
|
|
|
43
44
|
return proof;
|
|
44
45
|
};
|
|
45
46
|
}
|
|
47
|
+
/** Create a proof builder for the current `swap.circom` witness shape. */
|
|
48
|
+
export function createSwapProver(artifacts) {
|
|
49
|
+
return async (inputs) => {
|
|
50
|
+
const snarkjs = await loadSnarkjs();
|
|
51
|
+
const { proof } = await snarkjs.groth16.fullProve(formatSwapInputsForSnarkjs(inputs), artifacts.wasmPath, artifacts.zkeyPath);
|
|
52
|
+
return proof;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
46
55
|
/**
|
|
47
56
|
* Verify a Groth16 proof against public signals and a verification key.
|
|
48
57
|
*
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-platform CSPRNG.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately backed by `nacl.randomBytes` rather than Node's `crypto.randomBytes`:
|
|
5
|
+
* tweetnacl resolves to `crypto.getRandomValues` in browsers and to Node's CSPRNG
|
|
6
|
+
* under Node, so this module carries no `node:crypto` import. That matters because
|
|
7
|
+
* every deposit path reaches this code via `createUTXO`/`generateKeypair`, and a
|
|
8
|
+
* bare `import { randomBytes } from "crypto"` throws at bundle time in browser
|
|
9
|
+
* builds that have no Node polyfill.
|
|
10
|
+
*
|
|
11
|
+
* tweetnacl is already a hard dependency, so this adds no install weight.
|
|
12
|
+
*/
|
|
13
|
+
/** `length` cryptographically-secure random bytes. */
|
|
14
|
+
export declare function randomBytes(length: number): Uint8Array;
|
|
15
|
+
/** 32 cryptographically-secure random bytes — the common case for blindings and seeds. */
|
|
16
|
+
export declare function randomBytes32(): Uint8Array;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import nacl from "tweetnacl";
|
|
2
|
+
/**
|
|
3
|
+
* Cross-platform CSPRNG.
|
|
4
|
+
*
|
|
5
|
+
* Deliberately backed by `nacl.randomBytes` rather than Node's `crypto.randomBytes`:
|
|
6
|
+
* tweetnacl resolves to `crypto.getRandomValues` in browsers and to Node's CSPRNG
|
|
7
|
+
* under Node, so this module carries no `node:crypto` import. That matters because
|
|
8
|
+
* every deposit path reaches this code via `createUTXO`/`generateKeypair`, and a
|
|
9
|
+
* bare `import { randomBytes } from "crypto"` throws at bundle time in browser
|
|
10
|
+
* builds that have no Node polyfill.
|
|
11
|
+
*
|
|
12
|
+
* tweetnacl is already a hard dependency, so this adds no install weight.
|
|
13
|
+
*/
|
|
14
|
+
/** `length` cryptographically-secure random bytes. */
|
|
15
|
+
export function randomBytes(length) {
|
|
16
|
+
return nacl.randomBytes(length);
|
|
17
|
+
}
|
|
18
|
+
/** 32 cryptographically-secure random bytes — the common case for blindings and seeds. */
|
|
19
|
+
export function randomBytes32() {
|
|
20
|
+
return nacl.randomBytes(32);
|
|
21
|
+
}
|