This commit is contained in:
Marin Petrunić 2020-11-23 14:42:52 +01:00
parent 135625ccc0
commit c153de6e24
No known key found for this signature in database
GPG Key ID: 834D07135E110DA5
8 changed files with 121 additions and 125 deletions

View File

@ -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<IBls> {
if (!context) {
switch(backing) {
case "herumi": {
switch (backing) {
case "herumi":
{
context = await herumiToIBls();
contextBacking = backing;
} break;
default: throw new Error(`Unsupported backing - ${backing}`)
}
break;
default:
throw new Error(`Unsupported backing - ${backing}`);
}
}
await context.init();
@ -23,7 +25,7 @@ export async function init(backing: Backing = "herumi"): Promise<IBls> {
}
export function destroy(): void {
if(context) {
if (context) {
context.destroy();
}
context = undefined;
@ -37,7 +39,6 @@ export function getContext(): IBls {
return context;
}
export function getContextBacking(): Backing {
if (!context) {
throw new Error("BLS not initialized");

View File

@ -1,6 +1,6 @@
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<IBls> {
await initHerumi();
@ -27,7 +27,7 @@ export async function herumiToIBls(): Promise<IBls> {
},
destroy: () => {
destroy();
}
}
},
};
return context;
}

View File

@ -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};

View File

@ -1,12 +1,11 @@
interface Common {
new(): this;
interface ICommon {
new (): this;
deserializeHexStr(s: string): void;
serializeToHexStr(): string;
isEqual(rhs: this): boolean
isEqual(rhs: this): boolean;
deserialize(v: Uint8Array): void;
@ -21,8 +20,7 @@ interface Common {
clone(): this;
}
export interface IPrivateKeyValue extends Common {
export interface IPrivateKeyValue extends ICommon {
setInt(x: number): void;
setHashOf(a: Uint8Array): void;
@ -34,24 +32,21 @@ export interface IPrivateKeyValue extends Common {
getPublicKey(): IPublicKeyValue;
sign(m: string | Uint8Array): ISignatureValue;
}
export interface IPublicKeyValue extends Common {
export interface IPublicKeyValue extends ICommon {
verify(signature: ISignatureValue, m: Uint8Array | string): boolean;
isValidOrder(): boolean;
deserializeUncompressed (s: Uint8Array): void;
serializeUncompressed (): Uint8Array;
deserializeUncompressedHexStr (s:string): void;
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;
export interface ISignatureValue extends ICommon {
deserializeUncompressed(s: Uint8Array): void;
serializeUncompressed(): Uint8Array;
deserializeUncompressedHexStr(s: string): void;
serializeUncompressedToHexStr(): string;
isValidOrder(): boolean;
aggregate(others: ISignatureValue[]): boolean;

View File

@ -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;

View File

@ -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);
}

View File

@ -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;