diff --git a/src/lib/crypto.ts b/src/lib/crypto.ts index cde7edc769..b563f1abd2 100644 --- a/src/lib/crypto.ts +++ b/src/lib/crypto.ts @@ -1,6 +1,6 @@ import nodeCrypto from "crypto"; -import { concat } from "uint8arrays/concat"; +import * as secp from "@noble/secp256k1"; declare const self: Record | undefined; const crypto: { node?: any; web?: any } = { @@ -20,29 +20,5 @@ export function getSubtle(): SubtleCrypto { } } -export function randomBytes(bytesLength = 32): Uint8Array { - if (crypto.web) { - return crypto.web.getRandomValues(new Uint8Array(bytesLength)); - } else if (crypto.node) { - const { randomBytes } = crypto.node; - return Uint8Array.from(randomBytes(bytesLength)); - } else { - throw new Error( - "The environment doesn't have randomBytes function (if in the browser, be sure to use to be in a secure context, ie, https)" - ); - } -} - -export async function sha256(...messages: Uint8Array[]): Promise { - if (crypto.web) { - const buffer = await crypto.web.subtle.digest("SHA-256", concat(messages)); - return new Uint8Array(buffer); - } else if (crypto.node) { - const { createHash } = crypto.node; - const hash = createHash("sha256"); - messages.forEach((m) => hash.update(m)); - return Uint8Array.from(hash.digest()); - } else { - throw new Error("The environment doesn't have sha256 function"); - } -} +export const randomBytes = secp.utils.randomBytes; +export const sha256 = secp.utils.sha256; diff --git a/src/lib/waku_message/index.spec.ts b/src/lib/waku_message/index.spec.ts index 84084b78af..df9073398a 100644 --- a/src/lib/waku_message/index.spec.ts +++ b/src/lib/waku_message/index.spec.ts @@ -53,6 +53,8 @@ describe("Waku Message: Browser & Node", function () { }); it("Waku message round trip binary encryption [asymmetric, signature]", async function () { + this.timeout(4000); + await fc.assert( fc.asyncProperty( fc.uint8Array({ minLength: 1 }), @@ -83,7 +85,7 @@ describe("Waku Message: Browser & Node", function () { await fc.assert( fc.asyncProperty( fc.uint8Array({ minLength: 1 }), - fc.uint8Array({ minLength: 32, maxLength: 32 }), + fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }), async (payload, key) => { const msg = await WakuMessage.fromBytes(payload, TestContentTopic, { symKey: key, @@ -102,8 +104,8 @@ describe("Waku Message: Browser & Node", function () { await fc.assert( fc.asyncProperty( fc.uint8Array({ minLength: 1 }), - fc.uint8Array({ minLength: 32, maxLength: 32 }), - fc.uint8Array({ minLength: 32, maxLength: 32 }), + fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }), + fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }), async (payload, sigPrivKey, symKey) => { const sigPubKey = getPublicKey(sigPrivKey);