Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wire: add method TxID to MsgTx #2055

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions wire/msgtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@ func (msg *MsgTx) TxHash() chainhash.Hash {
return chainhash.DoubleHashRaw(msg.SerializeNoWitness)
}

// TxID generates the transaction ID of the transaction.
func (msg *MsgTx) TxID() string {
return msg.TxHash().String()
}

// WitnessHash generates the hash of the transaction serialized according to
// the new witness serialization defined in BIP0141 and BIP0144. The final
// output is used within the Segregated Witness commitment of all the witnesses
Expand Down
28 changes: 28 additions & 0 deletions wire/msgtx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,34 @@ func TestTxSerializeSizeStripped(t *testing.T) {
}
}

// TestTxID performs tests to ensure the serialize size for various transactions
// is accurate.
func TestTxID(t *testing.T) {
// Empty tx message.
noTx := NewMsgTx(1)
noTx.Version = 1

tests := []struct {
in *MsgTx // Tx to encode.
txid string // Expected transaction ID.
}{
// No inputs or outputs.
{noTx, "d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43"},

// Transaction with an input and an output.
{multiTx, "0100d15a522ff38de05c164ca0a56379a1b77dd1e4805a6534dc9b3d88290e9d"},

// Transaction with an input which includes witness data, and
// one output.
{multiWitnessTx, "0f167d1385a84d1518cfee208b653fc9163b605ccf1b75347e2850b3e2eb19f3"},
}

for i, test := range tests {
txid := test.in.TxID()
require.Equal(t, test.txid, txid, "test #%d", i)
}
}

// TestTxWitnessSize performs tests to ensure that the serialized size for
// various types of transactions that include witness data is accurate.
func TestTxWitnessSize(t *testing.T) {
Expand Down
Loading