Skip to content

Commit 34d8210

Browse files
ItshMohacul71cursoragent
authored
feat: Bitswap py-cid Migration Phase 1 + Phase 2 (#1235)
* the helper function required for proper conversion the terms to the CID object * pushed the builder part * pushed the new functions for compute cid v0 or cid v1 * removed the unused functions * fix: PR 1235 – add newsfragment, type aliases, narrow exceptions, parse_cid docstring - Add newsfragments/1234.feature.rst for issue #1234 - Fix mypy: CIDInput/CIDObject as TypeAlias in libp2p/bitswap/cid.py - Narrow except Exception to ValueError in get_cid_prefix, reconstruct_cid_from_prefix_and_data, verify_cid, parse_cid_version, parse_cid_codec - Docstring: parse_cid accepts hex with or without 0x Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: acul71 <34693171+acul71@users.noreply.github.com> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent def1658 commit 34d8210

File tree

6 files changed

+229
-437
lines changed

6 files changed

+229
-437
lines changed

examples/cid/cid_usage.py

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
from libp2p.bitswap.cid import (
1010
CODEC_DAG_PB,
1111
CODEC_RAW,
12-
analyze_cid_collection,
1312
compute_cid_v0,
1413
compute_cid_v1,
15-
detect_cid_encoding_format,
16-
recompute_cid_from_data,
14+
compute_cid_v1_obj,
15+
parse_cid_codec,
16+
parse_cid_version,
1717
verify_cid,
1818
)
1919

@@ -44,33 +44,17 @@ def main() -> None:
4444
("CIDv1 raw", cid_v1_raw),
4545
("CIDv1 dag-pb", cid_v1_dag_pb),
4646
]:
47-
info = detect_cid_encoding_format(cid)
48-
if "error" in info:
49-
logger.info("%s: %s", name, info["error"])
50-
else:
51-
logger.info(
52-
"%s: version=%s, codec=%s, encoding=%s, is_breaking=%s",
53-
name,
54-
info["version"],
55-
info["codec_name"],
56-
info["encoding"],
57-
info["is_breaking"],
58-
)
59-
60-
try:
61-
recomputed = recompute_cid_from_data(cid_v1_raw, data)
62-
logger.info("Recomputed CID matches original: %s", cid_v1_raw == recomputed)
63-
except ValueError as e:
64-
logger.info("Recompute error: %s", e)
65-
66-
collection = [cid_v0, cid_v1_raw, cid_v1_dag_pb]
67-
analysis = analyze_cid_collection(collection)
47+
logger.info(
48+
"%s: version=%s, codec=%s",
49+
name,
50+
parse_cid_version(cid),
51+
parse_cid_codec(cid),
52+
)
53+
54+
cid_obj = compute_cid_v1_obj(data, codec=CODEC_RAW)
55+
logger.info("CIDv1 object text form: %s", cid_obj)
6856
logger.info(
69-
"Collection: total=%s, backward_compatible=%s, breaking_change=%s, by_codec=%s",
70-
analysis["total"],
71-
analysis["backward_compatible"],
72-
analysis["breaking_change"],
73-
analysis["by_codec"],
57+
"CIDv1 object bytes == compute_cid_v1 bytes: %s", cid_obj.buffer == cid_v1_raw
7458
)
7559

7660

libp2p/bitswap/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@
1111
from . import config
1212
from .cid import (
1313
compute_cid,
14+
compute_cid_obj,
1415
compute_cid_v0,
16+
compute_cid_v0_obj,
1517
compute_cid_v1,
18+
compute_cid_v1_obj,
19+
parse_cid,
20+
cid_to_bytes,
21+
cid_to_text,
1622
get_cid_prefix,
1723
reconstruct_cid_from_prefix_and_data,
1824
verify_cid,
@@ -41,8 +47,14 @@
4147
"config",
4248
# CID utilities
4349
"compute_cid",
50+
"compute_cid_obj",
4451
"compute_cid_v0",
52+
"compute_cid_v0_obj",
4553
"compute_cid_v1",
54+
"compute_cid_v1_obj",
55+
"parse_cid",
56+
"cid_to_bytes",
57+
"cid_to_text",
4658
"get_cid_prefix",
4759
"reconstruct_cid_from_prefix_and_data",
4860
"verify_cid",

0 commit comments

Comments
 (0)