Fix herumi's bytes length checks

This commit is contained in:
dapplion 2020-11-29 13:57:08 +00:00
parent 78f66280de
commit 70574b45c1
3 changed files with 17 additions and 6 deletions

View File

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

View File

@ -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)) {

View File

@ -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)) {