cmd/geth: add PrintDevAccounts function to display developer accounts and keys

This commit is contained in:
Arya Nair 2025-05-25 22:32:37 +05:30
parent 5b9918fe99
commit 538a9416a8
2 changed files with 64 additions and 46 deletions

View file

@ -42,6 +42,11 @@ import (
_ "github.com/ethereum/go-ethereum/eth/tracers/live" _ "github.com/ethereum/go-ethereum/eth/tracers/live"
_ "github.com/ethereum/go-ethereum/eth/tracers/native" _ "github.com/ethereum/go-ethereum/eth/tracers/native"
"encoding/hex"
"math/big"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -329,6 +334,21 @@ func prepare(ctx *cli.Context) {
} }
} }
func PrintDevAccounts(devAccounts []core.DevAccount) {
fmt.Println("Available Accounts")
fmt.Println("==================")
for i, acct := range devAccounts {
ethBal := new(big.Float).Quo(new(big.Float).SetInt(acct.Balance), big.NewFloat(1e18))
fmt.Printf("(%d) %s (%s ETH)\n", i, acct.Address.Hex(), ethBal.Text('f', 18))
}
fmt.Println()
fmt.Println("Private Keys")
fmt.Println("==================")
for i, acct := range devAccounts {
fmt.Printf("(%d) 0x%s\n", i, hex.EncodeToString(crypto.FromECDSA(acct.PrivateKey)))
}
}
// geth is the main entry point into the system if no special subcommand is run. // geth is the main entry point into the system if no special subcommand is run.
// It creates a default node based on the command line arguments and runs it in // It creates a default node based on the command line arguments and runs it in
// blocking mode, waiting for it to be shut down. // blocking mode, waiting for it to be shut down.
@ -338,6 +358,14 @@ func geth(ctx *cli.Context) error {
} }
prepare(ctx) prepare(ctx)
if ctx.IsSet(utils.DeveloperFlag.Name) {
devAccounts, err := core.GenerateDevAccounts()
if err == nil {
PrintDevAccounts(devAccounts)
}
}
stack := makeFullNode(ctx) stack := makeFullNode(ctx)
defer stack.Close() defer stack.Close()

View file

@ -25,7 +25,6 @@ import (
"strings" "strings"
"crypto/ecdsa" "crypto/ecdsa"
"encoding/hex"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
@ -647,28 +646,44 @@ func DefaultHoodiGenesisBlock() *Genesis {
} }
} }
// DeveloperGenesisBlock returns the 'geth --dev' genesis block. const (
func DeveloperGenesisBlock(gasLimit uint64, faucet *common.Address) *Genesis { devAccountCount = 10
config := *params.AllDevChainProtocolChanges devAccountSeedPrefix = "dev-account-seed-"
devAccountBalance = 10000
)
// Generate 10 deterministic dev accounts type DevAccount struct {
type DevAccount struct { Address common.Address
Address common.Address PrivateKey *ecdsa.PrivateKey
PrivateKey *ecdsa.PrivateKey Balance *big.Int
Balance *big.Int }
}
func GenerateDevAccounts() ([]DevAccount, error) {
var devAccounts []DevAccount var devAccounts []DevAccount
for i := 0; i < 10; i++ { for i := 0; i < devAccountCount; i++ {
seed := []byte("dev-account-seed-") seed := append([]byte(devAccountSeedPrefix), byte(i))
seed = append(seed, byte(i)) key, err := crypto.ToECDSA(crypto.Keccak256(seed))
key, _ := crypto.ToECDSA(crypto.Keccak256(seed)) if err != nil {
return nil, err
}
addr := crypto.PubkeyToAddress(key.PublicKey) addr := crypto.PubkeyToAddress(key.PublicKey)
devAccounts = append(devAccounts, DevAccount{ devAccounts = append(devAccounts, DevAccount{
Address: addr, Address: addr,
PrivateKey: key, PrivateKey: key,
Balance: new(big.Int).Mul(big.NewInt(10000), big.NewInt(1e18)), // 10000 ETH Balance: new(big.Int).Mul(big.NewInt(devAccountBalance), big.NewInt(1e18)),
}) })
} }
return devAccounts, nil
}
// DeveloperGenesisBlock returns the 'geth --dev' genesis block.
func DeveloperGenesisBlock(gasLimit uint64, faucet *common.Address) *Genesis {
config := *params.AllDevChainProtocolChanges
devAccounts, err := GenerateDevAccounts()
if err != nil {
panic(fmt.Errorf("failed to generate dev accounts: %w", err))
}
alloc := make(map[common.Address]types.Account) alloc := make(map[common.Address]types.Account)
// Prefund dev accounts // Prefund dev accounts
@ -676,23 +691,12 @@ func DeveloperGenesisBlock(gasLimit uint64, faucet *common.Address) *Genesis {
alloc[acct.Address] = types.Account{Balance: acct.Balance} alloc[acct.Address] = types.Account{Balance: acct.Balance}
} }
// Add precompiles and system contracts // Add precompiles and system contracts
alloc[common.BytesToAddress([]byte{0x01})] = types.Account{Balance: big.NewInt(1)} precompileAddrs := [][]byte{
alloc[common.BytesToAddress([]byte{0x02})] = types.Account{Balance: big.NewInt(1)} {0x01}, {0x02}, {0x03}, {0x04}, {0x05}, {0x06}, {0x07}, {0x08}, {0x09}, {0x0a}, {0x0b}, {0x0c}, {0x0d}, {0x0e}, {0x0f}, {0x10}, {0x11},
alloc[common.BytesToAddress([]byte{0x03})] = types.Account{Balance: big.NewInt(1)} }
alloc[common.BytesToAddress([]byte{0x04})] = types.Account{Balance: big.NewInt(1)} for _, addr := range precompileAddrs {
alloc[common.BytesToAddress([]byte{0x05})] = types.Account{Balance: big.NewInt(1)} alloc[common.BytesToAddress(addr)] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x06})] = types.Account{Balance: big.NewInt(1)} }
alloc[common.BytesToAddress([]byte{0x07})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x08})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x09})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x0a})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x0b})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x0c})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x0d})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x0e})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x0f})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x10})] = types.Account{Balance: big.NewInt(1)}
alloc[common.BytesToAddress([]byte{0x11})] = types.Account{Balance: big.NewInt(1)}
alloc[params.BeaconRootsAddress] = types.Account{Nonce: 1, Code: params.BeaconRootsCode, Balance: common.Big0} alloc[params.BeaconRootsAddress] = types.Account{Nonce: 1, Code: params.BeaconRootsCode, Balance: common.Big0}
alloc[params.HistoryStorageAddress] = types.Account{Nonce: 1, Code: params.HistoryStorageCode, Balance: common.Big0} alloc[params.HistoryStorageAddress] = types.Account{Nonce: 1, Code: params.HistoryStorageCode, Balance: common.Big0}
alloc[params.WithdrawalQueueAddress] = types.Account{Nonce: 1, Code: params.WithdrawalQueueCode, Balance: common.Big0} alloc[params.WithdrawalQueueAddress] = types.Account{Nonce: 1, Code: params.WithdrawalQueueCode, Balance: common.Big0}
@ -702,20 +706,6 @@ func DeveloperGenesisBlock(gasLimit uint64, faucet *common.Address) *Genesis {
alloc[*faucet] = types.Account{Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))} alloc[*faucet] = types.Account{Balance: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(9))}
} }
// Print accounts and keys
fmt.Println("Available Accounts")
fmt.Println("==================")
for i, acct := range devAccounts {
ethBal := new(big.Float).Quo(new(big.Float).SetInt(acct.Balance), big.NewFloat(1e18))
fmt.Printf("(%d) %s (%s ETH)\n", i, acct.Address.Hex(), ethBal.Text('f', 18))
}
fmt.Println()
fmt.Println("Private Keys")
fmt.Println("==================")
for i, acct := range devAccounts {
fmt.Printf("(%d) 0x%s\n", i, hex.EncodeToString(crypto.FromECDSA(acct.PrivateKey)))
}
genesis := &Genesis{ genesis := &Genesis{
Config: &config, Config: &config,
GasLimit: gasLimit, GasLimit: gasLimit,