pyrxd.transaction — Transaction building

Transaction building, signing, and serialization for Radiant.

exception pyrxd.transaction.InsufficientFunds[source]

Bases: ValueError

class pyrxd.transaction.Transaction[source]

Bases: object

__init__(tx_inputs=None, tx_outputs=None, version=1, locktime=0, merkle_path=None, **kwargs)[source]
Parameters:
add_input(tx_input)[source]
Parameters:

tx_input (TransactionInput)

Return type:

Transaction

add_inputs(tx_inputs)[source]
Parameters:

tx_inputs (list[TransactionInput])

Return type:

Transaction

add_output(tx_output)[source]
Parameters:

tx_output (TransactionOutput)

Return type:

Transaction

add_outputs(tx_outputs)[source]
Parameters:

tx_outputs (list[TransactionOutput])

Return type:

Transaction

byte_length()[source]
Returns:

actual byte length of this transaction under the current state

Return type:

int

estimated_byte_length()[source]
Returns:

estimated byte length of this transaction after signing

Return type:

int

if transaction has already signed, it will return the same value as function byte_length

estimated_size()
Returns:

estimated byte length of this transaction after signing

Return type:

int

if transaction has already signed, it will return the same value as function byte_length

fee(model_or_fee=None, change_distribution='equal')[source]

Computes the fee for the transaction and adjusts the change outputs accordingly.

Parameters:
  • model_or_fee – Fee model or fee amount. Defaults to SatoshisPerKilobyte(TRANSACTION_FEE_RATE), i.e. Radiant’s effective minimum relay rate of 10_000_000 photons/kB. The old default of 5 photons/kB was a Bitcoin-shaped inheritance that produced transactions 2_000_000x under the floor AcceptToMemoryPool enforces; every one of them was unrelayable. Prefer pyrxd.fee_sizing for anything that must provably clear the floor at its FINAL serialised size — this method sizes against the estimate, not the signed bytes.

  • change_distribution – Method of change distribution (‘equal’ or ‘random’). Defaults to ‘equal’.

classmethod from_beef(stream)[source]
Parameters:

stream (str | bytes | Reader)

Return type:

Transaction

classmethod from_hex(stream)[source]

Deserialize a transaction.

Given a whole buffer (str/bytes) the parse must consume ALL of it. An input whose script-length varint over-claims does not run off the end of a transaction — it steals bytes from the fields that follow, and the field boundaries slide until the tail comes up short. The result parsed “successfully” into a different transaction: a 63-byte blob re-serialized to 56 bytes, so txid() returned the id of a transaction those bytes do not encode. Requiring the reader to finish at EOF is what makes from_hex -> serialize an identity instead of a rewrite.

Given a Reader, the caller is streaming (from_beef reads a sequence of transactions from one buffer) and owns the remaining bytes, so no EOF check applies.

Parameters:

stream (str | bytes | Reader)

Return type:

Transaction | None

classmethod from_reader(reader)[source]
Parameters:

reader (Reader)

Return type:

Transaction

get_fee()[source]
Returns:

actual fee paid of this transaction under the current state

Return type:

int

hash()[source]
Return type:

bytes

hex()[source]
Return type:

str

is_coinbase()[source]
Return type:

bool

classmethod parse_script_offsets(octets)[source]

Since the validation of blockchain data is atomically transaction data validation, any application seeking to validate data in output scripts must store the entire transaction as well. Since the transaction data includes the output script data, saving a second copy of potentially large scripts can bloat application storage requirements.

This function efficiently parses binary transaction data to determine the offsets and lengths of each script. This supports the efficient retrieval of script data from transaction data.

@param octets: binary transaction data or hex string @returns: {

inputs: { vin: number, offset: number, length: number }[] outputs: { vout: number, offset: number, length: number }[]

}

Parameters:

octets (bytes | str)

Return type:

dict[str, list[dict[str, int]]]

preimage(index)[source]
Returns:

digest of the input specified by index

Parameters:

index (int)

Return type:

bytes

raw()
Return type:

str

serialize()[source]
Return type:

bytes

sign(bypass=True)[source]
Bypass:

if True then ONLY sign inputs which unlocking script is None, otherwise sign all the inputs

Parameters:

bypass (bool)

Return type:

Transaction

sign all inputs according to their script type

size()
Returns:

actual byte length of this transaction under the current state

Return type:

int

to_beef()[source]
Return type:

bytes

to_ef()[source]
Return type:

bytes

total_value_in()[source]
Return type:

int

total_value_out()[source]
Return type:

int

txid()[source]
Return type:

str

class pyrxd.transaction.TransactionInput[source]

Bases: object

__init__(source_transaction=None, source_txid=None, source_output_index=0, unlocking_script=None, unlocking_script_template=None, sequence=4294967295, sighash=SIGHASH.ALL_FORKID)[source]
Parameters:
  • source_txid (str | None)

  • source_output_index (int)

  • unlocking_script (Script | None)

  • unlocking_script_template (UnlockingScriptTemplate)

  • sequence (int)

  • sighash (SIGHASH)

classmethod from_hex(stream)[source]
Parameters:

stream (str | bytes | Reader)

Return type:

TransactionInput | None

serialize()[source]
Return type:

bytes

class pyrxd.transaction.TransactionOutput[source]

Bases: object

__init__(locking_script, satoshis=None, change=False)[source]
Parameters:
classmethod from_hex(stream)[source]
Parameters:

stream (str | bytes | Reader)

Return type:

TransactionOutput | None

serialize()[source]
Return type:

bytes