Skip to content

Commit

Permalink
feat: New WASM build
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 committed Jan 3, 2024
1 parent 533f073 commit e23f72e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-needles-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfield-zk-crypto': minor
---

feat: Build WASM
24 changes: 16 additions & 8 deletions packages/zk-crypto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand All @@ -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)
})()
Expand All @@ -29,10 +30,11 @@ window.__PUBLIC_ZKCRYPTO_WASM_PATH__ = 'zk wasm path';
```html
<script src="https://cdn.jsdelivr.net/npm/@bnb-chain/greenfield-zk-crypto/dist/umd/index.js"></script>
<script>
// set wasm path
window.__PUBLIC_CROSS_WASM_PATH__ = 'https://cdn.jsdelivr.net/npm/@bnb-chain/greenfield-zk-crypto/dist/node/zk-crypto.wasm';
;(async () => {
await ZkCrypto.init(
'https://unpkg.com/@bnb-chain/greenfield-zk-crypto/dist/node/zk-crypto.wasm'
);
const res = await getEddsaCompressedPublicKey('foo');
console.log(res)
})()
Expand All @@ -48,3 +50,9 @@ const { getEddsaCompressedPublicKey } = require("@bnb-chain/greenfield-zk-crypto
const res = await getEddsaCompressedPublicKey('foo');
})()
```

## Build WASM

```bash
tinygo build -no-debug -o zk-crypto.wasm -target=wasm main.go
```
25 changes: 25 additions & 0 deletions packages/zk-crypto/examples/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="../dist/browser/umd/index.js"></script>
<script>
// WASM exports as `ZkCrypto` UMD module.
console.log('UMD', ZkCrypto);

;(async () => {
await ZkCrypto.init(
'https://unpkg.com/@bnb-chain/greenfield-zk-crypto/dist/node/zk-crypto.wasm'
);

console.log('getEddsaCompressedPublicKey', ZkCrypto.getEddsaCompressedPublicKey('xx'))

console.log( 'eddsaSign:', ZkCrypto.eddsaSign('xcvxcv', 'hello world') )
})()
</script>
</body>
</html>
7 changes: 7 additions & 0 deletions packages/zk-crypto/examples/node.js
Original file line number Diff line number Diff line change
@@ -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'));
})();
33 changes: 16 additions & 17 deletions packages/zk-crypto/src/browser/index.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
5 changes: 2 additions & 3 deletions packages/zk-crypto/src/browser/init.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit e23f72e

Please sign in to comment.