-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathencode.go
1494 lines (1337 loc) Β· 36.1 KB
/
encode.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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Flow Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package interpreter
import (
"bytes"
"fmt"
"github.com/fxamacker/cbor/v2"
"github.com/onflow/atree"
"github.com/onflow/cadence/common"
"github.com/onflow/cadence/errors"
"github.com/onflow/cadence/values"
)
// CBOREncMode
//
// See https://github.com/fxamacker/cbor:
// "For best performance, reuse EncMode and DecMode after creating them."
var CBOREncMode = func() cbor.EncMode {
options := cbor.CanonicalEncOptions()
options.BigIntConvert = cbor.BigIntConvertNone
encMode, err := options.EncMode()
if err != nil {
panic(err)
}
return encMode
}()
func init() {
// Here, init is only used for sanity checks (coding errors) and does not perform any initialization.
// Check if the CBOR tag number range is not reserved for internal use by atree.
// Cadence must only use available (unreserved by atree) CBOR tag numbers to encode elements in atree managed containers.
// As of Aug 15, 2024:
// - Atree reserves CBOR tag numbers from 240 to 255 for atree internal use.
// - Cadence uses CBOR tag numbers from 128 to 230 to encode internal Cadence values.
// When a new tag number is needed, Atree will use higher tag number first from its reserved range.
// In contrast, Cadence will use lower tag numbers first from its own (different) reserved range.
// This allows Atree and Cadence more flexibility in case we need to revisit the
// allocation of adjacent unused ranges for Atree and Cadence.
minCBORTagNum := uint64(values.CBORTagBase)
maxCBORTagNum := uint64(values.CBORTag_Count) - 1
tagNumOK, err := atree.IsCBORTagNumberRangeAvailable(minCBORTagNum, maxCBORTagNum)
if err != nil {
panic(err)
}
if !tagNumOK {
atreeMinCBORTagNum, atreeMaxCBORTagNum := atree.ReservedCBORTagNumberRange()
panic(fmt.Errorf(
"cadence internal tag numbers [%d, %d] overlaps with atree internal tag numbers [%d, %d]",
minCBORTagNum,
maxCBORTagNum,
atreeMinCBORTagNum,
atreeMaxCBORTagNum))
}
}
// Encode encodes the value as a CBOR nil
func (v NilValue) Encode(e *atree.Encoder) error {
// NOTE: when updating, also update NilValue.ByteSize
return e.CBOR.EncodeNil()
}
// Encode encodes the value as a CBOR string
func (v CharacterValue) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagCharacterValue,
})
if err != nil {
return err
}
return e.CBOR.EncodeString(v.Str)
}
// Encode encodes the value as a CBOR string
func (v *StringValue) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagStringValue,
})
if err != nil {
return err
}
return e.CBOR.EncodeString(v.Str)
}
// Encode encodes the value as a CBOR string
func (v StringAtreeValue) Encode(e *atree.Encoder) error {
return e.CBOR.EncodeString(string(v))
}
// Encode encodes the value as a CBOR unsigned integer
func (v Uint64AtreeValue) Encode(e *atree.Encoder) error {
return e.CBOR.EncodeUint64(uint64(v))
}
// cborVoidValue represents the CBOR value:
//
// cbor.Tag{
// Number: CBORTagVoidValue,
// Content: nil
// }
var cborVoidValue = []byte{
// tag
0xd8, values.CBORTagVoidValue,
// null
0xf6,
}
// Encode writes a value of type Void to the encoder
func (VoidValue) Encode(e *atree.Encoder) error {
// TODO: optimize: use 0xf7, but decoded by github.com/fxamacker/cbor/v2 as Go `nil`:
// https://github.com/fxamacker/cbor/blob/a6ed6ff68e99cbb076997a08d19f03c453851555/README.md#limitations
return e.CBOR.EncodeRawBytes(cborVoidValue)
}
// Encode encodes Int8Value as
//
// cbor.Tag{
// Number: CBORTagInt8Value,
// Content: int8(v),
// }
func (v Int8Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagInt8Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeInt8(int8(v))
}
// Encode encodes Int16Value as
//
// cbor.Tag{
// Number: CBORTagInt16Value,
// Content: int16(v),
// }
func (v Int16Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagInt16Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeInt16(int16(v))
}
// Encode encodes Int32Value as
//
// cbor.Tag{
// Number: CBORTagInt32Value,
// Content: int32(v),
// }
func (v Int32Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagInt32Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeInt32(int32(v))
}
// Encode encodes Int64Value as
//
// cbor.Tag{
// Number: CBORTagInt64Value,
// Content: int64(v),
// }
func (v Int64Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagInt64Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeInt64(int64(v))
}
// Encode encodes Int128Value as
//
// cbor.Tag{
// Number: CBORTagInt128Value,
// Content: *big.Int(v.BigInt),
// }
func (v Int128Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagInt128Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeBigInt(v.BigInt)
}
// Encode encodes Int256Value as
//
// cbor.Tag{
// Number: CBORTagInt256Value,
// Content: *big.Int(v.BigInt),
// }
func (v Int256Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagInt256Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeBigInt(v.BigInt)
}
// Encode encodes UIntValue as
//
// cbor.Tag{
// Number: CBORTagUIntValue,
// Content: *big.Int(v.BigInt),
// }
func (v UIntValue) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagUIntValue,
})
if err != nil {
return err
}
return e.CBOR.EncodeBigInt(v.BigInt)
}
// Encode encodes UInt8Value as
//
// cbor.Tag{
// Number: CBORTagUInt8Value,
// Content: uint8(v),
// }
func (v UInt8Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagUInt8Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeUint8(uint8(v))
}
// Encode encodes UInt16Value as
//
// cbor.Tag{
// Number: CBORTagUInt16Value,
// Content: uint16(v),
// }
func (v UInt16Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagUInt16Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeUint16(uint16(v))
}
// Encode encodes UInt32Value as
//
// cbor.Tag{
// Number: CBORTagUInt32Value,
// Content: uint32(v),
// }
func (v UInt32Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagUInt32Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeUint32(uint32(v))
}
// Encode encodes UInt64Value as
//
// cbor.Tag{
// Number: CBORTagUInt64Value,
// Content: uint64(v),
// }
func (v UInt64Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagUInt64Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeUint64(uint64(v))
}
// Encode encodes UInt128Value as
//
// cbor.Tag{
// Number: CBORTagUInt128Value,
// Content: *big.Int(v.BigInt),
// }
func (v UInt128Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagUInt128Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeBigInt(v.BigInt)
}
// Encode encodes UInt256Value as
//
// cbor.Tag{
// Number: CBORTagUInt256Value,
// Content: *big.Int(v.BigInt),
// }
func (v UInt256Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagUInt256Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeBigInt(v.BigInt)
}
// Encode encodes Word8Value as
//
// cbor.Tag{
// Number: CBORTagWord8Value,
// Content: uint8(v),
// }
func (v Word8Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagWord8Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeUint8(uint8(v))
}
// Encode encodes Word16Value as
//
// cbor.Tag{
// Number: CBORTagWord16Value,
// Content: uint16(v),
// }
func (v Word16Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagWord16Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeUint16(uint16(v))
}
// Encode encodes Word32Value as
//
// cbor.Tag{
// Number: CBORTagWord32Value,
// Content: uint32(v),
// }
func (v Word32Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagWord32Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeUint32(uint32(v))
}
// Encode encodes Word64Value as
//
// cbor.Tag{
// Number: CBORTagWord64Value,
// Content: uint64(v),
// }
func (v Word64Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagWord64Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeUint64(uint64(v))
}
// Encode encodes Word128Value as
//
// cbor.Tag{
// Number: CBORTagWord128Value,
// Content: *big.Int(v.BigInt),
// }
func (v Word128Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagWord128Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeBigInt(v.BigInt)
}
// Encode encodes Word256Value as
//
// cbor.Tag{
// Number: CBORTagWord256Value,
// Content: *big.Int(v.BigInt),
// }
func (v Word256Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagWord256Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeBigInt(v.BigInt)
}
// Encode encodes Fix64Value as
//
// cbor.Tag{
// Number: CBORTagFix64Value,
// Content: int64(v),
// }
func (v Fix64Value) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagFix64Value,
})
if err != nil {
return err
}
return e.CBOR.EncodeInt64(int64(v))
}
var _ atree.ContainerStorable = &SomeStorable{}
func (s SomeStorable) Encode(e *atree.Encoder) error {
nonSomeStorable, nestedLevels := s.nonSomeStorable()
if nestedLevels == 1 {
return s.encode(e)
}
return s.encodeMultipleNestedLevels(e, nestedLevels, nonSomeStorable)
}
// encode encodes SomeStorable with nested levels = 1 as
//
// cbor.Tag{
// Number: CBORTagSomeValue,
// Content: Value(v.Value),
// }
func (s SomeStorable) encode(e *atree.Encoder) error {
// NOTE: when updating, also update SomeStorable.ByteSize
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagSomeValue,
})
if err != nil {
return err
}
return s.Storable.Encode(e)
}
const (
someStorableWithMultipleNestedlevelsArraySize = 1
someStorableWithMultipleNestedLevelsArrayCount = 2
)
// encodeMultipleNestedLevels encodes SomeStorable with nested levels > 1 as
//
// cbor.Tag{
// Number: CBORTagSomeValueWithNestedLevels,
// Content: CBORArray[nested_levels, innermsot_value],
// }
func (s SomeStorable) encodeMultipleNestedLevels(
e *atree.Encoder,
levels uint64,
nonSomeStorable atree.Storable,
) error {
// NOTE: when updating, also update SomeStorable.ByteSize
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagSomeValueWithNestedLevels,
// array of 2 elements
0x82,
})
if err != nil {
return err
}
err = e.CBOR.EncodeUint64(levels)
if err != nil {
return err
}
return nonSomeStorable.Encode(e)
}
// Encode encodes AddressValue as
//
// cbor.Tag{
// Number: CBORTagAddressValue,
// Content: []byte(v.ToAddress().Bytes()),
// }
func (v AddressValue) Encode(e *atree.Encoder) error {
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagAddressValue,
})
if err != nil {
return err
}
return e.CBOR.EncodeBytes(v.ToAddress().Bytes())
}
// NOTE: NEVER change, only add/increment; ensure uint64
const (
// encodedPathValueDomainFieldKey uint64 = 0
// encodedPathValueIdentifierFieldKey uint64 = 1
// !!! *WARNING* !!!
//
// encodedPathValueLength MUST be updated when new element is added.
// It is used to verify encoded path length during decoding.
encodedPathValueLength = 2
)
// Encode encodes PathValue as
//
// cbor.Tag{
// Number: CBORTagPathValue,
// Content: []any{
// encodedPathValueDomainFieldKey: uint(v.Domain),
// encodedPathValueIdentifierFieldKey: string(v.Identifier),
// },
// }
func (v PathValue) Encode(e *atree.Encoder) error {
// Encode tag number and array head
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagPathValue,
// array, 2 items follow
0x82,
})
if err != nil {
return err
}
// Encode domain at array index encodedPathValueDomainFieldKey
err = e.CBOR.EncodeUint(uint(v.Domain))
if err != nil {
return err
}
// Encode identifier at array index encodedPathValueIdentifierFieldKey
return e.CBOR.EncodeString(v.Identifier)
}
// NOTE: NEVER change, only add/increment; ensure uint64
const (
// encodedCapabilityValueAddressFieldKey uint64 = 0
// encodedCapabilityValueIDFieldKey uint64 = 1
// encodedCapabilityValueBorrowTypeFieldKey uint64 = 2
// !!! *WARNING* !!!
//
// encodedCapabilityValueLength MUST be updated when new element is added.
// It is used to verify encoded capability length during decoding.
encodedCapabilityValueLength = 3
)
// Encode encodes IDCapabilityValue as
//
// cbor.Tag{
// Number: CBORTagCapabilityValue,
// Content: []any{
// encodedCapabilityValueAddressFieldKey: AddressValue(v.Address),
// encodedCapabilityValueIDFieldKey: v.ID,
// encodedCapabilityValueBorrowTypeFieldKey: StaticType(v.BorrowType),
// },
// }
func (v *IDCapabilityValue) Encode(e *atree.Encoder) error {
// Encode tag number and array head
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagCapabilityValue,
// array, 3 items follow
0x83,
})
if err != nil {
return err
}
// Encode address at array index encodedCapabilityValueAddressFieldKey
err = v.address.Encode(e)
if err != nil {
return err
}
// Encode ID at array index encodedCapabilityValueIDFieldKey
err = e.CBOR.EncodeUint64(uint64(v.ID))
if err != nil {
return err
}
// Encode borrow type at array index encodedCapabilityValueBorrowTypeFieldKey
return v.BorrowType.Encode(e.CBOR)
}
// NOTE: NEVER change, only add/increment; ensure uint64
const (
// encodedAddressLocationAddressFieldKey uint64 = 0
// encodedAddressLocationNameFieldKey uint64 = 1
// !!! *WARNING* !!!
//
// encodedAddressLocationLength MUST be updated when new element is added.
// It is used to verify encoded address location length during decoding.
encodedAddressLocationLength = 2
)
func encodeLocation(e *cbor.StreamEncoder, l common.Location) error {
if l == nil {
return e.EncodeNil()
}
switch l := l.(type) {
case common.StringLocation:
// common.StringLocation is encoded as
// cbor.Tag{
// Number: CBORTagStringLocation,
// Content: string(l),
// }
err := e.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagStringLocation,
})
if err != nil {
return err
}
return e.EncodeString(string(l))
case common.IdentifierLocation:
// common.IdentifierLocation is encoded as
// cbor.Tag{
// Number: CBORTagIdentifierLocation,
// Content: string(l),
// }
err := e.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagIdentifierLocation,
})
if err != nil {
return err
}
return e.EncodeString(string(l))
case common.AddressLocation:
// common.AddressLocation is encoded as
// cbor.Tag{
// Number: CBORTagAddressLocation,
// Content: []any{
// encodedAddressLocationAddressFieldKey: []byte{l.Address.Bytes()},
// encodedAddressLocationNameFieldKey: string(l.Name),
// },
// }
// Encode tag number and array head
err := e.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagAddressLocation,
// array, 2 items follow
0x82,
})
if err != nil {
return err
}
// Encode address at array index encodedAddressLocationAddressFieldKey
err = e.EncodeBytes(l.Address.Bytes())
if err != nil {
return err
}
// Encode name at array index encodedAddressLocationNameFieldKey
return e.EncodeString(l.Name)
case common.TransactionLocation:
// common.TransactionLocation is encoded as
// cbor.Tag{
// Number: CBORTagTransactionLocation,
// Content: []byte(l),
// }
// Encode tag number and array head
err := e.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagTransactionLocation,
})
if err != nil {
return err
}
return e.EncodeBytes(l[:])
case common.ScriptLocation:
// common.ScriptLocation is encoded as
// cbor.Tag{
// Number: CBORTagScriptLocation,
// Content: []byte(l),
// }
// Encode tag number and array head
err := e.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagScriptLocation,
})
if err != nil {
return err
}
return e.EncodeBytes(l[:])
default:
return errors.NewUnexpectedError("unsupported location: %T", l)
}
}
// NOTE: NEVER change, only add/increment; ensure uint64
const (
// encodedPublishedValueRecipientFieldKey uint64 = 0
// encodedPublishedValueValueFieldKey uint64 = 1
// !!! *WARNING* !!!
//
// encodedPublishedValueLength MUST be updated when new element is added.
// It is used to verify encoded link length during decoding.
encodedPublishedValueLength = 2
)
// Encode encodes PublishedValue as
//
// cbor.Tag{
// Number: CBORTagPublishedValue,
// Content: []any{
// encodedPublishedValueRecipientFieldKey: AddressValue(v.Recipient),
// encodedPublishedValueValueFieldKey: v.Value,
// },
// }
func (v *PublishedValue) Encode(e *atree.Encoder) error {
// Encode tag number and array head
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagPublishedValue,
// array, 2 items follow
0x82,
})
if err != nil {
return err
}
// Encode path at array index encodedPublishedValueRecipientFieldKey
err = v.Recipient.Encode(e)
if err != nil {
return err
}
// Encode type at array index encodedPublishedValueValueFieldKey
return v.Value.Encode(e)
}
// NOTE: NEVER change, only add/increment; ensure uint64
const (
// encodedTypeValueTypeFieldKey uint64 = 0
// !!! *WARNING* !!!
//
// encodedTypeValueTypeLength MUST be updated when new element is added.
// It is used to verify encoded type length during decoding.
encodedTypeValueTypeLength = 1
)
// Encode encodes TypeValue as
//
// cbor.Tag{
// Number: CBORTagTypeValue,
// Content: cborArray{
// encodedTypeValueTypeFieldKey: StaticType(v.Type),
// },
// }
func (v TypeValue) Encode(e *atree.Encoder) error {
// Encode tag number and array head
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagTypeValue,
// array, 1 item follow
0x81,
})
if err != nil {
return err
}
// Encode type at array index encodedTypeValueTypeFieldKey
if v.Type == nil {
return e.CBOR.EncodeNil()
} else {
return v.Type.Encode(e.CBOR)
}
}
// NOTE: NEVER change, only add/increment; ensure uint64
const (
// encodedStorageCapabilityControllerValueBorrowTypeFieldKey uint64 = 0
// encodedStorageCapabilityControllerValueCapabilityIDFieldKey uint64 = 1
// encodedStorageCapabilityControllerValueTargetPathFieldKey uint64 = 2
// !!! *WARNING* !!!
//
// encodedStorageCapabilityControllerValueLength MUST be updated when new element is added.
// It is used to verify encoded storage capability controller length during decoding.
encodedStorageCapabilityControllerValueLength = 3
)
// Encode encodes StorageCapabilityControllerValue as
//
// cbor.Tag{
// Number: CBORTagStorageCapabilityControllerValue,
// Content: []any{
// encodedStorageCapabilityControllerValueBorrowTypeFieldKey: StaticType(v.BorrowType),
// encodedStorageCapabilityControllerValueCapabilityIDFieldKey: UInt64Value(v.CapabilityID),
// encodedStorageCapabilityControllerValueTargetPathFieldKey: PathValue(v.TargetPath),
// },
// }
func (v *StorageCapabilityControllerValue) Encode(e *atree.Encoder) error {
// Encode tag number and array head
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagStorageCapabilityControllerValue,
// array, 3 items follow
0x83,
})
if err != nil {
return err
}
// Encode borrow type at array index encodedStorageCapabilityControllerValueBorrowTypeFieldKey
err = v.BorrowType.Encode(e.CBOR)
if err != nil {
return err
}
// Encode ID at array index encodedStorageCapabilityControllerValueCapabilityIDFieldKey
err = e.CBOR.EncodeUint64(uint64(v.CapabilityID))
if err != nil {
return err
}
// Encode target path at array index encodedStorageCapabilityControllerValueTargetPathFieldKey
return v.TargetPath.Encode(e)
}
// NOTE: NEVER change, only add/increment; ensure uint64
const (
// encodedAccountCapabilityControllerValueBorrowTypeFieldKey uint64 = 0
// encodedAccountCapabilityControllerValueCapabilityIDFieldKey uint64 = 1
// !!! *WARNING* !!!
//
// encodedAccountCapabilityControllerValueLength MUST be updated when new element is added.
// It is used to verify encoded account capability controller length during decoding.
encodedAccountCapabilityControllerValueLength = 2
)
// Encode encodes AccountCapabilityControllerValue as
//
// cbor.Tag{
// Number: CBORTagAccountCapabilityControllerValue,
// Content: []any{
// encodedAccountCapabilityControllerValueBorrowTypeFieldKey: StaticType(v.BorrowType),
// encodedAccountCapabilityControllerValueCapabilityIDFieldKey: UInt64Value(v.CapabilityID),
// },
// }
func (v *AccountCapabilityControllerValue) Encode(e *atree.Encoder) error {
// Encode tag number and array head
err := e.CBOR.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagAccountCapabilityControllerValue,
// array, 2 items follow
0x82,
})
if err != nil {
return err
}
// Encode borrow type at array index encodedAccountCapabilityControllerValueBorrowTypeFieldKey
err = v.BorrowType.Encode(e.CBOR)
if err != nil {
return err
}
// Encode ID at array index encodedAccountCapabilityControllerValueCapabilityIDFieldKey
return e.CBOR.EncodeUint64(uint64(v.CapabilityID))
}
func StaticTypeToBytes(t StaticType) (cbor.RawMessage, error) {
var buf bytes.Buffer
enc := CBOREncMode.NewStreamEncoder(&buf)
err := t.Encode(enc)
if err != nil {
return nil, err
}
err = enc.Flush()
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// Encode encodes PrimitiveStaticType as
//
// cbor.Tag{
// Number: CBORTagPrimitiveStaticType,
// Content: uint(v),
// }
func (t PrimitiveStaticType) Encode(e *cbor.StreamEncoder) error {
err := e.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagPrimitiveStaticType,
})
if err != nil {
return err
}
return e.EncodeUint(uint(t))
}
// Encode encodes OptionalStaticType as
//
// cbor.Tag{
// Number: CBORTagOptionalStaticType,
// Content: StaticType(v.Type),
// }
func (t *OptionalStaticType) Encode(e *cbor.StreamEncoder) error {
err := e.EncodeRawBytes([]byte{
// tag number
0xd8, values.CBORTagOptionalStaticType,
})
if err != nil {
return err
}
return t.Type.Encode(e)
}