Data
extension Data
extension Data: XDRCodable
Extension providing base32 encoding and decoding for Data.
Supports both standard base32 (RFC 4648) and base32hex variants. Used extensively in Stellar’s StrKey encoding for account addresses and keys.
-
Encodes data to a base32 string.
Uses the standard base32 alphabet (A-Z, 2-7).
Example:
let data = Data([72, 101, 108, 108, 111]) let encoded = data.base32EncodedString // "JBSWY3DP"Declaration
Swift
public var base32EncodedString: String { get } -
Encodes data to base32 format as Data.
Returns the base32-encoded string as UTF-8 data.
Example:
let data = Data([72, 101, 108, 108, 111]) let encodedData = data.base32EncodedDataDeclaration
Swift
public var base32EncodedData: Data { get } -
Decodes base32-encoded data.
Assumes the data contains a UTF-8 encoded base32 string.
Example:
let encoded = "JBSWY3DP".data(using: .utf8)! if let decoded = encoded.base32DecodedData { // Use decoded data }Declaration
Swift
public var base32DecodedData: Data? { get }Return Value
Decoded data, or nil if decoding fails
-
Encodes data to a base32hex string.
Uses the extended hex alphabet (0-9, A-V).
Example:
let data = Data([72, 101, 108, 108, 111]) let encoded = data.base32HexEncodedStringDeclaration
Swift
public var base32HexEncodedString: String { get } -
Encodes data to base32hex format as Data.
Returns the base32hex-encoded string as UTF-8 data.
Declaration
Swift
public var base32HexEncodedData: Data { get } -
Decodes base32hex-encoded data.
Assumes the data contains a UTF-8 encoded base32hex string.
Declaration
Swift
public var base32HexDecodedData: Data? { get }Return Value
Decoded data, or nil if decoding fails
-
Options for base16 encoding.
See moreDeclaration
Swift
enum Base16EncodingOptions -
Encodes data to a base16 (hexadecimal) string.
Declaration
Swift
func base16EncodedString(options: [Base16EncodingOptions] = []) -> StringParameters
optionsEncoding options
Return Value
Hexadecimal string representation
-
Encodes data to base16 format as Data (UTF-8 bytes of hex string).
Declaration
Swift
func base16EncodedData(options: [Base16EncodingOptions] = []) -> DataParameters
optionsEncoding options
Return Value
UTF-8 encoded hexadecimal string as Data
-
Creates data from a base16 (hexadecimal) encoded string.
Throws
Base16EncodingError if the string is invalidDeclaration
Swift
init(base16Encoded string: String) throwsParameters
stringHexadecimal string (even length, case insensitive)
-
Creates data from base16 (hexadecimal) encoded UTF-8 Data.
Throws
Base16EncodingError if the data is invalidDeclaration
Swift
init(base16Encoded data: Data) throwsParameters
dataUTF-8 encoded hexadecimal string as Data
-
Returns a Base64URL-encoded string (RFC 4648 §5, no padding).
Declaration
Swift
func base64URLEncodedString() -> String -
Creates data from a Base64URL-encoded string.
Accepts input with or without trailing
=padding. Re-pads internally and delegates to the platform Base64 decoder.Throws
Base64URLEncodingError.invalidInputwhen the string is not valid Base64URL.Declaration
Swift
init(base64URLEncoded string: String) throwsParameters
stringBase64URL-encoded string (with or without
=padding). -
Compares two byte sequences in constant time.
Always inspects every byte of both sequences regardless of where the first mismatch occurs, preventing an attacker from inferring partial match length by measuring execution time. The length-difference indicator is stored as a Boolean flag (0 or 1) rather than a narrowed XOR of the lengths, keeping the implementation trap-free for any input sizes and avoiding the edge case where two different-length inputs could produce a zero-difference accumulator through integer overflow truncation.
Declaration
Swift
func constantTimeEquals(_ other: Data) -> BoolParameters
otherThe byte sequence to compare against.
Return Value
truewhen both sequences have identical length and byte contents. -
Computes the SHA-256 hash of the data.
Declaration
Swift
var sha256Hash: Data { get } -
Encodes data to strkey ed25519 public key (“G…”).
Throws
StellarSDKError.invalidArgument if the data is not the 32 bytes an ed25519 public key is wideDeclaration
Swift
public func encodeEd25519PublicKey() throws -> String -
Encodes data to strkey ed25519 seed (“S…”).
Throws
StellarSDKError.invalidArgument if the data is not the 32 bytes an ed25519 seed is wideDeclaration
Swift
public func encodeEd25519SecretSeed() throws -> String -
Encodes data to strkey med25519 public key. (“M…”)
The data is the ed25519 key followed by the muxed account id.
Throws
StellarSDKError.invalidArgument if the data is not the 40 bytes the 32 byte ed25519 key and the 8 byte muxed account id occupyDeclaration
Swift
public func encodeMEd25519AccountId() throws -> String -
Encodes data to strkey preAuthTx. (“T…”)
Throws
StellarSDKError.invalidArgument if the data is not the 32 bytes a pre authorized transaction hash is wideDeclaration
Swift
public func encodePreAuthTx() throws -> String -
Encodes data to strkey sha256 hash. (“X…”)
Throws
StellarSDKError.invalidArgument if the data is not the 32 bytes a sha256 hash is wideDeclaration
Swift
public func encodeSha256Hash() throws -> String -
Encodes raw data to strkey signed payload (“P…”).
The data is the signed payload signer body: the signer key, the length of the signed data, the signed data itself and zero padding to a multiple of four bytes.
Throws
StellarSDKError.invalidArgument if the data does not carry that framing: if it is too narrow to hold the 32 byte signer key and the 4 byte length field, if the length field does not name a length of 1 to 64 bytes, if the data is not as wide as that length padded to a multiple of four bytes, or if the padding is not zero bytesDeclaration
Swift
public func encodeSignedPayload() throws -> String -
Encodes raw data to strkey contract id (“C…”).
Throws
StellarSDKError.invalidArgument if the data is not the 32 bytes a contract id is wideDeclaration
Swift
public func encodeContractId() throws -> String -
Encodes raw data to strkey claimable balance (“B…”).
The data is the id on its own, in which case the type discriminant CLAIMABLE_BALANCE_ID_TYPE_V0 is put in front of it, the body that already carries that discriminant, or the XDR encoding opening with the four byte union discriminant, the shape Horizon reports. CLAIMABLE_BALANCE_ID_TYPE_V0 is the only type the XDR union defines.
Throws
StellarSDKError.invalidArgument if the data is none of these: neither the 32 bytes an id is wide, nor the 33 bytes of a body, nor the 36 bytes of the XDR encoding, or if a body does not open with that discriminantDeclaration
Swift
public func encodeClaimableBalanceId() throws -> String -
Encodes raw data to strkey liquidity pool id (“L…”).
Throws
StellarSDKError.invalidArgument if the data is not the 32 bytes a liquidity pool id is wideDeclaration
Swift
public func encodeLiquidityPoolId() throws -> String -
Encodes raw data representing a MuxedAccountXDR to strkey muxed account id (“M…”).
The data is the 4 byte key type followed by the key material. KEY_TYPE_ED25519 with the 32 byte ed25519 key gives the “G…” strkey of that key; KEY_TYPE_MUXED_ED25519 with the 8 byte muxed account id and the 32 byte ed25519 key gives an “M…” strkey.
The key type is read first and decides the width: the data must then be exactly 36 bytes for KEY_TYPE_ED25519 and 44 bytes for KEY_TYPE_MUXED_ED25519.
Throws
StellarSDKError.invalidArgument if the data is shorter than the 4 byte key type, if the key type is neither KEY_TYPE_ED25519 nor KEY_TYPE_MUXED_ED25519, or if the data is not exactly the width its key type names.Declaration
Swift
public func encodeMuxedAccount() throws -> String -
Encodes data to XDR format with automatic padding.
Encodes the byte count followed by the bytes, padded to a 4-byte boundary.
Declaration
Swift
public func xdrEncode(to encoder: XDREncoder) throws -
Encodes data to XDR format without length prefix (fixed-size encoding).
Used for fixed-size byte arrays where the length is known in advance.
Declaration
Swift
public func xdrEncodeFixed(to encoder: XDREncoder) throws -
Decodes variable-length data from XDR format.
Reads the byte count, then the bytes, consuming any padding bytes.
Declaration
Swift
public init(fromBinary decoder: XDRDecoder) throws -
Decodes fixed-size data from XDR format.
Reads exactly
countraw bytes with no length prefix and no padding, the layoutxdrEncodeFixed(to:)writes.Throws
StellarSDKError.xdrDecodingError if the count is negative or exceeds the remaining bytesDeclaration
Swift
public init(fromBinary xdrDecoder: XDRDecoder, count: Int) throwsParameters
xdrDecoderDecoder to read from
countNumber of bytes to read
View on GitHub
Install in Dash