// Initialize a new transaction tx := btcwire.NewMsgTx(btcwire.TxVersion) // Add an input to the transaction prevOut := &btcwire.OutPoint{Hash: prevTxHash, Index: prevTxIndex} txIn := btcwire.NewTxIn(prevOut, nil, nil) tx.AddTxIn(txIn) // Add an output to the transaction txOut := btcwire.NewTxOut(value, scriptPubKey) tx.AddTxOut(txOut)
// Deserialize a transaction serializedTx := []byte{0x01, 0x00, 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x02, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0x32, 0x7b, 0x7a, 0xed, 0xad, 0xe5, 0x32, 0x59, 0xff, 0xc9, 0xb5, 0x92, 0x5f, 0x1c, 0x14, 0xe3, 0x41, 0x6d, 0xd5, 0xd3, 0x5f} tx := &btcwire.MsgTx{} err := tx.Deserialize(bytes.NewReader(serializedTx)) if err != nil { // Handle error }This example deserializes a Bitcoin transaction from a byte slice. The serialized transaction includes one input and two outputs.