Skip to main content

Crate stellar_strkey

Crate stellar_strkey 

Source
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:

§Strkey kinds

§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 — derives Serialize/Deserialize that round-trip strkeys as their textual form. ed25519::PrivateKey is Deserialize but not directly Serialize; wrap in Unredacted to serialize.
  • serde-decoded — adds a Decoded<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. Requires alloc and implies serde.
  • cli — builds the stellar-strkey binary for encoding and decoding strkeys from the command line. Requires std (disables no_std for the crate). Not intended for enabling with the library.

Modules§

ed25519

Structs§

Contract
A contract identifier (C...).
HashX
A hash-x signer (X...).
LiquidityPool
A liquidity pool identifier (L...).
PreAuthTx
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§

ClaimableBalance
A claimable balance identifier (B...).
DecodeError
An error returned when decoding a strkey.
Strkey
A decoded Stellar strkey of any supported type.

Constants§

VERSION