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.
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Generates a Zero-Knowledge proof for a transaction.
3
- * Supports both Node.js (via snarkjs CLI) and Web Browser (via snarkjs library).
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[];
@@ -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 (via snarkjs CLI) and Web Browser (via snarkjs library).
6
+ * Supports both Node.js (optimized CLI) and Web Browser (via snarkjs library).
7
7
  */
8
8
  export async function prove(input, keyBasePath) {
9
- // 1. Check environment
10
- const isNode = typeof process !== 'undefined' && process.versions && (!!process.versions.node || !!process.versions.bun);
11
- if (isNode) {
12
- return await proveNode(input, keyBasePath);
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 proveBrowser(input, keyBasePath);
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
- // In the browser, we use snarkjs.groth16.fullProve
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
- const { execFile } = await import('child_process');
33
- const { promises: fs } = await import('fs');
34
- const os = (await import('os')).default;
35
- const path = (await import('path')).default;
36
- const { promisify } = await import('util');
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bstest001",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "exports": {
@@ -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 Node.js environment
11
- const isNode = typeof process !== 'undefined' && process.versions && (!!process.versions.node || !!process.versions.bun);
10
+ // Check if running in browser (most reliable method for Next.js compatibility)
11
+ const isBrowser = typeof window !== 'undefined';
12
12
 
13
- if (isNode) {
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 child_process in browser
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
- const { promises: fs } = await import('fs');
45
- const os = (await import('os')).default;
46
- const path = (await import('path')).default;
47
- const { promisify } = await import('util');
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-'));