-
Notifications
You must be signed in to change notification settings - Fork 95
/
output_id.gen.go
210 lines (163 loc) · 5.27 KB
/
output_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
package iotago
// Code generated by go generate; DO NOT EDIT. Check gen/ directory instead.
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"sort"
"sync"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/iota.go/v4/hexutil"
)
const (
// OutputIndexLength defines the length of an OutputIndex.
OutputIndexLength = serializer.UInt16ByteSize
// OutputIDLength defines the length of an OutputID.
OutputIDLength = TransactionIDLength + OutputIndexLength
)
var (
EmptyOutputID = OutputID{}
)
// OutputID is a 32 byte hash value together with an 4 byte slot index and a 2 byte output index.
type OutputID [OutputIDLength]byte
// OutputIDFromHexString converts the hex to a OutputID representation.
func OutputIDFromHexString(hex string) (OutputID, error) {
b, err := hexutil.DecodeHex(hex)
if err != nil {
return EmptyOutputID, err
}
s, _, err := OutputIDFromBytes(b)
return s, err
}
// IsValidOutputID returns an error if the passed bytes are not a valid OutputID, otherwise nil.
func IsValidOutputID(b []byte) error {
if len(b) != OutputIDLength {
return ierrors.Errorf("invalid outputID length: expected %d bytes, got %d bytes", OutputIDLength, len(b))
}
return nil
}
// OutputIDFromBytes returns a new OutputID represented by the passed bytes.
func OutputIDFromBytes(b []byte) (OutputID, int, error) {
if len(b) < OutputIDLength {
return EmptyOutputID, 0, ierrors.Errorf("invalid length for outputID, expected at least %d bytes, got %d bytes", OutputIDLength, len(b))
}
return OutputID(b), OutputIDLength, nil
}
// MustOutputIDFromHexString converts the hex to a OutputID representation.
func MustOutputIDFromHexString(hex string) OutputID {
o, err := OutputIDFromHexString(hex)
if err != nil {
panic(err)
}
return o
}
func (o OutputID) Bytes() ([]byte, error) {
return o[:], nil
}
func (o OutputID) MarshalText() (text []byte, err error) {
dst := make([]byte, hex.EncodedLen(len(EmptyOutputID)))
hex.Encode(dst, o[:])
return dst, nil
}
func (o *OutputID) UnmarshalText(text []byte) error {
_, err := hex.Decode(o[:], text)
return err
}
// Empty tells whether the OutputID is empty.
func (o OutputID) Empty() bool {
return o == EmptyOutputID
}
// ToHex converts the Identifier to its hex representation.
func (o OutputID) ToHex() string {
return hexutil.EncodeHex(o[:])
}
func (o OutputID) String() string {
return fmt.Sprintf("OutputID(%s:%d)", o.Alias(), o.Slot())
}
func (o OutputID) Slot() SlotIndex {
return SlotIndex(binary.LittleEndian.Uint32(o[IdentifierLength:]))
}
// Index returns the index of the Output this OutputID references.
func (outputID OutputID) Index() uint16 {
return binary.LittleEndian.Uint16(outputID[TransactionIDLength:])
}
func (o OutputID) Identifier() Identifier {
return Identifier(o[:IdentifierLength])
}
var (
// OutputIDAliases contains a dictionary of identifiers associated to their human-readable alias.
OutputIDAliases = make(map[OutputID]string)
// outputIDAliasesMutex is the mutex that is used to synchronize access to the previous map.
outputIDAliasesMutex = 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 (o OutputID) RegisterAlias(alias string) {
outputIDAliasesMutex.Lock()
defer outputIDAliasesMutex.Unlock()
OutputIDAliases[o] = alias
}
// Alias returns the human-readable alias of the Identifier (or the base58 encoded bytes of no alias was set).
func (o OutputID) Alias() (alias string) {
outputIDAliasesMutex.RLock()
defer outputIDAliasesMutex.RUnlock()
if existingAlias, exists := OutputIDAliases[o]; exists {
return existingAlias
}
return o.ToHex()
}
// UnregisterAlias allows to unregister a previously registered alias.
func (o OutputID) UnregisterAlias() {
outputIDAliasesMutex.Lock()
defer outputIDAliasesMutex.Unlock()
delete(OutputIDAliases, o)
}
// Compare compares two OutputIDs.
func (o OutputID) Compare(other OutputID) int {
return bytes.Compare(o[:], other[:])
}
type OutputIDs []OutputID
// ToHex converts the OutputIDs to their hex representation.
func (ids OutputIDs) ToHex() []string {
hexIDs := make([]string, len(ids))
for i, o := range ids {
hexIDs[i] = hexutil.EncodeHex(o[:])
}
return hexIDs
}
// RemoveDupsAndSort removes duplicated OutputIDs and sorts the slice by the lexical ordering.
func (ids OutputIDs) RemoveDupsAndSort() OutputIDs {
sorted := append(OutputIDs{}, ids...)
sort.Slice(sorted, func(i, j int) bool {
return bytes.Compare(sorted[i][:], sorted[j][:]) == -1
})
var result OutputIDs
var prev OutputID
for i, o := range sorted {
if i == 0 || !bytes.Equal(prev[:], o[:]) {
result = append(result, o)
}
prev = o
}
return result
}
// Sort sorts the OutputIDs lexically and in-place.
func (ids OutputIDs) Sort() {
sort.Slice(ids, func(i, j int) bool {
return ids[i].Compare(ids[j]) < 0
})
}
// OutputIDsFromHexString converts the given block IDs from their hex to OutputID representation.
func OutputIDsFromHexString(OutputIDsHex []string) (OutputIDs, error) {
result := make(OutputIDs, len(OutputIDsHex))
for i, hexString := range OutputIDsHex {
OutputID, err := OutputIDFromHexString(hexString)
if err != nil {
return nil, err
}
result[i] = OutputID
}
return result, nil
}