-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
vrf.go
150 lines (131 loc) · 3.25 KB
/
vrf.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
package keystore
import (
"fmt"
"math/big"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/vrfkey"
)
//go:generate mockery --quiet --name VRF --output ./mocks/ --case=underscore --filename vrf.go
type VRF interface {
Get(id string) (vrfkey.KeyV2, error)
GetAll() ([]vrfkey.KeyV2, error)
Create() (vrfkey.KeyV2, error)
Add(key vrfkey.KeyV2) error
Delete(id string) (vrfkey.KeyV2, error)
Import(keyJSON []byte, password string) (vrfkey.KeyV2, error)
Export(id string, password string) ([]byte, error)
GenerateProof(id string, seed *big.Int) (vrfkey.Proof, error)
}
var (
ErrMissingVRFKey = errors.New("unable to find VRF key")
)
type vrf struct {
*keyManager
}
var _ VRF = &vrf{}
func newVRFKeyStore(km *keyManager) *vrf {
return &vrf{
km,
}
}
func (ks *vrf) Get(id string) (vrfkey.KeyV2, error) {
ks.lock.RLock()
defer ks.lock.RUnlock()
if ks.isLocked() {
return vrfkey.KeyV2{}, ErrLocked
}
return ks.getByID(id)
}
func (ks *vrf) GetAll() (keys []vrfkey.KeyV2, _ error) {
ks.lock.RLock()
defer ks.lock.RUnlock()
if ks.isLocked() {
return nil, ErrLocked
}
for _, key := range ks.keyRing.VRF {
keys = append(keys, key)
}
return keys, nil
}
func (ks *vrf) Create() (vrfkey.KeyV2, error) {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return vrfkey.KeyV2{}, ErrLocked
}
key, err := vrfkey.NewV2()
if err != nil {
return vrfkey.KeyV2{}, err
}
return key, ks.safeAddKey(key)
}
func (ks *vrf) Add(key vrfkey.KeyV2) error {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return ErrLocked
}
if _, found := ks.keyRing.VRF[key.ID()]; found {
return fmt.Errorf("key with ID %s already exists", key.ID())
}
return ks.safeAddKey(key)
}
func (ks *vrf) Delete(id string) (vrfkey.KeyV2, error) {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return vrfkey.KeyV2{}, ErrLocked
}
key, err := ks.getByID(id)
if err != nil {
return vrfkey.KeyV2{}, err
}
err = ks.safeRemoveKey(key)
return key, err
}
func (ks *vrf) Import(keyJSON []byte, password string) (vrfkey.KeyV2, error) {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return vrfkey.KeyV2{}, ErrLocked
}
key, err := vrfkey.FromEncryptedJSON(keyJSON, password)
if err != nil {
return vrfkey.KeyV2{}, errors.Wrap(err, "VRFKeyStore#ImportKey failed to decrypt key")
}
if _, found := ks.keyRing.VRF[key.ID()]; found {
return vrfkey.KeyV2{}, fmt.Errorf("key with ID %s already exists", key.ID())
}
return key, ks.keyManager.safeAddKey(key)
}
func (ks *vrf) Export(id string, 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 *vrf) GenerateProof(id string, seed *big.Int) (vrfkey.Proof, error) {
ks.lock.RLock()
defer ks.lock.RUnlock()
if ks.isLocked() {
return vrfkey.Proof{}, ErrLocked
}
key, err := ks.getByID(id)
if err != nil {
return vrfkey.Proof{}, err
}
return key.GenerateProof(seed)
}
func (ks *vrf) getByID(id string) (vrfkey.KeyV2, error) {
key, found := ks.keyRing.VRF[id]
if !found {
return vrfkey.KeyV2{}, KeyNotFoundError{ID: id, KeyType: "VRF"}
}
return key, nil
}