-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathmaster.go
366 lines (326 loc) · 9.32 KB
/
master.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package keystore
import (
"fmt"
"math/big"
"reflect"
"sync"
starkkey "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/keys"
"github.com/smartcontractkit/chainlink/core/services/keystore/keys/dkgencryptkey"
"github.com/smartcontractkit/chainlink/core/services/keystore/keys/dkgsignkey"
"github.com/smartcontractkit/chainlink/core/services/keystore/keys/ocr2key"
"github.com/smartcontractkit/chainlink/core/services/keystore/keys/solkey"
"github.com/smartcontractkit/chainlink/core/services/keystore/keys/terrakey"
"github.com/pkg/errors"
"github.com/smartcontractkit/sqlx"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/services/keystore/keys/csakey"
"github.com/smartcontractkit/chainlink/core/services/keystore/keys/ethkey"
"github.com/smartcontractkit/chainlink/core/services/keystore/keys/ocrkey"
"github.com/smartcontractkit/chainlink/core/services/keystore/keys/p2pkey"
"github.com/smartcontractkit/chainlink/core/services/keystore/keys/vrfkey"
"github.com/smartcontractkit/chainlink/core/services/pg"
"github.com/smartcontractkit/chainlink/core/utils"
)
var ErrLocked = errors.New("Keystore is locked")
// DefaultEVMChainIDFunc is a func for getting a default evm chain ID -
// necessary because it is lazily evaluated
type DefaultEVMChainIDFunc func() (defaultEVMChainID *big.Int, err error)
//go:generate mockery --name Master --output ./mocks/ --case=underscore
type Master interface {
CSA() CSA
DKGSign() DKGSign
DKGEncrypt() DKGEncrypt
Eth() Eth
OCR() OCR
OCR2() OCR2
P2P() P2P
Solana() Solana
Terra() Terra
StarkNet() StarkNet
VRF() VRF
Unlock(password string) error
Migrate(vrfPassword string, f DefaultEVMChainIDFunc) error
IsEmpty() (bool, error)
}
type master struct {
*keyManager
csa *csa
eth *eth
ocr *ocr
ocr2 ocr2
p2p *p2p
solana *solana
terra *terra
starknet *starknet
vrf *vrf
dkgSign *dkgSign
dkgEncrypt *dkgEncrypt
}
func New(db *sqlx.DB, scryptParams utils.ScryptParams, lggr logger.Logger, cfg pg.LogConfig) Master {
return newMaster(db, scryptParams, lggr, cfg)
}
func newMaster(db *sqlx.DB, scryptParams utils.ScryptParams, lggr logger.Logger, cfg pg.LogConfig) *master {
km := &keyManager{
orm: NewORM(db, lggr, cfg),
scryptParams: scryptParams,
lock: &sync.RWMutex{},
logger: lggr.Named("KeyStore"),
}
return &master{
keyManager: km,
csa: newCSAKeyStore(km),
eth: newEthKeyStore(km),
ocr: newOCRKeyStore(km),
ocr2: newOCR2KeyStore(km),
p2p: newP2PKeyStore(km),
solana: newSolanaKeyStore(km),
terra: newTerraKeyStore(km),
starknet: newStarkNetKeyStore(km),
vrf: newVRFKeyStore(km),
dkgSign: newDKGSignKeyStore(km),
dkgEncrypt: newDKGEncryptKeyStore(km),
}
}
func (ks *master) DKGEncrypt() DKGEncrypt {
return ks.dkgEncrypt
}
func (ks master) DKGSign() DKGSign {
return ks.dkgSign
}
func (ks master) CSA() CSA {
return ks.csa
}
func (ks *master) Eth() Eth {
return ks.eth
}
func (ks *master) OCR() OCR {
return ks.ocr
}
func (ks *master) OCR2() OCR2 {
return ks.ocr2
}
func (ks *master) P2P() P2P {
return ks.p2p
}
func (ks *master) Solana() Solana {
return ks.solana
}
func (ks *master) Terra() Terra {
return ks.terra
}
func (ks *master) StarkNet() StarkNet {
return ks.starknet
}
func (ks *master) VRF() VRF {
return ks.vrf
}
func (ks *master) IsEmpty() (bool, error) {
var count int64
err := ks.orm.q.QueryRow("SELECT count(*) FROM encrypted_key_rings").Scan(&count)
if err != nil {
return false, err
}
return count == 0, nil
}
func (ks *master) Migrate(vrfPssword string, f DefaultEVMChainIDFunc) error {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return ErrLocked
}
csaKeys, err := ks.csa.GetV1KeysAsV2()
if err != nil {
return err
}
for _, csaKey := range csaKeys {
if _, exists := ks.keyRing.CSA[csaKey.ID()]; exists {
continue
}
ks.logger.Debugf("Migrating CSA key %s", csaKey.ID())
ks.keyRing.CSA[csaKey.ID()] = csaKey
}
ocrKeys, err := ks.ocr.GetV1KeysAsV2()
if err != nil {
return err
}
for _, ocrKey := range ocrKeys {
if _, exists := ks.keyRing.OCR[ocrKey.ID()]; exists {
continue
}
ks.logger.Debugf("Migrating OCR key %s", ocrKey.ID())
ks.keyRing.OCR[ocrKey.ID()] = ocrKey
}
p2pKeys, err := ks.p2p.GetV1KeysAsV2()
if err != nil {
return err
}
for _, p2pKey := range p2pKeys {
if _, exists := ks.keyRing.P2P[p2pKey.ID()]; exists {
continue
}
ks.logger.Debugf("Migrating P2P key %s", p2pKey.ID())
ks.keyRing.P2P[p2pKey.ID()] = p2pKey
}
vrfKeys, err := ks.vrf.GetV1KeysAsV2(vrfPssword)
if err != nil {
return err
}
for _, vrfKey := range vrfKeys {
if _, exists := ks.keyRing.VRF[vrfKey.ID()]; exists {
continue
}
ks.logger.Debugf("Migrating VRF key %s", vrfKey.ID())
ks.keyRing.VRF[vrfKey.ID()] = vrfKey
}
if err = ks.keyManager.save(); err != nil {
return err
}
ethKeys, nonces, fundings, err := ks.eth.getV1KeysAsV2()
if err != nil {
return err
}
if len(ethKeys) > 0 {
chainID, err := f()
if err != nil {
return errors.Wrapf(err, `%d legacy eth keys detected, but no default EVM chain ID was specified
PLEASE READ THIS ADDITIONAL INFO
If you are running Chainlink with EVM_ENABLED=false and don't care about EVM keys at all, you can run the following SQL to remove any lingering eth keys that may have been autogenerated by an older version of Chainlink, and boot the node again:
pqsl> TRUNCATE keys;
WARNING: This will PERMANENTLY AND IRRECOVERABLY delete any legacy eth keys, so please be absolutely sure this is what you want before you run this. Consider taking a database backup first`, len(ethKeys))
}
for i, ethKey := range ethKeys {
if _, exists := ks.keyRing.Eth[ethKey.ID()]; exists {
continue
}
ks.logger.Debugf("Migrating Eth key %s (and pegging to chain ID %s)", ethKey.ID(), chainID.String())
// Note that V1 keys that were "funding" will be migrated as "disabled"
if err = ks.eth.addWithNonce(ethKey, chainID, nonces[i], fundings[i]); err != nil {
return err
}
if err = ks.keyManager.save(); err != nil {
return err
}
}
}
return nil
}
type keyManager struct {
orm ksORM
scryptParams utils.ScryptParams
keyRing *keyRing
keyStates *keyStates
lock *sync.RWMutex
password string
logger logger.Logger
}
func (km *keyManager) Unlock(password string) error {
km.lock.Lock()
defer km.lock.Unlock()
// DEV: allow Unlock() to be idempotent - this is especially useful in tests,
if km.password != "" {
if password != km.password {
return errors.New("attempting to unlock keystore again with a different password")
}
return nil
}
ekr, err := km.orm.getEncryptedKeyRing()
if err != nil {
return errors.Wrap(err, "unable to get encrypted key ring")
}
kr, err := ekr.Decrypt(password)
if err != nil {
return errors.Wrap(err, "unable to decrypt encrypted key ring")
}
kr.logPubKeys(km.logger)
km.keyRing = kr
ks, err := km.orm.loadKeyStates()
if err != nil {
return errors.Wrap(err, "unable to load key states")
}
km.keyStates = ks
km.password = password
return nil
}
// caller must hold lock!
func (km *keyManager) save(callbacks ...func(pg.Queryer) error) error {
ekb, err := km.keyRing.Encrypt(km.password, km.scryptParams)
if err != nil {
return errors.Wrap(err, "unable to encrypt keyRing")
}
return km.orm.saveEncryptedKeyRing(&ekb, callbacks...)
}
// caller must hold lock!
func (km *keyManager) safeAddKey(unknownKey Key, callbacks ...func(pg.Queryer) error) error {
fieldName, err := getFieldNameForKey(unknownKey)
if err != nil {
return err
}
// add key to keyring
id := reflect.ValueOf(unknownKey.ID())
key := reflect.ValueOf(unknownKey)
keyRing := reflect.Indirect(reflect.ValueOf(km.keyRing))
keyMap := keyRing.FieldByName(fieldName)
keyMap.SetMapIndex(id, key)
// save keyring to DB
err = km.save(callbacks...)
// if save fails, remove key from keyring
if err != nil {
keyMap.SetMapIndex(id, reflect.Value{})
return err
}
return nil
}
// caller must hold lock!
func (km *keyManager) safeRemoveKey(unknownKey Key, callbacks ...func(pg.Queryer) error) (err error) {
fieldName, err := getFieldNameForKey(unknownKey)
if err != nil {
return err
}
id := reflect.ValueOf(unknownKey.ID())
key := reflect.ValueOf(unknownKey)
keyRing := reflect.Indirect(reflect.ValueOf(km.keyRing))
keyMap := keyRing.FieldByName(fieldName)
keyMap.SetMapIndex(id, reflect.Value{})
// save keyring to DB
err = km.save(callbacks...)
// if save fails, add key back to keyRing
if err != nil {
keyMap.SetMapIndex(id, key)
return err
}
return nil
}
// caller must hold lock!
func (km *keyManager) isLocked() bool {
return len(km.password) == 0
}
func getFieldNameForKey(unknownKey Key) (string, error) {
switch unknownKey.(type) {
case csakey.KeyV2:
return "CSA", nil
case ethkey.KeyV2:
return "Eth", nil
case ocrkey.KeyV2:
return "OCR", nil
case ocr2key.KeyBundle:
return "OCR2", nil
case p2pkey.KeyV2:
return "P2P", nil
case solkey.Key:
return "Solana", nil
case terrakey.Key:
return "Terra", nil
case starkkey.Key:
return "StarkNet", nil
case vrfkey.KeyV2:
return "VRF", nil
case dkgsignkey.Key:
return "DKGSign", nil
case dkgencryptkey.Key:
return "DKGEncrypt", nil
}
return "", fmt.Errorf("unknown key type: %T", unknownKey)
}
type Key interface {
ID() string
}