From e23f72ec833a9de1265c6d92a24365b80df73e58 Mon Sep 17 00:00:00 2001 From: rrr523 Date: Thu, 4 Jan 2024 01:48:15 +0800 Subject: [PATCH] feat: New WASM build --- .changeset/sharp-needles-exist.md | 5 ++++ packages/zk-crypto/README.md | 24 ++++++++++++------ packages/zk-crypto/examples/index.html | 25 +++++++++++++++++++ packages/zk-crypto/examples/node.js | 7 ++++++ packages/zk-crypto/src/browser/index.js | 33 ++++++++++++------------- packages/zk-crypto/src/browser/init.js | 5 ++-- 6 files changed, 71 insertions(+), 28 deletions(-) create mode 100644 .changeset/sharp-needles-exist.md create mode 100644 packages/zk-crypto/examples/index.html create mode 100644 packages/zk-crypto/examples/node.js diff --git a/.changeset/sharp-needles-exist.md b/.changeset/sharp-needles-exist.md new file mode 100644 index 00000000..cc128bc4 --- /dev/null +++ b/.changeset/sharp-needles-exist.md @@ -0,0 +1,5 @@ +--- +'@bnb-chain/greenfield-zk-crypto': minor +--- + +feat: Build WASM diff --git a/packages/zk-crypto/README.md b/packages/zk-crypto/README.md index 0ab7a576..f3195cb5 100644 --- a/packages/zk-crypto/README.md +++ b/packages/zk-crypto/README.md @@ -4,7 +4,7 @@ NPM Wrap for [zkbnb-js-sdk](https://github.com/bnb-chain/zkbnb-js-sdk). ## Usage -``` +```bash > npm i @bnb-chain/greenfield-zk-crypto ``` @@ -13,12 +13,13 @@ NPM Wrap for [zkbnb-js-sdk](https://github.com/bnb-chain/zkbnb-js-sdk). #### ESM ```js -import {getEddsaCompressedPublicKey} from "@bnb-chain/greenfield-zk-crypto" - -// set wasm path: CDN or your server path -window.__PUBLIC_ZKCRYPTO_WASM_PATH__ = 'zk wasm path'; +import { init, getEddsaCompressedPublicKey } from "@bnb-chain/greenfield-zk-crypto" ;(async () => { + await init( + 'https://unpkg.com/@bnb-chain/greenfield-zk-crypto/dist/node/zk-crypto.wasm' + ); + const res = await getEddsaCompressedPublicKey('foo'); console.log(res) })() @@ -29,10 +30,11 @@ window.__PUBLIC_ZKCRYPTO_WASM_PATH__ = 'zk wasm path'; ```html + + + diff --git a/packages/zk-crypto/examples/node.js b/packages/zk-crypto/examples/node.js new file mode 100644 index 00000000..bed87d35 --- /dev/null +++ b/packages/zk-crypto/examples/node.js @@ -0,0 +1,7 @@ +/* eslint-disable */ +const { getEddsaCompressedPublicKey, eddsaSign } = require('../dist/node/index'); + +(async () => { + console.log('getEddsaCompressedPublicKey', await getEddsaCompressedPublicKey('xx')); + console.log('eddsaSign:', await eddsaSign('xcvxcv', 'hello world')); +})(); diff --git a/packages/zk-crypto/src/browser/index.js b/packages/zk-crypto/src/browser/index.js index 54833873..5a043a09 100644 --- a/packages/zk-crypto/src/browser/index.js +++ b/packages/zk-crypto/src/browser/index.js @@ -1,14 +1,22 @@ import { ensureServiceIsRunning, initialize, instantiateWASM } from './init'; -// 1. modify method of `exports` and `globalThis` export. -export const startRunningService = async (wasmURL) => { - const module = await instantiateWASM(wasmURL); - module.instance.exports; +export const eddsaSign = (seed, message) => { + return ensureServiceIsRunning().eddsaSign(seed, message); +}; + +export const getEddsaCompressedPublicKey = (seed) => { + return ensureServiceIsRunning().getEddsaCompressedPublicKey(seed); +}; + +export const startRunningService = async (input) => { + if (input === undefined) { + input = new URL('../wasm/zk-crypto.wasm', import.meta.url); + } - // `exports` is a map to `//export` way of TinyGo way. - // const { add } = exports; + await instantiateWASM(input); + // const module = await instantiateWASM(input); + // const exports = module.instance.exports; - // `globalThis` is a map to complex way of `syscall/js` way. const { getEddsaCompressedPublicKey, eddsaSign } = globalThis; return { @@ -17,13 +25,4 @@ export const startRunningService = async (wasmURL) => { }; }; -// 2. wasm export function: -export const eddsaSign = async (seed, message) => { - await initialize(); - return ensureServiceIsRunning().eddsaSign(seed, message); -}; - -export const getEddsaCompressedPublicKey = async (seed) => { - await initialize(); - return ensureServiceIsRunning().getEddsaCompressedPublicKey(seed); -}; +export const init = initialize; diff --git a/packages/zk-crypto/src/browser/init.js b/packages/zk-crypto/src/browser/init.js index c5ef92cd..f566eb11 100644 --- a/packages/zk-crypto/src/browser/init.js +++ b/packages/zk-crypto/src/browser/init.js @@ -1,10 +1,9 @@ import { startRunningService } from '.'; import Go from './wasm_exec.js'; -export const initialize = async () => { +export const initialize = async (wasmPath) => { if (!initializePromise) { - const input = window.__PUBLIC_ZKCRYPTO_WASM_PATH__; - initializePromise = startRunningService(input).catch((err) => { + initializePromise = startRunningService(wasmPath).catch((err) => { // Let the caller try again if this fails. initializePromise = void 0; // But still, throw the error back up the caller.