-
Notifications
You must be signed in to change notification settings - Fork 95
/
commitment_id.gen.go
220 lines (170 loc) · 5.84 KB
/
commitment_id.gen.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
package iotago
// Code generated by go generate; DO NOT EDIT. Check gen/ directory instead.
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"sort"
"sync"
"golang.org/x/crypto/blake2b"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/iota.go/v4/hexutil"
)
const (
CommitmentIDLength = IdentifierLength + SlotIndexLength
)
var (
EmptyCommitmentID = CommitmentID{}
)
// CommitmentID is a 32 byte hash value together with an 4 byte slot index.
type CommitmentID [CommitmentIDLength]byte
// CommitmentIDRepresentingData returns a new CommitmentID for the given data by hashing it with blake2b and associating it with the given slot index.
func CommitmentIDRepresentingData(slot SlotIndex, data []byte) CommitmentID {
return NewCommitmentID(slot, blake2b.Sum256(data))
}
func NewCommitmentID(slot SlotIndex, idBytes Identifier) CommitmentID {
c := CommitmentID{}
copy(c[:], idBytes[:])
binary.LittleEndian.PutUint32(c[IdentifierLength:], uint32(slot))
return c
}
// CommitmentIDFromHexString converts the hex to a CommitmentID representation.
func CommitmentIDFromHexString(hex string) (CommitmentID, error) {
b, err := hexutil.DecodeHex(hex)
if err != nil {
return EmptyCommitmentID, err
}
s, _, err := CommitmentIDFromBytes(b)
return s, err
}
// IsValidCommitmentID returns an error if the passed bytes are not a valid CommitmentID, otherwise nil.
func IsValidCommitmentID(b []byte) error {
if len(b) != CommitmentIDLength {
return ierrors.Errorf("invalid commitmentID length: expected %d bytes, got %d bytes", CommitmentIDLength, len(b))
}
return nil
}
// CommitmentIDFromBytes returns a new CommitmentID represented by the passed bytes.
func CommitmentIDFromBytes(b []byte) (CommitmentID, int, error) {
if len(b) < CommitmentIDLength {
return EmptyCommitmentID, 0, ierrors.Errorf("invalid length for commitmentID, expected at least %d bytes, got %d bytes", CommitmentIDLength, len(b))
}
return CommitmentID(b), CommitmentIDLength, nil
}
// MustCommitmentIDFromHexString converts the hex to a CommitmentID representation.
func MustCommitmentIDFromHexString(hex string) CommitmentID {
c, err := CommitmentIDFromHexString(hex)
if err != nil {
panic(err)
}
return c
}
func (c CommitmentID) Bytes() ([]byte, error) {
return c[:], nil
}
func (c CommitmentID) MarshalText() (text []byte, err error) {
dst := make([]byte, hex.EncodedLen(len(EmptyCommitmentID)))
hex.Encode(dst, c[:])
return dst, nil
}
func (c *CommitmentID) UnmarshalText(text []byte) error {
_, err := hex.Decode(c[:], text)
return err
}
// Empty tells whether the CommitmentID is empty.
func (c CommitmentID) Empty() bool {
return c == EmptyCommitmentID
}
// ToHex converts the Identifier to its hex representation.
func (c CommitmentID) ToHex() string {
return hexutil.EncodeHex(c[:])
}
func (c CommitmentID) String() string {
return fmt.Sprintf("CommitmentID(%s:%d)", c.Alias(), c.Slot())
}
func (c CommitmentID) Slot() SlotIndex {
return SlotIndex(binary.LittleEndian.Uint32(c[IdentifierLength:]))
}
// Index returns a slot index to conform with hive's IndexedID interface.
func (c CommitmentID) Index() SlotIndex {
return c.Slot()
}
func (c CommitmentID) Identifier() Identifier {
return Identifier(c[:IdentifierLength])
}
var (
// CommitmentIDAliases contains a dictionary of identifiers associated to their human-readable alias.
CommitmentIDAliases = make(map[CommitmentID]string)
// commitmentIDAliasesMutex is the mutex that is used to synchronize access to the previous map.
commitmentIDAliasesMutex = sync.RWMutex{}
)
// RegisterAlias allows to register a human-readable alias for the Identifier which will be used as a replacement for
// the String method.
func (c CommitmentID) RegisterAlias(alias string) {
commitmentIDAliasesMutex.Lock()
defer commitmentIDAliasesMutex.Unlock()
CommitmentIDAliases[c] = alias
}
// Alias returns the human-readable alias of the Identifier (or the base58 encoded bytes of no alias was set).
func (c CommitmentID) Alias() (alias string) {
commitmentIDAliasesMutex.RLock()
defer commitmentIDAliasesMutex.RUnlock()
if existingAlias, exists := CommitmentIDAliases[c]; exists {
return existingAlias
}
return c.ToHex()
}
// UnregisterAlias allows to unregister a previously registered alias.
func (c CommitmentID) UnregisterAlias() {
commitmentIDAliasesMutex.Lock()
defer commitmentIDAliasesMutex.Unlock()
delete(CommitmentIDAliases, c)
}
// Compare compares two CommitmentIDs.
func (c CommitmentID) Compare(other CommitmentID) int {
return bytes.Compare(c[:], other[:])
}
type CommitmentIDs []CommitmentID
// ToHex converts the CommitmentIDs to their hex representation.
func (ids CommitmentIDs) ToHex() []string {
hexIDs := make([]string, len(ids))
for i, c := range ids {
hexIDs[i] = hexutil.EncodeHex(c[:])
}
return hexIDs
}
// RemoveDupsAndSort removes duplicated CommitmentIDs and sorts the slice by the lexical ordering.
func (ids CommitmentIDs) RemoveDupsAndSort() CommitmentIDs {
sorted := append(CommitmentIDs{}, ids...)
sort.Slice(sorted, func(i, j int) bool {
return bytes.Compare(sorted[i][:], sorted[j][:]) == -1
})
var result CommitmentIDs
var prev CommitmentID
for i, c := range sorted {
if i == 0 || !bytes.Equal(prev[:], c[:]) {
result = append(result, c)
}
prev = c
}
return result
}
// Sort sorts the CommitmentIDs lexically and in-place.
func (ids CommitmentIDs) Sort() {
sort.Slice(ids, func(i, j int) bool {
return ids[i].Compare(ids[j]) < 0
})
}
// CommitmentIDsFromHexString converts the given block IDs from their hex to CommitmentID representation.
func CommitmentIDsFromHexString(CommitmentIDsHex []string) (CommitmentIDs, error) {
result := make(CommitmentIDs, len(CommitmentIDsHex))
for i, hexString := range CommitmentIDsHex {
CommitmentID, err := CommitmentIDFromHexString(hexString)
if err != nil {
return nil, err
}
result[i] = CommitmentID
}
return result, nil
}