使用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 连接公链步骤

  1. 安装dotenv和truffle-hdwallet-provider
npm install dotenv --save-dev -g
npm install truffle-hdwallet-provider --save-dev -g

注意:windows 系统可能需要重启这两个依赖包才会在项目中生效

  1. 在项目根目录中创建一个.env文件,在.env文件中以键值对的形式写入Mnemonic和INFURA_API_KEY
INFURA_API_KEY= your infura_api_key
MNEMONIC="your mnemonic"
  1. 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
    }
  }
}

  1. 在控制台中输入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: [] }

附上:

  1. .env文件使用讲解

  2. 以太坊连接公链教程

以上是关于使用truffle console连接到公共区块链网络,出现undefined Error: Mnemonic invalid or undefined的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Ganache 连接到远程区块链网络?

区块链技术:Truffle开发入门

区块链入门Truffle创建项目,编译,及项目部署

区块链技术:Truffle开发入门

Truffle详解

区块链测试 以太坊 truffle是什么 怎么用