bitcoinlib.transactions module

class bitcoinlib.transactions.Input(prev_txid, output_n, keys=None, signatures=None, public_hash=b'', unlocking_script=b'', unlocking_script_unsigned=None, script=None, script_type=None, address='', sequence=4294967295, compressed=None, sigs_required=None, sort=False, index_n=0, value=0, double_spend=False, locktime_cltv=None, locktime_csv=None, key_path='', witness_type=None, witnesses=None, encoding=None, strict=True, network='bitcoin')[source]

Bases: object

Transaction Input class, used by Transaction class

An Input contains a reference to an UTXO or Unspent Transaction Output (prev_txid + output_n). To spend the UTXO an unlocking script can be included to prove ownership.

Inputs are verified by the Transaction class.

Create a new transaction input

Parameters
  • prev_txid (bytes, str) – Transaction hash of the UTXO (previous output) which will be spent.

  • output_n (bytes, int) – Output number in previous transaction.

  • keys (list (bytes, str, Key)) – A list of Key objects or public / private key string in various formats. If no list is provided but a bytes or string variable, a list with one item will be created. Optional

  • signatures (list (bytes, str, Signature)) – Specify optional signatures

  • public_hash (bytes) – Public key hash or script hash. Specify if key is not available

  • unlocking_script (bytes, hexstring) – Unlocking script (scriptSig) to prove ownership. Optional

  • unlocking_script_unsigned (bytes, hexstring) – Unlocking script for signing transaction

  • script_type (str) – Type of unlocking script used, i.e. p2pkh or p2sh_multisig. Default is p2pkh

  • address (str, Address) – Address string or object for input

  • sequence (bytes, int) – Sequence part of input, you normally do not have to touch this

  • compressed (bool) – Use compressed or uncompressed public keys. Default is compressed

  • sigs_required (int) – Number of signatures required for a p2sh_multisig unlocking script

  • sort (boolean) – Sort public keys according to BIP0045 standard. Default is False to avoid unexpected change of key order.

  • index_n (int) – Index of input in transaction. Used by Transaction class.

  • value (int, Value, str) – Value of input in the smallest denominator integers (Satoshi’s) or as Value object or string

  • double_spend (bool) – Is this input also spend in another transaction

  • locktime_cltv (int) – Check Lock Time Verify value. Script level absolute time lock for this input

  • locktime_csv (int) – Check Sequence Verify value

  • key_path (str, list) – Key path of input key as BIP32 string or list

  • witness_type (str) – Specify witness/signature position: ‘segwit’ or ‘legacy’. Determine from script, address or encoding if not specified.

  • witnesses (list of bytes, list of str, bytes) – List of witnesses for inputs, used for segwit transactions for instance. Argument can be list of bytes or string or a single bytes string with concatenated witnesses as found in a raw transaction.

  • encoding (str) – Address encoding used. For example bech32/base32 or base58. Leave empty for default

  • strict (bool) – Raise exception when input is malformed, incomplete or not understood

  • network (str, Network) – Network, leave empty for default

as_dict()[source]

Get transaction input information in json format

Return dict

Json with output_n, prev_txid, output_n, type, address, public_key, public_hash, unlocking_script and sequence

classmethod parse(raw, witness_type='segwit', index_n=0, strict=True, network='bitcoin')[source]

Parse raw BytesIO string and return Input object

Parameters
  • raw (BytesIO) – Input

  • witness_type (str) – Specify witness/signature position: ‘segwit’ or ‘legacy’. Derived from script if not specified.

  • index_n (int) – Index number of input

  • strict (bool) – Raise exception when input is malformed, incomplete or not understood

  • network (str, Network) – Network, leave empty for default

Return Input

update_scripts(hash_type=1)[source]

Method to update Input scripts.

Creates or updates unlocking script, witness script for segwit inputs, multisig redeemscripts and locktime scripts. This method is called when initializing an Input class or when signing an input.

Parameters

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

Return bool

Always returns True when method is completed

verify(transaction_hash)[source]

Verify input with provided transaction hash, check if signatures matches public key.

Does not check if UTXO is valid or has already been spent

Parameters

transaction_hash (bytes) – Double SHA256 Hash of Transaction signature

Return bool

True if enough signatures provided and if all signatures are valid

class bitcoinlib.transactions.Output(value, address='', public_hash=b'', public_key=b'', lock_script=b'', spent=False, output_n=0, script_type=None, witver=0, encoding=None, spending_txid='', spending_index_n=None, strict=True, network='bitcoin')[source]

Bases: object

Transaction Output class, normally part of Transaction class.

Contains the amount and destination of a transaction.

Create a new transaction output

A transaction outputs locks the specified amount to a public key. Anyone with the private key can unlock this output.

The transaction output class contains an amount and the destination which can be provided either as address, public key, public key hash or a locking script. Only one needs to be provided as they all can be derived from each other, but you can provide as many attributes as you know to improve speed.

Parameters
  • value (int, Value, str) – Amount of output in the smallest denominator integers (Satoshi’s) or as Value object or string

  • address (str, Address, HDKey) – Destination address of output. Leave empty to derive from other attributes you provide. An instance of an Address or HDKey class is allowed as argument.

  • public_hash (bytes, str) – Hash of public key or script

  • public_key (bytes, str) – Destination public key

  • lock_script (bytes, str) – Locking script of output. If not provided a default unlocking script will be provided with a public key hash.

  • spent (bool) – Is output already spent? Default is False

  • output_n (int) – Output index number, default is 0. Index number has to be unique per transaction and 0 for first output, 1 for second, etc

  • script_type (str) – Script type of output (p2pkh, p2sh, segwit p2wpkh, etc). Extracted from lock_script if provided.

  • witver (int) – Witness version

  • encoding (str) – Address encoding used. For example bech32/base32 or base58. Leave empty to derive from address or default base58 encoding

  • spending_txid (str) – Transaction hash of input spending this transaction output

  • spending_index_n (int) – Index number of input spending this transaction output

  • strict (bool) – Raise exception when output is malformed, incomplete or not understood

  • network (str, Network) – Network, leave empty for default

property address
property address_obj

Get address object property. Create standard address object if not defined already.

Return Address

as_dict()[source]

Get transaction output information in json format

Return dict

Json with amount, locking script, public key, public key hash and address

classmethod parse(raw, output_n=0, strict=True, network='bitcoin')[source]

Parse raw BytesIO string and return Output object

Parameters
  • raw (BytesIO) – raw output stream

  • output_n (int) – Output number of Transaction output

  • strict (bool) – Raise exception when output is malformed, incomplete or not understood

  • network (str, Network) – Network, leave empty for default network

Return Output

set_locktime_relative(locktime)[source]

Relative timelocks with CHECKSEQUENCEVERIFY (CSV) as defined in BIP112 :param locktime: :return:

set_locktime_relative_blocks(blocks)[source]

Set nSequence relative locktime for this transaction input. The transaction will only be valid if the specified number of blocks has been mined since the previous UTXO is confirmed.

Maximum number of blocks is 65535 as defined in BIP-0068, which is around 455 days.

When setting a relative timelock, the transaction version must be at least 2. The transaction will be updated so existing signatures for this input will be removed.

Parameters

blocks (int) – The blocks value is the number of blocks since the previous transaction output has been confirmed.

Return None

set_locktime_relative_time(seconds)[source]

Set nSequence relative locktime for this transaction input. The transaction will only be valid if the specified amount of seconds have been passed since the previous UTXO is confirmed.

Number of seconds will be rounded to the nearest 512 seconds. Any value below 512 will be interpreted as 512 seconds.

Maximum number of seconds is 33553920 (512 * 65535), which equals 384 days. See BIP-0068 definition.

When setting a relative timelock, the transaction version must be at least 2. The transaction will be updated so existing signatures for this input will be removed.

Parameters

seconds – Number of seconds since the related previous transaction output has been confirmed.

Returns

class bitcoinlib.transactions.Transaction(inputs=None, outputs=None, locktime=0, version=None, network='bitcoin', fee=None, fee_per_kb=None, size=None, txid='', txhash='', date=None, confirmations=None, block_height=None, block_hash=None, input_total=0, output_total=0, rawtx=b'', status='new', coinbase=False, verified=False, witness_type='legacy', flag=None)[source]

Bases: object

Transaction Class

Contains 1 or more Input class object with UTXO’s to spent and 1 or more Output class objects with destinations. Besides the transaction class contains a locktime and version.

Inputs and outputs can be included when creating the transaction, or can be added later with add_input and add_output respectively.

A verify method is available to check if the transaction Inputs have valid unlocking scripts.

Each input in the transaction can be signed with the sign method provided a valid private key.

Create a new transaction class with provided inputs and outputs.

You can also create an empty transaction and add input and outputs later.

To verify and sign transactions all inputs and outputs need to be included in transaction. Any modification after signing makes the transaction invalid.

Parameters
  • inputs (list (Input)) – Array of Input objects. Leave empty to add later

  • outputs (list (Output)) – Array of Output object. Leave empty to add later

  • locktime (int) – Transaction level locktime. Locks the transaction until a specified block (value from 1 to 5 million) or until a certain time (Timestamp in seconds after 1-jan-1970). Default value is 0 for transactions without locktime

  • version (bytes, int) – Version rules. Defaults to 1 in bytes

  • network (str, Network) – Network, leave empty for default network

  • fee (int) – Fee in smallest denominator (ie Satoshi) for complete transaction

  • fee_per_kb (int) – Fee in smallest denominator per kilobyte. Specify when exact transaction size is not known.

  • size (int) – Transaction size in bytes

  • txid (str) – The transaction id (same for legacy/segwit) based on [nVersion][txins][txouts][nLockTime as hexadecimal string

  • txhash (str) – The transaction hash (differs from txid for witness transactions), based on [nVersion][marker][flag][txins][txouts][witness][nLockTime] in Segwit (as hexadecimal string). Unused at the moment

  • date (datetime) – Confirmation date of transaction

  • confirmations (int) – Number of confirmations

  • block_height (int) – Block number which includes transaction

  • block_hash (str) – Hash of block for this transaction

  • input_total (int) – Total value of inputs

  • output_total (int) – Total value of outputs

  • rawtx (bytes) – Bytes representation of complete transaction

  • status (str) – Transaction status, for example: ‘new’, ‘unconfirmed’, ‘confirmed’

  • coinbase (bool) – Coinbase transaction or not?

  • verified (bool) – Is transaction successfully verified? Updated when verified() method is called

  • witness_type (str) – Specify witness/signature position: ‘segwit’ or ‘legacy’. Determine from script, address or encoding if not specified.

  • flag (bytes, str) – Transaction flag to indicate version, for example for SegWit

add_input(prev_txid, output_n, keys=None, signatures=None, public_hash=b'', unlocking_script=b'', unlocking_script_unsigned=None, script_type=None, address='', sequence=4294967295, compressed=True, sigs_required=None, sort=False, index_n=None, value=None, double_spend=False, locktime_cltv=None, locktime_csv=None, key_path='', witness_type=None, witnesses=None, encoding=None, strict=True)[source]

Add input to this transaction

Wrapper for append method of Input class.

Parameters
  • prev_txid (bytes, hexstring) – Transaction hash of the UTXO (previous output) which will be spent.

  • output_n (bytes, int) – Output number in previous transaction.

  • keys (bytes, str) – Public keys can be provided to construct an Unlocking script. Optional

  • signatures (bytes, str) – Add signatures to input if already known

  • public_hash (bytes) – Specify public hash from key or redeemscript if key is not available

  • unlocking_script (bytes, hexstring) – Unlocking script (scriptSig) to prove ownership. Optional

  • unlocking_script_unsigned (bytes, str) – TODO: find better name…

  • script_type (str) – Type of unlocking script used, i.e. p2pkh or p2sh_multisig. Default is p2pkh

  • address (str, Address) – Specify address of input if known, default is to derive from key or scripts

  • sequence (int, bytes) – Sequence part of input, used for timelocked transactions

  • compressed (bool) – Use compressed or uncompressed public keys. Default is compressed

  • sigs_required – Number of signatures required for a p2sh_multisig unlocking script

  • sigs_required – int

  • sort (boolean) – Sort public keys according to BIP0045 standard. Default is False to avoid unexpected change of key order.

  • index_n (int) – Index number of position in transaction, leave empty to add input to end of inputs list

  • value (int) – Value of input

  • double_spend (bool) – True if double spend is detected, depends on which service provider is selected

  • locktime_cltv (int) – Check Lock Time Verify value. Script level absolute time lock for this input

  • locktime_csv (int) – Check Sequency Verify value.

  • key_path (str, list) – Key path of input key as BIP32 string or list

  • witness_type (str) – Specify witness/signature position: ‘segwit’ or ‘legacy’. Determine from script, address or encoding if not specified.

  • witnesses (list of bytes, list of str) – List of witnesses for inputs, used for segwit transactions for instance.

  • encoding (str) – Address encoding used. For example bech32/base32 or base58. Leave empty to derive from script or script type

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

Return int

Transaction index number (index_n)

add_output(value, address='', public_hash=b'', public_key=b'', lock_script=b'', spent=False, output_n=None, encoding=None, spending_txid=None, spending_index_n=None, strict=True)[source]

Add an output to this transaction

Wrapper for the append method of the Output class.

Parameters
  • value (int) – Value of output in the smallest denominator of currency, for example satoshi’s for bitcoins

  • address (str, Address) – Destination address of output. Leave empty to derive from other attributes you provide.

  • public_hash (bytes, str) – Hash of public key or script

  • public_key (bytes, str) – Destination public key

  • lock_script (bytes, str) – Locking script of output. If not provided a default unlocking script will be provided with a public key hash.

  • spent (bool, None) – Has output been spent in new transaction?

  • output_n (int) – Index number of output in transaction

  • encoding (str) – Address encoding used. For example bech32/base32 or base58. Leave empty for to derive from script or script type

  • spending_txid (str) – Transaction hash of input spending this transaction output

  • spending_index_n (int) – Index number of input spending this transaction output

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

Return int

Transaction output number (output_n)

as_dict()[source]

Return Json dictionary with transaction information: Inputs, outputs, version and locktime

Return dict

as_json()[source]

Get current key as json formatted string

Return str

calc_weight_units()[source]

Calculate weight units and vsize for this Transaction. Weight units are used to determine fee.

Return int

calculate_fee()[source]

Get fee for this transaction in the smallest denominator (i.e. Satoshi) based on its size and the transaction.fee_per_kb value

Return int

Estimated transaction fee

estimate_size(number_of_change_outputs=0)[source]

Get estimated vsize in for current transaction based on transaction type and number of inputs and outputs.

For old-style legacy transaction the vsize is the length of the transaction. In segwit transaction the witness data has less weight. The formula used is: math.ceil(((est_size-witness_size) * 3 + est_size) / 4)

Parameters

number_of_change_outputs (int) – Number of change outputs, default is 0

Return int

Estimated transaction size

static import_raw(rawtx, network='bitcoin', check_size=True)[source]

Import a raw transaction and create a Transaction object

Uses the transaction_deserialize method to parse the raw transaction and then calls the init method of this transaction class to create the transaction object

REPLACED BY THE PARSE() METHOD

Parameters
  • rawtx (bytes, str) – Raw transaction string

  • network (str, Network) – Network, leave empty for default

  • check_size (bool) – Check if no bytes are left when parsing is finished. Disable when parsing list of transactions, such as the transactions in a raw block. Default is True

Return Transaction

info()[source]

Prints transaction information to standard output

static load(txid=None, filename=None)[source]

Load transaction object from file which has been stored with the save() method.

Specify transaction ID or filename.

Parameters
  • txid (str) – Transaction ID. Transaction object will be read from .bitcoinlib datadir

  • filename (str) – Name of transaction object file

Return Transaction

merge_transaction(transaction)[source]

Merge this transaction with provided Transaction object.

Add all inputs and outputs of a transaction to this Transaction object. Because the transaction signature changes with this operation, the transaction inputs need to be signed again.

Can be used to implement CoinJoin. Where two or more unrelated Transactions are merged into 1 transaction to safe fees and increase privacy.

Parameters

transaction (Transaction) – The transaction to be merged

classmethod parse(rawtx, strict=True, network='bitcoin')[source]

Parse a raw transaction and create a Transaction object

Parameters
  • rawtx (BytesIO, bytes, str) – Raw transaction string

  • strict (bool) – Raise exception when transaction is malformed, incomplete or not understood

  • network (str, Network) – Network, leave empty for default network

Return Transaction

classmethod parse_bytes(rawtx, strict=True, network='bitcoin')[source]

Parse a raw bytes transaction and create a Transaction object. Wrapper for the parse_bytesio() method

Parameters
  • rawtx (bytes) – Raw transaction hexadecimal string

  • strict (bool) – Raise exception when transaction is malformed, incomplete or not understood

  • network (str, Network) – Network, leave empty for default network

Return Transaction

classmethod parse_bytesio(rawtx, strict=True, network='bitcoin')[source]

Parse a raw transaction and create a Transaction object

Parameters
  • rawtx (BytesIO) – Raw transaction string

  • strict (bool) – Raise exception when transaction is malformed, incomplete or not understood

  • network (str, Network) – Network, leave empty for default network

Return Transaction

classmethod parse_hex(rawtx, strict=True, network='bitcoin')[source]

Parse a raw hexadecimal transaction and create a Transaction object. Wrapper for the parse_bytesio() method

Parameters
  • rawtx (str) – Raw transaction hexadecimal string

  • strict (bool) – Raise exception when transaction is malformed, incomplete or not understood

  • network (str, Network) – Network, leave empty for default network

Return Transaction

raw(sign_id=None, hash_type=1, witness_type=None)[source]

Serialize raw transaction

Return transaction with signed inputs if signatures are available

Parameters
  • sign_id (int, None) – Create raw transaction which can be signed by transaction with this input ID

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

  • witness_type (str) – Serialize transaction with other witness type then default. Use to create legacy raw transaction for segwit transaction to create transaction signature ID’s

Return bytes

raw_hex(sign_id=None, hash_type=1, witness_type=None)[source]

Wrapper for raw() method. Return current raw transaction hex

Parameters
  • sign_id (int) – Create raw transaction which can be signed by transaction with this input ID

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

  • witness_type (str) – Serialize transaction with other witness type then default. Use to create legacy raw transaction for segwit transaction to create transaction signature ID’s

Return hexstring

save(filename=None)[source]

Store transaction object as file, so it can be imported in bitcoinlib later with the load() method.

Parameters

filename (str) – Location and name of file, leave empty to store transaction in bitcoinlib data directory: .bitcoinlib/<transaction_id.tx)

Returns

set_locktime_blocks(blocks)[source]

Set nLocktime, a transaction level absolute lock time in blocks using the transaction sequence field.

So for example if you set this value to 600000 the transaction will only be valid after block 600000.

Parameters

blocks (int) – Transaction is valid after supplied block number. Value must be between 0 and 500000000. Zero means no locktime.

Returns

set_locktime_relative_blocks(blocks, input_index_n=0)[source]

Set nSequence relative locktime for this transaction. The transaction will only be valid if the specified number of blocks has been mined since the previous UTXO is confirmed.

Maximum number of blocks is 65535 as defined in BIP-0068, which is around 455 days.

When setting a relative timelock, the transaction version must be at least 2. The transaction will be updated so existing signatures for this input will be removed.

Parameters
  • blocks (int) – The blocks value is the number of blocks since the previous transaction output has been confirmed.

  • input_index_n (int) – Index number of input for nSequence locktime

Returns

set_locktime_relative_time(seconds, input_index_n=0)[source]

Set nSequence relative locktime for this transaction. The transaction will only be valid if the specified amount of seconds have been passed since the previous UTXO is confirmed.

Number of seconds will be rounded to the nearest 512 seconds. Any value below 512 will be interpreted as 512 seconds.

Maximum number of seconds is 33553920 (512 * 65535), which equals 384 days. See BIP-0068 definition.

When setting a relative timelock, the transaction version must be at least 2. The transaction will be updated so existing signatures for this input will be removed.

Parameters
  • seconds (int) – Number of seconds since the related previous transaction output has been confirmed.

  • input_index_n (int) – Index number of input for nSequence locktime

Returns

set_locktime_time(timestamp)[source]

Set nLocktime, a transaction level absolute lock time in timestamp using the transaction sequence field.

Parameters

timestamp – Transaction is valid after the given timestamp. Value must be between 500000000 and 0xfffffffe

Returns

shuffle()[source]

Shuffle transaction inputs and outputs in random order.

Returns

shuffle_inputs()[source]

Shuffle transaction inputs in random order.

Returns

shuffle_outputs()[source]

Shuffle transaction outputs in random order.

Returns

sign(keys=None, index_n=None, multisig_key_n=None, hash_type=1, fail_on_unknown_key=True, replace_signatures=False)[source]

Sign the transaction input with provided private key

Parameters
  • keys (HDKey, Key, bytes, list) – A private key or list of private keys

  • index_n (int) – Index of transaction input. Leave empty to sign all inputs

  • multisig_key_n (int) – Index number of key for multisig input for segwit transactions. Leave empty if not known. If not specified all possibilities will be checked

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

  • fail_on_unknown_key (bool) – Method fails if public key from signature is not found in public key list

  • replace_signatures (bool) – Replace signature with new one if already signed.

Return None

sign_and_update(index_n=None)[source]

Update transaction ID and resign. Use if some properties of the transaction changed

Parameters

index_n (int) – Index of transaction input. Leave empty to sign all inputs

Returns

signature(sign_id=None, hash_type=1, witness_type=None)[source]

Serializes transaction and calculates signature for Legacy or Segwit transactions

Parameters
  • sign_id (int) – Index of input to sign

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

  • witness_type (str) – Legacy or Segwit witness type? Leave empty to use Transaction witness type

Return bytes

Transaction signature

signature_hash(sign_id=None, hash_type=1, witness_type=None, as_hex=False)[source]

Double SHA256 Hash of Transaction signature

Parameters
  • sign_id (int) – Index of input to sign

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

  • witness_type (str) – Legacy or Segwit witness type? Leave empty to use Transaction witness type

  • as_hex (bool) – Return value as hexadecimal string. Default is False

Return bytes

Transaction signature hash

signature_segwit(sign_id, hash_type=1)[source]

Serialize transaction signature for segregated witness transaction

Parameters
  • sign_id (int) – Index of input to sign

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

Return bytes

Segwit transaction signature

update_totals()[source]

Update input_total, output_total and fee according to inputs and outputs of this transaction

Return int

verify()[source]

Verify all inputs of a transaction, check if signatures match public key.

Does not check if UTXO is valid or has already been spent

Return bool

True if enough signatures provided and if all signatures are valid

property weight_units
witness_data()[source]

Get witness data for all inputs of this transaction

Return bytes

exception bitcoinlib.transactions.TransactionError(msg='')[source]

Bases: Exception

Handle Transaction class Exceptions

bitcoinlib.transactions.get_unlocking_script_type(locking_script_type, witness_type='legacy', multisig=False)[source]

Specify locking script type and get corresponding script type for unlocking script

>>> get_unlocking_script_type('p2wsh')
'p2sh_multisig'
Parameters
  • locking_script_type (str) – Locking script type. I.e.: p2pkh, p2sh, p2wpkh, p2wsh

  • witness_type (str) – Type of witness: legacy or segwit. Default is legacy

  • multisig (bool) – Is multisig script or not? Default is False

Return str

Unlocking script type such as sig_pubkey or p2sh_multisig

bitcoinlib.transactions.script_add_locktime_cltv(locktime_cltv, script)[source]
bitcoinlib.transactions.script_add_locktime_csv(locktime_csv, script)[source]
bitcoinlib.transactions.script_deserialize(script, script_types=None, locking_script=None, size_bytes_check=True)[source]

Deserialize a script: determine type, number of signatures and script data.

Parameters
  • script (str, bytes) – Raw script

  • script_types (list) – Limit script type determination to this list. Leave to default None to search in all script types.

  • locking_script (bool) – Only deserialize locking scripts. Specify False to only deserialize for unlocking scripts. Default is None for both

  • size_bytes_check (bool) – Check if script or signature starts with size bytes and remove size bytes before parsing. Default is True

Return list

With this items: [script_type, data, number_of_sigs_n, number_of_sigs_m]

bitcoinlib.transactions.script_to_string(script, name_data=False)[source]

Convert script to human-readable string format with OP-codes, signatures, keys, etc

Parameters
  • script (bytes, str) – A locking or unlocking script

  • name_data (bool) – Replace signatures and keys strings with name

Return str

bitcoinlib.transactions.serialize_multisig_redeemscript(key_list, n_required=None, compressed=True)[source]

Create a multisig redeemscript used in a p2sh.

Contains the number of signatures, followed by the list of public keys and the OP-code for the number of signatures required.

Parameters
  • key_list (Key, list) – List of public keys

  • n_required (int) – Number of required signatures

  • compressed (bool) – Use compressed public keys?

Return bytes

A multisig redeemscript

bitcoinlib.transactions.transaction_deserialize(rawtx, network='bitcoin', check_size=True)[source]

Deserialize a raw transaction

Returns a dictionary with list of input and output objects, locktime and version.

Will raise an error if wrong number of inputs are found or if there are no output found.

Parameters
  • rawtx (str, bytes) – Raw transaction as hexadecimal string or bytes

  • network (str, Network) – Network code, i.e. ‘bitcoin’, ‘testnet’, ‘litecoin’, etc. Leave emtpy for default network

  • check_size (bool) – Check if not bytes are left when parsing is finished. Disable when parsing list of transactions, such as the transactions in a raw block. Default is True

Return Transaction

bitcoinlib.transactions.transaction_update_spents(txs, address)[source]

Update spent information for list of transactions for a specific address. This method assumes the list of transaction complete and up-to-date.

This method loops through all the transaction and update all transaction outputs for given address, checks if the output is spent and add the spending transaction ID and index number to the outputs.

The same list of transactions with updates outputs will be returned

Parameters
  • txs (list of Transaction) – Complete list of transactions for given address

  • address (str) – Address string

Return list of Transaction