-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
p2p.go
201 lines (175 loc) · 4.51 KB
/
p2p.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
package keystore
import (
"fmt"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/p2pkey"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
)
//go:generate mockery --quiet --name P2P --output ./mocks/ --case=underscore --filename p2p.go
type P2P interface {
Get(id p2pkey.PeerID) (p2pkey.KeyV2, error)
GetAll() ([]p2pkey.KeyV2, error)
Create() (p2pkey.KeyV2, error)
Add(key p2pkey.KeyV2) error
Delete(id p2pkey.PeerID) (p2pkey.KeyV2, error)
Import(keyJSON []byte, password string) (p2pkey.KeyV2, error)
Export(id p2pkey.PeerID, password string) ([]byte, error)
EnsureKey() error
GetV1KeysAsV2() ([]p2pkey.KeyV2, error)
GetOrFirst(id p2pkey.PeerID) (p2pkey.KeyV2, error)
}
type p2p struct {
*keyManager
}
var _ P2P = &p2p{}
func newP2PKeyStore(km *keyManager) *p2p {
return &p2p{
km,
}
}
func (ks *p2p) Get(id p2pkey.PeerID) (p2pkey.KeyV2, error) {
ks.lock.RLock()
defer ks.lock.RUnlock()
if ks.isLocked() {
return p2pkey.KeyV2{}, ErrLocked
}
return ks.getByID(id)
}
func (ks *p2p) GetAll() (keys []p2pkey.KeyV2, _ error) {
ks.lock.RLock()
defer ks.lock.RUnlock()
if ks.isLocked() {
return nil, ErrLocked
}
for _, key := range ks.keyRing.P2P {
keys = append(keys, key)
}
return keys, nil
}
func (ks *p2p) Create() (p2pkey.KeyV2, error) {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return p2pkey.KeyV2{}, ErrLocked
}
key, err := p2pkey.NewV2()
if err != nil {
return p2pkey.KeyV2{}, err
}
return key, ks.safeAddKey(key)
}
func (ks *p2p) Add(key p2pkey.KeyV2) error {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return ErrLocked
}
if _, found := ks.keyRing.P2P[key.ID()]; found {
return fmt.Errorf("key with ID %s already exists", key.ID())
}
return ks.safeAddKey(key)
}
func (ks *p2p) Delete(id p2pkey.PeerID) (p2pkey.KeyV2, error) {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return p2pkey.KeyV2{}, ErrLocked
}
key, err := ks.getByID(id)
if err != nil {
return p2pkey.KeyV2{}, err
}
err = ks.safeRemoveKey(key, func(tx pg.Queryer) error {
_, err2 := tx.Exec(`DELETE FROM p2p_peers WHERE peer_id = $1`, key.ID())
return err2
})
return key, err
}
func (ks *p2p) Import(keyJSON []byte, password string) (p2pkey.KeyV2, error) {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return p2pkey.KeyV2{}, ErrLocked
}
key, err := p2pkey.FromEncryptedJSON(keyJSON, password)
if err != nil {
return p2pkey.KeyV2{}, errors.Wrap(err, "P2PKeyStore#ImportKey failed to decrypt key")
}
if _, found := ks.keyRing.P2P[key.ID()]; found {
return p2pkey.KeyV2{}, fmt.Errorf("key with ID %s already exists", key.ID())
}
return key, ks.keyManager.safeAddKey(key)
}
func (ks *p2p) Export(id p2pkey.PeerID, password string) ([]byte, error) {
ks.lock.RLock()
defer ks.lock.RUnlock()
if ks.isLocked() {
return nil, ErrLocked
}
key, err := ks.getByID(id)
if err != nil {
return nil, err
}
return key.ToEncryptedJSON(password, ks.scryptParams)
}
func (ks *p2p) EnsureKey() error {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return ErrLocked
}
if len(ks.keyRing.P2P) > 0 {
return nil
}
key, err := p2pkey.NewV2()
if err != nil {
return err
}
ks.logger.Infof("Created P2P key with ID %s", key.ID())
return ks.safeAddKey(key)
}
func (ks *p2p) GetV1KeysAsV2() (keys []p2pkey.KeyV2, _ error) {
v1Keys, err := ks.orm.GetEncryptedV1P2PKeys()
if err != nil {
return keys, err
}
for _, keyV1 := range v1Keys {
pk, err := keyV1.Decrypt(ks.password)
if err != nil {
return keys, err
}
keys = append(keys, pk.ToV2())
}
return keys, nil
}
var (
ErrNoP2PKey = errors.New("no p2p keys exist")
)
func (ks *p2p) GetOrFirst(id p2pkey.PeerID) (p2pkey.KeyV2, error) {
ks.lock.RLock()
defer ks.lock.RUnlock()
if ks.isLocked() {
return p2pkey.KeyV2{}, ErrLocked
}
if id != "" {
return ks.getByID(id)
} else if len(ks.keyRing.P2P) == 1 {
ks.logger.Warn("No P2P.PeerID set, defaulting to first key in database")
for _, key := range ks.keyRing.P2P {
return key, nil
}
} else if len(ks.keyRing.P2P) == 0 {
return p2pkey.KeyV2{}, ErrNoP2PKey
}
return p2pkey.KeyV2{}, errors.New(
"multiple p2p keys found but peer ID was not set - you must specify a P2P.PeerID " +
"env var if you have more than one key, or delete the keys you aren't using",
)
}
func (ks *p2p) getByID(id p2pkey.PeerID) (p2pkey.KeyV2, error) {
key, found := ks.keyRing.P2P[id.Raw()]
if !found {
return p2pkey.KeyV2{}, KeyNotFoundError{ID: id.String(), KeyType: "P2P"}
}
return key, nil
}