From a5682357e39b9e2e82c5839f3f9828030d91c34c Mon Sep 17 00:00:00 2001 From: sumanjeet0012 Date: Sun, 12 Jul 2026 22:13:35 +0530 Subject: [PATCH 1/3] Support complete multihash type mappings in Prefix --- cid/prefix.py | 39 +++++++++++---------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/cid/prefix.py b/cid/prefix.py index 8ff42a1..a8a2328 100644 --- a/cid/prefix.py +++ b/cid/prefix.py @@ -215,40 +215,23 @@ def from_bytes(cls, data: bytes) -> "Prefix": @staticmethod def _mh_type_to_code(mh_type: str) -> int: """Convert multihash type name to code.""" - # Common multihash type codes - # These match the multiformats specification - mh_codes = { - "sha1": 0x11, - "sha2-256": 0x12, - "sha2-512": 0x13, - "sha3-224": 0x17, - "sha3-256": 0x16, - "sha3-512": 0x14, - "blake2b-256": 0xB220, - "blake2b-512": 0xB240, - } - if mh_type not in mh_codes: - msg = f"Unknown multihash type: {mh_type}" - raise ValueError(msg) - return mh_codes[mh_type] + try: + return multihash.Func[mh_type.replace("-", "_")].value + except KeyError: + try: + return multihash.coerce_code(mh_type) + except (ValueError, TypeError): + msg = f"Unknown multihash type: {mh_type}" + raise ValueError(msg) @staticmethod def _mh_code_to_type(mh_code: int) -> str: """Convert multihash code to type name.""" - mh_types = { - 0x11: "sha1", - 0x12: "sha2-256", - 0x13: "sha2-512", - 0x17: "sha3-224", - 0x16: "sha3-256", - 0x14: "sha3-512", - 0xB220: "blake2b-256", - 0xB240: "blake2b-512", - } - if mh_code not in mh_types: + try: + return multihash.Func(mh_code).name.replace("_", "-") + except ValueError: msg = f"Unknown multihash code: {mh_code}" raise ValueError(msg) - return mh_types[mh_code] def __eq__(self, other: object) -> bool: """Check equality with another Prefix.""" From 7d07d6c72cb26e6c340d246d70b54a66dbeb116e Mon Sep 17 00:00:00 2001 From: sumanjeet0012 Date: Mon, 13 Jul 2026 00:20:18 +0530 Subject: [PATCH 2/3] added newsfragment --- newsfragments/65.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/65.bugfix.rst diff --git a/newsfragments/65.bugfix.rst b/newsfragments/65.bugfix.rst new file mode 100644 index 0000000..b18ddbc --- /dev/null +++ b/newsfragments/65.bugfix.rst @@ -0,0 +1 @@ +Delegate multihash type mappings in Prefix to the multihash library. From ee90a9bf351278b46f319e111d8dba9dfce5d5c6 Mon Sep 17 00:00:00 2001 From: sumanjeet0012 Date: Mon, 13 Jul 2026 19:38:15 +0530 Subject: [PATCH 3/3] Fix typechecker lint errors --- cid/prefix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cid/prefix.py b/cid/prefix.py index a8a2328..77efda3 100644 --- a/cid/prefix.py +++ b/cid/prefix.py @@ -216,10 +216,10 @@ def from_bytes(cls, data: bytes) -> "Prefix": def _mh_type_to_code(mh_type: str) -> int: """Convert multihash type name to code.""" try: - return multihash.Func[mh_type.replace("-", "_")].value + return multihash.Func[mh_type.replace("-", "_")].value # type: ignore except KeyError: try: - return multihash.coerce_code(mh_type) + return int(multihash.coerce_code(mh_type)) # type: ignore except (ValueError, TypeError): msg = f"Unknown multihash type: {mh_type}" raise ValueError(msg)