full-blockchain-solidity-course-js icon indicating copy to clipboard operation
full-blockchain-solidity-course-js copied to clipboard

Lesson 5: Running into a TypeError while entering private key value in encryptKey.js

Open namyasharma opened this issue 1 year ago • 1 comments

Discussed in https://github.com/smartcontractkit/full-blockchain-solidity-course-js/discussions/5363

Originally posted by namyasharma April 24, 2023 I just started out with Lesson 5 and typed out the first few lines of code in encryptKey.js.

Here's my code:

const fs = require("fs-extra");
require("dotenv").config();

async function main() {
  const wallet = new ethers.Wallet(process.env.PRIVATE_KEY);
  const encryptedJsonKey = await ethers.encryptKeystoreJson(
    process.env.PRIVATE_KEY_PASSWORD,
    process.env.PRIVATE_KEY
  );
}

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

I keep running into the following issue:

TypeError: invalid BytesLike value (argument="privateKey", value=null, code=INVALID_ARGUMENT, version=6.3.0)
    at makeError (/home/namysharma/blockchain/ethers-simple-storage/node_modules/ethers/lib.commonjs/utils/errors.js:114:21)
    at assert (/home/namysharma/blockchain/ethers-simple-storage/node_modules/ethers/lib.commonjs/utils/errors.js:138:15)
    at assertArgument (/home/namysharma/blockchain/ethers-simple-storage/node_modules/ethers/lib.commonjs/utils/errors.js:150:5)
    at _getBytes (/home/namysharma/blockchain/ethers-simple-storage/node_modules/ethers/lib.commonjs/utils/data.js:27:36)
    at getBytes (/home/namysharma/blockchain/ethers-simple-storage/node_modules/ethers/lib.commonjs/utils/data.js:37:12)
    at _encryptKeystore (/home/namysharma/blockchain/ethers-simple-storage/node_modules/ethers/lib.commonjs/wallet/json-keystore.js:192:48)
    at Object.encryptKeystoreJson (/home/namysharma/blockchain/ethers-simple-storage/node_modules/ethers/lib.commonjs/wallet/json-keystore.js:292:12)
    at async main (/home/namysharma/blockchain/ethers-simple-storage/encryptKey.js:7:28) {
  code: 'INVALID_ARGUMENT',
  argument: 'privateKey',
  value: undefined
}

I've tried reinstalling node modules, and making sure that I am adding the correct environment variable. I am not exactly sure why this type error occurs. Has the syntax changed after an update or so? Please let me know if you have faced a similar issue or have any suggestions. Thanks in advance!

Here's the timestamp from the course for reference: https://youtu.be/gyMwXuJrbJQ?t=27855

And a link to my repo if needed: https://github.com/namyasharma/ethers-simple-storage

namyasharma avatar Apr 24 '23 06:04 namyasharma

Did you add your own privatekey in your .env file. Otherwise you add const ethers = require("ethers") on the top.

akhilmanga avatar Apr 27 '23 12:04 akhilmanga