7 "github.com/vapor/encoding/blockchain"
8 "github.com/vapor/errors"
9 "github.com/vapor/protocol/bc"
12 // serflag variables for input types.
14 IssuanceInputType uint8 = iota
20 // TxInput is the top level struct of tx input.
24 CommitmentSuffix []byte
28 // TypedInput return the txinput type.
29 TypedInput interface {
34 var errBadAssetID = errors.New("asset ID does not match other issuance parameters")
36 // AssetAmount return the asset id and amount of the txinput.
37 func (t *TxInput) AssetAmount() bc.AssetAmount {
38 switch inp := t.TypedInput.(type) {
40 assetID := inp.AssetID()
41 return bc.AssetAmount{
46 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) {
63 // Amount return the asset amount of the txinput
64 func (t *TxInput) Amount() uint64 {
65 switch inp := t.TypedInput.(type) {
74 // ControlProgram return the control program of the spend input
75 func (t *TxInput) ControlProgram() []byte {
76 if si, ok := t.TypedInput.(*SpendInput); ok {
77 return si.ControlProgram
82 // IssuanceProgram return the control program of the issuance input
83 func (t *TxInput) IssuanceProgram() []byte {
84 if ii, ok := t.TypedInput.(*IssuanceInput); ok {
85 return ii.IssuanceProgram
90 // AssetDefinition return the asset definition of the issuance input
91 func (t *TxInput) AssetDefinition() []byte {
92 if ii, ok := t.TypedInput.(*IssuanceInput); ok {
93 return ii.AssetDefinition
98 // Arguments get the args for the input
99 func (t *TxInput) Arguments() [][]byte {
100 switch inp := t.TypedInput.(type) {
109 // SetArguments set the args for the input
110 func (t *TxInput) SetArguments(args [][]byte) {
111 switch inp := t.TypedInput.(type) {
119 // SpentOutputID calculate the hash of spended output
120 func (t *TxInput) SpentOutputID() (o bc.Hash, err error) {
121 if si, ok := t.TypedInput.(*SpendInput); ok {
122 o, err = ComputeOutputID(&si.SpendCommitment)
127 func (t *TxInput) readFrom(r *blockchain.Reader) (err error) {
128 if t.AssetVersion, err = blockchain.ReadVarint63(r); err != nil {
132 var assetID bc.AssetID
133 t.CommitmentSuffix, err = blockchain.ReadExtensibleString(r, func(r *blockchain.Reader) error {
134 if t.AssetVersion != 1 {
138 if _, err = io.ReadFull(r, icType[:]); err != nil {
139 return errors.Wrap(err, "reading input commitment type")
142 case IssuanceInputType:
143 ii := new(IssuanceInput)
146 if ii.Nonce, err = blockchain.ReadVarstr31(r); err != nil {
149 if _, err = assetID.ReadFrom(r); err != nil {
152 if ii.Amount, err = blockchain.ReadVarint63(r); err != nil {
157 si := new(SpendInput)
159 if si.SpendCommitmentSuffix, err = si.SpendCommitment.readFrom(r, 1); err != nil {
163 case CoinbaseInputType:
164 ci := new(CoinbaseInput)
166 if ci.Arbitrary, err = blockchain.ReadVarstr31(r); err != nil {
171 return fmt.Errorf("unsupported input type %d", icType[0])
179 t.WitnessSuffix, err = blockchain.ReadExtensibleString(r, func(r *blockchain.Reader) error {
180 if t.AssetVersion != 1 {
184 switch inp := t.TypedInput.(type) {
186 if inp.AssetDefinition, err = blockchain.ReadVarstr31(r); err != nil {
189 if inp.VMVersion, err = blockchain.ReadVarint63(r); err != nil {
192 if inp.IssuanceProgram, err = blockchain.ReadVarstr31(r); err != nil {
195 if inp.AssetID() != assetID {
198 if inp.Arguments, err = blockchain.ReadVarstrList(r); err != nil {
203 if inp.Arguments, err = blockchain.ReadVarstrList(r); err != nil {
213 func (t *TxInput) writeTo(w io.Writer) error {
214 if _, err := blockchain.WriteVarint63(w, t.AssetVersion); err != nil {
215 return errors.Wrap(err, "writing asset version")
218 if _, err := blockchain.WriteExtensibleString(w, t.CommitmentSuffix, t.writeInputCommitment); err != nil {
219 return errors.Wrap(err, "writing input commitment")
222 _, err := blockchain.WriteExtensibleString(w, t.WitnessSuffix, t.writeInputWitness)
223 return errors.Wrap(err, "writing input witness")
226 func (t *TxInput) writeInputCommitment(w io.Writer) (err error) {
227 if t.AssetVersion != 1 {
231 switch inp := t.TypedInput.(type) {
233 if _, err = w.Write([]byte{IssuanceInputType}); err != nil {
236 if _, err = blockchain.WriteVarstr31(w, inp.Nonce); err != nil {
239 assetID := t.AssetID()
240 if _, err = assetID.WriteTo(w); err != nil {
243 _, err = blockchain.WriteVarint63(w, inp.Amount)
247 if _, err = w.Write([]byte{SpendInputType}); err != nil {
250 return inp.SpendCommitment.writeExtensibleString(w, inp.SpendCommitmentSuffix, t.AssetVersion)
253 if _, err = w.Write([]byte{CoinbaseInputType}); err != nil {
256 if _, err = blockchain.WriteVarstr31(w, inp.Arbitrary); err != nil {
257 return errors.Wrap(err, "writing coinbase arbitrary")
263 func (t *TxInput) writeInputWitness(w io.Writer) error {
264 if t.AssetVersion != 1 {
268 switch inp := t.TypedInput.(type) {
270 if _, err := blockchain.WriteVarstr31(w, inp.AssetDefinition); err != nil {
273 if _, err := blockchain.WriteVarint63(w, inp.VMVersion); err != nil {
276 if _, err := blockchain.WriteVarstr31(w, inp.IssuanceProgram); err != nil {
279 _, err := blockchain.WriteVarstrList(w, inp.Arguments)
283 _, err := blockchain.WriteVarstrList(w, inp.Arguments)