Skip to content

Commit

Permalink
Provide more debug information when obtaining accounts.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed Feb 3, 2025
1 parent 16548d9 commit 8097dba
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 28 deletions.
29 changes: 20 additions & 9 deletions cli/wallet.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 - 2023 Weald Technology Trading Limited
// Copyright 2017 - 2025 Weald Technology Trading Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,31 +58,31 @@ func ObtainWallets(chainID *big.Int, debug bool) ([]accounts.Wallet, error) {
}

// ObtainWalletAndAccount obtains the wallet and account for an address.
func ObtainWalletAndAccount(chainID *big.Int, address common.Address) (accounts.Wallet, *accounts.Account, error) {
func ObtainWalletAndAccount(chainID *big.Int, address common.Address, debug bool) (accounts.Wallet, *accounts.Account, error) {
var account *accounts.Account
wallet, err := ObtainWallet(chainID, address)
wallet, err := ObtainWallet(chainID, address, debug)
if err == nil {
account, err = ObtainAccount(&wallet, &address, viper.GetString("passphrase"))
}
return wallet, account, err
}

// ObtainWallet fetches the wallet for a given address.
func ObtainWallet(chainID *big.Int, address common.Address) (accounts.Wallet, error) {
wallet, err := obtainGethWallet(chainID, address)
func ObtainWallet(chainID *big.Int, address common.Address, debug bool) (accounts.Wallet, error) {
wallet, err := obtainGethWallet(chainID, address, debug)
if err == nil {
return wallet, nil
}

wallet, err = obtainParityWallet(address)
wallet, err = obtainParityWallet(address, debug)
if err == nil {
return wallet, nil
}

return wallet, fmt.Errorf("failed to obtain wallet for %s", address.Hex())
return wallet, errors.Join(fmt.Errorf("failed to obtain wallet for %s", address.Hex()), err)
}

func obtainGethWallet(chainID *big.Int, address common.Address) (accounts.Wallet, error) {
func obtainGethWallet(chainID *big.Int, address common.Address, debug bool) (accounts.Wallet, error) {
keydir := DefaultDataDir()
switch {
case chainID.Cmp(params.MainnetChainConfig.ChainID) == 0:
Expand All @@ -93,11 +93,18 @@ func obtainGethWallet(chainID *big.Int, address common.Address) (accounts.Wallet
keydir = filepath.Join(keydir, "holesky")
}
keydir = filepath.Join(keydir, "keystore")
if debug {
fmt.Fprintf(os.Stderr, "Looking in %s for %s\n", address.String(), keydir)
}
backends := []accounts.Backend{keystore.NewKeyStore(keydir, keystore.StandardScryptN, keystore.StandardScryptP)}
accountManager := accounts.NewManager(nil, backends...)
defer accountManager.Close()
account := accounts.Account{Address: address}
wallet, err := accountManager.Find(account)
if debug && err == nil {
fmt.Fprintf(os.Stderr, "Obtained account\n")
}

return wallet, err
}

Expand All @@ -121,7 +128,7 @@ func obtainGethWallets(chainID *big.Int, debug bool) ([]accounts.Wallet, error)
return accountManager.Wallets(), nil
}

func obtainParityWallet(address common.Address) (accounts.Wallet, error) {
func obtainParityWallet(address common.Address, debug bool) (accounts.Wallet, error) {
keydir, err := homedir.Dir()
if err != nil {
return nil, fmt.Errorf("failed to find home directory")
Expand All @@ -137,6 +144,10 @@ func obtainParityWallet(address common.Address) (accounts.Wallet, error) {
return nil, fmt.Errorf("unsupported operating system")
}

if debug {
fmt.Printf("Parity key directory is %s\n", keydir)
}

backends := []accounts.Backend{keystore.NewKeyStore(keydir, keystore.StandardScryptN, keystore.StandardScryptP)}
accountManager := accounts.NewManager(nil, backends...)
defer accountManager.Close()
Expand Down
2 changes: 1 addition & 1 deletion cmd/accountkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ In quiet mode this will return 0 if the account was successfully decoded, otherw
cli.ErrCheck(err, quiet, "Invalid private key")
} else {
addr := common.HexToAddress(address)
key, err = util.PrivateKeyForAccount(c.ChainID(), addr, passphrase)
key, err = util.PrivateKeyForAccount(c.ChainID(), addr, passphrase, debug)
}
cli.ErrCheck(err, quiet, "Failed to access account")
if quiet {
Expand Down
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ func connect(ctx context.Context) error {

if offline {
// Handle offline connection.
c, err = conn.New(ctx, "offline")
c, err = conn.New(ctx, "offline", debug)
} else {
var address string
address, err = connectionAddress(ctx)
if err == nil {
c, err = conn.New(ctx, address)
c, err = conn.New(ctx, address, debug)
}
}
if err != nil {
Expand Down Expand Up @@ -397,7 +397,7 @@ func generateTxOpts(sender common.Address) (*bind.TransactOpts, error) {
// Signer depends on what information is available to us.
var signer bind.SignerFn
if viper.GetString("passphrase") != "" {
wallet, account, err := cli.ObtainWalletAndAccount(c.ChainID(), sender)
wallet, account, err := cli.ObtainWalletAndAccount(c.ChainID(), sender, debug)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/signaturesign.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ provided below:
var err error
if signatureSignPassphrase != "" {
signer := common.HexToAddress(signatureSignSigner)
key, err = util.PrivateKeyForAccount(c.ChainID(), signer, signatureSignPassphrase)
key, err = util.PrivateKeyForAccount(c.ChainID(), signer, signatureSignPassphrase, debug)
cli.ErrCheck(err, quiet, "Invalid account or passphrse")
} else if signatureSignPrivateKey != "" {
key, err = crypto.HexToECDSA(strings.TrimPrefix(signatureSignPrivateKey, "0x"))
Expand Down
9 changes: 6 additions & 3 deletions conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
// Conn is a connection to an Ethereum execution client.
type Conn struct {
timeout time.Duration
debug bool

rpcClient *rpc.Client
client *ethclient.Client
Expand All @@ -48,10 +49,10 @@ type Conn struct {
}

// New creates a new execution client.
func New(ctx context.Context, url string) (*Conn, error) {
func New(ctx context.Context, url string, debug bool) (*Conn, error) {
if url == "offline" {
// We are offline...
return newOffline(ctx)
return newOffline(ctx, debug)
}

rpcClient, err := rpc.DialContext(ctx, url)
Expand All @@ -76,6 +77,7 @@ func New(ctx context.Context, url string) (*Conn, error) {
}

conn := &Conn{
debug: debug,
timeout: timeout,
rpcClient: rpcClient,
client: client,
Expand All @@ -86,7 +88,7 @@ func New(ctx context.Context, url string) (*Conn, error) {
return conn, nil
}

func newOffline(_ context.Context) (*Conn, error) {
func newOffline(_ context.Context, debug bool) (*Conn, error) {
var chainID *big.Int
if viper.GetString("network") == "" && viper.GetString("chainid") == "" {
return nil, errors.New("network or chainid is required when offline")
Expand Down Expand Up @@ -118,6 +120,7 @@ func newOffline(_ context.Context) (*Conn, error) {
}

return &Conn{
debug: debug,
offline: true,
chainID: chainID,
nonces: make(map[common.Address]uint64),
Expand Down
11 changes: 10 additions & 1 deletion conn/signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package conn

import (
"context"
"fmt"
"os"
"strings"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -29,14 +31,18 @@ import (
func (c *Conn) SignTransaction(_ context.Context,
signer common.Address,
tx *types.Transaction,
debug bool,
) (
*types.Transaction,
error,
) {
var signedTx *types.Transaction
switch {
case viper.GetString("passphrase") != "":
wallet, account, err := cli.ObtainWalletAndAccount(c.ChainID(), signer)
if c.debug {
fmt.Fprintf(os.Stderr, "Signing for account %s with passphrase\n", signer.String())
}
wallet, account, err := cli.ObtainWalletAndAccount(c.ChainID(), signer, debug)
if err != nil {
return nil, err
}
Expand All @@ -45,6 +51,9 @@ func (c *Conn) SignTransaction(_ context.Context,
return nil, err
}
case viper.GetString("privatekey") != "":
if c.debug {
fmt.Fprintf(os.Stderr, "Signing for account %s with private key\n", signer.String())
}
key, err := crypto.HexToECDSA(strings.TrimPrefix(viper.GetString("privatekey"), "0x"))
if err != nil {
return nil, errors.Wrap(err, "invalid private key")
Expand Down
2 changes: 1 addition & 1 deletion conn/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *Conn) CreateSignedTransaction(ctx context.Context,
}

// Sign the transaction.
signedTx, err := c.SignTransaction(ctx, txData.From, tx)
signedTx, err := c.SignTransaction(ctx, txData.From, tx, c.debug)
if err != nil {
err = fmt.Errorf("failed to sign transaction: %v", err)
return nil, err
Expand Down
1 change: 1 addition & 0 deletions util/funcparser/Tester.bin

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion util/funcparser/Tester.json

This file was deleted.

2 changes: 2 additions & 0 deletions util/funcparser/Tester.sol
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// SPDX-License-Identifier: Apache-2.0

pragma solidity ^0.8.20;
pragma experimental ABIEncoderV2;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2019, 2022 Weald Technology Trading
// Copyright © 2019 - 2025 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -28,7 +28,7 @@ import (
"github.com/wealdtech/ethereal/v2/util"
)

func TestGoerli(t *testing.T) {
func TestSepolia(t *testing.T) {
tests := []struct {
json string
input string
Expand Down Expand Up @@ -141,17 +141,17 @@ func TestGoerli(t *testing.T) {
},
}

// Connect to goerli
conn, err := ethclient.Dial("https://goerli.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
require.Nil(t, err, "failed to connect to goerli")
// Connect to sepolia.
conn, err := ethclient.Dial("https://sepolia.infura.io/v3/831a5442dc2e4536a9f8dee4ea1707a6")
require.Nil(t, err, "failed to connect to sepolia")

json, err := os.ReadFile("Tester.json")
require.Nil(t, err, "failed to read Tester ABI")
contract, err := util.ParseCombinedJSON(string(json), "Tester")
require.Nil(t, err, "failed to parse contract JSON")

fromAddress := common.HexToAddress("0x0000000000000000000000000000000000000000")
contractAddress := common.HexToAddress("0xf942De0Be16b8B30C07C4D0d3Fca725277306892")
contractAddress := common.HexToAddress("0x30706C450c39cAE416Ae01B024Fc1A4A184ea19E")
for i, test := range tests {
method, args, err := ParseCall(conn, contract, test.input)
require.Nil(t, err, fmt.Sprintf("failed to parse call at test %d", i))
Expand Down
4 changes: 2 additions & 2 deletions util/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
)

// PrivateKeyForAccount returns the private key for an account.
func PrivateKeyForAccount(chainID *big.Int, address common.Address, passphrase string) (*ecdsa.PrivateKey, error) {
func PrivateKeyForAccount(chainID *big.Int, address common.Address, passphrase string, debug bool) (*ecdsa.PrivateKey, error) {
var account *accounts.Account
_, account, err := cli.ObtainWalletAndAccount(chainID, address)
_, account, err := cli.ObtainWalletAndAccount(chainID, address, debug)
if err != nil {
return nil, fmt.Errorf("unable to find account %s", address.Hex())
}
Expand Down

0 comments on commit 8097dba

Please sign in to comment.