Skip to content

Commit

Permalink
feat: complete async api
Browse files Browse the repository at this point in the history
  • Loading branch information
tplooker committed Feb 22, 2021
1 parent f5df617 commit 6bb5916
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 30 deletions.
17 changes: 11 additions & 6 deletions scripts/build-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ $WASM_2_JS $OPT --output $ASM
# WASM2JS only supports generation of es6
# Whereas node environments require es5 or cjs
# Hence the folloing converts the import and export syntax
sed -i -e '/import {/d' $ASM
sed -i -e '/export var /d' $ASM
sed -i -e 's/{abort.*},memasmFunc/wbg, memasmFunc/g' $ASM
sed -i -e 's/var retasmFunc = /module.exports = (wbg) => /' $ASM

sed -i -e 's/import {/\/\/ import {/g' $ASM
sed -i -e 's/function asmFunc/var bbs = require('\''\.\/wasm'\''); function asmFunc/g' $ASM
sed -i -e 's/{abort.*},memasmFunc/bbs, memasmFunc/g' $ASM
sed -i -e 's/export var /module\.exports\./g' $ASM
# Copy over package sources
cp -r src/js/* lib/

Expand All @@ -55,7 +54,13 @@ sed -i -e 's/wasm = wasmInstance/\/\/ wasm = wasmInstance/g' $SRC_WASM
# Replace with a helper function that support loading the asm.js
# version as a fall back
echo "
wasm = require('./wasm_helper')();
const createPromise = require('./wasm_helper');
const wasmPromise = createPromise().catch(() => null);
module.exports.isReady = function () { return !!wasm; }
module.exports.waitReady = function () { return wasmPromise.then(() => !!wasm); }
wasmPromise.then((_wasm) => { wasm = _wasm });
" >> $SRC_WASM

# Delete the gitignore and readme automatically created by wasm-pack
Expand Down
51 changes: 35 additions & 16 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ if (useWasm) {
module.exports.BBS_SIGNATURE_LENGTH = 112;

module.exports.generateBls12381G1KeyPair = async (seed) => {
await wasm.waitReady();
var result = await throwErrorOnRejectedPromise(
wasm.generateBls12381G1KeyPair(seed ? seed : null)
);
Expand All @@ -64,6 +65,7 @@ if (useWasm) {
};

module.exports.generateBls12381G2KeyPair = async (seed) => {
await wasm.waitReady();
var result = await throwErrorOnRejectedPromise(
wasm.generateBls12381G2KeyPair(seed ? seed : null)
);
Expand All @@ -74,6 +76,7 @@ if (useWasm) {
};

module.exports.bls12381toBbs = async (request) => {
await wasm.waitReady();
var result = await throwErrorOnRejectedPromise(wasm.bls12381toBbs(request));
return {
publicKey: new Uint8Array(result.publicKey),
Expand All @@ -84,27 +87,43 @@ if (useWasm) {
};
};

module.exports.sign = (request) =>
throwErrorOnRejectedPromise(wasm.sign(request));
module.exports.sign = async (request) => {
await wasm.waitReady();
return await throwErrorOnRejectedPromise(wasm.sign(request));
};

module.exports.blsSign = (request) =>
throwErrorOnRejectedPromise(wasm.blsSign(request));
module.exports.blsSign = async (request) => {
await wasm.waitReady();
return await throwErrorOnRejectedPromise(wasm.blsSign(request));
};

module.exports.verify = (request) =>
throwErrorOnRejectedPromise(wasm.verify(request));
module.exports.verify = async (request) => {
await wasm.waitReady();
return await throwErrorOnRejectedPromise(wasm.verify(request));
};

module.exports.blsVerify = (request) =>
throwErrorOnRejectedPromise(wasm.blsVerify(request));
module.exports.blsVerify = async (request) => {
await wasm.waitReady();
return await throwErrorOnRejectedPromise(wasm.blsVerify(request));
};

module.exports.createProof = (request) =>
throwErrorOnRejectedPromise(wasm.createProof(request));
module.exports.createProof = async (request) => {
await wasm.waitReady();
return await throwErrorOnRejectedPromise(wasm.createProof(request));
};

module.exports.blsCreateProof = (request) =>
throwErrorOnRejectedPromise(wasm.blsCreateProof(request));
module.exports.blsCreateProof = async (request) => {
await wasm.waitReady();
return await throwErrorOnRejectedPromise(wasm.blsCreateProof(request));
};

module.exports.verifyProof = (request) =>
throwErrorOnRejectedPromise(wasm.verifyProof(request));
module.exports.verifyProof = async (request) => {
await wasm.waitReady();
return await throwErrorOnRejectedPromise(wasm.verifyProof(request));
};

module.exports.blsVerifyProof = (request) =>
throwErrorOnRejectedPromise(wasm.blsVerifyProof(request));
module.exports.blsVerifyProof = async (request) => {
await wasm.waitReady();
return await throwErrorOnRejectedPromise(wasm.blsVerifyProof(request));
};
}
16 changes: 8 additions & 8 deletions src/js/wasm_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const {
BBS_SIGNATURES_MODES,
} = require("./util");

module.exports = function getWasmInstance() {
module.exports = async function () {
if (
!process.env.BBS_SIGNATURES_MODE ||
process.env.BBS_SIGNATURES_MODE !== BBS_SIGNATURES_MODES.asmjs
Expand All @@ -30,10 +30,11 @@ module.exports = function getWasmInstance() {
if (!WebAssembly) {
throw new Error(WEB_ASSEMBLY_NOT_FOUND_ERROR);
}
const wasmModule = new WebAssembly.Module(bytes);
return new WebAssembly.Instance(wasmModule, {
__wbindgen_placeholder__: imports,
}).exports;
return (
await WebAssembly.instantiate(bytes, {
__wbindgen_placeholder__: imports,
})
).instance.exports;
} catch (error) {
console.log(
"The following error occurred in attempting to load the WASM. Attempting to use ASM fallback."
Expand All @@ -43,10 +44,9 @@ module.exports = function getWasmInstance() {
}

// if we have a valid supplied asm.js, return that
if (asm()) {
return asm();
if (asm) {
return asm;
}

console.error(FAILED_INITIALIZE_ERROR);
console.error(error);

Expand Down

0 comments on commit 6bb5916

Please sign in to comment.