-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
solana.go
177 lines (154 loc) · 3.76 KB
/
solana.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
package keystore
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/solkey"
)
//go:generate mockery --quiet --name Solana --output ./mocks/ --case=underscore --filename solana.go
type Solana interface {
Get(id string) (solkey.Key, error)
GetAll() ([]solkey.Key, error)
Create() (solkey.Key, error)
Add(key solkey.Key) error
Delete(id string) (solkey.Key, error)
Import(keyJSON []byte, password string) (solkey.Key, error)
Export(id string, password string) ([]byte, error)
EnsureKey() error
Sign(ctx context.Context, id string, msg []byte) (signature []byte, err error)
}
// SolanaSigner adapts Solana to [loop.Keystore].
type SolanaSigner struct {
Solana
}
func (s *SolanaSigner) Accounts(ctx context.Context) (accounts []string, err error) {
ks, err := s.GetAll()
if err != nil {
return nil, err
}
for _, k := range ks {
accounts = append(accounts, k.PublicKeyStr())
}
return
}
type solana struct {
*keyManager
}
var _ Solana = &solana{}
func newSolanaKeyStore(km *keyManager) *solana {
return &solana{
km,
}
}
func (ks *solana) Get(id string) (solkey.Key, error) {
ks.lock.RLock()
defer ks.lock.RUnlock()
if ks.isLocked() {
return solkey.Key{}, ErrLocked
}
return ks.getByID(id)
}
func (ks *solana) GetAll() (keys []solkey.Key, _ error) {
ks.lock.RLock()
defer ks.lock.RUnlock()
if ks.isLocked() {
return nil, ErrLocked
}
for _, key := range ks.keyRing.Solana {
keys = append(keys, key)
}
return keys, nil
}
func (ks *solana) Create() (solkey.Key, error) {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return solkey.Key{}, ErrLocked
}
key, err := solkey.New()
if err != nil {
return solkey.Key{}, err
}
return key, ks.safeAddKey(key)
}
func (ks *solana) Add(key solkey.Key) error {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return ErrLocked
}
if _, found := ks.keyRing.Solana[key.ID()]; found {
return fmt.Errorf("key with ID %s already exists", key.ID())
}
return ks.safeAddKey(key)
}
func (ks *solana) Delete(id string) (solkey.Key, error) {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return solkey.Key{}, ErrLocked
}
key, err := ks.getByID(id)
if err != nil {
return solkey.Key{}, err
}
err = ks.safeRemoveKey(key)
return key, err
}
func (ks *solana) Import(keyJSON []byte, password string) (solkey.Key, error) {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return solkey.Key{}, ErrLocked
}
key, err := solkey.FromEncryptedJSON(keyJSON, password)
if err != nil {
return solkey.Key{}, errors.Wrap(err, "SolanaKeyStore#ImportKey failed to decrypt key")
}
if _, found := ks.keyRing.Solana[key.ID()]; found {
return solkey.Key{}, fmt.Errorf("key with ID %s already exists", key.ID())
}
return key, ks.keyManager.safeAddKey(key)
}
func (ks *solana) 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 *solana) EnsureKey() error {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return ErrLocked
}
if len(ks.keyRing.Solana) > 0 {
return nil
}
key, err := solkey.New()
if err != nil {
return err
}
ks.logger.Infof("Created Solana key with ID %s", key.ID())
return ks.safeAddKey(key)
}
func (ks *solana) Sign(_ context.Context, id string, msg []byte) (signature []byte, err error) {
k, err := ks.Get(id)
if err != nil {
return nil, err
}
return k.Sign(msg)
}
func (ks *solana) getByID(id string) (solkey.Key, error) {
key, found := ks.keyRing.Solana[id]
if !found {
return solkey.Key{}, KeyNotFoundError{ID: id, KeyType: "Solana"}
}
return key, nil
}