macula-py 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. macula_py-0.1.0/LICENSE +190 -0
  2. macula_py-0.1.0/PKG-INFO +238 -0
  3. macula_py-0.1.0/README.md +214 -0
  4. macula_py-0.1.0/pyproject.toml +42 -0
  5. macula_py-0.1.0/setup.cfg +4 -0
  6. macula_py-0.1.0/src/macula_py/__init__.py +3 -0
  7. macula_py-0.1.0/src/macula_py/blake3_hash.py +34 -0
  8. macula_py-0.1.0/src/macula_py/bolt4.py +80 -0
  9. macula_py-0.1.0/src/macula_py/cbor.py +304 -0
  10. macula_py-0.1.0/src/macula_py/connection.py +456 -0
  11. macula_py-0.1.0/src/macula_py/content.py +150 -0
  12. macula_py-0.1.0/src/macula_py/frame.py +789 -0
  13. macula_py-0.1.0/src/macula_py/identity.py +188 -0
  14. macula_py-0.1.0/src/macula_py/manifest.py +266 -0
  15. macula_py-0.1.0/src/macula_py.egg-info/PKG-INFO +238 -0
  16. macula_py-0.1.0/src/macula_py.egg-info/SOURCES.txt +34 -0
  17. macula_py-0.1.0/src/macula_py.egg-info/dependency_links.txt +1 -0
  18. macula_py-0.1.0/src/macula_py.egg-info/requires.txt +7 -0
  19. macula_py-0.1.0/src/macula_py.egg-info/top_level.txt +1 -0
  20. macula_py-0.1.0/tests/test_blake3_hash.py +53 -0
  21. macula_py-0.1.0/tests/test_bolt4.py +49 -0
  22. macula_py-0.1.0/tests/test_cbor.py +225 -0
  23. macula_py-0.1.0/tests/test_cbor_golden_vectors.py +83 -0
  24. macula_py-0.1.0/tests/test_connection_live.py +58 -0
  25. macula_py-0.1.0/tests/test_content.py +170 -0
  26. macula_py-0.1.0/tests/test_content_live.py +83 -0
  27. macula_py-0.1.0/tests/test_frame.py +222 -0
  28. macula_py-0.1.0/tests/test_frame_call.py +100 -0
  29. macula_py-0.1.0/tests/test_frame_pubsub.py +86 -0
  30. macula_py-0.1.0/tests/test_frame_streaming.py +115 -0
  31. macula_py-0.1.0/tests/test_identity.py +167 -0
  32. macula_py-0.1.0/tests/test_manifest.py +233 -0
  33. macula_py-0.1.0/tests/test_pubsub_live.py +118 -0
  34. macula_py-0.1.0/tests/test_rpc_live.py +110 -0
  35. macula_py-0.1.0/tests/test_stream.py +287 -0
  36. macula_py-0.1.0/tests/test_stream_live.py +127 -0
@@ -0,0 +1,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing
141
+ the origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2026 Macula
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
@@ -0,0 +1,238 @@
1
+ Metadata-Version: 2.4
2
+ Name: macula-py
3
+ Version: 0.1.0
4
+ Summary: Native Python client for the Macula mesh wire protocol -- QUIC transport, deterministic CBOR, Ed25519 identity, BLAKE3 content-addressing, unary RPC, PubSub, content transfer, and streaming RPC (both caller and provider roles).
5
+ Author: Macula
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/macula-io/macula-py
8
+ Project-URL: Repository, https://github.com/macula-io/macula-py
9
+ Keywords: macula,mesh,quic,rpc,pubsub,wire-protocol
10
+ Classifier: License :: OSI Approved :: Apache Software License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Topic :: System :: Networking
13
+ Classifier: Typing :: Typed
14
+ Requires-Python: >=3.11
15
+ Description-Content-Type: text/markdown
16
+ License-File: LICENSE
17
+ Requires-Dist: aioquic>=1.2.0
18
+ Requires-Dist: cryptography>=43.0.0
19
+ Requires-Dist: blake3>=1.0.0
20
+ Provides-Extra: dev
21
+ Requires-Dist: pytest>=8.0.0; extra == "dev"
22
+ Requires-Dist: pytest-asyncio>=0.24.0; extra == "dev"
23
+ Dynamic: license-file
24
+
25
+ # macula-py
26
+
27
+ [![CI](https://img.shields.io/github/actions/workflow/status/macula-io/macula-py/ci.yml?branch=main&label=CI)](https://github.com/macula-io/macula-py/actions/workflows/ci.yml)
28
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](#license)
29
+ [![Python](https://img.shields.io/badge/python-3.11%2B-3776AB?logo=python&logoColor=white)](https://python.org)
30
+ [![GitHub Sponsors](https://img.shields.io/badge/GitHub%20Sponsors-support-ea4aaa.svg?logo=githubsponsors&logoColor=white)](https://github.com/sponsors/rgfaber)
31
+
32
+ <p align="center">
33
+ <picture>
34
+ <source media="(prefers-color-scheme: dark)" srcset="assets/macula-py-full-dark.svg">
35
+ <img src="assets/macula-py-full-light.svg" alt="Macula" width="320">
36
+ </picture>
37
+ </p>
38
+
39
+ <p align="center">
40
+ <strong>Python port of the Macula mesh wire protocol</strong>
41
+ </p>
42
+
43
+ ---
44
+
45
+ ## What is this?
46
+
47
+ A native Python implementation of the client half of Macula's wire
48
+ protocol -- the same protocol [`macula-io/macula`](https://github.com/macula-io/macula)
49
+ (the Erlang/OTP SDK) speaks, and the same protocol
50
+ [`macula-go`](https://github.com/macula-io/macula-go) and
51
+ [`macula-rust`](https://github.com/macula-io/macula-rust) already port.
52
+ Macula is a federated mesh for sovereign, end-to-end-encrypted
53
+ application networks; a **station** is the relay/DHT node, and this
54
+ package is what a **leaf** -- anything that isn't itself a station --
55
+ uses to join it.
56
+
57
+ ## Quick start
58
+
59
+ Also lives as a runnable example -- `python examples/quickstart.py`:
60
+
61
+ ```python
62
+ import asyncio
63
+ import uuid
64
+
65
+ from macula_py import frame
66
+ from macula_py.connection import Session
67
+ from macula_py.identity import KeyPair
68
+
69
+ STATION_HOST = "station-de-frankfurt.macula.io"
70
+ STATION_PORT = 4433
71
+ REALM = bytes(32)
72
+ PROCEDURE = f"macula_py.quickstart_echo.{uuid.uuid4().hex}"
73
+
74
+
75
+ async def main() -> None:
76
+ # Puzzle-hardened identity -- required. An unhardened identity fails
77
+ # the handshake silently (QUIC/TLS looks healthy, HELLO never accepts).
78
+ provider_identity = KeyPair.generate()
79
+ caller_identity = KeyPair.generate()
80
+
81
+ async with await Session.connect(STATION_HOST, STATION_PORT, provider_identity) as provider:
82
+ await provider.advertise(REALM, PROCEDURE)
83
+ await asyncio.sleep(0.5) # ADVERTISE is fire-and-forget; give it a moment to land
84
+
85
+ async def echo(payload):
86
+ return payload
87
+
88
+ serve_task = asyncio.create_task(provider.serve_one_call(lambda realm, proc: echo, timeout=10))
89
+
90
+ async with await Session.connect(STATION_HOST, STATION_PORT, caller_identity) as caller:
91
+ deadline_ms = frame.current_millis() + 5_000
92
+ response = await caller.call(PROCEDURE, REALM, "hello", deadline_ms, timeout=5)
93
+
94
+ await serve_task
95
+ print(response)
96
+
97
+
98
+ asyncio.run(main())
99
+ ```
100
+
101
+ Two identities are used (a provider and a caller) because a station
102
+ kicks a connection the instant a second one arrives under the same
103
+ identity -- the same reason every one of this SDK's own live tests uses
104
+ separate `KeyPair`s for each role.
105
+
106
+ ## Status, 2026-09-05 (phase 1 basics complete, all live-verified)
107
+
108
+ Built and verified in the order every sibling SDK was built in:
109
+
110
+ - **Identity** (`macula_py.identity`) -- Ed25519 keypairs, S/Kademlia
111
+ puzzle-hardened generation (matches `macula_identity.erl`'s own
112
+ default: puzzle-hardened by default, no unhardened shortcut exposed),
113
+ sign/verify, atomic key-file persistence in the exact wire format
114
+ `macula_identity:save/2` uses.
115
+ - **Deterministic CBOR** (`macula_py.cbor`) -- a hand-rolled codec matching
116
+ `macula_record_cbor.erl` exactly, NOT a generic CBOR library (this
117
+ wire's rules diverge from RFC 8949's own canonical form: floats are
118
+ always full binary64 never the shorter widths, map keys sort by their
119
+ own encoded bytes, and there is no boolean simple value on this wire
120
+ at all -- every SDK's convention is 1/0). Verified byte-for-byte
121
+ against the real Erlang encoder itself (see
122
+ `tests/test_cbor_golden_vectors.py`), not just self-consistency.
123
+ - **BLAKE3** (`macula_py.blake3_hash`) -- content-addressing, backed by the
124
+ same Rust `blake3` crate `macula_crypto_nif` uses (not Erlang's own
125
+ pure fallback, which its own source documents as NOT cryptographically
126
+ real BLAKE3). Cross-verified against the real NIF's output.
127
+ - **The frame envelope** (`macula_py.frame`) -- Ed25519-signed frame
128
+ construction/verification and the length-prefixed wire codec, matching
129
+ `macula_frame.erl` exactly, including its frame-level
130
+ boolean-as-text-string convention (distinct from `macula_py.cbor`'s own
131
+ payload-level 1/0 convention -- these are two different rules for two
132
+ different layers, confirmed by reading the Erlang source directly). A
133
+ signed CONNECT frame built entirely by this module was independently
134
+ decoded and signature-verified by the real, unmodified Erlang
135
+ `macula_frame` module -- genuine cross-language wire and cryptographic
136
+ compatibility, not just self-consistency.
137
+ - **QUIC transport + CONNECT/HELLO handshake** (`macula_py.connection`) --
138
+ built on `aioquic`. **Live-verified against the real production
139
+ station fleet** (`station-de-frankfurt.macula.io`): a real handshake
140
+ completes, the HELLO's signature verifies, `accepted` is `true`.
141
+ - **Unary RPC, both roles** (`Session.call`/`Session.advertise`/
142
+ `Session.serve_one_call`) -- BOLT#4 error taxonomy (`macula_py.bolt4`).
143
+ **Live-verified to the standard this org's own SDK work holds real
144
+ proof to**: not just "reached the call stage with a clean
145
+ `unknown_next_peer`" (which only proves the caller's own path works),
146
+ but a genuine advertise+serve+call round trip returning an actual
147
+ RESULT payload from an actual running handler, plus a handler that
148
+ raises correctly reporting `unknown_error` with detail.
149
+ - **PubSub, both roles** (`Session.publish`/`Session.subscribe`/
150
+ `Session.unsubscribe`/`Session.recv_event`). **Live-verified**: a real
151
+ publish/subscribe round trip within one session, a real cross-session
152
+ delivery (separate identities, separate connections), and unsubscribe
153
+ actually stopping delivery. Real finding, root-caused: SUBSCRIBE needs
154
+ a moment to register at the station before a PUBLISH sent immediately
155
+ afterward is reliably delivered -- same shape as RPC's own
156
+ ADVERTISE-needs-a-moment finding, documented in
157
+ `tests/test_pubsub_live.py`.
158
+
159
+ - **Content transfer** (`macula_py.manifest`, `macula_py.content`) -- fixed-size
160
+ chunking, Merkle-root computation, and MCID derivation matching
161
+ `macula_manifest.erl` exactly (cross-verified byte-for-byte against the
162
+ real Erlang implementation for a 3-chunk input, including the
163
+ odd-chunk-paired-with-itself fold case), plus `put`/`get` over the
164
+ `_content.*` RPCs on a dedicated QUIC stream. **Live-verified**: single
165
+ block and chunked (multi-block) put/get round trips, not_found
166
+ handling, and cross-session put/get all pass against the real fleet.
167
+ Found and fixed a genuine bug along the way in **macula-station**
168
+ itself (not this SDK, and not SDK-specific -- it affected every
169
+ client): `_content.put_manifest` crashed with a generic
170
+ `temporary_relay_failure` for any manifest whose `name` wasn't already
171
+ an interned Erlang atom, i.e. any real content name. Root-caused via a
172
+ standalone reproduction against the real unmodified station code,
173
+ fixed at the source (macula-io/macula-station), and confirmed live
174
+ against the production fleet before calling this piece done.
175
+
176
+ - **Streaming RPC, both caller and provider roles** (`Session.open_stream`/
177
+ `Session.accept_stream`, `macula_py.connection.StreamHandle`) --
178
+ STREAM_OPEN/DATA/END/ERROR/REPLY over their own dedicated QUIC stream
179
+ (like content transfer, not the control stream), matching
180
+ `macula_frame.erl`'s constructors and `macula_station_link.erl`'s own
181
+ open/dispatch sequencing exactly, including the `signer`/`responded_by`
182
+ fields non-OPEN stream frames carry for cross-hop authentication.
183
+ Cross-verified in both directions against real Erlang-signed frames
184
+ (this module decodes and signature-verifies frames built by the actual
185
+ `macula_frame:stream_*/1` + `sign/2`; frames this module builds decode
186
+ and verify cleanly against the real Erlang `macula_frame:decode/1` too)
187
+ -- see `tests/test_frame_streaming.py`. A stream procedure is
188
+ advertised exactly like a unary one (`Session.advertise`): the wire's
189
+ ADVERTISE frame doesn't distinguish them, and what disambiguates on
190
+ the receiving end is that STREAM_OPEN always arrives on a fresh
191
+ dedicated stream while CALL always arrives on the control stream.
192
+ **Live-verified**: real `server_stream` AND `client_stream` round
193
+ trips against the production fleet -- provider-pushes-chunks and
194
+ caller-pushes-chunks both confirmed working end to end, including the
195
+ terminal STREAM_REPLY reaching the other side in both directions.
196
+
197
+ `client_stream`/`bidi`'s round trip was `xfail` until 2026-09-05:
198
+ macula-station's own stream-route lifecycle
199
+ (`macula_station_peer_observer.erl`) used to drop the *entire*
200
+ bidirectional route on the first terminal frame it saw for a
201
+ stream_id rather than deciding per the session's actual mode, so a
202
+ `client_stream` caller's own half-close (`STREAM_END(role=send)`)
203
+ tore the route down before the provider's reply could be relayed
204
+ back. Found via this SDK's own live testing, fixed at the source
205
+ (mode-aware half-close semantics, `macula-io/macula-station`
206
+ commit 07db0d8, verified against real production traffic patterns
207
+ before shipping since a naive fix would have broken working
208
+ `server_stream` providers on the fleet), and confirmed live here.
209
+
210
+ **Not yet built**: direct-dial, periodic re-advertise, UCAN, cert-chain
211
+ verification, and the supervised pubsub wrapper are explicitly OUT of
212
+ scope for this first pass, matching the order every other Macula SDK
213
+ was built and reviewed in. RPC telemetry facts are likewise deferred.
214
+
215
+ ## Development
216
+
217
+ ```sh
218
+ python3 -m venv .venv
219
+ .venv/bin/pip install -e ".[dev]"
220
+ .venv/bin/pytest # offline tests only
221
+ .venv/bin/pytest -m live # + live-fleet tests (dials the real production fleet)
222
+ ```
223
+
224
+ This repo pins Python 3.13 via `.tool-versions` -- `aioquic`'s C
225
+ extension dependencies do not currently build against free-threaded
226
+ Python 3.14 builds (confirmed: `pylsqpack`'s limited-API usage hits
227
+ missing internal refcounting symbols under `3.14t`). 3.13 and non-free-
228
+ threaded 3.14 both build cleanly; 3.13 is pinned for reproducibility.
229
+
230
+ ## License
231
+
232
+ Apache-2.0. See [LICENSE](LICENSE).
233
+
234
+ ---
235
+
236
+ <p align="center">
237
+ <sub>Built with the BEAM's protocol, ported to Python -- <a href="https://github.com/sponsors/rgfaber">sponsor the work</a> if this saved you some time</sub>
238
+ </p>
@@ -0,0 +1,214 @@
1
+ # macula-py
2
+
3
+ [![CI](https://img.shields.io/github/actions/workflow/status/macula-io/macula-py/ci.yml?branch=main&label=CI)](https://github.com/macula-io/macula-py/actions/workflows/ci.yml)
4
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](#license)
5
+ [![Python](https://img.shields.io/badge/python-3.11%2B-3776AB?logo=python&logoColor=white)](https://python.org)
6
+ [![GitHub Sponsors](https://img.shields.io/badge/GitHub%20Sponsors-support-ea4aaa.svg?logo=githubsponsors&logoColor=white)](https://github.com/sponsors/rgfaber)
7
+
8
+ <p align="center">
9
+ <picture>
10
+ <source media="(prefers-color-scheme: dark)" srcset="assets/macula-py-full-dark.svg">
11
+ <img src="assets/macula-py-full-light.svg" alt="Macula" width="320">
12
+ </picture>
13
+ </p>
14
+
15
+ <p align="center">
16
+ <strong>Python port of the Macula mesh wire protocol</strong>
17
+ </p>
18
+
19
+ ---
20
+
21
+ ## What is this?
22
+
23
+ A native Python implementation of the client half of Macula's wire
24
+ protocol -- the same protocol [`macula-io/macula`](https://github.com/macula-io/macula)
25
+ (the Erlang/OTP SDK) speaks, and the same protocol
26
+ [`macula-go`](https://github.com/macula-io/macula-go) and
27
+ [`macula-rust`](https://github.com/macula-io/macula-rust) already port.
28
+ Macula is a federated mesh for sovereign, end-to-end-encrypted
29
+ application networks; a **station** is the relay/DHT node, and this
30
+ package is what a **leaf** -- anything that isn't itself a station --
31
+ uses to join it.
32
+
33
+ ## Quick start
34
+
35
+ Also lives as a runnable example -- `python examples/quickstart.py`:
36
+
37
+ ```python
38
+ import asyncio
39
+ import uuid
40
+
41
+ from macula_py import frame
42
+ from macula_py.connection import Session
43
+ from macula_py.identity import KeyPair
44
+
45
+ STATION_HOST = "station-de-frankfurt.macula.io"
46
+ STATION_PORT = 4433
47
+ REALM = bytes(32)
48
+ PROCEDURE = f"macula_py.quickstart_echo.{uuid.uuid4().hex}"
49
+
50
+
51
+ async def main() -> None:
52
+ # Puzzle-hardened identity -- required. An unhardened identity fails
53
+ # the handshake silently (QUIC/TLS looks healthy, HELLO never accepts).
54
+ provider_identity = KeyPair.generate()
55
+ caller_identity = KeyPair.generate()
56
+
57
+ async with await Session.connect(STATION_HOST, STATION_PORT, provider_identity) as provider:
58
+ await provider.advertise(REALM, PROCEDURE)
59
+ await asyncio.sleep(0.5) # ADVERTISE is fire-and-forget; give it a moment to land
60
+
61
+ async def echo(payload):
62
+ return payload
63
+
64
+ serve_task = asyncio.create_task(provider.serve_one_call(lambda realm, proc: echo, timeout=10))
65
+
66
+ async with await Session.connect(STATION_HOST, STATION_PORT, caller_identity) as caller:
67
+ deadline_ms = frame.current_millis() + 5_000
68
+ response = await caller.call(PROCEDURE, REALM, "hello", deadline_ms, timeout=5)
69
+
70
+ await serve_task
71
+ print(response)
72
+
73
+
74
+ asyncio.run(main())
75
+ ```
76
+
77
+ Two identities are used (a provider and a caller) because a station
78
+ kicks a connection the instant a second one arrives under the same
79
+ identity -- the same reason every one of this SDK's own live tests uses
80
+ separate `KeyPair`s for each role.
81
+
82
+ ## Status, 2026-09-05 (phase 1 basics complete, all live-verified)
83
+
84
+ Built and verified in the order every sibling SDK was built in:
85
+
86
+ - **Identity** (`macula_py.identity`) -- Ed25519 keypairs, S/Kademlia
87
+ puzzle-hardened generation (matches `macula_identity.erl`'s own
88
+ default: puzzle-hardened by default, no unhardened shortcut exposed),
89
+ sign/verify, atomic key-file persistence in the exact wire format
90
+ `macula_identity:save/2` uses.
91
+ - **Deterministic CBOR** (`macula_py.cbor`) -- a hand-rolled codec matching
92
+ `macula_record_cbor.erl` exactly, NOT a generic CBOR library (this
93
+ wire's rules diverge from RFC 8949's own canonical form: floats are
94
+ always full binary64 never the shorter widths, map keys sort by their
95
+ own encoded bytes, and there is no boolean simple value on this wire
96
+ at all -- every SDK's convention is 1/0). Verified byte-for-byte
97
+ against the real Erlang encoder itself (see
98
+ `tests/test_cbor_golden_vectors.py`), not just self-consistency.
99
+ - **BLAKE3** (`macula_py.blake3_hash`) -- content-addressing, backed by the
100
+ same Rust `blake3` crate `macula_crypto_nif` uses (not Erlang's own
101
+ pure fallback, which its own source documents as NOT cryptographically
102
+ real BLAKE3). Cross-verified against the real NIF's output.
103
+ - **The frame envelope** (`macula_py.frame`) -- Ed25519-signed frame
104
+ construction/verification and the length-prefixed wire codec, matching
105
+ `macula_frame.erl` exactly, including its frame-level
106
+ boolean-as-text-string convention (distinct from `macula_py.cbor`'s own
107
+ payload-level 1/0 convention -- these are two different rules for two
108
+ different layers, confirmed by reading the Erlang source directly). A
109
+ signed CONNECT frame built entirely by this module was independently
110
+ decoded and signature-verified by the real, unmodified Erlang
111
+ `macula_frame` module -- genuine cross-language wire and cryptographic
112
+ compatibility, not just self-consistency.
113
+ - **QUIC transport + CONNECT/HELLO handshake** (`macula_py.connection`) --
114
+ built on `aioquic`. **Live-verified against the real production
115
+ station fleet** (`station-de-frankfurt.macula.io`): a real handshake
116
+ completes, the HELLO's signature verifies, `accepted` is `true`.
117
+ - **Unary RPC, both roles** (`Session.call`/`Session.advertise`/
118
+ `Session.serve_one_call`) -- BOLT#4 error taxonomy (`macula_py.bolt4`).
119
+ **Live-verified to the standard this org's own SDK work holds real
120
+ proof to**: not just "reached the call stage with a clean
121
+ `unknown_next_peer`" (which only proves the caller's own path works),
122
+ but a genuine advertise+serve+call round trip returning an actual
123
+ RESULT payload from an actual running handler, plus a handler that
124
+ raises correctly reporting `unknown_error` with detail.
125
+ - **PubSub, both roles** (`Session.publish`/`Session.subscribe`/
126
+ `Session.unsubscribe`/`Session.recv_event`). **Live-verified**: a real
127
+ publish/subscribe round trip within one session, a real cross-session
128
+ delivery (separate identities, separate connections), and unsubscribe
129
+ actually stopping delivery. Real finding, root-caused: SUBSCRIBE needs
130
+ a moment to register at the station before a PUBLISH sent immediately
131
+ afterward is reliably delivered -- same shape as RPC's own
132
+ ADVERTISE-needs-a-moment finding, documented in
133
+ `tests/test_pubsub_live.py`.
134
+
135
+ - **Content transfer** (`macula_py.manifest`, `macula_py.content`) -- fixed-size
136
+ chunking, Merkle-root computation, and MCID derivation matching
137
+ `macula_manifest.erl` exactly (cross-verified byte-for-byte against the
138
+ real Erlang implementation for a 3-chunk input, including the
139
+ odd-chunk-paired-with-itself fold case), plus `put`/`get` over the
140
+ `_content.*` RPCs on a dedicated QUIC stream. **Live-verified**: single
141
+ block and chunked (multi-block) put/get round trips, not_found
142
+ handling, and cross-session put/get all pass against the real fleet.
143
+ Found and fixed a genuine bug along the way in **macula-station**
144
+ itself (not this SDK, and not SDK-specific -- it affected every
145
+ client): `_content.put_manifest` crashed with a generic
146
+ `temporary_relay_failure` for any manifest whose `name` wasn't already
147
+ an interned Erlang atom, i.e. any real content name. Root-caused via a
148
+ standalone reproduction against the real unmodified station code,
149
+ fixed at the source (macula-io/macula-station), and confirmed live
150
+ against the production fleet before calling this piece done.
151
+
152
+ - **Streaming RPC, both caller and provider roles** (`Session.open_stream`/
153
+ `Session.accept_stream`, `macula_py.connection.StreamHandle`) --
154
+ STREAM_OPEN/DATA/END/ERROR/REPLY over their own dedicated QUIC stream
155
+ (like content transfer, not the control stream), matching
156
+ `macula_frame.erl`'s constructors and `macula_station_link.erl`'s own
157
+ open/dispatch sequencing exactly, including the `signer`/`responded_by`
158
+ fields non-OPEN stream frames carry for cross-hop authentication.
159
+ Cross-verified in both directions against real Erlang-signed frames
160
+ (this module decodes and signature-verifies frames built by the actual
161
+ `macula_frame:stream_*/1` + `sign/2`; frames this module builds decode
162
+ and verify cleanly against the real Erlang `macula_frame:decode/1` too)
163
+ -- see `tests/test_frame_streaming.py`. A stream procedure is
164
+ advertised exactly like a unary one (`Session.advertise`): the wire's
165
+ ADVERTISE frame doesn't distinguish them, and what disambiguates on
166
+ the receiving end is that STREAM_OPEN always arrives on a fresh
167
+ dedicated stream while CALL always arrives on the control stream.
168
+ **Live-verified**: real `server_stream` AND `client_stream` round
169
+ trips against the production fleet -- provider-pushes-chunks and
170
+ caller-pushes-chunks both confirmed working end to end, including the
171
+ terminal STREAM_REPLY reaching the other side in both directions.
172
+
173
+ `client_stream`/`bidi`'s round trip was `xfail` until 2026-09-05:
174
+ macula-station's own stream-route lifecycle
175
+ (`macula_station_peer_observer.erl`) used to drop the *entire*
176
+ bidirectional route on the first terminal frame it saw for a
177
+ stream_id rather than deciding per the session's actual mode, so a
178
+ `client_stream` caller's own half-close (`STREAM_END(role=send)`)
179
+ tore the route down before the provider's reply could be relayed
180
+ back. Found via this SDK's own live testing, fixed at the source
181
+ (mode-aware half-close semantics, `macula-io/macula-station`
182
+ commit 07db0d8, verified against real production traffic patterns
183
+ before shipping since a naive fix would have broken working
184
+ `server_stream` providers on the fleet), and confirmed live here.
185
+
186
+ **Not yet built**: direct-dial, periodic re-advertise, UCAN, cert-chain
187
+ verification, and the supervised pubsub wrapper are explicitly OUT of
188
+ scope for this first pass, matching the order every other Macula SDK
189
+ was built and reviewed in. RPC telemetry facts are likewise deferred.
190
+
191
+ ## Development
192
+
193
+ ```sh
194
+ python3 -m venv .venv
195
+ .venv/bin/pip install -e ".[dev]"
196
+ .venv/bin/pytest # offline tests only
197
+ .venv/bin/pytest -m live # + live-fleet tests (dials the real production fleet)
198
+ ```
199
+
200
+ This repo pins Python 3.13 via `.tool-versions` -- `aioquic`'s C
201
+ extension dependencies do not currently build against free-threaded
202
+ Python 3.14 builds (confirmed: `pylsqpack`'s limited-API usage hits
203
+ missing internal refcounting symbols under `3.14t`). 3.13 and non-free-
204
+ threaded 3.14 both build cleanly; 3.13 is pinned for reproducibility.
205
+
206
+ ## License
207
+
208
+ Apache-2.0. See [LICENSE](LICENSE).
209
+
210
+ ---
211
+
212
+ <p align="center">
213
+ <sub>Built with the BEAM's protocol, ported to Python -- <a href="https://github.com/sponsors/rgfaber">sponsor the work</a> if this saved you some time</sub>
214
+ </p>