From 12528acaeb301ee0f5b87bba787ca49c02711d1a Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Mon, 7 Mar 2022 07:51:36 +1100 Subject: [PATCH] Remove node crypto import --- src/lib/enr/keypair/secp256k1.ts | 16 +++------------- src/lib/enr/v4.ts | 20 +++++--------------- src/lib/waku_message/version_1.ts | 12 +----------- 3 files changed, 9 insertions(+), 39 deletions(-) diff --git a/src/lib/enr/keypair/secp256k1.ts b/src/lib/enr/keypair/secp256k1.ts index a8aa47816c..251b0d7cab 100644 --- a/src/lib/enr/keypair/secp256k1.ts +++ b/src/lib/enr/keypair/secp256k1.ts @@ -1,8 +1,8 @@ -import crypto from "crypto"; - import * as secp256k1 from "secp256k1"; import { concat } from "uint8arrays/concat"; +import { randomBytes } from "../../crypto"; + import { AbstractKeypair, IKeypair, IKeypairClass, KeypairType } from "./types"; export function secp256k1PublicKeyToCompressed( @@ -41,7 +41,7 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair } static async generate(): Promise { - const privateKey = await randomBytes(32); + const privateKey = randomBytes(32); const publicKey = secp256k1.publicKeyCreate(privateKey); return new Secp256k1Keypair(privateKey, publicKey); } @@ -69,13 +69,3 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair return secp256k1.ecdsaVerify(sig, msg, this.publicKey); } }; - -function randomBytes(length: number): Uint8Array { - if (typeof window !== "undefined" && window && window.crypto) { - const array = new Uint8Array(length); - window.crypto.getRandomValues(array); - return array; - } else { - return crypto.randomBytes(length); - } -} diff --git a/src/lib/enr/v4.ts b/src/lib/enr/v4.ts index 0738a30732..0e70dc2640 100644 --- a/src/lib/enr/v4.ts +++ b/src/lib/enr/v4.ts @@ -1,8 +1,8 @@ -import crypto from "crypto"; - import { keccak256 } from "js-sha3"; import * as secp256k1 from "secp256k1"; +import { randomBytes } from "../crypto"; + import { createNodeId } from "./create"; import { NodeId } from "./types"; @@ -10,7 +10,7 @@ export function hash(input: Uint8Array): Uint8Array { return new Uint8Array(keccak256.arrayBuffer(input)); } -export async function createPrivateKey(): Promise { +export function createPrivateKey(): Uint8Array { return randomBytes(32); } @@ -45,13 +45,13 @@ export class ENRKeyPair { public readonly publicKey: Uint8Array ) {} - public static async create(privateKey?: Uint8Array): Promise { + public static create(privateKey?: Uint8Array): ENRKeyPair { if (privateKey) { if (!secp256k1.privateKeyVerify(privateKey)) { throw new Error("Invalid private key"); } } - const _privateKey = privateKey || (await createPrivateKey()); + const _privateKey = privateKey || createPrivateKey(); const _publicKey = publicKey(_privateKey); const _nodeId = nodeId(_publicKey); @@ -66,13 +66,3 @@ export class ENRKeyPair { return verify(this.publicKey, msg, sig); } } - -function randomBytes(length: number): Uint8Array { - if (typeof window !== "undefined" && window && window.crypto) { - const array = new Uint8Array(length); - window.crypto.getRandomValues(array); - return array; - } else { - return crypto.randomBytes(length); - } -} diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index 535c232484..db4883ea4d 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -1,9 +1,9 @@ import { Buffer } from "buffer"; -import * as crypto from "crypto"; import { keccak256 } from "js-sha3"; import * as secp256k1 from "secp256k1"; +import { randomBytes } from "../crypto"; import { hexToBytes } from "../utils"; import * as ecies from "./ecies"; @@ -258,13 +258,3 @@ function ecRecoverPubKey(messageHash: string, signature: Buffer): Uint8Array { false ); } - -function randomBytes(length: number): Uint8Array { - if (typeof window !== "undefined" && window && window.crypto) { - const array = new Uint8Array(length); - window.crypto.getRandomValues(array); - return array; - } else { - return crypto.randomBytes(length); - } -}