github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/protocol/bc/types/spend.go (about) 1 package types 2 3 import ( 4 "io" 5 6 "github.com/bytom/bytom/encoding/blockchain" 7 "github.com/bytom/bytom/protocol/bc" 8 ) 9 10 // SpendInput satisfies the TypedInput interface and represents a spend transaction. 11 type SpendInput struct { 12 SpendCommitmentSuffix []byte // The unconsumed suffix of the output commitment 13 Arguments [][]byte // Witness 14 SpendCommitment 15 } 16 17 // NewSpendInput create a new SpendInput struct. 18 func NewSpendInput(arguments [][]byte, sourceID bc.Hash, assetID bc.AssetID, amount, sourcePos uint64, controlProgram []byte, stateData [][]byte) *TxInput { 19 sc := SpendCommitment{ 20 AssetAmount: bc.AssetAmount{ 21 AssetId: &assetID, 22 Amount: amount, 23 }, 24 SourceID: sourceID, 25 SourcePosition: sourcePos, 26 VMVersion: 1, 27 ControlProgram: controlProgram, 28 StateData: stateData, 29 } 30 return &TxInput{ 31 AssetVersion: 1, 32 TypedInput: &SpendInput{ 33 SpendCommitment: sc, 34 Arguments: arguments, 35 }, 36 } 37 } 38 39 // AssetID implement the TypedInput. 40 func (si *SpendInput) AssetID() bc.AssetID { 41 return *si.AssetId 42 } 43 44 // InputType is the interface function for return the input type. 45 func (si *SpendInput) InputType() uint8 { return SpendInputType } 46 47 func (si *SpendInput) readCommitment(r *blockchain.Reader) (err error) { 48 si.SpendCommitmentSuffix, err = si.SpendCommitment.readFrom(r, 1) 49 return err 50 } 51 52 func (si *SpendInput) readWitness(r *blockchain.Reader) (err error) { 53 si.Arguments, err = blockchain.ReadVarstrList(r) 54 return err 55 } 56 57 func (si *SpendInput) writeCommitment(w io.Writer, assetVersion uint64) error { 58 if _, err := w.Write([]byte{SpendInputType}); err != nil { 59 return err 60 } 61 62 return si.SpendCommitment.writeExtensibleString(w, si.SpendCommitmentSuffix, assetVersion) 63 } 64 65 func (si *SpendInput) writeWitness(w io.Writer) error { 66 _, err := blockchain.WriteVarstrList(w, si.Arguments) 67 return err 68 }