smartcontractkit / full-blockchain-solidity-course-js

Learn Blockchain, Solidity, and Full Stack Web3 Development with Javascript
12.01k stars 2.91k forks source link

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

Open namyasharma opened 1 year ago

namyasharma commented 1 year ago

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 ethers = require("ethers"); 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](url) And a link to my repo if needed: [https://github.com/namyasharma/ethers-simple-storage](url)
akhilmanga commented 1 year ago

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