From 9829cc2cab90879ace0d4a2848b4a4bb6c6b2c50 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Tue, 10 May 2022 10:47:13 +1000 Subject: [PATCH] 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) {