herumi / bls-wasm

BLS signature for Node.js by WebAssembly
47 stars 26 forks source link

Question: how to verify BLS has been initialized? #15

Closed shaktals closed 2 years ago

shaktals commented 2 years ago

Hey Shigeo, hajimemashite

First, thanks for the excellent lib! On our web app we use it on multiple locations asynchronously. To avoid running init multiple times, I have written a small getter fn:

export const getBls = async () => {
  const bls = window.bls

  if (!bls?.mod?.calledRun) await bls.init(bls.BN254)

  return bls
}

It seems to work for now, but I'm thinking about future compatibility.

Does this method look ok to you? Would you suggest another approach?

herumi commented 2 years ago

calledRun is automatically generated by emscripten, so I don't think that it will keep future compatibility.

I am not too sure, but how about the following?

bls = require('./')

async function asyncInit() {
  console.log('init')
  await bls.init()
}

function once(f) {
  return async function() {
    if (!f.called) {
      f.called = true
      await f()
    }
  }
}

async function sample() {
  console.log('call 1')
  await once(asyncInit)()
  console.log('call 2')
  await once(asyncInit)()
}

sample().then(() => console.log('end'))
shaktals commented 2 years ago

Best to track initialization state on the web app site, got it! 本当 に 共 ありがと お浸かり様 です よ