2020-11-25 15:03:11 +00:00
|
|
|
import {IBls} from "./interface";
|
2020-11-25 16:23:53 +00:00
|
|
|
import {bls as blsHerumi} from "./herumi";
|
2020-11-25 15:03:11 +00:00
|
|
|
|
|
|
|
export type Implementation = "herumi" | "blst-native";
|
|
|
|
|
2020-11-25 16:09:44 +00:00
|
|
|
// TODO: Use a Proxy for example to throw an error if it's not initialized yet
|
2020-11-29 16:52:29 +00:00
|
|
|
export let bls: IBls = {} as IBls;
|
2020-11-25 15:03:11 +00:00
|
|
|
|
2020-11-25 17:58:03 +00:00
|
|
|
async function getImplementation(impl: Implementation = "herumi"): Promise<IBls> {
|
2020-11-25 15:03:11 +00:00
|
|
|
switch (impl) {
|
|
|
|
case "herumi":
|
2020-11-25 16:09:44 +00:00
|
|
|
await blsHerumi.init();
|
2020-11-25 15:03:11 +00:00
|
|
|
return blsHerumi;
|
|
|
|
|
|
|
|
case "blst-native":
|
|
|
|
if (typeof require !== "function") {
|
|
|
|
throw Error("blst-native is only supported in NodeJS");
|
|
|
|
}
|
2020-11-25 16:23:53 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
2020-11-25 15:03:11 +00:00
|
|
|
return require("./blst");
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported implementation - ${impl}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 16:23:53 +00:00
|
|
|
export async function init(impl: Implementation): Promise<void> {
|
2020-11-29 16:52:29 +00:00
|
|
|
// Using Object.assign instead of just bls = getImplementation()
|
|
|
|
// because otherwise the default import breaks. The reference is lost
|
|
|
|
// and the imported object is still undefined after calling init()
|
|
|
|
Object.assign(bls, await getImplementation(impl));
|
2020-11-25 15:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default bls;
|