Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use Wallet.createRandom() in my npm package due to crypto package and SystemRandomSource #4646

Open
confxsd opened this issue Mar 13, 2024 · 1 comment
Assignees
Labels
discussion Questions, feedback and general information. v6 Issues regarding v6

Comments

@confxsd
Copy link

confxsd commented Mar 13, 2024

Ethers Version

6.11.1

Search Terms

crypto, SystemRandomSource, randomBytes

Describe the Problem

Hello ethers team,

I am encountering an issue when using the ethers library in a UMD environment that targets both browser and server. The issue relates to the crypto functionality used by the ethers library.

The problem occurs when I try to use Wallet.createRandom(); and the ethers library tries to access Node.js's crypto module functionalities.

I tried polyfill in webpack but it didn't work out.

fallback: { crypto: require.resolve("crypto-browserify"),

Webpack version: 5.90.3
TypeScript version: 4.7.2
Node.js version: 20.4.0

webpack.config.js:

library: {
  type: "umd",
  export: "default",
}

tsconfig.json:

"target": "es2020",
"module": "es2020",

Code Snippet

import { Wallet } from "ethers";

const wallet = Wallet.createRandom();

Contract ABI

No response

Errors

Error: System random byte generator is not available.
    at e.SystemRandomSource.randomBytes (/Users/serhat/Code/index/sdk/dist/index.js:2:8715)
    at o (/Users/serhat/Code/index/sdk/dist/index.js:2:6954)
    at Km.authenticate (/Users/serhat/Code/index/sdk/dist/index.js:2:408205)
    at ev (/Users/serhat/Code/index/sdk/dist/index.js:2:417334)
    at fv.authenticate (/Users/serhat/Code/index/sdk/dist/index.js:2:420990)
    at file:///Users/serhat/Code/index/sdk-demo/index.js:9:11
    at ModuleJob.run (node:internal/modules/esm/module_job:192:25)
    at async DefaultModuleLoader.import (node:internal/modules/esm/loader:228:24)
    at async loadESM (node:internal/process/esm_loader:40:7)
    at async handleMainPromise (node:internal/modules/run_main:66:12)

Environment

node.js (v12 or newer), Browser (Chrome, Safari, etc)

Environment (Other)

No response

@confxsd confxsd added investigate Under investigation and may be a bug. v6 Issues regarding v6 labels Mar 13, 2024
@ricmoo
Copy link
Member

ricmoo commented Mar 26, 2024

You should never try to generate a private key on an environment that does not have a secure random source, but the source can be modified:

ethers.randomBytes.register((length) => {
    // Return a Uint8Array of length, filled with your random values
});

Alternatively, when building for the browser, make sure your bundler is picking up the browser versions of the files; there is a crypto-browser file pulled in via the package.json when configured in your bundler, which uses the Web crypto API.

Does that make sense?

@ricmoo ricmoo added discussion Questions, feedback and general information. and removed investigate Under investigation and may be a bug. labels Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Questions, feedback and general information. v6 Issues regarding v6
Projects
None yet
Development

No branches or pull requests

2 participants