-
Notifications
You must be signed in to change notification settings - Fork 95
/
output_id.go
156 lines (124 loc) · 3.96 KB
/
output_id.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
package iotago
import (
"encoding/binary"
"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/iota.go/v4/hexutil"
)
func EmptyOutputIDWithCreationSlot(slot SlotIndex) OutputID {
var outputID OutputID
binary.LittleEndian.PutUint32(outputID[IdentifierLength:OutputIDLength], uint32(slot))
return outputID
}
// TransactionID returns the TransactionID of the Output this OutputID references.
func (o OutputID) TransactionID() TransactionID {
var txID TransactionID
copy(txID[:], o[:TransactionIDLength])
return txID
}
// CreationSlot returns the slot the Output was created in.
func (o OutputID) CreationSlot() SlotIndex {
return o.TransactionID().Slot()
}
// UTXOInput creates a UTXOInput from this OutputID.
func (o OutputID) UTXOInput() *UTXOInput {
return &UTXOInput{
TransactionID: o.TransactionID(),
TransactionOutputIndex: o.Index(),
}
}
// OutputIDFromTransactionIDAndIndex creates a OutputID from the given TransactionID and output index.
func OutputIDFromTransactionIDAndIndex(txID TransactionID, index uint16) OutputID {
utxo := &UTXOInput{
TransactionID: txID,
TransactionOutputIndex: index,
}
return utxo.OutputID()
}
// UTXOInputs converts the OutputIDs slice to Inputs.
func (ids OutputIDs) UTXOInputs() TxEssenceInputs {
inputs := make(TxEssenceInputs, 0)
for _, outputID := range ids {
inputs = append(inputs, outputID.UTXOInput())
}
return inputs
}
// OrderedSet returns an Outputs slice ordered by this OutputIDs slice given an OutputSet.
func (ids OutputIDs) OrderedSet(set OutputSet) Outputs[Output] {
outputs := make(Outputs[Output], len(ids))
for i, outputID := range ids {
outputs[i] = set[outputID]
}
return outputs
}
// OutputIDHex is the hex representation of an output ID.
type OutputIDHex string
// MustSplitParts returns the transaction ID and output index parts of the hex output ID.
// It panics if the hex output ID is invalid.
func (oih OutputIDHex) MustSplitParts() (*TransactionID, uint16) {
txID, outputIndex, err := oih.SplitParts()
if err != nil {
panic(err)
}
return txID, outputIndex
}
// SplitParts returns the transaction ID and output index parts of the hex output ID.
func (oih OutputIDHex) SplitParts() (*TransactionID, uint16, error) {
outputIDBytes, err := hexutil.DecodeHex(string(oih))
if err != nil {
return nil, 0, err
}
var txID TransactionID
copy(txID[:], outputIDBytes[:TransactionIDLength])
outputIndex := binary.LittleEndian.Uint16(outputIDBytes[TransactionIDLength : TransactionIDLength+serializer.UInt16ByteSize])
return &txID, outputIndex, nil
}
// MustAsUTXOInput converts the hex output ID to a UTXOInput.
// It panics if the hex output ID is invalid.
func (oih OutputIDHex) MustAsUTXOInput() *UTXOInput {
utxoInput, err := oih.AsUTXOInput()
if err != nil {
panic(err)
}
return utxoInput
}
// AsUTXOInput converts the hex output ID to a UTXOInput.
func (oih OutputIDHex) AsUTXOInput() (*UTXOInput, error) {
var utxoInput UTXOInput
txID, outputIndex, err := oih.SplitParts()
if err != nil {
return nil, err
}
copy(utxoInput.TransactionID[:], txID[:])
utxoInput.TransactionOutputIndex = outputIndex
return &utxoInput, nil
}
type HexOutputID string
// HexOutputIDs is a slice of hex encoded OutputID strings.
type HexOutputIDs []HexOutputID
// MustOutputIDs converts the hex strings into OutputIDs.
func (ids HexOutputIDs) MustOutputIDs() OutputIDs {
vals, err := ids.OutputIDs()
if err != nil {
panic(err)
}
return vals
}
// OutputIDs converts the hex strings into OutputIDs.
func (ids HexOutputIDs) OutputIDs() (OutputIDs, error) {
vals := make(OutputIDs, len(ids))
for i, v := range ids {
val, err := hexutil.DecodeHex(string(v))
if err != nil {
return nil, err
}
copy(vals[i][:], val)
}
return vals, nil
}
func HexOutputIDsFromOutputIDs(outputIDs ...OutputID) HexOutputIDs {
hexOutputIDs := make(HexOutputIDs, len(outputIDs))
for i, outputID := range outputIDs {
hexOutputIDs[i] = HexOutputID(outputID.ToHex())
}
return hexOutputIDs
}