Skip to content

Commit

Permalink
chore: use native node:crypto (#151)
Browse files Browse the repository at this point in the history
* chore: use native node:crypto

* fix: workaround webpack limitation
  • Loading branch information
jeluard authored Feb 8, 2024
1 parent d79efcc commit 169f0a3
Show file tree
Hide file tree
Showing 6 changed files with 581 additions and 199 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@
},
"dependencies": {
"@chainsafe/bls-keygen": "^0.4.0",
"bls-eth-wasm": "^0.4.8",
"randombytes": "^2.1.0"
"bls-eth-wasm": "^0.4.8"
},
"devDependencies": {
"@chainsafe/blst": "^0.2.4",
Expand All @@ -89,11 +88,12 @@
"@lodestar/spec-test-util": "1.13.0",
"@types/chai": "^4.2.9",
"@types/mocha": "^10.0.0",
"@types/randombytes": "^2.0.0",
"@types/node": "^20.11.13",
"@typescript-eslint/eslint-plugin": "^4.31.1",
"@typescript-eslint/parser": "^4.31.1",
"buffer": "^6.0.3",
"chai": "^4.3.6",
"crypto-browserify": "^3.12.0",
"eslint": "^7.14.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-prettier": "^3.1.4",
Expand Down
5 changes: 3 additions & 2 deletions src/blst-native/secretKey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as blst from "@chainsafe/blst";
import {bytesToHex, hexToBytes, isZeroUint8Array, randomBytes} from "../helpers/index.js";
import crypto from "crypto";
import {bytesToHex, hexToBytes, isZeroUint8Array} from "../helpers/index.js";
import {SECRET_KEY_LENGTH} from "../constants.js";
import {SecretKey as ISecretKey} from "../types.js";
import {PublicKey} from "./publicKey.js";
Expand Down Expand Up @@ -27,7 +28,7 @@ export class SecretKey implements ISecretKey {
}

static fromKeygen(entropy?: Uint8Array): SecretKey {
const sk = blst.SecretKey.fromKeygen(entropy || randomBytes(SECRET_KEY_LENGTH));
const sk = blst.SecretKey.fromKeygen(entropy || crypto.randomBytes(SECRET_KEY_LENGTH));
return new SecretKey(sk);
}

Expand Down
5 changes: 0 additions & 5 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import randomBytes from "randombytes";

// Single import to ease changing this lib if necessary
export {randomBytes};

/**
* Validate bytes to prevent confusing WASM errors downstream if bytes is null
*/
Expand Down
4 changes: 2 additions & 2 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {randomBytes} from "../src/helpers/index.js";
import crypto from "crypto";

export function randomMessage(): Uint8Array {
return randomBytes(32);
return crypto.randomBytes(32);
}

export function getN<T>(n: number, getter: () => T): T[] {
Expand Down
5 changes: 4 additions & 1 deletion webpack.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ module.exports = {
devtool: "source-map",
resolve: {
plugins: [new ResolveTypeScriptPlugin()],
alias: {
"crypto": "crypto-browserify"
},
fallback: {
crypto: false,
fs: false,
path: false,
stream: false
},
},
experiments: {
Expand Down
Loading

0 comments on commit 169f0a3

Please sign in to comment.