Skip to content

Commit 9b4a87e

Browse files
committed
upgrade deps, and clean up ethwallet a bit
1 parent 889cce4 commit 9b4a87e

File tree

6 files changed

+118
-171
lines changed

6 files changed

+118
-171
lines changed

cmd/ethkit/wallet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func init() {
3232
cmd.Flags().Bool("print-private-key", false, "print wallet private key from keyfile (danger!)")
3333
cmd.Flags().Bool("import-mnemonic", false, "import a secret mnemonic to a new keyfile")
3434
cmd.Flags().Int("entropy", 256, "set entropy bit size for new wallet, options: 128, 256 (default: 256)")
35-
cmd.Flags().String("path", "", fmt.Sprintf("set derivation path, default: %s", ethwallet.DefaultWalletOptions.DerivationPath))
35+
cmd.Flags().String("path", "", fmt.Sprintf("set derivation path, default: %s", ethwallet.DefaultWalletOptions().DerivationPath))
3636

3737
rootCmd.AddCommand(cmd)
3838
}
@@ -185,7 +185,7 @@ func (c *wallet) createNew() error {
185185

186186
derivationPath := c.fPath
187187
if derivationPath == "" {
188-
derivationPath = ethwallet.DefaultWalletOptions.DerivationPath
188+
derivationPath = ethwallet.DefaultWalletOptions().DerivationPath
189189
}
190190

191191
c.wallet, err = getWallet(importMnemonic, derivationPath, c.fEntropy)

ethwallet/ethwallet.go

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,25 @@ import (
1818
"github.com/0xsequence/ethkit/go-ethereum/crypto"
1919
)
2020

21-
var DefaultWalletOptions = WalletOptions{
22-
DerivationPath: "m/44'/60'/0'/0/0",
23-
RandomWalletEntropyBitSize: EntropyBitSize12WordMnemonic,
21+
func DefaultWalletOptions() WalletOptions {
22+
return WalletOptions{
23+
DerivationPath: "m/44'/60'/0'/0/0",
24+
RandomWalletEntropyBitSize: EntropyBitSize12WordMnemonic,
25+
}
2426
}
2527

2628
type Wallet struct {
27-
hdnode *HDNode
28-
provider *ethrpc.Provider
29-
walletProvider *WalletProvider
29+
hdnode *HDNode
30+
provider *ethrpc.Provider
3031
}
3132

3233
type WalletOptions struct {
3334
DerivationPath string
3435
RandomWalletEntropyBitSize int
3536
}
3637

37-
func NewWalletFromPrivateKey(key string) (*Wallet, error) {
38-
hdnode, err := NewHDNodeFromPrivateKey(key)
38+
func NewWalletFromPrivateKey(hexPrivateKey string) (*Wallet, error) {
39+
hdnode, err := NewHDNodeFromPrivateKey(hexPrivateKey)
3940
if err != nil {
4041
return nil, err
4142
}
@@ -58,7 +59,7 @@ func NewWalletFromHDNode(hdnode *HDNode, optPath ...accounts.DerivationPath) (*W
5859
}
5960

6061
func NewWalletFromRandomEntropy(options ...WalletOptions) (*Wallet, error) {
61-
opts := DefaultWalletOptions
62+
opts := DefaultWalletOptions()
6263
if len(options) > 0 {
6364
opts = options[0]
6465
}
@@ -112,6 +113,14 @@ func (w *Wallet) Clone() (*Wallet, error) {
112113
}, nil
113114
}
114115

116+
func (w *Wallet) GetProvider() *ethrpc.Provider {
117+
return w.provider
118+
}
119+
120+
func (w *Wallet) SetProvider(provider *ethrpc.Provider) {
121+
w.provider = provider
122+
}
123+
115124
func (w *Wallet) Transactor(ctx context.Context) (*bind.TransactOpts, error) {
116125
var chainID *big.Int
117126
if w.provider != nil {
@@ -126,30 +135,13 @@ func (w *Wallet) Transactor(ctx context.Context) (*bind.TransactOpts, error) {
126135

127136
func (w *Wallet) TransactorForChainID(chainID *big.Int) (*bind.TransactOpts, error) {
128137
if chainID == nil {
129-
// This is deprecated and will log a warning since it uses the original Homestead signer
130-
return bind.NewKeyedTransactor(w.hdnode.PrivateKey()), nil
138+
return nil, fmt.Errorf("ethwallet: chainID is required")
131139
} else {
132140
return bind.NewKeyedTransactorWithChainID(w.hdnode.PrivateKey(), chainID)
133141
}
134142
}
135143

136-
func (w *Wallet) GetProvider() *ethrpc.Provider {
137-
return w.provider
138-
}
139-
140-
func (w *Wallet) SetProvider(provider *ethrpc.Provider) {
141-
w.provider = provider
142-
143-
if w.walletProvider == nil {
144-
w.walletProvider = &WalletProvider{wallet: w}
145-
}
146-
w.walletProvider.provider = provider
147-
}
148-
149-
func (w *Wallet) Provider() *WalletProvider {
150-
return w.walletProvider
151-
}
152-
144+
// TODOXXX: deprecate?
153145
func (w *Wallet) SelfDerivePath(path accounts.DerivationPath) (common.Address, error) {
154146
err := w.hdnode.DerivePath(path)
155147
if err != nil {
@@ -227,16 +219,33 @@ func (w *Wallet) PublicKeyHex() string {
227219
return hexutil.Encode(publicKeyBytes)
228220
}
229221

230-
func (w *Wallet) GetBalance(ctx context.Context) (*big.Int, error) {
231-
return w.GetProvider().BalanceAt(ctx, w.Address(), nil)
222+
func (w *Wallet) GetBalance(ctx context.Context, optBlockNum ...*big.Int) (*big.Int, error) {
223+
var blockNum *big.Int = nil
224+
if len(optBlockNum) > 0 {
225+
blockNum = optBlockNum[0]
226+
}
227+
return w.GetProvider().BalanceAt(ctx, w.Address(), blockNum)
232228
}
233229

234230
func (w *Wallet) GetNonce(ctx context.Context) (uint64, error) {
235-
return w.GetProvider().NonceAt(ctx, w.Address(), nil)
231+
if w.provider == nil {
232+
return 0, fmt.Errorf("ethwallet: provider is not set")
233+
}
234+
return w.provider.NonceAt(ctx, w.Address(), nil)
236235
}
237236

238-
// TODO: rename this to SignTransaction, but add SignTx for backwards compatibility and add deprecation warning
239-
func (w *Wallet) SignTx(tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
237+
func (w *Wallet) GetTransactionCount(ctx context.Context) (uint64, error) {
238+
if w.provider == nil {
239+
return 0, fmt.Errorf("ethwallet: provider is not set")
240+
}
241+
nonce, err := w.provider.PendingNonceAt(ctx, w.Address())
242+
if err != nil {
243+
return 0, err
244+
}
245+
return nonce, nil
246+
}
247+
248+
func (w *Wallet) SignTransaction(tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
240249
signer := types.LatestSignerForChainID(chainID)
241250
signedTx, err := types.SignTx(tx, signer, w.hdnode.PrivateKey())
242251
if err != nil {
@@ -256,6 +265,11 @@ func (w *Wallet) SignTx(tx *types.Transaction, chainID *big.Int) (*types.Transac
256265
return signedTx, nil
257266
}
258267

268+
// Deprecated: use SignTransaction instead
269+
func (w *Wallet) SignTx(tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
270+
return w.SignTransaction(tx, chainID)
271+
}
272+
259273
// SignMessage signs a message with EIP-191 prefix with the wallet's private key.
260274
//
261275
// This is the same as SignData, but it adds the prefix "Ethereum Signed Message:\n" to
@@ -365,10 +379,10 @@ func (w *Wallet) NewTransaction(ctx context.Context, txnRequest *ethtxn.Transact
365379
return signedTx, nil
366380
}
367381

368-
func (w *Wallet) SendTransaction(ctx context.Context, signedTx *types.Transaction) (*types.Transaction, ethtxn.WaitReceipt, error) {
382+
func (w *Wallet) SendTransaction(ctx context.Context, signedTxn *types.Transaction) (*types.Transaction, ethtxn.WaitReceipt, error) {
369383
provider := w.GetProvider()
370384
if provider == nil {
371-
return nil, nil, fmt.Errorf("ethwallet (SendTransaction): provider is not set")
385+
return nil, nil, fmt.Errorf("ethwallet: provider is not set")
372386
}
373-
return ethtxn.SendTransaction(ctx, provider, signedTx)
387+
return ethtxn.SendTransaction(ctx, provider, signedTxn)
374388
}

ethwallet/hdnode.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ const (
2323
// DefaultBaseDerivationPath is the base path from which custom derivation endpoints
2424
// are incremented. As such, the first account will be at m/44'/60'/0'/0/0, the second
2525
// at m/44'/60'/0'/0/1, etc.
26-
var defaultBaseDerivationPath = accounts.DefaultBaseDerivationPath
27-
2826
func DefaultBaseDerivationPath() accounts.DerivationPath {
29-
derivationPath := make(accounts.DerivationPath, len(defaultBaseDerivationPath))
30-
copy(derivationPath, defaultBaseDerivationPath)
27+
derivationPath := make(accounts.DerivationPath, len(accounts.DefaultBaseDerivationPath))
28+
copy(derivationPath, accounts.DefaultBaseDerivationPath)
3129
return derivationPath
3230
}
3331

@@ -43,8 +41,8 @@ type HDNode struct {
4341
address common.Address
4442
}
4543

46-
func NewHDNodeFromPrivateKey(privateKey string) (*HDNode, error) {
47-
key, err := crypto.HexToECDSA(privateKey)
44+
func NewHDNodeFromPrivateKey(hexPrivateKey string) (*HDNode, error) {
45+
key, err := crypto.HexToECDSA(hexPrivateKey)
4846
if err != nil {
4947
return nil, err
5048
}

ethwallet/provider.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

go.mod

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/0xsequence/ethkit
22

3-
go 1.23.4
3+
go 1.24.0
44

55
toolchain go1.24.3
66

@@ -9,13 +9,13 @@ require (
99
github.com/btcsuite/btcd/btcec/v2 v2.3.5
1010
github.com/btcsuite/btcd/btcutil v1.1.6
1111
github.com/cespare/cp v1.1.1
12-
github.com/consensys/gnark-crypto v0.17.0
13-
github.com/crate-crypto/go-eth-kzg v1.3.0
12+
github.com/consensys/gnark-crypto v0.19.0
13+
github.com/crate-crypto/go-eth-kzg v1.4.0
1414
github.com/crate-crypto/go-kzg-4844 v1.1.0
1515
github.com/davecgh/go-spew v1.1.1
1616
github.com/deckarep/golang-set/v2 v2.8.0
1717
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0
18-
github.com/ethereum/c-kzg-4844/v2 v2.1.1
18+
github.com/ethereum/c-kzg-4844/v2 v2.1.5
1919
github.com/ethereum/go-verkle v0.2.2
2020
github.com/fsnotify/fsnotify v1.9.0
2121
github.com/go-chi/httpvcr v0.2.0
@@ -35,40 +35,37 @@ require (
3535
github.com/holiman/uint256 v1.3.2
3636
github.com/jedisct1/go-minisign v0.0.0-20241212093149-d2f9f49435c7
3737
github.com/kylelemons/godebug v1.1.0
38-
github.com/spf13/cobra v1.9.1
39-
github.com/stretchr/testify v1.10.0
38+
github.com/spf13/cobra v1.10.1
39+
github.com/stretchr/testify v1.11.1
4040
github.com/tyler-smith/go-bip39 v1.1.0
4141
github.com/zeebo/xxh3 v1.0.2
42-
golang.org/x/crypto v0.38.0
43-
golang.org/x/net v0.40.0
44-
golang.org/x/sync v0.14.0
45-
golang.org/x/sys v0.33.0
46-
golang.org/x/term v0.32.0
47-
golang.org/x/tools v0.33.0
42+
golang.org/x/crypto v0.43.0
43+
golang.org/x/net v0.46.0
44+
golang.org/x/sync v0.17.0
45+
golang.org/x/sys v0.37.0
46+
golang.org/x/term v0.36.0
47+
golang.org/x/tools v0.38.0
4848
)
4949

5050
require (
51-
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa
52-
golang.org/x/mod v0.24.0 // indirect
53-
golang.org/x/text v0.25.0 // indirect
51+
golang.org/x/exp v0.0.0-20251009144603-d2f985daa21b
52+
golang.org/x/mod v0.29.0 // indirect
53+
golang.org/x/text v0.30.0 // indirect
5454
)
5555

5656
require (
57-
github.com/bits-and-blooms/bitset v1.22.0 // indirect
57+
github.com/bits-and-blooms/bitset v1.24.0 // indirect
5858
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
5959
github.com/cespare/xxhash/v2 v2.3.0 // indirect
60-
github.com/consensys/bavard v0.1.30 // indirect
61-
github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c // indirect
60+
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a // indirect
6261
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
6362
github.com/elastic/go-freelru v0.16.0 // indirect
6463
github.com/goware/singleflight v0.3.0 // indirect
6564
github.com/inconshreveable/mousetrap v1.1.0 // indirect
66-
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
67-
github.com/mmcloughlin/addchain v0.4.0 // indirect
65+
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
6866
github.com/pmezard/go-difflib v1.0.0 // indirect
69-
github.com/redis/go-redis/v9 v9.7.3 // indirect
70-
github.com/spf13/pflag v1.0.6 // indirect
71-
github.com/supranational/blst v0.3.14 // indirect
67+
github.com/redis/go-redis/v9 v9.14.0 // indirect
68+
github.com/spf13/pflag v1.0.10 // indirect
69+
github.com/supranational/blst v0.3.16 // indirect
7270
gopkg.in/yaml.v3 v3.0.1 // indirect
73-
rsc.io/tmplfunc v0.0.3 // indirect
7471
)

0 commit comments

Comments
 (0)