最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - error "fromMnemonic()"Create ether wallet - Stack Overflow

programmeradmin5浏览0评论

What is wrong here?

const userWalletKeys = Wallet.createRandom().mnemonic
const userWallet = ethers.Wallet.fromMnemonic(userWalletKeys.phrase)

I get this error at line 2 of code: Uncaught TypeError: ethers__WEBPACK_IMPORTED_MODULE_3__.Wallet.fromMnemonic is not a function

I tried to generate a random mnemonic phrase for a ether wallet.

What is wrong here?

const userWalletKeys = Wallet.createRandom().mnemonic
const userWallet = ethers.Wallet.fromMnemonic(userWalletKeys.phrase)

I get this error at line 2 of code: Uncaught TypeError: ethers__WEBPACK_IMPORTED_MODULE_3__.Wallet.fromMnemonic is not a function

I tried to generate a random mnemonic phrase for a ether wallet.

Share Improve this question asked Mar 8, 2023 at 23:34 Radu BogdanRadu Bogdan 312 silver badges2 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

In the version 6.1.0 you need to use const wallet = ethers.Wallet.fromPhrase(mnemonic.phrase) This would return a new HDWallet for you , that would consist of the public and private keys.

const wallet = ethers.Wallet.fromPhrase(mnemonic);
const privateKey = wallet.privateKey;
const publicKey = wallet.publicKey

The syntax changed in v6. Instead of

const userWallet = ethers.Wallet.fromMnemonic(userWalletKeys.phrase)

you'll do

 const userWallet = ethers.HDNodeWallet.fromMnemonic(userWalletKeys.phrase)

You can also just import modularly like this:

const { HDNodeWallet } = require('ethers') 

then use just use it without importing the entire ethers library, like this:

const userWallet = HDNodeWallet.fromMnemonic(userWalletKeys.phrase)

see docs here: ethers v6 docs

In v.6.1.0 you have to use ethers.Wallet.fromPhrase

Just been using ChatGPT to debug this very same error and after many twists and turns it suggested to fall back to ethers 5.0.0 (modifying package.json) and that worked. It seems in version 6.1.0 that method has dissappeared or maybe isn't ready yet or something...

发布评论

评论列表(0)

  1. 暂无评论