Skip to content

Commit 4ca3b26

Browse files
committed
refactor(proof): create function to convert to bigint
1 parent a1dc768 commit 4ca3b26

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

packages/proof/src/generate-proof.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { Group, MerkleProof } from "@semaphore-protocol/group"
22
import type { Identity } from "@semaphore-protocol/identity"
3-
import { encodeBytes32String, toBigInt } from "ethers"
43
import { NumericString, groth16 } from "snarkjs"
54
import getSnarkArtifacts from "./get-snark-artifacts.node"
65
import hash from "./hash"
76
import packPoints from "./pack-points"
87
import { BigNumberish, SemaphoreProof, SnarkArtifacts } from "./types"
8+
import toBigInt from "./to-bigint"
99

1010
/**
1111
* Generates a Semaphore proof.
@@ -25,25 +25,8 @@ export default async function generateProof(
2525
merkleTreeDepth?: number,
2626
snarkArtifacts?: SnarkArtifacts
2727
): Promise<SemaphoreProof> {
28-
try {
29-
message = toBigInt(message)
30-
} catch (error: any) {
31-
if (typeof message === "string") {
32-
message = encodeBytes32String(message)
33-
} else {
34-
throw TypeError(error.message)
35-
}
36-
}
37-
38-
try {
39-
scope = toBigInt(scope)
40-
} catch (error: any) {
41-
if (typeof scope === "string") {
42-
scope = encodeBytes32String(scope)
43-
} else {
44-
throw TypeError(error.message)
45-
}
46-
}
28+
message = toBigInt(message)
29+
scope = toBigInt(scope)
4730

4831
let merkleProof
4932

packages/proof/src/to-bigint.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { encodeBytes32String, toBigInt as _toBigInt } from "ethers"
2+
import { BigNumberish } from "./types"
3+
4+
export default function toBigInt(value: BigNumberish | Uint8Array | string): bigint {
5+
try {
6+
return _toBigInt(value)
7+
} catch (error: any) {
8+
if (typeof value === "string") {
9+
return _toBigInt(encodeBytes32String(value))
10+
}
11+
12+
throw TypeError(error.message)
13+
}
14+
}

0 commit comments

Comments
 (0)