From b5b2e6dfc7b692ab8c1ab0a9ee2f4f20b7d8b31f Mon Sep 17 00:00:00 2001 From: Cayman Date: Mon, 30 Nov 2020 12:01:13 -0600 Subject: [PATCH] Consistent naming --- src/blst/index.ts | 8 +++---- src/blst/publicKey.ts | 6 ++--- src/blst/{privateKey.ts => secretKey.ts} | 18 +++++++-------- src/blst/signature.ts | 4 ++-- src/errors.ts | 4 ++-- src/functional.ts | 13 +++++------ src/herumi/index.ts | 8 +++---- src/herumi/publicKey.ts | 8 +++---- src/herumi/{privateKey.ts => secretKey.ts} | 18 +++++++-------- src/index.ts | 19 ++++++++-------- src/interface.ts | 16 ++++++------- test/benchmark/index.ts | 8 +++---- test/spec/sign.test.ts | 4 ++-- test/unit/index-named-exports.test.ts | 4 ++-- test/unit/index.test.ts | 10 ++++----- test/unit/privateKey.test.ts | 26 ---------------------- test/unit/publicKey.test.ts | 4 ++-- test/unit/run-all-implementations.test.ts | 4 ++-- test/unit/run-web-implementation.test.ts | 4 ++-- test/unit/secretKey.test.ts | 26 ++++++++++++++++++++++ 20 files changed, 106 insertions(+), 106 deletions(-) rename src/blst/{privateKey.ts => secretKey.ts} (72%) rename src/herumi/{privateKey.ts => secretKey.ts} (70%) delete mode 100644 test/unit/privateKey.test.ts create mode 100644 test/unit/secretKey.test.ts diff --git a/src/blst/index.ts b/src/blst/index.ts index 88afe2b..478804c 100644 --- a/src/blst/index.ts +++ b/src/blst/index.ts @@ -1,11 +1,11 @@ -import {PrivateKey} from "./privateKey"; +import {SecretKey} from "./secretKey"; import {PublicKey} from "./publicKey"; import {Signature} from "./signature"; import {IBls} from "../interface"; import {functionalInterfaceFactory} from "../functional"; export * from "../constants"; -export {PrivateKey, PublicKey, Signature}; +export {SecretKey, PublicKey, Signature}; export async function init(): Promise { // Native bindings require no init() call @@ -15,11 +15,11 @@ export function destroy(): void { } export const bls: IBls = { - PrivateKey, + SecretKey, PublicKey, Signature, - ...functionalInterfaceFactory({PrivateKey, PublicKey, Signature}), + ...functionalInterfaceFactory({SecretKey, PublicKey, Signature}), init, destroy, }; diff --git a/src/blst/publicKey.ts b/src/blst/publicKey.ts index 39c3684..02dd1db 100644 --- a/src/blst/publicKey.ts +++ b/src/blst/publicKey.ts @@ -26,12 +26,12 @@ export class PublicKey implements IPublicKey { return this.fromBytes(hexToBytes(hex)); } - static aggregate(pubkeys: PublicKey[]): PublicKey { - if (pubkeys.length === 0) { + static aggregate(publicKeys: PublicKey[]): PublicKey { + if (publicKeys.length === 0) { throw new EmptyAggregateError(); } - const jacobian = blst.aggregatePubkeys(pubkeys.map((pk) => pk.jacobian)); + const jacobian = blst.aggregatePubkeys(publicKeys.map((pk) => pk.jacobian)); const affine = jacobian.toPublicKey(); return new PublicKey(affine, jacobian); } diff --git a/src/blst/privateKey.ts b/src/blst/secretKey.ts similarity index 72% rename from src/blst/privateKey.ts rename to src/blst/secretKey.ts index 4902473..4a83d2b 100644 --- a/src/blst/privateKey.ts +++ b/src/blst/secretKey.ts @@ -1,35 +1,35 @@ import * as blst from "@chainsafe/blst"; import {bytesToHex, hexToBytes, isZeroUint8Array, randomBytes} from "../helpers"; import {SECRET_KEY_LENGTH} from "../constants"; -import {IPrivateKey} from "../interface"; +import {ISecretKey} from "../interface"; import {PublicKey} from "./publicKey"; import {Signature} from "./signature"; -import {ZeroPrivateKeyError} from "../errors"; +import {ZeroSecretKeyError} from "../errors"; -export class PrivateKey implements IPrivateKey { +export class SecretKey implements ISecretKey { readonly value: blst.SecretKey; constructor(value: blst.SecretKey) { this.value = value; } - static fromBytes(bytes: Uint8Array): PrivateKey { + static fromBytes(bytes: Uint8Array): SecretKey { // draft-irtf-cfrg-bls-signature-04 does not allow SK == 0 if (isZeroUint8Array(bytes)) { - throw new ZeroPrivateKeyError(); + throw new ZeroSecretKeyError(); } const sk = blst.SecretKey.fromBytes(bytes); - return new PrivateKey(sk); + return new SecretKey(sk); } - static fromHex(hex: string): PrivateKey { + static fromHex(hex: string): SecretKey { return this.fromBytes(hexToBytes(hex)); } - static fromKeygen(entropy?: Uint8Array): PrivateKey { + static fromKeygen(entropy?: Uint8Array): SecretKey { const sk = blst.SecretKey.fromKeygen(entropy || randomBytes(SECRET_KEY_LENGTH)); - return new PrivateKey(sk); + return new SecretKey(sk); } sign(message: Uint8Array): Signature { diff --git a/src/blst/signature.ts b/src/blst/signature.ts index 3a476fd..6c9a46d 100644 --- a/src/blst/signature.ts +++ b/src/blst/signature.ts @@ -58,8 +58,8 @@ export class Signature implements ISignature { } private aggregateVerify(msgs: Uint8Array[], pks: blst.PublicKey[]): boolean { - // If this set is simply an infinity signature and infinity pubkey then skip verification. - // This has the effect of always declaring that this sig/pubkey combination is valid. + // If this set is simply an infinity signature and infinity publicKey then skip verification. + // This has the effect of always declaring that this sig/publicKey combination is valid. // for Eth2.0 specs tests if (this.affine.value.is_inf() && pks.length === 1 && pks[0].value.is_inf()) { return true; diff --git a/src/errors.ts b/src/errors.ts index 58ac7b2..d95c3be 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -8,9 +8,9 @@ export class NotInitializedError extends Error { } } -export class ZeroPrivateKeyError extends Error { +export class ZeroSecretKeyError extends Error { constructor() { - super("ZERO_PRIVATE_KEY"); + super("ZERO_SECRET_KEY"); } } diff --git a/src/functional.ts b/src/functional.ts index a731a7d..7ff58e5 100644 --- a/src/functional.ts +++ b/src/functional.ts @@ -5,10 +5,10 @@ import {NotInitializedError} from "./errors"; // Returned type is enforced at each implementation's index // eslint-disable-next-line @typescript-eslint/explicit-function-return-type export function functionalInterfaceFactory({ - PrivateKey, + SecretKey, PublicKey, Signature, -}: Pick) { +}: Pick) { /** * Signs given message using secret key. * @param secretKey @@ -18,8 +18,7 @@ export function functionalInterfaceFactory({ validateBytes(secretKey, "secretKey"); validateBytes(message, "message"); - const privateKey = PrivateKey.fromBytes(secretKey); - return privateKey.sign(message).toBytes(); + return SecretKey.fromBytes(secretKey).sign(message).toBytes(); } /** @@ -35,7 +34,7 @@ export function functionalInterfaceFactory({ * Combines all given public keys into single one * @param publicKeys */ - function aggregatePubkeys(publicKeys: Uint8Array[]): Uint8Array { + function aggregatePublicKeys(publicKeys: Uint8Array[]): Uint8Array { const agg = PublicKey.aggregate(publicKeys.map((p) => PublicKey.fromBytes(p))); return agg.toBytes(); } @@ -72,7 +71,7 @@ export function functionalInterfaceFactory({ try { return Signature.fromBytes(signature).verifyAggregate( - publicKeys.map((pubkey) => PublicKey.fromBytes(pubkey)), + publicKeys.map((publicKey) => PublicKey.fromBytes(publicKey)), message ); } catch (e) { @@ -110,7 +109,7 @@ export function functionalInterfaceFactory({ return { sign, aggregateSignatures, - aggregatePubkeys, + aggregatePublicKeys, verify, verifyAggregate, verifyMultiple, diff --git a/src/herumi/index.ts b/src/herumi/index.ts index cfa4e22..bad6872 100644 --- a/src/herumi/index.ts +++ b/src/herumi/index.ts @@ -1,4 +1,4 @@ -import {PrivateKey} from "./privateKey"; +import {SecretKey} from "./secretKey"; import {PublicKey} from "./publicKey"; import {Signature} from "./signature"; import {init, destroy} from "./context"; @@ -6,14 +6,14 @@ import {IBls} from "../interface"; import {functionalInterfaceFactory} from "../functional"; export * from "../constants"; -export {PrivateKey, PublicKey, Signature, init, destroy}; +export {SecretKey, PublicKey, Signature, init, destroy}; export const bls: IBls = { - PrivateKey, + SecretKey, PublicKey, Signature, - ...functionalInterfaceFactory({PrivateKey, PublicKey, Signature}), + ...functionalInterfaceFactory({SecretKey, PublicKey, Signature}), init, destroy, }; diff --git a/src/herumi/publicKey.ts b/src/herumi/publicKey.ts index 34c8338..8f53950 100644 --- a/src/herumi/publicKey.ts +++ b/src/herumi/publicKey.ts @@ -33,13 +33,13 @@ export class PublicKey implements IPublicKey { return this.fromBytes(hexToBytes(hex)); } - static aggregate(pubkeys: PublicKey[]): PublicKey { - if (pubkeys.length === 0) { + static aggregate(publicKeys: PublicKey[]): PublicKey { + if (publicKeys.length === 0) { throw new EmptyAggregateError(); } - const agg = new PublicKey(pubkeys[0].value.clone()); - for (const pk of pubkeys.slice(1)) { + const agg = new PublicKey(publicKeys[0].value.clone()); + for (const pk of publicKeys.slice(1)) { agg.value.add(pk.value); } return agg; diff --git a/src/herumi/privateKey.ts b/src/herumi/secretKey.ts similarity index 70% rename from src/herumi/privateKey.ts rename to src/herumi/secretKey.ts index 7eb9df6..f38ef9d 100644 --- a/src/herumi/privateKey.ts +++ b/src/herumi/secretKey.ts @@ -5,36 +5,36 @@ import {getContext} from "./context"; import {PublicKey} from "./publicKey"; import {Signature} from "./signature"; import {bytesToHex, hexToBytes} from "../helpers"; -import {IPrivateKey} from "../interface"; -import {InvalidLengthError, ZeroPrivateKeyError} from "../errors"; +import {ISecretKey} from "../interface"; +import {InvalidLengthError, ZeroSecretKeyError} from "../errors"; -export class PrivateKey implements IPrivateKey { +export class SecretKey implements ISecretKey { readonly value: SecretKeyType; constructor(value: SecretKeyType) { if (value.isZero()) { - throw new ZeroPrivateKeyError(); + throw new ZeroSecretKeyError(); } this.value = value; } - static fromBytes(bytes: Uint8Array): PrivateKey { + static fromBytes(bytes: Uint8Array): SecretKey { if (bytes.length !== SECRET_KEY_LENGTH) { - throw new InvalidLengthError("PrivateKey", SECRET_KEY_LENGTH); + throw new InvalidLengthError("SecretKey", SECRET_KEY_LENGTH); } const context = getContext(); const secretKey = new context.SecretKey(); secretKey.deserialize(bytes); - return new PrivateKey(secretKey); + return new SecretKey(secretKey); } - static fromHex(hex: string): PrivateKey { + static fromHex(hex: string): SecretKey { return this.fromBytes(hexToBytes(hex)); } - static fromKeygen(entropy?: Uint8Array): PrivateKey { + static fromKeygen(entropy?: Uint8Array): SecretKey { const sk = generateRandomSecretKey(entropy && Buffer.from(entropy)); return this.fromBytes(sk); } diff --git a/src/index.ts b/src/index.ts index 0ac5fa1..9243d00 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import {IBls, IPrivateKey, IPublicKey, ISignature} from "./interface"; +import {IBls, ISecretKey, IPublicKey, ISignature} from "./interface"; import {bls as blsHerumi} from "./herumi"; export type Implementation = "herumi" | "blst-native"; @@ -40,15 +40,15 @@ export async function init(impl: Implementation): Promise { // Proxy named exports, will get set by `Object.assign(exports, blsImpl)` export declare let sign: IBls["sign"]; export declare let aggregateSignatures: IBls["aggregateSignatures"]; -export declare let aggregatePubkeys: IBls["aggregatePubkeys"]; +export declare let aggregatePublicKeys: IBls["aggregatePublicKeys"]; export declare let verify: IBls["verify"]; export declare let verifyAggregate: IBls["verifyAggregate"]; export declare let verifyMultiple: IBls["verifyMultiple"]; -export declare class PrivateKey implements IPrivateKey { - static fromBytes(bytes: Uint8Array): PrivateKey; - static fromHex(hex: string): PrivateKey; - static fromKeygen(entropy?: Uint8Array): PrivateKey; +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; @@ -56,13 +56,14 @@ export declare class PrivateKey implements IPrivateKey { } export declare class PublicKey implements IPublicKey { + // Virtual property so PublicKey type != Signature type + private isPublicKey: true; + static fromBytes(bytes: Uint8Array): PublicKey; static fromHex(hex: string): PublicKey; - static aggregate(pubkeys: PublicKey[]): PublicKey; + static aggregate(publicKeys: PublicKey[]): PublicKey; toBytes(): Uint8Array; toHex(): string; - // Virtual property so PublicKey type != Signature type - private isPublicKey: true; } export declare class Signature implements ISignature { diff --git a/src/interface.ts b/src/interface.ts index 7035717..bc00966 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -1,13 +1,13 @@ export interface IBls { - PrivateKey: { - fromBytes(bytes: Uint8Array): IPrivateKey; - fromHex(hex: string): IPrivateKey; - fromKeygen(ikm?: Uint8Array): IPrivateKey; + SecretKey: { + fromBytes(bytes: Uint8Array): ISecretKey; + fromHex(hex: string): ISecretKey; + fromKeygen(ikm?: Uint8Array): ISecretKey; }; PublicKey: { fromBytes(bytes: Uint8Array): IPublicKey; fromHex(hex: string): IPublicKey; - aggregate(pubkeys: IPublicKey[]): IPublicKey; + aggregate(publicKeys: IPublicKey[]): IPublicKey; }; Signature: { fromBytes(bytes: Uint8Array): ISignature; @@ -16,7 +16,7 @@ export interface IBls { }; sign(secretKey: Uint8Array, message: Uint8Array): Uint8Array; - aggregatePubkeys(publicKeys: Uint8Array[]): Uint8Array; + aggregatePublicKeys(publicKeys: Uint8Array[]): Uint8Array; aggregateSignatures(signatures: Uint8Array[]): Uint8Array; verify(publicKey: Uint8Array, message: Uint8Array, signature: Uint8Array): boolean; verifyAggregate(publicKeys: Uint8Array[], message: Uint8Array, signature: Uint8Array): boolean; @@ -28,7 +28,7 @@ export interface IBls { export interface IKeypair { publicKey: IPublicKey; - privateKey: IPrivateKey; + secretKey: ISecretKey; } export interface IPublicKey { @@ -44,7 +44,7 @@ export interface ISignature { verifyMultiple(publicKeys: IPublicKey[], messages: Uint8Array[]): boolean; } -export interface IPrivateKey { +export interface ISecretKey { toPublicKey(): IPublicKey; sign(message: Uint8Array): ISignature; toBytes(): Uint8Array; diff --git a/test/benchmark/index.ts b/test/benchmark/index.ts index 9e0380f..f316e4e 100644 --- a/test/benchmark/index.ts +++ b/test/benchmark/index.ts @@ -13,7 +13,7 @@ runForAllImplementations((bls, implementation) => { prepareTest: () => { const msg = randomMsg(); - const sk = bls.PrivateKey.fromKeygen(); + const sk = bls.SecretKey.fromKeygen(); const pk = sk.toPublicKey(); const sig = sk.sign(msg); return { @@ -34,7 +34,7 @@ runForAllImplementations((bls, implementation) => { prepareTest: () => { const msg = randomMsg(); const dataArr = range(aggCount).map(() => { - const sk = bls.PrivateKey.fromKeygen(); + const sk = bls.SecretKey.fromKeygen(); const pk = sk.toPublicKey(); const sig = sk.sign(msg); return {pk, sig}; @@ -60,7 +60,7 @@ runForAllImplementations((bls, implementation) => { prepareTest: () => { return { - input: range(aggCount).map(() => bls.PrivateKey.fromKeygen().toPublicKey()), + input: range(aggCount).map(() => bls.SecretKey.fromKeygen().toPublicKey()), }; }, testRunner: (pks) => { @@ -76,7 +76,7 @@ runForAllImplementations((bls, implementation) => { prepareTest: () => { const msg = randomMsg(); const sigs = range(aggCount).map(() => { - const sk = bls.PrivateKey.fromKeygen(); + const sk = bls.SecretKey.fromKeygen(); return sk.sign(msg); }); return { diff --git a/test/spec/sign.test.ts b/test/spec/sign.test.ts index f8a9869..5ecc780 100644 --- a/test/spec/sign.test.ts +++ b/test/spec/sign.test.ts @@ -3,7 +3,7 @@ import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-tes import {bytesToHex, hexToBytes} from "../../src/helpers"; import {SPEC_TESTS_DIR} from "../params"; import {describeForAllImplementations} from "../switch"; -import {ZeroPrivateKeyError} from "../../src/errors"; +import {ZeroSecretKeyError} from "../../src/errors"; interface ISignMessageTestCase { data: { @@ -25,7 +25,7 @@ describeForAllImplementations((bls) => { const signature = bls.sign(hexToBytes(privkey), hexToBytes(message)); return bytesToHex(signature); } catch (e) { - if (e instanceof ZeroPrivateKeyError) return null; + if (e instanceof ZeroSecretKeyError) return null; else throw e; } }, diff --git a/test/unit/index-named-exports.test.ts b/test/unit/index-named-exports.test.ts index e383ebf..112a8a2 100644 --- a/test/unit/index-named-exports.test.ts +++ b/test/unit/index-named-exports.test.ts @@ -1,11 +1,11 @@ import {expect} from "chai"; -import {PrivateKey, PublicKey, Signature, init} from "../../src"; +import {SecretKey, PublicKey, Signature, init} from "../../src"; describe("index named exports", () => { it("Classes and methods should be defined", async () => { await init("herumi"); - const sk = PrivateKey.fromKeygen(); + const sk = SecretKey.fromKeygen(); const msg = new Uint8Array(32); const sig = sk.sign(msg); const pk = sk.toPublicKey(); diff --git a/test/unit/index.test.ts b/test/unit/index.test.ts index 68714ea..cf50d4a 100644 --- a/test/unit/index.test.ts +++ b/test/unit/index.test.ts @@ -5,7 +5,7 @@ import {getN, randomMessage} from "../util"; export function runIndexTests(bls: IBls): void { // eslint-disable-next-line @typescript-eslint/explicit-function-return-type function getRandomData() { - const sk = bls.PrivateKey.fromKeygen(); + const sk = bls.SecretKey.fromKeygen(); const pk = sk.toPublicKey(); const msg = randomMessage(); const sig = sk.sign(msg); @@ -47,15 +47,15 @@ export function runIndexTests(bls: IBls): void { describe("verify multiple", () => { it("should verify aggregated signatures", () => { - const sks = getN(4, () => bls.PrivateKey.fromKeygen()); + const sks = getN(4, () => bls.SecretKey.fromKeygen()); const msgs = getN(2, () => randomMessage()); const pks = sks.map((sk) => sk.toPublicKey()); const sigs = [sks[0].sign(msgs[0]), sks[1].sign(msgs[0]), sks[2].sign(msgs[1]), sks[3].sign(msgs[1])]; const aggPubkeys = [ - bls.aggregatePubkeys([pks[0], pks[1]].map((pk) => pk.toBytes())), - bls.aggregatePubkeys([pks[2], pks[3]].map((pk) => pk.toBytes())), + bls.aggregatePublicKeys([pks[0], pks[1]].map((pk) => pk.toBytes())), + bls.aggregatePublicKeys([pks[2], pks[3]].map((pk) => pk.toBytes())), ]; const aggSig = bls.aggregateSignatures(sigs.map((sig) => sig.toBytes())); @@ -67,7 +67,7 @@ export function runIndexTests(bls: IBls): void { it("should verify aggregated signatures - same message", () => { const n = 4; const msg = randomMessage(); - const sks = getN(n, () => bls.PrivateKey.fromKeygen()); + const sks = getN(n, () => bls.SecretKey.fromKeygen()); const pks = sks.map((sk) => sk.toPublicKey()); const sigs = sks.map((sk) => sk.sign(msg)); diff --git a/test/unit/privateKey.test.ts b/test/unit/privateKey.test.ts deleted file mode 100644 index b5060e2..0000000 --- a/test/unit/privateKey.test.ts +++ /dev/null @@ -1,26 +0,0 @@ -import {expect} from "chai"; -import {IBls} from "../../src/interface"; - -export function runPrivateKeyTests(bls: IBls): void { - describe("PrivateKey", () => { - it("should generate fromKeygen private key", () => { - const privateKey1 = bls.PrivateKey.fromKeygen(); - const privateKey2 = bls.PrivateKey.fromKeygen(); - expect(privateKey1.toHex()).to.not.be.equal(privateKey2.toHex()); - }); - - const privateKey = "0x07656fd676da43883d163f49566c72b9cbf0a5a294f26808c807700732456da7"; - - it("should export private key to hex string", () => { - expect(bls.PrivateKey.fromHex(privateKey).toHex()).to.be.equal(privateKey); - }); - - it("should export private key to hex string from non-prefixed hex", () => { - expect(bls.PrivateKey.fromHex(privateKey).toHex()).to.be.equal(privateKey); - }); - - it("should not accept too short private key", () => { - expect(() => bls.PrivateKey.fromHex("0x2123")).to.throw(); - }); - }); -} diff --git a/test/unit/publicKey.test.ts b/test/unit/publicKey.test.ts index 23a52c0..4111353 100644 --- a/test/unit/publicKey.test.ts +++ b/test/unit/publicKey.test.ts @@ -14,8 +14,8 @@ export function runPublicKeyTests(bls: IBls): void { expect(bls.PublicKey.fromHex(publicKey).toHex()).to.be.equal(publicKey); }); - it("from private key", () => { - bls.PrivateKey.fromKeygen().toPublicKey(); + it("from secret key", () => { + bls.SecretKey.fromKeygen().toPublicKey(); }); }); } diff --git a/test/unit/run-all-implementations.test.ts b/test/unit/run-all-implementations.test.ts index f42a3f1..7ebc0cf 100644 --- a/test/unit/run-all-implementations.test.ts +++ b/test/unit/run-all-implementations.test.ts @@ -1,4 +1,4 @@ -import {runPrivateKeyTests} from "./privateKey.test"; +import {runSecretKeyTests} from "./secretKey.test"; import {runPublicKeyTests} from "./publicKey.test"; // import {runKeypairTests} from "./keypair.test"; import {runIndexTests} from "./index.test"; @@ -6,7 +6,7 @@ import {describeForAllImplementations} from "../switch"; // Import test's bls lib lazily to prevent breaking test with Karma describeForAllImplementations((bls) => { - runPrivateKeyTests(bls); + runSecretKeyTests(bls); runPublicKeyTests(bls); // runKeypairTests(bls); runIndexTests(bls); diff --git a/test/unit/run-web-implementation.test.ts b/test/unit/run-web-implementation.test.ts index 159c0f2..764bacd 100644 --- a/test/unit/run-web-implementation.test.ts +++ b/test/unit/run-web-implementation.test.ts @@ -1,5 +1,5 @@ import herumi from "../../src/herumi"; -import {runPrivateKeyTests} from "./privateKey.test"; +import {runSecretKeyTests} from "./secretKey.test"; import {runPublicKeyTests} from "./publicKey.test"; // import {runKeypairTests} from "./keypair.test"; import {runIndexTests} from "./index.test"; @@ -14,7 +14,7 @@ describe("herumi", () => { await herumi.init(); }); - runPrivateKeyTests(herumi); + runSecretKeyTests(herumi); runPublicKeyTests(herumi); // runKeypairTests(bls); runIndexTests(herumi); diff --git a/test/unit/secretKey.test.ts b/test/unit/secretKey.test.ts new file mode 100644 index 0000000..f9dd440 --- /dev/null +++ b/test/unit/secretKey.test.ts @@ -0,0 +1,26 @@ +import {expect} from "chai"; +import {IBls} from "../../src/interface"; + +export function runSecretKeyTests(bls: IBls): void { + describe("SecretKey", () => { + it("should generate fromKeygen secret key", () => { + const secretKey1 = bls.SecretKey.fromKeygen(); + const secretKey2 = bls.SecretKey.fromKeygen(); + expect(secretKey1.toHex()).to.not.be.equal(secretKey2.toHex()); + }); + + const secretKey = "0x07656fd676da43883d163f49566c72b9cbf0a5a294f26808c807700732456da7"; + + it("should export secret key to hex string", () => { + expect(bls.SecretKey.fromHex(secretKey).toHex()).to.be.equal(secretKey); + }); + + it("should export secret key to hex string from non-prefixed hex", () => { + expect(bls.SecretKey.fromHex(secretKey).toHex()).to.be.equal(secretKey); + }); + + it("should not accept too short secret key", () => { + expect(() => bls.SecretKey.fromHex("0x2123")).to.throw(); + }); + }); +}