Skip to content

Class BSV::Wallet::Wire::Writer

Inherits: Object

Builds a binary byte string using BRC-100 wire protocol encoding conventions.

All multi-byte integers use little-endian order unless stated otherwise. VarInts follow Bitcoin encoding; -1 is encoded as the 9-byte MaxUint64 sentinel.

Constants

MAX_PRIVILEGED_REASON

Maximum byte length for a PrivilegedReason string (Int8 length field).

Public Instance Methods

initialize()

  • @return [Writer] a new instance of Writer

to_binary()

Returns the accumulated binary data. - @return [String] binary string (encoding: ASCII-8BIT)

write_byte(val)

Appends a single unsigned byte (0–255). - @param val [Integer]

write_byte_array(data)

Appends a VarInt-prefixed byte array. - @param data [String] binary string

write_bytes(data)

Appends raw bytes with no length prefix. - @param data [String] binary string

write_counterparty(val)

Appends a counterparty value using the first-byte dispatch scheme.

Encoding: 'self' → 0x0B (11) 'anyone' → 0x0C (12) nil → 0x00 (0) hex pubkey (33 bytes compressed) → 33 raw bytes - @param val [String, nil] 'self', 'anyone', nil, or 66-char hex pubkey

write_int8(val)

Appends a signed 8-bit integer (-128–127). Negative values are encoded as their two's-complement byte. - @param val [Integer]

write_map(hash_or_nil)

Appends an optional string→string map: VarInt count + key/value pairs, or -1 if nil. - @param hash_or_nil [Hash, nil]

write_optional_bool(val_or_nil)

Appends an optional boolean as a signed Int8: 1=true, 0=false, -1=nil. - @param val_or_nil [Boolean, nil]

write_optional_byte_array(data_or_nil)

Appends a VarInt-prefixed byte array, or the -1 sentinel if nil. - @param data_or_nil [String, nil]

write_optional_utf8_string(str_or_nil)

Appends a VarInt-prefixed UTF-8 string, or the -1 sentinel if nil. - @param str_or_nil [String, nil]

write_outpoint(txid_hex, index)

Appends an outpoint: 32 bytes (txid in display order) + VarInt index. - @param txid_hex [String] 64-character hex txid - @param index [Integer] output index

write_privileged(privileged, privileged_reason)

Appends privileged parameters: optional bool + Int8-length reason string.

PrivilegedReason uses Int8 for its length field (max 127 bytes), not VarInt. -1 (0xFF) means absent. - @param privileged [Boolean, nil] - @param privileged_reason [String, nil]

write_protocol_id(protocol_id)

Appends a protocol ID: UInt8 security level + VarInt-prefixed UTF-8 name. - @param protocol_id [Array] [Integer level, String name]

write_signed_varint(val)

Appends a signed VarInt where -1 is encoded as MaxUint64 (9 bytes: FF * 9). - @param val [Integer] non-negative integer, or -1 to signal absence

write_string_array(arr_or_nil)

Appends an optional string array: VarInt count + strings, or -1 if nil. - @param arr_or_nil [Array, nil]

write_utf8_string(str)

Appends a VarInt-prefixed UTF-8 string. - @param str [String]

write_varint(val)

Appends an unsigned Bitcoin VarInt. - @param val [Integer] non-negative integer