From 79f8e3d10e804aac9b0e87133a21f1a04e3f4145 Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 1 Dec 2020 10:55:44 -0500 Subject: [PATCH 1/3] add test case for tx that fails to parse, and code required to make it pass. --- tx.go | 6 +----- tx_test.go | 6 ++++++ varint.go | 4 +++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/tx.go b/tx.go index a8a35487..a2319835 100644 --- a/tx.go +++ b/tx.go @@ -90,11 +90,6 @@ func NewTxFromStream(b []byte) (*Tx, int, error) { } offset += 4 - // There is an optional Flag of 2 bytes after the version. It is always "0001" and represent that this is a SegWit tx. - if b[offset] == 0x00 && b[offset+1] == 0x01 { - offset += 2 - } - inputCount, size := DecodeVarInt(b[offset:]) offset += size @@ -116,6 +111,7 @@ func NewTxFromStream(b []byte) (*Tx, int, error) { var outputCount uint64 var output *Output outputCount, size = DecodeVarInt(b[offset:]) + offset += size for i = 0; i < outputCount; i++ { output, size, err = NewOutputFromBytes(b[offset:]) diff --git a/tx_test.go b/tx_test.go index 6b91487d..a1e431f2 100644 --- a/tx_test.go +++ b/tx_test.go @@ -30,6 +30,12 @@ func TestNewTx(t *testing.T) { func TestNewTxFromString(t *testing.T) { t.Parallel() + t.Run("valid tx no inputs", func(t *testing.T) { + tx, err := bt.NewTxFromString("01000000000100000000000000001a006a07707265666978310c6578616d706c65206461746102133700000000") + assert.NoError(t, err) + assert.NotNil(t, tx) + }) + t.Run("invalid tx", func(t *testing.T) { tx, err := bt.NewTxFromString("0") assert.Error(t, err) diff --git a/varint.go b/varint.go index 6f7f5f5c..8108a156 100644 --- a/varint.go +++ b/varint.go @@ -1,6 +1,8 @@ package bt -import "encoding/binary" +import ( + "encoding/binary" +) // VarInt takes an unsigned integer and returns a byte array in VarInt format. // See http://learnmeabitcoin.com/glossary/varint From 30443bf6229e9b6882e5d6006cdb24b1656be812 Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 1 Dec 2020 11:09:28 -0500 Subject: [PATCH 2/3] remove space --- tx.go | 1 - 1 file changed, 1 deletion(-) diff --git a/tx.go b/tx.go index a2319835..0e254dca 100644 --- a/tx.go +++ b/tx.go @@ -111,7 +111,6 @@ func NewTxFromStream(b []byte) (*Tx, int, error) { var outputCount uint64 var output *Output outputCount, size = DecodeVarInt(b[offset:]) - offset += size for i = 0; i < outputCount; i++ { output, size, err = NewOutputFromBytes(b[offset:]) From c2a912f431dd5bb58840a3f8536425f21f01ed08 Mon Sep 17 00:00:00 2001 From: Luke Date: Tue, 1 Dec 2020 11:26:49 -0500 Subject: [PATCH 3/3] single import --- varint.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/varint.go b/varint.go index 8108a156..6f7f5f5c 100644 --- a/varint.go +++ b/varint.go @@ -1,8 +1,6 @@ package bt -import ( - "encoding/binary" -) +import "encoding/binary" // VarInt takes an unsigned integer and returns a byte array in VarInt format. // See http://learnmeabitcoin.com/glossary/varint