-
Notifications
You must be signed in to change notification settings - Fork 9
/
txinput.go
326 lines (257 loc) · 6.99 KB
/
txinput.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package types
import (
"fmt"
"io"
"github.com/bytom/vapor/encoding/blockchain"
"github.com/bytom/vapor/errors"
"github.com/bytom/vapor/protocol/bc"
)
// serflag variables for input types.
const (
CrossChainInputType uint8 = iota
SpendInputType
CoinbaseInputType
VetoInputType
)
type (
// TxInput is the top level struct of tx input.
TxInput struct {
AssetVersion uint64
TypedInput
CommitmentSuffix []byte
WitnessSuffix []byte
}
// TypedInput return the txinput type.
TypedInput interface {
InputType() uint8
}
)
// AssetAmount return the asset id and amount of the txinput.
func (t *TxInput) AssetAmount() bc.AssetAmount {
switch inp := t.TypedInput.(type) {
case *SpendInput:
return inp.AssetAmount
case *CrossChainInput:
return inp.AssetAmount
case *VetoInput:
return inp.AssetAmount
}
return bc.AssetAmount{}
}
// AssetID return the assetID of the txinput
func (t *TxInput) AssetID() bc.AssetID {
switch inp := t.TypedInput.(type) {
case *SpendInput:
return *inp.AssetId
case *CrossChainInput:
return *inp.AssetAmount.AssetId
case *VetoInput:
return *inp.AssetId
}
return bc.AssetID{}
}
// Amount return the asset amount of the txinput
func (t *TxInput) Amount() uint64 {
switch inp := t.TypedInput.(type) {
case *SpendInput:
return inp.Amount
case *CrossChainInput:
return inp.AssetAmount.Amount
case *VetoInput:
return inp.Amount
}
return 0
}
// ControlProgram return the control program of the spend input
func (t *TxInput) ControlProgram() []byte {
switch inp := t.TypedInput.(type) {
case *SpendInput:
return inp.ControlProgram
case *CrossChainInput:
return inp.ControlProgram
case *VetoInput:
return inp.ControlProgram
}
return nil
}
// Arguments get the args for the input
func (t *TxInput) Arguments() [][]byte {
switch inp := t.TypedInput.(type) {
case *SpendInput:
return inp.Arguments
case *CrossChainInput:
return inp.Arguments
case *VetoInput:
return inp.Arguments
}
return nil
}
// SetArguments set the args for the input
func (t *TxInput) SetArguments(args [][]byte) {
switch inp := t.TypedInput.(type) {
case *SpendInput:
inp.Arguments = args
case *CrossChainInput:
inp.Arguments = args
case *VetoInput:
inp.Arguments = args
}
}
// SpentOutputID calculate the hash of spended output
func (t *TxInput) SpentOutputID() (o bc.Hash, err error) {
switch inp := t.TypedInput.(type) {
case *SpendInput:
o, err = ComputeOutputID(&inp.SpendCommitment, SpendInputType, nil)
case *VetoInput:
o, err = ComputeOutputID(&inp.SpendCommitment, VetoInputType, inp.Vote)
case *CrossChainInput:
o, err = ComputeOutputID(&inp.SpendCommitment, SpendInputType, nil)
}
return o, err
}
func (t *TxInput) readFrom(r *blockchain.Reader) (err error) {
if t.AssetVersion, err = blockchain.ReadVarint63(r); err != nil {
return err
}
t.CommitmentSuffix, err = blockchain.ReadExtensibleString(r, func(r *blockchain.Reader) error {
if t.AssetVersion != 1 {
return nil
}
var icType [1]byte
if _, err = io.ReadFull(r, icType[:]); err != nil {
return errors.Wrap(err, "reading input commitment type")
}
switch icType[0] {
case SpendInputType:
si := new(SpendInput)
t.TypedInput = si
if si.SpendCommitmentSuffix, err = si.SpendCommitment.readFrom(r, 1); err != nil {
return err
}
case CoinbaseInputType:
ci := new(CoinbaseInput)
t.TypedInput = ci
if ci.Arbitrary, err = blockchain.ReadVarstr31(r); err != nil {
return err
}
case CrossChainInputType:
ci := new(CrossChainInput)
t.TypedInput = ci
if ci.SpendCommitmentSuffix, err = ci.SpendCommitment.readFrom(r, 1); err != nil {
return err
}
if ci.IssuanceVMVersion, err = blockchain.ReadVarint63(r); err != nil {
return err
}
if ci.AssetDefinition, err = blockchain.ReadVarstr31(r); err != nil {
return err
}
if ci.IssuanceProgram, err = blockchain.ReadVarstr31(r); err != nil {
return err
}
case VetoInputType:
ui := new(VetoInput)
t.TypedInput = ui
if ui.VetoCommitmentSuffix, err = ui.SpendCommitment.readFrom(r, 1); err != nil {
return err
}
if ui.Vote, err = blockchain.ReadVarstr31(r); err != nil {
return err
}
default:
return fmt.Errorf("unsupported input type %d", icType[0])
}
return nil
})
if err != nil {
return err
}
t.WitnessSuffix, err = blockchain.ReadExtensibleString(r, func(r *blockchain.Reader) error {
if t.AssetVersion != 1 {
return nil
}
var err error
switch inp := t.TypedInput.(type) {
case *SpendInput:
inp.Arguments, err = blockchain.ReadVarstrList(r)
case *CrossChainInput:
inp.Arguments, err = blockchain.ReadVarstrList(r)
case *VetoInput:
inp.Arguments, err = blockchain.ReadVarstrList(r)
}
return err
})
return err
}
func (t *TxInput) writeTo(w io.Writer) error {
if _, err := blockchain.WriteVarint63(w, t.AssetVersion); err != nil {
return errors.Wrap(err, "writing asset version")
}
if _, err := blockchain.WriteExtensibleString(w, t.CommitmentSuffix, t.writeInputCommitment); err != nil {
return errors.Wrap(err, "writing input commitment")
}
if _, err := blockchain.WriteExtensibleString(w, t.WitnessSuffix, t.writeInputWitness); err != nil {
return errors.Wrap(err, "writing input witness")
}
return nil
}
func (t *TxInput) writeInputCommitment(w io.Writer) (err error) {
if t.AssetVersion != 1 {
return nil
}
switch inp := t.TypedInput.(type) {
case *SpendInput:
if _, err = w.Write([]byte{SpendInputType}); err != nil {
return err
}
return inp.SpendCommitment.writeExtensibleString(w, inp.SpendCommitmentSuffix, t.AssetVersion)
case *CrossChainInput:
if _, err = w.Write([]byte{CrossChainInputType}); err != nil {
return err
}
if err := inp.SpendCommitment.writeExtensibleString(w, inp.SpendCommitmentSuffix, t.AssetVersion); err != nil {
return err
}
if _, err := blockchain.WriteVarint63(w, inp.IssuanceVMVersion); err != nil {
return err
}
if _, err := blockchain.WriteVarstr31(w, inp.AssetDefinition); err != nil {
return err
}
if _, err := blockchain.WriteVarstr31(w, inp.IssuanceProgram); err != nil {
return err
}
case *CoinbaseInput:
if _, err := w.Write([]byte{CoinbaseInputType}); err != nil {
return err
}
if _, err := blockchain.WriteVarstr31(w, inp.Arbitrary); err != nil {
return errors.Wrap(err, "writing coinbase arbitrary")
}
case *VetoInput:
if _, err = w.Write([]byte{VetoInputType}); err != nil {
return err
}
if err := inp.SpendCommitment.writeExtensibleString(w, inp.VetoCommitmentSuffix, t.AssetVersion); err != nil {
return err
}
_, err := blockchain.WriteVarstr31(w, inp.Vote)
return err
}
return nil
}
func (t *TxInput) writeInputWitness(w io.Writer) error {
if t.AssetVersion != 1 {
return nil
}
var err error
switch inp := t.TypedInput.(type) {
case *SpendInput:
_, err = blockchain.WriteVarstrList(w, inp.Arguments)
case *CrossChainInput:
_, err = blockchain.WriteVarstrList(w, inp.Arguments)
case *VetoInput:
_, err = blockchain.WriteVarstrList(w, inp.Arguments)
}
return err
}