NodeJS 无效帐户:#0用于网络:ropsten -私钥太短,应为32个字节

db2dz4w8  于 2023-01-25  发布在  Node.js
关注(0)|答案(3)|浏览(105)

error picture

require("@nomiclabs/hardhat-waffle");

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
  const accounts = await hre.ethers.getSigners();

  for (const account of accounts) {
    console.log(account.address);
  }
});

// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more

/**
 * @type import('hardhat/config').HardhatUserConfig
 */

const INFURA_PROJECT_ID = "3dxxxx";
const ROPSTEN_PRIVATE_KEY = "e6xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

module.exports = {
  networks: {
    ropsten: {
      url: `https://ropsten.infura.io/v3/${INFURA_PROJECT_ID}`,
      accounts: [`0x${ROPSTEN_PRIVATE_KEY}`]
    }
  }
};

我按照网站上的示例将合约部署到ropsten,但是报告了一个错误,私钥大于32位

vzgqcmou

vzgqcmou1#

这是因为你把“0x”放在你的私钥前面。我也有过类似的错误。另外,如果你想的话,你可以使用炼金术。在你的配置中,其他一切看起来都很好。

ruarlubt

ruarlubt2#

我也遇到了同样的错误,使用了一个env文件来存储常量。所以在我的例子中,错误是通过删除分号来解决的。

7vux5j2d

7vux5j2d3#

一些可能的修复方法:

  • 将0x添加到私钥
  • 使用StringtoString()和字符串插值$0x{process.env.PRIVATE_KEY}将私钥从.env文件转换或强制转换为字符串
  • 检查是否从正确的目录调用测试
  • 检查.env文件是否位于正确的(项目根)目录中
  • 检查钱包软件是否给了我完整的私钥,而不是部分密钥

但解决这个问题的方法是导入:

require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();

hardhat.config.js文件的顶部

相关问题