Expand description
Encode and decode Stellar strkeys as defined by SEP-23.
Strkeys are the human-readable textual representation of identifiers used
across the Stellar network — account IDs, signing keys, contract IDs,
liquidity pool IDs, and others. Each strkey begins with a single ASCII
letter that identifies its kind (G, S, M, T, X, P, C, L,
B) and is encoded as base32 without padding. The binary form is a
one-byte version, the type’s payload, and a two-byte CRC16-XMODEM
checksum, ensuring that mistyped strkeys are detected before they are
used.
This crate provides:
- The
Strkeyenum, which can hold and round-trip any strkey kind other thanPrivateKeyEd25519(S…); private-key strkeys are handled directly viaed25519::PrivateKey, with rendering gated behindUnredacted. - Per-kind types in this module (
PreAuthTx,HashX,Contract,LiquidityPool,ClaimableBalance) and ined25519(ed25519::PublicKey,ed25519::PrivateKey,ed25519::MuxedAccount,ed25519::SignedPayload) for callers that know the kind in advance. DisplayandFromStrimplementations for every kind, plus inherentto_string/from_string/from_slicemethods.ed25519::PrivateKeyis the exception: it does not implementDisplayor inherentto_stringdirectly — wrap it inUnredacted(pk.as_unredacted()) to render the encoded strkey form.
§Strkey kinds
| Prefix | Kind | Payload bytes |
|---|---|---|
G | Strkey::PublicKeyEd25519 / ed25519::PublicKey | 32 |
S | ed25519::PrivateKey only (omitted from Strkey) | 32 |
M | Strkey::MuxedAccountEd25519 / ed25519::MuxedAccount | 40 |
T | Strkey::PreAuthTx / PreAuthTx | 32 |
X | Strkey::HashX / HashX | 32 |
P | Strkey::SignedPayloadEd25519 / ed25519::SignedPayload | 40–100 |
C | Strkey::Contract / Contract | 32 |
L | Strkey::LiquidityPool / LiquidityPool | 32 |
B | Strkey::ClaimableBalance / ClaimableBalance | 33 |
§Examples
Parse any strkey when the kind isn’t known up front:
use stellar_strkey::Strkey;
let s = "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF";
let strkey: Strkey = s.parse().unwrap();
assert!(matches!(strkey, Strkey::PublicKeyEd25519(_)));Parse a specific kind and reject anything else:
use stellar_strkey::Contract;
let s = "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSC4";
let contract: Contract = s.parse().unwrap();
assert_eq!(contract.0, [0u8; 32]);Construct a strkey from raw bytes and render it:
use stellar_strkey::ed25519::PublicKey;
let key = PublicKey([0u8; 32]);
assert_eq!(
key.to_string().as_str(),
"GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF",
);§no_std
With default features the crate is no_std and does not depend on
alloc. Encoding uses fixed-capacity buffers from the heapless crate,
so each kind’s to_string returns a heapless::String sized to the
maximum length for that kind.
§Cargo features
serde— derivesSerialize/Deserializethat round-trip strkeys as their textual form.ed25519::PrivateKeyisDeserializebut not directlySerialize; wrap inUnredactedto serialize.serde-decoded— adds aDecoded<T>wrapper that serializes a strkey as a structured JSON object with hex-encoded byte fields, which is useful for tooling that wants to inspect the underlying bytes. Requiresallocand impliesserde.cli— builds thestellar-strkeybinary for encoding and decoding strkeys from the command line. Requiresstd(disablesno_stdfor the crate). Not intended for enabling with the library.
Modules§
Structs§
- Contract
- A contract identifier (
C...). - HashX
- A hash-x signer (
X...). - Liquidity
Pool - A liquidity pool identifier (
L...). - PreAuth
Tx - A pre-authorized transaction signer (
T...). - Unredacted
- Wrapper that opts a value in to formatting or serialization that would otherwise expose private-key bytes.
- Version
Enums§
- Claimable
Balance - A claimable balance identifier (
B...). - Decode
Error - An error returned when decoding a strkey.
- Strkey
- A decoded Stellar strkey of any supported type.