TxRepHelper
in package
Shared utility functions for TxRep (SEP-0011) encoding and decoding.
Provides consistent parsing, formatting, and Stellar type conversion used by both generated and hand-written TxRep serialization code.
Tags
Table of Contents
Methods
- bytesToHex() : string
- Encode a binary string as a lowercase hex string.
- escapeString() : string
- Escape a string for TxRep double-quoted format.
- formatAccountId() : string
- Convert an XdrAccountID to a StrKey account ID string (`G...`).
- formatAllowTrustAsset() : string
- Format an XdrAllowTrustOperationAsset as a compact asset code string.
- formatAmount() : string
- Convert a stroop value to a decimal XLM string with exactly 7 decimal places.
- formatAsset() : string
- Format an XdrAsset as a TxRep asset string.
- formatChangeTrustAsset() : string
- Format an XdrChangeTrustAsset as a TxRep string.
- formatMuxedAccount() : string
- Convert an XdrMuxedAccount to a StrKey string.
- formatSignerKey() : string
- Format an XdrSignerKey as a StrKey string.
- formatTrustlineAsset() : string
- Format an XdrTrustlineAsset as a TxRep string.
- getValue() : string|null
- Get a value from a parsed TxRep map, stripping inline comments.
- hexToBytes() : string
- Decode a hex string to a raw binary string.
- parse() : array<string, string>
- Parse TxRep text into a key-value map.
- parseAccountId() : XdrAccountID
- Parse a StrKey account ID string (`G...`) to an XdrAccountID.
- parseAllowTrustAsset() : XdrAllowTrustOperationAsset
- Parse an asset code string to an XdrAllowTrustOperationAsset.
- parseAmount() : int
- Convert a decimal XLM string to stroops.
- parseAsset() : XdrAsset
- Parse a TxRep asset string (`native` or `CODE:ISSUER`) to an XdrAsset.
- parseBigInt() : BigInteger
- Parse a big-integer string, supporting decimal and `0x`/`0X` hex notation.
- parseChangeTrustAsset() : XdrChangeTrustAsset
- Parse a TxRep string to an XdrChangeTrustAsset.
- parseInt() : int
- Parse an integer string, supporting decimal and `0x`/`0X` hex notation.
- parseMuxedAccount() : XdrMuxedAccount
- Parse a StrKey string to an XdrMuxedAccount.
- parseSignerKey() : XdrSignerKey
- Parse a StrKey string to an XdrSignerKey.
- parseTrustlineAsset() : XdrTrustlineAsset
- Parse a TxRep string to an XdrTrustlineAsset.
- removeComment() : string
- Remove an inline comment from a TxRep value string.
- unescapeString() : string
- Unescape a TxRep string value.
Methods
bytesToHex()
Encode a binary string as a lowercase hex string.
public
static bytesToHex(string $bytes) : string
Returns "0" for empty input (SEP-0011 convention for empty byte arrays).
Parameters
- $bytes : string
-
Raw binary string.
Return values
string —Lowercase hex representation, or "0" for empty input.
escapeString()
Escape a string for TxRep double-quoted format.
public
static escapeString(string $s) : string
Wraps the result in double quotes. Escape rules:
\becomes\\"becomes\"- newline (0x0A) becomes
\n - carriage return (0x0D) becomes
\r - tab (0x09) becomes
\t - printable ASCII (0x20–0x7E) is passed through unchanged
- all other bytes are encoded as
\xNN(two hex digits, lowercase)
Parameters
- $s : string
-
The raw UTF-8 string to escape.
Return values
string —The escaped string wrapped in double quotes.
formatAccountId()
Convert an XdrAccountID to a StrKey account ID string (`G...`).
public
static formatAccountId(XdrAccountID $id) : string
Parameters
- $id : XdrAccountID
-
The XDR account ID.
Return values
string —The StrKey-encoded account ID (G...).
formatAllowTrustAsset()
Format an XdrAllowTrustOperationAsset as a compact asset code string.
public
static formatAllowTrustAsset(XdrAllowTrustOperationAsset $asset) : string
Returns the asset code with trailing null bytes stripped.
Parameters
- $asset : XdrAllowTrustOperationAsset
-
The XDR allow-trust asset.
Tags
Return values
string —The asset code string.
formatAmount()
Convert a stroop value to a decimal XLM string with exactly 7 decimal places.
public
static formatAmount(int $stroops) : string
Example: 1000000000 → "100.0000000"
Parameters
- $stroops : int
-
Amount in stroops (1 XLM = 10,000,000 stroops).
Return values
string —Decimal string with 7 decimal places.
formatAsset()
Format an XdrAsset as a TxRep asset string.
public
static formatAsset(XdrAsset $asset) : string
Returns native for XLM (ASSET_TYPE_NATIVE), and CODE:ISSUER for
credit assets (ASSET_TYPE_CREDIT_ALPHANUM4 / ASSET_TYPE_CREDIT_ALPHANUM12).
Parameters
- $asset : XdrAsset
-
The XDR asset.
Tags
Return values
string —The TxRep asset string.
formatChangeTrustAsset()
Format an XdrChangeTrustAsset as a TxRep string.
public
static formatChangeTrustAsset(XdrChangeTrustAsset $asset) : string
Returns native for XLM and CODE:ISSUER for credit assets.
Pool share assets cannot be represented as a single compact string and
throw an exception — callers must expand those field-by-field.
Parameters
- $asset : XdrChangeTrustAsset
-
The XDR change-trust asset.
Tags
Return values
string —The TxRep asset string.
formatMuxedAccount()
Convert an XdrMuxedAccount to a StrKey string.
public
static formatMuxedAccount(XdrMuxedAccount $mux) : string
Returns a G... address for plain Ed25519 accounts or an M... address
for muxed (med25519) accounts.
Parameters
- $mux : XdrMuxedAccount
-
The XDR muxed account.
Return values
string —The StrKey-encoded account address.
formatSignerKey()
Format an XdrSignerKey as a StrKey string.
public
static formatSignerKey(XdrSignerKey $key) : string
Uses the prefix appropriate for the key type:
G...for Ed25519 public keysT...for pre-authorization transaction hashesX...for SHA-256 hash keysP...for signed payload keys
Parameters
- $key : XdrSignerKey
-
The XDR signer key.
Tags
Return values
string —The StrKey-encoded signer key.
formatTrustlineAsset()
Format an XdrTrustlineAsset as a TxRep string.
public
static formatTrustlineAsset(XdrTrustlineAsset $asset) : string
Returns native for XLM, CODE:ISSUER for credit assets, and the
64-character lowercase hex pool ID for pool share assets.
Parameters
- $asset : XdrTrustlineAsset
-
The XDR trustline asset.
Tags
Return values
string —The TxRep asset string.
getValue()
Get a value from a parsed TxRep map, stripping inline comments.
public
static getValue(array<string, string> $map, string $key) : string|null
Parameters
- $map : array<string, string>
-
Map produced by self::parse().
- $key : string
-
Key to look up.
Return values
string|null —The cleaned value, or null if the key is not present.
hexToBytes()
Decode a hex string to a raw binary string.
public
static hexToBytes(string $hex) : string
The special value "0" decodes to an empty string. Odd-length hex
strings are left-padded with a leading zero before decoding.
Parameters
- $hex : string
-
Lowercase or uppercase hex string, or
"0".
Tags
Return values
string —Raw binary string.
parse()
Parse TxRep text into a key-value map.
public
static parse(string $txRep) : array<string, string>
Normalizes CRLF line endings, skips blank lines and comment-only lines
(lines whose first non-whitespace character is :), splits on the first
: only, and trims both key and value whitespace.
Parameters
- $txRep : string
-
Raw TxRep text.
Return values
array<string, string> —Ordered map of key => raw value strings.
parseAccountId()
Parse a StrKey account ID string (`G...`) to an XdrAccountID.
public
static parseAccountId(string $strKey) : XdrAccountID
Parameters
- $strKey : string
-
StrKey-encoded account ID (G...).
Tags
Return values
XdrAccountID —The decoded XDR account ID.
parseAllowTrustAsset()
Parse an asset code string to an XdrAllowTrustOperationAsset.
public
static parseAllowTrustAsset(string $code) : XdrAllowTrustOperationAsset
Codes up to 4 characters become ASSET_TYPE_CREDIT_ALPHANUM4; codes up to 12 characters become ASSET_TYPE_CREDIT_ALPHANUM12.
Parameters
- $code : string
-
The asset code (1–12 characters).
Tags
Return values
XdrAllowTrustOperationAsset —The decoded XDR allow-trust asset.
parseAmount()
Convert a decimal XLM string to stroops.
public
static parseAmount(string $decimal) : int
Example: "100.0000000" → 1000000000
The input may have fewer than 7 decimal places; missing places are treated as zeros. Values with more than 7 decimal places are truncated.
Parameters
- $decimal : string
-
Decimal amount string (e.g.
"100.5","0.0000001").
Tags
Return values
int —Amount in stroops.
parseAsset()
Parse a TxRep asset string (`native` or `CODE:ISSUER`) to an XdrAsset.
public
static parseAsset(string $value) : XdrAsset
Accepts both native and XLM as representations of the native asset.
Parameters
- $value : string
-
TxRep asset string.
Tags
Return values
XdrAsset —The decoded XDR asset.
parseBigInt()
Parse a big-integer string, supporting decimal and `0x`/`0X` hex notation.
public
static parseBigInt(string $s) : BigInteger
Handles an optional leading minus sign for negative values.
Parameters
- $s : string
-
The integer string to parse.
Tags
Return values
BigInteger —The parsed big integer.
parseChangeTrustAsset()
Parse a TxRep string to an XdrChangeTrustAsset.
public
static parseChangeTrustAsset(string $value) : XdrChangeTrustAsset
Handles native, XLM, and CODE:ISSUER representations. Pool share
assets cannot be parsed from a compact string; use field-by-field parsing
in the caller.
Parameters
- $value : string
-
TxRep asset string.
Tags
Return values
XdrChangeTrustAsset —The decoded XDR change-trust asset.
parseInt()
Parse an integer string, supporting decimal and `0x`/`0X` hex notation.
public
static parseInt(string $s) : int
Handles an optional leading minus sign for negative values.
Parameters
- $s : string
-
The integer string to parse.
Tags
Return values
int —The parsed integer.
parseMuxedAccount()
Parse a StrKey string to an XdrMuxedAccount.
public
static parseMuxedAccount(string $strKey) : XdrMuxedAccount
Accepts both G... (standard Ed25519) and M... (muxed ed25519) addresses.
Parameters
- $strKey : string
-
StrKey-encoded account address (G... or M...).
Tags
Return values
XdrMuxedAccount —The decoded XDR muxed account.
parseSignerKey()
Parse a StrKey string to an XdrSignerKey.
public
static parseSignerKey(string $value) : XdrSignerKey
The key type is inferred from the StrKey prefix:
G— Ed25519 public key (SIGNER_KEY_TYPE_ED25519)T— pre-authorization transaction hash (SIGNER_KEY_TYPE_PRE_AUTH_TX)X— SHA-256 hash (SIGNER_KEY_TYPE_HASH_X)P— signed payload (SIGNER_KEY_TYPE_ED25519_SIGNED_PAYLOAD)
Parameters
- $value : string
-
StrKey-encoded signer key.
Tags
Return values
XdrSignerKey —The decoded XDR signer key.
parseTrustlineAsset()
Parse a TxRep string to an XdrTrustlineAsset.
public
static parseTrustlineAsset(string $value) : XdrTrustlineAsset
Handles:
native/XLM— native asset- 64-character hex string — pool share asset (liquidity pool ID)
CODE:ISSUER— credit asset
Parameters
- $value : string
-
TxRep asset string.
Tags
Return values
XdrTrustlineAsset —The decoded XDR trustline asset.
removeComment()
Remove an inline comment from a TxRep value string.
public
static removeComment(string $value) : string
If the value starts with a double-quote, the closing quote is located
(respecting \ escapes) and everything after it is discarded. For
unquoted values the first ( marks the start of a comment.
Parameters
- $value : string
-
Raw value string (possibly with trailing comment).
Return values
string —The value with any inline comment removed and trimmed.
unescapeString()
Unescape a TxRep string value.
public
static unescapeString(string $s) : string
Strips surrounding double quotes if present, then processes escape
sequences: \", \\, \n, \r, \t, and \xNN (two hex digits).
Parameters
- $s : string
-
The raw TxRep string value (optionally quoted).
Return values
string —The unescaped string.