From 9829cc2cab90879ace0d4a2848b4a4bb6c6b2c50 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Tue, 10 May 2022 10:47:13 +1000 Subject: [PATCH 1/4] Use `@noble/secp2156k1`'s random byte util --- src/lib/crypto.ts | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/lib/crypto.ts b/src/lib/crypto.ts index cde7edc769..3985984caf 100644 --- a/src/lib/crypto.ts +++ b/src/lib/crypto.ts @@ -1,5 +1,6 @@ import nodeCrypto from "crypto"; +import * as secp from "@noble/secp256k1"; import { concat } from "uint8arrays/concat"; declare const self: Record | undefined; @@ -20,18 +21,7 @@ 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 const randomBytes = secp.utils.randomBytes; export async function sha256(...messages: Uint8Array[]): Promise { if (crypto.web) { From 50fa2d881df311d9066ada128983381cf0c3aeae Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Tue, 10 May 2022 10:59:27 +1000 Subject: [PATCH 2/4] Use `@noble/secp2156k1`'s sha3 util --- src/lib/crypto.ts | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/src/lib/crypto.ts b/src/lib/crypto.ts index 3985984caf..b563f1abd2 100644 --- a/src/lib/crypto.ts +++ b/src/lib/crypto.ts @@ -1,7 +1,6 @@ import nodeCrypto from "crypto"; import * as secp from "@noble/secp256k1"; -import { concat } from "uint8arrays/concat"; declare const self: Record | undefined; const crypto: { node?: any; web?: any } = { @@ -22,17 +21,4 @@ export function getSubtle(): SubtleCrypto { } export const randomBytes = secp.utils.randomBytes; - -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 sha256 = secp.utils.sha256; From 7a15fbf8f889517126a97edcceb804ba1daddf2a Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Tue, 10 May 2022 15:07:17 +1000 Subject: [PATCH 3/4] test: Ensure private key is not 0 --- src/lib/waku_message/index.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/waku_message/index.spec.ts b/src/lib/waku_message/index.spec.ts index 84084b78af..2403de9fb3 100644 --- a/src/lib/waku_message/index.spec.ts +++ b/src/lib/waku_message/index.spec.ts @@ -83,7 +83,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 +102,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); From c41dfcba405596e0c6a4c60b663f638a0f353df0 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Tue, 10 May 2022 15:24:53 +1000 Subject: [PATCH 4/4] test: Increase timeout --- src/lib/waku_message/index.spec.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lib/waku_message/index.spec.ts b/src/lib/waku_message/index.spec.ts index 2403de9fb3..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 }),