github.com/btcsuite/btcd@v0.24.0/wire/msgtx_test.go (about)

     1  // Copyright (c) 2013-2016 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package wire
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"io"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/btcsuite/btcd/chaincfg/chainhash"
    15  	"github.com/davecgh/go-spew/spew"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  // TestTx tests the MsgTx API.
    20  func TestTx(t *testing.T) {
    21  	pver := ProtocolVersion
    22  
    23  	// Block 100000 hash.
    24  	hashStr := "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
    25  	hash, err := chainhash.NewHashFromStr(hashStr)
    26  	if err != nil {
    27  		t.Errorf("NewHashFromStr: %v", err)
    28  	}
    29  
    30  	// Ensure the command is expected value.
    31  	wantCmd := "tx"
    32  	msg := NewMsgTx(1)
    33  	if cmd := msg.Command(); cmd != wantCmd {
    34  		t.Errorf("NewMsgAddr: wrong command - got %v want %v",
    35  			cmd, wantCmd)
    36  	}
    37  
    38  	// Ensure max payload is expected value for latest protocol version.
    39  	wantPayload := uint32(1000 * 4000)
    40  	maxPayload := msg.MaxPayloadLength(pver)
    41  	if maxPayload != wantPayload {
    42  		t.Errorf("MaxPayloadLength: wrong max payload length for "+
    43  			"protocol version %d - got %v, want %v", pver,
    44  			maxPayload, wantPayload)
    45  	}
    46  
    47  	// Ensure we get the same transaction output point data back out.
    48  	// NOTE: This is a block hash and made up index, but we're only
    49  	// testing package functionality.
    50  	prevOutIndex := uint32(1)
    51  	prevOut := NewOutPoint(hash, prevOutIndex)
    52  	if !prevOut.Hash.IsEqual(hash) {
    53  		t.Errorf("NewOutPoint: wrong hash - got %v, want %v",
    54  			spew.Sprint(&prevOut.Hash), spew.Sprint(hash))
    55  	}
    56  	if prevOut.Index != prevOutIndex {
    57  		t.Errorf("NewOutPoint: wrong index - got %v, want %v",
    58  			prevOut.Index, prevOutIndex)
    59  	}
    60  	prevOutStr := fmt.Sprintf("%s:%d", hash.String(), prevOutIndex)
    61  	if s := prevOut.String(); s != prevOutStr {
    62  		t.Errorf("OutPoint.String: unexpected result - got %v, "+
    63  			"want %v", s, prevOutStr)
    64  	}
    65  
    66  	// Ensure we get the same transaction input back out.
    67  	sigScript := []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62}
    68  	witnessData := [][]byte{
    69  		{0x04, 0x31},
    70  		{0x01, 0x43},
    71  	}
    72  	txIn := NewTxIn(prevOut, sigScript, witnessData)
    73  	if !reflect.DeepEqual(&txIn.PreviousOutPoint, prevOut) {
    74  		t.Errorf("NewTxIn: wrong prev outpoint - got %v, want %v",
    75  			spew.Sprint(&txIn.PreviousOutPoint),
    76  			spew.Sprint(prevOut))
    77  	}
    78  	if !bytes.Equal(txIn.SignatureScript, sigScript) {
    79  		t.Errorf("NewTxIn: wrong signature script - got %v, want %v",
    80  			spew.Sdump(txIn.SignatureScript),
    81  			spew.Sdump(sigScript))
    82  	}
    83  	if !reflect.DeepEqual(txIn.Witness, TxWitness(witnessData)) {
    84  		t.Errorf("NewTxIn: wrong witness data - got %v, want %v",
    85  			spew.Sdump(txIn.Witness),
    86  			spew.Sdump(witnessData))
    87  	}
    88  
    89  	// Ensure we get the same transaction output back out.
    90  	txValue := int64(5000000000)
    91  	pkScript := []byte{
    92  		0x41, // OP_DATA_65
    93  		0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
    94  		0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
    95  		0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
    96  		0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
    97  		0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
    98  		0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
    99  		0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
   100  		0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
   101  		0xa6, // 65-byte signature
   102  		0xac, // OP_CHECKSIG
   103  	}
   104  	txOut := NewTxOut(txValue, pkScript)
   105  	if txOut.Value != txValue {
   106  		t.Errorf("NewTxOut: wrong pk script - got %v, want %v",
   107  			txOut.Value, txValue)
   108  
   109  	}
   110  	if !bytes.Equal(txOut.PkScript, pkScript) {
   111  		t.Errorf("NewTxOut: wrong pk script - got %v, want %v",
   112  			spew.Sdump(txOut.PkScript),
   113  			spew.Sdump(pkScript))
   114  	}
   115  
   116  	// Ensure transaction inputs are added properly.
   117  	msg.AddTxIn(txIn)
   118  	if !reflect.DeepEqual(msg.TxIn[0], txIn) {
   119  		t.Errorf("AddTxIn: wrong transaction input added - got %v, want %v",
   120  			spew.Sprint(msg.TxIn[0]), spew.Sprint(txIn))
   121  	}
   122  
   123  	// Ensure transaction outputs are added properly.
   124  	msg.AddTxOut(txOut)
   125  	if !reflect.DeepEqual(msg.TxOut[0], txOut) {
   126  		t.Errorf("AddTxIn: wrong transaction output added - got %v, want %v",
   127  			spew.Sprint(msg.TxOut[0]), spew.Sprint(txOut))
   128  	}
   129  
   130  	// Ensure the copy produced an identical transaction message.
   131  	newMsg := msg.Copy()
   132  	if !reflect.DeepEqual(newMsg, msg) {
   133  		t.Errorf("Copy: mismatched tx messages - got %v, want %v",
   134  			spew.Sdump(newMsg), spew.Sdump(msg))
   135  	}
   136  }
   137  
   138  // TestTxHash tests the ability to generate the hash of a transaction accurately.
   139  func TestTxHash(t *testing.T) {
   140  	// Hash of first transaction from block 113875.
   141  	hashStr := "f051e59b5e2503ac626d03aaeac8ab7be2d72ba4b7e97119c5852d70d52dcb86"
   142  	wantHash, err := chainhash.NewHashFromStr(hashStr)
   143  	if err != nil {
   144  		t.Errorf("NewHashFromStr: %v", err)
   145  		return
   146  	}
   147  
   148  	// First transaction from block 113875.
   149  	msgTx := NewMsgTx(1)
   150  	txIn := TxIn{
   151  		PreviousOutPoint: OutPoint{
   152  			Hash:  chainhash.Hash{},
   153  			Index: 0xffffffff,
   154  		},
   155  		SignatureScript: []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62},
   156  		Sequence:        0xffffffff,
   157  	}
   158  	txOut := TxOut{
   159  		Value: 5000000000,
   160  		PkScript: []byte{
   161  			0x41, // OP_DATA_65
   162  			0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
   163  			0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
   164  			0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
   165  			0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
   166  			0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
   167  			0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
   168  			0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
   169  			0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
   170  			0xa6, // 65-byte signature
   171  			0xac, // OP_CHECKSIG
   172  		},
   173  	}
   174  	msgTx.AddTxIn(&txIn)
   175  	msgTx.AddTxOut(&txOut)
   176  	msgTx.LockTime = 0
   177  
   178  	// Ensure the hash produced is expected.
   179  	txHash := msgTx.TxHash()
   180  	if !txHash.IsEqual(wantHash) {
   181  		t.Errorf("TxHash: wrong hash - got %v, want %v",
   182  			spew.Sprint(txHash), spew.Sprint(wantHash))
   183  	}
   184  }
   185  
   186  // TestTxSha tests the ability to generate the wtxid, and txid of a transaction
   187  // with witness inputs accurately.
   188  func TestWTxSha(t *testing.T) {
   189  	hashStrTxid := "0f167d1385a84d1518cfee208b653fc9163b605ccf1b75347e2850b3e2eb19f3"
   190  	wantHashTxid, err := chainhash.NewHashFromStr(hashStrTxid)
   191  	if err != nil {
   192  		t.Errorf("NewShaHashFromStr: %v", err)
   193  		return
   194  	}
   195  	hashStrWTxid := "0858eab78e77b6b033da30f46699996396cf48fcf625a783c85a51403e175e74"
   196  	wantHashWTxid, err := chainhash.NewHashFromStr(hashStrWTxid)
   197  	if err != nil {
   198  		t.Errorf("NewShaHashFromStr: %v", err)
   199  		return
   200  	}
   201  
   202  	// From block 23157 in a past version of segnet.
   203  	msgTx := NewMsgTx(1)
   204  	txIn := TxIn{
   205  		PreviousOutPoint: OutPoint{
   206  			Hash: chainhash.Hash{
   207  				0xa5, 0x33, 0x52, 0xd5, 0x13, 0x57, 0x66, 0xf0,
   208  				0x30, 0x76, 0x59, 0x74, 0x18, 0x26, 0x3d, 0xa2,
   209  				0xd9, 0xc9, 0x58, 0x31, 0x59, 0x68, 0xfe, 0xa8,
   210  				0x23, 0x52, 0x94, 0x67, 0x48, 0x1f, 0xf9, 0xcd,
   211  			},
   212  			Index: 19,
   213  		},
   214  		Witness: [][]byte{
   215  			{ // 70-byte signature
   216  				0x30, 0x43, 0x02, 0x1f, 0x4d, 0x23, 0x81, 0xdc,
   217  				0x97, 0xf1, 0x82, 0xab, 0xd8, 0x18, 0x5f, 0x51,
   218  				0x75, 0x30, 0x18, 0x52, 0x32, 0x12, 0xf5, 0xdd,
   219  				0xc0, 0x7c, 0xc4, 0xe6, 0x3a, 0x8d, 0xc0, 0x36,
   220  				0x58, 0xda, 0x19, 0x02, 0x20, 0x60, 0x8b, 0x5c,
   221  				0x4d, 0x92, 0xb8, 0x6b, 0x6d, 0xe7, 0xd7, 0x8e,
   222  				0xf2, 0x3a, 0x2f, 0xa7, 0x35, 0xbc, 0xb5, 0x9b,
   223  				0x91, 0x4a, 0x48, 0xb0, 0xe1, 0x87, 0xc5, 0xe7,
   224  				0x56, 0x9a, 0x18, 0x19, 0x70, 0x01,
   225  			},
   226  			{ // 33-byte serialize pub key
   227  				0x03, 0x07, 0xea, 0xd0, 0x84, 0x80, 0x7e, 0xb7,
   228  				0x63, 0x46, 0xdf, 0x69, 0x77, 0x00, 0x0c, 0x89,
   229  				0x39, 0x2f, 0x45, 0xc7, 0x64, 0x25, 0xb2, 0x61,
   230  				0x81, 0xf5, 0x21, 0xd7, 0xf3, 0x70, 0x06, 0x6a,
   231  				0x8f,
   232  			},
   233  		},
   234  		Sequence: 0xffffffff,
   235  	}
   236  	txOut := TxOut{
   237  		Value: 395019,
   238  		PkScript: []byte{
   239  			0x00, // Version 0 witness program
   240  			0x14, // OP_DATA_20
   241  			0x9d, 0xda, 0xc6, 0xf3, 0x9d, 0x51, 0xe0, 0x39,
   242  			0x8e, 0x53, 0x2a, 0x22, 0xc4, 0x1b, 0xa1, 0x89,
   243  			0x40, 0x6a, 0x85, 0x23, // 20-byte pub key hash
   244  		},
   245  	}
   246  	msgTx.AddTxIn(&txIn)
   247  	msgTx.AddTxOut(&txOut)
   248  	msgTx.LockTime = 0
   249  
   250  	// Ensure the correct txid, and wtxid is produced as expected.
   251  	txid := msgTx.TxHash()
   252  	if !txid.IsEqual(wantHashTxid) {
   253  		t.Errorf("TxSha: wrong hash - got %v, want %v",
   254  			spew.Sprint(txid), spew.Sprint(wantHashTxid))
   255  	}
   256  	wtxid := msgTx.WitnessHash()
   257  	if !wtxid.IsEqual(wantHashWTxid) {
   258  		t.Errorf("WTxSha: wrong hash - got %v, want %v",
   259  			spew.Sprint(wtxid), spew.Sprint(wantHashWTxid))
   260  	}
   261  }
   262  
   263  // TestTxWire tests the MsgTx wire encode and decode for various numbers
   264  // of transaction inputs and outputs and protocol versions.
   265  func TestTxWire(t *testing.T) {
   266  	// Empty tx message.
   267  	noTx := NewMsgTx(1)
   268  	noTx.Version = 1
   269  	noTxEncoded := []byte{
   270  		0x01, 0x00, 0x00, 0x00, // Version
   271  		0x00,                   // Varint for number of input transactions
   272  		0x00,                   // Varint for number of output transactions
   273  		0x00, 0x00, 0x00, 0x00, // Lock time
   274  	}
   275  
   276  	tests := []struct {
   277  		in   *MsgTx          // Message to encode
   278  		out  *MsgTx          // Expected decoded message
   279  		buf  []byte          // Wire encoding
   280  		pver uint32          // Protocol version for wire encoding
   281  		enc  MessageEncoding // Message encoding format
   282  	}{
   283  		// Latest protocol version with no transactions.
   284  		{
   285  			noTx,
   286  			noTx, noTxEncoded,
   287  			ProtocolVersion,
   288  			BaseEncoding,
   289  		},
   290  
   291  		// Latest protocol version with multiple transactions.
   292  		{
   293  			multiTx,
   294  			multiTx,
   295  			multiTxEncoded,
   296  			ProtocolVersion,
   297  			BaseEncoding,
   298  		},
   299  
   300  		// Protocol version BIP0035Version with no transactions.
   301  		{
   302  			noTx,
   303  			noTx,
   304  			noTxEncoded,
   305  			BIP0035Version,
   306  			BaseEncoding,
   307  		},
   308  
   309  		// Protocol version BIP0035Version with multiple transactions.
   310  		{
   311  			multiTx,
   312  			multiTx,
   313  			multiTxEncoded,
   314  			BIP0035Version,
   315  			BaseEncoding,
   316  		},
   317  
   318  		// Protocol version BIP0031Version with no transactions.
   319  		{
   320  			noTx,
   321  			noTx,
   322  			noTxEncoded,
   323  			BIP0031Version,
   324  			BaseEncoding,
   325  		},
   326  
   327  		// Protocol version BIP0031Version with multiple transactions.
   328  		{
   329  			multiTx,
   330  			multiTx,
   331  			multiTxEncoded,
   332  			BIP0031Version,
   333  			BaseEncoding,
   334  		},
   335  
   336  		// Protocol version NetAddressTimeVersion with no transactions.
   337  		{
   338  			noTx,
   339  			noTx,
   340  			noTxEncoded,
   341  			NetAddressTimeVersion,
   342  			BaseEncoding,
   343  		},
   344  
   345  		// Protocol version NetAddressTimeVersion with multiple transactions.
   346  		{
   347  			multiTx,
   348  			multiTx,
   349  			multiTxEncoded,
   350  			NetAddressTimeVersion,
   351  			BaseEncoding,
   352  		},
   353  
   354  		// Protocol version MultipleAddressVersion with no transactions.
   355  		{
   356  			noTx,
   357  			noTx,
   358  			noTxEncoded,
   359  			MultipleAddressVersion,
   360  			BaseEncoding,
   361  		},
   362  
   363  		// Protocol version MultipleAddressVersion with multiple transactions.
   364  		{
   365  			multiTx,
   366  			multiTx,
   367  			multiTxEncoded,
   368  			MultipleAddressVersion,
   369  			BaseEncoding,
   370  		},
   371  	}
   372  
   373  	t.Logf("Running %d tests", len(tests))
   374  	for i, test := range tests {
   375  		// Encode the message to wire format.
   376  		var buf bytes.Buffer
   377  		err := test.in.BtcEncode(&buf, test.pver, test.enc)
   378  		if err != nil {
   379  			t.Errorf("BtcEncode #%d error %v", i, err)
   380  			continue
   381  		}
   382  		if !bytes.Equal(buf.Bytes(), test.buf) {
   383  			t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
   384  				spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
   385  			continue
   386  		}
   387  
   388  		// Decode the message from wire format.
   389  		var msg MsgTx
   390  		rbuf := bytes.NewReader(test.buf)
   391  		err = msg.BtcDecode(rbuf, test.pver, test.enc)
   392  		if err != nil {
   393  			t.Errorf("BtcDecode #%d error %v", i, err)
   394  			continue
   395  		}
   396  		if !reflect.DeepEqual(&msg, test.out) {
   397  			t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
   398  				spew.Sdump(&msg), spew.Sdump(test.out))
   399  			continue
   400  		}
   401  	}
   402  }
   403  
   404  // TestTxWireErrors performs negative tests against wire encode and decode
   405  // of MsgTx to confirm error paths work correctly.
   406  func TestTxWireErrors(t *testing.T) {
   407  	// Use protocol version 60002 specifically here instead of the latest
   408  	// because the test data is using bytes encoded with that protocol
   409  	// version.
   410  	pver := uint32(60002)
   411  
   412  	tests := []struct {
   413  		in       *MsgTx          // Value to encode
   414  		buf      []byte          // Wire encoding
   415  		pver     uint32          // Protocol version for wire encoding
   416  		enc      MessageEncoding // Message encoding format
   417  		max      int             // Max size of fixed buffer to induce errors
   418  		writeErr error           // Expected write error
   419  		readErr  error           // Expected read error
   420  	}{
   421  		// Force error in version.
   422  		{multiTx, multiTxEncoded, pver, BaseEncoding, 0, io.ErrShortWrite, io.EOF},
   423  		// Force error in number of transaction inputs.
   424  		{multiTx, multiTxEncoded, pver, BaseEncoding, 4, io.ErrShortWrite, io.EOF},
   425  		// Force error in transaction input previous block hash.
   426  		{multiTx, multiTxEncoded, pver, BaseEncoding, 5, io.ErrShortWrite, io.EOF},
   427  		// Force error in transaction input previous block output index.
   428  		{multiTx, multiTxEncoded, pver, BaseEncoding, 37, io.ErrShortWrite, io.EOF},
   429  		// Force error in transaction input signature script length.
   430  		{multiTx, multiTxEncoded, pver, BaseEncoding, 41, io.ErrShortWrite, io.EOF},
   431  		// Force error in transaction input signature script.
   432  		{multiTx, multiTxEncoded, pver, BaseEncoding, 42, io.ErrShortWrite, io.EOF},
   433  		// Force error in transaction input sequence.
   434  		{multiTx, multiTxEncoded, pver, BaseEncoding, 49, io.ErrShortWrite, io.EOF},
   435  		// Force error in number of transaction outputs.
   436  		{multiTx, multiTxEncoded, pver, BaseEncoding, 53, io.ErrShortWrite, io.EOF},
   437  		// Force error in transaction output value.
   438  		{multiTx, multiTxEncoded, pver, BaseEncoding, 54, io.ErrShortWrite, io.EOF},
   439  		// Force error in transaction output pk script length.
   440  		{multiTx, multiTxEncoded, pver, BaseEncoding, 62, io.ErrShortWrite, io.EOF},
   441  		// Force error in transaction output pk script.
   442  		{multiTx, multiTxEncoded, pver, BaseEncoding, 63, io.ErrShortWrite, io.EOF},
   443  		// Force error in transaction output lock time.
   444  		{multiTx, multiTxEncoded, pver, BaseEncoding, 206, io.ErrShortWrite, io.EOF},
   445  	}
   446  
   447  	t.Logf("Running %d tests", len(tests))
   448  	for i, test := range tests {
   449  		// Encode to wire format.
   450  		w := newFixedWriter(test.max)
   451  		err := test.in.BtcEncode(w, test.pver, test.enc)
   452  		if err != test.writeErr {
   453  			t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
   454  				i, err, test.writeErr)
   455  			continue
   456  		}
   457  
   458  		// Decode from wire format.
   459  		var msg MsgTx
   460  		r := newFixedReader(test.max, test.buf)
   461  		err = msg.BtcDecode(r, test.pver, test.enc)
   462  		if err != test.readErr {
   463  			t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
   464  				i, err, test.readErr)
   465  			continue
   466  		}
   467  	}
   468  }
   469  
   470  // TestTxSerialize tests MsgTx serialize and deserialize.
   471  func TestTxSerialize(t *testing.T) {
   472  	noTx := NewMsgTx(1)
   473  	noTx.Version = 1
   474  	noTxEncoded := []byte{
   475  		0x01, 0x00, 0x00, 0x00, // Version
   476  		0x00,                   // Varint for number of input transactions
   477  		0x00,                   // Varint for number of output transactions
   478  		0x00, 0x00, 0x00, 0x00, // Lock time
   479  	}
   480  
   481  	tests := []struct {
   482  		in           *MsgTx // Message to encode
   483  		out          *MsgTx // Expected decoded message
   484  		buf          []byte // Serialized data
   485  		pkScriptLocs []int  // Expected output script locations
   486  		witness      bool   // Serialize using the witness encoding
   487  	}{
   488  		// No transactions.
   489  		{
   490  			noTx,
   491  			noTx,
   492  			noTxEncoded,
   493  			nil,
   494  			false,
   495  		},
   496  
   497  		// Multiple transactions.
   498  		{
   499  			multiTx,
   500  			multiTx,
   501  			multiTxEncoded,
   502  			multiTxPkScriptLocs,
   503  			false,
   504  		},
   505  		// Multiple outputs witness transaction.
   506  		{
   507  			multiWitnessTx,
   508  			multiWitnessTx,
   509  			multiWitnessTxEncoded,
   510  			multiWitnessTxPkScriptLocs,
   511  			true,
   512  		},
   513  	}
   514  
   515  	t.Logf("Running %d tests", len(tests))
   516  	for i, test := range tests {
   517  		// Serialize the transaction.
   518  		var buf bytes.Buffer
   519  		err := test.in.Serialize(&buf)
   520  		if err != nil {
   521  			t.Errorf("Serialize #%d error %v", i, err)
   522  			continue
   523  		}
   524  		if !bytes.Equal(buf.Bytes(), test.buf) {
   525  			t.Errorf("Serialize #%d\n got: %s want: %s", i,
   526  				spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
   527  			continue
   528  		}
   529  
   530  		// Deserialize the transaction.
   531  		var tx MsgTx
   532  		rbuf := bytes.NewReader(test.buf)
   533  		if test.witness {
   534  			err = tx.Deserialize(rbuf)
   535  		} else {
   536  			err = tx.DeserializeNoWitness(rbuf)
   537  		}
   538  		if err != nil {
   539  			t.Errorf("Deserialize #%d error %v", i, err)
   540  			continue
   541  		}
   542  		if !reflect.DeepEqual(&tx, test.out) {
   543  			t.Errorf("Deserialize #%d\n got: %s want: %s", i,
   544  				spew.Sdump(&tx), spew.Sdump(test.out))
   545  			continue
   546  		}
   547  
   548  		// Ensure the public key script locations are accurate.
   549  		pkScriptLocs := test.in.PkScriptLocs()
   550  		if !reflect.DeepEqual(pkScriptLocs, test.pkScriptLocs) {
   551  			t.Errorf("PkScriptLocs #%d\n got: %s want: %s", i,
   552  				spew.Sdump(pkScriptLocs),
   553  				spew.Sdump(test.pkScriptLocs))
   554  			continue
   555  		}
   556  		for j, loc := range pkScriptLocs {
   557  			wantPkScript := test.in.TxOut[j].PkScript
   558  			gotPkScript := test.buf[loc : loc+len(wantPkScript)]
   559  			if !bytes.Equal(gotPkScript, wantPkScript) {
   560  				t.Errorf("PkScriptLocs #%d:%d\n unexpected "+
   561  					"script got: %s want: %s", i, j,
   562  					spew.Sdump(gotPkScript),
   563  					spew.Sdump(wantPkScript))
   564  			}
   565  		}
   566  	}
   567  }
   568  
   569  // TestTxSerializeErrors performs negative tests against wire encode and decode
   570  // of MsgTx to confirm error paths work correctly.
   571  func TestTxSerializeErrors(t *testing.T) {
   572  	tests := []struct {
   573  		in       *MsgTx // Value to encode
   574  		buf      []byte // Serialized data
   575  		max      int    // Max size of fixed buffer to induce errors
   576  		writeErr error  // Expected write error
   577  		readErr  error  // Expected read error
   578  	}{
   579  		// Force error in version.
   580  		{multiTx, multiTxEncoded, 0, io.ErrShortWrite, io.EOF},
   581  		// Force error in number of transaction inputs.
   582  		{multiTx, multiTxEncoded, 4, io.ErrShortWrite, io.EOF},
   583  		// Force error in transaction input previous block hash.
   584  		{multiTx, multiTxEncoded, 5, io.ErrShortWrite, io.EOF},
   585  		// Force error in transaction input previous block output index.
   586  		{multiTx, multiTxEncoded, 37, io.ErrShortWrite, io.EOF},
   587  		// Force error in transaction input signature script length.
   588  		{multiTx, multiTxEncoded, 41, io.ErrShortWrite, io.EOF},
   589  		// Force error in transaction input signature script.
   590  		{multiTx, multiTxEncoded, 42, io.ErrShortWrite, io.EOF},
   591  		// Force error in transaction input sequence.
   592  		{multiTx, multiTxEncoded, 49, io.ErrShortWrite, io.EOF},
   593  		// Force error in number of transaction outputs.
   594  		{multiTx, multiTxEncoded, 53, io.ErrShortWrite, io.EOF},
   595  		// Force error in transaction output value.
   596  		{multiTx, multiTxEncoded, 54, io.ErrShortWrite, io.EOF},
   597  		// Force error in transaction output pk script length.
   598  		{multiTx, multiTxEncoded, 62, io.ErrShortWrite, io.EOF},
   599  		// Force error in transaction output pk script.
   600  		{multiTx, multiTxEncoded, 63, io.ErrShortWrite, io.EOF},
   601  		// Force error in transaction output lock time.
   602  		{multiTx, multiTxEncoded, 206, io.ErrShortWrite, io.EOF},
   603  	}
   604  
   605  	t.Logf("Running %d tests", len(tests))
   606  	for i, test := range tests {
   607  		// Serialize the transaction.
   608  		w := newFixedWriter(test.max)
   609  		err := test.in.Serialize(w)
   610  		if err != test.writeErr {
   611  			t.Errorf("Serialize #%d wrong error got: %v, want: %v",
   612  				i, err, test.writeErr)
   613  			continue
   614  		}
   615  
   616  		// Deserialize the transaction.
   617  		var tx MsgTx
   618  		r := newFixedReader(test.max, test.buf)
   619  		err = tx.Deserialize(r)
   620  		if err != test.readErr {
   621  			t.Errorf("Deserialize #%d wrong error got: %v, want: %v",
   622  				i, err, test.readErr)
   623  			continue
   624  		}
   625  	}
   626  }
   627  
   628  // TestTxOverflowErrors performs tests to ensure deserializing transactions
   629  // which are intentionally crafted to use large values for the variable number
   630  // of inputs and outputs are handled properly.  This could otherwise potentially
   631  // be used as an attack vector.
   632  func TestTxOverflowErrors(t *testing.T) {
   633  	// Use protocol version 70001 and transaction version 1 specifically
   634  	// here instead of the latest values because the test data is using
   635  	// bytes encoded with those versions.
   636  	pver := uint32(70001)
   637  	txVer := uint32(1)
   638  
   639  	tests := []struct {
   640  		buf     []byte          // Wire encoding
   641  		pver    uint32          // Protocol version for wire encoding
   642  		enc     MessageEncoding // Message encoding format
   643  		version uint32          // Transaction version
   644  		err     error           // Expected error
   645  	}{
   646  		// Transaction that claims to have ~uint64(0) inputs.
   647  		{
   648  			[]byte{
   649  				0x00, 0x00, 0x00, 0x01, // Version
   650  				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   651  				0xff, // Varint for number of input transactions
   652  			}, pver, BaseEncoding, txVer, &MessageError{},
   653  		},
   654  
   655  		// Transaction that claims to have ~uint64(0) outputs.
   656  		{
   657  			[]byte{
   658  				0x00, 0x00, 0x00, 0x01, // Version
   659  				0x00, // Varint for number of input transactions
   660  				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   661  				0xff, // Varint for number of output transactions
   662  			}, pver, BaseEncoding, txVer, &MessageError{},
   663  		},
   664  
   665  		// Transaction that has an input with a signature script that
   666  		// claims to have ~uint64(0) length.
   667  		{
   668  			[]byte{
   669  				0x00, 0x00, 0x00, 0x01, // Version
   670  				0x01, // Varint for number of input transactions
   671  				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   672  				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   673  				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   674  				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
   675  				0xff, 0xff, 0xff, 0xff, // Prevous output index
   676  				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   677  				0xff, // Varint for length of signature script
   678  			}, pver, BaseEncoding, txVer, &MessageError{},
   679  		},
   680  
   681  		// Transaction that has an output with a public key script
   682  		// that claims to have ~uint64(0) length.
   683  		{
   684  			[]byte{
   685  				0x00, 0x00, 0x00, 0x01, // Version
   686  				0x01, // Varint for number of input transactions
   687  				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   688  				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   689  				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   690  				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
   691  				0xff, 0xff, 0xff, 0xff, // Prevous output index
   692  				0x00,                   // Varint for length of signature script
   693  				0xff, 0xff, 0xff, 0xff, // Sequence
   694  				0x01,                                           // Varint for number of output transactions
   695  				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Transaction amount
   696  				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
   697  				0xff, // Varint for length of public key script
   698  			}, pver, BaseEncoding, txVer, &MessageError{},
   699  		},
   700  	}
   701  
   702  	t.Logf("Running %d tests", len(tests))
   703  	for i, test := range tests {
   704  		// Decode from wire format.
   705  		var msg MsgTx
   706  		r := bytes.NewReader(test.buf)
   707  		err := msg.BtcDecode(r, test.pver, test.enc)
   708  		if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
   709  			t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
   710  				i, err, reflect.TypeOf(test.err))
   711  			continue
   712  		}
   713  
   714  		// Decode from wire format.
   715  		r = bytes.NewReader(test.buf)
   716  		err = msg.Deserialize(r)
   717  		if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
   718  			t.Errorf("Deserialize #%d wrong error got: %v, want: %v",
   719  				i, err, reflect.TypeOf(test.err))
   720  			continue
   721  		}
   722  	}
   723  }
   724  
   725  // TestTxSerializeSizeStripped performs tests to ensure the serialize size for
   726  // various transactions is accurate.
   727  func TestTxSerializeSizeStripped(t *testing.T) {
   728  	// Empty tx message.
   729  	noTx := NewMsgTx(1)
   730  	noTx.Version = 1
   731  
   732  	tests := []struct {
   733  		in   *MsgTx // Tx to encode
   734  		size int    // Expected serialized size
   735  	}{
   736  		// No inputs or outpus.
   737  		{noTx, 10},
   738  
   739  		// Transcaction with an input and an output.
   740  		{multiTx, 210},
   741  
   742  		// Transaction with an input which includes witness data, and
   743  		// one output. Note that this uses SerializeSizeStripped which
   744  		// excludes the additional bytes due to witness data encoding.
   745  		{multiWitnessTx, 82},
   746  	}
   747  
   748  	t.Logf("Running %d tests", len(tests))
   749  	for i, test := range tests {
   750  		serializedSize := test.in.SerializeSizeStripped()
   751  		if serializedSize != test.size {
   752  			t.Errorf("MsgTx.SerializeSizeStripped: #%d got: %d, want: %d", i,
   753  				serializedSize, test.size)
   754  			continue
   755  		}
   756  	}
   757  }
   758  
   759  // TestTxWitnessSize performs tests to ensure that the serialized size for
   760  // various types of transactions that include witness data is accurate.
   761  func TestTxWitnessSize(t *testing.T) {
   762  	tests := []struct {
   763  		in   *MsgTx // Tx to encode
   764  		size int    // Expected serialized size w/ witnesses
   765  	}{
   766  		// Transaction with an input which includes witness data, and
   767  		// one output.
   768  		{multiWitnessTx, 190},
   769  	}
   770  
   771  	t.Logf("Running %d tests", len(tests))
   772  	for i, test := range tests {
   773  		serializedSize := test.in.SerializeSize()
   774  		if serializedSize != test.size {
   775  			t.Errorf("MsgTx.SerializeSize: #%d got: %d, want: %d", i,
   776  				serializedSize, test.size)
   777  			continue
   778  		}
   779  	}
   780  }
   781  
   782  // TestTxOutPointFromString performs tests to ensure that the outpoint string
   783  // parser works as expected.
   784  func TestTxOutPointFromString(t *testing.T) {
   785  	hashFromStr := func(hash string) chainhash.Hash {
   786  		h, _ := chainhash.NewHashFromStr(hash)
   787  		return *h
   788  	}
   789  
   790  	tests := []struct {
   791  		name   string
   792  		input  string
   793  		result *OutPoint
   794  		err    bool
   795  	}{
   796  		{
   797  			name:  "normal outpoint 1",
   798  			input: "2ebd15a7e758d5f4c7c74181b99e5b8586f88e0682dc13e09d92612a2b2bb0a2:1",
   799  			result: &OutPoint{
   800  				Hash:  hashFromStr("2ebd15a7e758d5f4c7c74181b99e5b8586f88e0682dc13e09d92612a2b2bb0a2"),
   801  				Index: 1,
   802  			},
   803  			err: false,
   804  		},
   805  		{
   806  			name:  "normal outpoint 2",
   807  			input: "94c7762a68ff164352bd31fd95fa875204e811c09acef40ba781787eb28e3b55:42",
   808  			result: &OutPoint{
   809  				Hash:  hashFromStr("94c7762a68ff164352bd31fd95fa875204e811c09acef40ba781787eb28e3b55"),
   810  				Index: 42,
   811  			},
   812  			err: false,
   813  		},
   814  		{
   815  			name:  "big index outpoint",
   816  			input: "94c7762a68ff164352bd31fd95fa875204e811c09acef40ba781787eb28e3b55:2147484242",
   817  			result: &OutPoint{
   818  				Hash:  hashFromStr("94c7762a68ff164352bd31fd95fa875204e811c09acef40ba781787eb28e3b55"),
   819  				Index: 2147484242,
   820  			},
   821  			err: false,
   822  		},
   823  		{
   824  			name:   "bad string",
   825  			input:  "not_outpoint_not_outpoint_not_outpoint",
   826  			result: nil,
   827  			err:    true,
   828  		},
   829  		{
   830  			name:   "empty string",
   831  			input:  "",
   832  			result: nil,
   833  			err:    true,
   834  		},
   835  	}
   836  
   837  	for _, test := range tests {
   838  		t.Run(test.name, func(t *testing.T) {
   839  			outpoint, err := NewOutPointFromString(test.input)
   840  
   841  			isErr := (err != nil)
   842  			require.Equal(t, isErr, test.err)
   843  
   844  			if !isErr {
   845  				require.Equal(t, test.result, outpoint)
   846  			}
   847  		})
   848  
   849  	}
   850  }
   851  
   852  // multiTx is a MsgTx with an input and output and used in various tests.
   853  var multiTx = &MsgTx{
   854  	Version: 1,
   855  	TxIn: []*TxIn{
   856  		{
   857  			PreviousOutPoint: OutPoint{
   858  				Hash:  chainhash.Hash{},
   859  				Index: 0xffffffff,
   860  			},
   861  			SignatureScript: []byte{
   862  				0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62,
   863  			},
   864  			Sequence: 0xffffffff,
   865  		},
   866  	},
   867  	TxOut: []*TxOut{
   868  		{
   869  			Value: 0x12a05f200,
   870  			PkScript: []byte{
   871  				0x41, // OP_DATA_65
   872  				0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
   873  				0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
   874  				0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
   875  				0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
   876  				0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
   877  				0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
   878  				0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
   879  				0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
   880  				0xa6, // 65-byte signature
   881  				0xac, // OP_CHECKSIG
   882  			},
   883  		},
   884  		{
   885  			Value: 0x5f5e100,
   886  			PkScript: []byte{
   887  				0x41, // OP_DATA_65
   888  				0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
   889  				0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
   890  				0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
   891  				0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
   892  				0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
   893  				0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
   894  				0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
   895  				0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
   896  				0xa6, // 65-byte signature
   897  				0xac, // OP_CHECKSIG
   898  			},
   899  		},
   900  	},
   901  	LockTime: 0,
   902  }
   903  
   904  // multiTxEncoded is the wire encoded bytes for multiTx using protocol version
   905  // 60002 and is used in the various tests.
   906  var multiTxEncoded = []byte{
   907  	0x01, 0x00, 0x00, 0x00, // Version
   908  	0x01, // Varint for number of input transactions
   909  	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   910  	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   911  	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   912  	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
   913  	0xff, 0xff, 0xff, 0xff, // Prevous output index
   914  	0x07,                                     // Varint for length of signature script
   915  	0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62, // Signature script
   916  	0xff, 0xff, 0xff, 0xff, // Sequence
   917  	0x02,                                           // Varint for number of output transactions
   918  	0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount
   919  	0x43, // Varint for length of pk script
   920  	0x41, // OP_DATA_65
   921  	0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
   922  	0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
   923  	0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
   924  	0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
   925  	0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
   926  	0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
   927  	0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
   928  	0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
   929  	0xa6,                                           // 65-byte signature
   930  	0xac,                                           // OP_CHECKSIG
   931  	0x00, 0xe1, 0xf5, 0x05, 0x00, 0x00, 0x00, 0x00, // Transaction amount
   932  	0x43, // Varint for length of pk script
   933  	0x41, // OP_DATA_65
   934  	0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
   935  	0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
   936  	0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
   937  	0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
   938  	0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
   939  	0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
   940  	0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
   941  	0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
   942  	0xa6,                   // 65-byte signature
   943  	0xac,                   // OP_CHECKSIG
   944  	0x00, 0x00, 0x00, 0x00, // Lock time
   945  }
   946  
   947  // multiTxPkScriptLocs is the location information for the public key scripts
   948  // located in multiTx.
   949  var multiTxPkScriptLocs = []int{63, 139}
   950  
   951  // multiWitnessTx is a MsgTx with an input with witness data, and an
   952  // output used in various tests.
   953  var multiWitnessTx = &MsgTx{
   954  	Version: 1,
   955  	TxIn: []*TxIn{
   956  		{
   957  			PreviousOutPoint: OutPoint{
   958  				Hash: chainhash.Hash{
   959  					0xa5, 0x33, 0x52, 0xd5, 0x13, 0x57, 0x66, 0xf0,
   960  					0x30, 0x76, 0x59, 0x74, 0x18, 0x26, 0x3d, 0xa2,
   961  					0xd9, 0xc9, 0x58, 0x31, 0x59, 0x68, 0xfe, 0xa8,
   962  					0x23, 0x52, 0x94, 0x67, 0x48, 0x1f, 0xf9, 0xcd,
   963  				},
   964  				Index: 19,
   965  			},
   966  			SignatureScript: []byte{},
   967  			Witness: [][]byte{
   968  				{ // 70-byte signature
   969  					0x30, 0x43, 0x02, 0x1f, 0x4d, 0x23, 0x81, 0xdc,
   970  					0x97, 0xf1, 0x82, 0xab, 0xd8, 0x18, 0x5f, 0x51,
   971  					0x75, 0x30, 0x18, 0x52, 0x32, 0x12, 0xf5, 0xdd,
   972  					0xc0, 0x7c, 0xc4, 0xe6, 0x3a, 0x8d, 0xc0, 0x36,
   973  					0x58, 0xda, 0x19, 0x02, 0x20, 0x60, 0x8b, 0x5c,
   974  					0x4d, 0x92, 0xb8, 0x6b, 0x6d, 0xe7, 0xd7, 0x8e,
   975  					0xf2, 0x3a, 0x2f, 0xa7, 0x35, 0xbc, 0xb5, 0x9b,
   976  					0x91, 0x4a, 0x48, 0xb0, 0xe1, 0x87, 0xc5, 0xe7,
   977  					0x56, 0x9a, 0x18, 0x19, 0x70, 0x01,
   978  				},
   979  				{ // 33-byte serialize pub key
   980  					0x03, 0x07, 0xea, 0xd0, 0x84, 0x80, 0x7e, 0xb7,
   981  					0x63, 0x46, 0xdf, 0x69, 0x77, 0x00, 0x0c, 0x89,
   982  					0x39, 0x2f, 0x45, 0xc7, 0x64, 0x25, 0xb2, 0x61,
   983  					0x81, 0xf5, 0x21, 0xd7, 0xf3, 0x70, 0x06, 0x6a,
   984  					0x8f,
   985  				},
   986  			},
   987  			Sequence: 0xffffffff,
   988  		},
   989  	},
   990  	TxOut: []*TxOut{
   991  		{
   992  			Value: 395019,
   993  			PkScript: []byte{ // p2wkh output
   994  				0x00, // Version 0 witness program
   995  				0x14, // OP_DATA_20
   996  				0x9d, 0xda, 0xc6, 0xf3, 0x9d, 0x51, 0xe0, 0x39,
   997  				0x8e, 0x53, 0x2a, 0x22, 0xc4, 0x1b, 0xa1, 0x89,
   998  				0x40, 0x6a, 0x85, 0x23, // 20-byte pub key hash
   999  			},
  1000  		},
  1001  	},
  1002  }
  1003  
  1004  // multiWitnessTxEncoded is the wire encoded bytes for multiWitnessTx including inputs
  1005  // with witness data using protocol version 70012 and is used in the various
  1006  // tests.
  1007  var multiWitnessTxEncoded = []byte{
  1008  	0x1, 0x0, 0x0, 0x0, // Version
  1009  	TxFlagMarker, // Marker byte indicating 0 inputs, or a segwit encoded tx
  1010  	WitnessFlag,  // Flag byte
  1011  	0x1,          // Varint for number of inputs
  1012  	0xa5, 0x33, 0x52, 0xd5, 0x13, 0x57, 0x66, 0xf0,
  1013  	0x30, 0x76, 0x59, 0x74, 0x18, 0x26, 0x3d, 0xa2,
  1014  	0xd9, 0xc9, 0x58, 0x31, 0x59, 0x68, 0xfe, 0xa8,
  1015  	0x23, 0x52, 0x94, 0x67, 0x48, 0x1f, 0xf9, 0xcd, // Previous output hash
  1016  	0x13, 0x0, 0x0, 0x0, // Little endian previous output index
  1017  	0x0,                    // No sig script (this is a witness input)
  1018  	0xff, 0xff, 0xff, 0xff, // Sequence
  1019  	0x1,                                    // Varint for number of outputs
  1020  	0xb, 0x7, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, // Output amount
  1021  	0x16, // Varint for length of pk script
  1022  	0x0,  // Version 0 witness program
  1023  	0x14, // OP_DATA_20
  1024  	0x9d, 0xda, 0xc6, 0xf3, 0x9d, 0x51, 0xe0, 0x39,
  1025  	0x8e, 0x53, 0x2a, 0x22, 0xc4, 0x1b, 0xa1, 0x89,
  1026  	0x40, 0x6a, 0x85, 0x23, // 20-byte pub key hash
  1027  	0x2,  // Two items on the witness stack
  1028  	0x46, // 70 byte stack item
  1029  	0x30, 0x43, 0x2, 0x1f, 0x4d, 0x23, 0x81, 0xdc,
  1030  	0x97, 0xf1, 0x82, 0xab, 0xd8, 0x18, 0x5f, 0x51,
  1031  	0x75, 0x30, 0x18, 0x52, 0x32, 0x12, 0xf5, 0xdd,
  1032  	0xc0, 0x7c, 0xc4, 0xe6, 0x3a, 0x8d, 0xc0, 0x36,
  1033  	0x58, 0xda, 0x19, 0x2, 0x20, 0x60, 0x8b, 0x5c,
  1034  	0x4d, 0x92, 0xb8, 0x6b, 0x6d, 0xe7, 0xd7, 0x8e,
  1035  	0xf2, 0x3a, 0x2f, 0xa7, 0x35, 0xbc, 0xb5, 0x9b,
  1036  	0x91, 0x4a, 0x48, 0xb0, 0xe1, 0x87, 0xc5, 0xe7,
  1037  	0x56, 0x9a, 0x18, 0x19, 0x70, 0x1,
  1038  	0x21, // 33 byte stack item
  1039  	0x3, 0x7, 0xea, 0xd0, 0x84, 0x80, 0x7e, 0xb7,
  1040  	0x63, 0x46, 0xdf, 0x69, 0x77, 0x0, 0xc, 0x89,
  1041  	0x39, 0x2f, 0x45, 0xc7, 0x64, 0x25, 0xb2, 0x61,
  1042  	0x81, 0xf5, 0x21, 0xd7, 0xf3, 0x70, 0x6, 0x6a,
  1043  	0x8f,
  1044  	0x0, 0x0, 0x0, 0x0, // Lock time
  1045  }
  1046  
  1047  // multiWitnessTxEncodedNonZeroFlag is an incorrect wire encoded bytes for
  1048  // multiWitnessTx including inputs with witness data. Instead of the flag byte
  1049  // being set to 0x01, the flag is 0x00, which should trigger a decoding error.
  1050  var multiWitnessTxEncodedNonZeroFlag = []byte{
  1051  	0x1, 0x0, 0x0, 0x0, // Version
  1052  	TxFlagMarker, // Marker byte indicating 0 inputs, or a segwit encoded tx
  1053  	0x0,          // Incorrect flag byte (should be 0x01)
  1054  	0x1,          // Varint for number of inputs
  1055  	0xa5, 0x33, 0x52, 0xd5, 0x13, 0x57, 0x66, 0xf0,
  1056  	0x30, 0x76, 0x59, 0x74, 0x18, 0x26, 0x3d, 0xa2,
  1057  	0xd9, 0xc9, 0x58, 0x31, 0x59, 0x68, 0xfe, 0xa8,
  1058  	0x23, 0x52, 0x94, 0x67, 0x48, 0x1f, 0xf9, 0xcd, // Previous output hash
  1059  	0x13, 0x0, 0x0, 0x0, // Little endian previous output index
  1060  	0x0,                    // No sig script (this is a witness input)
  1061  	0xff, 0xff, 0xff, 0xff, // Sequence
  1062  	0x1,                                    // Varint for number of outputs
  1063  	0xb, 0x7, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, // Output amount
  1064  	0x16, // Varint for length of pk script
  1065  	0x0,  // Version 0 witness program
  1066  	0x14, // OP_DATA_20
  1067  	0x9d, 0xda, 0xc6, 0xf3, 0x9d, 0x51, 0xe0, 0x39,
  1068  	0x8e, 0x53, 0x2a, 0x22, 0xc4, 0x1b, 0xa1, 0x89,
  1069  	0x40, 0x6a, 0x85, 0x23, // 20-byte pub key hash
  1070  	0x2,  // Two items on the witness stack
  1071  	0x46, // 70 byte stack item
  1072  	0x30, 0x43, 0x2, 0x1f, 0x4d, 0x23, 0x81, 0xdc,
  1073  	0x97, 0xf1, 0x82, 0xab, 0xd8, 0x18, 0x5f, 0x51,
  1074  	0x75, 0x30, 0x18, 0x52, 0x32, 0x12, 0xf5, 0xdd,
  1075  	0xc0, 0x7c, 0xc4, 0xe6, 0x3a, 0x8d, 0xc0, 0x36,
  1076  	0x58, 0xda, 0x19, 0x2, 0x20, 0x60, 0x8b, 0x5c,
  1077  	0x4d, 0x92, 0xb8, 0x6b, 0x6d, 0xe7, 0xd7, 0x8e,
  1078  	0xf2, 0x3a, 0x2f, 0xa7, 0x35, 0xbc, 0xb5, 0x9b,
  1079  	0x91, 0x4a, 0x48, 0xb0, 0xe1, 0x87, 0xc5, 0xe7,
  1080  	0x56, 0x9a, 0x18, 0x19, 0x70, 0x1,
  1081  	0x21, // 33 byte stack item
  1082  	0x3, 0x7, 0xea, 0xd0, 0x84, 0x80, 0x7e, 0xb7,
  1083  	0x63, 0x46, 0xdf, 0x69, 0x77, 0x0, 0xc, 0x89,
  1084  	0x39, 0x2f, 0x45, 0xc7, 0x64, 0x25, 0xb2, 0x61,
  1085  	0x81, 0xf5, 0x21, 0xd7, 0xf3, 0x70, 0x6, 0x6a,
  1086  	0x8f,
  1087  	0x0, 0x0, 0x0, 0x0, // Lock time
  1088  }
  1089  
  1090  // multiTxPkScriptLocs is the location information for the public key scripts
  1091  // located in multiWitnessTx.
  1092  var multiWitnessTxPkScriptLocs = []int{58}