-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathnode.go
More file actions
182 lines (148 loc) · 4.41 KB
/
node.go
File metadata and controls
182 lines (148 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package wallet
import (
"context"
"crypto/ecdsa"
"encoding/json"
"errors"
"fmt"
"math/big"
"github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
// Get the node account
func (w *Wallet) GetNodeAccount() (accounts.Account, error) {
// Check wallet is initialized
if !w.IsInitialized() {
return accounts.Account{}, errors.New("Wallet is not initialized")
}
if w.Offline() {
return *w.nodeAddress, nil
}
// Get private key
privateKey, path, err := w.getNodePrivateKey()
if err != nil {
return accounts.Account{}, err
}
// Get public key
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return accounts.Account{}, errors.New("Could not get node public key")
}
// Create & return account
return accounts.Account{
Address: crypto.PubkeyToAddress(*publicKeyECDSA),
URL: accounts.URL{
Scheme: "",
Path: path,
},
}, nil
}
// Get a transactor for the node account
func (w *Wallet) GetNodeAccountTransactor() (*bind.TransactOpts, error) {
// Check wallet is initialized
if !w.IsInitialized() {
return nil, errors.New("Wallet is not initialized")
}
if w.Offline() {
var transactor bind.TransactOpts
transactor.From = w.nodeAddress.Address
transactor.Context = context.Background()
transactor.GasFeeCap = w.maxFee
transactor.GasTipCap = w.maxPriorityFee
transactor.GasLimit = w.gasLimit
transactor.Signer = func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
txJSON, err := json.MarshalIndent(tx, "", " ")
if err != nil {
return tx, err
}
fmt.Printf("Offline mode: this transaction would have been signed by %s:\n%s\n", address.String(), string(txJSON))
return nil, fmt.Errorf("Offline mode - transaction not signed")
}
return &transactor, nil
}
// Get private key
privateKey, _, err := w.getNodePrivateKey()
if err != nil {
return nil, err
}
// Create & return transactor
transactor, err := bind.NewKeyedTransactorWithChainID(privateKey, w.chainID)
transactor.GasFeeCap = w.maxFee
transactor.GasTipCap = w.maxPriorityFee
transactor.GasLimit = w.gasLimit
transactor.Context = context.Background()
return transactor, err
}
// Get the node account private key bytes
func (w *Wallet) GetNodePrivateKeyBytes() ([]byte, error) {
// Check wallet is initialized
if !w.IsInitialized() {
return nil, errors.New("Wallet is not initialized")
}
// Get private key
privateKey, _, err := w.getNodePrivateKey()
if err != nil {
return nil, err
}
// Return private key bytes
return crypto.FromECDSA(privateKey), nil
}
// Get the node private key
func (w *Wallet) getNodePrivateKey() (*ecdsa.PrivateKey, string, error) {
// Check for cached node key
if w.nodeKey != nil {
return w.nodeKey, w.nodeKeyPath, nil
}
// Get derived key
derivedKey, path, err := w.getNodeDerivedKey(w.ws.WalletIndex)
if err != nil {
return nil, "", err
}
// Get private key
privateKey, err := derivedKey.ECPrivKey()
if err != nil {
return nil, "", fmt.Errorf("Could not get node private key: %w", err)
}
privateKeyECDSA := privateKey.ToECDSA()
// Cache node key
w.nodeKey = privateKeyECDSA
w.nodeKeyPath = path
// Return
return privateKeyECDSA, path, nil
}
// Get the derived key & derivation path for the node account at the index
func (w *Wallet) getNodeDerivedKey(index uint) (*hdkeychain.ExtendedKey, string, error) {
// Get derivation path
if w.ws.DerivationPath == "" {
w.ws.DerivationPath = DefaultNodeKeyPath
}
derivationPath := fmt.Sprintf(w.ws.DerivationPath, index)
// Parse derivation path
path, err := accounts.ParseDerivationPath(derivationPath)
if err != nil {
return nil, "", fmt.Errorf("Invalid node key derivation path '%s': %w", derivationPath, err)
}
// Follow derivation path
key := w.mk
for i, n := range path {
// Use the legacy implementation for Goerli
// TODO: remove this if Prater ever goes away!
if w.chainID.Cmp(big.NewInt(5)) == 0 {
key, err = key.DeriveNonStandard(n)
} else {
key, err = key.Derive(n)
}
if err == hdkeychain.ErrInvalidChild {
return w.getNodeDerivedKey(index + 1)
} else if err != nil {
return nil, "", fmt.Errorf("Invalid child key at depth %d: %w", i, err)
}
}
// Return
return key, derivationPath, nil
}