Welcome to encrypt.js
const ethers = require("ethers");
const fs = require("fs-extra");
require("dotenv").config();
async function main() {
const wallet = new ethers.Wallet(process.env.RRIVATE_KEY);
const encryptedJsonKey = await wallet.encrypt(
process.env.PRIVATE_KEY_PASSWORD,
process.env.PRIVATE_KEY
);
console.log(encryptedJsonKey);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
This is the content of .env file
PRIVATE_KEY=0x3ed74d4599fbc880e1f2dd094bf1dde078e808779f4f888d4b59a6d07bee99cb
RPC_URL=http://127.0.0.1:7545
PRIVATE_KEY_PASSWORD=password
An error occurred with the message: "TypeError: Cannot read properties of undefined (reading 'toHexString')"
I tried searching online for a solution and someone suggested that I should keep all files in the same directory, which I have already done. Check here for directory When hovering over "PRIVATE_KEY_PASSWORD" or "PRIVATE_PASSWORD", it shows "string | undefined". Where could the issue be?
Upon further inspection, I discovered the mistake. There was a spelling error in the code: const wallet = new ethers.Wallet(process.env.RRIVATE_KEY); Simply change RRIVATE to PRIVATE. Apologies for the oversight.