tba-codec-dag-cbor 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,51 @@
1
+ Metadata-Version: 2.4
2
+ Name: tba-codec-dag-cbor
3
+ Version: 0.4.0.dev2
4
+ Summary: Strict canonical DAG-CBOR with IPLD CID links.
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
+ Requires-Dist: cbor2 (==6.1.3)
14
+ Requires-Dist: tba-codec-cid (==0.4.0.dev2)
15
+ Description-Content-Type: text/markdown
16
+
17
+ # tba-codec-dag-cbor
18
+
19
+ <!-- BEGIN TBA README HEADER -->
20
+ <p align="center">
21
+ <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>
22
+ </p>
23
+ <p align="center">
24
+ <a href="https://github.com/tigrbl/tigrbl_auth/blob/master/pkgs/03-codecs/tba-codec-dag-cbor/pyproject.toml"><img alt="Checkout: 0.4.0.dev2" src="https://img.shields.io/badge/Checkout-0.4.0.dev2-1f6feb"></a>
25
+ <a href="https://github.com/tigrbl/tigrbl_auth/blob/master/pkgs/03-codecs/tba-codec-dag-cbor/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>
26
+ <a href="https://github.com/tigrbl/tigrbl_auth/blob/master/pkgs/03-codecs/tba-codec-dag-cbor/pyproject.toml"><img alt="License: Apache-2.0" src="https://img.shields.io/badge/License-Apache--2.0-525252"></a>
27
+ <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>
28
+ <a href="https://pypi.org/project/tba-codec-dag-cbor/"><img alt="PyPI version for tba-codec-dag-cbor" src="https://img.shields.io/pypi/v/tba-codec-dag-cbor?label=tba-codec-dag-cbor%20PyPI"></a>
29
+ </p>
30
+ <p align="center">
31
+ <a href="https://pepy.tech/project/tba-codec-dag-cbor"><img alt="Monthly PyPI downloads for tba-codec-dag-cbor" src="https://static.pepy.tech/badge/tba-codec-dag-cbor/month"></a>
32
+ <a href="https://github.com/tigrbl/tigrbl_auth/blob/master/pkgs/03-codecs/tba-codec-dag-cbor/README.md#verification"><img alt="Evidence: verification" src="https://img.shields.io/badge/Evidence-verification-8250df"></a>
33
+ </p>
34
+ <!-- END TBA README HEADER -->
35
+
36
+ Use `encode` and `decode` for canonical DAG-CBOR. CID links use tag 42 and the binary identity prefix. Decoding rejects duplicate keys, trailing data and noncanonical encodings. Callers set message size limits and protocol constraints.
37
+
38
+ [Specification](https://ipld.io/specs/codecs/dag-cbor/spec/)
39
+
40
+ ## Quick start
41
+
42
+ Build the package from the repository root.
43
+
44
+ ```bash
45
+ uv build --wheel pkgs/03-codecs/tba-codec-dag-cbor
46
+ ```
47
+
48
+ ## Verification
49
+
50
+ Check that the build produces a wheel with the package version declared in `pyproject.toml`.
51
+
@@ -0,0 +1,34 @@
1
+ # tba-codec-dag-cbor
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-dag-cbor/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-dag-cbor/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-dag-cbor/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-dag-cbor/"><img alt="PyPI version for tba-codec-dag-cbor" src="https://img.shields.io/pypi/v/tba-codec-dag-cbor?label=tba-codec-dag-cbor%20PyPI"></a>
13
+ </p>
14
+ <p align="center">
15
+ <a href="https://pepy.tech/project/tba-codec-dag-cbor"><img alt="Monthly PyPI downloads for tba-codec-dag-cbor" src="https://static.pepy.tech/badge/tba-codec-dag-cbor/month"></a>
16
+ <a href="https://github.com/tigrbl/tigrbl_auth/blob/master/pkgs/03-codecs/tba-codec-dag-cbor/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
+ Use `encode` and `decode` for canonical DAG-CBOR. CID links use tag 42 and the binary identity prefix. Decoding rejects duplicate keys, trailing data and noncanonical encodings. Callers set message size limits and protocol constraints.
21
+
22
+ [Specification](https://ipld.io/specs/codecs/dag-cbor/spec/)
23
+
24
+ ## Quick start
25
+
26
+ Build the package from the repository root.
27
+
28
+ ```bash
29
+ uv build --wheel pkgs/03-codecs/tba-codec-dag-cbor
30
+ ```
31
+
32
+ ## Verification
33
+
34
+ Check that the build produces a wheel with the package version declared in `pyproject.toml`.
@@ -0,0 +1,26 @@
1
+ [project]
2
+ name = "tba-codec-dag-cbor"
3
+ version = "0.4.0.dev2"
4
+ description = "Strict canonical DAG-CBOR with IPLD CID links."
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
+ "cbor2==6.1.3",
18
+ "tba-codec-cid==0.4.0.dev2",
19
+ ]
20
+
21
+ [build-system]
22
+ requires = ["poetry-core>=1.0.0"]
23
+ build-backend = "poetry.core.masonry.api"
24
+
25
+ [tool.poetry]
26
+ packages = [{ include = "tba_codec_dag_cbor", from = "src" }]
@@ -0,0 +1,82 @@
1
+ """Strict canonical DAG-CBOR with CID links."""
2
+
3
+ import math
4
+ import struct
5
+
6
+ import cbor2
7
+ from cbor2 import CBORTag
8
+ from tba_codec_cid import Cid
9
+
10
+
11
+ def cid_link(cid: Cid) -> CBORTag:
12
+ """Encode an IPLD link as CBOR tag 42 with the binary identity prefix."""
13
+ return CBORTag(42, b"\x00" + cid.to_bytes())
14
+
15
+
16
+ def link_cid(link: object) -> Cid:
17
+ if (
18
+ not isinstance(link, CBORTag)
19
+ or link.tag != 42
20
+ or type(link.value) is not bytes
21
+ or not link.value.startswith(b"\x00")
22
+ ):
23
+ raise ValueError("expected a DAG-CBOR CID link")
24
+ return Cid.from_bytes(link.value[1:])
25
+
26
+
27
+ def _validate(value: object, depth: int) -> None:
28
+ if depth <= 0:
29
+ raise ValueError("DAG-CBOR nesting limit exceeded")
30
+ if value is None or type(value) in (bool, str, bytes):
31
+ return
32
+ if type(value) is int and -(2**64) <= value < 2**64:
33
+ return
34
+ if type(value) is float and math.isfinite(value):
35
+ return
36
+ if type(value) in (list, tuple):
37
+ for item in value:
38
+ _validate(item, depth - 1)
39
+ return
40
+ if type(value) is dict and all(type(key) is str for key in value):
41
+ for item in value.values():
42
+ _validate(item, depth - 1)
43
+ return
44
+ if isinstance(value, CBORTag):
45
+ link_cid(value)
46
+ return
47
+ raise ValueError("unsupported DAG-CBOR value")
48
+
49
+
50
+ def _encode_float(encoder: cbor2.CBOREncoder, value: float) -> None:
51
+ # DAG-CBOR requires binary64, even when CBOR could use fewer bytes.
52
+ encoder.write(b"\xfb" + struct.pack(">d", value or 0.0))
53
+
54
+
55
+ def encode(value: object, *, max_depth: int = 64) -> bytes:
56
+ """Encode a value using canonical DAG-CBOR."""
57
+ _validate(value, max_depth)
58
+ try:
59
+ return cbor2.dumps(value, canonical=True, encoders={float: _encode_float})
60
+ except (cbor2.CBORError, TypeError, UnicodeError) as exc:
61
+ raise ValueError("cannot encode DAG-CBOR value") from exc
62
+
63
+
64
+ def decode(wire: bytes, *, max_depth: int = 64) -> object:
65
+ """Decode a single DAG-CBOR value and reject alternate serializations."""
66
+ if type(wire) is not bytes:
67
+ raise ValueError("expected a DAG-CBOR byte string")
68
+ try:
69
+ value = cbor2.loads(
70
+ wire,
71
+ max_depth=max_depth,
72
+ allow_indefinite=False,
73
+ allow_duplicate_keys=False,
74
+ )
75
+ except (cbor2.CBORError, ValueError, TypeError) as exc:
76
+ raise ValueError("invalid DAG-CBOR") from exc
77
+ if encode(value, max_depth=max_depth) != wire:
78
+ raise ValueError("noncanonical DAG-CBOR")
79
+ return value
80
+
81
+
82
+ __all__ = ["CBORTag", "cid_link", "decode", "encode", "link_cid"]