privacycash 1.0.6
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/.github/workflows/npm-publish.yml +67 -0
- package/README.md +22 -0
- package/__tests__/e2e.test.ts +52 -0
- package/__tests__/encryption.test.ts +1635 -0
- package/circuit2/transaction2.wasm +0 -0
- package/circuit2/transaction2.zkey +0 -0
- package/dist/config.d.ts +7 -0
- package/dist/config.js +16 -0
- package/dist/deposit.d.ts +18 -0
- package/dist/deposit.js +402 -0
- package/dist/exportUtils.d.ts +6 -0
- package/dist/exportUtils.js +6 -0
- package/dist/getUtxos.d.ts +27 -0
- package/dist/getUtxos.js +352 -0
- package/dist/index.d.ts +61 -0
- package/dist/index.js +169 -0
- package/dist/models/keypair.d.ts +26 -0
- package/dist/models/keypair.js +43 -0
- package/dist/models/utxo.d.ts +49 -0
- package/dist/models/utxo.js +76 -0
- package/dist/utils/address_lookup_table.d.ts +8 -0
- package/dist/utils/address_lookup_table.js +21 -0
- package/dist/utils/constants.d.ts +14 -0
- package/dist/utils/constants.js +15 -0
- package/dist/utils/encryption.d.ts +107 -0
- package/dist/utils/encryption.js +374 -0
- package/dist/utils/logger.d.ts +9 -0
- package/dist/utils/logger.js +35 -0
- package/dist/utils/merkle_tree.d.ts +92 -0
- package/dist/utils/merkle_tree.js +186 -0
- package/dist/utils/node-shim.d.ts +5 -0
- package/dist/utils/node-shim.js +5 -0
- package/dist/utils/prover.d.ts +33 -0
- package/dist/utils/prover.js +123 -0
- package/dist/utils/utils.d.ts +67 -0
- package/dist/utils/utils.js +151 -0
- package/dist/withdraw.d.ts +21 -0
- package/dist/withdraw.js +270 -0
- package/package.json +48 -0
- package/src/config.ts +28 -0
- package/src/deposit.ts +496 -0
- package/src/exportUtils.ts +6 -0
- package/src/getUtxos.ts +466 -0
- package/src/index.ts +191 -0
- package/src/models/keypair.ts +52 -0
- package/src/models/utxo.ts +97 -0
- package/src/utils/address_lookup_table.ts +29 -0
- package/src/utils/constants.ts +26 -0
- package/src/utils/encryption.ts +461 -0
- package/src/utils/logger.ts +42 -0
- package/src/utils/merkle_tree.ts +207 -0
- package/src/utils/node-shim.ts +6 -0
- package/src/utils/prover.ts +189 -0
- package/src/utils/utils.ts +213 -0
- package/src/withdraw.ts +334 -0
- package/tsconfig.json +28 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// log level
|
|
2
|
+
export type LogLevel = "debug" | "info" | "warn" | "error";
|
|
3
|
+
|
|
4
|
+
export type LoggerFn = (level: LogLevel, message: string) => void;
|
|
5
|
+
|
|
6
|
+
const defaultLogger: LoggerFn = (level, message) => {
|
|
7
|
+
const prefix = `[${level.toUpperCase()}]`;
|
|
8
|
+
console.log(prefix, message);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
let userLogger: LoggerFn = defaultLogger;
|
|
12
|
+
|
|
13
|
+
export function setLogger(logger: LoggerFn) {
|
|
14
|
+
userLogger = logger;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function argToStr(args: unknown[]) {
|
|
18
|
+
return args.map(arg => {
|
|
19
|
+
if (typeof arg === "object" && arg !== null) {
|
|
20
|
+
try {
|
|
21
|
+
return JSON.stringify(arg);
|
|
22
|
+
} catch {
|
|
23
|
+
return String(arg);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return String(arg);
|
|
27
|
+
}).join(" ");
|
|
28
|
+
}
|
|
29
|
+
export const logger = {
|
|
30
|
+
debug: (...args: unknown[]) => {
|
|
31
|
+
userLogger('debug', argToStr(args))
|
|
32
|
+
},
|
|
33
|
+
info: (...args: unknown[]) => {
|
|
34
|
+
userLogger('info', argToStr(args))
|
|
35
|
+
},
|
|
36
|
+
warn: (...args: unknown[]) => {
|
|
37
|
+
userLogger('warn', argToStr(args))
|
|
38
|
+
},
|
|
39
|
+
error: (...args: unknown[]) => {
|
|
40
|
+
userLogger('error', argToStr(args))
|
|
41
|
+
},
|
|
42
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import * as hasher from '@lightprotocol/hasher.rs';
|
|
2
|
+
export const DEFAULT_ZERO = 0;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @callback hashFunction
|
|
6
|
+
* @param left Left leaf
|
|
7
|
+
* @param right Right leaf
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Merkle tree
|
|
11
|
+
*/
|
|
12
|
+
export class MerkleTree {
|
|
13
|
+
/**
|
|
14
|
+
* Constructor
|
|
15
|
+
* @param {number} levels Number of levels in the tree
|
|
16
|
+
* @param {Array} [elements] Initial elements
|
|
17
|
+
* @param {Object} options
|
|
18
|
+
* @param {hashFunction} [options.hashFunction] Function used to hash 2 leaves
|
|
19
|
+
* @param [options.zeroElement] Value for non-existent leaves
|
|
20
|
+
*/
|
|
21
|
+
levels: number;
|
|
22
|
+
capacity: number;
|
|
23
|
+
zeroElement;
|
|
24
|
+
_zeros: string[];
|
|
25
|
+
_layers: string[][];
|
|
26
|
+
_lightWasm: hasher.LightWasm;
|
|
27
|
+
|
|
28
|
+
constructor(
|
|
29
|
+
levels: number,
|
|
30
|
+
lightWasm: hasher.LightWasm,
|
|
31
|
+
elements: string[] = [],
|
|
32
|
+
{ zeroElement = DEFAULT_ZERO } = {},
|
|
33
|
+
) {
|
|
34
|
+
this.levels = levels;
|
|
35
|
+
this.capacity = 2 ** levels;
|
|
36
|
+
this.zeroElement = zeroElement.toString();
|
|
37
|
+
this._lightWasm = lightWasm;
|
|
38
|
+
if (elements.length > this.capacity) {
|
|
39
|
+
throw new Error("Tree is full");
|
|
40
|
+
}
|
|
41
|
+
this._zeros = [];
|
|
42
|
+
this._layers = [];
|
|
43
|
+
this._layers[0] = elements;
|
|
44
|
+
this._zeros[0] = this.zeroElement;
|
|
45
|
+
|
|
46
|
+
for (let i = 1; i <= levels; i++) {
|
|
47
|
+
this._zeros[i] = this._lightWasm.poseidonHashString([
|
|
48
|
+
this._zeros[i - 1],
|
|
49
|
+
this._zeros[i - 1],
|
|
50
|
+
]);
|
|
51
|
+
}
|
|
52
|
+
this._rebuild();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
_rebuild() {
|
|
56
|
+
for (let level = 1; level <= this.levels; level++) {
|
|
57
|
+
this._layers[level] = [];
|
|
58
|
+
for (let i = 0; i < Math.ceil(this._layers[level - 1].length / 2); i++) {
|
|
59
|
+
this._layers[level][i] = this._lightWasm.poseidonHashString([
|
|
60
|
+
this._layers[level - 1][i * 2],
|
|
61
|
+
i * 2 + 1 < this._layers[level - 1].length
|
|
62
|
+
? this._layers[level - 1][i * 2 + 1]
|
|
63
|
+
: this._zeros[level - 1],
|
|
64
|
+
]);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Get tree root
|
|
71
|
+
* @returns {*}
|
|
72
|
+
*/
|
|
73
|
+
root() {
|
|
74
|
+
return this._layers[this.levels].length > 0
|
|
75
|
+
? this._layers[this.levels][0]
|
|
76
|
+
: this._zeros[this.levels];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Insert new element into the tree
|
|
81
|
+
* @param element Element to insert
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
insert(element: string) {
|
|
85
|
+
if (this._layers[0].length >= this.capacity) {
|
|
86
|
+
throw new Error("Tree is full");
|
|
87
|
+
}
|
|
88
|
+
this.update(this._layers[0].length, element);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Insert multiple elements into the tree. Tree will be fully rebuilt during this operation.
|
|
93
|
+
* @param {Array} elements Elements to insert
|
|
94
|
+
*/
|
|
95
|
+
bulkInsert(elements: string[]) {
|
|
96
|
+
if (this._layers[0].length + elements.length > this.capacity) {
|
|
97
|
+
throw new Error("Tree is full");
|
|
98
|
+
}
|
|
99
|
+
this._layers[0].push(...elements);
|
|
100
|
+
this._rebuild();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// TODO: update does not work debug
|
|
104
|
+
/**
|
|
105
|
+
* Change an element in the tree
|
|
106
|
+
* @param {number} index Index of element to change
|
|
107
|
+
* @param element Updated element value
|
|
108
|
+
*/
|
|
109
|
+
update(index: number, element: string) {
|
|
110
|
+
// index 0 and 1 and element is the commitment hash
|
|
111
|
+
if (
|
|
112
|
+
isNaN(Number(index)) ||
|
|
113
|
+
index < 0 ||
|
|
114
|
+
index > this._layers[0].length ||
|
|
115
|
+
index >= this.capacity
|
|
116
|
+
) {
|
|
117
|
+
throw new Error("Insert index out of bounds: " + index);
|
|
118
|
+
}
|
|
119
|
+
this._layers[0][index] = element;
|
|
120
|
+
for (let level = 1; level <= this.levels; level++) {
|
|
121
|
+
index >>= 1;
|
|
122
|
+
this._layers[level][index] = this._lightWasm.poseidonHashString([
|
|
123
|
+
this._layers[level - 1][index * 2],
|
|
124
|
+
index * 2 + 1 < this._layers[level - 1].length
|
|
125
|
+
? this._layers[level - 1][index * 2 + 1]
|
|
126
|
+
: this._zeros[level - 1],
|
|
127
|
+
]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Get merkle path to a leaf
|
|
133
|
+
* @param {number} index Leaf index to generate path for
|
|
134
|
+
* @returns {{pathElements: number[], pathIndex: number[]}} An object containing adjacent elements and left-right index
|
|
135
|
+
*/
|
|
136
|
+
path(index: number) {
|
|
137
|
+
if (isNaN(Number(index)) || index < 0 || index >= this._layers[0].length) {
|
|
138
|
+
throw new Error("Index out of bounds: " + index);
|
|
139
|
+
}
|
|
140
|
+
const pathElements: string[] = [];
|
|
141
|
+
const pathIndices: number[] = [];
|
|
142
|
+
for (let level = 0; level < this.levels; level++) {
|
|
143
|
+
pathIndices[level] = index % 2;
|
|
144
|
+
pathElements[level] =
|
|
145
|
+
(index ^ 1) < this._layers[level].length
|
|
146
|
+
? this._layers[level][index ^ 1]
|
|
147
|
+
: this._zeros[level];
|
|
148
|
+
index >>= 1;
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
pathElements,
|
|
152
|
+
pathIndices,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Find an element in the tree
|
|
158
|
+
* @param element An element to find
|
|
159
|
+
* @param comparator A function that checks leaf value equality
|
|
160
|
+
* @returns {number} Index if element is found, otherwise -1
|
|
161
|
+
*/
|
|
162
|
+
indexOf(element: string, comparator: Function | null = null) {
|
|
163
|
+
if (comparator) {
|
|
164
|
+
return this._layers[0].findIndex((el: string) => comparator(element, el));
|
|
165
|
+
} else {
|
|
166
|
+
return this._layers[0].indexOf(element);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Returns a copy of non-zero tree elements
|
|
172
|
+
* @returns {Object[]}
|
|
173
|
+
*/
|
|
174
|
+
elements() {
|
|
175
|
+
return this._layers[0].slice();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Serialize entire tree state including intermediate layers into a plain object
|
|
180
|
+
* Deserializing it back will not require to recompute any hashes
|
|
181
|
+
* Elements are not converted to a plain type, this is responsibility of the caller
|
|
182
|
+
*/
|
|
183
|
+
serialize() {
|
|
184
|
+
return {
|
|
185
|
+
levels: this.levels,
|
|
186
|
+
_zeros: this._zeros,
|
|
187
|
+
_layers: this._layers,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Deserialize data into a MerkleTree instance
|
|
193
|
+
* Make sure to provide the same hashFunction as was used in the source tree,
|
|
194
|
+
* otherwise the tree state will be invalid
|
|
195
|
+
*
|
|
196
|
+
* @param data
|
|
197
|
+
* @param hashFunction
|
|
198
|
+
* @returns {MerkleTree}
|
|
199
|
+
*/
|
|
200
|
+
static deserialize(data: any, hashFunction: Function) {
|
|
201
|
+
const instance = Object.assign(Object.create(this.prototype), data);
|
|
202
|
+
instance._hash = hashFunction;
|
|
203
|
+
instance.capacity = 2 ** instance.levels;
|
|
204
|
+
instance.zeroElement = instance._zeros[0];
|
|
205
|
+
return instance;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ZK Proof Generation Utilities
|
|
3
|
+
*
|
|
4
|
+
* This file provides functions for generating zero-knowledge proofs for privacy-preserving
|
|
5
|
+
* transactions on Solana. It handles both snarkjs and zkutil proof generation workflows.
|
|
6
|
+
*
|
|
7
|
+
* Inspired by: https://github.com/tornadocash/tornado-nova/blob/f9264eeffe48bf5e04e19d8086ee6ec58cdf0d9e/src/prover.js
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/// <reference types="node" />
|
|
11
|
+
|
|
12
|
+
import * as anchor from "@coral-xyz/anchor";
|
|
13
|
+
import { wtns, groth16 } from 'snarkjs'
|
|
14
|
+
import { FIELD_SIZE } from './constants.js'
|
|
15
|
+
|
|
16
|
+
// @ts-ignore - ignore TypeScript errors for ffjavascript
|
|
17
|
+
import { utils } from 'ffjavascript'
|
|
18
|
+
|
|
19
|
+
// Type definitions for external modules
|
|
20
|
+
type WtnsModule = {
|
|
21
|
+
debug: (input: any, wasmFile: string, wtnsFile: string, symFile: string, options: any, logger: any) => Promise<void>
|
|
22
|
+
exportJson: (wtnsFile: string) => Promise<any>
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
type Groth16Module = {
|
|
26
|
+
fullProve: (input: any, wasmFile: string, zkeyFile: string) => Promise<{ proof: Proof; publicSignals: string[] }>
|
|
27
|
+
verify: (vkeyData: any, publicSignals: any, proof: Proof) => Promise<boolean>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type UtilsModule = {
|
|
31
|
+
stringifyBigInts: (obj: any) => any
|
|
32
|
+
unstringifyBigInts: (obj: any) => any
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Cast imported modules to their types
|
|
36
|
+
const wtnsTyped = wtns as unknown as WtnsModule
|
|
37
|
+
const groth16Typed = groth16 as unknown as Groth16Module
|
|
38
|
+
const utilsTyped = utils as unknown as UtilsModule
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
// Define interfaces for the proof structures
|
|
42
|
+
interface Proof {
|
|
43
|
+
pi_a: string[];
|
|
44
|
+
pi_b: string[][];
|
|
45
|
+
pi_c: string[];
|
|
46
|
+
protocol: string;
|
|
47
|
+
curve: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface ProofResult {
|
|
51
|
+
proof: Proof
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Generates a ZK proof using snarkjs and formats it for use on-chain
|
|
56
|
+
*
|
|
57
|
+
* @param input The circuit inputs to generate a proof for
|
|
58
|
+
* @param keyBasePath The base path for the circuit keys (.wasm and .zkey files)
|
|
59
|
+
* @returns A proof object with formatted proof elements and public signals
|
|
60
|
+
*/
|
|
61
|
+
async function prove(input: any, keyBasePath: string): Promise<{
|
|
62
|
+
proof: Proof
|
|
63
|
+
publicSignals: string[];
|
|
64
|
+
}> {
|
|
65
|
+
|
|
66
|
+
return await groth16Typed.fullProve(
|
|
67
|
+
utilsTyped.stringifyBigInts(input),
|
|
68
|
+
`${keyBasePath}.wasm`,
|
|
69
|
+
`${keyBasePath}.zkey`,
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function parseProofToBytesArray(
|
|
74
|
+
proof: Proof,
|
|
75
|
+
compressed: boolean = false,
|
|
76
|
+
): {
|
|
77
|
+
proofA: number[];
|
|
78
|
+
proofB: number[][];
|
|
79
|
+
proofC: number[];
|
|
80
|
+
} {
|
|
81
|
+
const proofJson = JSON.stringify(proof, null, 1);
|
|
82
|
+
const mydata = JSON.parse(proofJson.toString());
|
|
83
|
+
try {
|
|
84
|
+
for (const i in mydata) {
|
|
85
|
+
if (i == "pi_a" || i == "pi_c") {
|
|
86
|
+
for (const j in mydata[i]) {
|
|
87
|
+
mydata[i][j] = Array.from(
|
|
88
|
+
utils.leInt2Buff(utils.unstringifyBigInts(mydata[i][j]), 32),
|
|
89
|
+
).reverse();
|
|
90
|
+
}
|
|
91
|
+
} else if (i == "pi_b") {
|
|
92
|
+
for (const j in mydata[i]) {
|
|
93
|
+
for (const z in mydata[i][j]) {
|
|
94
|
+
mydata[i][j][z] = Array.from(
|
|
95
|
+
utils.leInt2Buff(utils.unstringifyBigInts(mydata[i][j][z]), 32),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (compressed) {
|
|
103
|
+
const proofA = mydata.pi_a[0];
|
|
104
|
+
// negate proof by reversing the bitmask
|
|
105
|
+
const proofAIsPositive = yElementIsPositiveG1(
|
|
106
|
+
new anchor.BN(mydata.pi_a[1]),
|
|
107
|
+
)
|
|
108
|
+
? false
|
|
109
|
+
: true;
|
|
110
|
+
proofA[0] = addBitmaskToByte(proofA[0], proofAIsPositive);
|
|
111
|
+
const proofB = mydata.pi_b[0].flat().reverse();
|
|
112
|
+
const proofBY = mydata.pi_b[1].flat().reverse();
|
|
113
|
+
const proofBIsPositive = yElementIsPositiveG2(
|
|
114
|
+
new anchor.BN(proofBY.slice(0, 32)),
|
|
115
|
+
new anchor.BN(proofBY.slice(32, 64)),
|
|
116
|
+
);
|
|
117
|
+
proofB[0] = addBitmaskToByte(proofB[0], proofBIsPositive);
|
|
118
|
+
const proofC = mydata.pi_c[0];
|
|
119
|
+
const proofCIsPositive = yElementIsPositiveG1(
|
|
120
|
+
new anchor.BN(mydata.pi_c[1]),
|
|
121
|
+
);
|
|
122
|
+
proofC[0] = addBitmaskToByte(proofC[0], proofCIsPositive);
|
|
123
|
+
return {
|
|
124
|
+
proofA,
|
|
125
|
+
proofB,
|
|
126
|
+
proofC,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
proofA: [mydata.pi_a[0], mydata.pi_a[1]].flat(),
|
|
131
|
+
proofB: [
|
|
132
|
+
mydata.pi_b[0].flat().reverse(),
|
|
133
|
+
mydata.pi_b[1].flat().reverse(),
|
|
134
|
+
].flat(),
|
|
135
|
+
proofC: [mydata.pi_c[0], mydata.pi_c[1]].flat(),
|
|
136
|
+
};
|
|
137
|
+
} catch (error: any) {
|
|
138
|
+
console.error("Error while parsing the proof.", error.message);
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// mainly used to parse the public signals of groth16 fullProve
|
|
144
|
+
export function parseToBytesArray(publicSignals: string[]): number[][] {
|
|
145
|
+
const publicInputsJson = JSON.stringify(publicSignals, null, 1);
|
|
146
|
+
const publicInputsBytesJson = JSON.parse(publicInputsJson.toString());
|
|
147
|
+
try {
|
|
148
|
+
const publicInputsBytes = new Array<Array<number>>();
|
|
149
|
+
for (const i in publicInputsBytesJson) {
|
|
150
|
+
const ref: Array<number> = Array.from([
|
|
151
|
+
...utils.leInt2Buff(utils.unstringifyBigInts(publicInputsBytesJson[i]), 32),
|
|
152
|
+
]).reverse();
|
|
153
|
+
publicInputsBytes.push(ref);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return publicInputsBytes;
|
|
157
|
+
} catch (error: any) {
|
|
158
|
+
console.error("Error while parsing public inputs.", error.message);
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function yElementIsPositiveG1(yElement: anchor.BN): boolean {
|
|
164
|
+
return yElement.lte(FIELD_SIZE.sub(yElement));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function yElementIsPositiveG2(yElement1: anchor.BN, yElement2: anchor.BN): boolean {
|
|
168
|
+
const fieldMidpoint = FIELD_SIZE.div(new anchor.BN(2));
|
|
169
|
+
|
|
170
|
+
// Compare the first component of the y coordinate
|
|
171
|
+
if (yElement1.lt(fieldMidpoint)) {
|
|
172
|
+
return true;
|
|
173
|
+
} else if (yElement1.gt(fieldMidpoint)) {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// If the first component is equal to the midpoint, compare the second component
|
|
178
|
+
return yElement2.lt(fieldMidpoint);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function addBitmaskToByte(byte: number, yIsPositive: boolean): number {
|
|
182
|
+
if (!yIsPositive) {
|
|
183
|
+
return (byte |= 1 << 7);
|
|
184
|
+
} else {
|
|
185
|
+
return byte;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export { prove, type Proof }
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for ZK Cash
|
|
3
|
+
*
|
|
4
|
+
* Provides common utility functions for the ZK Cash system
|
|
5
|
+
* Based on: https://github.com/tornadocash/tornado-nova
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import BN from 'bn.js';
|
|
9
|
+
import { Utxo } from '../models/utxo.js';
|
|
10
|
+
import * as borsh from 'borsh';
|
|
11
|
+
import { sha256 } from '@ethersproject/sha2';
|
|
12
|
+
import { PublicKey } from '@solana/web3.js';
|
|
13
|
+
import { INDEXER_API_URL, PROGRAM_ID } from './constants.js';
|
|
14
|
+
import { logger } from './logger.js';
|
|
15
|
+
import { getConfig } from '../config.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Calculate deposit fee based on deposit amount and fee rate
|
|
19
|
+
* @param depositAmount Amount being deposited in lamports
|
|
20
|
+
* @returns Fee amount in lamports
|
|
21
|
+
*/
|
|
22
|
+
export async function calculateDepositFee(depositAmount: number) {
|
|
23
|
+
return Math.floor(depositAmount * (await getConfig('deposit_fee_rate')) / 10000);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Calculate withdrawal fee based on withdrawal amount and fee rate
|
|
28
|
+
* @param withdrawalAmount Amount being withdrawn in lamports
|
|
29
|
+
* @returns Fee amount in lamports
|
|
30
|
+
*/
|
|
31
|
+
export async function calculateWithdrawalFee(withdrawalAmount: number) {
|
|
32
|
+
return Math.floor(withdrawalAmount * (await getConfig('withdraw_fee_rate')) / 10000);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Mock encryption function - in real implementation this would be proper encryption
|
|
37
|
+
* For testing, we just return a fixed prefix to ensure consistent extDataHash
|
|
38
|
+
* @param value Value to encrypt
|
|
39
|
+
* @returns Encrypted string representation
|
|
40
|
+
*/
|
|
41
|
+
export function mockEncrypt(value: Utxo): string {
|
|
42
|
+
return JSON.stringify(value);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Calculates the hash of ext data using Borsh serialization
|
|
47
|
+
* @param extData External data object containing recipient, amount, encrypted outputs, fee, fee recipient, and mint address
|
|
48
|
+
* @returns The hash as a Uint8Array (32 bytes)
|
|
49
|
+
*/
|
|
50
|
+
export function getExtDataHash(extData: {
|
|
51
|
+
recipient: string | PublicKey;
|
|
52
|
+
extAmount: string | number | BN;
|
|
53
|
+
encryptedOutput1?: string | Uint8Array; // Optional for Account Data Separation
|
|
54
|
+
encryptedOutput2?: string | Uint8Array; // Optional for Account Data Separation
|
|
55
|
+
fee: string | number | BN;
|
|
56
|
+
feeRecipient: string | PublicKey;
|
|
57
|
+
mintAddress: string | PublicKey;
|
|
58
|
+
}): Uint8Array {
|
|
59
|
+
// Convert all inputs to their appropriate types
|
|
60
|
+
const recipient = extData.recipient instanceof PublicKey
|
|
61
|
+
? extData.recipient
|
|
62
|
+
: new PublicKey(extData.recipient);
|
|
63
|
+
|
|
64
|
+
const feeRecipient = extData.feeRecipient instanceof PublicKey
|
|
65
|
+
? extData.feeRecipient
|
|
66
|
+
: new PublicKey(extData.feeRecipient);
|
|
67
|
+
|
|
68
|
+
const mintAddress = extData.mintAddress instanceof PublicKey
|
|
69
|
+
? extData.mintAddress
|
|
70
|
+
: new PublicKey(extData.mintAddress);
|
|
71
|
+
|
|
72
|
+
// Convert to BN for proper i64/u64 handling
|
|
73
|
+
const extAmount = new BN(extData.extAmount.toString());
|
|
74
|
+
const fee = new BN(extData.fee.toString());
|
|
75
|
+
|
|
76
|
+
// Handle encrypted outputs - they might not be present in Account Data Separation approach
|
|
77
|
+
const encryptedOutput1 = extData.encryptedOutput1
|
|
78
|
+
? Buffer.from(extData.encryptedOutput1 as any)
|
|
79
|
+
: Buffer.alloc(0); // Empty buffer if not provided
|
|
80
|
+
const encryptedOutput2 = extData.encryptedOutput2
|
|
81
|
+
? Buffer.from(extData.encryptedOutput2 as any)
|
|
82
|
+
: Buffer.alloc(0); // Empty buffer if not provided
|
|
83
|
+
|
|
84
|
+
// Define the borsh schema matching the Rust struct
|
|
85
|
+
const schema = {
|
|
86
|
+
struct: {
|
|
87
|
+
recipient: { array: { type: 'u8', len: 32 } },
|
|
88
|
+
extAmount: 'i64',
|
|
89
|
+
encryptedOutput1: { array: { type: 'u8' } },
|
|
90
|
+
encryptedOutput2: { array: { type: 'u8' } },
|
|
91
|
+
fee: 'u64',
|
|
92
|
+
feeRecipient: { array: { type: 'u8', len: 32 } },
|
|
93
|
+
mintAddress: { array: { type: 'u8', len: 32 } },
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const value = {
|
|
98
|
+
recipient: recipient.toBytes(),
|
|
99
|
+
extAmount: extAmount, // BN instance - Borsh handles it correctly with i64 type
|
|
100
|
+
encryptedOutput1: encryptedOutput1,
|
|
101
|
+
encryptedOutput2: encryptedOutput2,
|
|
102
|
+
fee: fee, // BN instance - Borsh handles it correctly with u64 type
|
|
103
|
+
feeRecipient: feeRecipient.toBytes(),
|
|
104
|
+
mintAddress: mintAddress.toBytes(),
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// Serialize with Borsh
|
|
108
|
+
const serializedData = borsh.serialize(schema, value);
|
|
109
|
+
|
|
110
|
+
// Calculate the SHA-256 hash
|
|
111
|
+
const hashHex = sha256(serializedData);
|
|
112
|
+
// Convert from hex string to Uint8Array
|
|
113
|
+
return Buffer.from(hashHex.slice(2), 'hex');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
// Function to fetch Merkle proof from API for a given commitment
|
|
118
|
+
export async function fetchMerkleProof(commitment: string): Promise<{ pathElements: string[], pathIndices: number[] }> {
|
|
119
|
+
try {
|
|
120
|
+
logger.debug(`Fetching Merkle proof for commitment: ${commitment}`);
|
|
121
|
+
const response = await fetch(`${INDEXER_API_URL}/merkle/proof/${commitment}`);
|
|
122
|
+
if (!response.ok) {
|
|
123
|
+
throw new Error(`Failed to fetch Merkle proof: ${response.status} ${response.statusText}`);
|
|
124
|
+
}
|
|
125
|
+
const data = await response.json() as { pathElements: string[], pathIndices: number[] };
|
|
126
|
+
logger.debug(`✓ Fetched Merkle proof with ${data.pathElements.length} elements`);
|
|
127
|
+
return data;
|
|
128
|
+
} catch (error) {
|
|
129
|
+
console.error(`Failed to fetch Merkle proof for commitment ${commitment}:`, error);
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Find nullifier PDAs for the given proof
|
|
135
|
+
export function findNullifierPDAs(proof: any) {
|
|
136
|
+
const [nullifier0PDA] = PublicKey.findProgramAddressSync(
|
|
137
|
+
[Buffer.from("nullifier0"), Buffer.from(proof.inputNullifiers[0])],
|
|
138
|
+
PROGRAM_ID
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
const [nullifier1PDA] = PublicKey.findProgramAddressSync(
|
|
142
|
+
[Buffer.from("nullifier1"), Buffer.from(proof.inputNullifiers[1])],
|
|
143
|
+
PROGRAM_ID
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
return { nullifier0PDA, nullifier1PDA };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Find commitment PDAs for the given proof
|
|
150
|
+
export function findCommitmentPDAs(proof: any) {
|
|
151
|
+
const [commitment0PDA] = PublicKey.findProgramAddressSync(
|
|
152
|
+
[Buffer.from("commitment0"), Buffer.from(proof.outputCommitments[0])],
|
|
153
|
+
PROGRAM_ID
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
const [commitment1PDA] = PublicKey.findProgramAddressSync(
|
|
157
|
+
[Buffer.from("commitment1"), Buffer.from(proof.outputCommitments[1])],
|
|
158
|
+
PROGRAM_ID
|
|
159
|
+
);
|
|
160
|
+
return { commitment0PDA, commitment1PDA };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Function to query remote tree state from indexer API
|
|
164
|
+
export async function queryRemoteTreeState(): Promise<{ root: string, nextIndex: number }> {
|
|
165
|
+
try {
|
|
166
|
+
logger.debug('Fetching Merkle root and nextIndex from API...');
|
|
167
|
+
const response = await fetch(`${INDEXER_API_URL}/merkle/root`);
|
|
168
|
+
if (!response.ok) {
|
|
169
|
+
throw new Error(`Failed to fetch Merkle root and nextIndex: ${response.status} ${response.statusText}`);
|
|
170
|
+
}
|
|
171
|
+
const data = await response.json() as { root: string, nextIndex: number };
|
|
172
|
+
logger.debug(`Fetched root from API: ${data.root}`);
|
|
173
|
+
logger.debug(`Fetched nextIndex from API: ${data.nextIndex}`);
|
|
174
|
+
return data;
|
|
175
|
+
} catch (error) {
|
|
176
|
+
console.error('Failed to fetch root and nextIndex from API:', error);
|
|
177
|
+
throw error;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function getProgramAccounts() {
|
|
182
|
+
// Derive PDA (Program Derived Addresses) for the tree account and other required accounts
|
|
183
|
+
const [treeAccount] = PublicKey.findProgramAddressSync(
|
|
184
|
+
[Buffer.from('merkle_tree')],
|
|
185
|
+
PROGRAM_ID
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
const [treeTokenAccount] = PublicKey.findProgramAddressSync(
|
|
189
|
+
[Buffer.from('tree_token')],
|
|
190
|
+
PROGRAM_ID
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
const [globalConfigAccount] = PublicKey.findProgramAddressSync(
|
|
194
|
+
[Buffer.from('global_config')],
|
|
195
|
+
PROGRAM_ID
|
|
196
|
+
);
|
|
197
|
+
return { treeAccount, treeTokenAccount, globalConfigAccount }
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
export function findCrossCheckNullifierPDAs(proof: any) {
|
|
202
|
+
const [nullifier2PDA] = PublicKey.findProgramAddressSync(
|
|
203
|
+
[Buffer.from("nullifier0"), Buffer.from(proof.inputNullifiers[1])],
|
|
204
|
+
PROGRAM_ID
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
const [nullifier3PDA] = PublicKey.findProgramAddressSync(
|
|
208
|
+
[Buffer.from("nullifier1"), Buffer.from(proof.inputNullifiers[0])],
|
|
209
|
+
PROGRAM_ID
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
return { nullifier2PDA, nullifier3PDA };
|
|
213
|
+
}
|