-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
dkgencrypt.go
163 lines (142 loc) · 3.7 KB
/
dkgencrypt.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
package keystore
import (
"fmt"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/dkgencryptkey"
)
//go:generate mockery --quiet --name DKGEncrypt --output mocks/ --case=underscore
// DKGEncrypt provides encryption keys for the DKG.
type DKGEncrypt interface {
Get(id string) (dkgencryptkey.Key, error)
GetAll() ([]dkgencryptkey.Key, error)
Create() (dkgencryptkey.Key, error)
Add(key dkgencryptkey.Key) error
Delete(id string) (dkgencryptkey.Key, error)
Import(keyJSON []byte, password string) (dkgencryptkey.Key, error)
Export(id string, password string) ([]byte, error)
EnsureKey() error
}
type dkgEncrypt struct {
*keyManager
}
func newDKGEncryptKeyStore(km *keyManager) *dkgEncrypt {
return &dkgEncrypt{
keyManager: km,
}
}
var _ DKGEncrypt = &dkgEncrypt{}
// Add implements DKGEncrypt
func (d *dkgEncrypt) Add(key dkgencryptkey.Key) error {
d.lock.Lock()
defer d.lock.Unlock()
if d.isLocked() {
return ErrLocked
}
return d.safeAddKey(key)
}
// Create implements DKGEncrypt
func (d *dkgEncrypt) Create() (dkgencryptkey.Key, error) {
d.lock.Lock()
defer d.lock.Unlock()
if d.isLocked() {
return dkgencryptkey.Key{}, ErrLocked
}
key, err := dkgencryptkey.New()
if err != nil {
return dkgencryptkey.Key{}, errors.Wrap(err, "dkgencryptkey.New()")
}
return key, d.safeAddKey(key)
}
// Delete implements DKGEncrypt
func (d *dkgEncrypt) Delete(id string) (dkgencryptkey.Key, error) {
d.lock.Lock()
defer d.lock.Unlock()
if d.isLocked() {
return dkgencryptkey.Key{}, ErrLocked
}
key, err := d.getByID(id)
if err != nil {
return dkgencryptkey.Key{}, err
}
err = d.safeRemoveKey(key)
return key, errors.Wrap(err, "safe remove key")
}
// EnsureKey implements DKGEncrypt
func (d *dkgEncrypt) EnsureKey() error {
d.lock.Lock()
defer d.lock.Unlock()
if d.isLocked() {
return ErrLocked
}
if len(d.keyRing.DKGEncrypt) > 0 {
return nil
}
key, err := dkgencryptkey.New()
if err != nil {
return errors.Wrap(err, "dkgencryptkey. New()")
}
d.logger.Infof("Created DKGEncrypt key with ID %s", key.ID())
return d.safeAddKey(key)
}
// Export implements DKGEncrypt
func (d *dkgEncrypt) Export(id string, password string) ([]byte, error) {
d.lock.RLock()
defer d.lock.RUnlock()
if d.isLocked() {
return nil, ErrLocked
}
key, err := d.getByID(id)
if err != nil {
return nil, err
}
return key.ToEncryptedJSON(password, d.scryptParams)
}
// Get implements DKGEncrypt
func (d *dkgEncrypt) Get(id string) (keys dkgencryptkey.Key, err error) {
d.lock.RLock()
defer d.lock.RUnlock()
if d.isLocked() {
return dkgencryptkey.Key{}, ErrLocked
}
return d.getByID(id)
}
// GetAll implements DKGEncrypt
func (d *dkgEncrypt) GetAll() (keys []dkgencryptkey.Key, err error) {
d.lock.RLock()
defer d.lock.RUnlock()
if d.isLocked() {
return nil, ErrLocked
}
for _, key := range d.keyRing.DKGEncrypt {
keys = append(keys, key)
}
return keys, nil
}
// Import implements DKGEncrypt
func (d *dkgEncrypt) Import(keyJSON []byte, password string) (dkgencryptkey.Key, error) {
d.lock.Lock()
defer d.lock.Unlock()
if d.isLocked() {
return dkgencryptkey.Key{}, ErrLocked
}
key, err := dkgencryptkey.FromEncryptedJSON(keyJSON, password)
if err != nil {
return dkgencryptkey.Key{}, errors.Wrap(err, "from encrypted json")
}
_, err = d.getByID(key.ID())
if err == nil {
return dkgencryptkey.Key{}, fmt.Errorf("key with ID %s already exists", key.ID())
}
return key, d.keyManager.safeAddKey(key)
}
// caller must hold lock
func (d *dkgEncrypt) getByID(id string) (dkgencryptkey.Key, error) {
key, found := d.keyRing.DKGEncrypt[id]
if !found {
return dkgencryptkey.Key{}, KeyNotFoundError{
ID: id,
KeyType: "DKGEncrypt",
}
}
return key, nil
}