-
Notifications
You must be signed in to change notification settings - Fork 311
/
keystore_mock.go
184 lines (158 loc) · 4.33 KB
/
keystore_mock.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
/*
* Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package identity
import (
"crypto/ecdsa"
crand "crypto/rand"
"encoding/hex"
"sync"
"github.com/ethereum/go-ethereum/accounts"
ethKs "github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
type mockKeystore struct {
keys map[common.Address]MockKey
lock sync.Mutex
}
// MockKeys represents the mocked keys
var MockKeys = map[common.Address]MockKey{
common.HexToAddress("53a835143c0ef3bbcbfa796d7eb738ca7dd28f68"): {
PkHex: "6f88637b68ee88816e73f663aef709d7009836c98ae91ef31e3dfac7be3a1657",
Pass: "",
},
}
// NewMockKeystore returns empty mock keystore
func NewMockKeystore() *mockKeystore {
return NewMockKeystoreWith(map[common.Address]MockKey{})
}
// NewMockKeystoreWith returns a new mock keystore with specified keys
func NewMockKeystoreWith(keys map[common.Address]MockKey) *mockKeystore {
copied := make(map[common.Address]MockKey)
for key, value := range keys {
copied[key] = value
}
return &mockKeystore{
keys: copied,
}
}
// MockKey represents a mocked key
type MockKey struct {
PkHex string
Pass string
pk *ecdsa.PrivateKey
isUnlocked bool
}
func (mk *mockKeystore) Accounts() []accounts.Account {
mk.lock.Lock()
defer mk.lock.Unlock()
res := make([]accounts.Account, 0)
for k := range mk.keys {
res = append(res, accounts.Account{
Address: k,
})
}
return res
}
func (mk *mockKeystore) SignHash(a accounts.Account, hash []byte) ([]byte, error) {
mk.lock.Lock()
defer mk.lock.Unlock()
if v, ok := mk.keys[a.Address]; ok {
if !v.isUnlocked {
return nil, ethKs.ErrLocked
}
return crypto.Sign(hash, v.pk)
}
return nil, ethKs.ErrNoMatch
}
func (mk *mockKeystore) Export(a accounts.Account, passphrase, newPassphrase string) (keyJSON []byte, err error) {
mk.lock.Lock()
defer mk.lock.Unlock()
if v, ok := mk.keys[a.Address]; ok {
if v.Pass != passphrase {
return nil, ethKs.ErrDecrypt
}
return common.Hex2Bytes(v.PkHex), nil
}
return nil, ethKs.ErrNoMatch
}
func (mk *mockKeystore) NewAccount(passphrase string) (accounts.Account, error) {
mk.lock.Lock()
defer mk.lock.Unlock()
privateKeyECDSA, err := ecdsa.GenerateKey(crypto.S256(), crand.Reader)
if err != nil {
return accounts.Account{}, err
}
address := crypto.PubkeyToAddress(privateKeyECDSA.PublicKey)
mk.keys[address] = MockKey{
Pass: passphrase,
PkHex: hex.EncodeToString(crypto.FromECDSA(privateKeyECDSA)),
pk: privateKeyECDSA,
isUnlocked: false,
}
return accounts.Account{
Address: address,
}, nil
}
func (mk *mockKeystore) Unlock(a accounts.Account, passphrase string) error {
mk.lock.Lock()
defer mk.lock.Unlock()
if v, ok := mk.keys[a.Address]; ok {
if v.isUnlocked {
return nil
}
if v.Pass != passphrase {
return ethKs.ErrDecrypt
}
pk, err := crypto.HexToECDSA(v.PkHex)
if err != nil {
return err
}
v.isUnlocked = true
v.pk = pk
mk.keys[a.Address] = v
return nil
}
return ethKs.ErrNoMatch
}
func (mk *mockKeystore) Lock(addr common.Address) error {
mk.lock.Lock()
defer mk.lock.Unlock()
if v, ok := mk.keys[addr]; ok {
v.isUnlocked = false
mk.keys[addr] = v
}
return nil
}
func (mk *mockKeystore) Find(a accounts.Account) (accounts.Account, error) {
mk.lock.Lock()
defer mk.lock.Unlock()
if _, ok := mk.keys[a.Address]; ok {
return a, nil
}
return accounts.Account{}, ethKs.ErrNoMatch
}
// MockDecryptFunc represents the mock decrypt func
var MockDecryptFunc = func(keyjson []byte, auth string) (*ethKs.Key, error) {
pk, err := crypto.HexToECDSA(common.Bytes2Hex(keyjson))
if err != nil {
return nil, err
}
return ðKs.Key{
PrivateKey: pk,
}, nil
}