Skip to content

Commit

Permalink
make it work with a script to load env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
carletex committed Dec 6, 2024
1 parent 940866f commit ab02f75
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/hardhat/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
3 changes: 2 additions & 1 deletion packages/hardhat/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat/scripts/generateAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const setNewEnvConfig = async (existingEnvConfig = {}) => {

const newEnvConfig = {
...existingEnvConfig,
DEPLOYER_PRIVATE_KEY: encryptedJson,
DEPLOYER_PRIVATE_KEY_ENCRYPTED: encryptedJson,
};

// Store in .env
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat/scripts/listAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
54 changes: 54 additions & 0 deletions packages/hardhat/scripts/runHardhatDeployWithPK.ts
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit ab02f75

Please sign in to comment.