-
Notifications
You must be signed in to change notification settings - Fork 147
/
output.go
173 lines (142 loc) · 4.32 KB
/
output.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
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
package isc
import (
"bytes"
"fmt"
"io"
"math"
iotago "github.com/iotaledger/iota.go/v3"
"github.com/iotaledger/wasp/packages/hashing"
"github.com/iotaledger/wasp/packages/testutil/testiotago"
"github.com/iotaledger/wasp/packages/util"
"github.com/iotaledger/wasp/packages/util/rwutil"
)
var emptyTransactionID = iotago.TransactionID{}
type OutputInfo struct {
OutputID iotago.OutputID
Output iotago.Output
TransactionIDSpent iotago.TransactionID
}
func (o *OutputInfo) Consumed() bool {
return o.TransactionIDSpent != emptyTransactionID
}
func NewOutputInfo(outputID iotago.OutputID, output iotago.Output, transactionIDSpent iotago.TransactionID) *OutputInfo {
return &OutputInfo{
OutputID: outputID,
Output: output,
TransactionIDSpent: transactionIDSpent,
}
}
func (o *OutputInfo) AliasOutputWithID() *AliasOutputWithID {
return NewAliasOutputWithID(o.Output.(*iotago.AliasOutput), o.OutputID)
}
type AliasOutputWithID struct {
outputID iotago.OutputID
aliasOutput *iotago.AliasOutput
}
func NewAliasOutputWithID(aliasOutput *iotago.AliasOutput, outputID iotago.OutputID) *AliasOutputWithID {
return &AliasOutputWithID{
outputID: outputID,
aliasOutput: aliasOutput,
}
}
// only for testing
func RandomAliasOutputWithID() *AliasOutputWithID {
outputID := testiotago.RandOutputID()
aliasOutput := &iotago.AliasOutput{
StateMetadata: []byte{},
}
return NewAliasOutputWithID(aliasOutput, outputID)
}
func AliasOutputWithIDFromBytes(data []byte) (*AliasOutputWithID, error) {
return rwutil.ReadFromBytes(data, new(AliasOutputWithID))
}
func (a *AliasOutputWithID) Bytes() []byte {
return rwutil.WriteToBytes(a)
}
func (a *AliasOutputWithID) GetAliasOutput() *iotago.AliasOutput {
return a.aliasOutput
}
func (a *AliasOutputWithID) OutputID() iotago.OutputID {
return a.outputID
}
func (a *AliasOutputWithID) TransactionID() iotago.TransactionID {
return a.outputID.TransactionID()
}
func (a *AliasOutputWithID) GetStateIndex() uint32 {
return a.aliasOutput.StateIndex
}
func (a *AliasOutputWithID) GetStateMetadata() []byte {
return a.aliasOutput.StateMetadata
}
func (a *AliasOutputWithID) GetStateAddress() iotago.Address {
return a.aliasOutput.StateController()
}
func (a *AliasOutputWithID) GetAliasID() iotago.AliasID {
return util.AliasIDFromAliasOutput(a.aliasOutput, a.outputID)
}
func (a *AliasOutputWithID) Equals(other *AliasOutputWithID) bool {
if other == nil {
return false
}
if a.outputID != other.outputID {
return false
}
ww1 := rwutil.NewBytesWriter()
ww1.WriteSerialized(a.aliasOutput, math.MaxInt32)
ww2 := rwutil.NewBytesWriter()
ww2.WriteSerialized(other.aliasOutput, math.MaxInt32)
return bytes.Equal(ww1.Bytes(), ww2.Bytes())
}
func (a *AliasOutputWithID) Hash() hashing.HashValue {
return hashing.HashDataBlake2b(a.Bytes())
}
func (a *AliasOutputWithID) String() string {
if a == nil {
return "nil"
}
return fmt.Sprintf("AO[si#%v]%v", a.GetStateIndex(), a.outputID.ToHex())
}
func (a *AliasOutputWithID) Read(r io.Reader) error {
rr := rwutil.NewReader(r)
rr.ReadN(a.outputID[:])
a.aliasOutput = new(iotago.AliasOutput)
rr.ReadSerialized(a.aliasOutput, math.MaxInt32)
return rr.Err
}
func (a *AliasOutputWithID) Write(w io.Writer) error {
ww := rwutil.NewWriter(w)
ww.WriteN(a.outputID[:])
ww.WriteSerialized(a.aliasOutput, math.MaxInt32)
return ww.Err
}
func OutputSetToOutputIDs(outputSet iotago.OutputSet) iotago.OutputIDs {
outputIDs := make(iotago.OutputIDs, len(outputSet))
i := 0
for id := range outputSet {
outputIDs[i] = id
i++
}
return outputIDs
}
func AliasOutputWithIDFromTx(tx *iotago.Transaction, aliasAddr iotago.Address) (*AliasOutputWithID, error) {
txID, err := tx.ID()
if err != nil {
return nil, err
}
for index, output := range tx.Essence.Outputs {
if aliasOutput, ok := output.(*iotago.AliasOutput); ok {
outputID := iotago.OutputIDFromTransactionIDAndIndex(txID, uint16(index))
aliasID := aliasOutput.AliasID
if aliasID.Empty() {
aliasID = iotago.AliasIDFromOutputID(outputID)
}
if aliasID.ToAddress().Equal(aliasAddr) {
// output found
return NewAliasOutputWithID(aliasOutput, outputID), nil
}
}
}
return nil, fmt.Errorf("cannot find alias output for address %v in transaction", aliasAddr.String())
}