使用truffle console连接到公共区块链网络,出现undefined Error: Mnemonic invalid or undefined
Posted weixin_46353030
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用truffle console连接到公共区块链网络,出现undefined Error: Mnemonic invalid or undefined相关的知识,希望对你有一定的参考价值。
错误提示
Error: Mnemonic invalid or undefined
原因:.env文件命名错误
将process.env文件名直接改成.env
后再执行 truffle console --network kovan
命令即可
truffle 连接公链步骤
- 安装dotenv和truffle-hdwallet-provider
npm install dotenv --save-dev -g
npm install truffle-hdwallet-provider --save-dev -g
注意:windows 系统可能需要重启这两个依赖包才会在项目中生效
- 在项目
根目录
中创建一个.env文件,在.env文件中以键值对的形式
写入Mnemonic和INFURA_API_KEY
INFURA_API_KEY= your infura_api_key
MNEMONIC="your mnemonic"
- 在
truffle-config.js
或者truffle.js
文件中进行配置:
// 导入dotenv库创用于读取`.env`文件中的设置
require('dotenv').config();
// 导入truffle-hdwallet-provider库重建钱包
const HDWalletProvider = require('truffle-hdwallet-provider');
module.exports = {
networks: {
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 7545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},
// Useful for deploying to a public network.
// NB: It's important to wrap the provider as a function.
kovan: {
provider: () => new HDWalletProvider(
process.env.MNEMONIC,
process.env.INFURA_API_KEY
),
gas: 5000000,
gasPrice: 25000000000,
network_id: 42
},
},
solc: {
optimizer: {
enabled: true,
runs: 200
}
}
}
- 在控制台中输入
truffle console --network kovan
,即可连接到公链,可以输入web3.eth.getBlock('latest').then(console.log)
进行验证,若返回以下内容则表示连接成功:
{ author: '0x03801efb0efe2a25ede5dd3a003ae880c0292e4d',
difficulty: '340282366920938463463374607431768211454',
extraData:
'0xde830206028f5061726974792d457468657265756d86312e33362e30826c69',
gasLimit: '0x7a1200',
gasUsed: '0x17d23',
hash:
'0xc7390c4f492c8c1da60608135fc9e05930123b645b39f221cba33d8b3c577b2a',
logsBloom:
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000080000000000000000000100000008000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400800000000000010000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009000000008000000',
receiptsRoot:
'0x3d05bb2ed4fcc90234eea6d840e7d0e3ce7f598a15e5314536b17bcd11c78b5b',
sealFields:
[ '0x84175e8801',
'0xb84155a8cdb108dccec1d314124058fa6f22e7400ee200db0a94b7b165e4c3454c1818cc05f815cb7ce48f7a88b8401515740311a3566d9cf079428d506a6daca50101' ],
sha3Uncles:
'0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
signature:
'55a8cdb108dccec1d314124058fa6f22e7400ee200db0a94b7b165e4c3454c1818cc05f815cb7ce48f7a88b8401515740311a3566d9cf079428d506a6daca50101',
size: 877,
stateRoot:
'0x03af5adce52a81ce5d332cddb9955e344214bff00859b78868116e1e839efdf7',
step: '392071169',
timestamp: 1568284676,
totalDifficulty: '4524524338444961608702071789512829094373049115',
transactions:
[ '0xded7fed0842fd65ec808bc3652ec4175bc190acc11345c49c44b1fb5d954610f',
'0x7e9112a46fa3c07aad813ea86355b15eebb44023c040d198ee7d15d379bbc2be' ],
transactionsRoot:
'0x0dd10d90686dda2684bd0ba70d1c9e1d9a5302c30ca75eb2c5b07a7b6e4498b9',
uncles: [] }
附上:
以上是关于使用truffle console连接到公共区块链网络,出现undefined Error: Mnemonic invalid or undefined的主要内容,如果未能解决你的问题,请参考以下文章