From 7f76672a40e4f606d98e136b70374226faed4cba Mon Sep 17 00:00:00 2001 From: dapplion Date: Thu, 3 Dec 2020 00:06:36 +0000 Subject: [PATCH] Use browser friendly concatUint8Arrays instead of Buffer.concat --- src/helpers/utils.ts | 14 ++++++++++++++ src/herumi/signature.ts | 5 ++--- test/unit/helpers/bytes.test.ts | 25 ++++++++++++++++++++----- test/unit/helpers/hex.test.ts | 5 +---- 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index 114ab7b..76eae27 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -20,3 +20,17 @@ export function validateBytes( export function isZeroUint8Array(bytes: Uint8Array): boolean { return bytes.every((byte) => byte === 0); } + +export function concatUint8Arrays(bytesArr: Uint8Array[]): Uint8Array { + const totalLen = bytesArr.reduce((total, bytes) => total + bytes.length, 0); + + const merged = new Uint8Array(totalLen); + let mergedLen = 0; + + for (const bytes of bytesArr) { + merged.set(bytes, mergedLen); + mergedLen += bytes.length; + } + + return merged; +} diff --git a/src/herumi/signature.ts b/src/herumi/signature.ts index cca6857..e8c41be 100644 --- a/src/herumi/signature.ts +++ b/src/herumi/signature.ts @@ -2,7 +2,7 @@ import {SIGNATURE_LENGTH} from "../constants"; import {SignatureType, multiVerify} from "bls-eth-wasm"; import {getContext} from "./context"; import {PublicKey} from "./publicKey"; -import {bytesToHex, hexToBytes, isZeroUint8Array} from "../helpers"; +import {bytesToHex, concatUint8Arrays, hexToBytes, isZeroUint8Array} from "../helpers"; import {Signature as ISignature} from "../interface"; import {EmptyAggregateError, InvalidLengthError, InvalidOrderError} from "../errors"; @@ -65,10 +65,9 @@ export class Signature implements ISignature { } verifyMultiple(publicKeys: PublicKey[], messages: Uint8Array[]): boolean { - const msgs = Buffer.concat(messages); return this.value.aggregateVerifyNoCheck( publicKeys.map((key) => key.value), - msgs + concatUint8Arrays(messages) ); } diff --git a/test/unit/helpers/bytes.test.ts b/test/unit/helpers/bytes.test.ts index 2b14df7..952aaa5 100644 --- a/test/unit/helpers/bytes.test.ts +++ b/test/unit/helpers/bytes.test.ts @@ -1,5 +1,6 @@ import {expect} from "chai"; -import {isZeroUint8Array} from "../../../src/helpers/utils"; +import {concatUint8Arrays, isZeroUint8Array} from "../../../src/helpers/utils"; +import {hexToBytesNode} from "../../util"; describe("helpers / bytes", () => { describe("isZeroUint8Array", () => { @@ -21,8 +22,22 @@ describe("helpers / bytes", () => { }); } }); -}); -function hexToBytesNode(hex: string): Buffer { - return Buffer.from(hex.replace("0x", ""), "hex"); -} + describe("concatUint8Arrays", () => { + it("Should merge multiple Uint8Array", () => { + const bytesArr = [ + new Uint8Array([1, 2, 3]), + new Uint8Array([4, 5]), + new Uint8Array([6]), + new Uint8Array([7, 8]), + new Uint8Array([9, 10, 11]), + ]; + + const expectedBytes = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); + + const bytes = concatUint8Arrays(bytesArr); + + expect(bytes.toString()).to.equal(expectedBytes.toString()); + }); + }); +}); diff --git a/test/unit/helpers/hex.test.ts b/test/unit/helpers/hex.test.ts index 93d5c09..cca61a3 100644 --- a/test/unit/helpers/hex.test.ts +++ b/test/unit/helpers/hex.test.ts @@ -1,5 +1,6 @@ import {expect} from "chai"; import {hexToBytes, bytesToHex} from "../../../src/helpers/hex"; +import {hexToBytesNode} from "../../util"; describe("helpers / hex", () => { const testCases: {id: string; hex: string}[] = [ @@ -23,7 +24,3 @@ describe("helpers / hex", () => { }); } }); - -function hexToBytesNode(hex: string): Buffer { - return Buffer.from(hex.replace("0x", ""), "hex"); -}