-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathprivatekey_am.go
204 lines (168 loc) · 5.57 KB
/
privatekey_am.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package sdk
import (
"crypto/ecdsa"
"encoding/hex"
"sync"
"time"
"github.com/Conflux-Chain/go-conflux-sdk/types"
sdkErrors "github.com/Conflux-Chain/go-conflux-sdk/types/errors"
"github.com/Conflux-Chain/go-conflux-sdk/utils/addressutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/openweb3/go-sdk-common/privatekeyhelper"
"github.com/pkg/errors"
)
type PrivatekeyAccountManager struct {
accountsMap map[string]*ecdsa.PrivateKey
addresses []types.Address
mutex sync.Mutex
networkID uint32
}
func NewPrivatekeyAccountManager(privateKeys []string, networkID uint32) *PrivatekeyAccountManager {
p := &PrivatekeyAccountManager{
networkID: networkID,
accountsMap: make(map[string]*ecdsa.PrivateKey),
}
for _, k := range privateKeys {
p.ImportKey(k, "")
}
return p
}
func (p *PrivatekeyAccountManager) Create(passphrase string) (types.Address, error) {
key, err := privatekeyhelper.NewRandom()
if err != nil {
return types.Address{}, err
}
addr := p.pushAccount(key)
return addr, nil
}
func (p *PrivatekeyAccountManager) Import(keystoreFile string, passphrase string, newPassphrase string) (types.Address, error) {
key, err := privatekeyhelper.NewFromKeystoreFile(keystoreFile, passphrase)
if err != nil {
return types.Address{}, err
}
addr := p.pushAccount(key)
return addr, nil
}
// Note: the passphrase will not be used; it is only for interface compatibility.
func (p *PrivatekeyAccountManager) ImportKey(keyString string, passphrase string) (types.Address, error) {
key, err := privatekeyhelper.NewFromKeyString(keyString)
if err != nil {
return types.Address{}, err
}
addr := p.pushAccount(key)
return addr, nil
}
// Note: the passphrase will not be used; it is only for interface compatibility.
func (p *PrivatekeyAccountManager) Export(address types.Address, passphrase string) (string, error) {
if !p.Contains(address) {
return "", sdkErrors.NewAccountNotFoundError(address)
}
keystr := hex.EncodeToString(crypto.FromECDSA(p.accountsMap[address.String()]))
return "0x" + keystr, nil
}
// Note: the passphrase will not be used; it is only for interface compatibility.
func (p *PrivatekeyAccountManager) Delete(address types.Address, passphrase string) error {
p.mutex.Lock()
defer p.mutex.Unlock()
if !p.Contains(address) {
return sdkErrors.NewAccountNotFoundError(address)
}
delete(p.accountsMap, address.GetHexAddress())
for i, addr := range p.addresses {
if addr.Equals(&address) {
p.addresses = append(p.addresses[:i], p.addresses[i+1:]...)
break
}
}
return nil
}
func (p *PrivatekeyAccountManager) Update(address types.Address, passphrase string, newPassphrase string) error {
return nil
}
func (p *PrivatekeyAccountManager) List() []types.Address {
return p.addresses
}
func (p *PrivatekeyAccountManager) Contains(address types.Address) bool {
_, ok := p.accountsMap[address.GetHexAddress()]
return ok
}
func (p *PrivatekeyAccountManager) GetDefault() (*types.Address, error) {
if len(p.List()) == 0 {
return nil, sdkErrors.ErrEmptyAddresses
}
return &p.List()[0], nil
}
func (p *PrivatekeyAccountManager) Unlock(address types.Address, passphrase string) error {
return nil
}
func (p *PrivatekeyAccountManager) UnlockDefault(passphrase string) error {
return nil
}
func (p *PrivatekeyAccountManager) TimedUnlock(address types.Address, passphrase string, timeout time.Duration) error {
return nil
}
func (p *PrivatekeyAccountManager) TimedUnlockDefault(passphrase string, timeout time.Duration) error {
return nil
}
func (p *PrivatekeyAccountManager) Lock(address types.Address) error {
return nil
}
func (p *PrivatekeyAccountManager) SignTransaction(tx types.UnsignedTransaction) ([]byte, error) {
return p.SignAndEcodeTransactionWithPassphrase(tx, "")
}
func (p *PrivatekeyAccountManager) SignAndEcodeTransactionWithPassphrase(tx types.UnsignedTransaction, passphrase string) ([]byte, error) {
v, r, s, err := p.Sign(tx, passphrase)
if err != nil {
return nil, err
}
encoded, err := tx.EncodeWithSignature(v, r, s)
if err != nil {
return nil, errors.Wrap(err, errMsgEncodeSignature)
}
return encoded, nil
}
func (p *PrivatekeyAccountManager) SignTransactionWithPassphrase(tx types.UnsignedTransaction, passphrase string) (types.SignedTransaction, error) {
v, r, s, err := p.Sign(tx, passphrase)
if err != nil {
return types.SignedTransaction{}, err
}
signdTx := types.SignedTransaction{}
signdTx.UnsignedTransaction = tx
signdTx.V = v
signdTx.R = r
signdTx.S = s
return signdTx, nil
}
func (p *PrivatekeyAccountManager) Sign(tx types.UnsignedTransaction, passphrase string) (v byte, r []byte, s []byte, err error) {
if tx.From == nil {
return 0, nil, nil, errors.New(errMsgFromAddressEmpty)
}
hash, err := tx.Hash()
if err != nil {
return 0, nil, nil, errors.Wrap(err, errMsgCalculateTxHash)
}
if !p.Contains(*tx.From) {
return 0, nil, nil, sdkErrors.NewAccountNotFoundError(*tx.From)
}
sig, err := crypto.Sign(hash, p.accountsMap[tx.From.GetHexAddress()])
if err != nil {
return 0, nil, nil, errors.Wrap(err, errMsgSignTx)
}
v = sig[64]
r = sig[0:32]
s = sig[32:64]
return v, r, s, nil
}
func (p *PrivatekeyAccountManager) pushAccount(key *ecdsa.PrivateKey) types.Address {
p.mutex.Lock()
defer p.mutex.Unlock()
cfxAddr := CfxAddressOfPrivateKey(key, p.networkID)
p.addresses = append(p.addresses, cfxAddr)
p.accountsMap[cfxAddr.GetHexAddress()] = key
return cfxAddr
}
func CfxAddressOfPrivateKey(key *ecdsa.PrivateKey, networkID uint32) types.Address {
ethAddr := crypto.PubkeyToAddress(key.PublicKey)
cfxAddr := addressutil.EtherAddressToCfxAddress(ethAddr, false, networkID)
return cfxAddr
}