bitcoinlib.scripts module

class bitcoinlib.scripts.Script(commands=None, message=None, script_types='', is_locking=True, keys=None, signatures=None, blueprint=None, tx_data=None, public_hash=b'', sigs_required=None, redeemscript=b'', hash_type=1)[source]

Bases: object

Create a Script object with specified parameters. Use parse() method to create a Script from raw hex

>>> s = Script([op.op_2, op.op_4, op.op_add])
>>> s
<Script([op.op_2, op.op_4, op.op_add])>
>>> s.blueprint
[82, 84, 147]
>>> s.evaluate()
True

Stack is empty now, because evaluate pops last item from stack and check if is non-zero >>> s.stack []

Parameters
  • commands (list) – List of script language commands

  • message (bytes) – Signed message to verify, normally a transaction hash. Used to validate script

  • script_types (list of str) – List of script_types as defined in SCRIPT_TYPES

  • is_locking (bool) – Is this a locking script (Output), otherwise unlocking (Input)

  • keys (list of Key) – Provide list of keys to create script

  • signatures (list of Signature) – Provide list of signatures to create script

  • blueprint (list of str) – Simplified version of script, normally generated by Script object

  • tx_data (dict) – Dictionary with extra information needed to verify script. Such as ‘redeemscript’ for multisignature scripts and ‘blockcount’ for time locked scripts

  • public_hash (bytes) – Public hash of key or redeemscript used to create scripts

  • sigs_required (int) – Nubmer of signatures required to create multisig script

  • redeemscript (bytes) – Provide redeemscript to create a new (multisig) script

  • hash_type (int) – Specific script hash type, default is SIGHASH_ALL

property blueprint
evaluate(message=None, tx_data=None)[source]

Evaluate script, run all commands and check if it is valid

>>> s = Script([op.op_2, op.op_4, op.op_add])
>>> s
<Script([op.op_2, op.op_4, op.op_add])>
>>> s.blueprint
[82, 84, 147]
>>> s.evaluate()
True
>>> lock_script = bytes.fromhex('76a914f9cc73824051cc82d64a716c836c54467a21e22c88ac')
>>> unlock_script = bytes.fromhex('483045022100ba2ec7c40257b3d22864c9558738eea4d8771ab97888368124e176fdd6d7cd8602200f47c8d0c437df1ea8f9819d344e05b9c93e38e88df1fc46abb6194506c50ce1012103e481f20561573cfd800e64efda61405917cb29e4bd20bed168c52b674937f535')
>>> s = Script.parse_bytes(unlock_script + lock_script)
>>> transaction_hash = bytes.fromhex('12824db63e7856d00ee5e109fd1c26ac8a6a015858c26f4b336274f6b52da1c3')
>>> s.evaluate(message=transaction_hash)
True
Parameters
  • message (bytes) – Signed message to verify, normally a transaction hash. Leave empty to use Script.message. If supplied Script.message will be ignored.

  • tx_data – Dictionary with extra information needed to verify script. Such as ‘redeemscript’ for multisignature scripts and ‘blockcount’ for time locked scripts. Leave emtpy to use Script.tx_data. If supplied Script.tx_data will be ignored

Return bool

Valid or not valid

classmethod parse(script, message=None, tx_data=None, strict=True, _level=0)[source]

Parse raw script and return Script object. Extracts script commands, keys, signatures and other data.

Wrapper for the parse_bytesio() method. Convert hexadecimal string or bytes script to BytesIO.

>>> Script.parse('76a914af8e14a2cecd715c363b3a72b55b59a31e2acac988ac')
<Script([op.op_dup, op.op_hash160, data-20, op.op_equalverify, op.op_checksig])>
Parameters
  • script (BytesIO, bytes, str) – Raw script to parse in bytes, BytesIO or hexadecimal string format

  • message (bytes) – Signed message to verify, normally a transaction hash

  • tx_data (dict) – Dictionary with extra information needed to verify script. Such as ‘redeemscript’ for multisignature scripts and ‘blockcount’ for time locked scripts

  • strict (bool) – Raise exception when script is malformed, incomplete or not understood. Default is True

  • _level (int) – Internal argument used to avoid recursive depth

Return Script

classmethod parse_bytes(script, message=None, tx_data=None, strict=True, _level=0)[source]

Parse raw script and return Script object. Extracts script commands, keys, signatures and other data.

Wrapper for the parse_bytesio() method. Convert bytes script to BytesIO.

Parameters
  • script (bytes) – Raw script to parse in bytes format

  • message (bytes) – Signed message to verify, normally a transaction hash

  • tx_data (dict) – Dictionary with extra information needed to verify script. Such as ‘redeemscript’ for multisignature scripts and ‘blockcount’ for time locked scripts

  • strict (bool) – Raise exception when script is malformed or incomplete

  • _level (int) – Internal argument used to avoid recursive depth

Return Script

classmethod parse_bytesio(script, message=None, tx_data=None, strict=True, _level=0)[source]

Parse raw script and return Script object. Extracts script commands, keys, signatures and other data.

Parameters
  • script (BytesIO) – Raw script to parse in bytes, BytesIO or hexadecimal string format

  • message (bytes) – Signed message to verify, normally a transaction hash

  • tx_data (dict) – Dictionary with extra information needed to verify script. Such as ‘redeemscript’ for multisignature scripts and ‘blockcount’ for time locked scripts

  • strict (bool) – Raise exception when script is malformed, incomplete or not understood. Default is True

  • _level (int) – Internal argument used to avoid recursive depth

Return Script

classmethod parse_hex(script, message=None, tx_data=None, strict=True, _level=0)[source]

Parse raw script and return Script object. Extracts script commands, keys, signatures and other data.

Wrapper for the parse_bytesio() method. Convert hexadecimal string script to BytesIO.

>>> Script.parse_hex('76a914af8e14a2cecd715c363b3a72b55b59a31e2acac988ac')
<Script([op.op_dup, op.op_hash160, data-20, op.op_equalverify, op.op_checksig])>
Parameters
  • script (str) – Raw script to parse in hexadecimal string format

  • message (bytes) – Signed message to verify, normally a transaction hash

  • tx_data (dict) – Dictionary with extra information needed to verify script. Such as ‘redeemscript’ for multisignature scripts and ‘blockcount’ for time locked scripts

  • strict (bool) – Raise exception when script is malformed, incomplete or not understood. Default is True

  • _level (int) – Internal argument used to avoid recursive depth

Return Script

property raw
serialize()[source]

Serialize script. Return all commands and data as bytes

>>> s = Script.parse_hex('76a914af8e14a2cecd715c363b3a72b55b59a31e2acac988ac')
>>> s.serialize().hex()
'76a914af8e14a2cecd715c363b3a72b55b59a31e2acac988ac'
Return bytes

serialize_list()[source]

Serialize script and return commands and data as list

>>> s = Script.parse_hex('76a9')
>>> s.serialize_list()
[b'v', b'\xa9']
Return list of bytes

exception bitcoinlib.scripts.ScriptError(msg='')[source]

Bases: Exception

Handle Key class Exceptions

class bitcoinlib.scripts.Stack(iterable=(), /)[source]

Bases: list

The Stack object is a child of the Python list object with extra operational (OP) methods. The operations as used in the Script language can be used to manipulate the stack / list.

For documentation of the op-methods you could check https://en.bitcoin.it/wiki/Script

as_ints()[source]

Return the Stack as list of integers

>>> st = Stack.from_ints([1, 2])
>>> st.as_ints()
[1, 2]
Return list of int

classmethod from_ints(list_ints)[source]

Create a Stack item with a list of integers.

>>> Stack.from_ints([1, 2])
[b'\x01', b'\x02']
Parameters

list_ints

Returns

is_arithmetic(items=1)[source]

Check if top stack item is or last stock are arithmetic and has no more then 4 bytes

Return bool

op_0notequal()[source]
op_1add()[source]
op_1sub()[source]
op_2drop()[source]
op_2dup()[source]
op_2over()[source]
op_2rot()[source]
op_2swap()[source]
op_3dup()[source]
op_abs()[source]
op_add()[source]
op_booland()[source]
op_boolor()[source]
op_checklocktimeverify(sequence, tx_locktime)[source]

Implements CHECKLOCKTIMEVERIFY opcode (CLTV) as defined in BIP65.

CLTV is an absolute timelock and is added to an output locking script. It locks an output until a certain time or block.

Parameters
  • sequence (int) – Sequence value from the transaction. Must be 0xffffffff to be valid

  • tx_locktime (int) – The nLocktime value from the transaction in blocks or as Median Time Past timestamp

Return bool

op_checkmultisig(message, data=None)[source]
op_checkmultisigverify(message, data=None)[source]
op_checksequenceverify(sequence, version)[source]

Implements CHECKSEQUENCEVERIFY opcode (CSV) as defined in BIP112

CSV is a relative timelock and is added to an output locking script. It locks an output for a certain number of blocks or time.

Parameters
  • sequence (int) – Sequence value from the transaction

  • version (int) – Transaction verion. Must be 2 or higher

Return bool

op_checksig(message, _=None)[source]
op_checksigverify(message, _=None)[source]
op_depth()[source]
op_drop()[source]
op_dup()[source]
op_equal()[source]
op_equalverify()[source]
op_hash160()[source]
op_hash256()[source]
op_if(commands)[source]
op_ifdup()[source]
op_max()[source]
op_min()[source]
op_negate()[source]
op_nip()[source]
op_nop()[source]
op_nop1()[source]
op_nop10()[source]
op_nop4()[source]
op_nop5()[source]
op_nop6()[source]
op_nop7()[source]
op_nop8()[source]
op_nop9()[source]
op_not()[source]
op_notif(commands)[source]
op_numequal()[source]
op_numequalverify()[source]
op_numgreaterthan()[source]
op_numgreaterthanorequal()[source]
op_numlessthan()[source]
op_numlessthanorequal()[source]
op_numnotequal()[source]
op_over()[source]
op_pick()[source]
static op_return()[source]
op_ripemd160()[source]
op_roll()[source]
op_rot()[source]
op_sha1()[source]
op_sha256()[source]
op_size()[source]
op_sub()[source]
op_swap()[source]
op_tuck()[source]
op_verify()[source]
op_within()[source]
pop_as_number()[source]

Pop the latest item from the list and decode as number

>>> st = Stack.from_ints([1, 2])
>>> st.pop_as_number()
2
Return int

bitcoinlib.scripts.data_pack(data)[source]

Add data length prefix to data string to include data in a script

Parameters

data (bytes) – Data to be packed

Return bytes

bitcoinlib.scripts.decode_num(encoded)[source]

Decode byte representation of number used in Script language to integer.

>>> decode_num(b'')
0
>>> decode_num(b'@B\x0f')
1000000
Parameters

encoded (bytes) – Number as bytes

Return int

bitcoinlib.scripts.encode_num(num)[source]

Encode number as byte used in Script language. Bitcoin specific little endian format with sign for negative integers.

>>> encode_num(0)
b''
>>> encode_num(1)
b'\x01'
>>> encode_num(1000)
b'\xe8\x03'
>>> encode_num(1000000)
b'@B\x0f'
Parameters

num (int) – number to represent

Return bytes

bitcoinlib.scripts.get_data_type(data)[source]

Get type of data in script. Recognises signatures, keys, hashes or sequence data. Return ‘other’ if data is not recognised.

Parameters

data (bytes) – Data part of script

Return str