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/index.ts

77 lines
2.8 KiB
TypeScript
Raw Normal View History

2020-11-30 18:01:13 +00:00
import {IBls, ISecretKey, IPublicKey, ISignature} 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-29 18:54:33 +00:00
export * from "./interface";
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
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":
2020-11-29 16:59:24 +00:00
// Lazy import native bindings to prevent automatically importing binding.node files
2020-11-25 15:03:11 +00:00
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-29 16:59:16 +00:00
return require("./blst").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> {
// 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);
Object.assign(exports, blsImpl);
2020-11-25 15:03:11 +00:00
}
2020-11-30 11:29:24 +00:00
// Proxy named exports, will get set by `Object.assign(exports, blsImpl)`
2020-11-30 11:32:36 +00:00
export declare let sign: IBls["sign"];
export declare let aggregateSignatures: IBls["aggregateSignatures"];
2020-11-30 18:01:13 +00:00
export declare let aggregatePublicKeys: IBls["aggregatePublicKeys"];
2020-11-30 11:32:36 +00:00
export declare let verify: IBls["verify"];
export declare let verifyAggregate: IBls["verifyAggregate"];
export declare let verifyMultiple: IBls["verifyMultiple"];
2020-11-30 19:45:23 +00:00
export declare let secretKeyToPublicKey: IBls["secretKeyToPublicKey"];
2020-11-30 11:29:24 +00:00
2020-11-30 18:01:13 +00:00
export declare class SecretKey implements ISecretKey {
static fromBytes(bytes: Uint8Array): SecretKey;
static fromHex(hex: string): SecretKey;
static fromKeygen(entropy?: Uint8Array): SecretKey;
2020-11-30 11:43:47 +00:00
sign(message: Uint8Array): Signature;
toPublicKey(): PublicKey;
toBytes(): Uint8Array;
toHex(): string;
}
export declare class PublicKey implements IPublicKey {
static fromBytes(bytes: Uint8Array): PublicKey;
static fromHex(hex: string): PublicKey;
2020-11-30 18:01:13 +00:00
static aggregate(publicKeys: PublicKey[]): PublicKey;
2020-11-30 11:43:47 +00:00
toBytes(): Uint8Array;
toHex(): string;
}
export declare class Signature implements ISignature {
static fromBytes(bytes: Uint8Array): Signature;
static fromHex(hex: string): Signature;
static aggregate(signatures: Signature[]): Signature;
verify(publicKey: PublicKey, message: Uint8Array): boolean;
verifyAggregate(publicKeys: PublicKey[], message: Uint8Array): boolean;
verifyMultiple(publicKeys: PublicKey[], messages: Uint8Array[]): boolean;
toBytes(): Uint8Array;
toHex(): string;
}