Fix herumi tests

This commit is contained in:
dapplion 2020-11-29 23:35:42 +00:00
parent d3616e50f6
commit 6fe9b09a92
4 changed files with 31 additions and 3 deletions

View File

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

View File

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

View File

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

View File

@ -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) => {
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},