-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
master.go
281 lines (247 loc) · 6.89 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
package keystore
import (
"fmt"
"math/big"
"reflect"
"sync"
"github.com/pkg/errors"
"github.com/smartcontractkit/sqlx"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/cosmoskey"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/csakey"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/dkgencryptkey"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/dkgsignkey"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ocr2key"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ocrkey"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/p2pkey"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/solkey"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/starkkey"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/vrfkey"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
var (
ErrLocked = errors.New("Keystore is locked")
ErrKeyNotFound = errors.New("Key not found")
ErrKeyExists = errors.New("Key already exists")
)
// 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 --quiet --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
Cosmos() Cosmos
StarkNet() StarkNet
VRF() VRF
Unlock(password string) error
IsEmpty() (bool, error)
}
type master struct {
*keyManager
cosmos *cosmos
csa *csa
eth *eth
ocr *ocr
ocr2 ocr2
p2p *p2p
solana *solana
starknet *starknet
vrf *vrf
dkgSign *dkgSign
dkgEncrypt *dkgEncrypt
}
func New(db *sqlx.DB, scryptParams utils.ScryptParams, lggr logger.Logger, cfg pg.QConfig) Master {
return newMaster(db, scryptParams, lggr, cfg)
}
func newMaster(db *sqlx.DB, scryptParams utils.ScryptParams, lggr logger.Logger, cfg pg.QConfig) *master {
km := &keyManager{
orm: NewORM(db, lggr, cfg),
scryptParams: scryptParams,
lock: &sync.RWMutex{},
logger: lggr.Named("KeyStore"),
}
return &master{
keyManager: km,
cosmos: newCosmosKeyStore(km),
csa: newCSAKeyStore(km),
eth: newEthKeyStore(km),
ocr: newOCRKeyStore(km),
ocr2: newOCR2KeyStore(km),
p2p: newP2PKeyStore(km),
solana: newSolanaKeyStore(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) Cosmos() Cosmos {
return ks.cosmos
}
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
}
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 cosmoskey.Key:
return "Cosmos", nil
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 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
}