2019-12-11 09:58:24 +00:00
|
|
|
/* eslint-disable require-atomic-updates */
|
2020-11-23 11:06:20 +00:00
|
|
|
import bls from "bls-eth-wasm";
|
2019-11-27 18:21:37 +00:00
|
|
|
|
2020-11-04 13:45:02 +00:00
|
|
|
type Bls = typeof bls;
|
|
|
|
let blsGlobal: Bls | null = null;
|
2020-11-20 19:03:17 +00:00
|
|
|
let blsGlobalPromise: Promise<void> | null = null;
|
2019-11-27 18:21:37 +00:00
|
|
|
|
2020-11-20 19:03:17 +00:00
|
|
|
export async function setupBls(): Promise<void> {
|
2020-11-04 13:45:02 +00:00
|
|
|
if (!blsGlobal) {
|
|
|
|
await bls.init();
|
|
|
|
blsGlobal = bls;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache a promise for Bls instead of Bls to make sure it is initialized only once
|
2020-11-20 19:03:17 +00:00
|
|
|
export async function initBLS(): Promise<void> {
|
2020-11-04 13:45:02 +00:00
|
|
|
if (!blsGlobalPromise) {
|
|
|
|
blsGlobalPromise = setupBls();
|
|
|
|
}
|
|
|
|
return blsGlobalPromise;
|
2019-11-27 18:21:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function destroy(): void {
|
2020-11-04 13:45:02 +00:00
|
|
|
blsGlobal = null;
|
|
|
|
blsGlobalPromise = null;
|
2019-11-27 18:21:37 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 13:45:02 +00:00
|
|
|
export function getContext(): Bls {
|
|
|
|
if (!blsGlobal) {
|
|
|
|
throw new Error("BLS not initialized");
|
2019-11-27 18:21:37 +00:00
|
|
|
}
|
2020-11-04 13:45:02 +00:00
|
|
|
return blsGlobal;
|
2020-11-04 17:40:36 +00:00
|
|
|
}
|