diff --git a/packages/hardhat/.env.example b/packages/hardhat/.env.example index 86614fe5e4..b12938318a 100644 --- a/packages/hardhat/.env.example +++ b/packages/hardhat/.env.example @@ -7,5 +7,7 @@ # To access the values stored in this .env file you can use: process.env.VARIABLENAME ALCHEMY_API_KEY= -DEPLOYER_PRIVATE_KEY= ETHERSCAN_MAINNET_API_KEY= + +# Don't fill this value manually, run yarn generate to generate a new account or yarn account:import to import an existing PK. +DEPLOYER_PRIVATE_KEY_ENCRYPTED= diff --git a/packages/hardhat/hardhat.config.ts b/packages/hardhat/hardhat.config.ts index 4bbddc0543..f905110f30 100644 --- a/packages/hardhat/hardhat.config.ts +++ b/packages/hardhat/hardhat.config.ts @@ -13,7 +13,8 @@ import { task } from "hardhat/config"; import generateTsAbis from "./scripts/generateTsAbis"; // If not set, it uses the hardhat account 0 private key. -const deployerPrivateKey = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"; +const deployerPrivateKey = + process.env.DEPLOYER_PRIVATE_KEY || "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"; // If not set, it uses our block explorers default API keys. const etherscanApiKey = process.env.ETHERSCAN_MAINNET_API_KEY || "DNXJA8RX2Q3VZ4URQIWP7Z68CJXQZSC6AW"; const etherscanOptimisticApiKey = process.env.ETHERSCAN_OPTIMISTIC_API_KEY || "RM62RDISS1RH448ZY379NX625ASG1N633R"; diff --git a/packages/hardhat/package.json b/packages/hardhat/package.json index 5791678ab6..7972229132 100644 --- a/packages/hardhat/package.json +++ b/packages/hardhat/package.json @@ -6,7 +6,7 @@ "chain": "hardhat node --network hardhat --no-deploy", "check-types": "tsc --noEmit --incremental", "compile": "hardhat compile", - "deploy": "hardhat deploy", + "deploy": "ts-node scripts/runHardhatDeployWithPK.ts", "fork": "MAINNET_FORKING_ENABLED=true hardhat node --network hardhat --no-deploy", "generate": "hardhat run scripts/generateAccount.ts", "flatten": "hardhat flatten", diff --git a/packages/hardhat/scripts/generateAccount.ts b/packages/hardhat/scripts/generateAccount.ts index 1fc127ccb6..5a8a16f2c9 100644 --- a/packages/hardhat/scripts/generateAccount.ts +++ b/packages/hardhat/scripts/generateAccount.ts @@ -26,7 +26,7 @@ const setNewEnvConfig = async (existingEnvConfig = {}) => { const newEnvConfig = { ...existingEnvConfig, - DEPLOYER_PRIVATE_KEY: encryptedJson, + DEPLOYER_PRIVATE_KEY_ENCRYPTED: encryptedJson, }; // Store in .env @@ -44,7 +44,7 @@ async function main() { } const existingEnvConfig = parse(fs.readFileSync(envFilePath).toString()); - if (existingEnvConfig.DEPLOYER_PRIVATE_KEY) { + if (existingEnvConfig.DEPLOYER_PRIVATE_KEY_ENCRYPTED) { console.log("⚠️ You already have a deployer account. Check the packages/hardhat/.env file"); return; } diff --git a/packages/hardhat/scripts/listAccount.ts b/packages/hardhat/scripts/listAccount.ts index 84273dd327..ad3942daf1 100644 --- a/packages/hardhat/scripts/listAccount.ts +++ b/packages/hardhat/scripts/listAccount.ts @@ -6,7 +6,7 @@ import { config } from "hardhat"; import password from "@inquirer/password"; async function main() { - const encryptedKey = process.env.DEPLOYER_PRIVATE_KEY; + const encryptedKey = process.env.DEPLOYER_PRIVATE_KEY_ENCRYPTED; if (!encryptedKey) { console.log("🚫️ You don't have a deployer account. Run `yarn generate` first"); diff --git a/packages/hardhat/scripts/runHardhatDeployWithPK.ts b/packages/hardhat/scripts/runHardhatDeployWithPK.ts new file mode 100644 index 0000000000..d58b7acefc --- /dev/null +++ b/packages/hardhat/scripts/runHardhatDeployWithPK.ts @@ -0,0 +1,54 @@ +import * as dotenv from "dotenv"; +dotenv.config(); +import { Wallet } from "ethers"; +import password from "@inquirer/password"; +import { spawn } from "child_process"; + +/** + * Unencrypts the private key and runs the hardhat deploy command + */ +async function main() { + const isNetworkProvided = process.argv.slice(2).includes("--network"); + + if (!isNetworkProvided) { + // Deploy command on the localhost network + const hardhat = spawn("hardhat", ["deploy", ...process.argv.slice(2)], { + stdio: "inherit", + env: process.env, + }); + + hardhat.on("exit", code => { + process.exit(code || 0); + }); + return; + } + + const encryptedKey = process.env.DEPLOYER_PRIVATE_KEY_ENCRYPTED; + + if (!encryptedKey) { + console.log("🚫️ You don't have a deployer account. Run `yarn generate` first"); + return; + } + + const pass = await password({ message: "Enter password to decrypt private key:", mask: true }); + + try { + const wallet = await Wallet.fromEncryptedJson(encryptedKey, pass); + process.env.DEPLOYER_PRIVATE_KEY = wallet.privateKey; + + const hardhat = spawn("hardhat", ["deploy", ...process.argv.slice(2)], { + stdio: "inherit", + env: process.env, + }); + + hardhat.on("exit", code => { + process.exit(code || 0); + }); + // eslint-disable-next-line @typescript-eslint/no-unused-vars + } catch (e) { + console.error("Failed to decrypt private key. Wrong password?"); + process.exit(1); + } +} + +main().catch(console.error);