tba-codec-multibase 0.4.0.dev2__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.
@@ -0,0 +1,55 @@
1
+ Metadata-Version: 2.4
2
+ Name: tba-codec-multibase
3
+ Version: 0.4.0.dev2
4
+ Summary: Multibase encoding and decoding for the base58btc alphabet.
5
+ License-Expression: Apache-2.0
6
+ Requires-Python: >=3.10,<3.16
7
+ Classifier: Programming Language :: Python :: 3.10
8
+ Classifier: Programming Language :: Python :: 3.11
9
+ Classifier: Programming Language :: Python :: 3.12
10
+ Classifier: Programming Language :: Python :: 3.13
11
+ Classifier: Programming Language :: Python :: 3.14
12
+ Classifier: Programming Language :: Python :: 3.15
13
+ Description-Content-Type: text/markdown
14
+
15
+ # tba-codec-multibase
16
+
17
+ <!-- BEGIN TBA README HEADER -->
18
+ <p align="center">
19
+ <a href="https://github.com/tigrbl/tigrbl_auth#readme"><img src="https://raw.githubusercontent.com/tigrbl/tigrbl_auth/master/docs/assets/tba-brand.png" alt="TBA · Tigrbl Auth" width="240"></a>
20
+ </p>
21
+ <p align="center">
22
+ <a href="https://github.com/tigrbl/tigrbl_auth/blob/master/pkgs/03-codecs/tba-codec-multibase/pyproject.toml"><img alt="Checkout: 0.4.0.dev2" src="https://img.shields.io/badge/Checkout-0.4.0.dev2-1f6feb"></a>
23
+ <a href="https://github.com/tigrbl/tigrbl_auth/blob/master/pkgs/03-codecs/tba-codec-multibase/pyproject.toml"><img alt="Python: &gt;=3.10,&lt;3.16" src="https://img.shields.io/badge/Python-%3E%3D3.10%2C%3C3.16-3776ab"></a>
24
+ <a href="https://github.com/tigrbl/tigrbl_auth/blob/master/pkgs/03-codecs/tba-codec-multibase/pyproject.toml"><img alt="License: Apache-2.0" src="https://img.shields.io/badge/License-Apache--2.0-525252"></a>
25
+ <a href="https://github.com/tigrbl/tigrbl_auth/tree/master/pkgs/03-codecs"><img alt="Layer: 03 codecs" src="https://img.shields.io/badge/Layer-03%20codecs-0a7f5a"></a>
26
+ <a href="https://pypi.org/project/tba-codec-multibase/"><img alt="PyPI version for tba-codec-multibase" src="https://img.shields.io/pypi/v/tba-codec-multibase?label=tba-codec-multibase%20PyPI"></a>
27
+ </p>
28
+ <p align="center">
29
+ <a href="https://pepy.tech/project/tba-codec-multibase"><img alt="Monthly PyPI downloads for tba-codec-multibase" src="https://static.pepy.tech/badge/tba-codec-multibase/month"></a>
30
+ <a href="https://github.com/tigrbl/tigrbl_auth/blob/master/pkgs/03-codecs/tba-codec-multibase/README.md#verification"><img alt="Evidence: verification" src="https://img.shields.io/badge/Evidence-verification-8250df"></a>
31
+ </p>
32
+ <!-- END TBA README HEADER -->
33
+
34
+ Dependency-free Python codec for base58btc Multibase values. Use
35
+ `encode_base58btc(bytes)` and `decode_base58btc(str)`. Both functions handle
36
+ the lowercase `z` prefix and preserve leading zero bytes.
37
+
38
+ The codec converts bytes and text. Its callers set input size limits for
39
+ untrusted data and validate decoded values according to their protocols.
40
+
41
+ See the [Multibase registry](https://github.com/multiformats/multibase)
42
+ and [Base58 test vectors](https://datatracker.ietf.org/doc/html/draft-msporny-base58-02#section-5).
43
+
44
+ ## Quick start
45
+
46
+ Build the package from the repository root.
47
+
48
+ ```bash
49
+ uv build --wheel pkgs/03-codecs/tba-codec-multibase
50
+ ```
51
+
52
+ ## Verification
53
+
54
+ Check that the build produces a wheel with the package version declared in `pyproject.toml`.
55
+
@@ -0,0 +1,40 @@
1
+ # tba-codec-multibase
2
+
3
+ <!-- BEGIN TBA README HEADER -->
4
+ <p align="center">
5
+ <a href="https://github.com/tigrbl/tigrbl_auth#readme"><img src="https://raw.githubusercontent.com/tigrbl/tigrbl_auth/master/docs/assets/tba-brand.png" alt="TBA · Tigrbl Auth" width="240"></a>
6
+ </p>
7
+ <p align="center">
8
+ <a href="https://github.com/tigrbl/tigrbl_auth/blob/master/pkgs/03-codecs/tba-codec-multibase/pyproject.toml"><img alt="Checkout: 0.4.0.dev2" src="https://img.shields.io/badge/Checkout-0.4.0.dev2-1f6feb"></a>
9
+ <a href="https://github.com/tigrbl/tigrbl_auth/blob/master/pkgs/03-codecs/tba-codec-multibase/pyproject.toml"><img alt="Python: &gt;=3.10,&lt;3.16" src="https://img.shields.io/badge/Python-%3E%3D3.10%2C%3C3.16-3776ab"></a>
10
+ <a href="https://github.com/tigrbl/tigrbl_auth/blob/master/pkgs/03-codecs/tba-codec-multibase/pyproject.toml"><img alt="License: Apache-2.0" src="https://img.shields.io/badge/License-Apache--2.0-525252"></a>
11
+ <a href="https://github.com/tigrbl/tigrbl_auth/tree/master/pkgs/03-codecs"><img alt="Layer: 03 codecs" src="https://img.shields.io/badge/Layer-03%20codecs-0a7f5a"></a>
12
+ <a href="https://pypi.org/project/tba-codec-multibase/"><img alt="PyPI version for tba-codec-multibase" src="https://img.shields.io/pypi/v/tba-codec-multibase?label=tba-codec-multibase%20PyPI"></a>
13
+ </p>
14
+ <p align="center">
15
+ <a href="https://pepy.tech/project/tba-codec-multibase"><img alt="Monthly PyPI downloads for tba-codec-multibase" src="https://static.pepy.tech/badge/tba-codec-multibase/month"></a>
16
+ <a href="https://github.com/tigrbl/tigrbl_auth/blob/master/pkgs/03-codecs/tba-codec-multibase/README.md#verification"><img alt="Evidence: verification" src="https://img.shields.io/badge/Evidence-verification-8250df"></a>
17
+ </p>
18
+ <!-- END TBA README HEADER -->
19
+
20
+ Dependency-free Python codec for base58btc Multibase values. Use
21
+ `encode_base58btc(bytes)` and `decode_base58btc(str)`. Both functions handle
22
+ the lowercase `z` prefix and preserve leading zero bytes.
23
+
24
+ The codec converts bytes and text. Its callers set input size limits for
25
+ untrusted data and validate decoded values according to their protocols.
26
+
27
+ See the [Multibase registry](https://github.com/multiformats/multibase)
28
+ and [Base58 test vectors](https://datatracker.ietf.org/doc/html/draft-msporny-base58-02#section-5).
29
+
30
+ ## Quick start
31
+
32
+ Build the package from the repository root.
33
+
34
+ ```bash
35
+ uv build --wheel pkgs/03-codecs/tba-codec-multibase
36
+ ```
37
+
38
+ ## Verification
39
+
40
+ Check that the build produces a wheel with the package version declared in `pyproject.toml`.
@@ -0,0 +1,23 @@
1
+ [project]
2
+ name = "tba-codec-multibase"
3
+ version = "0.4.0.dev2"
4
+ description = "Multibase encoding and decoding for the base58btc alphabet."
5
+ readme = "README.md"
6
+ requires-python = ">=3.10,<3.16"
7
+ classifiers = [
8
+ "Programming Language :: Python :: 3.10",
9
+ "Programming Language :: Python :: 3.11",
10
+ "Programming Language :: Python :: 3.12",
11
+ "Programming Language :: Python :: 3.13",
12
+ "Programming Language :: Python :: 3.14",
13
+ "Programming Language :: Python :: 3.15",
14
+ ]
15
+ license = "Apache-2.0"
16
+ dependencies = []
17
+
18
+ [build-system]
19
+ requires = ["poetry-core>=1.0.0"]
20
+ build-backend = "poetry.core.masonry.api"
21
+
22
+ [tool.poetry]
23
+ packages = [{ include = "tba_codec_multibase", from = "src" }]
@@ -0,0 +1,35 @@
1
+ """Multibase encoding and decoding for the base58btc alphabet."""
2
+
3
+ _ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
4
+ _DIGITS = {char: digit for digit, char in enumerate(_ALPHABET)}
5
+ _PREFIX = "z"
6
+
7
+
8
+ def encode_base58btc(data: bytes) -> str:
9
+ """Encode bytes as a z-prefixed base58btc Multibase value."""
10
+ number = int.from_bytes(data, "big")
11
+ digits = []
12
+ while number:
13
+ number, digit = divmod(number, 58)
14
+ digits.append(_ALPHABET[digit])
15
+ zeros = len(data) - len(data.lstrip(b"\x00"))
16
+ return _PREFIX + "1" * zeros + "".join(reversed(digits))
17
+
18
+
19
+ def decode_base58btc(text: str) -> bytes:
20
+ """Decode a z-prefixed base58btc Multibase value or raise ValueError."""
21
+ if not isinstance(text, str) or not text.startswith(_PREFIX):
22
+ raise ValueError("expected a z-prefixed base58btc value")
23
+
24
+ payload = text[1:]
25
+ zeros = len(payload) - len(payload.lstrip("1"))
26
+ number = 0
27
+ try:
28
+ for char in payload[zeros:]:
29
+ number = number * 58 + _DIGITS[char]
30
+ except KeyError as exc:
31
+ raise ValueError("invalid base58btc character") from exc
32
+ return b"\x00" * zeros + number.to_bytes((number.bit_length() + 7) // 8, "big")
33
+
34
+
35
+ __all__ = ["decode_base58btc", "encode_base58btc"]