Skip to content

Commit

Permalink
Add method to print Script object in human-readable string
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryp Toon committed Apr 15, 2024
1 parent b85cc50 commit 48aabd8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
62 changes: 60 additions & 2 deletions bitcoinlib/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,46 @@ def parse_bytes(cls, script, message=None, env_data=None, strict=True, _level=0)
data_length = len(script)
return cls.parse_bytesio(BytesIO(script), message, env_data, data_length, strict, _level)

@classmethod
def parse_str(cls, script, message=None, env_data=None, strict=True, _level=0):
"""
Parse script in string format and return Script object.
Extracts script commands, keys, signatures and other data.
>>> s = Script.parse_str("1 98 OP_ADD 99 OP_EQUAL")
>>> s
data-1 data-1 OP_ADD data-1 OP_EQUAL
>>> s.evaluate()
True
:param script: Raw script to parse in bytes format
:type script: str
:param message: Signed message to verify, normally a transaction hash
:type message: bytes
:param env_data: Dictionary with extra information needed to verify script. Such as 'redeemscript' for multisignature scripts and 'blockcount' for time locked scripts
:type env_data: dict
:param strict: Raise exception when script is malformed or incomplete
:type strict: bool
:param _level: Internal argument used to avoid recursive depth
:type _level: int
:return Script:
"""
items = script.split(' ')
s_items = []
for item in items:
if item.isdigit():
ival = int(item)
if 0 < ival <= 16:
s_items.append(ival.to_bytes(1, 'big'))
else:
s_items.append(int_to_varbyteint(ival))
elif item.startswith('OP_'):
s_items.append(getattr(op, item.lower(), 'unknown-command-%s' % item))
else:
s_items.append(bytes.fromhex(item))
return cls(s_items, message, env_data, strict, _level)

def __repr__(self):
s_items = self.view(blueprint=True, as_list=True)
return '<Script([' + ', '.join(s_items) + '])>'
Expand Down Expand Up @@ -557,7 +597,21 @@ def serialize_list(self):
clist.append(bytes(cmd))
return clist

def view(self, blueprint=False, as_list=False, op_code_numbers=False):
def view(self, blueprint=False, as_list=False, op_code_numbers=False, show_1_byte_data_as_int=True):
"""
View script as string in human-readable format.
:param blueprint: Show blueprint only, without detailed data.
:type blueprint: bool
:param as_list: Show script as list
:type as_list: bool
:param op_code_numbers: Show opcodes as numbers instead of string.
:type op_code_numbers: bool
:param show_1_byte_data_as_int: Show 1 byte data objects as integers.
:type show_1_byte_data_as_int: bool
:return str:
"""
s_items = []
i = 0
for command in self.commands:
Expand All @@ -573,7 +627,11 @@ def view(self, blueprint=False, as_list=False, op_code_numbers=False):
else:
s_items.append('data-%d' % len(command))
else:
s_items.append(command.hex())
chex = command.hex()
if len(chex) == 2 and show_1_byte_data_as_int:
s_items.append(int(chex, 16))
else:
s_items.append(chex)
i += 1

return s_items if as_list else ' '.join(str(i) for i in s_items)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,19 @@ def test_script_view(self):
self.assertEqual(s.blueprint, s.view(blueprint=True, as_list=True, op_code_numbers=True))
self.assertEqual(str(s), s.view(blueprint=True))

def test_script_str(self):
script_str = "1 98 OP_ADD 99 OP_EQUAL"
s = Script.parse_str(script_str)
self.assertEqual(s.view(), script_str)
self.assertTrue(s.evaluate())
self.assertEqual(s.as_hex(), '0101016293016387')

script_str_2 = "OP_DUP OP_HASH160 af8e14a2cecd715c363b3a72b55b59a31e2acac9 OP_EQUALVERIFY OP_CHECKSIG"
s = Script.parse_str(script_str_2)
clist = [118, 169, b'\xaf\x8e\x14\xa2\xce\xcdq\\6;:r\xb5[Y\xa3\x1e*\xca\xc9', 136, 172]
self.assertListEqual(s.commands, clist)
self.assertEqual(s.view(), script_str_2)

class TestScriptMPInumbers(unittest.TestCase):

def test_encode_decode_numbers(self):
Expand Down

0 comments on commit 48aabd8

Please sign in to comment.