Use isEqualBytes helper

This commit is contained in:
dapplion 2020-11-20 09:37:44 +00:00
parent fa12879651
commit 1e9f778846
3 changed files with 8 additions and 4 deletions

View File

@ -26,6 +26,10 @@ export function getRandomBytes(size: number): Uint8Array {
return Uint8Array.from(crypto.randomBytes(size)); 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 { export function toBuffer(input: Uint8Array): Buffer {
return Buffer.from(input.buffer, input.byteOffset, input.length); return Buffer.from(input.buffer, input.byteOffset, input.length);
} }

View File

@ -2,7 +2,7 @@ import {PublicKeyType} from "@chainsafe/eth2-bls-wasm";
import {getContext} from "./context"; import {getContext} from "./context";
import {EMPTY_PUBLIC_KEY} from "../constants"; import {EMPTY_PUBLIC_KEY} from "../constants";
import {Signature} from "./signature"; import {Signature} from "./signature";
import {bytesToHex, hexToBytes} from "../helpers/utils"; import {bytesToHex, hexToBytes, isEqualBytes} from "../helpers/utils";
export class PublicKey { export class PublicKey {
readonly value: PublicKeyType; readonly value: PublicKeyType;
@ -14,7 +14,7 @@ export class PublicKey {
static fromBytes(bytes: Uint8Array): PublicKey { static fromBytes(bytes: Uint8Array): PublicKey {
const context = getContext(); const context = getContext();
const publicKey = new context.PublicKey(); const publicKey = new context.PublicKey();
if (!EMPTY_PUBLIC_KEY.equals(bytes)) { if (!isEqualBytes(EMPTY_PUBLIC_KEY, bytes)) {
publicKey.deserialize(bytes); publicKey.deserialize(bytes);
} }
return new PublicKey(publicKey); return new PublicKey(publicKey);

View File

@ -3,7 +3,7 @@ import {SIGNATURE_LENGTH, EMPTY_SIGNATURE} from "../constants";
import {SignatureType} from "@chainsafe/eth2-bls-wasm"; import {SignatureType} from "@chainsafe/eth2-bls-wasm";
import {getContext} from "./context"; import {getContext} from "./context";
import {PublicKey} from "./publicKey"; import {PublicKey} from "./publicKey";
import {bytesToHex, hexToBytes} from "../helpers/utils"; import {bytesToHex, hexToBytes, isEqualBytes} from "../helpers/utils";
export class Signature { export class Signature {
readonly value: SignatureType; readonly value: SignatureType;
@ -17,7 +17,7 @@ export class Signature {
assert(bytes.length === SIGNATURE_LENGTH, `Signature must have ${SIGNATURE_LENGTH} bytes`); assert(bytes.length === SIGNATURE_LENGTH, `Signature must have ${SIGNATURE_LENGTH} bytes`);
const context = getContext(); const context = getContext();
const signature = new context.Signature(); const signature = new context.Signature();
if (!EMPTY_SIGNATURE.equals(bytes)) { if (!isEqualBytes(EMPTY_SIGNATURE, bytes)) {
signature.deserialize(bytes); signature.deserialize(bytes);
} }
return new Signature(signature); return new Signature(signature);