-
Notifications
You must be signed in to change notification settings - Fork 27
/
auxiliary_data.go
74 lines (59 loc) · 1.58 KB
/
auxiliary_data.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
package cardano
import (
"reflect"
"github.com/echovl/cardano-go/internal/cbor"
)
// Metadata represents the transaction metadata.
type Metadata map[uint]interface{}
// AuxiliaryData is the auxiliary data in the transaction.
type AuxiliaryData struct {
Metadata Metadata `cbor:"0,keyasint,omitempty"`
NativeScripts interface{} `cbor:"1,keyasint,omitempty"`
PlutusScripts interface{} `cbor:"2,keyasint,omitempty"`
}
// MarshalCBOR implements cbor.Marshaler
func (d *AuxiliaryData) MarshalCBOR() ([]byte, error) {
type auxiliaryData AuxiliaryData
// Register tag 259 for maps
tags, err := d.tagSet(auxiliaryData{})
if err != nil {
return nil, err
}
em, err := cbor.CanonicalEncOptions().EncModeWithTags(tags)
if err != nil {
return nil, err
}
return em.Marshal(auxiliaryData(*d))
}
// UnmarshalCBOR implements cbor.Unmarshaler
func (d *AuxiliaryData) UnmarshalCBOR(data []byte) error {
type auxiliaryData AuxiliaryData
// Register tag 259 for maps
tags, err := d.tagSet(auxiliaryData{})
if err != nil {
return err
}
dm, err := cbor.DecOptions{
MapKeyByteString: cbor.MapKeyByteStringWrap,
}.DecModeWithTags(tags)
if err != nil {
return err
}
var dd auxiliaryData
if err := dm.Unmarshal(data, &dd); err != nil {
return err
}
d.Metadata = dd.Metadata
return nil
}
func (d *AuxiliaryData) tagSet(contentType interface{}) (cbor.TagSet, error) {
tags := cbor.NewTagSet()
err := tags.Add(
cbor.TagOptions{EncTag: cbor.EncTagRequired, DecTag: cbor.DecTagRequired},
reflect.TypeOf(contentType),
259)
if err != nil {
return nil, err
}
return tags, nil
}