Skip to content

Commit f09daf8

Browse files
committed
build(identity): config rollup to handle browser bundle
re #481 Former-commit-id: 12cc55c
1 parent 4c9f03f commit f09daf8

20 files changed

+114
-34
lines changed

packages/cli/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
},
2525
"scripts": {
2626
"start": "node --loader ts-node/esm --no-warnings src/index.ts",
27-
"build:watch": "rollup -c rollup.config.ts -w --configPlugin typescript",
2827
"build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
2928
"prepublishOnly": "yarn build"
3029
},

packages/cli/rollup.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const banner = `#!/usr/bin/env node
1818
export default {
1919
input: "src/index.ts",
2020
output: [{ file: pkg.bin.semaphore, format: "es", banner }],
21-
external: ["url", "fs", "path", ...Object.keys(pkg.dependencies)],
21+
external: [...Object.keys(pkg.dependencies), "url", "fs", "path", "child_process"],
2222
plugins: [
2323
(typescript as any)({
2424
tsconfig: "./build.tsconfig.json",

packages/data/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"url": "https://github.com/semaphore-protocol/semaphore.git/issues"
2323
},
2424
"scripts": {
25-
"build:watch": "rollup -c rollup.config.ts -w --configPlugin typescript",
2625
"build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
2726
"prepublishOnly": "yarn build"
2827
},
@@ -35,7 +34,7 @@
3534
"rollup-plugin-typescript2": "^0.31.2"
3635
},
3736
"dependencies": {
38-
"axios": "^0.27.2",
37+
"axios": "1.6.6",
3938
"ethers": "6.10.0"
4039
}
4140
}

packages/group/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"url": "https://github.com/semaphore-protocol/semaphore.git/issues"
2323
},
2424
"scripts": {
25-
"build:watch": "rollup -c rollup.config.ts -w --configPlugin typescript",
2625
"build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
2726
"prepublishOnly": "yarn build"
2827
},

packages/hardhat/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"url": "https://github.com/semaphore-protocol/semaphore.git/issues"
2323
},
2424
"scripts": {
25-
"build:watch": "rollup -c rollup.config.ts -w --configPlugin typescript",
2625
"build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
2726
"prepublishOnly": "yarn build"
2827
},

packages/hardhat/rollup.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default {
1818
{ file: pkg.exports.require, format: "cjs", banner, exports: "auto" },
1919
{ file: pkg.exports.import, format: "es", banner }
2020
],
21-
external: Object.keys(pkg.dependencies),
21+
external: [...Object.keys(pkg.dependencies), "hardhat/config"],
2222
plugins: [
2323
typescript({
2424
tsconfig: "./build.tsconfig.json",

packages/heyauthn/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"url": "https://github.com/semaphore-protocol/semaphore.git/issues"
2323
},
2424
"scripts": {
25-
"build:watch": "rollup -c rollup.config.ts -w --configPlugin typescript",
2625
"build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
2726
"prepublishOnly": "yarn build"
2827
},

packages/identity/package.json

+14-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@
44
"description": "A library to create Semaphore identities.",
55
"license": "MIT",
66
"main": "dist/index.node.js",
7+
"browser": "dist/index.browser.mjs",
78
"exports": {
8-
"import": "./dist/index.mjs",
9-
"require": "./dist/index.node.js",
10-
"types": "./dist/types/index.d.ts"
9+
".": {
10+
"node": {
11+
"import": "./dist/index.node.mjs",
12+
"require": "./dist/index.node.js"
13+
},
14+
"browser": "./dist/index.browser.mjs",
15+
"default": "./dist/index.browser.mjs",
16+
"types": "./dist/types/index.d.ts"
17+
}
1118
},
1219
"types": "dist/types/index.d.ts",
1320
"files": [
@@ -22,14 +29,16 @@
2229
"url": "https://github.com/semaphore-protocol/semaphore.git/issues"
2330
},
2431
"scripts": {
25-
"build:watch": "rollup -c rollup.config.ts -w --configPlugin typescript",
26-
"build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
32+
"build": "rimraf dist && yarn build:browser && yarn build:node",
33+
"build:browser": "rollup -c rollup.browser.config.ts --configPlugin typescript",
34+
"build:node": "rollup -c rollup.node.config.ts --configPlugin typescript",
2735
"prepublishOnly": "yarn build"
2836
},
2937
"publishConfig": {
3038
"access": "public"
3139
},
3240
"devDependencies": {
41+
"@rollup/plugin-alias": "^5.1.0",
3342
"@rollup/plugin-commonjs": "^24.0.1",
3443
"@rollup/plugin-node-resolve": "^15.0.1",
3544
"rollup-plugin-cleanup": "^3.2.1",
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import alias from "@rollup/plugin-alias"
2+
import json from "@rollup/plugin-json"
3+
import * as fs from "fs"
4+
import cleanup from "rollup-plugin-cleanup"
5+
import typescript from "rollup-plugin-typescript2"
6+
7+
const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8"))
8+
const banner = `/**
9+
* @module ${pkg.name}
10+
* @version ${pkg.version}
11+
* @file ${pkg.description}
12+
* @copyright Ethereum Foundation 2024
13+
* @license ${pkg.license}
14+
* @see [Github]{@link ${pkg.homepage}}
15+
*/`
16+
17+
export default {
18+
input: "src/index.ts",
19+
output: [
20+
{
21+
file: pkg.exports.browser,
22+
format: "es",
23+
banner
24+
}
25+
],
26+
external: [...Object.keys(pkg.dependencies), "poseidon-lite/poseidon2"],
27+
plugins: [
28+
alias({
29+
entries: [{ find: "./random-number.node", replacement: "./random-number.browser" }]
30+
}),
31+
typescript({
32+
tsconfig: "./build.tsconfig.json",
33+
useTsconfigDeclarationDir: true
34+
}),
35+
cleanup({ comments: "jsdoc" }),
36+
json()
37+
]
38+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import typescript from "rollup-plugin-typescript2"
2-
import commonjs from "@rollup/plugin-commonjs"
31
import * as fs from "fs"
42
import cleanup from "rollup-plugin-cleanup"
5-
import { nodeResolve } from "@rollup/plugin-node-resolve"
3+
import typescript from "rollup-plugin-typescript2"
64

75
const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8"))
86
const banner = `/**
@@ -17,17 +15,15 @@ const banner = `/**
1715
export default {
1816
input: "src/index.ts",
1917
output: [
20-
{ file: pkg.exports.require, format: "cjs", banner, exports: "auto" },
21-
{ file: pkg.exports.import, format: "es", banner }
18+
{ file: pkg.exports.node.require, format: "cjs", banner, exports: "auto" },
19+
{ file: pkg.exports.node.import, format: "es", banner }
2220
],
23-
external: Object.keys(pkg.dependencies),
21+
external: [...Object.keys(pkg.dependencies), "poseidon-lite/poseidon2", "node:crypto"],
2422
plugins: [
2523
typescript({
2624
tsconfig: "./build.tsconfig.json",
2725
useTsconfigDeclarationDir: true
2826
}),
29-
commonjs(),
30-
nodeResolve(),
3127
cleanup({ comments: "jsdoc" })
3228
]
3329
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function bytesToBigint(bytes: Uint8Array): bigint {
2+
let hex = "0x"
3+
4+
for (let i = 0; i < bytes.length; i += 1) {
5+
hex += bytes[i].toString(16).padStart(2, "0")
6+
}
7+
8+
return BigInt(hex)
9+
}

packages/identity/src/identity.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
signMessage,
88
verifySignature
99
} from "@zk-kit/eddsa-poseidon"
10-
import { randomBytes } from "crypto"
1110
import { poseidon2 } from "poseidon-lite/poseidon2"
11+
import { randomNumber } from "./random-number.node"
1212

1313
export default class Identity {
1414
private _privateKey: BigNumberish
@@ -20,7 +20,7 @@ export default class Identity {
2020
* Initializes the class attributes based on the parameters.
2121
* @param privateKey The secret value used to generate an EdDSA public key.
2222
*/
23-
constructor(privateKey: BigNumberish = BigInt(`0x${randomBytes(32).toString("hex")}`).toString()) {
23+
constructor(privateKey: BigNumberish = randomNumber().toString()) {
2424
this._privateKey = privateKey
2525
this._secretScalar = deriveSecretScalar(privateKey)
2626

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* istanbul ignore file */
2+
import { bytesToBigint } from "./bytes-to-bigint"
3+
4+
export function randomNumber(): bigint {
5+
const bytes = crypto.getRandomValues(new Uint8Array(32))
6+
7+
return bytesToBigint(bytes)
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { randomBytes } from "node:crypto"
2+
3+
export function randomNumber(): bigint {
4+
const bytes = randomBytes(32)
5+
6+
const hex = `0x${bytes.toString("hex")}`
7+
8+
return BigInt(hex)
9+
}

packages/identity/tests/index.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { randomBytes } from "crypto"
12
import { Identity } from "../src"
3+
import { bytesToBigint } from "../src/bytes-to-bigint"
24

35
describe("Identity", () => {
46
const privateKey = "secret"
@@ -60,4 +62,15 @@ describe("Identity", () => {
6062
expect(Identity.verifySignature("message", signature, identity.publicKey)).toBeTruthy()
6163
})
6264
})
65+
66+
describe("# bytesToBigint", () => {
67+
it("Should convert 32 bytes to bigint", () => {
68+
const bytes = randomBytes(32)
69+
70+
const integer = bytesToBigint(bytes)
71+
72+
expect(typeof integer).toBe("bigint")
73+
expect(integer).toBe(BigInt(`0x${bytes.toString("hex")}`))
74+
})
75+
})
6376
})

packages/identity/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "../../tsconfig.json",
3-
"include": ["src", "tests", "rollup.config.ts"]
3+
"include": ["src", "tests", "rollup.node.config.ts", "rollup.browser.config.ts"]
44
}

packages/proof/package.json

+10-7
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
"description": "A library to generate and verify Semaphore proofs.",
55
"license": "MIT",
66
"main": "dist/index.node.js",
7+
"browser": "dist/index.browser.mjs",
78
"exports": {
8-
"node": {
9-
"import": "./dist/index.node.mjs",
10-
"require": "./dist/index.node.js"
11-
},
12-
"browser": "./dist/index.browser.mjs",
13-
"default": "./dist/index.browser.mjs",
14-
"types": "./dist/types/index.d.ts"
9+
".": {
10+
"node": {
11+
"import": "./dist/index.node.mjs",
12+
"require": "./dist/index.node.js"
13+
},
14+
"browser": "./dist/index.browser.mjs",
15+
"default": "./dist/index.browser.mjs",
16+
"types": "./dist/types/index.d.ts"
17+
}
1518
},
1619
"types": "dist/types/index.d.ts",
1720
"files": [

packages/proof/rollup.node.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default {
2828
banner
2929
}
3030
],
31-
external: Object.keys(pkg.dependencies),
31+
external: [...Object.keys(pkg.dependencies), "fs"],
3232
plugins: [
3333
typescript({
3434
tsconfig: "./build.tsconfig.json",

packages/proof/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "../../tsconfig.json",
3-
"include": ["src", "tests", "rollup.config.ts"]
3+
"include": ["src", "tests", "rollup.node.config.ts", "rollup.browser.config.ts"]
44
}

yarn.lock.REMOVED.git-id

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
d3dec95f38eaf98de105a46c8b2a023337c9360e
1+
a1f836136a63bf31ff94a3476fe812378036cc2a

0 commit comments

Comments
 (0)