-
Notifications
You must be signed in to change notification settings - Fork 5
/
wallet.go
287 lines (247 loc) · 7.19 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package service
import (
"github.com/drep-project/DREP-Chain/common/fileutil"
"github.com/drep-project/DREP-Chain/crypto"
"github.com/drep-project/DREP-Chain/crypto/secp256k1"
accountsComponent "github.com/drep-project/DREP-Chain/pkgs/accounts/component"
accountTypes "github.com/drep-project/DREP-Chain/pkgs/accounts/types"
"github.com/drep-project/DREP-Chain/types"
"github.com/pkg/errors"
)
const (
RPERMISSION = iota //read
WPERMISSION //write
)
const (
LOCKED = iota //locked
UNLOCKED //unlocked
)
//Wallet is used to manage private keys, build simple transactions and other functions.
type Wallet struct {
cacheStore *accountsComponent.CacheStore
chainId types.ChainIdType
config *accountTypes.Config
isLock int32
password string
}
// NewWallet based in config
func NewWallet(config *accountTypes.Config, chainId types.ChainIdType) (*Wallet, error) {
wallet := &Wallet{
config: config,
chainId: chainId,
}
wallet.password = config.Password
return wallet, nil
}
// OpenWallet wallet to use wallet
func (wallet *Wallet) OpenWallet(password string) error {
if wallet.cacheStore != nil {
return ErrClosedWallet
}
//cryptedPassword := wallet.cryptoPassword(wallet.password)
wallet.password = password
var store accountsComponent.KeyStore
if wallet.config.Type == "dbstore" {
store = accountsComponent.NewDbStore(wallet.config.KeyStoreDir)
} else if wallet.config.Type == "memorystore" {
store = accountsComponent.NewMemoryStore()
} else {
store = accountsComponent.NewFileStore(wallet.config.KeyStoreDir)
}
accountCacheStore, err := accountsComponent.NewCacheStore(store, password)
if err != nil {
log.WithField("err", err).Info("cache account err")
return nil
}
wallet.cacheStore = accountCacheStore
//wallet.unLock(wallet.config.Password)
//keys, err := wallet.cacheStore.ExportKey(cryptedPassword)
//if err != nil {
// log.WithField("err", err).Info("export key err")
//}
//if len(keys) == 0 {
// wallet.NewAccount()
//}
return nil
}
func (wallet *Wallet) UnlockAccount(addr *crypto.CommonAddress, password string) error {
return wallet.cacheStore.LoadKeys(addr, password)
}
// Close wallet to disable wallet
func (wallet *Wallet) Close() {
//wallet.Lock()
wallet.cacheStore = nil
wallet.password = ""
}
// NewAccount create new address
func (wallet *Wallet) NewAccount(password string) (*types.Node, error) {
if err := wallet.checkWallet(WPERMISSION); err != nil {
return nil, err
}
newNode := types.NewNode(nil, wallet.chainId)
wallet.cacheStore.StoreKey(newNode, password)
return newNode, nil
}
// GetAccountByAddress query account according to address
func (wallet *Wallet) GetAccountByAddress(addr *crypto.CommonAddress) (*types.Node, error) {
if err := wallet.checkWallet(RPERMISSION); err != nil {
return nil, ErrClosedWallet
}
return wallet.cacheStore.GetKey(addr)
}
// GetAccountByAddress query account according to public key
func (wallet *Wallet) GetAccountByPubkey(pubkey *secp256k1.PublicKey) (*types.Node, error) {
if err := wallet.checkWallet(RPERMISSION); err != nil {
return nil, ErrClosedWallet
}
addr := crypto.PubkeyToAddress(pubkey)
//if err := wallet.unLock(&addr, wallet.password); err != nil {
// return nil, ErrAccountExist
//}
return wallet.GetAccountByAddress(&addr)
}
// ListAddress get all address in wallet
func (wallet *Wallet) ListAddress() ([]string, error) {
if err := wallet.checkWallet(RPERMISSION); err != nil {
return nil, ErrClosedWallet
}
addrs, err := wallet.cacheStore.ListAddr(wallet.password)
if err != nil {
return nil, err
}
return addrs, nil
}
// DumpPrivateKey query private key by address
func (wallet *Wallet) DumpPrivateKey(addr *crypto.CommonAddress) (*secp256k1.PrivateKey, error) {
if err := wallet.checkWallet(WPERMISSION); err != nil {
return nil, err
}
node, err := wallet.cacheStore.GetKey(addr)
if err != nil {
return nil, err
}
return node.PrivateKey, nil
}
// Sign sign a message using key in wallet
func (wallet *Wallet) Sign(addr *crypto.CommonAddress, msg []byte) ([]byte, error) {
if len(msg) != 32 {
return nil, ErrNotAHash
}
if err := wallet.checkWallet(WPERMISSION); err != nil {
return nil, err
}
node, err := wallet.cacheStore.GetKey(addr)
if err != nil {
return nil, err
}
sig, err := secp256k1.SignCompact(node.PrivateKey, msg, true)
if err != nil {
return nil, err
}
return sig, nil
}
// IsLock query current lock state 0 is locked 1 is unlock
func (wallet *Wallet) IsLock() bool {
//return atomic.LoadInt32(&wallet.isLock) == LOCKED
return false
}
// IsOpen query current wallet open state
func (wallet *Wallet) IsOpen() bool {
return wallet.cacheStore != nil
}
// Lock wallet to disable private key
func (wallet *Wallet) Lock(addr *crypto.CommonAddress) error {
//atomic.StoreInt32(&wallet.isLock, LOCKED)
err := wallet.cacheStore.ClearKey(addr)
return err
}
// UnLock wallet to enable private key
func (wallet *Wallet) UnLock(addr *crypto.CommonAddress, password string) error {
if wallet.cacheStore == nil {
err := wallet.OpenWallet(password)
if err != nil {
return err
}
}
return wallet.unLock(addr, password)
}
func (wallet *Wallet) unLock(addr *crypto.CommonAddress, password string) error {
//atomic.StoreInt32(&wallet.isLock, UNLOCKED)
return wallet.cacheStore.LoadKeys(addr, password)
}
func (wallet *Wallet) checkWallet(op int) error {
if wallet.cacheStore == nil {
return ErrClosedWallet
}
if op == WPERMISSION {
if wallet.IsLock() {
return ErrLockedWallet
}
}
return nil
}
//func (wallet *Wallet) cryptoPassword(password string) string {
// return string(sha3.Keccak256([]byte(password)))
//}
func (wallet *Wallet) ImportPrivKey(key *secp256k1.PrivateKey, password string) (*types.Node, error) {
if err := wallet.checkWallet(WPERMISSION); err != nil {
return nil, err
}
addr := crypto.PubkeyToAddress(key.PubKey())
node := &types.Node{
Address: &addr,
PrivateKey: key,
ChainId: wallet.chainId,
}
_, err := wallet.cacheStore.GetKey(&addr)
if err == nil {
return nil, errors.Wrap(ErrExistKey, addr.String())
}
err = wallet.cacheStore.LoadKeys(&addr, password)
if err == nil {
return nil, errors.Wrap(ErrExistKey, addr.String())
}
err = wallet.cacheStore.StoreKey(node, password)
if err != nil {
return nil, err
}
return node, nil
}
func (wallet *Wallet) ImportKeyStore(path, password string) ([]*crypto.CommonAddress, error) {
if err := wallet.checkWallet(WPERMISSION); err != nil {
return nil, err
}
if !fileutil.IsDirExists(path) {
return nil, errors.Wrap(ErrMissingKeystore, path)
}
newWallet, err := NewWallet(&accountTypes.Config{
Enable: true,
Type: "keystore",
KeyStoreDir: path,
}, wallet.chainId)
if err != nil {
return nil, err
}
err = newWallet.OpenWallet(password)
if err != nil {
return nil, err
}
nodes, err := newWallet.cacheStore.ExportKey(password)
if err != nil {
return nil, err
}
addrs := []*crypto.CommonAddress{}
for _, node := range nodes {
_, err := wallet.cacheStore.GetKey(node.Address)
if err == nil {
log.WithField("addr", node.Address.String()).Info("privkey exist")
continue
}
err = wallet.cacheStore.StoreKey(node, password)
if err != nil {
return addrs, err
}
addrs = append(addrs, node.Address)
}
return addrs, nil
}