3

I read the doc: https://hardhat.org/tutorial/deploying-to-a-live-network

This is the config file:

require("@nomicfoundation/hardhat-toolbox");

// Go to https://www.alchemyapi.io, sign up, create
// a new App in its dashboard, and replace "KEY" with its key
const ALCHEMY_API_KEY = "KEY";

// Replace this private key with your Goerli account private key
// To export your private key from Metamask, open Metamask and
// go to Account Details > Export Private Key
// Beware: NEVER put real Ether into testing accounts
const GOERLI_PRIVATE_KEY = "YOUR GOERLI PRIVATE KEY";

module.exports = {
  solidity: "0.8.9",
  networks: {
    goerli: {
      url: `https://eth-goerli.alchemyapi.io/v2/${ALCHEMY_API_KEY}`,
      accounts: [GOERLI_PRIVATE_KEY]
    }
  }
};

So I have export the private key of my account in metamask.

I want to know, why not hardhat deploy contract by metamask which stored the account, so we do not need to export a private key? It seems Remix can deploy contract by metamask?

2 Answers 2

4

Hardhat is a backend platform. Metamask lives in your browser. They are not meant to communicate like that. So you can't deploy your contract through Hardhat by utilizing private keys inside Metamask.

I would say this is mostly due to two factors:

  1. Security. Browsers are all the time exposed to the wide internet and they need lots of different security factors. Hardhat lives only in the backend and has a lot less attack surface.

  2. Integration challenges. Since hardhat is a backend tool, it would be possibly quite difficult to integrate it directly with a browser wallet.

They are simply meant for different purposes.

4

This can be achieved with the help of this npm library: https://www.npmjs.com/package/hardhat_metamask_client

  • The approach of the library is a little tricky due to the fact that hardhat doesn't meant to do such things.
1
  • That works for me. Thanks!
    – Miko
    Commented Jan 2, 2023 at 12:48

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.