-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
starknet.go
197 lines (170 loc) · 4.9 KB
/
starknet.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
package keystore
import (
"context"
"fmt"
"math/big"
"github.com/pkg/errors"
"github.com/smartcontractkit/caigo"
"github.com/smartcontractkit/chainlink-common/pkg/loop"
adapters "github.com/smartcontractkit/chainlink-common/pkg/loop/adapters/starknet"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/starkkey"
)
//go:generate mockery --quiet --name StarkNet --output ./mocks/ --case=underscore --filename starknet.go
type StarkNet interface {
Get(id string) (starkkey.Key, error)
GetAll() ([]starkkey.Key, error)
Create() (starkkey.Key, error)
Add(key starkkey.Key) error
Delete(id string) (starkkey.Key, error)
Import(keyJSON []byte, password string) (starkkey.Key, error)
Export(id string, password string) ([]byte, error)
EnsureKey() error
}
type starknet struct {
*keyManager
}
var _ StarkNet = &starknet{}
func newStarkNetKeyStore(km *keyManager) *starknet {
return &starknet{
km,
}
}
func (ks *starknet) Get(id string) (starkkey.Key, error) {
ks.lock.RLock()
defer ks.lock.RUnlock()
if ks.isLocked() {
return starkkey.Key{}, ErrLocked
}
return ks.getByID(id)
}
func (ks *starknet) GetAll() (keys []starkkey.Key, _ error) {
ks.lock.RLock()
defer ks.lock.RUnlock()
if ks.isLocked() {
return nil, ErrLocked
}
for _, key := range ks.keyRing.StarkNet {
keys = append(keys, key)
}
return keys, nil
}
func (ks *starknet) Create() (starkkey.Key, error) {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return starkkey.Key{}, ErrLocked
}
key, err := starkkey.New()
if err != nil {
return starkkey.Key{}, err
}
return key, ks.safeAddKey(key)
}
func (ks *starknet) Add(key starkkey.Key) error {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return ErrLocked
}
if _, found := ks.keyRing.StarkNet[key.ID()]; found {
return fmt.Errorf("key with ID %s already exists", key.ID())
}
return ks.safeAddKey(key)
}
func (ks *starknet) Delete(id string) (starkkey.Key, error) {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return starkkey.Key{}, ErrLocked
}
key, err := ks.getByID(id)
if err != nil {
return starkkey.Key{}, err
}
err = ks.safeRemoveKey(key)
return key, err
}
func (ks *starknet) Import(keyJSON []byte, password string) (starkkey.Key, error) {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return starkkey.Key{}, ErrLocked
}
key, err := starkkey.FromEncryptedJSON(keyJSON, password)
if err != nil {
return starkkey.Key{}, errors.Wrap(err, "StarkNetKeyStore#ImportKey failed to decrypt key")
}
if _, found := ks.keyRing.StarkNet[key.ID()]; found {
return starkkey.Key{}, fmt.Errorf("key with ID %s already exists", key.ID())
}
return key, ks.keyManager.safeAddKey(key)
}
func (ks *starknet) 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 starkkey.ToEncryptedJSON(key, password, ks.scryptParams)
}
func (ks *starknet) EnsureKey() error {
ks.lock.Lock()
defer ks.lock.Unlock()
if ks.isLocked() {
return ErrLocked
}
if len(ks.keyRing.StarkNet) > 0 {
return nil
}
key, err := starkkey.New()
if err != nil {
return err
}
ks.logger.Infof("Created StarkNet key with ID %s", key.ID())
return ks.safeAddKey(key)
}
func (ks *starknet) getByID(id string) (starkkey.Key, error) {
key, found := ks.keyRing.StarkNet[id]
if !found {
return starkkey.Key{}, KeyNotFoundError{ID: id, KeyType: "StarkNet"}
}
return key, nil
}
// StarknetLooppSigner implements [github.com/smartcontractkit/chainlink-common/pkg/loop.Keystore] interface and the requirements
// of signature d/encoding of the [github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/txm.NewKeystoreAdapter]
type StarknetLooppSigner struct {
StarkNet
}
var _ loop.Keystore = &StarknetLooppSigner{}
// Sign implements [loop.Keystore]
// hash is expected to be the byte representation of big.Int
// the returned []byte is an encoded [github.com/smartcontractkit/chainlink-common/pkg/loop/adapters/starknet.Signature].
// this enables compatibility with [github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/txm.NewKeystoreAdapter]
func (lk *StarknetLooppSigner) Sign(ctx context.Context, id string, hash []byte) ([]byte, error) {
k, err := lk.Get(id)
if err != nil {
return nil, err
}
// loopp spec requires passing nil hash to check existence of id
if hash == nil {
return nil, nil
}
starkHash := new(big.Int).SetBytes(hash)
x, y, err := caigo.Curve.Sign(starkHash, k.ToPrivKey())
if err != nil {
return nil, fmt.Errorf("error signing data with curve: %w", err)
}
sig, err := adapters.SignatureFromBigInts(x, y)
if err != nil {
return nil, err
}
return sig.Bytes()
}
// TODO what is this supposed to return for starknet?
func (lk *StarknetLooppSigner) Accounts(ctx context.Context) ([]string, error) {
return nil, fmt.Errorf("unimplemented")
}