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, data_length=0, 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
data_length (int) – Length of script data if known. Supply if you can to increase efficiency and lower change of incorrect parsing
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
- 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 than 4 bytes
- Return bool:
- 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_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:
- 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 to decode
- 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: