Metadata-Version: 2.4
Name: stellar-xdr
Version: 0.2.0
Summary: Strict, fully typed encoding and decoding of Stellar XDR, including SEP-51 XDR-JSON.
Keywords: stellar,xdr,sep-51,soroban
Author: overcat
Author-email: overcat <hi@overcat.dev>
License-Expression: Apache-2.0
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Python :: Free Threading :: 3 - Stable
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Dist: stellar-strkey>=0.2.0,<0.3.0
Requires-Dist: typing-extensions>=4.6 ; python_full_version < '3.12'
Requires-Python: >=3.11
Project-URL: Homepage, https://github.com/lightsail-network/py-stellar-xdr
Project-URL: Documentation, https://stellar-xdr.readthedocs.io/
Project-URL: Repository, https://github.com/lightsail-network/py-stellar-xdr
Project-URL: Issues, https://github.com/lightsail-network/py-stellar-xdr/issues
Project-URL: Changelog, https://github.com/lightsail-network/py-stellar-xdr/blob/main/CHANGELOG.md
Description-Content-Type: text/markdown

# py-stellar-xdr

A Python library to encode and decode [Stellar](https://stellar.org) XDR, the binary
format of transactions, ledger entries, contract values and everything else on the
Stellar network, and its
[SEP-51](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0051.md)
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.

> [!WARNING]
> Releases before 1.0 may still change the API. 1.0 will come with the next major
> version of the Python Stellar SDK, which will use this package for its XDR.

## Install

```shell
pip install stellar-xdr
```

## Usage

```python
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](https://stellar-xdr.readthedocs.io/en/latest/guide/) covers values, unions,
decoding, errors and JSON in more detail.

## Development

The project uses [uv](https://docs.astral.sh/uv/).

```shell
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:

```shell
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](LICENSE)
