5

Hi Please i get this error while compiling hardhat.config.js : * Invalid account: #0 for network: Rinkeby - private key too short, expected 32 bytes i have my private key in dot env copied from metamask please how to i convert the normal private key into 32 bytes and how do i solve this thanks in advance here's my code

/**
 * @type import('hardhat/config').HardhatUserConfig
 */

     require('dotenv').config();
     require("@nomiclabs/hardhat-ethers");
    
     const { API_URL, PRIVATE_KEY } = process.env
    
     module.exports = {
      solidity: "0.7.1",
      defaultNetwork: "Rinkeby",
      networks: {
        hardhat: {},
        Rinkeby: {
          url: API_URL,
          accounts: ['${PRIVATE_KEY}']
        }
      }
      }

error :

  * Invalid account: #0 for network: Rinkeby - private key too short, expected 32 bytes

enter image description here

6 Answers 6

5

Make sure you are copying your PRIVATE KEY and not your PUBLIC KEY.

If you are using metamask, you can get your PRIVATE KEY from

  • the 3 dots menu beside your public key,

  • and then account details

  • then export private key

enter image description here

enter image description here

1

In my case key was from alchemy

https://wiki.polygon.technology/docs/develop/alchemy/

this is extremely bugged tutorial, no recommend nobody but finally

i converted my API_KEY to hex using

const hex = utf8ToHex(PRIVATE_KEY ?? '');

and this hex in cofig

const config: HardhatUserConfig = {
  solidity: "0.8.9",
  defaultNetwork: "polygon_mumbai",
  networks: {
    hardhat: {},
    polygon_mumbai: {
      url: API_URL,
      accounts: [`0x${hex}`],
      chainId: 8
    }
  },
};

below function

function utf8ToHex(str: string) {
  return Array.from(str).map(c =>
      c.charCodeAt(0) < 128 ? c.charCodeAt(0).toString(16) :
          encodeURIComponent(c).replace(/\%/g,'').toLowerCase()
  ).join('');
}
0

You're not doing string extrapolation correctly, you should be using `, the tilde button. Read more here.

0

if you copy PRIVATE_KEY from Metamask, you should put 0x as prefix

0

In hardhat.config.js, accounts key value should be an array of hex-encoded private keys, for that you need to add the 0x as prefix of your Matamask private key before passing this private key to the account array as below

networks: {
    goerli: {
      url: RCP_GOERLI_URL,
      accounts: [`0x${process.env.RCP_PRIVATE_KEY}`],
      chainId: 5,
    },
  },
0

I may be a bit late to this problem given above, if none of the answers given above worked for you, (which was my case), you can simply add 0x before your metamask private key in your .env file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.