7 "github.com/vapor/encoding/blockchain"
8 "github.com/vapor/errors"
9 "github.com/vapor/protocol/bc"
12 // serflag variables for input types.
14 CrossChainInputType uint8 = iota
21 // TxInput is the top level struct of tx input.
25 CommitmentSuffix []byte
29 // TypedInput return the txinput type.
30 TypedInput interface {
35 // AssetAmount return the asset id and amount of the txinput.
36 func (t *TxInput) AssetAmount() bc.AssetAmount {
37 switch inp := t.TypedInput.(type) {
39 return inp.AssetAmount
41 case *CrossChainInput:
42 return inp.AssetAmount
45 return inp.AssetAmount
48 return bc.AssetAmount{}
51 // AssetID return the assetID of the txinput
52 func (t *TxInput) AssetID() bc.AssetID {
53 switch inp := t.TypedInput.(type) {
57 case *CrossChainInput:
58 return *inp.AssetAmount.AssetId
67 // Amount return the asset amount of the txinput
68 func (t *TxInput) Amount() uint64 {
69 switch inp := t.TypedInput.(type) {
73 case *CrossChainInput:
74 return inp.AssetAmount.Amount
83 // ControlProgram return the control program of the spend input
84 func (t *TxInput) ControlProgram() []byte {
85 switch inp := t.TypedInput.(type) {
87 return inp.ControlProgram
89 case *CrossChainInput:
90 return inp.ControlProgram
93 return inp.ControlProgram
100 // Arguments get the args for the input
101 func (t *TxInput) Arguments() [][]byte {
102 switch inp := t.TypedInput.(type) {
106 case *CrossChainInput:
115 // SetArguments set the args for the input
116 func (t *TxInput) SetArguments(args [][]byte) {
117 switch inp := t.TypedInput.(type) {
121 case *CrossChainInput:
129 // SpentOutputID calculate the hash of spended output
130 func (t *TxInput) SpentOutputID() (o bc.Hash, err error) {
131 switch inp := t.TypedInput.(type) {
133 o, err = ComputeOutputID(&inp.SpendCommitment, SpendInputType, nil)
136 o, err = ComputeOutputID(&inp.SpendCommitment, VetoInputType, inp.Vote)
142 func (t *TxInput) readFrom(r *blockchain.Reader) (err error) {
143 if t.AssetVersion, err = blockchain.ReadVarint63(r); err != nil {
147 t.CommitmentSuffix, err = blockchain.ReadExtensibleString(r, func(r *blockchain.Reader) error {
148 if t.AssetVersion != 1 {
152 if _, err = io.ReadFull(r, icType[:]); err != nil {
153 return errors.Wrap(err, "reading input commitment type")
158 si := new(SpendInput)
160 if si.SpendCommitmentSuffix, err = si.SpendCommitment.readFrom(r, 1); err != nil {
164 case CoinbaseInputType:
165 ci := new(CoinbaseInput)
167 if ci.Arbitrary, err = blockchain.ReadVarstr31(r); err != nil {
171 case CrossChainInputType:
172 ci := new(CrossChainInput)
174 if ci.SpendCommitmentSuffix, err = ci.SpendCommitment.readFrom(r, 1); err != nil {
178 if ci.IssuanceVMVersion, err = blockchain.ReadVarint63(r); err != nil {
182 if ci.AssetDefinition, err = blockchain.ReadVarstr31(r); err != nil {
186 if ci.IssuanceProgram, err = blockchain.ReadVarstr31(r); err != nil {
193 if ui.VetoCommitmentSuffix, err = ui.SpendCommitment.readFrom(r, 1); err != nil {
197 if ui.Vote, err = blockchain.ReadVarstr31(r); err != nil {
202 return fmt.Errorf("unsupported input type %d", icType[0])
210 t.WitnessSuffix, err = blockchain.ReadExtensibleString(r, func(r *blockchain.Reader) error {
211 if t.AssetVersion != 1 {
216 switch inp := t.TypedInput.(type) {
218 inp.Arguments, err = blockchain.ReadVarstrList(r)
220 case *CrossChainInput:
221 inp.Arguments, err = blockchain.ReadVarstrList(r)
224 inp.Arguments, err = blockchain.ReadVarstrList(r)
232 func (t *TxInput) writeTo(w io.Writer) error {
233 if _, err := blockchain.WriteVarint63(w, t.AssetVersion); err != nil {
234 return errors.Wrap(err, "writing asset version")
237 if _, err := blockchain.WriteExtensibleString(w, t.CommitmentSuffix, t.writeInputCommitment); err != nil {
238 return errors.Wrap(err, "writing input commitment")
241 if _, err := blockchain.WriteExtensibleString(w, t.WitnessSuffix, t.writeInputWitness); err != nil {
242 return errors.Wrap(err, "writing input witness")
248 func (t *TxInput) writeInputCommitment(w io.Writer) (err error) {
249 if t.AssetVersion != 1 {
253 switch inp := t.TypedInput.(type) {
255 if _, err = w.Write([]byte{SpendInputType}); err != nil {
259 return inp.SpendCommitment.writeExtensibleString(w, inp.SpendCommitmentSuffix, t.AssetVersion)
261 case *CrossChainInput:
262 if _, err = w.Write([]byte{CrossChainInputType}); err != nil {
266 if err := inp.SpendCommitment.writeExtensibleString(w, inp.SpendCommitmentSuffix, t.AssetVersion); err != nil {
270 if _, err := blockchain.WriteVarint63(w, inp.IssuanceVMVersion); err != nil {
274 if _, err := blockchain.WriteVarstr31(w, inp.AssetDefinition); err != nil {
278 if _, err := blockchain.WriteVarstr31(w, inp.IssuanceProgram); err != nil {
283 if _, err := w.Write([]byte{CoinbaseInputType}); err != nil {
287 if _, err := blockchain.WriteVarstr31(w, inp.Arbitrary); err != nil {
288 return errors.Wrap(err, "writing coinbase arbitrary")
292 if _, err = w.Write([]byte{VetoInputType}); err != nil {
296 if err := inp.SpendCommitment.writeExtensibleString(w, inp.VetoCommitmentSuffix, t.AssetVersion); err != nil {
300 _, err := blockchain.WriteVarstr31(w, inp.Vote)
306 func (t *TxInput) writeInputWitness(w io.Writer) error {
307 if t.AssetVersion != 1 {
312 switch inp := t.TypedInput.(type) {
314 _, err = blockchain.WriteVarstrList(w, inp.Arguments)
316 case *CrossChainInput:
317 _, err = blockchain.WriteVarstrList(w, inp.Arguments)
320 _, err = blockchain.WriteVarstrList(w, inp.Arguments)