zpywallet.transactions package

Submodules

zpywallet.transactions.decode module

exception InvalidTransactionError[source]

Bases: Exception

Exception raised for invalid Bitcoin transactions.

message

Explanation of the error.

Type:

str

hex_to_int(string)[source]
insert_address_in_outputs(fine_rawtx, network=<class 'zpywallet.network.BitcoinSegwitMainNet'>)[source]
parse_transaction(raw_transaction_hex, segwit=False)[source]

Parse a raw Bitcoin-like transaction in hexadecimal format.

Parameters:
  • raw_transaction_hex (str) – The raw transaction data in hexadecimal format.

  • segwit (bool, optional) – Whether the transaction follows SegWit rules. Defaults to False.

Returns:

A tuple containing the parsed transaction dictionary and the witness size.

Return type:

tuple

Raises:

InvalidTransactionError – If the transaction data is invalid or incomplete.

parse_transaction_simple(raw_transaction_hex)[source]

Convenience wrapper around parse_transaction that auto-detects the transaction type.

parse_varint_hex(data)[source]
transaction_size(raw_transaction_hex, segwit=False)[source]

Calculate the size of a Bitcoin-like transaction.

Parameters:
  • raw_transaction_hex (str) – The raw transaction data in hexadecimal format.

  • segwit (bool, optional) – Whether the transaction follows SegWit rules. Defaults to False.

Returns:

The size of the transaction in virtual bytes (vbytes) if SegWit, otherwise bytes.

Return type:

int

Raises:

InvalidTransactionError – If the transaction data is invalid or incomplete.

transaction_size_simple(raw_transaction_hex)[source]

Convenience wrapper around transaction_size that auto-detects the transaction type.

zpywallet.transactions.encode module

assemble_legacy_signature(bytes_1, bytes_2_inputs, bytes_3, bytes_4, network, b2i)[source]
assemble_segwit_payload(i, inputs, nsequence, outputs, nlocktime='00000000', sighash=1)[source]
create_signatures_legacy(bytes_1, bytes_2_inputs, bytes_3, bytes_4)[source]
create_signatures_segwit(bytes_1, bytes_2_inputs, bytes_3, bytes_4)[source]
create_transaction(inputs: ~typing.List[~zpywallet.utxo.UTXO], outputs: ~typing.List[~zpywallet.destination.Destination], rbf=True, network=<class 'zpywallet.network.BitcoinSegwitMainNet'>, full_nodes=None, **kwargs)[source]

Creates a signed Bitcoin transaction.

Usually you do not want to use this function, because it does not automatically calculate change or fees. Instead, use Wallet.create_transaction().

Parameters:
  • inputs (List[UTXO]) – A list of Unspent Transaction Output objects.

  • outputs (List[Destination]) – A list of Destination objects representing the transaction outputs.

  • rbf (bool, optional) – Whether to enable Replace-By-Fee. Defaults to True. Only for Bitcoin-like blockchains. <Deprecated>

  • network (CryptoNetwork, optional) – The network to use. Defaults to BitcoinSegwitMainNet.

  • full_nodes (list, optional) – List of Web3 nodes to connect for signing. Only for EVM blockchains.

  • gas (int, optional) – Specifies the gas in Gwei. Only for EVM blockchains.

Returns:

The signed transaction data in bytes.

Return type:

bytes

Raises:

ValueError – If there’s an issue with the transaction or network configuration.

create_varint(value)[source]
create_web3_transaction(a_from, a_to, amount, private_key, fullnodes, gas, chain_id, nonce=None, data=None, gas_price=None, max_fee_per_gas=None, max_priority_fee_per_gas=None)[source]
int_to_hex(i, min_bytes=1)[source]
script_is_p2pkh(script)[source]
script_is_p2sh(script)[source]
script_is_p2wpkh(script)[source]
script_is_p2wsh(script)[source]

zpywallet.transactions.script module

class Script(script, network=<class 'zpywallet.network.BitcoinSegwitMainNet'>)[source]

Bases: object

Represents a script in Bitcoin. Can also represent most scripts in other altcoins

like Litecoin, Dogecoin, and Bitcoin Cash.

A Script contains just a list of OP_CODES and also knows how to serialize into bytes.

script

The list with all the script OP_CODES and data.

Type:

list

Raises:

ValueError – If string data is too large or integer is negative.

classmethod copy(script)[source]
static from_raw(scriptraw, has_segwit=False, network=<class 'zpywallet.network.BitcoinSegwitMainNet'>)[source]

Imports a Script commands list from raw hexadecimal data

Parameters:
  • txinputraw – string (hex) The hexadecimal raw string representing the Script commands

  • has_segwit – boolean Is the Tx Input segwit or not

get_script()[source]

Returns script as array of strings

is_p2pkh()[source]

Checks whether the transaction is P2PKH

is_p2sh()[source]

Checks whether the transaction is P2SH

is_p2tr()[source]

Checks whether the transaction is P2TR

is_p2wpkh()[source]

Checks whether the transaction is P2WPKH

is_p2wsh()[source]

Checks whether the transaction is P2WSH

to_bytes()[source]

Returns a serialized byte version of the script

If an OP code the appropriate byte is included according to: https://en.bitcoin.it/wiki/Script If not consider it data (signature, public key, public key hash, etc.) and and include with appropriate OP_PUSHDATA OP code plus length

to_hex()[source]

Converts the script to hexadecimal

to_p2pkh()[source]

Creates the P2PKH address from the script.

to_p2sh()[source]

Creates the P2PKH address from the script.

to_p2sh_script_pub_key()[source]

Converts script to p2sh scriptPubKey (locking script)

Calculates the hash160 (via the address) of the script and uses it to construct a P2SH script.

to_p2tr()[source]

Creates the P2TR address from the script.

to_p2wpkh()[source]

Creates the P2WPKH address from the script.

to_p2wsh()[source]

Creates the P2WSH address from the script.

to_p2wsh_script_pub_key()[source]

Converts script to p2wsh scriptPubKey (locking script)

Calculates the sha256 of the script and uses it to construct a P2WSH script.

hash160(data)[source]
p2sh_address(redeem_script, network=<class 'zpywallet.network.BitcoinSegwitMainNet'>)[source]
parse_varint(data)[source]
to_bytes(string, unhex=True)[source]

Module contents