Keyboard shortcuts

Press or to navigate between chapters

Press ? to show this help

Press Esc to hide this help

TransactionEncoderTrait

Bitcoin transaction encoder trait providing methods to encode transaction data to raw bytes.

This trait implements the complete Bitcoin transaction format encoder, supporting both legacy and SegWit transaction formats according to BIP 141. It handles variable-length encoding (compact size), little-endian integer encoding, and proper transaction structure serialization.

Fully qualified path: alexandria_btc::encoder::TransactionEncoderTrait

pub trait TransactionEncoderTrait

Trait functions

new

Creates a new Bitcoin transaction encoder with an empty ByteArray.

Returns

  • BTCEncoder - Initialized encoder context with empty data buffer

Fully qualified path: alexandria_btc::encoder::TransactionEncoderTrait::new

fn new() -> BTCEncoder

encode_transaction

Encodes a complete Bitcoin transaction to raw bytes.

Serializes the entire transaction structure including version, inputs, outputs, witness data (if SegWit), and locktime. Automatically includes SegWit marker (0x00) and flag (0x01) bytes for SegWit transactions.

Arguments

  • self - Mutable reference to the encoder context
  • transaction - Complete Bitcoin transaction structure to encode

Returns

  • ByteArray - Raw transaction bytes ready for broadcast

Fully qualified path: alexandria_btc::encoder::TransactionEncoderTrait::encode_transaction

fn encode_transaction(ref self: BTCEncoder, transaction: BitcoinTransaction) -> ByteArray

encode_input

Encodes a single Bitcoin transaction input to the current position.

Serializes input structure: previous TXID (32 bytes), previous output index, script signature with compact size length prefix, and sequence number.

Arguments

  • self - Mutable reference to the encoder context
  • input - Transaction input to encode

Fully qualified path: alexandria_btc::encoder::TransactionEncoderTrait::encode_input

fn encode_input(ref self: BTCEncoder, input: TransactionInput)

encode_output

Encodes a single Bitcoin transaction output to the current position.

Serializes output structure: value (8 bytes, little-endian) and script public key with compact size length prefix.

Arguments

  • self - Mutable reference to the encoder context
  • output - Transaction output to encode

Fully qualified path: alexandria_btc::encoder::TransactionEncoderTrait::encode_output

fn encode_output(ref self: BTCEncoder, output: TransactionOutput)

encode_witness

Encodes SegWit witness data for a single input.

Serializes witness stack structure: witness item count (compact size) followed by each witness item with its length prefix and data.

Arguments

  • self - Mutable reference to the encoder context
  • witness - Witness data to encode

Fully qualified path: alexandria_btc::encoder::TransactionEncoderTrait::encode_witness

fn encode_witness(ref self: BTCEncoder, witness: TransactionWitness)

write_compact_size

Writes a Bitcoin compact size integer (variable-length encoding).

Implements Bitcoin’s compact size encoding:

  • < 0xfd: 1 byte value
  • 0xfd: 3 bytes (1 + 2 little-endian)
  • 0xfe: 5 bytes (1 + 4 little-endian)
  • 0xff: 9 bytes (1 + 8 little-endian)

Arguments

  • self - Mutable reference to the encoder context
  • value - The integer value to encode

Fully qualified path: alexandria_btc::encoder::TransactionEncoderTrait::write_compact_size

fn write_compact_size(ref self: BTCEncoder, value: u64)

write_u8

Writes a single byte to the current position.

Arguments

  • self - Mutable reference to the encoder context
  • value - The byte value to write

Fully qualified path: alexandria_btc::encoder::TransactionEncoderTrait::write_u8

fn write_u8(ref self: BTCEncoder, value: u8)

write_u32_le

Writes a 32-bit unsigned integer as little-endian bytes.

Arguments

  • self - Mutable reference to the encoder context
  • value - The u32 value to encode

Fully qualified path: alexandria_btc::encoder::TransactionEncoderTrait::write_u32_le

fn write_u32_le(ref self: BTCEncoder, value: u32)

write_u64_le

Writes a 64-bit unsigned integer as little-endian bytes.

Arguments

  • self - Mutable reference to the encoder context
  • value - The u64 value to encode

Fully qualified path: alexandria_btc::encoder::TransactionEncoderTrait::write_u64_le

fn write_u64_le(ref self: BTCEncoder, value: u64)

write_bytes

Writes raw bytes to the current position.

Arguments

  • self - Mutable reference to the encoder context
  • bytes - The ByteArray to append

Fully qualified path: alexandria_btc::encoder::TransactionEncoderTrait::write_bytes

fn write_bytes(ref self: BTCEncoder, bytes: ByteArray)