btclib.tx package

Submodules

btclib.tx.block_header module

BlockHeader dataclass.

Dataclass encapsulating version, previous block hash, merkle root, time, bits, and nonce.

class btclib.tx.block_header.BlockHeader(version: int = 1, previous_block_hash: Union[bytes, str] = b'', merkle_root_: Union[bytes, str] = b'', time: datetime.datetime = datetime.datetime(1970, 1, 1, 0, 0), bits: Union[bytes, str] = b'', nonce: int = 0, check_validity: bool = True)

Bases: object

assert_valid() None
assert_valid_pow() None

Assert whether the BlockHeader provides a valid proof-of-work.

bits: bytes
property difficulty: float

Return the BlockHeader difficulty.

Difficulty is the ratio of the genesis block target over the BlockHeader target.

It represents the average number of hash function evaluations required to satisfy the BlockHeader target, expressed as multiple of the genesis block difficulty used as unit.

The difficulty of the genesis block is 2^32 (4*2^30), i.e. 4 GigaHash function evaluations.

classmethod from_dict(dict_: Mapping[str, Any], check_validity: bool = True) BlockHeader
property hash: bytes

Return the reversed hash of the BlockHeader.

merkle_root: bytes
nonce: int
classmethod parse(data: Union[BytesIO, bytes, str], check_validity: bool = True) BlockHeader

Return a BlockHeader by parsing 80 bytes from binary data.

previous_block_hash: bytes
serialize(check_validity: bool = True) bytes

Return a BlockHeader binary serialization.

property target: bytes

Return the BlockHeader proof-of-work target.

The target aabbcc * 256^dd is represented in scientific notation by the 4 bytes bits 0xaabbccdd

time: datetime
to_dict(check_validity: bool = True) Dict[str, Union[int, float, str]]
version: int

btclib.tx.blocks module

Block dataclass.

Dataclass encapsulating BlockHeader and List[Tx].

class btclib.tx.blocks.Block(header: btclib.tx.block_header.BlockHeader, transactions: Union[Sequence[btclib.tx.tx.Tx], NoneType] = None, check_validity: bool = True)

Bases: object

assert_valid() None
assert_valid_merkle_root() None
classmethod from_dict(dict_: Mapping[str, Any], check_validity: bool = True) Block
has_segwit_tx() bool
header: BlockHeader
property height: Optional[int]

Return the height committed into a BIP34 coinbase script_sig.

Version 2 blocks commit block height into the coinbase script_sig.

https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki

Block 227,835 (2013-03-24 15:49:13 GMT) was the last version 1 block.

classmethod parse(data: Union[BytesIO, bytes, str], check_validity: bool = True) Block

Return a Block by parsing binary data.

serialize(include_witness: bool = True, check_validity: bool = True) bytes
property size: int
to_dict(check_validity: bool = True) Dict[str, Any]
transactions: List[Tx]
property vsize: int
property weight: int

btclib.tx.out_point module

OutPoint dataclass.

Dataclass encapsulating tx_id and vout.

class btclib.tx.out_point.OutPoint(tx_id: Union[bytes, str] = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', vout: int = 4294967295, check_validity: bool = True)

Bases: object

assert_valid() None
classmethod from_dict(dict_: Mapping[str, Any], check_validity: bool = True) OutPoint
property hash: int

Return the hash int for compatibility with COutPoint.

is_coinbase() bool
property n: int

Return the n int for compatibility with COutPoint.

classmethod parse(data: Union[BytesIO, bytes, str], check_validity: bool = True) OutPoint

Return an OutPoint from the first 36 bytes of the provided data.

serialize(check_validity: bool = True) bytes

Return the 36 bytes serialization of the OutPoint.

to_dict(check_validity: bool = True) Dict[str, Union[str, int]]
tx_id: bytes
vout: int

btclib.tx.tx module

Transaction (Tx) class.

Dataclass encapsulating version, lock_time, vin (List[TxIn]), and vout (List[TxOut]).

https://en.bitcoin.it/wiki/Transaction https://learnmeabitcoin.com/guide/coinbase-transaction https://bitcoin.stackexchange.com/questions/20721/what-is-the-format-of-the-coinbase-transaction

For TxIn.sequence and TX.lock_time see: https://developer.bitcoin.org/devguide/transactions.html https://medium.com/summa-technology/bitcoins-time-locks-27e0c362d7a1 https://bitcoin.stackexchange.com/questions/40764/is-my-understanding-of-locktime-correct https://en.bitcoin.it/wiki/Timelock

class btclib.tx.tx.Tx(version: int = 1, lock_time: int = 0, vin: Union[Sequence[btclib.tx.tx_in.TxIn], NoneType] = None, vout: Union[Sequence[btclib.tx.tx_out.TxOut], NoneType] = None, check_validity: bool = True)

Bases: object

assert_standard() None
assert_valid() None
classmethod from_dict(dict_: Mapping[str, Any], check_validity: bool = True) Tx
property hash: bytes

Return the transaction hash.

It differs from tx_id for witness transactions.

property id: bytes

Return the transaction id.

is_coinbase() bool
is_segwit() bool
lock_time: int
property nLockTime: int

Return the nLockTime int for compatibility with CTransaction.

property nVersion: int

Return the nVersion int for compatibility with CTransaction.

classmethod parse(data: Union[BytesIO, bytes, str], check_validity: bool = True) Tx

Return a Tx by parsing binary data.

serialize(include_witness: bool, check_validity: bool = True) bytes
property size: int

Return the transaction size.

to_dict(check_validity: bool = True) Dict[str, Union[str, int, List[Any]]]
version: int
vin: List[TxIn]
vout: List[TxOut]
property vsize: int

Return the virtual transaction size.

It differs from size for witness transactions.

property vwitness: List[Witness]
property weight: int

btclib.tx.tx_in module

Transaction Input (TxIn) dataclass.

Dataclass encapsulating prev_out, script_sig, sequence, and script_witness.

class btclib.tx.tx_in.TxIn(prev_out: btclib.tx.out_point.OutPoint = OutPoint(tx_id=b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', vout=4294967295), script_sig: Union[bytes, str] = b'', sequence: int = 0, script_witness: btclib.script.witness.Witness = Witness(stack=[]), check_validity: bool = True)

Bases: object

assert_valid() None
classmethod from_dict(dict_: Mapping[str, Any], check_validity: bool = True) TxIn
is_coinbase() bool
is_segwit() bool
property nSequence: int

Return the nSequence int for compatibility with CTxIn.

property outpoint: OutPoint

Return the outpoint OutPoint for compatibility with CTxIn.

classmethod parse(data: Union[BytesIO, bytes, str], check_validity: bool = True) TxIn
prev_out: OutPoint
property scriptSig: bytes

Return the scriptSig bytes for compatibility with CTxIn.

script_sig: bytes
script_witness: Witness
sequence: int
serialize(check_validity: bool = True) bytes
to_dict(check_validity: bool = True) Dict[str, Any]

btclib.tx.tx_out module

Transaction Output (TxOut) dataclass.

Dataclass encapsulating value and script_pub_key (and network to convert script_pub_key to and from address).

class btclib.tx.tx_out.TxOut(value: int, script_pub_key: Union[btclib.script.script_pub_key.ScriptPubKey, bytes, str], check_validity: bool = True)

Bases: object

assert_valid() None
classmethod from_address(value: int, address: Union[bytes, str]) TxOut
classmethod from_dict(dict_: Mapping[str, Any], check_validity: bool = True) TxOut
property nValue: int

Return the nValue int for compatibility with CTxOut.

classmethod parse(data: Union[BytesIO, bytes, str], check_validity: bool = True) TxOut
property scriptPubKey: bytes

Return the scriptPubKey bytes for compatibility with CTxOut.

script_pub_key: ScriptPubKey
serialize(check_validity: bool = True) bytes
to_dict(check_validity: bool = True) Dict[str, Any]
value: int

Module contents

btclib.tx submodule.