bitcoinlib.scripts module
- class bitcoinlib.scripts.Script(commands=None, message=None, script_types='', is_locking=True, keys=None, signatures=None, blueprint=None, env_data=None, public_hash=b'', sigs_required=None, redeemscript=b'', hash_type=1)[source]
Bases:
objectCreate 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_2, op_4, 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 []
>>> key1 = '5JruagvxNLXTnkksyLMfgFgf3CagJ3Ekxu5oGxpTm5mPfTAPez3' >>> key2 = '5JX3qAwDEEaapvLXRfbXRMSiyRgRSW9WjgxeyJQWwBugbudCwsk' >>> key3 = '5JjHVMwJdjPEPQhq34WMUhzLcEd4SD7HgZktEh8WHstWcCLRceV' >>> keylist = [Key(k) for k in [key1, key2, key3]] >>> redeemscript = Script(keys=keylist, sigs_required=2, script_types=['multisig'])
- 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
env_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, Script) – Provide redeemscript to create a new (multisig) script
hash_type (int) – Specific script hash type, default is SIGHASH_ALL
- property blueprint
- evaluate(message=None, env_data=None, trace=False)[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_2, op_4, 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.
env_data (dict()) – Dictionary with extra information needed to verify script. Such as ‘redeemscript’ for
trace (bool) – Write trace information to stdout
multisignature scripts and ‘blockcount’ for time locked scripts. Leave emtpy to use Script.data. If supplied Script.data will be ignored
- Return bool:
Valid or not valid
- classmethod parse(script, message=None, env_data=None, is_locking=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_dup, op_hash160, data-20, op_equalverify, 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
env_data (dict) – Dictionary with extra information needed to verify script. Such as ‘redeemscript’ for multisignature scripts and ‘blockcount’ for time locked scripts
is_locking – Is this a locking script or not, use None if not known and derive from script.
is_locking – bool, None
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, env_data=None, is_locking=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
env_data (dict) – Dictionary with extra information needed to verify script. Such as ‘redeemscript’ for multisignature scripts and ‘blockcount’ for time locked scripts
is_locking – Is this a locking script or not, use None if not known and derive from script.
is_locking – bool, None
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, env_data=None, data_length=0, is_locking=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
env_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
is_locking – Is this a locking script or not, use None if not known and derive from script.
is_locking – bool, None
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, env_data=None, is_locking=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_dup, op_hash160, data-20, op_equalverify, op_checksig])>
- Parameters:
script (str) – Raw script to parse in hexadecimal string format
message (bytes) – Signed message to verify, normally a transaction hash
env_data (dict) – Dictionary with extra information needed to verify script. Such as ‘redeemscript’ for multisignature scripts and ‘blockcount’ for time locked scripts
is_locking – Is this a locking script or not, use None if not known and derive from script.
is_locking – bool, None
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_str(script, message=None, env_data=None, is_locking=None, strict=True, _level=0)[source]
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 <Script([data-1, data-1, op_add, data-1, op_equal])> >>> s.evaluate() True
- Parameters:
script (str) – Raw script to parse in bytes format
message (bytes) – Signed message to verify, normally a transaction hash
env_data (dict) – Dictionary with extra information needed to verify script. Such as ‘redeemscript’ for multisignature scripts and ‘blockcount’ for time locked scripts
is_locking – Is this a locking script or not, use None if not known and derive from script.
is_locking – bool, None
strict (bool) – Raise exception when script is malformed or incomplete
_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:
- view(blueprint=False, as_list=False, op_code_numbers=False, show_1_byte_data_as_int=True)[source]
View script as string in human-readable format.
- Parameters:
blueprint (bool) – Show blueprint only, without detailed data.
as_list (bool) – Show script as list
op_code_numbers (bool) – Show opcodes as numbers instead of string.
show_1_byte_data_as_int (bool) – Show 1 byte data objects as integers.
- Return str:
- exception bitcoinlib.scripts.ScriptError(msg='')[source]
Bases:
ExceptionHandle Key class Exceptions
- class bitcoinlib.scripts.Stack(iterable=(), /)[source]
Bases:
listThe 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_add()[source]
Add the top 2 numbers of the stack and appends the result on the top of the stack.
Fails if the top 2 items are not arithmetic or if there are not enough items on the stack.
- Return bool:
Operation succeeded
- 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: