From d2ecc4d089557140c514f9d714fa89d67793b0b7 Mon Sep 17 00:00:00 2001 From: Patricio Palladino Date: Sat, 23 Oct 2021 18:50:39 -0300 Subject: [PATCH 1/7] Print a warning if the default accounts are used --- .../hardhat-core/src/builtin-tasks/node.ts | 21 ++++++++++++++++--- .../internal/core/config/default-config.ts | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index 042dede3e1..60d6834a3a 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -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, @@ -31,10 +32,18 @@ import { watchCompilerOutput } from "./utils/watch"; const log = debug("hardhat:core:tasks:node"); +function printDefaultConfigWarning() { + console.log( + chalk.bold( + ">>>> WARNING: These accounts are public. Any funds sent to them 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; @@ -42,6 +51,12 @@ function logHardhatNetworkAccounts(networkConfig: HardhatNetworkConfig) { console.log("Accounts"); console.log("========"); + if (isDefaultConfig) { + console.log(); + printDefaultConfigWarning(); + console.log(); + } + const accounts = normalizeHardhatNetworkAccountsConfig( networkConfig.accounts ); diff --git a/packages/hardhat-core/src/internal/core/config/default-config.ts b/packages/hardhat-core/src/internal/core/config/default-config.ts index 704f13832d..1e51340741 100644 --- a/packages/hardhat-core/src/internal/core/config/default-config.ts +++ b/packages/hardhat-core/src/internal/core/config/default-config.ts @@ -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"; From 9a75e6165e47ae909a4a441f0d8c3cd21474d9f4 Mon Sep 17 00:00:00 2001 From: Patricio Palladino Date: Sat, 23 Oct 2021 18:50:51 -0300 Subject: [PATCH 2/7] Only print the pks of the default accounts --- packages/hardhat-core/src/builtin-tasks/node.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index 60d6834a3a..2bda37d6e0 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -63,14 +63,21 @@ function logHardhatNetworkAccounts(networkConfig: HardhatNetworkConfig) { 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(); } } From c8ba0976694eab07ba195ba9b63283637f3f368e Mon Sep 17 00:00:00 2001 From: Patricio Palladino Date: Sat, 23 Oct 2021 18:51:09 -0300 Subject: [PATCH 3/7] Repeat the warning at the end --- packages/hardhat-core/src/builtin-tasks/node.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index 2bda37d6e0..f7b968a2b4 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -79,6 +79,11 @@ Private Key: ${privateKey}`; console.log(entry); console.log(); } + + if (isDefaultConfig) { + printDefaultConfigWarning(); + console.log(); + } } subtask(TASK_NODE_GET_PROVIDER) From d00a1a71846564a5c6848bf3b30f77a350baff8f Mon Sep 17 00:00:00 2001 From: Patricio Palladino Date: Sat, 23 Oct 2021 18:58:50 -0300 Subject: [PATCH 4/7] Create thick-paws-pretend.md --- .changeset/thick-paws-pretend.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/thick-paws-pretend.md diff --git a/.changeset/thick-paws-pretend.md b/.changeset/thick-paws-pretend.md new file mode 100644 index 0000000000..97b8d7ecb9 --- /dev/null +++ b/.changeset/thick-paws-pretend.md @@ -0,0 +1,5 @@ +--- +"hardhat": patch +--- + +Print a warning in the node task if the default accounts are used. From 09a00bcda47e1e14ec1ffc25d83e501e0b19a040 Mon Sep 17 00:00:00 2001 From: Patricio Palladino Date: Mon, 25 Oct 2021 13:15:28 -0300 Subject: [PATCH 5/7] Reword the warning --- packages/hardhat-core/src/builtin-tasks/node.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index f7b968a2b4..877f4faf56 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -35,7 +35,12 @@ const log = debug("hardhat:core:tasks:node"); function printDefaultConfigWarning() { console.log( chalk.bold( - ">>>> WARNING: These accounts are public. Any funds sent to them WILL BE LOST <<<<" + "WARNING: These accounts are public. Only use them in Hardhat Network." + ) + ); + console.log( + chalk.bold( + "Any sent to them on Mainnet or other live network WILL BE LOST." ) ); } From 5323eea8e371ea2d0c12aa9d824931c1f83e8d27 Mon Sep 17 00:00:00 2001 From: Patricio Palladino Date: Tue, 26 Oct 2021 13:26:49 -0300 Subject: [PATCH 6/7] Apply suggestions from code review Co-authored-by: F. Eugene Aumson --- packages/hardhat-core/src/builtin-tasks/node.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index 877f4faf56..4169f3fac2 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -35,12 +35,12 @@ const log = debug("hardhat:core:tasks:node"); function printDefaultConfigWarning() { console.log( chalk.bold( - "WARNING: These accounts are public. Only use them in Hardhat Network." + "WARNING: These accounts, and their private keys, are publicly known." ) ); console.log( chalk.bold( - "Any sent to them on Mainnet or other live network WILL BE LOST." + "Any funds sent to them on Mainnet or any other live network WILL BE LOST." ) ); } From 647c7f53b618d1c21200111d632ae465c2adeeee Mon Sep 17 00:00:00 2001 From: Patricio Palladino Date: Tue, 26 Oct 2021 16:41:52 -0300 Subject: [PATCH 7/7] Fix unrelated linter error --- docs/advanced/hardhat-runtime-environment.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/advanced/hardhat-runtime-environment.md b/docs/advanced/hardhat-runtime-environment.md index 877ef6accf..2c0361532d 100644 --- a/docs/advanced/hardhat-runtime-environment.md +++ b/docs/advanced/hardhat-runtime-environment.md @@ -66,6 +66,5 @@ extendEnvironment((hre) => { // hre.network.provider is an EIP1193-compatible provider. hre.web3 = new Web3(hre.network.provider); - }); ```