follow-up prev: add testcase and minor formatting
(minor reshuffling of check so that it matches following line and is more clear it is a bounds check)
This commit is contained in:
@@ -597,7 +597,6 @@ class BCDataStream(object):
|
|||||||
|
|
||||||
|
|
||||||
def script_GetOp(_bytes : bytes):
|
def script_GetOp(_bytes : bytes):
|
||||||
_bytes_len = len(_bytes)
|
|
||||||
i = 0
|
i = 0
|
||||||
while i < len(_bytes):
|
while i < len(_bytes):
|
||||||
vch = None
|
vch = None
|
||||||
@@ -618,10 +617,9 @@ def script_GetOp(_bytes : bytes):
|
|||||||
try: (nSize,) = struct.unpack_from('<I', _bytes, i)
|
try: (nSize,) = struct.unpack_from('<I', _bytes, i)
|
||||||
except struct.error: raise MalformedBitcoinScript()
|
except struct.error: raise MalformedBitcoinScript()
|
||||||
i += 4
|
i += 4
|
||||||
|
if i + nSize > len(_bytes):
|
||||||
if nSize > _bytes_len - i:
|
raise MalformedBitcoinScript(
|
||||||
raise MalformedBitcoinScript(f"Push of data element that is larger than remaining data: {nSize} vs {_bytes_len - i}")
|
f"Push of data element that is larger than remaining data: {nSize} vs {len(_bytes) - i}")
|
||||||
|
|
||||||
vch = _bytes[i:i + nSize]
|
vch = _bytes[i:i + nSize]
|
||||||
i += nSize
|
i += nSize
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,8 @@ from electrum import transaction, bitcoin
|
|||||||
from electrum.transaction import (convert_raw_tx_to_hex, tx_from_any, Transaction,
|
from electrum.transaction import (convert_raw_tx_to_hex, tx_from_any, Transaction,
|
||||||
PartialTransaction, TxOutpoint, PartialTxInput,
|
PartialTransaction, TxOutpoint, PartialTxInput,
|
||||||
PartialTxOutput, Sighash, match_script_against_template,
|
PartialTxOutput, Sighash, match_script_against_template,
|
||||||
SCRIPTPUBKEY_TEMPLATE_ANYSEGWIT, TxOutput)
|
SCRIPTPUBKEY_TEMPLATE_ANYSEGWIT, TxOutput, script_GetOp,
|
||||||
|
MalformedBitcoinScript)
|
||||||
from electrum.util import bfh
|
from electrum.util import bfh
|
||||||
from electrum.bitcoin import (deserialize_privkey, opcodes,
|
from electrum.bitcoin import (deserialize_privkey, opcodes,
|
||||||
construct_script, construct_witness)
|
construct_script, construct_witness)
|
||||||
@@ -91,6 +92,16 @@ class TestTransaction(ElectrumTestCase):
|
|||||||
script = construct_script([opcodes.OP_0, bytes(50)])
|
script = construct_script([opcodes.OP_0, bytes(50)])
|
||||||
self.assertFalse(match_script_against_template(script, SCRIPTPUBKEY_TEMPLATE_ANYSEGWIT))
|
self.assertFalse(match_script_against_template(script, SCRIPTPUBKEY_TEMPLATE_ANYSEGWIT))
|
||||||
|
|
||||||
|
def test_script_GetOp(self):
|
||||||
|
# TODO add more test cases for script_GetOp
|
||||||
|
# cases from https://github.com/bitcoinj/bitcoinj/blob/09defa626648687f8bd6ea7d197818249eebd3c8/core/src/test/resources/org/bitcoinj/script/script_tests.json#L721-L723
|
||||||
|
with self.assertRaises(MalformedBitcoinScript):
|
||||||
|
[x for x in script_GetOp(bfh("4c01"))] # PUSHDATA1 with not enough bytes
|
||||||
|
with self.assertRaises(MalformedBitcoinScript):
|
||||||
|
[x for x in script_GetOp(bfh("4d0200ff"))] # PUSHDATA2 with not enough bytes
|
||||||
|
with self.assertRaises(MalformedBitcoinScript):
|
||||||
|
[x for x in script_GetOp(bfh("4e03000000ffff"))] # PUSHDATA4 with not enough bytes
|
||||||
|
|
||||||
def test_tx_update_signatures(self):
|
def test_tx_update_signatures(self):
|
||||||
tx = tx_from_any("cHNidP8BAFUBAAAAASpcmpT83pj1WBzQAWLGChOTbOt1OJ6mW/OGM7Qk60AxAAAAAAD/////AUBCDwAAAAAAGXapFCMKw3g0BzpCFG8R74QUrpKf6q/DiKwAAAAAAAAA")
|
tx = tx_from_any("cHNidP8BAFUBAAAAASpcmpT83pj1WBzQAWLGChOTbOt1OJ6mW/OGM7Qk60AxAAAAAAD/////AUBCDwAAAAAAGXapFCMKw3g0BzpCFG8R74QUrpKf6q/DiKwAAAAAAAAA")
|
||||||
pubkey = bfh('02e61d176da16edd1d258a200ad9759ef63adf8e14cd97f53227bae35cdb84d2f6')
|
pubkey = bfh('02e61d176da16edd1d258a200ad9759ef63adf8e14cd97f53227bae35cdb84d2f6')
|
||||||
|
|||||||
Reference in New Issue
Block a user