-
Notifications
You must be signed in to change notification settings - Fork 178
/
account_key_updater.go
624 lines (548 loc) · 15.3 KB
/
account_key_updater.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
package environment
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"
"github.com/onflow/flow-go/module/trace"
)
// 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 {
return nil, fmt.Errorf(
"adding account key failed: %w",
errors.NewValueErrorf(
fmt.Sprintf("%d", publicKey.SignAlgo),
"signature algorithm type not supported"))
}
hashAlgorithm := crypto.RuntimeToCryptoHashingAlgorithm(hashAlgo)
if hashAlgorithm != fghash.SHA2_256 &&
hashAlgorithm != fghash.SHA3_256 {
return nil, fmt.Errorf(
"adding account key failed: %w",
errors.NewValueErrorf(
fmt.Sprintf("%d", hashAlgo),
"hashing algorithm type not supported"))
}
decodedPublicKey, err := fgcrypto.DecodePublicKey(
signAlgorithm,
publicKey.PublicKey)
if err != nil {
return nil, fmt.Errorf(
"adding account key failed: %w",
errors.NewValueErrorf(
hex.EncodeToString(publicKey.PublicKey),
"cannot decode public key: %w",
err))
}
return &flow.AccountPublicKey{
Index: keyIndex,
PublicKey: decodedPublicKey,
SignAlgo: signAlgorithm,
HashAlgo: hashAlgorithm,
SeqNumber: 0,
Weight: weight,
Revoked: false,
}, nil
}
// AccountKeyUpdater handles all account keys modification.
//
// Note that scripts cannot modify account keys, but must expose the API in
// compliance with the runtime environment interface.
type AccountKeyUpdater interface {
// 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.
//
// Note that the script variant will return OperationNotSupportedError.
AddEncodedAccountKey(address runtime.Address, publicKey []byte) error
// RevokeEncodedAccountKey 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.
//
// Note that the script variant will return OperationNotSupportedError.
RevokeEncodedAccountKey(
address runtime.Address,
index int,
) (
[]byte,
error,
)
// 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.
//
// Note that the script variant will return OperationNotSupportedError.
AddAccountKey(
address runtime.Address,
publicKey *runtime.PublicKey,
hashAlgo runtime.HashAlgorithm,
weight int,
) (
*runtime.AccountKey,
error,
)
// 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.
//
// Note that the script variant will return OperationNotSupportedError.
RevokeAccountKey(
address runtime.Address,
keyIndex int,
) (
*runtime.AccountKey,
error,
)
}
type ParseRestrictedAccountKeyUpdater struct {
txnState *state.TransactionState
impl AccountKeyUpdater
}
func NewParseRestrictedAccountKeyUpdater(
txnState *state.TransactionState,
impl AccountKeyUpdater,
) ParseRestrictedAccountKeyUpdater {
return ParseRestrictedAccountKeyUpdater{
txnState: txnState,
impl: impl,
}
}
func (updater ParseRestrictedAccountKeyUpdater) AddEncodedAccountKey(
address runtime.Address,
publicKey []byte,
) error {
return parseRestrict2Arg(
updater.txnState,
"AddEncodedAccountKey",
updater.impl.AddEncodedAccountKey,
address,
publicKey)
}
func (updater ParseRestrictedAccountKeyUpdater) RevokeEncodedAccountKey(
address runtime.Address,
index int,
) (
[]byte,
error,
) {
return parseRestrict2Arg1Ret(
updater.txnState,
"RevokeEncodedAccountKey",
updater.impl.RevokeEncodedAccountKey,
address,
index)
}
func (updater ParseRestrictedAccountKeyUpdater) AddAccountKey(
address runtime.Address,
publicKey *runtime.PublicKey,
hashAlgo runtime.HashAlgorithm,
weight int,
) (
*runtime.AccountKey,
error,
) {
return parseRestrict4Arg1Ret(
updater.txnState,
"AddAccountKey",
updater.impl.AddAccountKey,
address,
publicKey,
hashAlgo,
weight)
}
func (updater ParseRestrictedAccountKeyUpdater) RevokeAccountKey(
address runtime.Address,
keyIndex int,
) (
*runtime.AccountKey,
error,
) {
return parseRestrict2Arg1Ret(
updater.txnState,
"RevokeAccountKey",
updater.impl.RevokeAccountKey,
address,
keyIndex)
}
type NoAccountKeyUpdater struct{}
func (NoAccountKeyUpdater) AddEncodedAccountKey(
address runtime.Address,
publicKey []byte,
) error {
return errors.NewOperationNotSupportedError("AddEncodedAccountKey")
}
func (NoAccountKeyUpdater) RevokeEncodedAccountKey(
address runtime.Address,
index int,
) (
[]byte,
error,
) {
return nil, errors.NewOperationNotSupportedError("RevokeEncodedAccountKey")
}
func (NoAccountKeyUpdater) AddAccountKey(
address runtime.Address,
publicKey *runtime.PublicKey,
hashAlgo runtime.HashAlgorithm,
weight int,
) (
*runtime.AccountKey,
error,
) {
return nil, errors.NewOperationNotSupportedError("AddAccountKey")
}
func (NoAccountKeyUpdater) RevokeAccountKey(
address runtime.Address,
keyIndex int,
) (
*runtime.AccountKey,
error,
) {
return nil, errors.NewOperationNotSupportedError("RevokeAccountKey")
}
type accountKeyUpdater struct {
tracer *Tracer
meter Meter
accounts Accounts
txnState *state.TransactionState
env Environment
}
func NewAccountKeyUpdater(
tracer *Tracer,
meter Meter,
accounts Accounts,
txnState *state.TransactionState,
env Environment,
) *accountKeyUpdater {
return &accountKeyUpdater{
tracer: tracer,
meter: meter,
accounts: accounts,
txnState: txnState,
env: env,
}
}
// 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 (updater *accountKeyUpdater) addAccountKey(
address runtime.Address,
publicKey *runtime.PublicKey,
hashAlgo runtime.HashAlgorithm,
weight int,
) (
*runtime.AccountKey,
error,
) {
accountAddress := flow.Address(address)
ok, err := updater.accounts.Exists(accountAddress)
if err != nil {
return nil, fmt.Errorf("adding account key failed: %w", err)
}
if !ok {
return nil, fmt.Errorf(
"adding account key failed: %w",
errors.NewAccountNotFoundError(accountAddress))
}
keyIndex, err := updater.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 = updater.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 (updater *accountKeyUpdater) revokeAccountKey(
address runtime.Address,
keyIndex int,
) (
*runtime.AccountKey,
error,
) {
accountAddress := flow.Address(address)
ok, err := updater.accounts.Exists(accountAddress)
if err != nil {
return nil, fmt.Errorf("revoking account key failed: %w", err)
}
if !ok {
return nil, fmt.Errorf(
"revoking account key failed: %w",
errors.NewAccountNotFoundError(accountAddress))
}
// Don't return an error for invalid key indices
if keyIndex < 0 {
return nil, nil
}
var publicKey flow.AccountPublicKey
publicKey, err = updater.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 = updater.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 {
return nil, fmt.Errorf(
"revoking account key failed: %w",
errors.NewValueErrorf(
publicKey.SignAlgo.String(),
"signature algorithm type not found"))
}
hashAlgo := crypto.CryptoToRuntimeHashingAlgorithm(publicKey.HashAlgo)
if hashAlgo == runtime.HashAlgorithmUnknown {
return nil, fmt.Errorf(
"revoking account key failed: %w",
errors.NewValueErrorf(
publicKey.HashAlgo.String(),
"hashing algorithm type not found"))
}
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 following error
// * NewAccountNotFoundError - if the specified account does not exist
// * ValueError - if the provided encodedPublicKey is not valid public key
func (updater *accountKeyUpdater) addEncodedAccountKey(
address runtime.Address,
encodedPublicKey []byte,
) error {
accountAddress := flow.Address(address)
ok, err := updater.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)
return fmt.Errorf(
"adding encoded account key failed: %w",
errors.NewValueErrorf(
hexEncodedPublicKey,
"invalid encoded public key value: %w",
err))
}
err = updater.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 (updater *accountKeyUpdater) removeAccountKey(
address runtime.Address,
keyIndex int,
) (
[]byte,
error,
) {
accountAddress := flow.Address(address)
ok, err := updater.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 = updater.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 := updater.accounts.SetPublicKey(
accountAddress,
uint64(keyIndex),
publicKey)
if err != nil {
return nil, fmt.Errorf("remove account key failed: %w", err)
}
return encodedPublicKey, nil
}
func (updater *accountKeyUpdater) AddEncodedAccountKey(
address runtime.Address,
publicKey []byte,
) error {
defer updater.tracer.StartSpanFromRoot(trace.FVMEnvAddAccountKey).End()
err := updater.meter.MeterComputation(
ComputationKindAddEncodedAccountKey,
1)
if err != nil {
return fmt.Errorf("add encoded account key failed: %w", err)
}
err = updater.accounts.CheckAccountNotFrozen(flow.Address(address))
if err != nil {
return fmt.Errorf("add encoded account key failed: %w", err)
}
// TODO do a call to track the computation usage and memory usage
//
// don't enforce limit during adding a key
updater.txnState.RunWithAllLimitsDisabled(func() {
err = updater.addEncodedAccountKey(address, publicKey)
})
if err != nil {
return fmt.Errorf("add encoded account key failed: %w", err)
}
return nil
}
func (updater *accountKeyUpdater) RevokeEncodedAccountKey(
address runtime.Address,
index int,
) (
[]byte,
error,
) {
defer updater.tracer.StartSpanFromRoot(trace.FVMEnvRemoveAccountKey).End()
err := updater.meter.MeterComputation(
ComputationKindRevokeEncodedAccountKey,
1)
if err != nil {
return nil, fmt.Errorf("revoke encoded account key failed: %w", err)
}
err = updater.accounts.CheckAccountNotFrozen(flow.Address(address))
if err != nil {
return nil, fmt.Errorf("revoke encoded account key failed: %w", err)
}
encodedKey, err := updater.removeAccountKey(address, index)
if err != nil {
return nil, fmt.Errorf("revoke encoded account key failed: %w", err)
}
return encodedKey, nil
}
func (updater *accountKeyUpdater) AddAccountKey(
address runtime.Address,
publicKey *runtime.PublicKey,
hashAlgo runtime.HashAlgorithm,
weight int,
) (
*runtime.AccountKey,
error,
) {
defer updater.tracer.StartSpanFromRoot(trace.FVMEnvAddAccountKey).End()
err := updater.meter.MeterComputation(
ComputationKindAddAccountKey,
1)
if err != nil {
return nil, fmt.Errorf("add account key failed: %w", err)
}
accKey, err := updater.addAccountKey(
address,
publicKey,
hashAlgo,
weight)
if err != nil {
return nil, fmt.Errorf("add account key failed: %w", err)
}
return accKey, nil
}
func (updater *accountKeyUpdater) RevokeAccountKey(
address runtime.Address,
keyIndex int,
) (
*runtime.AccountKey,
error,
) {
defer updater.tracer.StartSpanFromRoot(trace.FVMEnvRemoveAccountKey).End()
err := updater.meter.MeterComputation(
ComputationKindRevokeAccountKey,
1)
if err != nil {
return nil, fmt.Errorf("revoke account key failed: %w", err)
}
return updater.revokeAccountKey(address, keyIndex)
}