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

witness flagbyte errors #250

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 21 additions & 3 deletions bitcoin/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,9 @@ def from_txwitness(cls, txwitness):

class CTransaction(ImmutableSerializable):
"""A transaction"""
__slots__ = ['nVersion', 'vin', 'vout', 'nLockTime', 'wit']
__slots__ = ['nVersion', 'vin', 'vout', 'nLockTime', 'wit', 'flagbyte']

def __init__(self, vin=(), vout=(), nLockTime=0, nVersion=1, witness=CTxWitness()):
def __init__(self, vin=(), vout=(), nLockTime=0, nVersion=1, witness=CTxWitness(), flagbyte=0):
"""Create a new transaction

vin and vout are iterables of transaction inputs and outputs
Expand All @@ -403,6 +403,7 @@ def __init__(self, vin=(), vout=(), nLockTime=0, nVersion=1, witness=CTxWitness(
object.__setattr__(self, 'vin', tuple(CTxIn.from_txin(txin) for txin in vin))
object.__setattr__(self, 'vout', tuple(CTxOut.from_txout(txout) for txout in vout))
object.__setattr__(self, 'wit', CTxWitness.from_txwitness(witness))
object.__setattr__(self, 'flagbyte', flagbyte)

@classmethod
def stream_deserialize(cls, f):
Expand All @@ -428,8 +429,19 @@ def stream_deserialize(cls, f):
wit = CTxWitness(tuple(0 for dummy in range(len(vin))))
wit = wit.stream_deserialize(f)
nLockTime = struct.unpack(b"<I", ser_read(f,4))[0]
return cls(vin, vout, nLockTime, nVersion, wit)
return cls(vin, vout, nLockTime, nVersion, wit, flagbyte)
else:
try:
vin = VectorSerializer.stream_deserialize(CTxIn, f)
if(len(vin)>0 and markerbyte ==0 and flagbyte!=1):
vout = VectorSerializer.stream_deserialize(CTxOut, f)
wit = CTxWitness(tuple(0 for dummy in range(len(vin))))
wit = wit.stream_deserialize(f)
nLockTime = struct.unpack(b"<I", ser_read(f,4))[0]
return cls(vin, vout, nLockTime, nVersion, wit, flagbyte)
except:
#failing here can mean we just need to deserialize a normal tx after setting the cursor in the right position
pass
f.seek(pos) # put marker byte back, since we don't have peek
vin = VectorSerializer.stream_deserialize(CTxIn, f)
vout = VectorSerializer.stream_deserialize(CTxOut, f)
Expand Down Expand Up @@ -825,6 +837,12 @@ def CheckTransaction(tx):
if txin.prevout.is_null():
raise CheckTransactionError("CheckTransaction() : prevout is null")

# Check for flagbyte segwit related errors:
if(tx.flagbyte!=0 and tx.flagbyte!=1):
raise CheckTransactionError("CheckTransaction() : Unknown optional flag")

if(tx.flagbyte==1 and tx.wit.is_null()):
raise CheckTransactionError("CheckTransaction() : Superfluous witnes records")



Expand Down
7 changes: 7 additions & 0 deletions bitcoin/tests/data/tx_invalid.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,12 @@
[[["ad503f72c18df5801ee64d76090afe4c607fb2b822e9b7b63c5826c50e22fc3b", 0, "0x21 0x027c3a97665bf283a102a587a62a30a0c102d4d3b141015e2cae6f64e2543113e5 CHECKSIG NOT"]],
"01000000013bfc220ec526583cb6b7e922b8b27f604cfe0a09764de61e80f58dc1723f50ad0000000000ffffffff0101000000000000002321027c3a97665bf283a102a587a62a30a0c102d4d3b141015e2cae6f64e2543113e5ac00000000", true],

["Witness with SigHash Single|AnyoneCanPay (same signature as previous), flagbyte set to 02"],
[[["0000000000000000000000000000000000000000000000000000000000000100", 0, "0x51", 1000],
["0000000000000000000000000000000000000000000000000000000000000100", 1, "0x00 0x14 0x4c9c3dfac4207d5d8cb89df5722cb3d712385e3f", 2000],
["0000000000000000000000000000000000000000000000000000000000000100", 2, "0x51", 3000]],
"0200000000020300010000000000000000000000000000000000000000000000000000000000000000000000ffffffff00010000000000000000000000000000000000000000000000000000000000000100000000ffffffff00010000000000000000000000000000000000000000000000000000000000000200000000ffffffff03e8030000000000000151d0070000000000000151b80b0000000000000151000248304502210092f4777a0f17bf5aeb8ae768dec5f2c14feabf9d1fe2c89c78dfed0f13fdb86902206da90a86042e252bcd1e80a168c719e4a1ddcc3cebea24b9812c5453c79107e9832103596d3451025c19dbbdeb932d6bf8bfb4ad499b95b6f88db8899efac102e5fc710000000000", "P2SH,WITNESS"],


["Make diffs cleaner by leaving a comment here without comma at the end"]
]
2 changes: 1 addition & 1 deletion bitcoin/tests/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def load_test_vectors(name):

prevouts = {}
for json_prevout in test_case[0]:
assert len(json_prevout) == 3
assert len(json_prevout) >= 3
n = json_prevout[1]
if n == -1:
n = 0xffffffff
Expand Down