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

fix: add forgotten check for coinbase input #3

Merged
merged 1 commit into from
Dec 11, 2019
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
20 changes: 20 additions & 0 deletions input.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rawtx

import (
"bytes"
"github.com/btcsuite/btcd/wire"
)

Expand Down Expand Up @@ -73,7 +74,12 @@ func (in *Input) FromWireTxIn(txIn *wire.TxIn) {
// GetType retruns the input type as a InputType
func (in *Input) GetType() InputType {
if in.inputType != 0 {
// return the cached input type
return in.inputType
}

if in.IsCoinbase() {
return InCOINBASE
} else if in.SpendsP2PKH() {
return InP2PKH
} else if in.SpendsP2SH() {
Expand Down Expand Up @@ -109,6 +115,20 @@ func (in *Input) SpendsNativeSegWit() bool {
return false
}

// IsCoinbase checks if an input is a coinbase input by checking the previous-
// output-index to be equal to 0xffffffff and then checking the previous-tx-hash
// to be all zero.
func (in *Input) IsCoinbase() bool {
// first do the inexpensive check if equal to 0xffffffff
if in.Outpoint.OutputIndex == 0xffffffff {
// only then check the more expensive equal for byte arrays
if bytes.Equal(in.Outpoint.PrevTxHash[:], make([]byte, 32)) {
return true
}
}
return false
}

// SpendsP2WSH checks if an input spends a P2WSH input.
// Since all native SegWit inputs that aren't P2WPKH are probably
// P2WSH this function just returns the complement for all native SegWit
Expand Down
3 changes: 2 additions & 1 deletion input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func TestInputGetType(t *testing.T) {
txm[tx7] = []InputType{InP2PKH}
txm[tx8] = []InputType{InP2WSH}
txm[tx9] = []InputType{InP2WPKH}
txm[tx10] = []InputType{InUNKNOWN}
txm[tx10] = []InputType{InCOINBASE}
txm[tx11] = []InputType{InP2SH, InP2SH, InP2SH, InP2SH, InP2SH, InP2SH, InP2SH}
txm[tx12] = []InputType{InP2PKH}
txm[tx13] = []InputType{InP2PKH}
Expand All @@ -427,6 +427,7 @@ func TestInputGetType(t *testing.T) {
txm[tx21] = []InputType{InP2WSH}
txm[tx22] = []InputType{InP2WSH}
txm[tx23] = []InputType{InP2PK}
txm[tx24] = []InputType{InCOINBASE}

for txString, expexted := range txm {
tx, err := StringToTx(txString)
Expand Down
4 changes: 4 additions & 0 deletions testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ const (
// Satoshi to Hal in Block 170
// inputs: 1 (P2PK), ouputs: 1 (P2PK)
tx23 string = "0100000001c997a5e56e104102fa209c6a852dd90660a20b2d9c352423edce25857fcd3704000000004847304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901ffffffff0200ca9a3b00000000434104ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84cac00286bee0000000043410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac00000000"

// Coinbase from Block 500342
// Used to test a bug fix that a coinbase was not recognized correctly
tx24 string = "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff5f0376a2071a2f5669614254432f4d696e6564206279206c7a6f7a313233342f2cfabe6d6da6d88f687bb14bcc0ebb42353875893e6e650aadd4f3c06645522a40c278ccb7010000000000000012d329f872eed68c722d5fafa8ed97dc000000ffffffff01807c814a000000001976a914536ffa992491508dca0354e52f32a3a7a679a53a88ac00000000"
)

// OP_RETURN tx
Expand Down