forked from ten-protocol/go-ten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.go
113 lines (90 loc) · 3.1 KB
/
wallet.go
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
package wallet
import (
"crypto/ecdsa"
"fmt"
"math/big"
"sync/atomic"
gethlog "github.com/ethereum/go-ethereum/log"
"github.com/ten-protocol/go-ten/go/common/log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
type Wallet interface {
// Address returns the pubkey Address of the wallet
Address() common.Address
// SignTransaction returns a signed transaction
SignTransaction(tx types.TxData) (*types.Transaction, error)
SignTransactionForChainID(tx types.TxData, chainID *big.Int) (*types.Transaction, error)
// SetNonce overrides the current nonce
// The GetTransactionCount is expected to be the next nonce to use in a transaction, not the current account GetTransactionCount
SetNonce(nonce uint64)
// GetNonceAndIncrement atomically increments the nonce by one and returns the previous value
GetNonceAndIncrement() uint64
GetNonce() uint64
ChainID() *big.Int
// PrivateKey returns the wallets private key
PrivateKey() *ecdsa.PrivateKey
}
type inMemoryWallet struct {
prvKey *ecdsa.PrivateKey
pubKey *ecdsa.PublicKey
pubKeyAddr common.Address
nonce uint64
chainID *big.Int
logger gethlog.Logger
}
func NewInMemoryWalletFromPK(chainID *big.Int, pk *ecdsa.PrivateKey, logger gethlog.Logger) Wallet {
publicKeyECDSA, ok := pk.Public().(*ecdsa.PublicKey)
if !ok {
logger.Crit("error casting public key to ECDSA")
}
return &inMemoryWallet{
chainID: chainID,
prvKey: pk,
pubKey: publicKeyECDSA,
pubKeyAddr: crypto.PubkeyToAddress(*publicKeyECDSA),
logger: logger,
}
}
func NewInMemoryWalletFromConfig(pkStr string, l1ChainID int64, logger gethlog.Logger) Wallet {
privateKey, err := crypto.HexToECDSA(pkStr)
if err != nil {
logger.Crit("could not recover private key from hex. ", log.ErrKey, err)
}
return NewInMemoryWalletFromPK(big.NewInt(l1ChainID), privateKey, logger)
}
// SignTransaction returns a signed transaction
func (m *inMemoryWallet) SignTransaction(tx types.TxData) (*types.Transaction, error) {
return types.SignNewTx(m.prvKey, types.NewCancunSigner(m.chainID), tx)
}
func (m *inMemoryWallet) SignTransactionForChainID(tx types.TxData, chainID *big.Int) (*types.Transaction, error) {
return types.SignNewTx(m.prvKey, types.NewLondonSigner(chainID), tx)
}
// Address returns the current wallet address
func (m *inMemoryWallet) Address() common.Address {
return m.pubKeyAddr
}
func (m *inMemoryWallet) ChainID() *big.Int {
return big.NewInt(0).Set(m.chainID)
}
func (m *inMemoryWallet) GetNonceAndIncrement() uint64 {
return atomic.AddUint64(&m.nonce, 1) - 1
}
func (m *inMemoryWallet) GetNonce() uint64 {
return atomic.LoadUint64(&m.nonce)
}
func (m *inMemoryWallet) SetNonce(nonce uint64) {
m.nonce = nonce
}
func (m *inMemoryWallet) PrivateKey() *ecdsa.PrivateKey {
return m.prvKey
}
func RetrieveAddress(pkStr string) (*common.Address, error) {
privateKey, err := crypto.HexToECDSA(pkStr)
if err != nil {
return nil, fmt.Errorf("could not recover private key from hex - %w", err)
}
addr := crypto.PubkeyToAddress(privateKey.PublicKey)
return &addr, nil
}