diff --git a/README.md b/README.md index a2a6bfc..e52969e 100644 --- a/README.md +++ b/README.md @@ -17,27 +17,29 @@ Clone this repo and build following the [Compilation] instructions below. Import the module: ``` -import { ec_add, ec_mul, ec_pairing } from 'rustbn' +import { initRustBN } from 'rustbn' + +const bn128 = await initRustBN() ``` Curve Addition ``` let inputHexString = ... -let outputHexString = ec_add(inputHexString) +let outputHexString = bn128.ec_add(inputHexString) ``` Curve Multiplication ``` let inputHexString = ... -let outputHexString = ec_mul(inputHexString) +let outputHexString = bn128.ec_mul(inputHexString) ``` Curve Pairing ``` let inputHexString = ... -let outputHexString = ec_pairing(inputHexString) +let outputHexString = bn128.ec_pairing(inputHexString) ``` ## Developer @@ -45,14 +47,17 @@ let outputHexString = ec_pairing(inputHexString) ### Building the module For basic setup: + - Install `rust` and `wasm-pack` via the [`wasm-pack` prerequisites](https://rustwasm.github.io/docs/wasm-pack/prerequisites/index.html) - Clone this repo - Install JS dependencies - `npm i` -- Run `npm run build` +- Run `npm run build:wasm` to compile the Rust code to WASM +- Run `npm run wasm2b64` to convert the WASM bytecode to a base64 string consumable by the Typescript wrapper +- Run `npm run build` to build the final JS outputs ### Build Outputs -The build process outputs both CommonJS and ESM builds of the library and specifies entry points based on the `main` and `module` fields in `package.json`. The CommonJS build is the direct output of `wasm-pack build --target nodejs` while the ESM build is a customized version of the output from `wasm-pack build --target web` that removes the need to use a bundler by converting the wasm bytecode to a base64 string and then placed in a JSON object that ESM environments can load directly. Note, the ESM glue code is found in [`src.ts`](./src.ts/rustbn.ts) +The build process outputs both CommonJS and ESM builds of the library and specifies entry points based on the `main`, `module`, and alternatively the `exports` fields in `package.json`. Both builds start with the wasm build from `wasm-bindgen` which is then loaded by a Typescript wrapper of the WASM output. Our Typescript code is slightly different than the original Javascript generated by `wasm-bindgen` because the default ESM output would require a bundler. ## License diff --git a/package.json b/package.json index ff70c42..d5a772c 100644 --- a/package.json +++ b/package.json @@ -24,13 +24,14 @@ "build": "rm -rf dist && scripts/ts-build.sh", "prepare": "npm run build", "test": "vitest run test/*", + "build:wasm": "wasm-pack build --target web -d wasm", "wasm2b64": "node scripts/wasmToB64.js" }, "main": "dist/cjs/rustbn.js", "module": "dist/esm/rustbn.js", "exports": { - "require": "dist/cjs/rustbn.js", - "import": "dist/esm/rustbn.js" + "require": "./dist/cjs/rustbn.js", + "import": "./dist/esm/rustbn.js" }, "dependencies": { "@scure/base": "^1.1.5"