bstest001 0.0.2 → 0.0.4
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/utils/prover.d.ts +1 -1
- package/dist/utils/prover.js +18 -13
- package/package.json +1 -1
- package/src/utils/prover.ts +14 -10
package/dist/utils/prover.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Generates a Zero-Knowledge proof for a transaction.
|
|
3
|
-
* Supports both Node.js (
|
|
3
|
+
* Supports both Node.js (optimized CLI) and Web Browser (via snarkjs library).
|
|
4
4
|
*/
|
|
5
5
|
export declare function prove(input: any, keyBasePath: string): Promise<{
|
|
6
6
|
pA: string[];
|
package/dist/utils/prover.js
CHANGED
|
@@ -3,22 +3,22 @@ import { utils } from 'ffjavascript';
|
|
|
3
3
|
import { toFixedHex } from './utils.js';
|
|
4
4
|
/**
|
|
5
5
|
* Generates a Zero-Knowledge proof for a transaction.
|
|
6
|
-
* Supports both Node.js (
|
|
6
|
+
* Supports both Node.js (optimized CLI) and Web Browser (via snarkjs library).
|
|
7
7
|
*/
|
|
8
8
|
export async function prove(input, keyBasePath) {
|
|
9
|
-
//
|
|
10
|
-
const
|
|
11
|
-
if (
|
|
12
|
-
return await
|
|
9
|
+
// Check if running in browser (most reliable method for Next.js compatibility)
|
|
10
|
+
const isBrowser = typeof window !== 'undefined';
|
|
11
|
+
if (isBrowser) {
|
|
12
|
+
return await proveBrowser(input, keyBasePath);
|
|
13
13
|
}
|
|
14
14
|
else {
|
|
15
|
-
return await
|
|
15
|
+
return await proveNode(input, keyBasePath);
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
async function proveBrowser(input, keyBasePath) {
|
|
19
19
|
// @ts-ignore
|
|
20
20
|
const snarkjs = await import('snarkjs');
|
|
21
|
-
//
|
|
21
|
+
// Browser-based snarkjs proof generation
|
|
22
22
|
const { proof } = await snarkjs.groth16.fullProve(utils.stringifyBigInts(input), `${keyBasePath}.wasm`, `${keyBasePath}.zkey`);
|
|
23
23
|
const pA = [toFixedHex(proof.pi_a[0]), toFixedHex(proof.pi_a[1])];
|
|
24
24
|
const pB = [
|
|
@@ -29,11 +29,16 @@ async function proveBrowser(input, keyBasePath) {
|
|
|
29
29
|
return { pA, pB, pC };
|
|
30
30
|
}
|
|
31
31
|
async function proveNode(input, keyBasePath) {
|
|
32
|
-
|
|
33
|
-
const {
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
32
|
+
// @ts-ignore webpackIgnore: true prevents bundler from trying to include Node.js modules in browser
|
|
33
|
+
const { execFile } = await import(/* webpackIgnore: true */ 'child_process');
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
const { promises: fs } = await import(/* webpackIgnore: true */ 'fs');
|
|
36
|
+
// @ts-ignore
|
|
37
|
+
const os = (await import(/* webpackIgnore: true */ 'os')).default;
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
const path = (await import(/* webpackIgnore: true */ 'path')).default;
|
|
40
|
+
// @ts-ignore
|
|
41
|
+
const { promisify } = await import(/* webpackIgnore: true */ 'util');
|
|
37
42
|
const execFileAsync = promisify(execFile);
|
|
38
43
|
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'privacycash-proof-'));
|
|
39
44
|
const inputPath = path.join(tmpDir, 'input.json');
|
|
@@ -57,7 +62,7 @@ async function proveNode(input, keyBasePath) {
|
|
|
57
62
|
await fs.rm(tmpDir, { recursive: true, force: true });
|
|
58
63
|
}
|
|
59
64
|
catch (e) {
|
|
60
|
-
// ignore
|
|
65
|
+
// ignore cleanup errors
|
|
61
66
|
}
|
|
62
67
|
}
|
|
63
68
|
}
|
package/package.json
CHANGED
package/src/utils/prover.ts
CHANGED
|
@@ -7,13 +7,13 @@ import { toFixedHex } from './utils.js';
|
|
|
7
7
|
* Supports both Node.js (optimized CLI) and Web Browser (via snarkjs library).
|
|
8
8
|
*/
|
|
9
9
|
export async function prove(input: any, keyBasePath: string) {
|
|
10
|
-
// Check if running in
|
|
11
|
-
const
|
|
10
|
+
// Check if running in browser (most reliable method for Next.js compatibility)
|
|
11
|
+
const isBrowser = typeof window !== 'undefined';
|
|
12
12
|
|
|
13
|
-
if (
|
|
14
|
-
return await proveNode(input, keyBasePath);
|
|
15
|
-
} else {
|
|
13
|
+
if (isBrowser) {
|
|
16
14
|
return await proveBrowser(input, keyBasePath);
|
|
15
|
+
} else {
|
|
16
|
+
return await proveNode(input, keyBasePath);
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
|
|
@@ -39,12 +39,16 @@ async function proveBrowser(input: any, keyBasePath: string) {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
async function proveNode(input: any, keyBasePath: string) {
|
|
42
|
-
// @ts-ignore webpackIgnore: true prevents bundler from trying to include
|
|
42
|
+
// @ts-ignore webpackIgnore: true prevents bundler from trying to include Node.js modules in browser
|
|
43
43
|
const { execFile } = await import(/* webpackIgnore: true */ 'child_process');
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
const
|
|
44
|
+
// @ts-ignore
|
|
45
|
+
const { promises: fs } = await import(/* webpackIgnore: true */ 'fs');
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
const os = (await import(/* webpackIgnore: true */ 'os')).default;
|
|
48
|
+
// @ts-ignore
|
|
49
|
+
const path = (await import(/* webpackIgnore: true */ 'path')).default;
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
const { promisify } = await import(/* webpackIgnore: true */ 'util');
|
|
48
52
|
const execFileAsync = promisify(execFile);
|
|
49
53
|
|
|
50
54
|
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'privacycash-proof-'));
|