Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 11 additions & 28 deletions cid/prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
1 change: 1 addition & 0 deletions newsfragments/65.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Delegate multihash type mappings in Prefix to the multihash library.
Loading