@semaphore-protocol/proof 4.0.0-alpha.7 → 4.0.0-alpha.8

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,13 +1,15 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/proof
3
- * @version 4.0.0-alpha.7
3
+ * @version 4.0.0-alpha.8
4
4
  * @file A library to generate and verify Semaphore proofs.
5
5
  * @copyright Ethereum Foundation 2024
6
6
  * @license MIT
7
7
  * @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/proof}
8
8
  */
9
9
  import { groth16 } from 'snarkjs';
10
- import { keccak256, toBeHex, toBigInt as toBigInt$1, encodeBytes32String } from 'ethers';
10
+ import { keccak256 } from 'ethers/crypto';
11
+ import { toBeHex, toBigInt as toBigInt$1 } from 'ethers/utils';
12
+ import { encodeBytes32String } from 'ethers/abi';
11
13
 
12
14
  /******************************************************************************
13
15
  Copyright (c) Microsoft Corporation.
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/proof
3
- * @version 4.0.0-alpha.7
3
+ * @version 4.0.0-alpha.8
4
4
  * @file A library to generate and verify Semaphore proofs.
5
5
  * @copyright Ethereum Foundation 2024
6
6
  * @license MIT
@@ -9,10 +9,15 @@
9
9
  'use strict';
10
10
 
11
11
  var snarkjs = require('snarkjs');
12
- var download = require('download');
13
- var fs = require('fs');
14
- var tmp = require('tmp');
15
- var ethers = require('ethers');
12
+ var node_fs = require('node:fs');
13
+ var promises$1 = require('node:fs/promises');
14
+ var os = require('node:os');
15
+ var node_path = require('node:path');
16
+ var node_stream = require('node:stream');
17
+ var promises = require('node:stream/promises');
18
+ var crypto = require('ethers/crypto');
19
+ var utils = require('ethers/utils');
20
+ var abi = require('ethers/abi');
16
21
 
17
22
  /******************************************************************************
18
23
  Copyright (c) Microsoft Corporation.
@@ -85,6 +90,30 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
85
90
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
86
91
  };
87
92
 
93
+ function download(url, outputPath) {
94
+ return __awaiter(this, void 0, void 0, function () {
95
+ var response, dir, fileStream;
96
+ return __generator(this, function (_a) {
97
+ switch (_a.label) {
98
+ case 0: return [4 /*yield*/, fetch(url)];
99
+ case 1:
100
+ response = _a.sent();
101
+ if (!response.ok) {
102
+ throw new Error("Failed to fetch ".concat(url, ": ").concat(response.statusText));
103
+ }
104
+ dir = node_path.dirname(outputPath);
105
+ return [4 /*yield*/, promises$1.mkdir(dir, { recursive: true })];
106
+ case 2:
107
+ _a.sent();
108
+ fileStream = node_fs.createWriteStream(outputPath);
109
+ return [4 /*yield*/, promises.finished(node_stream.Readable.fromWeb(response.body).pipe(fileStream))];
110
+ case 3:
111
+ _a.sent();
112
+ return [2 /*return*/];
113
+ }
114
+ });
115
+ });
116
+ }
88
117
  function getSnarkArtifacts(treeDepth) {
89
118
  return __awaiter(this, void 0, void 0, function () {
90
119
  var tmpDir, tmpPath;
@@ -92,12 +121,12 @@ function getSnarkArtifacts(treeDepth) {
92
121
  switch (_a.label) {
93
122
  case 0:
94
123
  tmpDir = "semaphore-proof";
95
- tmpPath = "".concat(tmp.tmpdir, "/").concat(tmpDir, "-").concat(treeDepth);
96
- if (!(!fs.existsSync(tmpPath) || fs.readdirSync(tmpPath).length !== 2)) return [3 /*break*/, 3];
97
- return [4 /*yield*/, download("https://semaphore.cedoor.dev/artifacts/".concat(treeDepth, "/semaphore.wasm"), tmpPath)];
124
+ tmpPath = "".concat(os.tmpdir(), "/").concat(tmpDir, "-").concat(treeDepth);
125
+ if (!(!node_fs.existsSync(tmpPath) || node_fs.readdirSync(tmpPath).length !== 2)) return [3 /*break*/, 3];
126
+ return [4 /*yield*/, download("https://semaphore.cedoor.dev/artifacts/".concat(treeDepth, "/semaphore.wasm"), "".concat(tmpPath, "/semaphore.wasm"))];
98
127
  case 1:
99
128
  _a.sent();
100
- return [4 /*yield*/, download("https://semaphore.cedoor.dev/artifacts/".concat(treeDepth, "/semaphore.zkey"), tmpPath)];
129
+ return [4 /*yield*/, download("https://semaphore.cedoor.dev/artifacts/".concat(treeDepth, "/semaphore.zkey"), "".concat(tmpPath, "/semaphore.zkey"))];
101
130
  case 2:
102
131
  _a.sent();
103
132
  _a.label = 3;
@@ -116,7 +145,7 @@ function getSnarkArtifacts(treeDepth) {
116
145
  * @returns The message digest.
117
146
  */
118
147
  function hash(message) {
119
- return (BigInt(ethers.keccak256(ethers.toBeHex(message, 32))) >> BigInt(8)).toString();
148
+ return (BigInt(crypto.keccak256(utils.toBeHex(message, 32))) >> BigInt(8)).toString();
120
149
  }
121
150
 
122
151
  /**
@@ -139,11 +168,11 @@ function packPoints(proof) {
139
168
 
140
169
  function toBigInt(value) {
141
170
  try {
142
- return ethers.toBigInt(value);
171
+ return utils.toBigInt(value);
143
172
  }
144
173
  catch (error) {
145
174
  if (typeof value === "string") {
146
- return ethers.toBigInt(ethers.encodeBytes32String(value));
175
+ return utils.toBigInt(abi.encodeBytes32String(value));
147
176
  }
148
177
  throw TypeError(error.message);
149
178
  }
@@ -1,16 +1,21 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/proof
3
- * @version 4.0.0-alpha.7
3
+ * @version 4.0.0-alpha.8
4
4
  * @file A library to generate and verify Semaphore proofs.
5
5
  * @copyright Ethereum Foundation 2024
6
6
  * @license MIT
7
7
  * @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/proof}
8
8
  */
9
9
  import { groth16 } from 'snarkjs';
10
- import download from 'download';
11
- import fs from 'fs';
12
- import tmp from 'tmp';
13
- import { keccak256, toBeHex, toBigInt as toBigInt$1, encodeBytes32String } from 'ethers';
10
+ import { existsSync, readdirSync, createWriteStream } from 'node:fs';
11
+ import { mkdir } from 'node:fs/promises';
12
+ import os from 'node:os';
13
+ import { dirname } from 'node:path';
14
+ import { Readable } from 'node:stream';
15
+ import { finished } from 'node:stream/promises';
16
+ import { keccak256 } from 'ethers/crypto';
17
+ import { toBeHex, toBigInt as toBigInt$1 } from 'ethers/utils';
18
+ import { encodeBytes32String } from 'ethers/abi';
14
19
 
15
20
  /******************************************************************************
16
21
  Copyright (c) Microsoft Corporation.
@@ -83,6 +88,30 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
83
88
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
84
89
  };
85
90
 
91
+ function download(url, outputPath) {
92
+ return __awaiter(this, void 0, void 0, function () {
93
+ var response, dir, fileStream;
94
+ return __generator(this, function (_a) {
95
+ switch (_a.label) {
96
+ case 0: return [4 /*yield*/, fetch(url)];
97
+ case 1:
98
+ response = _a.sent();
99
+ if (!response.ok) {
100
+ throw new Error("Failed to fetch ".concat(url, ": ").concat(response.statusText));
101
+ }
102
+ dir = dirname(outputPath);
103
+ return [4 /*yield*/, mkdir(dir, { recursive: true })];
104
+ case 2:
105
+ _a.sent();
106
+ fileStream = createWriteStream(outputPath);
107
+ return [4 /*yield*/, finished(Readable.fromWeb(response.body).pipe(fileStream))];
108
+ case 3:
109
+ _a.sent();
110
+ return [2 /*return*/];
111
+ }
112
+ });
113
+ });
114
+ }
86
115
  function getSnarkArtifacts(treeDepth) {
87
116
  return __awaiter(this, void 0, void 0, function () {
88
117
  var tmpDir, tmpPath;
@@ -90,12 +119,12 @@ function getSnarkArtifacts(treeDepth) {
90
119
  switch (_a.label) {
91
120
  case 0:
92
121
  tmpDir = "semaphore-proof";
93
- tmpPath = "".concat(tmp.tmpdir, "/").concat(tmpDir, "-").concat(treeDepth);
94
- if (!(!fs.existsSync(tmpPath) || fs.readdirSync(tmpPath).length !== 2)) return [3 /*break*/, 3];
95
- return [4 /*yield*/, download("https://semaphore.cedoor.dev/artifacts/".concat(treeDepth, "/semaphore.wasm"), tmpPath)];
122
+ tmpPath = "".concat(os.tmpdir(), "/").concat(tmpDir, "-").concat(treeDepth);
123
+ if (!(!existsSync(tmpPath) || readdirSync(tmpPath).length !== 2)) return [3 /*break*/, 3];
124
+ return [4 /*yield*/, download("https://semaphore.cedoor.dev/artifacts/".concat(treeDepth, "/semaphore.wasm"), "".concat(tmpPath, "/semaphore.wasm"))];
96
125
  case 1:
97
126
  _a.sent();
98
- return [4 /*yield*/, download("https://semaphore.cedoor.dev/artifacts/".concat(treeDepth, "/semaphore.zkey"), tmpPath)];
127
+ return [4 /*yield*/, download("https://semaphore.cedoor.dev/artifacts/".concat(treeDepth, "/semaphore.zkey"), "".concat(tmpPath, "/semaphore.zkey"))];
99
128
  case 2:
100
129
  _a.sent();
101
130
  _a.label = 3;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@semaphore-protocol/proof",
3
- "version": "4.0.0-alpha.7",
3
+ "version": "4.0.0-alpha.8",
4
4
  "description": "A library to generate and verify Semaphore proofs.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -38,7 +38,6 @@
38
38
  "access": "public"
39
39
  },
40
40
  "devDependencies": {
41
- "@ethersproject/strings": "^5.7.0",
42
41
  "@rollup/plugin-alias": "^5.1.0",
43
42
  "@rollup/plugin-json": "^6.1.0",
44
43
  "@types/download": "^8.0.5",
@@ -50,14 +49,12 @@
50
49
  "rollup-plugin-typescript2": "^0.36.0"
51
50
  },
52
51
  "peerDependencies": {
53
- "@semaphore-protocol/group": "4.0.0-alpha.7",
54
- "@semaphore-protocol/identity": "4.0.0-alpha.7"
52
+ "@semaphore-protocol/group": "4.0.0-alpha.8",
53
+ "@semaphore-protocol/identity": "4.0.0-alpha.8"
55
54
  },
56
55
  "dependencies": {
57
56
  "@types/snarkjs": "0.7.8",
58
- "download": "8.0.0",
59
57
  "ethers": "6.10.0",
60
- "snarkjs": "0.7.3",
61
- "tmp": "0.2.1"
58
+ "snarkjs": "0.7.3"
62
59
  }
63
60
  }
@@ -48,7 +48,6 @@ export default async function generateProof(
48
48
  }
49
49
 
50
50
  // If the Snark artifacts are not defined they will be automatically downloaded.
51
- /* istanbul ignore next */
52
51
  if (!snarkArtifacts) {
53
52
  snarkArtifacts = await getSnarkArtifacts(merkleTreeDepth)
54
53
  }
@@ -1,16 +1,40 @@
1
1
  /* istanbul ignore file */
2
- import download from "download"
3
- import fs from "fs"
4
- import tmp from "tmp"
2
+ import { createWriteStream, existsSync, readdirSync } from "node:fs"
3
+ import { mkdir } from "node:fs/promises"
4
+ import os from "node:os"
5
+ import { dirname } from "node:path"
6
+ import { Readable } from "node:stream"
7
+ import { finished } from "node:stream/promises"
5
8
  import { SnarkArtifacts } from "./types"
6
9
 
10
+ async function download(url: string, outputPath: string) {
11
+ const response = await fetch(url)
12
+
13
+ if (!response.ok) {
14
+ throw new Error(`Failed to fetch ${url}: ${response.statusText}`)
15
+ }
16
+
17
+ // Ensure the directory exists.
18
+ const dir = dirname(outputPath)
19
+ await mkdir(dir, { recursive: true })
20
+
21
+ const fileStream = createWriteStream(outputPath)
22
+ await finished(Readable.fromWeb(response.body as any).pipe(fileStream))
23
+ }
24
+
7
25
  export default async function getSnarkArtifacts(treeDepth: number): Promise<SnarkArtifacts> {
8
26
  const tmpDir = "semaphore-proof"
9
- const tmpPath = `${tmp.tmpdir}/${tmpDir}-${treeDepth}`
27
+ const tmpPath = `${os.tmpdir()}/${tmpDir}-${treeDepth}`
10
28
 
11
- if (!fs.existsSync(tmpPath) || fs.readdirSync(tmpPath).length !== 2) {
12
- await download(`https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.wasm`, tmpPath)
13
- await download(`https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.zkey`, tmpPath)
29
+ if (!existsSync(tmpPath) || readdirSync(tmpPath).length !== 2) {
30
+ await download(
31
+ `https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.wasm`,
32
+ `${tmpPath}/semaphore.wasm`
33
+ )
34
+ await download(
35
+ `https://semaphore.cedoor.dev/artifacts/${treeDepth}/semaphore.zkey`,
36
+ `${tmpPath}/semaphore.zkey`
37
+ )
14
38
  }
15
39
 
16
40
  return {
package/src/hash.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { keccak256, toBeHex } from "ethers"
1
+ import { keccak256 } from "ethers/crypto"
2
+ import { toBeHex } from "ethers/utils"
2
3
  import { NumericString } from "snarkjs"
3
4
  import { BigNumberish } from "./types"
4
5
 
package/src/to-bigint.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { encodeBytes32String, toBigInt as _toBigInt } from "ethers"
1
+ import { encodeBytes32String } from "ethers/abi"
2
+ import { toBigInt as _toBigInt } from "ethers/utils"
2
3
  import { BigNumberish } from "./types"
3
4
 
4
5
  export default function toBigInt(value: BigNumberish | Uint8Array | string): bigint {