-
Notifications
You must be signed in to change notification settings - Fork 1
/
keystore.go
187 lines (156 loc) · 4.7 KB
/
keystore.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
package keystore
import (
"crypto/ecdsa"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"sync"
"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/crypto"
log "github.com/xlab/suplog"
)
type PersonalSignFn func(account common.Address, data []byte) (sig []byte, err error)
type SignerFn = bind.SignerFn
type EthKeyStore interface {
PrivateKey(account common.Address, password string) (*ecdsa.PrivateKey, error)
SignerFn(chainID uint64, account common.Address, password string) (SignerFn, error)
PersonalSignFn(account common.Address, password string) (PersonalSignFn, error)
UnsetKey(account common.Address, password string)
Accounts() []common.Address
AddPath(keystorePath string) error
RemovePath(keystorePath string)
Paths() []string
}
func New(paths ...string) (EthKeyStore, error) {
ks := &keyStore{
cache: NewKeyCache(),
paths: make(map[string]struct{}),
pathsMux: new(sync.RWMutex),
}
for _, path := range paths {
ks.paths[path] = struct{}{}
}
ks.reloadPathsCache()
return ks, nil
}
type keyStore struct {
cache KeyCache
paths map[string]struct{}
pathsMux *sync.RWMutex
}
func (ks *keyStore) PrivateKey(account common.Address, password string) (*ecdsa.PrivateKey, error) {
return ks.cache.PrivateKey(account, password)
}
func (ks *keyStore) SignerFn(chainID uint64, account common.Address, password string) (SignerFn, error) {
return ks.cache.SignerFn(chainID, account, password)
}
func (ks *keyStore) PersonalSignFn(account common.Address, password string) (PersonalSignFn, error) {
return ks.cache.PersonalSignFn(account, password)
}
func (ks *keyStore) UnsetKey(account common.Address, password string) {
ks.cache.UnsetKey(account, password)
}
func (ks *keyStore) Accounts() []common.Address {
paths := ks.Paths()
var accounts []common.Address
for _, keystorePath := range paths {
if err := ks.forEachWallet(keystorePath, func(spec *WalletSpec) error {
accounts = append(accounts, spec.AddressFromHex())
return nil
}); err != nil {
log.WithField("keystore", keystorePath).WithError(err).Warningln("failed to read keystore files")
}
}
return accounts
}
var errRangeStop = errors.New("stop")
func (ks *keyStore) forEachWallet(keystorePath string, fn func(spec *WalletSpec) error) error {
return filepath.Walk(keystorePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
} else if path == keystorePath {
return nil
} else if info.IsDir() {
return filepath.SkipDir
}
var spec *WalletSpec
if data, err := ioutil.ReadFile(path); err != nil {
return err
} else if err = json.Unmarshal(data, &spec); err != nil {
return err
}
if len(spec.Address) == 0 {
return fmt.Errorf("failed to load address from %s", path)
} else if !common.IsHexAddress(spec.Address) {
return fmt.Errorf("wrong (not hex) address from %s", path)
}
spec.Path = path
return fn(spec)
})
}
func (ks *keyStore) AddPath(keystorePath string) error {
f, err := os.Stat(keystorePath)
if err != nil {
return err
} else if !f.IsDir() {
return fmt.Errorf("%s is not a directory", keystorePath)
}
ks.pathsMux.Lock()
ks.paths[keystorePath] = struct{}{}
ks.pathsMux.Unlock()
ks.reloadPathsCache()
return nil
}
func (ks *keyStore) reloadPathsCache() {
paths := ks.Paths()
for _, keystorePath := range paths {
err := ks.forEachWallet(keystorePath, func(spec *WalletSpec) error {
_ = ks.cache.SetPath(spec.AddressFromHex(), spec.Path)
return nil
})
if err != nil {
log.WithField("keystore", keystorePath).WithError(err).Warningln("failed to read keystore files")
}
}
}
func (ks *keyStore) RemovePath(keystorePath string) {
ks.pathsMux.Lock()
delete(ks.paths, keystorePath)
ks.pathsMux.Unlock()
}
func (ks *keyStore) Paths() []string {
ks.pathsMux.RLock()
paths := make([]string, 0, len(ks.paths))
for p := range ks.paths {
paths = append(paths, p)
}
ks.pathsMux.RUnlock()
sort.Sort(sort.StringSlice(paths))
return paths
}
type WalletSpec struct {
Address string `json:"address"`
ID string `json:"id"`
Version int `json:"version"`
Path string `json:"-"`
}
func (spec *WalletSpec) AddressFromHex() common.Address {
return common.HexToAddress(spec.Address)
}
func PrivateKeyPersonalSignFn(privKey *ecdsa.PrivateKey) (PersonalSignFn, error) {
keyAddress := crypto.PubkeyToAddress(privKey.PublicKey)
signFn := func(from common.Address, data []byte) (sig []byte, err error) {
if from != keyAddress {
return nil, errors.New("from address mismatch")
}
protectedHash := accounts.TextHash(data)
return crypto.Sign(protectedHash, privKey)
}
return signFn, nil
}