Skip to main content

py-stellar-xdr

A Python library to encode and decode Stellar XDR, the binary format of transactions, ledger entries, contract values and everything else on the Stellar network, and its SEP-51 JSON form.

  • Complete. Every type of the network's XDR definitions, generated from them, with a release for each protocol version; stellar_xdr.PROTOCOL_VERSION says which.
  • Strict. Binary decoding accepts only the one canonical encoding of a value. Constructors check their arguments, so any value that exists can be encoded as XDR.
  • Typed. Immutable classes, unions you can match on, and complete type hints that mypy and pyright check in strict mode.
  • Thoroughly tested. Every type and union arm is tested with random values, and the tests cover 100% of the branches of the code. On top of that, real data from the public networks decodes and encodes back unchanged, and JSON matches the reference implementation value for value.

Install

pip install stellar-xdr

Usage

import json

import stellar_xdr as xdr

# Decode a transaction envelope, as Stellar's APIs return it.
envelope = xdr.TransactionEnvelope.from_xdr_base64(
    "AAAAAgAAAADmmSZkwY3163TMouB2TY8MljqXw2IxVYTGyvDrR6YtAAAqmmQAABpuAAAAAQAAAAAAAAAAAAAAAQ"
    "AAAAAAAAAYAAAAAQAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAAAAAABAAAABgAAAAHXkotywnA8z+r365/0"
    "701QSlWouXn8m0UOoshCtNHOYQAAABQAAAABAAI9fQAAAAAAAAD4AAAAAAAqmgAAAAABR6YtAAAAAEArDtxbqU"
    "I+CsdkRmV0lFhVt0wyB7fyrmmkM6Fr35wpPcK8WKcXeKTl4BQ+akE14MZtpaea9LMdhXopaW3pJA0E"
)

# A union is one class per arm, so match tells them apart.
match envelope:
    case xdr.TransactionEnvelope.Tx(v1):
        transaction = v1.tx
    case _:
        raise ValueError("expected a v1 transaction")
assert transaction.fee == 2792036
assert isinstance(transaction.operations[0].body, xdr.OperationBody.InvokeHostFunction)

# Build a value. Strings accept str, and arrays any sequence.
args = xdr.SCVal.Vec(
    [xdr.SCVal.Symbol("transfer"), xdr.SCVal.I128(xdr.Int128Parts(0, 100))]
)
assert xdr.SCVal.from_xdr_base64(args.to_xdr_base64()) == args

# SEP-51 JSON, ready for json.dumps.
assert json.dumps(args.to_json_value()) == (
    '{"vec": [{"symbol": "transfer"}, {"i128": "100"}]}'
)
assert xdr.SCVal.from_json_value(json.loads('{"u32": 5}')) == xdr.SCVal.U32(5)

The guide covers values, unions, decoding, errors and JSON in more detail.

Development

The project uses uv.

make check       # lint, type-check and test
make generate    # regenerate the package from xdr/xdr.json
make docs-serve  # preview the documentation

The modules of the package, other than _runtime, are generated by generator/ from xdr/xdr.json, a description of the network's .x files. xdr/source.toml records where the file comes from. To move to another version:

make update-xdr COMMIT=<full commit hash>
make generate

The tests also check the package against data from the public networks and from the reference implementation. tests/vectors/README.md describes that data and how to update it with a new version.

License

Apache-2.0

Metadata

Release files for stellar-xdr 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stellar-xdr 0.3.0
File Size Uploaded Provenance Actions
stellar_xdr-0.3.0.tar.gz 1.8 MB With provenance Details for stellar_xdr-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for stellar-xdr 0.3.0
File Interpreter ABI Platform Actions
stellar_xdr-0.3.0-py3-none-any.whl Python 3 none any Details for stellar_xdr-0.3.0-py3-none-any.whl

Total release size: 2.1 MB

Release files / stellar_xdr-0.3.0.tar.gz

Download URL stellar_xdr-0.3.0.tar.gz
Size 1.8 MB
Tags Source
SHA-256 checksum
How to use checksums
84aea5c759f2c97ce9a4405e84cd7e83a8f32135f9d32ae885871f08cc6f0600
BLAKE2b-256 checksum
How to use checksums
811130e9fb7b689a2ce78805d3bd9cc9107395babb4370d9851a08de13dfb662
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Release files / stellar_xdr-0.3.0-py3-none-any.whl

Download URL stellar_xdr-0.3.0-py3-none-any.whl
Size 282.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e3ca294fae6f000193e84026e056bab460e0ee00174349d053e822493251a31e
BLAKE2b-256 checksum
How to use checksums
de36549d6b564ff369dd53ecd6f354b5f48175bd32e751a04e56e5e090c249bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Security for stellar-xdr 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for stellar_xdr-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page