-
Notifications
You must be signed in to change notification settings - Fork 2
/
wallet.go
208 lines (181 loc) · 5.87 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
// Copyright 2018 The uranus Authors
// This file is part of the uranus library.
//
// The uranus library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The uranus library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the uranus library. If not, see <http://www.gnu.org/licenses/>.
package wallet
import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
lockcache "github.com/UranusBlockStack/uranus/common/cache"
"github.com/UranusBlockStack/uranus/common/crypto"
"github.com/UranusBlockStack/uranus/common/log"
"github.com/UranusBlockStack/uranus/common/utils"
"github.com/UranusBlockStack/uranus/core/types"
)
const (
defaultAccountCacheLimit = 1000
)
// Wallet represents a software wallet.
type Wallet struct {
ks *KeyStore
accountCache *lockcache.Cache
}
// NewWallet initialize wallet.
func NewWallet(ksdir string) *Wallet {
log.Infof("Disk storage enabled for keystore dir: %v ", ksdir)
accountCache, _ := lockcache.New(defaultAccountCacheLimit)
wallet := &Wallet{
ks: NewKeyStore(ksdir),
accountCache: accountCache,
}
return wallet
}
// Accounts list all account.
func (w *Wallet) Accounts() (Accounts, error) {
accounts := Accounts{}
rd, err := ioutil.ReadDir(w.ks.keyStoreDir)
if err != nil {
return nil, err
}
for _, fi := range rd {
if !noKeyFile(fi) {
addrHex := strings.TrimSuffix(fi.Name(), keyFileSuffix)
addr := utils.HexToAddress(addrHex[len(addrHex)-40:])
accounts = append(accounts, Account{
Address: addr,
FileName: fi.Name(),
})
}
}
sort.Sort(accounts)
return accounts, nil
}
// ImportRawKey import key in to key store.
func (w *Wallet) ImportRawKey(privkey string, passphrase string) (utils.Address, error) {
key, err := crypto.HexToECDSA(privkey)
if err != nil {
return utils.Address{}, err
}
addr := crypto.PubkeyToAddress(key.PublicKey)
tmpAccount, err := w.Find(addr, passphrase)
if err == nil {
return tmpAccount.Address, nil
} else if err != nil && err != ErrNoMatch {
return utils.Address{}, err
}
account := Account{
Address: addr,
PrivateKey: key,
FileName: keyFileName(addr),
}
path := filepath.Join(w.ks.keyStoreDir, keyFileName(addr))
if err := w.ks.PutKey(account, path, passphrase); err != nil {
return utils.Address{}, err
}
w.accountCache.Add(account.Address, &lockAccount{passphrase: passphrase, account: &account})
return account.Address, nil
}
// ExportRawKey returns key hex.
func (w *Wallet) ExportRawKey(addr utils.Address, passphrase string) (string, error) {
account, err := w.Find(addr, passphrase)
if err != nil {
return "", err
}
return utils.BytesToHex(crypto.ByteFromECDSA(account.PrivateKey)), nil
}
// NewAccount creates a new account
func (w *Wallet) NewAccount(passphrase string) (Account, error) {
account, err := genNewAccount()
if err != nil {
return Account{}, err
}
path := filepath.Join(w.ks.keyStoreDir, account.FileName)
if err := w.ks.PutKey(account, path, passphrase); err != nil {
return Account{}, err
}
w.accountCache.Add(account.Address, &lockAccount{passphrase: passphrase, account: &account})
return account, nil
}
// Find resolves the given account into a unique entry in the keystore.
func (w *Wallet) Find(addr utils.Address, passphrase string) (Account, error) {
if la, ok := w.accountCache.Get(addr); ok && la.(*lockAccount).passphrase == passphrase {
return *la.(*lockAccount).account, nil
}
rd, err := ioutil.ReadDir(w.ks.keyStoreDir)
if err != nil {
return Account{}, err
}
for _, fi := range rd {
if !noKeyFile(fi) {
addrHex := strings.TrimSuffix(fi.Name(), keyFileSuffix)
address := utils.HexToAddress(addrHex[len(addrHex)-40:])
if address == addr {
path := filepath.Join(w.ks.keyStoreDir, fi.Name())
account, err := w.ks.GetKey(addr, path, passphrase)
if err != nil {
return Account{}, err
}
w.accountCache.Add(addr, &lockAccount{passphrase: passphrase, account: account})
return *account, nil
}
}
}
return Account{}, ErrNoMatch
}
// Delete removes the speciified account
func (w *Wallet) Delete(account Account, passphrase string) error {
path := filepath.Join(w.ks.keyStoreDir, account.FileName)
if !utils.FileExists(path) {
return nil
}
w.accountCache.Remove(account.Address)
return os.Remove(path)
}
// Update update the specified account
func (w *Wallet) Update(account Account, passphrase, newPassphrase string) error {
path := filepath.Join(w.ks.keyStoreDir, account.FileName)
if !utils.FileExists(path) {
return ErrNoMatch
}
newaccount, err := w.ks.GetKey(account.Address, path, passphrase)
if err != nil {
return err
}
newaccount.FileName = account.FileName
w.accountCache.Add(account.Address, &lockAccount{passphrase: newPassphrase, account: newaccount})
return w.ks.PutKey(*newaccount, path, newPassphrase)
}
// SignTx sign the specified transaction
func (w *Wallet) SignTx(addr utils.Address, tx *types.Transaction, passphrase string) (*types.Transaction, error) {
account, err := w.Find(addr, passphrase)
if err != nil {
return nil, err
}
if err := tx.SignTx(types.Signer{}, account.PrivateKey); err != nil {
return nil, err
}
return tx, nil
}
// SignHash signs hash if the private key matching the given address
// can be decrypted with the given passphrase.
func (w *Wallet) SignHash(addr utils.Address, passphrase string, hash []byte) ([]byte, error) {
account, err := w.Find(addr, passphrase)
if err != nil {
return nil, err
}
return crypto.Sign(hash[:], account.PrivateKey)
}