-
Notifications
You must be signed in to change notification settings - Fork 166
/
accountKey.go
316 lines (265 loc) · 10.2 KB
/
accountKey.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package handler
import (
"encoding/hex"
"fmt"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/sema"
fgcrypto "github.com/onflow/flow-go/crypto"
fghash "github.com/onflow/flow-go/crypto/hash"
"github.com/onflow/flow-go/fvm/crypto"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/state"
"github.com/onflow/flow-go/model/flow"
)
// AccountKeyHandler handles all interaction
// with account keys such as get/set/revoke
type AccountKeyHandler struct {
accounts state.Accounts
}
// NewAccountPublicKey construct an account public key given a runtime public key.
func NewAccountPublicKey(publicKey *runtime.PublicKey,
hashAlgo sema.HashAlgorithm,
keyIndex int,
weight int,
) (*flow.AccountPublicKey, error) {
var err error
signAlgorithm := crypto.RuntimeToCryptoSigningAlgorithm(publicKey.SignAlgo)
if signAlgorithm != fgcrypto.ECDSAP256 && signAlgorithm != fgcrypto.ECDSASecp256k1 {
err = errors.NewValueErrorf(string(publicKey.SignAlgo), "signature algorithm type not supported")
return nil, fmt.Errorf("adding account key failed: %w", err)
}
hashAlgorithm := crypto.RuntimeToCryptoHashingAlgorithm(hashAlgo)
if hashAlgorithm != fghash.SHA2_256 && hashAlgorithm != fghash.SHA3_256 {
err = errors.NewValueErrorf(string(hashAlgo), "hashing algorithm type not supported")
return nil, fmt.Errorf("adding account key failed: %w", err)
}
decodedPublicKey, err := fgcrypto.DecodePublicKey(signAlgorithm, publicKey.PublicKey)
if err != nil {
err = errors.NewValueErrorf(string(publicKey.PublicKey), "cannot decode public key: %w", err)
return nil, fmt.Errorf("adding account key failed: %w", err)
}
return &flow.AccountPublicKey{
Index: keyIndex,
PublicKey: decodedPublicKey,
SignAlgo: signAlgorithm,
HashAlgo: hashAlgorithm,
SeqNumber: 0,
Weight: weight,
Revoked: false,
}, nil
}
func NewAccountKeyHandler(accounts *state.StatefulAccounts) *AccountKeyHandler {
return &AccountKeyHandler{
accounts: accounts,
}
}
// AddAccountKey adds a public key to an existing account.
//
// This function returns an error if the specified account does not exist or
// if the key insertion fails.
func (h *AccountKeyHandler) AddAccountKey(address runtime.Address,
publicKey *runtime.PublicKey,
hashAlgo runtime.HashAlgorithm,
weight int,
) (
*runtime.AccountKey,
error,
) {
accountAddress := flow.Address(address)
ok, err := h.accounts.Exists(accountAddress)
if err != nil {
return nil, fmt.Errorf("adding account key failed: %w", err)
}
if !ok {
issue := errors.NewAccountNotFoundError(accountAddress)
return nil, fmt.Errorf("adding account key failed: %w", issue)
}
keyIndex, err := h.accounts.GetPublicKeyCount(accountAddress)
if err != nil {
return nil, fmt.Errorf("adding account key failed: %w", err)
}
accountPublicKey, err := NewAccountPublicKey(publicKey, hashAlgo, int(keyIndex), weight)
if err != nil {
return nil, fmt.Errorf("adding account key failed: %w", err)
}
err = h.accounts.AppendPublicKey(accountAddress, *accountPublicKey)
if err != nil {
return nil, fmt.Errorf("adding account key failed: %w", err)
}
return &runtime.AccountKey{
KeyIndex: accountPublicKey.Index,
PublicKey: publicKey,
HashAlgo: hashAlgo,
Weight: accountPublicKey.Weight,
IsRevoked: accountPublicKey.Revoked,
}, nil
}
// RevokeAccountKey revokes a public key by index from an existing account,
// and returns the revoked key.
//
// This function returns a nil key with no errors, if a key doesn't exist at the given index.
// An error is returned if the specified account does not exist, the provided index is not valid,
// or if the key revoking fails.
// TODO (ramtin) do we have to return runtime.AccountKey for this method or can be separated into another method
func (h *AccountKeyHandler) RevokeAccountKey(address runtime.Address, keyIndex int) (*runtime.AccountKey, error) {
accountAddress := flow.Address(address)
ok, err := h.accounts.Exists(accountAddress)
if err != nil {
return nil, fmt.Errorf("revoking account key failed: %w", err)
}
if !ok {
issue := errors.NewAccountNotFoundError(accountAddress)
return nil, fmt.Errorf("revoking account key failed: %w", issue)
}
// Don't return an error for invalid key indices
if keyIndex < 0 {
return nil, nil
}
var publicKey flow.AccountPublicKey
publicKey, err = h.accounts.GetPublicKey(accountAddress, uint64(keyIndex))
if err != nil {
// If a key is not found at a given index, then return a nil key with no errors.
// This is to be inline with the Cadence runtime. Otherwise Cadence runtime cannot
// distinguish between a 'key not found error' vs other internal errors.
if errors.IsAccountAccountPublicKeyNotFoundError(err) {
return nil, nil
}
return nil, fmt.Errorf("revoking account key failed: %w", err)
}
// mark this key as revoked
publicKey.Revoked = true
_, err = h.accounts.SetPublicKey(accountAddress, uint64(keyIndex), publicKey)
if err != nil {
return nil, fmt.Errorf("revoking account key failed: %w", err)
}
// Prepare account key to return
signAlgo := crypto.CryptoToRuntimeSigningAlgorithm(publicKey.SignAlgo)
if signAlgo == runtime.SignatureAlgorithmUnknown {
err = errors.NewValueErrorf(publicKey.SignAlgo.String(), "signature algorithm type not found")
return nil, fmt.Errorf("revoking account key failed: %w", err)
}
hashAlgo := crypto.CryptoToRuntimeHashingAlgorithm(publicKey.HashAlgo)
if hashAlgo == runtime.HashAlgorithmUnknown {
err = errors.NewValueErrorf(publicKey.HashAlgo.String(), "hashing algorithm type not found")
return nil, fmt.Errorf("revoking account key failed: %w", err)
}
return &runtime.AccountKey{
KeyIndex: publicKey.Index,
PublicKey: &runtime.PublicKey{
PublicKey: publicKey.PublicKey.Encode(),
SignAlgo: signAlgo,
},
HashAlgo: hashAlgo,
Weight: publicKey.Weight,
IsRevoked: publicKey.Revoked,
}, nil
}
// GetAccountKey retrieves a public key by index from an existing account.
//
// This function returns a nil key with no errors, if a key doesn't exist at the given index.
// An error is returned if the specified account does not exist, the provided index is not valid,
// or if the key retrieval fails.
func (h *AccountKeyHandler) GetAccountKey(address runtime.Address, keyIndex int) (*runtime.AccountKey, error) {
accountAddress := flow.Address(address)
ok, err := h.accounts.Exists(accountAddress)
if err != nil {
return nil, fmt.Errorf("getting account key failed: %w", err)
}
if !ok {
issue := errors.NewAccountNotFoundError(accountAddress)
return nil, fmt.Errorf("getting account key failed: %w", issue)
}
// Don't return an error for invalid key indices
if keyIndex < 0 {
return nil, nil
}
var publicKey flow.AccountPublicKey
publicKey, err = h.accounts.GetPublicKey(accountAddress, uint64(keyIndex))
if err != nil {
// If a key is not found at a given index, then return a nil key with no errors.
// This is to be inline with the Cadence runtime. Otherwise Cadence runtime cannot
// distinguish between a 'key not found error' vs other internal errors.
if errors.IsAccountAccountPublicKeyNotFoundError(err) {
return nil, nil
}
return nil, fmt.Errorf("getting account key failed: %w", err)
}
// Prepare the account key to return
signAlgo := crypto.CryptoToRuntimeSigningAlgorithm(publicKey.SignAlgo)
if signAlgo == runtime.SignatureAlgorithmUnknown {
err = errors.NewValueErrorf(publicKey.SignAlgo.String(), "signature algorithm type not found")
return nil, fmt.Errorf("getting account key failed: %w", err)
}
hashAlgo := crypto.CryptoToRuntimeHashingAlgorithm(publicKey.HashAlgo)
if hashAlgo == runtime.HashAlgorithmUnknown {
err = errors.NewValueErrorf(publicKey.HashAlgo.String(), "hashing algorithm type not found")
return nil, fmt.Errorf("getting account key failed: %w", err)
}
return &runtime.AccountKey{
KeyIndex: publicKey.Index,
PublicKey: &runtime.PublicKey{
PublicKey: publicKey.PublicKey.Encode(),
SignAlgo: signAlgo,
},
HashAlgo: hashAlgo,
Weight: publicKey.Weight,
IsRevoked: publicKey.Revoked,
}, nil
}
// AddEncodedAccountKey adds an encoded public key to an existing account.
//
// This function returns an error if the specified account does not exist or
// if the key insertion fails.
func (e *AccountKeyHandler) AddEncodedAccountKey(address runtime.Address, encodedPublicKey []byte) (err error) {
accountAddress := flow.Address(address)
ok, err := e.accounts.Exists(accountAddress)
if err != nil {
return fmt.Errorf("adding encoded account key failed: %w", err)
}
if !ok {
return errors.NewAccountNotFoundError(accountAddress)
}
var publicKey flow.AccountPublicKey
publicKey, err = flow.DecodeRuntimeAccountPublicKey(encodedPublicKey, 0)
if err != nil {
hexEncodedPublicKey := hex.EncodeToString(encodedPublicKey)
err = errors.NewValueErrorf(hexEncodedPublicKey, "invalid encoded public key value: %w", err)
return fmt.Errorf("adding encoded account key failed: %w", err)
}
err = e.accounts.AppendPublicKey(accountAddress, publicKey)
if err != nil {
return fmt.Errorf("adding encoded account key failed: %w", err)
}
return nil
}
// RemoveAccountKey revokes a public key by index from an existing account.
//
// This function returns an error if the specified account does not exist, the
// provided key is invalid, or if key revoking fails.
func (e *AccountKeyHandler) RemoveAccountKey(address runtime.Address, keyIndex int) (encodedPublicKey []byte, err error) {
accountAddress := flow.Address(address)
ok, err := e.accounts.Exists(accountAddress)
if err != nil {
return nil, fmt.Errorf("remove account key failed: %w", err)
}
if !ok {
issue := errors.NewAccountNotFoundError(accountAddress)
return nil, fmt.Errorf("remove account key failed: %w", issue)
}
if keyIndex < 0 {
err = errors.NewValueErrorf(fmt.Sprint(keyIndex), "key index must be positive")
return nil, fmt.Errorf("remove account key failed: %w", err)
}
var publicKey flow.AccountPublicKey
publicKey, err = e.accounts.GetPublicKey(accountAddress, uint64(keyIndex))
if err != nil {
return nil, fmt.Errorf("remove account key failed: %w", err)
}
// mark this key as revoked
publicKey.Revoked = true
encodedPublicKey, err = e.accounts.SetPublicKey(accountAddress, uint64(keyIndex), publicKey)
if err != nil {
return nil, fmt.Errorf("remove account key failed: %w", err)
}
return encodedPublicKey, nil
}