2022-04-11 15:08:15 +00:00
|
|
|
import {IBls, Implementation} from "./interface.js";
|
2020-11-25 15:03:11 +00:00
|
|
|
|
2022-04-11 15:08:15 +00:00
|
|
|
export {IBls, Implementation, CoordType, PointFormat} from "./interface.js";
|
2020-11-29 18:54:33 +00:00
|
|
|
|
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 17:10:20 +00:00
|
|
|
export const bls: IBls = {} as IBls;
|
2020-11-30 11:43:47 +00:00
|
|
|
export default bls;
|
2020-11-25 15:03:11 +00:00
|
|
|
|
2022-04-11 15:08:15 +00:00
|
|
|
// Thanks https://github.com/iliakan/detect-node/blob/master/index.esm.js
|
|
|
|
const isNode = Object.prototype.toString.call(typeof process !== "undefined" ? process : 0) === "[object process]";
|
|
|
|
|
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) {
|
2022-04-11 15:08:15 +00:00
|
|
|
case "herumi": {
|
|
|
|
const blsHerumi = (await import("./herumi/index.js")).bls;
|
2020-11-25 16:09:44 +00:00
|
|
|
await blsHerumi.init();
|
2020-11-25 15:03:11 +00:00
|
|
|
return blsHerumi;
|
2022-04-11 15:08:15 +00:00
|
|
|
}
|
2020-11-25 15:03:11 +00:00
|
|
|
|
|
|
|
case "blst-native":
|
2020-11-29 16:59:24 +00:00
|
|
|
// Lazy import native bindings to prevent automatically importing binding.node files
|
2022-04-11 15:08:15 +00:00
|
|
|
if (!isNode) {
|
2020-11-25 15:03:11 +00:00
|
|
|
throw Error("blst-native is only supported in NodeJS");
|
|
|
|
}
|
2022-04-11 15:08:15 +00:00
|
|
|
return (await import("./blst-native/index.js")).bls;
|
2020-11-25 15:03:11 +00:00
|
|
|
|
|
|
|
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()
|
2020-11-30 11:29:24 +00:00
|
|
|
const blsImpl = await getImplementation(impl);
|
|
|
|
Object.assign(bls, blsImpl);
|
2020-11-25 15:03:11 +00:00
|
|
|
}
|