Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add accounts info printing to the hardhat node task #6042

Closed
galargh opened this issue Dec 10, 2024 · 0 comments · Fixed by #6205
Closed

Add accounts info printing to the hardhat node task #6042

galargh opened this issue Dec 10, 2024 · 0 comments · Fixed by #6205
Assignees
Labels
status:blocked Blocked by other issues or external reasons v-next A Hardhat v3 development task
Milestone

Comments

@galargh
Copy link
Member

galargh commented Dec 10, 2024

This is a follow-up task to #5615

It is about printing account information from the network config once it starts being resolved.

Below, you'll find a draft of the implementation.

Implementation:

import type {
  EdrNetworkAccountConfig,
  EdrNetworkAccountsConfig,
  GenesisAccount,
} from "../../../types/config.js";

import {
  bytesToHexString,
  hexStringToBytes,
} from "@ignored/hardhat-vnext-utils/hex";
import chalk from "chalk";
import { addr } from "micro-eth-signer";

import { derivePrivateKeys } from "../network-manager/request-handlers/handlers/accounts/derive-private-keys.js";

export function normalizeEdrNetworkConfigAccounts(
  accounts: EdrNetworkAccountsConfig,
): EdrNetworkAccountConfig[] {
  const normalizedAccounts: EdrNetworkAccountConfig[] = [];

  if (accounts !== undefined) {
    if (Array.isArray(accounts)) {
      normalizedAccounts.push(...accounts);
    } else if (accounts !== undefined) {
      const privateKeys = derivePrivateKeys(
        accounts.mnemonic,
        accounts.path,
        accounts.initialIndex,
        accounts.count,
        accounts.passphrase,
      );
      for (const privateKey of privateKeys) {
        normalizedAccounts.push({
          privateKey: bytesToHexString(privateKey),
          balance: accounts.accountsBalance,
        });
      }
    }
  }

  return normalizedAccounts;
}

export function printEdrNetworkConfigAccounts(
  accounts: Array<EdrNetworkAccountConfig | GenesisAccount>,
): void {
  if (accounts.length === 0) {
    return;
  }

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

  // NOTE: In v2, we were printing the warning only if the default config was used.
  console.log();
  printPublicPrivateKeysWarning();
  console.log();

  for (const [index, account] of accounts.entries()) {
    const address = addr
      .fromPrivateKey(hexStringToBytes(account.privateKey))
      .toLowerCase();
    const balance = (BigInt(account.balance) / 10n ** 18n).toString(10);

    console.log(`Account #${index}: ${address} (${balance} ETH)`);
    // TODO: Should we print the private key as well?
    // console.log(`Private Key: ${account.privateKey}`);

    console.log();
  }

  // NOTE: In v2, we were printing the warning only if the default config was used.
  printPublicPrivateKeysWarning();
  console.log();
}

// NOTE: In v2, we were printing the warning only if the default config was used.
// Because of that we were certain that the printed accounts were publicly known.
function printPublicPrivateKeysWarning(): void {
  console.log(
    chalk.bold(
      "WARNING: Funds sent on live network to accounts with publicly known private keys WILL BE LOST.",
    ),
  );
}

Usage:

import { normalizeEdrNetworkConfigAccounts, printEdrNetworkConfigAccounts } from "./helpers.js";

...

  printEdrNetworkConfigAccounts(normalizeEdrNetworkConfigAccounts(networkConfig.accounts))

@galargh galargh added status:blocked Blocked by other issues or external reasons v-next A Hardhat v3 development task labels Dec 10, 2024
@galargh galargh self-assigned this Dec 10, 2024
@github-project-automation github-project-automation bot moved this to Backlog in Hardhat Dec 10, 2024
@kanej kanej added this to the Public Alpha milestone Dec 13, 2024
@kanej kanej moved this from Backlog to To-do in Hardhat Dec 13, 2024
@kanej kanej linked a pull request Jan 31, 2025 that will close this issue
3 tasks
@kanej kanej moved this from To-do to In Review in Hardhat Jan 31, 2025
@kanej kanej closed this as completed Feb 4, 2025
@github-project-automation github-project-automation bot moved this from In Review to Done in Hardhat Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:blocked Blocked by other issues or external reasons v-next A Hardhat v3 development task
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants