-
Notifications
You must be signed in to change notification settings - Fork 211
/
encode.go
84 lines (74 loc) · 2.21 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
package types
import (
"bytes"
"errors"
"fmt"
"github.com/nullstyle/go-xdr/xdr3"
"github.com/spacemeshos/go-spacemesh/common/util"
)
// ToBytes returns the BlockID as a byte slice.
func (id BlockID) ToBytes() []byte { return id.AsHash32().Bytes() }
// ToBytes returns the byte representation of the LayerID, using little endian encoding.
func (l LayerID) ToBytes() []byte { return util.Uint64ToBytes(uint64(l)) }
// BlockIdsAsBytes serializes a slice of BlockIDs.
func BlockIdsAsBytes(ids []BlockID) ([]byte, error) {
var w bytes.Buffer
SortBlockIDs(ids)
if _, err := xdr.Marshal(&w, &ids); err != nil {
return nil, errors.New("error marshalling block ids ")
}
return w.Bytes(), nil
}
// BytesToBlockIds deserializes a slice of BlockIDs.
func BytesToBlockIds(blockIds []byte) ([]BlockID, error) {
var ids []BlockID
if _, err := xdr.Unmarshal(bytes.NewReader(blockIds), &ids); err != nil {
return nil, fmt.Errorf("error marshaling layer: %v", err)
}
return ids, nil
}
// BytesAsAtx deserializes an ActivationTx.
func BytesAsAtx(b []byte) (*ActivationTx, error) {
buf := bytes.NewReader(b)
var atx ActivationTx
_, err := xdr.Unmarshal(buf, &atx)
if err != nil {
return nil, err
}
return &atx, nil
}
// NIPSTChallengeAsBytes serializes a NIPSTChallenge.
func NIPSTChallengeAsBytes(challenge *NIPSTChallenge) ([]byte, error) {
var w bytes.Buffer
if _, err := xdr.Marshal(&w, challenge); err != nil {
return nil, fmt.Errorf("error marshalling NIPST Challenge: %v", err)
}
return w.Bytes(), nil
}
// BytesAsTransaction deserializes a Transaction.
func BytesAsTransaction(buf []byte) (*Transaction, error) {
b := Transaction{}
_, err := xdr.Unmarshal(bytes.NewReader(buf), &b)
if err != nil {
return nil, err
}
return &b, nil
}
// BytesToInterface deserializes any type.
// ⚠️ Pass the interface by reference
func BytesToInterface(buf []byte, i interface{}) error {
_, err := xdr.Unmarshal(bytes.NewReader(buf), i)
if err != nil {
return err
}
return nil
}
// InterfaceToBytes serializes any type.
// ⚠️ Pass the interface by reference
func InterfaceToBytes(i interface{}) ([]byte, error) {
var w bytes.Buffer
if _, err := xdr.Marshal(&w, &i); err != nil {
return nil, err
}
return w.Bytes(), nil
}