Remove node crypto import

This commit is contained in:
Franck Royer 2022-03-07 07:51:36 +11:00
parent 111f31bb53
commit 12528acaeb
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 9 additions and 39 deletions

View File

@ -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<Secp256k1Keypair> {
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);
}
}

View File

@ -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<Uint8Array> {
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<ENRKeyPair> {
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);
}
}

View File

@ -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);
}
}