【问题标题】:* Invalid account: #0 for network: rinkeby - private key too short, expected 32 bytes* 无效帐户:#0 网络:rinkeby - 私钥太短,预计 32 字节
【发布时间】:2022-12-18 23:05:08
【问题描述】:

尝试使用在 rinkeby 测试网络上启动 dapp

npx hardhat run scripts/deploy.js --network rinkeby

但是每当我尝试启动合同时,当我将私钥复制到环境变量中时,它会返回一条错误消息,提示私钥大小太短。

当我将私钥直接传递到我的 module.exports 上的帐户时,它会抛出一个错误,说我的私钥太长了!不管我是否在 ${} 之前附加了 0x 前缀。

安全帽.config.js

require("@nomicfoundation/hardhat-toolbox");


const PRIVATE_KEY = "privateKeyjdhsbcidsjhnc"
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.4",
  paths: {
    artifacts: './src/artifacts',
  },
  networks: {
    hardhat: {
      chainId: 1337
    },
    rinkeby: {
      url: "https://eth-rinkeby.alchemyapi.io/v2/_YGPVs4UBSWtFxp-fpynFRcF34oU7WUI",
      accounts: ['0x${process.env.PRIVATE_KEY}']
    }
  },
  solidity: "0.8.4",
};`

部署.js

const hre = require("hardhat");

async function main() {
    const Greeter = await hre.ethers.getContractFactory("Greeter");
    const greeter = await Greeter.deploy("Hello,Hardhat!");

    await greeter.deployed();

    console.log("Greeter deployed to:", greeter.address)
}

main()
    .then(() => process.exit(0))
    .catch(error => {
        console.error(error);
        process.exit(1);
    });

【问题讨论】:

    标签: reactjs solidity


    【解决方案1】:

    改变这个

      '0x${process.env.PRIVATE_KEY}' 
    

    `0x${process.env.PRIVATE_KEY}`
    

    【讨论】:

      【解决方案2】:

      必须安装dotenv文件:npm i dotenv

      1. 检查你的 .env 文件

        GOERLI_URL_API= d6164sbshdfkjsnkfj6515 WALLET_PRIVATE_KEY = "hb5154sdhbasd5651sdfsdbf54sf54ss5465F"

      2. 检查 hardhat/config.js

        #然后需要DOTENV文件

        require('dotenv').config()
        

        //add network connection

        networks: {
            hardhat: {},
             goerli: {
              url: `https://goerli.infura.io/v3/${process.env.GOERLI_URL_API}`,
              accounts: [process.env.WALLET_PRIVATE_KEY.toString()]
            }
          }
        

        4.然后Deploy运行构建

        npx hardhat run scripts/deploy.js --show-stack-traces
        

      【讨论】:

        猜你喜欢
        • 2022-06-16
        • 2023-01-26
        • 2019-03-19
        • 1970-01-01
        • 2021-06-04
        • 2019-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多