Skip to content

Commit

Permalink
Merge pull request NomicFoundation#1989 from nomiclabs/hardhat-node-w…
Browse files Browse the repository at this point in the history
…arning

Print a warning in the node task about the accounts being shared
  • Loading branch information
alcuadrado authored Oct 27, 2021
2 parents add460e + 647c7f5 commit af7afb0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-paws-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hardhat": patch
---

Print a warning in the node task if the default accounts are used.
1 change: 0 additions & 1 deletion docs/advanced/hardhat-runtime-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,5 @@ extendEnvironment((hre) => {

// hre.network.provider is an EIP1193-compatible provider.
hre.web3 = new Web3(hre.network.provider);

});
```
46 changes: 39 additions & 7 deletions packages/hardhat-core/src/builtin-tasks/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
JsonRpcServer,
} from "../types";

import { HARDHAT_NETWORK_MNEMONIC } from "../internal/core/config/default-config";
import {
TASK_NODE,
TASK_NODE_CREATE_SERVER,
Expand All @@ -31,31 +32,62 @@ import { watchCompilerOutput } from "./utils/watch";

const log = debug("hardhat:core:tasks:node");

function printDefaultConfigWarning() {
console.log(
chalk.bold(
"WARNING: These accounts, and their private keys, are publicly known."
)
);
console.log(
chalk.bold(
"Any funds sent to them on Mainnet or any other live network WILL BE LOST."
)
);
}

function logHardhatNetworkAccounts(networkConfig: HardhatNetworkConfig) {
if (networkConfig.accounts === undefined) {
return;
}
const isDefaultConfig =
!Array.isArray(networkConfig.accounts) &&
networkConfig.accounts.mnemonic === HARDHAT_NETWORK_MNEMONIC;

const { BN, bufferToHex, privateToAddress, toBuffer } =
require("ethereumjs-util") as typeof EthereumjsUtilT;

console.log("Accounts");
console.log("========");

if (isDefaultConfig) {
console.log();
printDefaultConfigWarning();
console.log();
}

const accounts = normalizeHardhatNetworkAccountsConfig(
networkConfig.accounts
);

for (const [index, account] of accounts.entries()) {
const address = bufferToHex(privateToAddress(toBuffer(account.privateKey)));
const privateKey = bufferToHex(toBuffer(account.privateKey));

const balance = new BN(account.balance)
.div(new BN(10).pow(new BN(18)))
.toString(10);

console.log(`Account #${index}: ${address} (${balance} ETH)
Private Key: ${privateKey}
`);
let entry = `Account #${index}: ${address} (${balance} ETH)`;

if (isDefaultConfig) {
const privateKey = bufferToHex(toBuffer(account.privateKey));
entry += `
Private Key: ${privateKey}`;
}

console.log(entry);
console.log();
}

if (isDefaultConfig) {
printDefaultConfigWarning();
console.log();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const DEFAULT_SOLC_VERSION = "0.7.3";
export const HARDHAT_NETWORK_DEFAULT_GAS_PRICE = "auto";
export const HARDHAT_NETWORK_DEFAULT_MAX_PRIORITY_FEE_PER_GAS = 1e9;
export const HARDHAT_NETWORK_DEFAULT_INITIAL_BASE_FEE_PER_GAS = 1e9;
const HARDHAT_NETWORK_MNEMONIC =
export const HARDHAT_NETWORK_MNEMONIC =
"test test test test test test test test test test test junk";
export const DEFAULT_HARDHAT_NETWORK_BALANCE = "10000000000000000000000";

Expand Down

0 comments on commit af7afb0

Please sign in to comment.