From 70574b45c124a367f4f1d831d2d44c327e00b244 Mon Sep 17 00:00:00 2001 From: dapplion Date: Sun, 29 Nov 2020 13:57:08 +0000 Subject: [PATCH] Fix herumi's bytes length checks --- src/herumi/privateKey.ts | 6 ++++-- src/herumi/publicKey.ts | 6 +++++- src/herumi/signature.ts | 11 ++++++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/herumi/privateKey.ts b/src/herumi/privateKey.ts index 177d886..6fcfb7f 100644 --- a/src/herumi/privateKey.ts +++ b/src/herumi/privateKey.ts @@ -1,4 +1,3 @@ -import assert from "assert"; import {SecretKeyType} from "bls-eth-wasm"; import {generateRandomSecretKey} from "@chainsafe/bls-keygen"; import {SECRET_KEY_LENGTH} from "../constants"; @@ -16,7 +15,10 @@ export class PrivateKey implements IPrivateKey { } static fromBytes(bytes: Uint8Array): PrivateKey { - assert(bytes.length === SECRET_KEY_LENGTH, "Private key should have 32 bytes"); + if (bytes.length !== SECRET_KEY_LENGTH) { + throw Error(`Private key should have ${SECRET_KEY_LENGTH} bytes`); + } + const context = getContext(); const secretKey = new context.SecretKey(); secretKey.deserialize(bytes); diff --git a/src/herumi/publicKey.ts b/src/herumi/publicKey.ts index c927728..a9a40a5 100644 --- a/src/herumi/publicKey.ts +++ b/src/herumi/publicKey.ts @@ -1,6 +1,6 @@ import {PublicKeyType} from "bls-eth-wasm"; import {getContext} from "./context"; -import {EMPTY_PUBLIC_KEY} from "../constants"; +import {EMPTY_PUBLIC_KEY, PUBLIC_KEY_LENGTH} from "../constants"; import {Signature} from "./signature"; import {bytesToHex, hexToBytes, isEqualBytes} from "../helpers"; import {IPublicKey} from "../interface"; @@ -13,6 +13,10 @@ export class PublicKey implements IPublicKey { } static fromBytes(bytes: Uint8Array): PublicKey { + if (bytes.length !== PUBLIC_KEY_LENGTH) { + throw Error(`Public key must have ${PUBLIC_KEY_LENGTH} bytes`); + } + const context = getContext(); const publicKey = new context.PublicKey(); if (!isEqualBytes(EMPTY_PUBLIC_KEY, bytes)) { diff --git a/src/herumi/signature.ts b/src/herumi/signature.ts index 5b16719..e527cf7 100644 --- a/src/herumi/signature.ts +++ b/src/herumi/signature.ts @@ -1,4 +1,3 @@ -import assert from "assert"; import {SIGNATURE_LENGTH, EMPTY_SIGNATURE} from "../constants"; import {SignatureType} from "bls-eth-wasm"; import {getContext} from "./context"; @@ -10,12 +9,18 @@ export class Signature implements ISignature { readonly value: SignatureType; constructor(value: SignatureType) { + if (!value.isValidOrder()) { + throw Error("Signature is not in valid order"); + } + this.value = value; - assert(this.value.isValidOrder()); } static fromBytes(bytes: Uint8Array): Signature { - assert(bytes.length === SIGNATURE_LENGTH, `Signature must have ${SIGNATURE_LENGTH} bytes`); + if (bytes.length !== SIGNATURE_LENGTH) { + throw Error(`Signature must have ${SIGNATURE_LENGTH} bytes`); + } + const context = getContext(); const signature = new context.Signature(); if (!isEqualBytes(EMPTY_SIGNATURE, bytes)) {