0

Description: I'm encountering an issue while attempting to deploy a contract using ethers.js. The error message I'm receiving is as follows:

Error:

TypeError: invalid private key (argument="privateKey", value="[ REDACTED ]", code=INVALID_ARGUMENT, version=6.10.0)
  at makeError (/home/amine/hh-fcc/ether-simple-storage/node_modules/ethers/lib.commonjs/utils/errors.js:122:21)
  at assert (/home/amine/hh-fcc/ether-simple-storage/node_modules/ethers/lib.commonjs/utils/errors.js:149:15)
  at assertArgument (/home/amine/hh-fcc/ether-simple-storage/node_modules/ethers/lib.commonjs/utils/errors.js:161:5)
  at new BaseWallet (/home/amine/hh-fcc/ether-simple-storage/node_modules/ethers/lib.commonjs/wallet/base-wallet.js:35:39)
  at new Wallet (/home/amine/hh-fcc/ether-simple-storage/node_modules/ethers/lib.commonjs/wallet/wallet.js:34:9)
  at main (/home/amine/hh-fcc/ether-simple-storage/deploy.js:9:18)
  at Object.<anonymous> (/home/amine/hh-fcc/ether-simple-storage/deploy.js:23:1)
  at Module._compile (node:internal/modules/cjs/loader:1376:14)
  at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
  at Module.load (node:internal/modules/cjs/loader:1207:32) {
  code: 'INVALID_ARGUMENT',
  argument: 'privateKey',
  value: '[ REDACTED ]',
  shortMessage: 'invalid private key'
}

My code :

const ethers = require("ethers");
const fs = require("fs-extra");

async function main() {
  const provider = new ethers.JsonRpcProvider("http://127.0.0.1:7545");
  const wallet = new ethers.Wallet(
    0xc0df683409025546f494690cf4ecfa9b3a9fc8ef5bdc1e7e82ae9182f2201591,
    provider
  );
  const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf8");
  const binary = fs.readFileSync(
    "./SimpleStorage_sol_SimpleStorage.bin",
    "utf8"
  );
  const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
  console.log("Deploying, please wait ");
  const contract = await contractFactory.deploy();
  console.log(contract);
}

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

Issue Description: I'm using the correct private key provided by Ganache, but I'm consistently receiving an "invalid private key" error during deployment. The private key is a hexadecimal string and appears to be in the correct format. Could someone please review the code and provide insights into what might be causing this issue? Any help would be greatly appreciated.

Thank you!

1
  • Instead of 0xc0df...591, try using "c0df...591"
    – 0xSanson
    Jan 22 at 22:45

1 Answer 1

0

I found a solution :

const privateKey = "0xcc362ddcadfb1e2826aa3a7e2c6cf08121e8946617b9a870a0ca5e610b830c14";
const wallet = new ethers.Wallet(privateKey, provider);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.