diff --git a/src/context.ts b/src/context.ts index 98b56d5..f75c680 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,21 +1,23 @@ -import herumi from "bls-eth-wasm"; -import { herumiToIBls } from "./herumi"; -import { IBls } from "./interface"; +import {herumiToIBls} from "./herumi"; +import {IBls} from "./interface"; export type Backing = "herumi" | "blst-native" | "blst-wasm"; -let contextBacking: Backing|undefined = undefined; -let context: IBls|undefined = undefined; +let contextBacking: Backing | undefined = undefined; +let context: IBls | undefined = undefined; //to maintain api compatible, add all backing context to return type export async function init(backing: Backing = "herumi"): Promise { if (!context) { - switch(backing) { - case "herumi": { - context = await herumiToIBls(); - contextBacking = backing; - } break; - default: throw new Error(`Unsupported backing - ${backing}`) + switch (backing) { + case "herumi": + { + context = await herumiToIBls(); + contextBacking = backing; + } + break; + default: + throw new Error(`Unsupported backing - ${backing}`); } } await context.init(); @@ -23,9 +25,9 @@ export async function init(backing: Backing = "herumi"): Promise { } export function destroy(): void { - if(context) { - context.destroy(); - } + if (context) { + context.destroy(); + } context = undefined; contextBacking = undefined; } @@ -37,7 +39,6 @@ export function getContext(): IBls { return context; } - export function getContextBacking(): Backing { if (!context) { throw new Error("BLS not initialized"); diff --git a/src/herumi/adapter.ts b/src/herumi/adapter.ts index 0837e04..5c2aca0 100644 --- a/src/herumi/adapter.ts +++ b/src/herumi/adapter.ts @@ -1,33 +1,33 @@ import herumi from "bls-eth-wasm"; -import { init as initHerumi, destroy, getContext } from "./context"; -import { IBls, IPrivateKeyValue, IPublicKeyValue, ISignatureValue } from '../interface'; +import {init as initHerumi, destroy, getContext} from "./context"; +import {IBls, IPrivateKeyValue, IPublicKeyValue, ISignatureValue} from "../interface"; export async function herumiToIBls(): Promise { - await initHerumi(); - const context: IBls = { - SecretKey: getContext().SecretKey as IPrivateKeyValue, - PublicKey: getContext().PublicKey as IPublicKeyValue, - Signature: getContext().Signature as ISignatureValue, + await initHerumi(); + const context: IBls = { + SecretKey: getContext().SecretKey as IPrivateKeyValue, + PublicKey: getContext().PublicKey as IPublicKeyValue, + Signature: getContext().Signature as ISignatureValue, - toHex: herumi.toHex, - toHexStr: herumi.toHexStr, - fromHexStr: herumi.fromHexStr, - getCurveOrder: herumi.getCurveOrder, - getFieldOrder: herumi.getFieldOrder, - verifySignatureOrder: herumi.verifySignatureOrder, - verifyPublicKeyOrder: herumi.verifyPublicKeyOrder, - areAllMsgDifferent: herumi.areAllMsgDifferent, - shouldVerifyBlsSignatureOrder: herumi.shouldVerifyBlsSignatureOrder, - shouldVerifyBlsPublicKeyOrder: herumi.shouldVerifyBlsPublicKeyOrder, - deserializeHexStrToSecretKey: herumi.deserializeHexStrToSecretKey as IBls["deserializeHexStrToSecretKey"], - deserializeHexStrToPublicKey: herumi.deserializeHexStrToPublicKey as IBls["deserializeHexStrToPublicKey"], - deserializeHexStrToSignature: herumi.deserializeHexStrToSignature as IBls["deserializeHexStrToSignature"], - init: async () => { - return context; - }, - destroy: () => { - destroy(); - } - } - return context; -} \ No newline at end of file + toHex: herumi.toHex, + toHexStr: herumi.toHexStr, + fromHexStr: herumi.fromHexStr, + getCurveOrder: herumi.getCurveOrder, + getFieldOrder: herumi.getFieldOrder, + verifySignatureOrder: herumi.verifySignatureOrder, + verifyPublicKeyOrder: herumi.verifyPublicKeyOrder, + areAllMsgDifferent: herumi.areAllMsgDifferent, + shouldVerifyBlsSignatureOrder: herumi.shouldVerifyBlsSignatureOrder, + shouldVerifyBlsPublicKeyOrder: herumi.shouldVerifyBlsPublicKeyOrder, + deserializeHexStrToSecretKey: herumi.deserializeHexStrToSecretKey as IBls["deserializeHexStrToSecretKey"], + deserializeHexStrToPublicKey: herumi.deserializeHexStrToPublicKey as IBls["deserializeHexStrToPublicKey"], + deserializeHexStrToSignature: herumi.deserializeHexStrToSignature as IBls["deserializeHexStrToSignature"], + init: async () => { + return context; + }, + destroy: () => { + destroy(); + }, + }; + return context; +} diff --git a/src/herumi/index.ts b/src/herumi/index.ts index ee2ec62..f811356 100644 --- a/src/herumi/index.ts +++ b/src/herumi/index.ts @@ -1 +1 @@ -export {herumiToIBls} from "./adapter"; \ No newline at end of file +export {herumiToIBls} from "./adapter"; diff --git a/src/index.ts b/src/index.ts index 2db838e..58cdea1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,9 @@ import {PUBLIC_KEY_LENGTH} from "./constants"; import assert from "assert"; -import { Keypair } from "./keypair"; -import { PrivateKey } from "./privateKey"; -import { PublicKey } from "./publicKey"; -import { Signature } from "./signature"; +import {Keypair} from "./keypair"; +import {PrivateKey} from "./privateKey"; +import {PublicKey} from "./publicKey"; +import {Signature} from "./signature"; export {Keypair, PrivateKey, PublicKey, Signature}; diff --git a/src/interface.ts b/src/interface.ts index 54905b9..e59395a 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -1,90 +1,85 @@ -interface Common { +interface ICommon { + new (): this; - new(): this; + deserializeHexStr(s: string): void; - deserializeHexStr(s: string): void; + serializeToHexStr(): string; - serializeToHexStr(): string; + isEqual(rhs: this): boolean; - isEqual(rhs: this): boolean + deserialize(v: Uint8Array): void; - deserialize(v: Uint8Array): void; + serialize(): Uint8Array; - serialize(): Uint8Array; + add(rhs: this): void; - add(rhs: this): void; + dump(msg?: string): string; - dump(msg?: string): string; - - clear(): void; + clear(): void; - clone(): this; + clone(): this; } -export interface IPrivateKeyValue extends Common { +export interface IPrivateKeyValue extends ICommon { + setInt(x: number): void; - setInt(x: number): void; + setHashOf(a: Uint8Array): void; - setHashOf(a: Uint8Array): void; + setLittleEndian(a: Uint8Array): void; - setLittleEndian(a: Uint8Array): void; + setByCSPRNG(): void; - setByCSPRNG(): void; - - getPublicKey(): IPublicKeyValue; - - sign(m: string | Uint8Array): ISignatureValue; + getPublicKey(): IPublicKeyValue; + sign(m: string | Uint8Array): ISignatureValue; } -export interface IPublicKeyValue extends Common { - - verify(signature: ISignatureValue, m: Uint8Array | string): boolean; - isValidOrder(): boolean; - deserializeUncompressed (s: Uint8Array): void; - serializeUncompressed (): Uint8Array; - deserializeUncompressedHexStr (s:string): void; - serializeUncompressedToHexStr(): string; +export interface IPublicKeyValue extends ICommon { + verify(signature: ISignatureValue, m: Uint8Array | string): boolean; + isValidOrder(): boolean; + deserializeUncompressed(s: Uint8Array): void; + serializeUncompressed(): Uint8Array; + deserializeUncompressedHexStr(s: string): void; + serializeUncompressedToHexStr(): string; } -export interface ISignatureValue extends Common { - - deserializeUncompressed (s: Uint8Array): void; - serializeUncompressed (): Uint8Array; - deserializeUncompressedHexStr (s:string): void; - serializeUncompressedToHexStr(): string; - isValidOrder(): boolean; - aggregate(others: ISignatureValue[]): boolean; - aggregateVerifyNoCheck(publicKeys: IPublicKeyValue[], messages: Uint8Array): boolean; - fastAggregateVerify(publicKeys: IPublicKeyValue[], message: Uint8Array): boolean; +export interface ISignatureValue extends ICommon { + deserializeUncompressed(s: Uint8Array): void; + serializeUncompressed(): Uint8Array; + deserializeUncompressedHexStr(s: string): void; + serializeUncompressedToHexStr(): string; + isValidOrder(): boolean; + aggregate(others: ISignatureValue[]): boolean; + aggregateVerifyNoCheck(publicKeys: IPublicKeyValue[], messages: Uint8Array): boolean; + fastAggregateVerify(publicKeys: IPublicKeyValue[], message: Uint8Array): boolean; } export interface IBls { - //property names are like that for api compatibility - SecretKey: InstanceType; - PublicKey: InstanceType; - Signature: InstanceType; + //property names are like that for api compatibility + SecretKey: InstanceType; + PublicKey: InstanceType; + Signature: InstanceType; - toHex(a: Uint8Array, start: number, length: number): string; - toHexStr(a: Uint8Array): string; - fromHexStr(s: string): Uint8Array; - getCurveOrder(): string; - getFieldOrder(): string; - verifySignatureOrder(doVerify: boolean): void; - verifyPublicKeyOrder(doVerify: boolean): void; - - /** - * - * @param msgs single array with concatenated messages - * @param msgSize defaults to MSG_SIZE - */ - areAllMsgDifferent(msgs: Uint8Array, msgSize?: number): boolean; - shouldVerifyBlsSignatureOrder(b: string): void; - shouldVerifyBlsPublicKeyOrder(b: string): void; - deserializeHexStrToSecretKey(s: string): IPrivateKeyValue; - deserializeHexStrToPublicKey(s: string): IPublicKeyValue; - deserializeHexStrToSignature(s: string): ISignatureValue; + toHex(a: Uint8Array, start: number, length: number): string; + toHexStr(a: Uint8Array): string; + fromHexStr(s: string): Uint8Array; + getCurveOrder(): string; + getFieldOrder(): string; + verifySignatureOrder(doVerify: boolean): void; + verifyPublicKeyOrder(doVerify: boolean): void; - init(): Promise; - destroy(): void; -} \ No newline at end of file + /** + * + * @param msgs single array with concatenated messages + * @param msgSize defaults to MSG_SIZE + */ + areAllMsgDifferent(msgs: Uint8Array, msgSize?: number): boolean; + shouldVerifyBlsSignatureOrder(b: string): void; + shouldVerifyBlsPublicKeyOrder(b: string): void; + deserializeHexStrToSecretKey(s: string): IPrivateKeyValue; + deserializeHexStrToPublicKey(s: string): IPublicKeyValue; + deserializeHexStrToSignature(s: string): ISignatureValue; + + init(): Promise; + destroy(): void; +} diff --git a/src/privateKey.ts b/src/privateKey.ts index 76149e2..b565997 100644 --- a/src/privateKey.ts +++ b/src/privateKey.ts @@ -1,9 +1,9 @@ import {SECRET_KEY_LENGTH} from "./constants"; import assert from "assert"; import {generateRandomSecretKey} from "@chainsafe/bls-keygen"; -import { IPrivateKeyValue } from "./interface"; -import { getContext } from "./context"; -import { PublicKey, Signature } from "."; +import {IPrivateKeyValue} from "./interface"; +import {getContext} from "./context"; +import {PublicKey, Signature} from "."; export class PrivateKey { private value: IPrivateKeyValue; diff --git a/src/publicKey.ts b/src/publicKey.ts index 802fb45..c684605 100644 --- a/src/publicKey.ts +++ b/src/publicKey.ts @@ -3,8 +3,8 @@ import {PUBLIC_KEY_LENGTH} from "./constants"; import assert from "assert"; import {Signature} from "./signature"; import {EMPTY_PUBLIC_KEY} from "./helpers/utils"; -import { IPublicKeyValue } from './interface'; -import { getContext } from "./context"; +import {IPublicKeyValue} from "./interface"; +import {getContext} from "./context"; export class PublicKey { private value: IPublicKeyValue; @@ -31,7 +31,7 @@ export class PublicKey { assert(value.length === PUBLIC_KEY_LENGTH * 2); const context = getContext(); const pubkeyValue = new context.PublicKey(); - pubkeyValue.deserializeHexStr(value) + pubkeyValue.deserializeHexStr(value); return new PublicKey(pubkeyValue); } diff --git a/src/signature.ts b/src/signature.ts index 0ada6b0..9a51587 100644 --- a/src/signature.ts +++ b/src/signature.ts @@ -1,9 +1,9 @@ import assert from "assert"; -import { FP_POINT_LENGTH } from "./constants"; -import { getContext } from "./context"; -import { EMPTY_SIGNATURE } from "./helpers/utils"; -import { ISignatureValue } from './interface'; -import { PublicKey } from "./publicKey"; +import {FP_POINT_LENGTH} from "./constants"; +import {getContext} from "./context"; +import {EMPTY_SIGNATURE} from "./helpers/utils"; +import {ISignatureValue} from "./interface"; +import {PublicKey} from "./publicKey"; export class Signature { private value: ISignatureValue;