Skill 4.3.2

Protect deployment keys and plan production deployments

Key Concepts

Private keys, mnemonic phrases and RPC API keys must never be written into configuration files or committed to source control. Hardhat's configVariable("NAME") reads a value at run time from an environment variable or from the Hardhat keystore, an encrypted file managed with npx hardhat keystore set NAME, which asks for a password. Older projects used .env files with the dotenv package, which must be listed in .gitignore.

Before a mainnet deployment, teams freeze and audit the code, test the deployment on a local fork and on Sepolia, check gas prices, and use a dedicated deployer account holding only the ether needed. A hardware wallet keeps the key off the computer. After deployment, administrative roles and ownership are transferred to a multisig such as Safe, often behind a timelock, and the contracts are verified and monitored.