From 6fe9b09a92285f2a21721bab9fe2966707923b20 Mon Sep 17 00:00:00 2001 From: dapplion Date: Sun, 29 Nov 2020 23:35:42 +0000 Subject: [PATCH] Fix herumi tests --- src/errors.ts | 12 ++++++++++++ src/herumi/privateKey.ts | 5 +++++ src/herumi/publicKey.ts | 5 +++++ test/spec/sign.test.ts | 12 +++++++++--- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/errors.ts b/src/errors.ts index 8666707..62b2daa 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -3,3 +3,15 @@ * by the functional interface try / catch blocks */ export class ExpectedError extends Error {} + +export class ZeroPrivateKeyError extends Error { + constructor() { + super("PRIVATE_KEY_IS_ZERO"); + } +} + +export class ZeroPublicKeyError extends Error { + constructor() { + super("PUBLIC_KEY_IS_ZERO"); + } +} diff --git a/src/herumi/privateKey.ts b/src/herumi/privateKey.ts index 6fcfb7f..cfa5438 100644 --- a/src/herumi/privateKey.ts +++ b/src/herumi/privateKey.ts @@ -6,11 +6,16 @@ import {PublicKey} from "./publicKey"; import {Signature} from "./signature"; import {bytesToHex, hexToBytes} from "../helpers"; import {IPrivateKey} from "../interface"; +import {ZeroPrivateKeyError} from "../errors"; export class PrivateKey implements IPrivateKey { readonly value: SecretKeyType; constructor(value: SecretKeyType) { + if (value.isZero()) { + throw new ZeroPrivateKeyError(); + } + this.value = value; } diff --git a/src/herumi/publicKey.ts b/src/herumi/publicKey.ts index 441193e..fa15485 100644 --- a/src/herumi/publicKey.ts +++ b/src/herumi/publicKey.ts @@ -3,11 +3,16 @@ import {getContext} from "./context"; import {EMPTY_PUBLIC_KEY, PUBLIC_KEY_LENGTH} from "../constants"; import {bytesToHex, hexToBytes, isEqualBytes} from "../helpers"; import {IPublicKey} from "../interface"; +import {ZeroPublicKeyError} from "../errors"; export class PublicKey implements IPublicKey { readonly value: PublicKeyType; constructor(value: PublicKeyType) { + if (value.isZero()) { + throw new ZeroPublicKeyError(); + } + this.value = value; } diff --git a/test/spec/sign.test.ts b/test/spec/sign.test.ts index a90f21b..f8a9869 100644 --- a/test/spec/sign.test.ts +++ b/test/spec/sign.test.ts @@ -3,6 +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"; interface ISignMessageTestCase { data: { @@ -19,9 +20,14 @@ describeForAllImplementations((bls) => { "bls/sign/small", path.join(SPEC_TESTS_DIR, "general/phase0/bls/sign/small"), (testCase) => { - const {privkey, message} = testCase.data.input; - const signature = bls.sign(hexToBytes(privkey), hexToBytes(message)); - return bytesToHex(signature); + try { + const {privkey, message} = testCase.data.input; + const signature = bls.sign(hexToBytes(privkey), hexToBytes(message)); + return bytesToHex(signature); + } catch (e) { + if (e instanceof ZeroPrivateKeyError) return null; + else throw e; + } }, { inputTypes: {data: InputType.YAML},