From 1e9f778846e2dfb94f4562a730d94a2105d09a2c Mon Sep 17 00:00:00 2001 From: dapplion Date: Fri, 20 Nov 2020 09:37:44 +0000 Subject: [PATCH] Use isEqualBytes helper --- src/helpers/utils.ts | 4 ++++ src/herumi/publicKey.ts | 4 ++-- src/herumi/signature.ts | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index 50a34d9..357078d 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -26,6 +26,10 @@ export function getRandomBytes(size: number): Uint8Array { return Uint8Array.from(crypto.randomBytes(size)); } +export function isEqualBytes(a: Buffer | Uint8Array, b: Buffer | Uint8Array): boolean { + return Buffer.from(a).equals(Buffer.from(b)); +} + export function toBuffer(input: Uint8Array): Buffer { return Buffer.from(input.buffer, input.byteOffset, input.length); } diff --git a/src/herumi/publicKey.ts b/src/herumi/publicKey.ts index 82dfe58..f104a09 100644 --- a/src/herumi/publicKey.ts +++ b/src/herumi/publicKey.ts @@ -2,7 +2,7 @@ import {PublicKeyType} from "@chainsafe/eth2-bls-wasm"; import {getContext} from "./context"; import {EMPTY_PUBLIC_KEY} from "../constants"; import {Signature} from "./signature"; -import {bytesToHex, hexToBytes} from "../helpers/utils"; +import {bytesToHex, hexToBytes, isEqualBytes} from "../helpers/utils"; export class PublicKey { readonly value: PublicKeyType; @@ -14,7 +14,7 @@ export class PublicKey { static fromBytes(bytes: Uint8Array): PublicKey { const context = getContext(); const publicKey = new context.PublicKey(); - if (!EMPTY_PUBLIC_KEY.equals(bytes)) { + if (!isEqualBytes(EMPTY_PUBLIC_KEY, bytes)) { publicKey.deserialize(bytes); } return new PublicKey(publicKey); diff --git a/src/herumi/signature.ts b/src/herumi/signature.ts index 971e3d3..4995ba1 100644 --- a/src/herumi/signature.ts +++ b/src/herumi/signature.ts @@ -3,7 +3,7 @@ import {SIGNATURE_LENGTH, EMPTY_SIGNATURE} from "../constants"; import {SignatureType} from "@chainsafe/eth2-bls-wasm"; import {getContext} from "./context"; import {PublicKey} from "./publicKey"; -import {bytesToHex, hexToBytes} from "../helpers/utils"; +import {bytesToHex, hexToBytes, isEqualBytes} from "../helpers/utils"; export class Signature { readonly value: SignatureType; @@ -17,7 +17,7 @@ export class Signature { assert(bytes.length === SIGNATURE_LENGTH, `Signature must have ${SIGNATURE_LENGTH} bytes`); const context = getContext(); const signature = new context.Signature(); - if (!EMPTY_SIGNATURE.equals(bytes)) { + if (!isEqualBytes(EMPTY_SIGNATURE, bytes)) { signature.deserialize(bytes); } return new Signature(signature);