Lesson 5: ethers.js Simple Storage: Cannot read properties of undefined (reading 'toHexString') #1313
-
Hello I am currently on: Better Private key management: LINK It seems like the error is from reading the .encryptedKey.json file. From what I understand the problem is with this line:
The whole code:
The error:
Just in case it is helpful here is the .encryptedKey.json with password=password
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
Use my encryptKey.js: (Refactor variable names but do not change the logic) const ethers = require("ethers");
const fs = require("fs-extra");
require("dotenv").config(); // To process .env file
async function main() {
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY);
const encryptedJsonKey = await wallet.encrypt( // For encryption
process.env.PRIVATE_KEY_PASSWORD,
process.env.PRIVATE_KEY
);
fs.writeFileSync("./.encryptedKey.json", encryptedJsonKey); // Stores encrypted key
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
}); Also, make a repo so I can diagnose your deploy file. |
Beta Was this translation helpful? Give feedback.
-
From my understanding the issue might be with encrypting using hardhat doesn't work that well, so I have continued with the course using the plain text in .env file. |
Beta Was this translation helpful? Give feedback.
-
Hi BentCrypto, I might have solved your issue. Are you using zsh for you terminal? If you do so, you need to set your environment variable using the following command:
That should help you out in the case you use zsh. If you get an error typing:
your environment variable hasn't been set. Hope that's helpful. This method at least helped me out. |
Beta Was this translation helpful? Give feedback.
From my understanding the issue might be with encrypting using hardhat doesn't work that well, so I have continued with the course using the plain text in .env file.