ethers-simple-storage-fcc icon indicating copy to clipboard operation
ethers-simple-storage-fcc copied to clipboard

Cannot read property 'toHexString' of undefined

Open nenadsky opened this issue 2 years ago • 3 comments

Hi!

I saw closed thread with almost the same title, but it didn't help me :(

I keep getting this error, and i can not continue with the course:

TypeError: Cannot read property 'toHexString' of undefined
    at isHexable (/home/nenadsky/Documents/Ethereum/hh-fcc/ethers-simple-storage-fcc/node_modules/@ethersproject/bytes/lib/index.js:9:21)
    at arrayify (/home/nenadsky/Documents/Ethereum/hh-fcc/ethers-simple-storage-fcc/node_modules/@ethersproject/bytes/lib/index.js:69:9)
    at getPassword (/home/nenadsky/Documents/Ethereum/hh-fcc/ethers-simple-storage-fcc/node_modules/@ethersproject/json-wallets/lib/utils.js:25:33)
    at _computeKdfKey (/home/nenadsky/Documents/Ethereum/hh-fcc/ethers-simple-storage-fcc/node_modules/@ethersproject/json-wallets/lib/keystore.js:160:49)
    at decryptSync (/home/nenadsky/Documents/Ethereum/hh-fcc/ethers-simple-storage-fcc/node_modules/@ethersproject/json-wallets/lib/keystore.js:210:15)
    at decryptJsonWalletSync (/home/nenadsky/Documents/Ethereum/hh-fcc/ethers
[nenadsky@archlinux ethers-simple-storage-fcc]$ node deploy.js 
TypeError: Cannot read property 'toHexString' of undefined
    at isHexable (/home/nenadsky/Documents/Ethereum/hh-fcc/ethers-simple-storage-fcc/node_modules/@ethersproject/bytes/lib/index.js:9:21)
    at arrayify (/home/nenadsky/Documents/Ethereum/hh-fcc/ethers-simple-storage-fcc/node_modules/@ethersproject/bytes/lib/index.js:69:9)
    at getPassword (/home/nenadsky/Documents/Ethereum/hh-fcc/ethers-simple-storage-fcc/node_modules/@ethersproject/json-wallets/lib/utils.js:25:33)
    at _computeKdfKey (/home/nenadsky/Documents/Ethereum/hh-fcc/ethers-simple-storage-fcc/node_modules/@ethersproject/json-wallets/lib/keystore.js:160:49)
    at decryptSync (/home/nenadsky/Documents/Ethereum/hh-fcc/ethers-simple-storage-fcc/node_modules/@ethersproject/json-wallets/lib/keystore.js:210:15)
    at decryptJsonWalletSync (/home/nenadsky/Documents/Ethereum/hh-fcc/ethers-simple-storage-fcc/node_modules/@ethersproject/json-wallets/lib/index.js:36:43)
    at new Wallet.fromEncryptedJsonSync (/home/nenadsky/Documents/Ethereum/hh-fcc/ethers-simple-storage-fcc/node_modules/@ethersproject/wallet/lib/index.js:229:68)
    at main (/home/nenadsky/Documents/Ethereum/hh-fcc/ethers-simple-storage-fcc/deploy.js:10:16)
    at Object.<anonymous> (/home/nenadsky/Documents/Ethereum/hh-fcc/ethers-simple-storage-fcc/deploy.js:52:1)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)

Deploy.js file

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

async function main() {
  const provider = new ethers.providers.JsonRpcProvider(
    process.env.RPC_URL
  );
  const encryptedJson = fs.readFileSync("./.encryptedKey.json", "utf8");
  let wallet = new ethers.Wallet.fromEncryptedJsonSync(encryptedJson, process.env.PRIVATE_KEY_PASSWORD);
  wallet = await wallet.connect(provider);

  const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf8");
  const binary = fs.readFileSync(
    "./SimpleStorage_sol_SimpleStorage.bin",
    "utf8"
  );
  console.log(`Contract address: ${contract.address}`);
  const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
  console.log("Deploying, please wait...");
  const contract = await contractFactory.deploy();
 

  /*const currentFavouriteNumber = await contract.retrieve();
  console.log(`Current favourite number: ${currentFavouriteNumber.toString()}`);
  const transactionResponse = await contract.store("7");
  const transactionReceipt = await transactionResponse.wait(1);
  const updatedFavouriteNumber = await contract.retrieve();
  console.log(`Updated favourite number: ${updatedFavouriteNumber.toString()}`);
*/
}

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

I can't find any typo or similar error.

.env file contains private_key and rpc_url constants...

nenadsky avatar Dec 03 '22 00:12 nenadsky

add 0x to your private key before you encrypt it.

video timestamp 7:36:10

jigarjain709 avatar Dec 06 '22 13:12 jigarjain709

It works fine with out 0x. Problem was that from console i couldn't assign a value to my PRIVATE_KEY so i just added it to .env file and deploy script executed with out any error :D

nenadsky avatar Dec 07 '22 21:12 nenadsky

I had a similar error, however my issue was that I created the .env file outside of the directory with deploy.js, so it couldn't read it. It worked once I moved .env to inside of ethers-simple-storage.

Before

image

After image

shawnesquivel avatar Dec 09 '22 19:12 shawnesquivel