diff --git a/packages/groth16/package.json b/packages/groth16/package.json index f4024d30c..d7b0cc814 100644 --- a/packages/groth16/package.json +++ b/packages/groth16/package.json @@ -25,7 +25,6 @@ "url": "https://github.com/privacy-scaling-explorations/zk-kit.git/issues" }, "scripts": { - "build:watch": "rollup -c rollup.config.ts -w --configPlugin typescript", "build": "rimraf dist && yarn build:browser && yarn build:node", "build:browser": "rollup -c rollup.browser.config.ts --configPlugin typescript", "build:node": "rollup -c rollup.node.config.ts --configPlugin typescript", diff --git a/packages/poseidon-proof/package.json b/packages/poseidon-proof/package.json index 387489572..433e140e6 100644 --- a/packages/poseidon-proof/package.json +++ b/packages/poseidon-proof/package.json @@ -1,13 +1,16 @@ { "name": "@zk-kit/poseidon-proof", - "version": "0.4.1", + "version": "0.5.0", "description": "A library to generate and verify Poseidon proofs.", "license": "MIT", - "main": "dist/index.js", + "main": "dist/index.node.js", "exports": { - "import": "./dist/index.mjs", - "require": "./dist/index.js", - "types": "./dist/types/index.d.ts" + "node": { + "import": "./dist/index.node.mjs", + "require": "./dist/index.node.js" + }, + "browser": "./dist/index.browser.mjs", + "default": "./dist/index.browser.mjs" }, "types": "dist/types/index.d.ts", "files": [ @@ -25,14 +28,16 @@ "url": "https://github.com/privacy-scaling-explorations/zk-kit.git/issues" }, "scripts": { - "build:watch": "rollup -c rollup.config.ts -w --configPlugin typescript", - "build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript", + "build": "rimraf dist && yarn build:browser && yarn build:node", + "build:browser": "rollup -c rollup.browser.config.ts --configPlugin typescript", + "build:node": "rollup -c rollup.node.config.ts --configPlugin typescript", "prepublishOnly": "yarn build" }, "publishConfig": { "access": "public" }, "devDependencies": { + "@rollup/plugin-alias": "^5.0.1", "@rollup/plugin-json": "^5.0.1", "@types/download": "^8.0.5", "@types/tmp": "^0.2.6", diff --git a/packages/poseidon-proof/rollup.browser.config.ts b/packages/poseidon-proof/rollup.browser.config.ts new file mode 100644 index 000000000..5d3ab649e --- /dev/null +++ b/packages/poseidon-proof/rollup.browser.config.ts @@ -0,0 +1,38 @@ +import alias from "@rollup/plugin-alias" +import json from "@rollup/plugin-json" +import * as fs from "fs" +import cleanup from "rollup-plugin-cleanup" +import typescript from "rollup-plugin-typescript2" + +const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8")) +const banner = `/** + * @module ${pkg.name} + * @version ${pkg.version} + * @file ${pkg.description} + * @copyright Ethereum Foundation 2023 + * @license ${pkg.license} + * @see [Github]{@link ${pkg.homepage}} +*/` + +export default { + input: "src/index.ts", + output: [ + { + file: pkg.exports.browser, + format: "es", + banner + } + ], + external: Object.keys(pkg.dependencies), + plugins: [ + alias({ + entries: [{ find: "./get-snark-artifacts.node", replacement: "./get-snark-artifacts.browser" }] + }), + typescript({ + tsconfig: "./build.tsconfig.json", + useTsconfigDeclarationDir: true + }), + cleanup({ comments: "jsdoc" }), + json() + ] +} diff --git a/packages/poseidon-proof/rollup.config.ts b/packages/poseidon-proof/rollup.node.config.ts similarity index 91% rename from packages/poseidon-proof/rollup.config.ts rename to packages/poseidon-proof/rollup.node.config.ts index 7cc6448f1..ee88555d2 100644 --- a/packages/poseidon-proof/rollup.config.ts +++ b/packages/poseidon-proof/rollup.node.config.ts @@ -17,13 +17,13 @@ export default { input: "src/index.ts", output: [ { - file: pkg.exports.require, + file: pkg.exports.node.require, format: "cjs", banner, exports: "auto" }, { - file: pkg.exports.import, + file: pkg.exports.node.import, format: "es", banner } diff --git a/packages/poseidon-proof/src/generate.ts b/packages/poseidon-proof/src/generate.ts index e26c6b225..935227611 100644 --- a/packages/poseidon-proof/src/generate.ts +++ b/packages/poseidon-proof/src/generate.ts @@ -1,12 +1,10 @@ import { BigNumber } from "@ethersproject/bignumber" import { BytesLike, Hexable } from "@ethersproject/bytes" import { NumericString, prove } from "@zk-kit/groth16" - -import { defaultSnarkArtifacts } from "./config" +import getSnarkArtifacts from "./get-snark-artifacts.node" import hash from "./hash" -import packProof from "./packProof" +import packProof from "./pack-proof" import { PoseidonProof, SnarkArtifacts } from "./types" -import { isBrowser, isNode } from "./utils" /** * Creates a zero-knowledge proof to prove that you have the pre-image of a hash, @@ -31,35 +29,7 @@ export default async function generate( // If the Snark artifacts are not defined they will be automatically downloaded. /* istanbul ignore next */ if (!snarkArtifacts) { - if (isNode()) { - const { default: download } = await import("download") - const { default: fs } = await import("fs") - const { default: tmp } = await import("tmp") - - const tmpDir = "poseidon-proof" - const tmpPath = `${tmp.tmpdir}/${tmpDir}` - - if (!fs.existsSync(tmpPath)) { - tmp.dirSync({ name: tmpDir }) - - await download(defaultSnarkArtifacts.wasmFilePath, tmpPath) - await download(defaultSnarkArtifacts.zkeyFilePath, tmpPath) - } - - snarkArtifacts = { - wasmFilePath: `${tmpPath}/poseidon-proof.wasm`, - zkeyFilePath: `${tmpPath}/poseidon-proof.zkey` - } - } - - if (isBrowser()) { - snarkArtifacts = defaultSnarkArtifacts - } - } - - /* istanbul ignore next */ - if (!snarkArtifacts) { - throw new Error("Error: Missing Snark artifacts. Please ensure all necessary Snark artifacts are included.") + snarkArtifacts = await getSnarkArtifacts() } const { proof, publicSignals } = await prove( diff --git a/packages/poseidon-proof/src/get-snark-artifacts.browser.ts b/packages/poseidon-proof/src/get-snark-artifacts.browser.ts new file mode 100644 index 000000000..3ecd2679e --- /dev/null +++ b/packages/poseidon-proof/src/get-snark-artifacts.browser.ts @@ -0,0 +1,6 @@ +/* istanbul ignore file */ +import { defaultSnarkArtifacts } from "./config" + +export default async function getSnarkArtifacts() { + return defaultSnarkArtifacts +} diff --git a/packages/poseidon-proof/src/get-snark-artifacts.node.ts b/packages/poseidon-proof/src/get-snark-artifacts.node.ts new file mode 100644 index 000000000..ba8db05ca --- /dev/null +++ b/packages/poseidon-proof/src/get-snark-artifacts.node.ts @@ -0,0 +1,22 @@ +/* istanbul ignore file */ +import download from "download" +import tmp from "tmp" +import fs from "fs" +import { defaultSnarkArtifacts } from "./config" + +export default async function getSnarkArtifacts() { + const tmpDir = "poseidon-proof" + const tmpPath = `${tmp.tmpdir}/${tmpDir}` + + if (!fs.existsSync(tmpPath)) { + tmp.dirSync({ name: tmpDir }) + + await download(defaultSnarkArtifacts.wasmFilePath, tmpPath) + await download(defaultSnarkArtifacts.zkeyFilePath, tmpPath) + } + + return { + wasmFilePath: `${tmpPath}/poseidon-proof.wasm`, + zkeyFilePath: `${tmpPath}/poseidon-proof.zkey` + } +} diff --git a/packages/poseidon-proof/src/packProof.ts b/packages/poseidon-proof/src/pack-proof.ts similarity index 100% rename from packages/poseidon-proof/src/packProof.ts rename to packages/poseidon-proof/src/pack-proof.ts diff --git a/packages/poseidon-proof/src/unpackProof.ts b/packages/poseidon-proof/src/unpack-proof.ts similarity index 100% rename from packages/poseidon-proof/src/unpackProof.ts rename to packages/poseidon-proof/src/unpack-proof.ts diff --git a/packages/poseidon-proof/src/utils.ts b/packages/poseidon-proof/src/utils.ts deleted file mode 100644 index 6fcd51b58..000000000 --- a/packages/poseidon-proof/src/utils.ts +++ /dev/null @@ -1,20 +0,0 @@ -/** - * Secure method to check if the environment is Node.js. - * @returns true if the environment is Node.js, false otherwise. - */ -export function isNode(): boolean { - // Checking the existence of 'window' variable, or 'process' is not enough, - // as those variables can be redefined by inner scopes (by any module/library). - // In the 'new Function()' constructor, the execution scope of 'this' is bound - // to the global scope and it can be compared to the expected value (the objects - // will have the same id if the environment is the expected one). - return new Function("try {return this===global}catch(e){ return false}")() -} - -/** - * Secure method to check if the environment is a browser. - * @returns true if the environment is a browser, false otherwise. - */ -export function isBrowser(): boolean { - return new Function("try {return this===window}catch(e){ return false}")() -} diff --git a/packages/poseidon-proof/src/verify.ts b/packages/poseidon-proof/src/verify.ts index 9ee0eb00e..5a6090e25 100644 --- a/packages/poseidon-proof/src/verify.ts +++ b/packages/poseidon-proof/src/verify.ts @@ -1,7 +1,7 @@ import { verify as _verify } from "@zk-kit/groth16" import hash from "./hash" import { PoseidonProof } from "./types" -import unpackProof from "./unpackProof" +import unpackProof from "./unpack-proof" import verificationKey from "./verification-key.json" /** diff --git a/packages/poseidon-proof/tests/index.test.ts b/packages/poseidon-proof/tests/index.test.ts index f3e6d6db1..949583cfc 100644 --- a/packages/poseidon-proof/tests/index.test.ts +++ b/packages/poseidon-proof/tests/index.test.ts @@ -1,9 +1,9 @@ import { buildBn128 } from "@zk-kit/groth16" import { poseidon1, poseidon2 } from "poseidon-lite" import generate from "../src/generate" -import packProof from "../src/packProof" +import packProof from "../src/pack-proof" import { PoseidonProof } from "../src/types" -import unpackProof from "../src/unpackProof" +import unpackProof from "../src/unpack-proof" import verify from "../src/verify" import hash from "../src/hash" diff --git a/yarn.lock b/yarn.lock index 9ff63c1e5..8044d8d3c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3386,6 +3386,20 @@ __metadata: languageName: node linkType: hard +"@rollup/plugin-alias@npm:^5.0.1": + version: 5.0.1 + resolution: "@rollup/plugin-alias@npm:5.0.1" + dependencies: + slash: ^4.0.0 + peerDependencies: + rollup: ^1.20.0||^2.0.0||^3.0.0||^4.0.0 + peerDependenciesMeta: + rollup: + optional: true + checksum: dab5bba16a0425c8412ece858df677c9f792bb0c5a7789c1677ee34b03533c1cda407191fcc40f3e93eb5f992cd8ed0e980e49dad3e38346547a2723b0f56fa1 + languageName: node + linkType: hard + "@rollup/plugin-commonjs@npm:^24.1.0": version: 24.1.0 resolution: "@rollup/plugin-commonjs@npm:24.1.0" @@ -4513,6 +4527,7 @@ __metadata: "@ethersproject/bignumber": ^5.7.0 "@ethersproject/bytes": ^5.7.0 "@ethersproject/keccak256": ^5.7.0 + "@rollup/plugin-alias": ^5.0.1 "@rollup/plugin-json": ^5.0.1 "@types/download": ^8.0.5 "@types/tmp": ^0.2.6 @@ -17832,6 +17847,13 @@ __metadata: languageName: node linkType: hard +"slash@npm:^4.0.0": + version: 4.0.0 + resolution: "slash@npm:4.0.0" + checksum: da8e4af73712253acd21b7853b7e0dbba776b786e82b010a5bfc8b5051a1db38ed8aba8e1e8f400dd2c9f373be91eb1c42b66e91abb407ff42b10feece5e1d2d + languageName: node + linkType: hard + "slice-ansi@npm:^2.1.0": version: 2.1.0 resolution: "slice-ansi@npm:2.1.0"