This repository has been archived on 2023-04-09. You can view files and clone it, but cannot push or open issues or pull requests.
chainsafe-bls/src/context.ts

35 lines
770 B
TypeScript
Raw Normal View History

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