-
Notifications
You must be signed in to change notification settings - Fork 12
/
commitment.go
137 lines (108 loc) · 3.48 KB
/
commitment.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
package model
import (
"bytes"
"encoding/json"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/serializer/v2/serix"
"github.com/iotaledger/hive.go/stringify"
iotago "github.com/iotaledger/iota.go/v4"
)
type Commitment struct {
api iotago.API
commitmentID iotago.CommitmentID
data []byte
commitment *iotago.Commitment
}
func NewEmptyCommitment(api iotago.API) *Commitment {
emptyCommitment := iotago.NewEmptyCommitment(api)
return lo.PanicOnErr(CommitmentFromCommitment(emptyCommitment, api))
}
func newCommitment(commitmentID iotago.CommitmentID, iotaCommitment *iotago.Commitment, data []byte, api iotago.API) (*Commitment, error) {
return &Commitment{
api: api,
commitmentID: commitmentID,
data: data,
commitment: iotaCommitment,
}, nil
}
func CommitmentFromCommitment(iotaCommitment *iotago.Commitment, api iotago.API, opts ...serix.Option) (*Commitment, error) {
data, err := api.Encode(iotaCommitment, opts...)
if err != nil {
return nil, err
}
commitmentID, err := iotaCommitment.ID()
if err != nil {
return nil, err
}
return newCommitment(commitmentID, iotaCommitment, data, api)
}
func CommitmentFromBytes(apiProvider iotago.APIProvider) func([]byte) (*Commitment, int, error) {
return func(bytes []byte) (*Commitment, int, error) {
var totalBytesRead int
// We read the version byte here to determine the API to use, but then we decode the entire commitment again.
// Thus, we don't count the version byte as read bytes.
version, _, err := iotago.VersionFromBytes(bytes)
if err != nil {
return nil, 0, ierrors.Wrap(err, "failed to determine version")
}
apiForVersion, err := apiProvider.APIForVersion(version)
if err != nil {
return nil, 0, ierrors.Wrapf(err, "failed to get API for version %d", version)
}
iotaCommitment := new(iotago.Commitment)
if totalBytesRead, err = apiForVersion.Decode(bytes, iotaCommitment, serix.WithValidation()); err != nil {
return nil, 0, ierrors.Wrap(err, "failed to decode commitment")
}
commitmentID, err := iotaCommitment.ID()
if err != nil {
return nil, 0, ierrors.Wrap(err, "failed to determine commitment ID")
}
commitment, err := newCommitment(commitmentID, iotaCommitment, bytes, apiForVersion)
if err != nil {
return nil, 0, ierrors.Wrap(err, "failed to create commitment")
}
return commitment, totalBytesRead, nil
}
}
func (c *Commitment) ID() iotago.CommitmentID {
return c.commitmentID
}
func (c *Commitment) Slot() iotago.SlotIndex {
return c.Commitment().Slot
}
func (c *Commitment) PreviousCommitmentID() iotago.CommitmentID {
return c.Commitment().PreviousCommitmentID
}
func (c *Commitment) RootsID() iotago.Identifier {
return c.Commitment().RootsID
}
func (c *Commitment) CumulativeWeight() uint64 {
return c.Commitment().CumulativeWeight
}
func (c *Commitment) ReferenceManaCost() iotago.Mana {
return c.Commitment().ReferenceManaCost
}
func (c *Commitment) Data() []byte {
return c.data
}
func (c *Commitment) Bytes() ([]byte, error) {
return c.data, nil
}
func (c *Commitment) Commitment() *iotago.Commitment {
return c.commitment
}
func (c *Commitment) String() string {
encode, err := c.api.JSONEncode(c.Commitment())
if err != nil {
panic(err)
}
var out bytes.Buffer
if err = json.Indent(&out, encode, "", " "); err != nil {
panic(err)
}
return stringify.Struct("Commitment",
stringify.NewStructField("ID", c.ID()),
stringify.NewStructField("Commitment", out.String()),
)
}