@veilo/sdk-core 0.1.17 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1296 -272
- package/SHIELD_INTEGRATION.md +143 -0
- package/config.d.ts +2 -0
- package/config.js +4 -0
- package/dist/cjs/client.d.ts +407 -0
- package/dist/cjs/client.js +951 -0
- package/dist/cjs/compactNote.d.ts +107 -0
- package/dist/cjs/compactNote.js +167 -0
- package/dist/cjs/config.d.ts +82 -0
- package/dist/cjs/config.js +57 -0
- package/dist/cjs/events.d.ts +77 -0
- package/dist/cjs/events.js +167 -0
- package/dist/cjs/idl/privacy_pool.d.ts +5 -0
- package/dist/cjs/idl/privacy_pool.js +15218 -0
- package/dist/cjs/idl/privacy_pool.json +10313 -0
- package/dist/cjs/index.d.ts +16 -0
- package/dist/cjs/index.js +67 -0
- package/dist/cjs/merkle.d.ts +77 -0
- package/dist/cjs/merkle.js +156 -0
- package/dist/cjs/poseidon.d.ts +29 -0
- package/dist/cjs/poseidon.js +100 -0
- package/dist/cjs/program.d.ts +37 -0
- package/dist/cjs/program.js +61 -0
- package/dist/cjs/proof.d.ts +183 -0
- package/dist/cjs/proof.js +292 -0
- package/dist/cjs/prover.d.ts +54 -0
- package/dist/cjs/prover.js +112 -0
- package/dist/cjs/random.d.ts +16 -0
- package/dist/cjs/random.js +28 -0
- package/dist/cjs/relayer.d.ts +318 -0
- package/dist/cjs/relayer.js +257 -0
- package/dist/cjs/retry.d.ts +32 -0
- package/dist/cjs/retry.js +75 -0
- 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 +126 -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 +499 -0
- package/dist/cjs/shield/types.d.ts +202 -0
- package/dist/cjs/shield/types.js +2 -0
- package/dist/cjs/utxo.d.ts +235 -0
- package/dist/cjs/utxo.js +407 -0
- package/dist/esm/client.d.ts +407 -0
- package/dist/esm/client.js +891 -0
- package/dist/esm/compactNote.d.ts +107 -0
- package/dist/esm/compactNote.js +156 -0
- package/dist/esm/config.d.ts +82 -0
- package/dist/esm/config.js +51 -0
- package/dist/esm/events.d.ts +77 -0
- package/dist/esm/events.js +129 -0
- package/dist/esm/idl/privacy_pool.d.ts +5 -0
- package/dist/esm/idl/privacy_pool.js +15216 -0
- package/dist/esm/idl/privacy_pool.json +10313 -0
- package/dist/esm/index.d.ts +16 -0
- package/dist/esm/index.js +29 -0
- package/dist/esm/merkle.d.ts +77 -0
- package/dist/esm/merkle.js +151 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/poseidon.d.ts +29 -0
- package/dist/esm/poseidon.js +87 -0
- package/dist/esm/program.d.ts +37 -0
- package/dist/esm/program.js +53 -0
- package/dist/esm/proof.d.ts +183 -0
- package/dist/esm/proof.js +281 -0
- package/dist/esm/prover.d.ts +54 -0
- package/dist/esm/prover.js +75 -0
- package/dist/esm/random.d.ts +16 -0
- package/dist/esm/random.js +21 -0
- package/dist/esm/relayer.d.ts +318 -0
- package/dist/esm/relayer.js +249 -0
- package/dist/esm/retry.d.ts +32 -0
- package/dist/esm/retry.js +71 -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 +122 -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 +492 -0
- package/dist/esm/shield/types.d.ts +202 -0
- package/dist/esm/shield/types.js +1 -0
- package/dist/esm/utxo.d.ts +235 -0
- package/dist/esm/utxo.js +382 -0
- package/dist/src/client.d.ts +407 -0
- package/dist/src/client.js +951 -0
- package/dist/src/compactNote.d.ts +107 -0
- package/dist/src/compactNote.js +167 -0
- package/dist/src/config.d.ts +82 -0
- package/dist/src/config.js +57 -0
- package/dist/src/events.d.ts +77 -0
- package/dist/src/events.js +167 -0
- package/dist/src/idl/privacy_pool.d.ts +5 -0
- package/dist/src/idl/privacy_pool.js +15218 -0
- package/dist/src/index.d.ts +16 -0
- package/dist/src/index.js +67 -0
- package/dist/src/merkle.d.ts +77 -0
- package/dist/src/merkle.js +156 -0
- package/dist/src/poseidon.d.ts +29 -0
- package/dist/src/poseidon.js +100 -0
- package/dist/src/program.d.ts +37 -0
- package/dist/src/program.js +61 -0
- package/dist/src/proof.d.ts +183 -0
- package/dist/src/proof.js +292 -0
- package/dist/src/prover.d.ts +54 -0
- package/dist/src/prover.js +112 -0
- package/dist/src/random.d.ts +16 -0
- package/dist/src/random.js +28 -0
- package/dist/src/relayer.d.ts +318 -0
- package/dist/src/relayer.js +257 -0
- package/dist/src/retry.d.ts +32 -0
- package/dist/src/retry.js +75 -0
- package/dist/src/shield/alt.d.ts +87 -0
- package/dist/src/shield/alt.js +194 -0
- package/dist/src/shield/computeBudget.d.ts +61 -0
- package/dist/src/shield/computeBudget.js +64 -0
- package/dist/src/shield/errors.d.ts +58 -0
- package/dist/src/shield/errors.js +121 -0
- package/dist/src/shield/finalize.d.ts +45 -0
- package/dist/src/shield/finalize.js +119 -0
- package/dist/src/shield/index.d.ts +35 -0
- package/dist/src/shield/index.js +68 -0
- package/dist/src/shield/ix.d.ts +54 -0
- package/dist/src/shield/ix.js +119 -0
- package/dist/src/shield/owner.d.ts +36 -0
- package/dist/src/shield/owner.js +126 -0
- package/dist/src/shield/ports.d.ts +43 -0
- package/dist/src/shield/ports.js +153 -0
- package/dist/src/shield/preflight.d.ts +30 -0
- package/dist/src/shield/preflight.js +154 -0
- package/dist/src/shield/shield.d.ts +68 -0
- package/dist/src/shield/shield.js +499 -0
- package/dist/src/shield/types.d.ts +202 -0
- package/dist/src/shield/types.js +2 -0
- package/dist/src/utxo.d.ts +235 -0
- package/dist/src/utxo.js +407 -0
- package/dist/tests/compact-note.test.d.ts +1 -0
- package/dist/tests/compact-note.test.js +173 -0
- package/dist/tests/config.test.d.ts +1 -0
- package/dist/tests/config.test.js +102 -0
- package/dist/tests/edge-cases.test.d.ts +1 -0
- package/dist/tests/edge-cases.test.js +220 -0
- package/dist/tests/encryption.test.d.ts +1 -0
- package/dist/tests/encryption.test.js +215 -0
- package/dist/tests/events.test.d.ts +1 -0
- package/dist/tests/events.test.js +78 -0
- package/dist/tests/multi-tree.test.d.ts +1 -0
- package/dist/tests/multi-tree.test.js +405 -0
- package/dist/tests/pda.test.d.ts +1 -0
- package/dist/tests/pda.test.js +229 -0
- package/dist/tests/poseidon-builder-parity.test.d.ts +1 -0
- package/dist/tests/poseidon-builder-parity.test.js +72 -0
- package/dist/tests/poseidon.test.d.ts +1 -0
- package/dist/tests/poseidon.test.js +142 -0
- package/dist/tests/proof.test.d.ts +1 -0
- package/dist/tests/proof.test.js +296 -0
- package/dist/tests/relayer.test.d.ts +1 -0
- package/dist/tests/relayer.test.js +271 -0
- package/dist/tests/sdk.integration.test.d.ts +1 -0
- package/dist/tests/sdk.integration.test.js +330 -0
- package/dist/tests/shield-owner.test.d.ts +1 -0
- package/dist/tests/shield-owner.test.js +89 -0
- package/dist/tests/shield-preflight.test.d.ts +1 -0
- package/dist/tests/shield-preflight.test.js +87 -0
- package/dist/tests/shield-realproof.test.d.ts +1 -0
- package/dist/tests/shield-realproof.test.js +272 -0
- package/dist/tests/shield.test.d.ts +1 -0
- package/dist/tests/shield.test.js +403 -0
- package/dist/tests/utxo.test.d.ts +1 -0
- package/dist/tests/utxo.test.js +140 -0
- package/package.json +105 -11
- package/poseidon.d.ts +2 -0
- package/poseidon.js +4 -0
- package/proof.d.ts +2 -0
- package/proof.js +4 -0
- package/prover.d.ts +2 -0
- package/prover.js +4 -0
- package/shield.d.ts +2 -0
- package/shield.js +4 -0
- package/src/client.ts +0 -352
- package/src/config.ts +0 -13
- package/src/index.ts +0 -6
- package/src/merkle.ts +0 -178
- package/src/note.ts +0 -193
- package/src/poseidon.ts +0 -62
- package/src/proof.ts +0 -170
- package/test/script.js +0 -0
- package/test-tsconfig.json +0 -19
- package/tests/note.test.ts +0 -50
- package/tests/sdk.integration.test.ts +0 -210
- package/tsconfig.json +0 -18
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
|
2
|
+
import { SerializedUTXO, InputUTXO } from "./utxo.js";
|
|
3
|
+
/**
|
|
4
|
+
* External data that gets hashed into ext_data_hash.
|
|
5
|
+
* These are public parameters that affect financial flows.
|
|
6
|
+
*/
|
|
7
|
+
export type ExtData = {
|
|
8
|
+
/** Who receives withdrawal (for withdrawals) */
|
|
9
|
+
recipient: PublicKey;
|
|
10
|
+
/** Who submits tx and gets fee */
|
|
11
|
+
relayer: PublicKey;
|
|
12
|
+
/** Fee to relayer in lamports/token units */
|
|
13
|
+
fee: bigint;
|
|
14
|
+
/** Refund to user in lamports/token units (for gas compensation) */
|
|
15
|
+
refund: bigint;
|
|
16
|
+
/** Claimant key committed into the proof */
|
|
17
|
+
claimant: PublicKey;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Transaction circuit inputs (8 public inputs, many private inputs).
|
|
21
|
+
* This structure is used to prepare inputs for the snarkjs prover.
|
|
22
|
+
*/
|
|
23
|
+
export type TransactionCircuitInputs = {
|
|
24
|
+
/** Merkle root of UTXO tree */
|
|
25
|
+
root: Uint8Array;
|
|
26
|
+
/** Net deposit/withdrawal amount (positive = deposit, negative = withdrawal) */
|
|
27
|
+
publicAmount: bigint;
|
|
28
|
+
/** Hash of external data (recipient, relayer, fee, refund) */
|
|
29
|
+
extDataHash: Uint8Array;
|
|
30
|
+
/** Token mint address */
|
|
31
|
+
mintAddress: PublicKey;
|
|
32
|
+
/** Nullifiers for the two input UTXOs */
|
|
33
|
+
inputNullifiers: [Uint8Array, Uint8Array];
|
|
34
|
+
/** Commitments for the two output UTXOs */
|
|
35
|
+
outputCommitments: [Uint8Array, Uint8Array];
|
|
36
|
+
inAmount: [bigint, bigint];
|
|
37
|
+
inPubkey: [bigint, bigint];
|
|
38
|
+
inBlinding: [bigint, bigint];
|
|
39
|
+
inPathIndex: [number, number];
|
|
40
|
+
inPathElements: [bigint[], bigint[]];
|
|
41
|
+
inPrivateKey: [bigint, bigint];
|
|
42
|
+
outAmount: [bigint, bigint];
|
|
43
|
+
outPubkey: [bigint, bigint];
|
|
44
|
+
outBlinding: [bigint, bigint];
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Proof structure matching the on-chain TransactionProof struct.
|
|
48
|
+
* Total size: 256 bytes (64 + 128 + 64)
|
|
49
|
+
*/
|
|
50
|
+
export type TransactionProofStruct = {
|
|
51
|
+
/** G1 point A (64 bytes) */
|
|
52
|
+
proofA: number[];
|
|
53
|
+
/** G2 point B (128 bytes) */
|
|
54
|
+
proofB: number[];
|
|
55
|
+
/** G1 point C (64 bytes) */
|
|
56
|
+
proofC: number[];
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Shape of a Groth16 proof produced by snarkjs.
|
|
60
|
+
*/
|
|
61
|
+
export type RawProof = {
|
|
62
|
+
pi_a: [string, string, string];
|
|
63
|
+
pi_b: [[string, string], [string, string], [string, string]];
|
|
64
|
+
pi_c: [string, string, string];
|
|
65
|
+
protocol: string;
|
|
66
|
+
curve: string;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Proof builder function type - takes circuit inputs and returns a raw snarkjs proof.
|
|
70
|
+
*/
|
|
71
|
+
export type TransactionProofBuilder = (inputs: TransactionCircuitInputs) => Promise<RawProof>;
|
|
72
|
+
/**
|
|
73
|
+
* Compute the external data hash.
|
|
74
|
+
* Matches on-chain computation:
|
|
75
|
+
* hash1 = Poseidon(recipient, relayer)
|
|
76
|
+
* hash2 = Poseidon(fee, refund)
|
|
77
|
+
* extDataHash = Poseidon(hash1, hash2, claimant)
|
|
78
|
+
*/
|
|
79
|
+
export declare function computeExtDataHash(extData: ExtData): Uint8Array;
|
|
80
|
+
/**
|
|
81
|
+
* Convert i64 to field element (handles negative via modular arithmetic).
|
|
82
|
+
* For negative values, we add the field modulus to get the equivalent positive representation.
|
|
83
|
+
*/
|
|
84
|
+
export declare function i64ToField(value: bigint): bigint;
|
|
85
|
+
/**
|
|
86
|
+
* Prepare transaction circuit inputs from high-level parameters.
|
|
87
|
+
* This converts the SDK types into the format expected by the circuit.
|
|
88
|
+
*/
|
|
89
|
+
export declare function prepareTransactionInputs(params: {
|
|
90
|
+
inputUTXOs: [InputUTXO, InputUTXO];
|
|
91
|
+
outputUTXOs: [SerializedUTXO, SerializedUTXO];
|
|
92
|
+
root: Uint8Array;
|
|
93
|
+
publicAmount: bigint;
|
|
94
|
+
extData: ExtData;
|
|
95
|
+
mintAddress: PublicKey;
|
|
96
|
+
}): TransactionCircuitInputs;
|
|
97
|
+
/**
|
|
98
|
+
* Turn a snarkjs Groth16 proof into 256 bytes:
|
|
99
|
+
* A(G1) | B(G2) | C(G1)
|
|
100
|
+
* where:
|
|
101
|
+
* A = (ax, ay) -> 64 bytes
|
|
102
|
+
* B = (bx1, bx0, by1, by0) -> 128 bytes (swapped pairs for alt_bn128)
|
|
103
|
+
* C = (cx, cy) -> 64 bytes
|
|
104
|
+
*
|
|
105
|
+
* Each field element is encoded as 32-byte BE to match the on-chain verifier.
|
|
106
|
+
* We ignore the extra projective/z components snarkjs keeps.
|
|
107
|
+
*/
|
|
108
|
+
export declare function packProofToBytes(proof: RawProof): Uint8Array;
|
|
109
|
+
/**
|
|
110
|
+
* Encode a snarkjs proof into the TransactionProofStruct format expected by the on-chain program.
|
|
111
|
+
*/
|
|
112
|
+
export declare function encodeSnarkjsProofToTransactionProof(proof: RawProof): TransactionProofStruct;
|
|
113
|
+
/**
|
|
114
|
+
* Convert TransactionCircuitInputs to the format expected by snarkjs.groth16.fullProve().
|
|
115
|
+
* All values are converted to strings (snarkjs expects string inputs for field elements).
|
|
116
|
+
*/
|
|
117
|
+
export declare function formatInputsForSnarkjs(inputs: TransactionCircuitInputs): Record<string, any>;
|
|
118
|
+
/**
|
|
119
|
+
* Swap circuit inputs. Matches the signal shape declared by swap.circom's
|
|
120
|
+
* main component (see zk-circuits/swap.circom lines 208-238). Unlike the
|
|
121
|
+
* transaction circuit, the swap circuit distinguishes a "change" output
|
|
122
|
+
* (same mint as source) from a "dest" output (dest mint) and takes a
|
|
123
|
+
* swap-specific params hash and swapAmount alongside the usual inputs.
|
|
124
|
+
*/
|
|
125
|
+
export type SwapCircuitInputs = {
|
|
126
|
+
sourceRoot: Uint8Array;
|
|
127
|
+
swapParamsHash: Uint8Array;
|
|
128
|
+
extDataHash: Uint8Array;
|
|
129
|
+
sourceMint: PublicKey;
|
|
130
|
+
destMint: PublicKey;
|
|
131
|
+
inputNullifiers: [Uint8Array, Uint8Array];
|
|
132
|
+
changeCommitment: Uint8Array;
|
|
133
|
+
destCommitment: Uint8Array;
|
|
134
|
+
swapAmount: bigint;
|
|
135
|
+
inAmount: [bigint, bigint];
|
|
136
|
+
inPubkey: [bigint, bigint];
|
|
137
|
+
inBlinding: [bigint, bigint];
|
|
138
|
+
inPathIndex: [number, number];
|
|
139
|
+
inPathElements: [bigint[], bigint[]];
|
|
140
|
+
inPrivateKey: [bigint, bigint];
|
|
141
|
+
changeAmount: bigint;
|
|
142
|
+
changePubkey: bigint;
|
|
143
|
+
changeBlinding: bigint;
|
|
144
|
+
destAmount: bigint;
|
|
145
|
+
destPubkey: bigint;
|
|
146
|
+
destBlinding: bigint;
|
|
147
|
+
minAmountOut: bigint;
|
|
148
|
+
deadline: bigint;
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Convert SwapCircuitInputs into the shape expected by
|
|
152
|
+
* snarkjs.groth16.fullProve() for swap.circom. Every value is emitted as a
|
|
153
|
+
* decimal string (or string array) because snarkjs only accepts field
|
|
154
|
+
* elements as strings.
|
|
155
|
+
*
|
|
156
|
+
* Mirrors `generateSwapProof` in
|
|
157
|
+
* relayer-server/src/controllers/swap.helpers.ts so SDK-side swap proofs
|
|
158
|
+
* verify against the same circuit the relayer uses.
|
|
159
|
+
*/
|
|
160
|
+
export declare function formatSwapInputsForSnarkjs(inputs: SwapCircuitInputs): Record<string, any>;
|
|
161
|
+
/**
|
|
162
|
+
* Compute the swap params hash that the swap circuit and on-chain program verify.
|
|
163
|
+
* Mirrors relayer-server/src/controllers/swap.helpers.ts `computeSwapParamsHash`.
|
|
164
|
+
*
|
|
165
|
+
* Circuit formula (swap.circom lines 293-307):
|
|
166
|
+
* mintPairHash = Poseidon(sourceMint, destMint)
|
|
167
|
+
* swapTermsHash = Poseidon(minAmountOut, deadline, destAmount)
|
|
168
|
+
* swapParamsHash = Poseidon(mintPairHash, swapTermsHash)
|
|
169
|
+
*
|
|
170
|
+
* @param sourceMint Source pool mint
|
|
171
|
+
* @param destMint Destination pool mint
|
|
172
|
+
* @param minAmountOut Minimum accepted output amount (slippage protected)
|
|
173
|
+
* @param deadline Unix timestamp deadline (i64, 0 = no deadline)
|
|
174
|
+
* @param destAmount Actual amount committed to destination UTXO (≥ minAmountOut -
|
|
175
|
+
* relayerFee). If omitted, defaults to minAmountOut.
|
|
176
|
+
*/
|
|
177
|
+
export declare function computeSwapParamsHash(sourceMint: PublicKey, destMint: PublicKey, minAmountOut: bigint, deadline: bigint, destAmount?: bigint): Uint8Array;
|
|
178
|
+
/**
|
|
179
|
+
* Compute the swap data hash committed on-chain for Jupiter instruction integrity.
|
|
180
|
+
* SHA-256 of the raw Jupiter swap instruction data bytes.
|
|
181
|
+
* Mirrors relayer-server/src/controllers/swap.helpers.ts `computeSwapDataHash`.
|
|
182
|
+
*/
|
|
183
|
+
export declare function computeSwapDataHash(swapData: Uint8Array | Buffer): Uint8Array;
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.computeExtDataHash = computeExtDataHash;
|
|
4
|
+
exports.i64ToField = i64ToField;
|
|
5
|
+
exports.prepareTransactionInputs = prepareTransactionInputs;
|
|
6
|
+
exports.packProofToBytes = packProofToBytes;
|
|
7
|
+
exports.encodeSnarkjsProofToTransactionProof = encodeSnarkjsProofToTransactionProof;
|
|
8
|
+
exports.formatInputsForSnarkjs = formatInputsForSnarkjs;
|
|
9
|
+
exports.formatSwapInputsForSnarkjs = formatSwapInputsForSnarkjs;
|
|
10
|
+
exports.computeSwapParamsHash = computeSwapParamsHash;
|
|
11
|
+
exports.computeSwapDataHash = computeSwapDataHash;
|
|
12
|
+
const sha256_1 = require("@noble/hashes/sha256");
|
|
13
|
+
const poseidon_js_1 = require("./poseidon.js");
|
|
14
|
+
const utxo_js_1 = require("./utxo.js");
|
|
15
|
+
// -----------------------------------------------------------------------------
|
|
16
|
+
// ExtData Hash Computation
|
|
17
|
+
// -----------------------------------------------------------------------------
|
|
18
|
+
/**
|
|
19
|
+
* Compute the external data hash.
|
|
20
|
+
* Matches on-chain computation:
|
|
21
|
+
* hash1 = Poseidon(recipient, relayer)
|
|
22
|
+
* hash2 = Poseidon(fee, refund)
|
|
23
|
+
* extDataHash = Poseidon(hash1, hash2, claimant)
|
|
24
|
+
*/
|
|
25
|
+
function computeExtDataHash(extData) {
|
|
26
|
+
const recipientField = (0, poseidon_js_1.pubkeyToField)(extData.recipient);
|
|
27
|
+
const relayerField = (0, poseidon_js_1.pubkeyToField)(extData.relayer);
|
|
28
|
+
const feeField = extData.fee % poseidon_js_1.BN254_FR_MODULUS;
|
|
29
|
+
const refundField = extData.refund % poseidon_js_1.BN254_FR_MODULUS;
|
|
30
|
+
const claimantField = (0, poseidon_js_1.pubkeyToField)(extData.claimant);
|
|
31
|
+
const hash1 = (0, poseidon_js_1.poseidon2)(recipientField, relayerField);
|
|
32
|
+
const hash2 = (0, poseidon_js_1.poseidon2)(feeField, refundField);
|
|
33
|
+
const finalHash = (0, poseidon_js_1.poseidon3)(hash1, hash2, claimantField);
|
|
34
|
+
return (0, poseidon_js_1.bigIntToBytesBE)(finalHash);
|
|
35
|
+
}
|
|
36
|
+
// -----------------------------------------------------------------------------
|
|
37
|
+
// Circuit Input Preparation
|
|
38
|
+
// -----------------------------------------------------------------------------
|
|
39
|
+
/**
|
|
40
|
+
* Convert i64 to field element (handles negative via modular arithmetic).
|
|
41
|
+
* For negative values, we add the field modulus to get the equivalent positive representation.
|
|
42
|
+
*/
|
|
43
|
+
function i64ToField(value) {
|
|
44
|
+
if (value >= 0n) {
|
|
45
|
+
return value % poseidon_js_1.BN254_FR_MODULUS;
|
|
46
|
+
}
|
|
47
|
+
return (poseidon_js_1.BN254_FR_MODULUS + value) % poseidon_js_1.BN254_FR_MODULUS;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Prepare transaction circuit inputs from high-level parameters.
|
|
51
|
+
* This converts the SDK types into the format expected by the circuit.
|
|
52
|
+
*/
|
|
53
|
+
function prepareTransactionInputs(params) {
|
|
54
|
+
const { inputUTXOs, outputUTXOs, root, publicAmount, extData, mintAddress } = params;
|
|
55
|
+
const extDataHash = computeExtDataHash(extData);
|
|
56
|
+
const mintField = (0, poseidon_js_1.pubkeyToField)(mintAddress);
|
|
57
|
+
// Prepare input UTXO data
|
|
58
|
+
const inAmount = [
|
|
59
|
+
inputUTXOs[0].amount,
|
|
60
|
+
inputUTXOs[1].amount,
|
|
61
|
+
];
|
|
62
|
+
const inPubkey = [
|
|
63
|
+
inputUTXOs[0].pubkey,
|
|
64
|
+
inputUTXOs[1].pubkey,
|
|
65
|
+
];
|
|
66
|
+
const inBlinding = [
|
|
67
|
+
inputUTXOs[0].blinding,
|
|
68
|
+
inputUTXOs[1].blinding,
|
|
69
|
+
];
|
|
70
|
+
const inPathIndex = [
|
|
71
|
+
inputUTXOs[0].pathIndex,
|
|
72
|
+
inputUTXOs[1].pathIndex,
|
|
73
|
+
];
|
|
74
|
+
const inPathElements = [
|
|
75
|
+
inputUTXOs[0].pathElements.map((e) => (0, poseidon_js_1.bytesToBigIntBE)(e) % poseidon_js_1.BN254_FR_MODULUS),
|
|
76
|
+
inputUTXOs[1].pathElements.map((e) => (0, poseidon_js_1.bytesToBigIntBE)(e) % poseidon_js_1.BN254_FR_MODULUS),
|
|
77
|
+
];
|
|
78
|
+
const inPrivateKey = [
|
|
79
|
+
inputUTXOs[0].privateKey,
|
|
80
|
+
inputUTXOs[1].privateKey,
|
|
81
|
+
];
|
|
82
|
+
// Prepare output UTXO data
|
|
83
|
+
const outAmount = [
|
|
84
|
+
outputUTXOs[0].amount,
|
|
85
|
+
outputUTXOs[1].amount,
|
|
86
|
+
];
|
|
87
|
+
const outPubkey = [
|
|
88
|
+
outputUTXOs[0].pubkey,
|
|
89
|
+
outputUTXOs[1].pubkey,
|
|
90
|
+
];
|
|
91
|
+
const outBlinding = [
|
|
92
|
+
outputUTXOs[0].blinding,
|
|
93
|
+
outputUTXOs[1].blinding,
|
|
94
|
+
];
|
|
95
|
+
// Derive nullifiers for input UTXOs
|
|
96
|
+
const inputNullifiers = [
|
|
97
|
+
(0, utxo_js_1.deriveNullifier)(inputUTXOs[0], inputUTXOs[0].pathIndex, inputUTXOs[0].privateKey),
|
|
98
|
+
(0, utxo_js_1.deriveNullifier)(inputUTXOs[1], inputUTXOs[1].pathIndex, inputUTXOs[1].privateKey),
|
|
99
|
+
];
|
|
100
|
+
return {
|
|
101
|
+
root,
|
|
102
|
+
publicAmount,
|
|
103
|
+
extDataHash,
|
|
104
|
+
mintAddress,
|
|
105
|
+
inputNullifiers,
|
|
106
|
+
outputCommitments: [outputUTXOs[0].commitment, outputUTXOs[1].commitment],
|
|
107
|
+
inAmount,
|
|
108
|
+
inPubkey,
|
|
109
|
+
inBlinding,
|
|
110
|
+
inPathIndex,
|
|
111
|
+
inPathElements,
|
|
112
|
+
inPrivateKey,
|
|
113
|
+
outAmount,
|
|
114
|
+
outPubkey,
|
|
115
|
+
outBlinding,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
// -----------------------------------------------------------------------------
|
|
119
|
+
// Proof Packing
|
|
120
|
+
// -----------------------------------------------------------------------------
|
|
121
|
+
/**
|
|
122
|
+
* Convert bigint to 32-byte big-endian representation.
|
|
123
|
+
* The alt_bn128 Solana precompile (and ark-works via change_endianness) expect BE.
|
|
124
|
+
*/
|
|
125
|
+
function bigintTo32BytesBE(x) {
|
|
126
|
+
const out = new Uint8Array(32);
|
|
127
|
+
let v = x;
|
|
128
|
+
for (let i = 31; i >= 0; i--) {
|
|
129
|
+
out[i] = Number(v & 0xffn);
|
|
130
|
+
v >>= 8n;
|
|
131
|
+
}
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Turn a snarkjs Groth16 proof into 256 bytes:
|
|
136
|
+
* A(G1) | B(G2) | C(G1)
|
|
137
|
+
* where:
|
|
138
|
+
* A = (ax, ay) -> 64 bytes
|
|
139
|
+
* B = (bx1, bx0, by1, by0) -> 128 bytes (swapped pairs for alt_bn128)
|
|
140
|
+
* C = (cx, cy) -> 64 bytes
|
|
141
|
+
*
|
|
142
|
+
* Each field element is encoded as 32-byte BE to match the on-chain verifier.
|
|
143
|
+
* We ignore the extra projective/z components snarkjs keeps.
|
|
144
|
+
*/
|
|
145
|
+
function packProofToBytes(proof) {
|
|
146
|
+
const chunks = [];
|
|
147
|
+
const ax = BigInt(proof.pi_a[0]);
|
|
148
|
+
const ay = BigInt(proof.pi_a[1]);
|
|
149
|
+
chunks.push(bigintTo32BytesBE(ax), bigintTo32BytesBE(ay));
|
|
150
|
+
const bx = proof.pi_b[0];
|
|
151
|
+
const by = proof.pi_b[1];
|
|
152
|
+
const bx0 = BigInt(bx[0]);
|
|
153
|
+
const bx1 = BigInt(bx[1]);
|
|
154
|
+
const by0 = BigInt(by[0]);
|
|
155
|
+
const by1 = BigInt(by[1]);
|
|
156
|
+
// alt_bn128 expects the G2 pairs swapped: bx1 before bx0, by1 before by0
|
|
157
|
+
chunks.push(bigintTo32BytesBE(bx1), bigintTo32BytesBE(bx0), bigintTo32BytesBE(by1), bigintTo32BytesBE(by0));
|
|
158
|
+
const cx = BigInt(proof.pi_c[0]);
|
|
159
|
+
const cy = BigInt(proof.pi_c[1]);
|
|
160
|
+
chunks.push(bigintTo32BytesBE(cx), bigintTo32BytesBE(cy));
|
|
161
|
+
const out = new Uint8Array(32 * (2 + 4 + 2)); // 256
|
|
162
|
+
let offset = 0;
|
|
163
|
+
for (const c of chunks) {
|
|
164
|
+
out.set(c, offset);
|
|
165
|
+
offset += c.length;
|
|
166
|
+
}
|
|
167
|
+
return out;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Encode a snarkjs proof into the TransactionProofStruct format expected by the on-chain program.
|
|
171
|
+
*/
|
|
172
|
+
function encodeSnarkjsProofToTransactionProof(proof) {
|
|
173
|
+
const full = packProofToBytes(proof);
|
|
174
|
+
if (full.length !== 256) {
|
|
175
|
+
throw new Error(`Expected 256 proof bytes, got ${full.length}`);
|
|
176
|
+
}
|
|
177
|
+
const proofA = full.slice(0, 64);
|
|
178
|
+
const proofB = full.slice(64, 192);
|
|
179
|
+
const proofC = full.slice(192, 256);
|
|
180
|
+
return {
|
|
181
|
+
proofA: Array.from(proofA),
|
|
182
|
+
proofB: Array.from(proofB),
|
|
183
|
+
proofC: Array.from(proofC),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
// -----------------------------------------------------------------------------
|
|
187
|
+
// Snarkjs Input Formatting
|
|
188
|
+
// -----------------------------------------------------------------------------
|
|
189
|
+
/**
|
|
190
|
+
* Convert TransactionCircuitInputs to the format expected by snarkjs.groth16.fullProve().
|
|
191
|
+
* All values are converted to strings (snarkjs expects string inputs for field elements).
|
|
192
|
+
*/
|
|
193
|
+
function formatInputsForSnarkjs(inputs) {
|
|
194
|
+
return {
|
|
195
|
+
// Public inputs
|
|
196
|
+
root: (0, poseidon_js_1.bytesToBigIntBE)(inputs.root).toString(),
|
|
197
|
+
publicAmount: i64ToField(inputs.publicAmount).toString(),
|
|
198
|
+
extDataHash: (0, poseidon_js_1.bytesToBigIntBE)(inputs.extDataHash).toString(),
|
|
199
|
+
mintAddress: (0, poseidon_js_1.pubkeyToField)(inputs.mintAddress).toString(),
|
|
200
|
+
inputNullifier: inputs.inputNullifiers.map((n) => (0, poseidon_js_1.bytesToBigIntBE)(n).toString()),
|
|
201
|
+
outputCommitment: inputs.outputCommitments.map((c) => (0, poseidon_js_1.bytesToBigIntBE)(c).toString()),
|
|
202
|
+
// Private inputs - Input UTXOs
|
|
203
|
+
inAmount: inputs.inAmount.map((a) => a.toString()),
|
|
204
|
+
inPubkey: inputs.inPubkey.map((p) => p.toString()),
|
|
205
|
+
inBlinding: inputs.inBlinding.map((b) => b.toString()),
|
|
206
|
+
inPathIndex: inputs.inPathIndex.map((i) => i.toString()),
|
|
207
|
+
inPathElements: inputs.inPathElements.map((path) => path.map((e) => e.toString())),
|
|
208
|
+
inPrivateKey: inputs.inPrivateKey.map((k) => k.toString()),
|
|
209
|
+
// Private inputs - Output UTXOs
|
|
210
|
+
outAmount: inputs.outAmount.map((a) => a.toString()),
|
|
211
|
+
outPubkey: inputs.outPubkey.map((p) => p.toString()),
|
|
212
|
+
outBlinding: inputs.outBlinding.map((b) => b.toString()),
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Convert SwapCircuitInputs into the shape expected by
|
|
217
|
+
* snarkjs.groth16.fullProve() for swap.circom. Every value is emitted as a
|
|
218
|
+
* decimal string (or string array) because snarkjs only accepts field
|
|
219
|
+
* elements as strings.
|
|
220
|
+
*
|
|
221
|
+
* Mirrors `generateSwapProof` in
|
|
222
|
+
* relayer-server/src/controllers/swap.helpers.ts so SDK-side swap proofs
|
|
223
|
+
* verify against the same circuit the relayer uses.
|
|
224
|
+
*/
|
|
225
|
+
function formatSwapInputsForSnarkjs(inputs) {
|
|
226
|
+
return {
|
|
227
|
+
// Public inputs
|
|
228
|
+
sourceRoot: (0, poseidon_js_1.bytesToBigIntBE)(inputs.sourceRoot).toString(),
|
|
229
|
+
swapParamsHash: (0, poseidon_js_1.bytesToBigIntBE)(inputs.swapParamsHash).toString(),
|
|
230
|
+
extDataHash: (0, poseidon_js_1.bytesToBigIntBE)(inputs.extDataHash).toString(),
|
|
231
|
+
sourceMint: (0, poseidon_js_1.pubkeyToField)(inputs.sourceMint).toString(),
|
|
232
|
+
destMint: (0, poseidon_js_1.pubkeyToField)(inputs.destMint).toString(),
|
|
233
|
+
inputNullifier: inputs.inputNullifiers.map((n) => (0, poseidon_js_1.bytesToBigIntBE)(n).toString()),
|
|
234
|
+
changeCommitment: (0, poseidon_js_1.bytesToBigIntBE)(inputs.changeCommitment).toString(),
|
|
235
|
+
destCommitment: (0, poseidon_js_1.bytesToBigIntBE)(inputs.destCommitment).toString(),
|
|
236
|
+
swapAmount: inputs.swapAmount.toString(),
|
|
237
|
+
// Private inputs - Input UTXOs
|
|
238
|
+
inAmount: inputs.inAmount.map((a) => a.toString()),
|
|
239
|
+
inPubkey: inputs.inPubkey.map((p) => p.toString()),
|
|
240
|
+
inBlinding: inputs.inBlinding.map((b) => b.toString()),
|
|
241
|
+
inPathIndex: inputs.inPathIndex.map((i) => i.toString()),
|
|
242
|
+
inPathElements: inputs.inPathElements.map((path) => path.map((e) => e.toString())),
|
|
243
|
+
inPrivateKey: inputs.inPrivateKey.map((k) => k.toString()),
|
|
244
|
+
// Private inputs - Change UTXO
|
|
245
|
+
changeAmount: inputs.changeAmount.toString(),
|
|
246
|
+
changePubkey: inputs.changePubkey.toString(),
|
|
247
|
+
changeBlinding: inputs.changeBlinding.toString(),
|
|
248
|
+
// Private inputs - Destination UTXO
|
|
249
|
+
destAmount: inputs.destAmount.toString(),
|
|
250
|
+
destPubkey: inputs.destPubkey.toString(),
|
|
251
|
+
destBlinding: inputs.destBlinding.toString(),
|
|
252
|
+
// Swap params
|
|
253
|
+
minAmountOut: inputs.minAmountOut.toString(),
|
|
254
|
+
deadline: inputs.deadline.toString(),
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
// -----------------------------------------------------------------------------
|
|
258
|
+
// Swap Parameter Hashing
|
|
259
|
+
// -----------------------------------------------------------------------------
|
|
260
|
+
/**
|
|
261
|
+
* Compute the swap params hash that the swap circuit and on-chain program verify.
|
|
262
|
+
* Mirrors relayer-server/src/controllers/swap.helpers.ts `computeSwapParamsHash`.
|
|
263
|
+
*
|
|
264
|
+
* Circuit formula (swap.circom lines 293-307):
|
|
265
|
+
* mintPairHash = Poseidon(sourceMint, destMint)
|
|
266
|
+
* swapTermsHash = Poseidon(minAmountOut, deadline, destAmount)
|
|
267
|
+
* swapParamsHash = Poseidon(mintPairHash, swapTermsHash)
|
|
268
|
+
*
|
|
269
|
+
* @param sourceMint Source pool mint
|
|
270
|
+
* @param destMint Destination pool mint
|
|
271
|
+
* @param minAmountOut Minimum accepted output amount (slippage protected)
|
|
272
|
+
* @param deadline Unix timestamp deadline (i64, 0 = no deadline)
|
|
273
|
+
* @param destAmount Actual amount committed to destination UTXO (≥ minAmountOut -
|
|
274
|
+
* relayerFee). If omitted, defaults to minAmountOut.
|
|
275
|
+
*/
|
|
276
|
+
function computeSwapParamsHash(sourceMint, destMint, minAmountOut, deadline, destAmount) {
|
|
277
|
+
const srcField = (0, poseidon_js_1.pubkeyToField)(sourceMint);
|
|
278
|
+
const dstField = (0, poseidon_js_1.pubkeyToField)(destMint);
|
|
279
|
+
const mintPairHash = (0, poseidon_js_1.poseidon2)(srcField, dstField);
|
|
280
|
+
const dest = destAmount ?? minAmountOut;
|
|
281
|
+
const swapTermsHash = (0, poseidon_js_1.poseidon3)(minAmountOut, deadline, dest);
|
|
282
|
+
const paramsHash = (0, poseidon_js_1.poseidon2)(mintPairHash, swapTermsHash);
|
|
283
|
+
return (0, poseidon_js_1.bigIntToBytesBE)(paramsHash);
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Compute the swap data hash committed on-chain for Jupiter instruction integrity.
|
|
287
|
+
* SHA-256 of the raw Jupiter swap instruction data bytes.
|
|
288
|
+
* Mirrors relayer-server/src/controllers/swap.helpers.ts `computeSwapDataHash`.
|
|
289
|
+
*/
|
|
290
|
+
function computeSwapDataHash(swapData) {
|
|
291
|
+
return (0, sha256_1.sha256)(swapData instanceof Buffer ? new Uint8Array(swapData) : swapData);
|
|
292
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { TransactionProofBuilder, RawProof } from "./proof.js";
|
|
2
|
+
/**
|
|
3
|
+
* Circuit artifact paths required by snarkjs.
|
|
4
|
+
*
|
|
5
|
+
* These files are large (10-100 MB) and should NOT be bundled in npm.
|
|
6
|
+
* Host them on GitHub Releases, IPFS, or a CDN and download at runtime.
|
|
7
|
+
*
|
|
8
|
+
* For Node.js: pass file system paths (e.g. "./circuits/transaction.wasm").
|
|
9
|
+
* For browsers: pass Uint8Array buffers or URLs that fetch returns as ArrayBuffer.
|
|
10
|
+
*/
|
|
11
|
+
export interface CircuitArtifacts {
|
|
12
|
+
/** Path or buffer for the compiled WASM witness generator */
|
|
13
|
+
wasmPath: string | Uint8Array;
|
|
14
|
+
/** Path or buffer for the Groth16 proving key (.zkey) */
|
|
15
|
+
zkeyPath: string | Uint8Array;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create a proof builder function for the transaction circuit.
|
|
19
|
+
*
|
|
20
|
+
* Uses dynamic `import("snarkjs")` so the dependency is only loaded when
|
|
21
|
+
* proof generation is actually called. Install snarkjs as a peer dependency:
|
|
22
|
+
*
|
|
23
|
+
* ```
|
|
24
|
+
* npm install snarkjs
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @param artifacts Paths or buffers to the circuit WASM and zkey files
|
|
28
|
+
* @returns A `TransactionProofBuilder` callback compatible with the SDK's client functions
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { createTransactionProver } from "@veilo/sdk-core";
|
|
33
|
+
*
|
|
34
|
+
* const prover = createTransactionProver({
|
|
35
|
+
* wasmPath: "./circuits/transaction.wasm",
|
|
36
|
+
* zkeyPath: "./circuits/transaction_final.zkey",
|
|
37
|
+
* });
|
|
38
|
+
*
|
|
39
|
+
* // Pass to SDK functions that accept a TransactionProofBuilder
|
|
40
|
+
* const proof = await prover(circuitInputs);
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare function createTransactionProver(artifacts: CircuitArtifacts): TransactionProofBuilder;
|
|
44
|
+
/**
|
|
45
|
+
* Verify a Groth16 proof against public signals and a verification key.
|
|
46
|
+
*
|
|
47
|
+
* Useful for local verification before submitting to the relayer.
|
|
48
|
+
*
|
|
49
|
+
* @param proof The raw snarkjs proof
|
|
50
|
+
* @param publicSignals Array of public signal strings
|
|
51
|
+
* @param vkeyPath Path to the verification key JSON file, or the parsed JSON object
|
|
52
|
+
* @returns true if the proof is valid
|
|
53
|
+
*/
|
|
54
|
+
export declare function verifyProof(proof: RawProof, publicSignals: string[], vkey: string | object): Promise<boolean>;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.createTransactionProver = createTransactionProver;
|
|
37
|
+
exports.verifyProof = verifyProof;
|
|
38
|
+
const proof_js_1 = require("./proof.js");
|
|
39
|
+
/** Lazy-load snarkjs — works in both CJS and ESM contexts. */
|
|
40
|
+
async function loadSnarkjs() {
|
|
41
|
+
try {
|
|
42
|
+
// Dynamic import works in both CJS (Node 12+) and ESM
|
|
43
|
+
return await Function('return import("snarkjs")')();
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
throw new Error("snarkjs is required for proof generation. Install it: npm install snarkjs");
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Create a proof builder function for the transaction circuit.
|
|
51
|
+
*
|
|
52
|
+
* Uses dynamic `import("snarkjs")` so the dependency is only loaded when
|
|
53
|
+
* proof generation is actually called. Install snarkjs as a peer dependency:
|
|
54
|
+
*
|
|
55
|
+
* ```
|
|
56
|
+
* npm install snarkjs
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* @param artifacts Paths or buffers to the circuit WASM and zkey files
|
|
60
|
+
* @returns A `TransactionProofBuilder` callback compatible with the SDK's client functions
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* import { createTransactionProver } from "@veilo/sdk-core";
|
|
65
|
+
*
|
|
66
|
+
* const prover = createTransactionProver({
|
|
67
|
+
* wasmPath: "./circuits/transaction.wasm",
|
|
68
|
+
* zkeyPath: "./circuits/transaction_final.zkey",
|
|
69
|
+
* });
|
|
70
|
+
*
|
|
71
|
+
* // Pass to SDK functions that accept a TransactionProofBuilder
|
|
72
|
+
* const proof = await prover(circuitInputs);
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
function createTransactionProver(artifacts) {
|
|
76
|
+
return async (inputs) => {
|
|
77
|
+
const snarkjs = await loadSnarkjs();
|
|
78
|
+
const formattedInputs = (0, proof_js_1.formatInputsForSnarkjs)(inputs);
|
|
79
|
+
const { proof } = await snarkjs.groth16.fullProve(formattedInputs, artifacts.wasmPath, artifacts.zkeyPath);
|
|
80
|
+
return proof;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Verify a Groth16 proof against public signals and a verification key.
|
|
85
|
+
*
|
|
86
|
+
* Useful for local verification before submitting to the relayer.
|
|
87
|
+
*
|
|
88
|
+
* @param proof The raw snarkjs proof
|
|
89
|
+
* @param publicSignals Array of public signal strings
|
|
90
|
+
* @param vkeyPath Path to the verification key JSON file, or the parsed JSON object
|
|
91
|
+
* @returns true if the proof is valid
|
|
92
|
+
*/
|
|
93
|
+
async function verifyProof(proof, publicSignals, vkey) {
|
|
94
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
95
|
+
let snarkjs;
|
|
96
|
+
try {
|
|
97
|
+
snarkjs = require("snarkjs");
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
throw new Error('snarkjs is required for proof verification. Install it: npm install snarkjs');
|
|
101
|
+
}
|
|
102
|
+
let vkeyObj;
|
|
103
|
+
if (typeof vkey === "string") {
|
|
104
|
+
// Node.js file path — read and parse
|
|
105
|
+
const fs = await Promise.resolve().then(() => __importStar(require("fs")));
|
|
106
|
+
vkeyObj = JSON.parse(fs.readFileSync(vkey, "utf-8"));
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
vkeyObj = vkey;
|
|
110
|
+
}
|
|
111
|
+
return snarkjs.groth16.verify(vkeyObj, publicSignals, proof);
|
|
112
|
+
}
|
|
@@ -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,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.randomBytes = randomBytes;
|
|
7
|
+
exports.randomBytes32 = randomBytes32;
|
|
8
|
+
const tweetnacl_1 = __importDefault(require("tweetnacl"));
|
|
9
|
+
/**
|
|
10
|
+
* Cross-platform CSPRNG.
|
|
11
|
+
*
|
|
12
|
+
* Deliberately backed by `nacl.randomBytes` rather than Node's `crypto.randomBytes`:
|
|
13
|
+
* tweetnacl resolves to `crypto.getRandomValues` in browsers and to Node's CSPRNG
|
|
14
|
+
* under Node, so this module carries no `node:crypto` import. That matters because
|
|
15
|
+
* every deposit path reaches this code via `createUTXO`/`generateKeypair`, and a
|
|
16
|
+
* bare `import { randomBytes } from "crypto"` throws at bundle time in browser
|
|
17
|
+
* builds that have no Node polyfill.
|
|
18
|
+
*
|
|
19
|
+
* tweetnacl is already a hard dependency, so this adds no install weight.
|
|
20
|
+
*/
|
|
21
|
+
/** `length` cryptographically-secure random bytes. */
|
|
22
|
+
function randomBytes(length) {
|
|
23
|
+
return tweetnacl_1.default.randomBytes(length);
|
|
24
|
+
}
|
|
25
|
+
/** 32 cryptographically-secure random bytes — the common case for blindings and seeds. */
|
|
26
|
+
function randomBytes32() {
|
|
27
|
+
return tweetnacl_1.default.randomBytes(32);
|
|
28
|
+
}
|