Skip to content

feat(python): add a CPython binding + build for the uUR module#10

Open
kdmukAI-bot wants to merge 1 commit into
odudex:mainfrom
kdmukAI-bot:feat/cpython-binding
Open

feat(python): add a CPython binding + build for the uUR module#10
kdmukAI-bot wants to merge 1 commit into
odudex:mainfrom
kdmukAI-bot:feat/cpython-binding

Conversation

@kdmukAI-bot

Copy link
Copy Markdown
Contributor

Add a CPython binding for the uUR module

This adds a CPython counterpart to the existing MicroPython uUR.c wrapper, so the native BC-UR encoder/decoder is importable as uUR on host/desktop CPython (e.g. Raspberry Pi), mirroring the module the ESP32/MicroPython firmware already exposes. Business logic that does import uUR then works identically on both runtimes.

What's here

  • python/uUR.c — the full uUR surface via the CPython C-API: UR, URDecoder, UREncoder, FountainEncoder, the Types CBOR-codec namespace (bytes/psbt/bip39/output + tag/type constants), and the DECODER_* state constants. It wraps the ur_decoder_get_state() state-machine API and returns the FountainEncoder view uncached (leak-free under refcounting; avoids a parent↔view reference cycle — behaviorally identical to the cached MicroPython view).
  • setup.py + pyproject.toml — a pip-installable build of the uUR extension from the portable core (envelope + types + the bundled SHA-256; no OpenSSL/mbedTLS). py-modules = [] keeps setuptools from misreading the C src/ dir as a Python src-layout package.
  • .gitignore — ignore the CPython build artifacts.

Notes

  • Host build uses the bundled src/sha256/sha256.c (UR_USE_MBEDTLS_SHA256 unset), so there's no external crypto dependency.
  • Kept intentionally in sync with the module contract of uUR.c so the two runtimes present one API.

Validation

  • pip install . builds + imports cleanly; a full multi-part fountain encode→decode roundtrip plus the Types codecs pass on x86-64 CPython 3.13 and, on-device, on ARMv6 CPython 3.10 (Raspberry Pi Zero).

🤖 Generated with Claude Code

Adds a CPython counterpart to the MicroPython uUR.c wrapper so the native
BC-UR encoder/decoder is importable as `uUR` on CPython (e.g. Raspberry Pi),
mirroring the ESP32 firmware's native module. Same module surface as the
MicroPython side, so host business logic that does `import uUR` works on both
runtimes.

- python/uUR.c: the full uUR surface via the CPython C-API — UR, URDecoder,
  UREncoder, FountainEncoder, the Types CBOR-codec namespace, and the DECODER_*
  state constants. Wraps the ur_decoder_get_state() state-machine API. The
  FountainEncoder view is returned uncached to avoid a parent<->view reference
  cycle (behaviorally identical to the cached MicroPython view; leak-free under
  refcounting).
- setup.py + pyproject.toml: a pip-installable build of the `uUR` extension
  from the portable core (envelope + types + bundled SHA-256; no OpenSSL).
  `py-modules = []` stops setuptools from misreading the C `src/` dir as a
  Python src-layout package.
- .gitignore: ignore the CPython build artifacts (*.so, egg-info, dist).

Host build uses the bundled src/sha256/sha256.c (UR_USE_MBEDTLS_SHA256 unset),
so there is no external crypto dependency.

Co-Authored-By: kdmukai <934746+kdmukai@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant