mirror of https://github.com/status-im/js-waku.git
Remove node crypto import
This commit is contained in:
parent
111f31bb53
commit
12528acaeb
|
@ -1,8 +1,8 @@
|
||||||
import crypto from "crypto";
|
|
||||||
|
|
||||||
import * as secp256k1 from "secp256k1";
|
import * as secp256k1 from "secp256k1";
|
||||||
import { concat } from "uint8arrays/concat";
|
import { concat } from "uint8arrays/concat";
|
||||||
|
|
||||||
|
import { randomBytes } from "../../crypto";
|
||||||
|
|
||||||
import { AbstractKeypair, IKeypair, IKeypairClass, KeypairType } from "./types";
|
import { AbstractKeypair, IKeypair, IKeypairClass, KeypairType } from "./types";
|
||||||
|
|
||||||
export function secp256k1PublicKeyToCompressed(
|
export function secp256k1PublicKeyToCompressed(
|
||||||
|
@ -41,7 +41,7 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair
|
||||||
}
|
}
|
||||||
|
|
||||||
static async generate(): Promise<Secp256k1Keypair> {
|
static async generate(): Promise<Secp256k1Keypair> {
|
||||||
const privateKey = await randomBytes(32);
|
const privateKey = randomBytes(32);
|
||||||
const publicKey = secp256k1.publicKeyCreate(privateKey);
|
const publicKey = secp256k1.publicKeyCreate(privateKey);
|
||||||
return new Secp256k1Keypair(privateKey, publicKey);
|
return new Secp256k1Keypair(privateKey, publicKey);
|
||||||
}
|
}
|
||||||
|
@ -69,13 +69,3 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair
|
||||||
return secp256k1.ecdsaVerify(sig, msg, this.publicKey);
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import crypto from "crypto";
|
|
||||||
|
|
||||||
import { keccak256 } from "js-sha3";
|
import { keccak256 } from "js-sha3";
|
||||||
import * as secp256k1 from "secp256k1";
|
import * as secp256k1 from "secp256k1";
|
||||||
|
|
||||||
|
import { randomBytes } from "../crypto";
|
||||||
|
|
||||||
import { createNodeId } from "./create";
|
import { createNodeId } from "./create";
|
||||||
import { NodeId } from "./types";
|
import { NodeId } from "./types";
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ export function hash(input: Uint8Array): Uint8Array {
|
||||||
return new Uint8Array(keccak256.arrayBuffer(input));
|
return new Uint8Array(keccak256.arrayBuffer(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createPrivateKey(): Promise<Uint8Array> {
|
export function createPrivateKey(): Uint8Array {
|
||||||
return randomBytes(32);
|
return randomBytes(32);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,13 +45,13 @@ export class ENRKeyPair {
|
||||||
public readonly publicKey: Uint8Array
|
public readonly publicKey: Uint8Array
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public static async create(privateKey?: Uint8Array): Promise<ENRKeyPair> {
|
public static create(privateKey?: Uint8Array): ENRKeyPair {
|
||||||
if (privateKey) {
|
if (privateKey) {
|
||||||
if (!secp256k1.privateKeyVerify(privateKey)) {
|
if (!secp256k1.privateKeyVerify(privateKey)) {
|
||||||
throw new Error("Invalid private key");
|
throw new Error("Invalid private key");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const _privateKey = privateKey || (await createPrivateKey());
|
const _privateKey = privateKey || createPrivateKey();
|
||||||
const _publicKey = publicKey(_privateKey);
|
const _publicKey = publicKey(_privateKey);
|
||||||
const _nodeId = nodeId(_publicKey);
|
const _nodeId = nodeId(_publicKey);
|
||||||
|
|
||||||
|
@ -66,13 +66,3 @@ export class ENRKeyPair {
|
||||||
return verify(this.publicKey, msg, sig);
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
import * as crypto from "crypto";
|
|
||||||
|
|
||||||
import { keccak256 } from "js-sha3";
|
import { keccak256 } from "js-sha3";
|
||||||
import * as secp256k1 from "secp256k1";
|
import * as secp256k1 from "secp256k1";
|
||||||
|
|
||||||
|
import { randomBytes } from "../crypto";
|
||||||
import { hexToBytes } from "../utils";
|
import { hexToBytes } from "../utils";
|
||||||
|
|
||||||
import * as ecies from "./ecies";
|
import * as ecies from "./ecies";
|
||||||
|
@ -258,13 +258,3 @@ function ecRecoverPubKey(messageHash: string, signature: Buffer): Uint8Array {
|
||||||
false
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue