-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeycache.go
More file actions
148 lines (133 loc) · 3.84 KB
/
keycache.go
File metadata and controls
148 lines (133 loc) · 3.84 KB
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
// Copyright 2017-2019 Tensigma Ltd. All rights reserved.
// Use of this source code is governed by Microsoft Reference Source
// License (MS-RSL) that can be found in the LICENSE file.
package ethfw
import (
"crypto/ecdsa"
"crypto/sha1"
"errors"
"io/ioutil"
"strings"
"sync"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
type KeyCache interface {
SetPath(account common.Address, path string) bool
UnsetPath(account common.Address, path string)
PrivateKey(account common.Address, password string) (key *ecdsa.PrivateKey, ok bool)
SetPrivateKey(account common.Address, pk *ecdsa.PrivateKey)
UnsetKey(account common.Address, password string)
SignerFn(account common.Address, password string) bind.SignerFn
}
func NewKeyCache() KeyCache {
return &keyCache{
paths: make(map[common.Address]string),
pathsMux: new(sync.RWMutex),
keys: make(map[string]*ecdsa.PrivateKey),
keysMux: new(sync.RWMutex),
guard: NewUniquify(),
}
}
type keyCache struct {
paths map[common.Address]string
pathsMux *sync.RWMutex
keys map[string]*ecdsa.PrivateKey
keysMux *sync.RWMutex
guard Uniquify
}
// SetPath sets the wallet path for a given account. Returns true if the new path
// has been added or was changed.
func (k *keyCache) SetPath(account common.Address, path string) bool {
k.pathsMux.Lock()
prevPath, existing := k.paths[account]
k.paths[account] = path
k.pathsMux.Unlock()
return !existing || prevPath != path
}
func (k *keyCache) UnsetPath(account common.Address, path string) {
k.pathsMux.Lock()
delete(k.paths, account)
k.pathsMux.Unlock()
}
var (
ErrNoKeyStore = errors.New("no keystore or file for account")
ErrKeyDecrypt = errors.New("private key decryption failed")
)
func (k *keyCache) UnsetKey(account common.Address, password string) {
h := hashAccountPass(account, password)
k.keysMux.Lock()
delete(k.keys, string(h))
k.keysMux.Unlock()
}
func (k *keyCache) SetPrivateKey(account common.Address, pk *ecdsa.PrivateKey) {
h := hashAccountPass(account, "")
k.keysMux.Lock()
k.keys[string(h)] = pk
k.keysMux.Unlock()
}
func (k *keyCache) PrivateKey(account common.Address, password string) (key *ecdsa.PrivateKey, ok bool) {
h := hashAccountPass(account, password)
if err := k.guard.Call(string(h), func() error {
k.keysMux.RLock()
key, ok = k.keys[string(h)]
k.keysMux.RUnlock()
if ok {
return nil
}
k.pathsMux.RLock()
path, pathOk := k.paths[account]
k.pathsMux.RUnlock()
if !pathOk {
return ErrNoKeyStore
}
if strings.HasPrefix(path, "keystore://") {
path = strings.TrimPrefix(path, "keystore://")
}
keyJSON, err := ioutil.ReadFile(path)
if err != nil {
return ErrNoKeyStore
}
pk, err := keystore.DecryptKey(keyJSON, password)
if err != nil {
return ErrKeyDecrypt
}
k.keysMux.Lock()
k.keys[string(h)] = pk.PrivateKey
k.keysMux.Unlock()
key = pk.PrivateKey
ok = true
return nil
}); err != nil {
return nil, false
}
return key, ok
}
func (k *keyCache) SignerFn(account common.Address, password string) bind.SignerFn {
key, ok := k.PrivateKey(account, password)
if !ok {
return nil
}
keyAddr := crypto.PubkeyToAddress(key.PublicKey)
return func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
if address != keyAddr {
return nil, errors.New("not authorized to sign this account")
}
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key)
if err != nil {
return nil, err
}
return tx.WithSignature(signer, signature)
}
}
var hashSep = []byte("-")
func hashAccountPass(account common.Address, password string) []byte {
h := sha1.New()
h.Write(account[:])
h.Write(hashSep)
h.Write([]byte(password))
return h.Sum(nil)
}