De-duplicate interface
This commit is contained in:
parent
ccd870f189
commit
a51022f802
|
@ -1,7 +1,7 @@
|
||||||
import * as blst from "@chainsafe/blst";
|
import * as blst from "@chainsafe/blst";
|
||||||
import {EmptyAggregateError, ZeroPublicKeyError} from "../errors";
|
import {EmptyAggregateError, ZeroPublicKeyError} from "../errors";
|
||||||
import {bytesToHex, hexToBytes} from "../helpers";
|
import {bytesToHex, hexToBytes} from "../helpers";
|
||||||
import {IPublicKey} from "../interface";
|
import {PublicKey as IPublicKey} from "../interface";
|
||||||
|
|
||||||
export class PublicKey implements IPublicKey {
|
export class PublicKey implements IPublicKey {
|
||||||
readonly affine: blst.PublicKey;
|
readonly affine: blst.PublicKey;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import * as blst from "@chainsafe/blst";
|
import * as blst from "@chainsafe/blst";
|
||||||
import {bytesToHex, hexToBytes, isZeroUint8Array, randomBytes} from "../helpers";
|
import {bytesToHex, hexToBytes, isZeroUint8Array, randomBytes} from "../helpers";
|
||||||
import {SECRET_KEY_LENGTH} from "../constants";
|
import {SECRET_KEY_LENGTH} from "../constants";
|
||||||
import {ISecretKey} from "../interface";
|
import {SecretKey as ISecretKey} from "../interface";
|
||||||
import {PublicKey} from "./publicKey";
|
import {PublicKey} from "./publicKey";
|
||||||
import {Signature} from "./signature";
|
import {Signature} from "./signature";
|
||||||
import {ZeroSecretKeyError} from "../errors";
|
import {ZeroSecretKeyError} from "../errors";
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import * as blst from "@chainsafe/blst";
|
import * as blst from "@chainsafe/blst";
|
||||||
import {bytesToHex, hexToBytes} from "../helpers";
|
import {bytesToHex, hexToBytes} from "../helpers";
|
||||||
import {ISignature} from "../interface";
|
import {Signature as ISignature} from "../interface";
|
||||||
import {PublicKey} from "./publicKey";
|
import {PublicKey} from "./publicKey";
|
||||||
import {EmptyAggregateError, ZeroSignatureError} from "../errors";
|
import {EmptyAggregateError, ZeroSignatureError} from "../errors";
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ import {PublicKeyType} from "bls-eth-wasm";
|
||||||
import {getContext} from "./context";
|
import {getContext} from "./context";
|
||||||
import {PUBLIC_KEY_LENGTH} from "../constants";
|
import {PUBLIC_KEY_LENGTH} from "../constants";
|
||||||
import {bytesToHex, hexToBytes, isZeroUint8Array} from "../helpers";
|
import {bytesToHex, hexToBytes, isZeroUint8Array} from "../helpers";
|
||||||
import {IPublicKey} from "../interface";
|
import {PublicKey as IPublicKey} from "../interface";
|
||||||
import {EmptyAggregateError, InvalidLengthError, ZeroPublicKeyError} from "../errors";
|
import {EmptyAggregateError, InvalidLengthError, ZeroPublicKeyError} from "../errors";
|
||||||
|
|
||||||
export class PublicKey implements IPublicKey {
|
export class PublicKey implements IPublicKey {
|
||||||
|
|
|
@ -5,7 +5,7 @@ import {getContext} from "./context";
|
||||||
import {PublicKey} from "./publicKey";
|
import {PublicKey} from "./publicKey";
|
||||||
import {Signature} from "./signature";
|
import {Signature} from "./signature";
|
||||||
import {bytesToHex, hexToBytes} from "../helpers";
|
import {bytesToHex, hexToBytes} from "../helpers";
|
||||||
import {ISecretKey} from "../interface";
|
import {SecretKey as ISecretKey} from "../interface";
|
||||||
import {InvalidLengthError, ZeroSecretKeyError} from "../errors";
|
import {InvalidLengthError, ZeroSecretKeyError} from "../errors";
|
||||||
|
|
||||||
export class SecretKey implements ISecretKey {
|
export class SecretKey implements ISecretKey {
|
||||||
|
|
|
@ -3,7 +3,7 @@ import {SignatureType} from "bls-eth-wasm";
|
||||||
import {getContext} from "./context";
|
import {getContext} from "./context";
|
||||||
import {PublicKey} from "./publicKey";
|
import {PublicKey} from "./publicKey";
|
||||||
import {bytesToHex, hexToBytes, isZeroUint8Array} from "../helpers";
|
import {bytesToHex, hexToBytes, isZeroUint8Array} from "../helpers";
|
||||||
import {ISignature} from "../interface";
|
import {Signature as ISignature} from "../interface";
|
||||||
import {EmptyAggregateError, InvalidLengthError, InvalidOrderError} from "../errors";
|
import {EmptyAggregateError, InvalidLengthError, InvalidOrderError} from "../errors";
|
||||||
|
|
||||||
export class Signature implements ISignature {
|
export class Signature implements ISignature {
|
||||||
|
|
31
src/index.ts
31
src/index.ts
|
@ -1,4 +1,4 @@
|
||||||
import {IBls, ISecretKey, IPublicKey, ISignature} from "./interface";
|
import {IBls} from "./interface";
|
||||||
import {bls as blsHerumi} from "./herumi";
|
import {bls as blsHerumi} from "./herumi";
|
||||||
|
|
||||||
export type Implementation = "herumi" | "blst-native";
|
export type Implementation = "herumi" | "blst-native";
|
||||||
|
@ -45,32 +45,3 @@ export declare let verify: IBls["verify"];
|
||||||
export declare let verifyAggregate: IBls["verifyAggregate"];
|
export declare let verifyAggregate: IBls["verifyAggregate"];
|
||||||
export declare let verifyMultiple: IBls["verifyMultiple"];
|
export declare let verifyMultiple: IBls["verifyMultiple"];
|
||||||
export declare let secretKeyToPublicKey: IBls["secretKeyToPublicKey"];
|
export declare let secretKeyToPublicKey: IBls["secretKeyToPublicKey"];
|
||||||
|
|
||||||
export declare class SecretKey implements ISecretKey {
|
|
||||||
static fromBytes(bytes: Uint8Array): SecretKey;
|
|
||||||
static fromHex(hex: string): SecretKey;
|
|
||||||
static fromKeygen(entropy?: Uint8Array): SecretKey;
|
|
||||||
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;
|
|
||||||
static aggregate(publicKeys: PublicKey[]): PublicKey;
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
export interface IBls {
|
export interface IBls {
|
||||||
SecretKey: {
|
SecretKey: {
|
||||||
fromBytes(bytes: Uint8Array): ISecretKey;
|
fromBytes(bytes: Uint8Array): SecretKey;
|
||||||
fromHex(hex: string): ISecretKey;
|
fromHex(hex: string): SecretKey;
|
||||||
fromKeygen(ikm?: Uint8Array): ISecretKey;
|
fromKeygen(ikm?: Uint8Array): SecretKey;
|
||||||
};
|
};
|
||||||
PublicKey: {
|
PublicKey: {
|
||||||
fromBytes(bytes: Uint8Array): IPublicKey;
|
fromBytes(bytes: Uint8Array): PublicKey;
|
||||||
fromHex(hex: string): IPublicKey;
|
fromHex(hex: string): PublicKey;
|
||||||
aggregate(publicKeys: IPublicKey[]): IPublicKey;
|
aggregate(publicKeys: PublicKey[]): PublicKey;
|
||||||
};
|
};
|
||||||
Signature: {
|
Signature: {
|
||||||
fromBytes(bytes: Uint8Array): ISignature;
|
fromBytes(bytes: Uint8Array): Signature;
|
||||||
fromHex(hex: string): ISignature;
|
fromHex(hex: string): Signature;
|
||||||
aggregate(signatures: ISignature[]): ISignature;
|
aggregate(signatures: Signature[]): Signature;
|
||||||
};
|
};
|
||||||
|
|
||||||
sign(secretKey: Uint8Array, message: Uint8Array): Uint8Array;
|
sign(secretKey: Uint8Array, message: Uint8Array): Uint8Array;
|
||||||
|
@ -27,27 +27,36 @@ export interface IBls {
|
||||||
destroy(): void;
|
destroy(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export declare class SecretKey {
|
||||||
|
static fromBytes(bytes: Uint8Array): SecretKey;
|
||||||
|
static fromHex(hex: string): SecretKey;
|
||||||
|
static fromKeygen(entropy?: Uint8Array): SecretKey;
|
||||||
|
sign(message: Uint8Array): Signature;
|
||||||
|
toPublicKey(): PublicKey;
|
||||||
|
toBytes(): Uint8Array;
|
||||||
|
toHex(): string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export declare class PublicKey {
|
||||||
|
static fromBytes(bytes: Uint8Array): PublicKey;
|
||||||
|
static fromHex(hex: string): PublicKey;
|
||||||
|
static aggregate(publicKeys: PublicKey[]): PublicKey;
|
||||||
|
toBytes(): Uint8Array;
|
||||||
|
toHex(): string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export declare class Signature {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
export interface IKeypair {
|
export interface IKeypair {
|
||||||
publicKey: IPublicKey;
|
publicKey: PublicKey;
|
||||||
secretKey: ISecretKey;
|
secretKey: SecretKey;
|
||||||
}
|
|
||||||
|
|
||||||
export interface IPublicKey {
|
|
||||||
toBytes(): Uint8Array;
|
|
||||||
toHex(): string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ISignature {
|
|
||||||
toBytes(): Uint8Array;
|
|
||||||
toHex(): string;
|
|
||||||
verify(publicKey: IPublicKey, message: Uint8Array): boolean;
|
|
||||||
verifyAggregate(publicKeys: IPublicKey[], message: Uint8Array): boolean;
|
|
||||||
verifyMultiple(publicKeys: IPublicKey[], messages: Uint8Array[]): boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ISecretKey {
|
|
||||||
toPublicKey(): IPublicKey;
|
|
||||||
sign(message: Uint8Array): ISignature;
|
|
||||||
toBytes(): Uint8Array;
|
|
||||||
toHex(): string;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import {runBenchmark} from "./runner";
|
import {runBenchmark} from "./runner";
|
||||||
import {runForAllImplementations} from "../switch";
|
import {runForAllImplementations} from "../switch";
|
||||||
import {IPublicKey, ISignature} from "../../src/interface";
|
import {PublicKey, Signature} from "../../src/interface";
|
||||||
import {range, randomMessage} from "../util";
|
import {range, randomMessage} from "../util";
|
||||||
|
|
||||||
const aggCount = 30;
|
const aggCount = 30;
|
||||||
|
@ -9,7 +9,7 @@ const aggCount = 30;
|
||||||
await runForAllImplementations(async (bls, implementation) => {
|
await runForAllImplementations(async (bls, implementation) => {
|
||||||
// verify
|
// verify
|
||||||
|
|
||||||
await runBenchmark<{pk: IPublicKey; msg: Uint8Array; sig: ISignature}, boolean>({
|
await runBenchmark<{pk: PublicKey; msg: Uint8Array; sig: Signature}, boolean>({
|
||||||
id: `${implementation} verify`,
|
id: `${implementation} verify`,
|
||||||
|
|
||||||
prepareTest: () => {
|
prepareTest: () => {
|
||||||
|
@ -29,7 +29,7 @@ const aggCount = 30;
|
||||||
|
|
||||||
// Fast aggregate
|
// Fast aggregate
|
||||||
|
|
||||||
await runBenchmark<{pks: IPublicKey[]; msg: Uint8Array; sig: ISignature}, boolean>({
|
await runBenchmark<{pks: PublicKey[]; msg: Uint8Array; sig: Signature}, boolean>({
|
||||||
id: `${implementation} verifyAggregate`,
|
id: `${implementation} verifyAggregate`,
|
||||||
|
|
||||||
prepareTest: () => {
|
prepareTest: () => {
|
||||||
|
@ -56,7 +56,7 @@ const aggCount = 30;
|
||||||
|
|
||||||
// Aggregate pubkeys
|
// Aggregate pubkeys
|
||||||
|
|
||||||
await runBenchmark<IPublicKey[], void>({
|
await runBenchmark<PublicKey[], void>({
|
||||||
id: `${implementation} aggregate pubkeys (${aggCount})`,
|
id: `${implementation} aggregate pubkeys (${aggCount})`,
|
||||||
|
|
||||||
prepareTest: () => {
|
prepareTest: () => {
|
||||||
|
@ -71,7 +71,7 @@ const aggCount = 30;
|
||||||
|
|
||||||
// Aggregate sigs
|
// Aggregate sigs
|
||||||
|
|
||||||
await runBenchmark<ISignature[], void>({
|
await runBenchmark<Signature[], void>({
|
||||||
id: `${implementation} aggregate signatures (${aggCount})`,
|
id: `${implementation} aggregate signatures (${aggCount})`,
|
||||||
|
|
||||||
prepareTest: () => {
|
prepareTest: () => {
|
||||||
|
|
|
@ -1,10 +1,17 @@
|
||||||
import {expect} from "chai";
|
import {expect} from "chai";
|
||||||
import {SecretKey, PublicKey, Signature, init} from "../../src";
|
import {SecretKey, PublicKey, Signature, init, bls} from "../../src";
|
||||||
|
|
||||||
describe("index named exports", () => {
|
describe("index named exports", () => {
|
||||||
it("Classes and methods should be defined", async () => {
|
it("Classes and methods should be defined", async () => {
|
||||||
await init("herumi");
|
await init("herumi");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sample helper to test argument typing
|
||||||
|
*/
|
||||||
|
function verifyHelper(pk: PublicKey, sig: Signature, msg: Uint8Array): boolean {
|
||||||
|
return sig.verify(pk, msg);
|
||||||
|
}
|
||||||
|
|
||||||
const sk = SecretKey.fromKeygen();
|
const sk = SecretKey.fromKeygen();
|
||||||
const msg = new Uint8Array(32);
|
const msg = new Uint8Array(32);
|
||||||
const sig = sk.sign(msg);
|
const sig = sk.sign(msg);
|
||||||
|
@ -12,10 +19,11 @@ describe("index named exports", () => {
|
||||||
expect(verifyHelper(pk, sig, msg)).to.be.true;
|
expect(verifyHelper(pk, sig, msg)).to.be.true;
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
it("Make sure exported classes are compatible with interface", () => {
|
||||||
* Sample helper to test argument typing
|
const sk: SecretKey = bls.SecretKey.fromKeygen();
|
||||||
*/
|
const pk: PublicKey = sk.toPublicKey();
|
||||||
function verifyHelper(pk: PublicKey, sig: Signature, msg: Uint8Array): boolean {
|
const sig: Signature = sk.sign(new Uint8Array(32));
|
||||||
return sig.verify(pk, msg);
|
pk;
|
||||||
}
|
sig;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Reference in New Issue