TxRepHelper
public final class TxRepHelper : Sendable
Shared utility functions for TxRep encoding and decoding.
Used by both generated and wrapper TxRep code to provide consistent parsing, formatting, and Stellar type conversion.
All methods are static; this class is never instantiated.
-
Parse TxRep text into a key-value map.
Handles blank lines, comment lines (starting with
:), CRLF line endings, and lines with no colon (skipped). Splits on the first:only and trims value whitespace. Duplicate keys use the last-write-wins rule.Declaration
Swift
public static func parse(_ txRep: String) -> [String : String]Parameters
txRepHuman-readable TxRep string.
Return Value
Dictionary mapping keys to raw (unprocessed) value strings.
-
Get a value from the map, stripping inline comments via
removeComment(_:).Declaration
Swift
public static func getValue(_ map: [String : String], _ key: String) -> String?Parameters
mapThe key-value map returned by
parse(_:).keyThe key to look up.
Return Value
The trimmed value with any inline comment removed, or
nilif the key is absent. -
Remove an inline comment from a TxRep value string.
If the value begins with a double-quote, the method finds the closing quote (respecting backslash escapes) and returns everything up to and including it. For unquoted values, any
(…)suffix is stripped and the result is trimmed.Declaration
Swift
public static func removeComment(_ value: String) -> StringParameters
valueRaw value string, possibly containing a trailing comment.
Return Value
Value with any inline comment removed.
-
Encode bytes as a lowercase hex string.
Returns
"0"for empty input to match the SEP-0011 convention for zero-length opaque fields.Declaration
Swift
public static func bytesToHex(_ bytes: Data) -> StringParameters
bytesBinary data to encode.
Return Value
Lowercase hex string, or
"0"for empty data. -
Decode a hex string to bytes.
"0"decodes to emptyData. Odd-length hex strings are left-padded with a zero digit (e.g.,"f"becomes"0f"). Non-hex characters cause a throw.Declaration
Swift
public static func hexToBytes(_ hex: String) throws -> DataParameters
hexHex string to decode. May be
"0"or any valid hex string.Return Value
Decoded
Data.
-
Escape a string for the TxRep double-quoted format.
The output is wrapped in double quotes. Inside the quotes:
\is encoded as\\"is encoded as\"\n(LF) is encoded as\n\r(CR) is encoded as\r\t(HT) is encoded as\t- Bytes 0x00–0x1F (excluding the above), 0x7F, and 0x80–0xFF are encoded
as
\xNNwhere NN is two lowercase hex digits per UTF-8 byte. Printable ASCII 0x20–0x7E (excluding
\and") is passed through.
Declaration
Swift
public static func escapeString(_ s: String) -> StringParameters
sThe string to escape.
Return Value
Escaped, double-quoted string.
-
Unescape a TxRep string value.
If the input is enclosed in double quotes they are stripped first. Handles
\",\\,\n,\r,\t,\xNN(hex), and\NNN(octal) escape sequences. If the input is not quoted it is returned as-is (no unescaping is attempted).Throws
TxRepError.invalidValue(key:)if the string is quoted but unterminated, or if a\xNNsequence contains invalid hex digits.Declaration
Swift
public static func unescapeString(_ s: String) throws -> StringParameters
sThe possibly-quoted string to unescape.
Return Value
Unescaped string.
-
Parse a string to
Int32, supporting decimal and0x/0Xhex prefixes.Leading
-is accepted for negative values. Hex input may also be negative (e.g.,-0x1). Throws on overflow or invalid characters.Throws
TxRepError.invalidValue(key:)on parse failure or overflow.Declaration
Swift
public static func parseInt(_ s: String) throws -> Int32Parameters
sString to parse.
Return Value
Parsed
Int32value. -
Parse a string to
Int64, supporting decimal and0x/0Xhex prefixes.Leading
-is accepted for negative values. Throws on overflow or invalid characters.Throws
TxRepError.invalidValue(key:)on parse failure or overflow.Declaration
Swift
public static func parseInt64(_ s: String) throws -> Int64Parameters
sString to parse.
Return Value
Parsed
Int64value. -
Parse a string to
UInt64, supporting decimal and0x/0Xhex prefixes.Does not accept a leading
-. Throws on invalid characters or if the value exceedsUInt64.max.Throws
TxRepError.invalidValue(key:)on parse failure.Declaration
Swift
public static func parseUInt64(_ s: String) throws -> UInt64Parameters
sString to parse.
Return Value
Parsed
UInt64value.
-
Parse a G-address StrKey string to a
PublicKey.Throws
TxRepError.invalidValue(key:)if the address is not a valid G-address.Declaration
Swift
public static func parseAccountId(_ strKey: String) throws -> PublicKeyParameters
strKeyG-address string.
Return Value
PublicKeyvalue. -
Convert a
MuxedAccountXDRto its canonical StrKey string.Returns a G-address for plain Ed25519 accounts and an M-address for muxed accounts.
Throws
TxRepError.invalidValue(key:)if encoding fails.Declaration
Swift
public static func formatMuxedAccount(_ mux: MuxedAccountXDR) throws -> StringParameters
muxThe muxed account XDR value.
Return Value
G- or M-address string.
-
Parse a G- or M-address StrKey string to a
MuxedAccountXDR.Throws
TxRepError.invalidValue(key:)if the string is not a valid account address.Declaration
Swift
public static func parseMuxedAccount(_ strKey: String) throws -> MuxedAccountXDRParameters
strKeyG-address or M-address string.
Return Value
MuxedAccountXDRvalue. -
Format an
AssetXDRas a TxRep asset string.Returns
"XLM"for native assets and"CODE:ISSUER"for credit assets.Declaration
Swift
public static func formatAsset(_ asset: AssetXDR) -> StringParameters
assetThe asset XDR value.
Return Value
TxRep asset string.
-
Parse a TxRep asset string to an
AssetXDR.Accepts
"XLM"or"native"for native, and"CODE:ISSUER"for credit assets. Asset codes of 1–4 characters producealphanum4; 5–12 producealphanum12.Throws
TxRepError.invalidValue(key:)if the string cannot be parsed.Declaration
Swift
public static func parseAsset(_ value: String) throws -> AssetXDRParameters
valueTxRep asset string.
Return Value
AssetXDRvalue. -
Format a
ChangeTrustAssetXDRas a TxRep string.Returns
"XLM"for native,"CODE:ISSUER"for credit assets. Pool-share assets cannot be represented as a single compact string (they are serialized field-by-field by the caller) and cause a throw.Throws
TxRepError.invalidValue(key:)for pool-share or unknown asset types.Declaration
Swift
public static func formatChangeTrustAsset(_ asset: ChangeTrustAssetXDR) throws -> StringParameters
assetThe change-trust asset XDR value.
Return Value
TxRep asset string.
-
Parse a TxRep string to a
ChangeTrustAssetXDR.Handles
"XLM"/"native"and"CODE:ISSUER"formats. Pool-share assets must be constructed separately from their constituent fields.Throws
TxRepError.invalidValue(key:)if the string cannot be parsed.Declaration
Swift
public static func parseChangeTrustAsset(_ value: String) throws -> ChangeTrustAssetXDRParameters
valueTxRep asset string.
Return Value
ChangeTrustAssetXDRvalue. -
Format a
TrustlineAssetXDRas a TxRep string.Returns
"XLM"for native,"CODE:ISSUER"for credit assets, and a 64-character lowercase hex string for pool-share assets (the 32-byte liquidity pool ID).Throws
TxRepError.invalidValue(key:)for unknown asset types.Declaration
Swift
public static func formatTrustlineAsset(_ asset: TrustlineAssetXDR) throws -> StringParameters
assetThe trustline asset XDR value.
Return Value
TxRep asset string.
-
Parse a TxRep string to a
TrustlineAssetXDR.Accepts
"XLM"/"native","CODE:ISSUER", and a 64-character hex string (pool-share liquidity pool ID).Throws
TxRepError.invalidValue(key:)if the string cannot be parsed.Declaration
Swift
public static func parseTrustlineAsset(_ value: String) throws -> TrustlineAssetXDRParameters
valueTxRep asset string.
Return Value
TrustlineAssetXDRvalue. -
Format a
SignerKeyXDRas its canonical StrKey string.ed25519→ G-addresspreAuthTx→ T-addresshashX→ X-addresssignedPayload→ P-address
Throws
TxRepError.invalidValue(key:)if encoding fails.Declaration
Swift
public static func formatSignerKey(_ key: SignerKeyXDR) throws -> StringParameters
keyThe signer key XDR value.
Return Value
StrKey-encoded signer key.
-
Parse a StrKey string to a
SignerKeyXDR.The key type is inferred from the leading character:
G→ ed25519T→ preAuthTxX→ hashXP→ signedPayload
Throws
TxRepError.invalidValue(key:)if the string cannot be decoded.Declaration
Swift
public static func parseSignerKey(_ value: String) throws -> SignerKeyXDRParameters
valueStrKey-encoded signer key string.
Return Value
SignerKeyXDRvalue. -
Format an
AllowTrustOpAssetXDRas a compact asset code string.Trailing null bytes are stripped from the fixed-length code arrays.
Throws
TxRepError.invalidValue(key:)for unknown asset types.Declaration
Swift
public static func formatAllowTrustAsset(_ asset: AllowTrustOpAssetXDR) throws -> StringParameters
assetThe allow-trust asset XDR value.
Return Value
Asset code string (e.g.,
"USDC"). -
Parse an asset code string to an
AllowTrustOpAssetXDR.Codes of 1–4 characters produce
alphanum4; 5–12 producealphanum12.Throws
TxRepError.invalidValue(key:)if the code is empty or longer than 12 characters.Declaration
Swift
public static func parseAllowTrustAsset(_ code: String) throws -> AllowTrustOpAssetXDRParameters
codeAsset code string.
Return Value
AllowTrustOpAssetXDRvalue.
-
Encode a MEMO_TEXT string in SEP-0011 C-style escape format.
Per SEP-0011 (and the reference
stcimplementation), non-printable and non-ASCII bytes are escaped as\xNNover the raw UTF-8 byte sequence — NOT as\uNNNNUnicode code points. Delegates toescapeString(_:)which implements that format.decodeMemoText(_:)accepts both the SEP-0011\xNNformat and the legacy JSON\uNNNNformat produced by older iOS SDK builds (and other SDKs that have the same historical bug), so previously written TxRep data continues to parse.Declaration
Swift
public static func encodeMemoText(_ s: String) -> StringParameters
sThe raw memo text string (at most 28 bytes in UTF-8).
Return Value
Quoted SEP-0011-escaped string, e.g.
"Hello"or"caf\xc3\xa9". -
Decode a MEMO_TEXT value from TxRep, supporting both the legacy JSON-encoded format and the newer
\xNNescape format.Tries JSON decoding first (for interoperability with older iOS SDK output and other SDKs that write JSON string literals). Falls back to
unescapeString(_:)for\xNN-escaped strings.Throws
TxRepError.invalidValue(key:)if neither decoder succeeds.Declaration
Swift
public static func decodeMemoText(_ s: String) throws -> StringParameters
sRaw value string from the TxRep key-value map.
Return Value
The decoded memo text.
-
Require a hex-encoded binary field from the map.
Throws
missingValuewhen the key is absent. Re-throws any hex-decoding error asinvalidValue(key:)using the field key, not the raw value string, so that error messages contain the TxRep field name.Declaration
Swift
public static func requireHex(_ map: [String : String], _ key: String) throws -> DataParameters
mapKey-value map from
parse(_:).keyThe TxRep field key, e.g.
"signatures[0].hint".Return Value
Decoded binary
Data. -
Require a hex-encoded field and wrap the result in a
WrappedData4.Declaration
Swift
public static func requireWrappedData4(_ map: [String : String], _ key: String) throws -> WrappedData4 -
Require a hex-encoded field and wrap the result in a
WrappedData12.Declaration
Swift
public static func requireWrappedData12(_ map: [String : String], _ key: String) throws -> WrappedData12 -
Require a hex-encoded field and wrap the result in a
WrappedData32.Declaration
Swift
public static func requireWrappedData32(_ map: [String : String], _ key: String) throws -> WrappedData32 -
Require a quoted/escaped string field from the map.
Throws
missingValuewhen absent,invalidValue(with the field key) when the string cannot be unescaped.Declaration
Swift
public static func requireString(_ map: [String : String], _ key: String) throws -> String -
Require an
Int64field from the map.Declaration
Swift
public static func requireInt64(_ map: [String : String], _ key: String) throws -> Int64 -
Require a
UInt64field from the map.Declaration
Swift
public static func requireUInt64(_ map: [String : String], _ key: String) throws -> UInt64 -
Require a
MuxedAccountXDRfield from the map.Throws
missingValuewhen absent,invalidValue(key: <field key>)when invalid.Declaration
Swift
public static func requireMuxedAccount(_ map: [String : String], _ key: String) throws -> MuxedAccountXDR -
Require an
AssetXDRfield from the map (compact single-line format).Declaration
Swift
public static func requireAsset(_ map: [String : String], _ key: String) throws -> AssetXDR -
Require a
SignerKeyXDRfield from the map.Declaration
Swift
public static func requireSignerKey(_ map: [String : String], _ key: String) throws -> SignerKeyXDR -
Require an
AllowTrustOpAssetXDRfield from the map.Declaration
Swift
public static func requireAllowTrustAsset(_ map: [String : String], _ key: String) throws -> AllowTrustOpAssetXDR -
Require a liquidity pool ID field from the map, accepting either a 64-character lowercase hex string or an L-address StrKey (as used by SEP-0011 and the original hand-written TxRep serialiser).
Throws
TxRepError.missingValue(key:)if the key is absent,TxRepError.invalidValue(key:)if the value is neither valid hex nor a valid L-address.Declaration
Swift
public static func requireLiquidityPoolId(_ map: [String : String], _ key: String) throws -> WrappedData32Parameters
mapKey-value map from
parse(_:).keyThe TxRep field key.
Return Value
The liquidity pool ID as a
WrappedData32. -
Parse a liquidity pool ID from a string, accepting either a 64-character hex string or an L-address StrKey.
Throws
TxRepError.invalidValue(key:)if the value cannot be parsed.Declaration
Swift
public static func parseLiquidityPoolId(_ value: String) throws -> WrappedData32Parameters
valueHex-64 or L-address StrKey string.
Return Value
Decoded
WrappedData32.
View on GitHub
Install in Dash