privacycash 1.1.17 → 1.1.19
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/dist/deposit.d.ts +1 -1
- package/dist/deposit.js +171 -190
- package/dist/depositSPL.d.ts +1 -1
- package/dist/depositSPL.js +170 -189
- package/dist/getUtxos.js +7 -2
- package/dist/withdraw.d.ts +1 -1
- package/dist/withdraw.js +109 -129
- package/dist/withdrawSPL.d.ts +1 -1
- package/dist/withdrawSPL.js +111 -130
- package/package.json +1 -1
- package/src/deposit.ts +196 -215
- package/src/depositSPL.ts +195 -213
- package/src/getUtxos.ts +9 -3
- package/src/withdraw.ts +129 -150
- package/src/withdrawSPL.ts +131 -152
package/dist/deposit.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as hasher from '@lightprotocol/hasher.rs';
|
|
2
1
|
import { Connection, PublicKey, VersionedTransaction } from '@solana/web3.js';
|
|
2
|
+
import * as hasher from '@lightprotocol/hasher.rs';
|
|
3
3
|
import { EncryptionService } from './utils/encryption.js';
|
|
4
4
|
type DepositParams = {
|
|
5
5
|
publicKey: PublicKey;
|
package/dist/deposit.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PublicKey, TransactionInstruction, SystemProgram, ComputeBudgetProgram, VersionedTransaction, TransactionMessage, LAMPORTS_PER_SOL } from '@solana/web3.js';
|
|
2
2
|
import BN from 'bn.js';
|
|
3
|
-
import { getUtxos } from './getUtxos.js';
|
|
4
|
-
import { Keypair as UtxoKeypair } from './models/keypair.js';
|
|
5
3
|
import { Utxo } from './models/utxo.js';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
4
|
+
import { fetchMerkleProof, findNullifierPDAs, getExtDataHash, getProgramAccounts, queryRemoteTreeState, findCrossCheckNullifierPDAs } from './utils/utils.js';
|
|
5
|
+
import { prove, parseProofToBytesArray, parseToBytesArray } from './utils/prover.js';
|
|
6
|
+
import { MerkleTree } from './utils/merkle_tree.js';
|
|
8
7
|
import { serializeProofAndExtData } from './utils/encryption.js';
|
|
8
|
+
import { Keypair as UtxoKeypair } from './models/keypair.js';
|
|
9
|
+
import { getUtxos } from './getUtxos.js';
|
|
10
|
+
import { FIELD_SIZE, FEE_RECIPIENT, MERKLE_TREE_DEPTH, RELAYER_API_URL, PROGRAM_ID, ALT_ADDRESS } from './utils/constants.js';
|
|
11
|
+
import { useExistingALT } from './utils/address_lookup_table.js';
|
|
9
12
|
import { logger } from './utils/logger.js';
|
|
10
|
-
import { MerkleTree } from './utils/merkle_tree.js';
|
|
11
|
-
import { parseProofToBytesArray, parseToBytesArray, prove } from './utils/prover.js';
|
|
12
|
-
import { fetchMerkleProof, findCrossCheckNullifierPDAs, findNullifierPDAs, getExtDataHash, getProgramAccounts, queryRemoteTreeState } from './utils/utils.js';
|
|
13
13
|
// Function to relay pre-signed deposit transaction to indexer backend
|
|
14
14
|
async function relayDepositToIndexer(signedTransaction, publicKey, referrer) {
|
|
15
15
|
try {
|
|
@@ -88,196 +88,177 @@ export async function deposit({ lightWasm, storage, keyBasePath, publicKey, conn
|
|
|
88
88
|
let inputs;
|
|
89
89
|
let inputMerklePathIndices;
|
|
90
90
|
let inputMerklePathElements;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
logger.debug(`Fee amount: ${fee_amount_in_lamports}`);
|
|
102
|
-
logger.debug(`Output amount: ${outputAmount}`);
|
|
103
|
-
// Use two dummy UTXOs as inputs
|
|
104
|
-
inputs = [
|
|
105
|
-
new Utxo({
|
|
106
|
-
lightWasm,
|
|
107
|
-
keypair: utxoKeypair
|
|
108
|
-
}),
|
|
109
|
-
new Utxo({
|
|
110
|
-
lightWasm,
|
|
111
|
-
keypair: utxoKeypair
|
|
112
|
-
})
|
|
113
|
-
];
|
|
114
|
-
// Both inputs are dummy, so use mock indices and zero-filled Merkle paths
|
|
115
|
-
inputMerklePathIndices = inputs.map((input) => input.index || 0);
|
|
116
|
-
inputMerklePathElements = inputs.map(() => {
|
|
117
|
-
return [...new Array(tree.levels).fill("0")];
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
// Scenario 2: Deposit that consolidates with existing UTXO(s)
|
|
122
|
-
const firstUtxo = existingUnspentUtxos[0];
|
|
123
|
-
const firstUtxoAmount = firstUtxo.amount;
|
|
124
|
-
const secondUtxoAmount = existingUnspentUtxos.length > 1 ? existingUnspentUtxos[1].amount : new BN(0);
|
|
125
|
-
extAmount = amount_in_lamports; // Still depositing new funds
|
|
126
|
-
// Output combines existing UTXO amounts + new deposit amount - fee
|
|
127
|
-
outputAmount = firstUtxoAmount.add(secondUtxoAmount).add(new BN(amount_in_lamports)).sub(new BN(fee_amount_in_lamports)).toString();
|
|
128
|
-
logger.debug(`Deposit with consolidation scenario:`);
|
|
129
|
-
logger.debug(`First existing UTXO amount: ${firstUtxoAmount.toString()}`);
|
|
130
|
-
if (secondUtxoAmount.gt(new BN(0))) {
|
|
131
|
-
logger.debug(`Second existing UTXO amount: ${secondUtxoAmount.toString()}`);
|
|
132
|
-
}
|
|
133
|
-
logger.debug(`New deposit amount: ${amount_in_lamports}`);
|
|
134
|
-
logger.debug(`Fee amount: ${fee_amount_in_lamports}`);
|
|
135
|
-
logger.debug(`Output amount (existing UTXOs + deposit - fee): ${outputAmount}`);
|
|
136
|
-
logger.debug(`External amount (deposit): ${extAmount}`);
|
|
137
|
-
logger.debug('\nFirst UTXO to be consolidated:');
|
|
138
|
-
await firstUtxo.log();
|
|
139
|
-
// Use first existing UTXO as first input, and either second UTXO or dummy UTXO as second input
|
|
140
|
-
const secondUtxo = existingUnspentUtxos.length > 1 ? existingUnspentUtxos[1] : new Utxo({
|
|
141
|
-
lightWasm,
|
|
142
|
-
keypair: utxoKeypair,
|
|
143
|
-
amount: '0'
|
|
144
|
-
});
|
|
145
|
-
inputs = [
|
|
146
|
-
firstUtxo, // Use the first existing UTXO
|
|
147
|
-
secondUtxo // Use second UTXO if available, otherwise dummy
|
|
148
|
-
];
|
|
149
|
-
// Fetch Merkle proofs for real UTXOs
|
|
150
|
-
const firstUtxoCommitment = await firstUtxo.getCommitment();
|
|
151
|
-
const firstUtxoMerkleProof = await fetchMerkleProof(firstUtxoCommitment);
|
|
152
|
-
let secondUtxoMerkleProof;
|
|
153
|
-
if (secondUtxo.amount.gt(new BN(0))) {
|
|
154
|
-
// Second UTXO is real, fetch its proof
|
|
155
|
-
const secondUtxoCommitment = await secondUtxo.getCommitment();
|
|
156
|
-
secondUtxoMerkleProof = await fetchMerkleProof(secondUtxoCommitment);
|
|
157
|
-
logger.debug('\nSecond UTXO to be consolidated:');
|
|
158
|
-
await secondUtxo.log();
|
|
159
|
-
}
|
|
160
|
-
// Use the real pathIndices from API for real inputs, mock index for dummy input
|
|
161
|
-
inputMerklePathIndices = [
|
|
162
|
-
firstUtxo.index || 0, // Use the real UTXO's index
|
|
163
|
-
secondUtxo.amount.gt(new BN(0)) ? (secondUtxo.index || 0) : 0 // Real UTXO index or dummy
|
|
164
|
-
];
|
|
165
|
-
// Create Merkle path elements: real proof for real inputs, zeros for dummy input
|
|
166
|
-
inputMerklePathElements = [
|
|
167
|
-
firstUtxoMerkleProof.pathElements, // Real Merkle proof for first existing UTXO
|
|
168
|
-
secondUtxo.amount.gt(new BN(0)) ? secondUtxoMerkleProof.pathElements : [...new Array(tree.levels).fill("0")] // Real proof or zero-filled for dummy
|
|
169
|
-
];
|
|
170
|
-
logger.debug(`Using first UTXO with amount: ${firstUtxo.amount.toString()} and index: ${firstUtxo.index}`);
|
|
171
|
-
logger.debug(`Using second ${secondUtxo.amount.gt(new BN(0)) ? 'UTXO' : 'dummy UTXO'} with amount: ${secondUtxo.amount.toString()}${secondUtxo.amount.gt(new BN(0)) ? ` and index: ${secondUtxo.index}` : ''}`);
|
|
172
|
-
logger.debug(`First UTXO Merkle proof path indices from API: [${firstUtxoMerkleProof.pathIndices.join(', ')}]`);
|
|
173
|
-
if (secondUtxo.amount.gt(new BN(0))) {
|
|
174
|
-
logger.debug(`Second UTXO Merkle proof path indices from API: [${secondUtxoMerkleProof.pathIndices.join(', ')}]`);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
const publicAmountForCircuit = new BN(extAmount).sub(new BN(fee_amount_in_lamports)).add(FIELD_SIZE).mod(FIELD_SIZE);
|
|
178
|
-
logger.debug(`Public amount calculation: (${extAmount} - ${fee_amount_in_lamports} + FIELD_SIZE) % FIELD_SIZE = ${publicAmountForCircuit.toString()}`);
|
|
179
|
-
// Create outputs for the transaction with the same shared keypair
|
|
180
|
-
const outputs = [
|
|
91
|
+
if (existingUnspentUtxos.length === 0) {
|
|
92
|
+
// Scenario 1: Fresh deposit with dummy inputs - add new funds to the system
|
|
93
|
+
extAmount = amount_in_lamports;
|
|
94
|
+
outputAmount = new BN(amount_in_lamports).sub(new BN(fee_amount_in_lamports)).toString();
|
|
95
|
+
logger.debug(`Fresh deposit scenario (no existing UTXOs):`);
|
|
96
|
+
logger.debug(`External amount (deposit): ${extAmount}`);
|
|
97
|
+
logger.debug(`Fee amount: ${fee_amount_in_lamports}`);
|
|
98
|
+
logger.debug(`Output amount: ${outputAmount}`);
|
|
99
|
+
// Use two dummy UTXOs as inputs
|
|
100
|
+
inputs = [
|
|
181
101
|
new Utxo({
|
|
182
102
|
lightWasm,
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
index: currentNextIndex // This UTXO will be inserted at currentNextIndex
|
|
186
|
-
}), // Output with value (either deposit amount minus fee, or input amount minus fee)
|
|
103
|
+
keypair: utxoKeypair
|
|
104
|
+
}),
|
|
187
105
|
new Utxo({
|
|
188
106
|
lightWasm,
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
index: currentNextIndex + 1 // This UTXO will be inserted at currentNextIndex + 1
|
|
192
|
-
}) // Empty UTXO
|
|
107
|
+
keypair: utxoKeypair
|
|
108
|
+
})
|
|
193
109
|
];
|
|
194
|
-
//
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
//
|
|
207
|
-
|
|
208
|
-
logger.debug(
|
|
209
|
-
logger.debug(
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
logger.debug(
|
|
215
|
-
|
|
216
|
-
logger.debug(
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
//
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
};
|
|
240
|
-
// Calculate the extDataHash with the encrypted outputs (now includes mintAddress for security)
|
|
241
|
-
const calculatedExtDataHash = getExtDataHash(extData);
|
|
242
|
-
// Create the input for the proof generation (must match circuit input order exactly)
|
|
243
|
-
const input = {
|
|
244
|
-
// Common transaction data
|
|
245
|
-
root: root,
|
|
246
|
-
inputNullifier: inputNullifiers, // Use resolved values instead of Promise objects
|
|
247
|
-
outputCommitment: outputCommitments, // Use resolved values instead of Promise objects
|
|
248
|
-
publicAmount: publicAmountForCircuit.toString(), // Use proper field arithmetic result
|
|
249
|
-
extDataHash: calculatedExtDataHash,
|
|
250
|
-
// Input UTXO data (UTXOs being spent) - ensure all values are in decimal format
|
|
251
|
-
inAmount: inputs.map(x => x.amount.toString(10)),
|
|
252
|
-
inPrivateKey: inputs.map(x => x.keypair.privkey),
|
|
253
|
-
inBlinding: inputs.map(x => x.blinding.toString(10)),
|
|
254
|
-
inPathIndices: inputMerklePathIndices,
|
|
255
|
-
inPathElements: inputMerklePathElements,
|
|
256
|
-
// Output UTXO data (UTXOs being created) - ensure all values are in decimal format
|
|
257
|
-
outAmount: outputs.map(x => x.amount.toString(10)),
|
|
258
|
-
outBlinding: outputs.map(x => x.blinding.toString(10)),
|
|
259
|
-
outPubkey: outputs.map(x => x.keypair.pubkey),
|
|
260
|
-
// new mint address
|
|
261
|
-
mintAddress: inputs[0].mintAddress
|
|
262
|
-
};
|
|
263
|
-
logger.info('generating ZK proof...');
|
|
264
|
-
// Generate the zero-knowledge proof
|
|
265
|
-
return await prove(input, keyBasePath);
|
|
266
|
-
};
|
|
267
|
-
let getProveResult = null;
|
|
268
|
-
while (!getProveResult) {
|
|
269
|
-
try {
|
|
270
|
-
getProveResult = await getProve();
|
|
110
|
+
// Both inputs are dummy, so use mock indices and zero-filled Merkle paths
|
|
111
|
+
inputMerklePathIndices = inputs.map((input) => input.index || 0);
|
|
112
|
+
inputMerklePathElements = inputs.map(() => {
|
|
113
|
+
return [...new Array(tree.levels).fill("0")];
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
// Scenario 2: Deposit that consolidates with existing UTXO(s)
|
|
118
|
+
const firstUtxo = existingUnspentUtxos[0];
|
|
119
|
+
const firstUtxoAmount = firstUtxo.amount;
|
|
120
|
+
const secondUtxoAmount = existingUnspentUtxos.length > 1 ? existingUnspentUtxos[1].amount : new BN(0);
|
|
121
|
+
extAmount = amount_in_lamports; // Still depositing new funds
|
|
122
|
+
// Output combines existing UTXO amounts + new deposit amount - fee
|
|
123
|
+
outputAmount = firstUtxoAmount.add(secondUtxoAmount).add(new BN(amount_in_lamports)).sub(new BN(fee_amount_in_lamports)).toString();
|
|
124
|
+
logger.debug(`Deposit with consolidation scenario:`);
|
|
125
|
+
logger.debug(`First existing UTXO amount: ${firstUtxoAmount.toString()}`);
|
|
126
|
+
if (secondUtxoAmount.gt(new BN(0))) {
|
|
127
|
+
logger.debug(`Second existing UTXO amount: ${secondUtxoAmount.toString()}`);
|
|
128
|
+
}
|
|
129
|
+
logger.debug(`New deposit amount: ${amount_in_lamports}`);
|
|
130
|
+
logger.debug(`Fee amount: ${fee_amount_in_lamports}`);
|
|
131
|
+
logger.debug(`Output amount (existing UTXOs + deposit - fee): ${outputAmount}`);
|
|
132
|
+
logger.debug(`External amount (deposit): ${extAmount}`);
|
|
133
|
+
logger.debug('\nFirst UTXO to be consolidated:');
|
|
134
|
+
await firstUtxo.log();
|
|
135
|
+
// Use first existing UTXO as first input, and either second UTXO or dummy UTXO as second input
|
|
136
|
+
const secondUtxo = existingUnspentUtxos.length > 1 ? existingUnspentUtxos[1] : new Utxo({
|
|
137
|
+
lightWasm,
|
|
138
|
+
keypair: utxoKeypair,
|
|
139
|
+
amount: '0'
|
|
140
|
+
});
|
|
141
|
+
inputs = [
|
|
142
|
+
firstUtxo, // Use the first existing UTXO
|
|
143
|
+
secondUtxo // Use second UTXO if available, otherwise dummy
|
|
144
|
+
];
|
|
145
|
+
// Fetch Merkle proofs for real UTXOs
|
|
146
|
+
const firstUtxoCommitment = await firstUtxo.getCommitment();
|
|
147
|
+
const firstUtxoMerkleProof = await fetchMerkleProof(firstUtxoCommitment);
|
|
148
|
+
let secondUtxoMerkleProof;
|
|
149
|
+
if (secondUtxo.amount.gt(new BN(0))) {
|
|
150
|
+
// Second UTXO is real, fetch its proof
|
|
151
|
+
const secondUtxoCommitment = await secondUtxo.getCommitment();
|
|
152
|
+
secondUtxoMerkleProof = await fetchMerkleProof(secondUtxoCommitment);
|
|
153
|
+
logger.debug('\nSecond UTXO to be consolidated:');
|
|
154
|
+
await secondUtxo.log();
|
|
271
155
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
156
|
+
// Use the real pathIndices from API for real inputs, mock index for dummy input
|
|
157
|
+
inputMerklePathIndices = [
|
|
158
|
+
firstUtxo.index || 0, // Use the real UTXO's index
|
|
159
|
+
secondUtxo.amount.gt(new BN(0)) ? (secondUtxo.index || 0) : 0 // Real UTXO index or dummy
|
|
160
|
+
];
|
|
161
|
+
// Create Merkle path elements: real proof for real inputs, zeros for dummy input
|
|
162
|
+
inputMerklePathElements = [
|
|
163
|
+
firstUtxoMerkleProof.pathElements, // Real Merkle proof for first existing UTXO
|
|
164
|
+
secondUtxo.amount.gt(new BN(0)) ? secondUtxoMerkleProof.pathElements : [...new Array(tree.levels).fill("0")] // Real proof or zero-filled for dummy
|
|
165
|
+
];
|
|
166
|
+
logger.debug(`Using first UTXO with amount: ${firstUtxo.amount.toString()} and index: ${firstUtxo.index}`);
|
|
167
|
+
logger.debug(`Using second ${secondUtxo.amount.gt(new BN(0)) ? 'UTXO' : 'dummy UTXO'} with amount: ${secondUtxo.amount.toString()}${secondUtxo.amount.gt(new BN(0)) ? ` and index: ${secondUtxo.index}` : ''}`);
|
|
168
|
+
logger.debug(`First UTXO Merkle proof path indices from API: [${firstUtxoMerkleProof.pathIndices.join(', ')}]`);
|
|
169
|
+
if (secondUtxo.amount.gt(new BN(0))) {
|
|
170
|
+
logger.debug(`Second UTXO Merkle proof path indices from API: [${secondUtxoMerkleProof.pathIndices.join(', ')}]`);
|
|
278
171
|
}
|
|
279
172
|
}
|
|
280
|
-
const
|
|
173
|
+
const publicAmountForCircuit = new BN(extAmount).sub(new BN(fee_amount_in_lamports)).add(FIELD_SIZE).mod(FIELD_SIZE);
|
|
174
|
+
logger.debug(`Public amount calculation: (${extAmount} - ${fee_amount_in_lamports} + FIELD_SIZE) % FIELD_SIZE = ${publicAmountForCircuit.toString()}`);
|
|
175
|
+
// Create outputs for the transaction with the same shared keypair
|
|
176
|
+
const outputs = [
|
|
177
|
+
new Utxo({
|
|
178
|
+
lightWasm,
|
|
179
|
+
amount: outputAmount,
|
|
180
|
+
keypair: utxoKeypair,
|
|
181
|
+
index: currentNextIndex // This UTXO will be inserted at currentNextIndex
|
|
182
|
+
}), // Output with value (either deposit amount minus fee, or input amount minus fee)
|
|
183
|
+
new Utxo({
|
|
184
|
+
lightWasm,
|
|
185
|
+
amount: '0',
|
|
186
|
+
keypair: utxoKeypair,
|
|
187
|
+
index: currentNextIndex + 1 // This UTXO will be inserted at currentNextIndex + 1
|
|
188
|
+
}) // Empty UTXO
|
|
189
|
+
];
|
|
190
|
+
// Verify this matches the circuit balance equation: sumIns + publicAmount = sumOuts
|
|
191
|
+
const sumIns = inputs.reduce((sum, input) => sum.add(input.amount), new BN(0));
|
|
192
|
+
const sumOuts = outputs.reduce((sum, output) => sum.add(output.amount), new BN(0));
|
|
193
|
+
logger.debug(`Circuit balance check: sumIns(${sumIns.toString()}) + publicAmount(${publicAmountForCircuit.toString()}) should equal sumOuts(${sumOuts.toString()})`);
|
|
194
|
+
// Convert to circuit-compatible format
|
|
195
|
+
const publicAmountCircuitResult = sumIns.add(publicAmountForCircuit).mod(FIELD_SIZE);
|
|
196
|
+
logger.debug(`Balance verification: ${sumIns.toString()} + ${publicAmountForCircuit.toString()} (mod FIELD_SIZE) = ${publicAmountCircuitResult.toString()}`);
|
|
197
|
+
logger.debug(`Expected sum of outputs: ${sumOuts.toString()}`);
|
|
198
|
+
logger.debug(`Balance equation satisfied: ${publicAmountCircuitResult.eq(sumOuts)}`);
|
|
199
|
+
// Generate nullifiers and commitments
|
|
200
|
+
const inputNullifiers = await Promise.all(inputs.map(x => x.getNullifier()));
|
|
201
|
+
const outputCommitments = await Promise.all(outputs.map(x => x.getCommitment()));
|
|
202
|
+
// Save original commitment and nullifier values for verification
|
|
203
|
+
logger.debug('\n=== UTXO VALIDATION ===');
|
|
204
|
+
logger.debug('Output 0 Commitment:', outputCommitments[0]);
|
|
205
|
+
logger.debug('Output 1 Commitment:', outputCommitments[1]);
|
|
206
|
+
// Encrypt the UTXO data using a compact format that includes the keypair
|
|
207
|
+
logger.debug('\nEncrypting UTXOs with keypair data...');
|
|
208
|
+
const encryptedOutput1 = encryptionService.encryptUtxo(outputs[0]);
|
|
209
|
+
const encryptedOutput2 = encryptionService.encryptUtxo(outputs[1]);
|
|
210
|
+
logger.debug(`\nOutput[0] (with value):`);
|
|
211
|
+
await outputs[0].log();
|
|
212
|
+
logger.debug(`\nOutput[1] (empty):`);
|
|
213
|
+
await outputs[1].log();
|
|
214
|
+
logger.debug(`\nEncrypted output 1 size: ${encryptedOutput1.length} bytes`);
|
|
215
|
+
logger.debug(`Encrypted output 2 size: ${encryptedOutput2.length} bytes`);
|
|
216
|
+
logger.debug(`Total encrypted outputs size: ${encryptedOutput1.length + encryptedOutput2.length} bytes`);
|
|
217
|
+
// Test decryption to verify commitment values match
|
|
218
|
+
logger.debug('\n=== TESTING DECRYPTION ===');
|
|
219
|
+
logger.debug('Decrypting output 1 to verify commitment matches...');
|
|
220
|
+
const decryptedUtxo1 = await encryptionService.decryptUtxo(encryptedOutput1, lightWasm);
|
|
221
|
+
const decryptedCommitment1 = await decryptedUtxo1.getCommitment();
|
|
222
|
+
logger.debug('Original commitment:', outputCommitments[0]);
|
|
223
|
+
logger.debug('Decrypted commitment:', decryptedCommitment1);
|
|
224
|
+
logger.debug('Commitment matches:', outputCommitments[0] === decryptedCommitment1);
|
|
225
|
+
// Create the deposit ExtData with real encrypted outputs
|
|
226
|
+
const extData = {
|
|
227
|
+
// recipient - just a placeholder, not actually used for deposits.
|
|
228
|
+
recipient: new PublicKey('AWexibGxNFKTa1b5R5MN4PJr9HWnWRwf8EW9g8cLx3dM'),
|
|
229
|
+
extAmount: new BN(extAmount),
|
|
230
|
+
encryptedOutput1: encryptedOutput1,
|
|
231
|
+
encryptedOutput2: encryptedOutput2,
|
|
232
|
+
fee: new BN(fee_amount_in_lamports),
|
|
233
|
+
feeRecipient: FEE_RECIPIENT,
|
|
234
|
+
mintAddress: inputs[0].mintAddress
|
|
235
|
+
};
|
|
236
|
+
// Calculate the extDataHash with the encrypted outputs (now includes mintAddress for security)
|
|
237
|
+
const calculatedExtDataHash = getExtDataHash(extData);
|
|
238
|
+
// Create the input for the proof generation (must match circuit input order exactly)
|
|
239
|
+
const input = {
|
|
240
|
+
// Common transaction data
|
|
241
|
+
root: root,
|
|
242
|
+
inputNullifier: inputNullifiers, // Use resolved values instead of Promise objects
|
|
243
|
+
outputCommitment: outputCommitments, // Use resolved values instead of Promise objects
|
|
244
|
+
publicAmount: publicAmountForCircuit.toString(), // Use proper field arithmetic result
|
|
245
|
+
extDataHash: calculatedExtDataHash,
|
|
246
|
+
// Input UTXO data (UTXOs being spent) - ensure all values are in decimal format
|
|
247
|
+
inAmount: inputs.map(x => x.amount.toString(10)),
|
|
248
|
+
inPrivateKey: inputs.map(x => x.keypair.privkey),
|
|
249
|
+
inBlinding: inputs.map(x => x.blinding.toString(10)),
|
|
250
|
+
inPathIndices: inputMerklePathIndices,
|
|
251
|
+
inPathElements: inputMerklePathElements,
|
|
252
|
+
// Output UTXO data (UTXOs being created) - ensure all values are in decimal format
|
|
253
|
+
outAmount: outputs.map(x => x.amount.toString(10)),
|
|
254
|
+
outBlinding: outputs.map(x => x.blinding.toString(10)),
|
|
255
|
+
outPubkey: outputs.map(x => x.keypair.pubkey),
|
|
256
|
+
// new mint address
|
|
257
|
+
mintAddress: inputs[0].mintAddress
|
|
258
|
+
};
|
|
259
|
+
logger.info('generating ZK proof...');
|
|
260
|
+
// Generate the zero-knowledge proof
|
|
261
|
+
const { proof, publicSignals } = await prove(input, keyBasePath);
|
|
281
262
|
// Parse the proof and public signals into byte arrays
|
|
282
263
|
const proofInBytes = parseProofToBytesArray(proof);
|
|
283
264
|
const inputsInBytes = parseToBytesArray(publicSignals);
|
package/dist/depositSPL.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as hasher from '@lightprotocol/hasher.rs';
|
|
2
1
|
import { Connection, PublicKey, VersionedTransaction } from '@solana/web3.js';
|
|
2
|
+
import * as hasher from '@lightprotocol/hasher.rs';
|
|
3
3
|
import { EncryptionService } from './utils/encryption.js';
|
|
4
4
|
type DepositParams = {
|
|
5
5
|
mintAddress: PublicKey | string;
|