Skip to content
Merged
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
14 changes: 12 additions & 2 deletions multiaddr/codecs/certhash.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, cast
from typing import Any

import multibase
import multihash
Expand Down Expand Up @@ -52,12 +52,22 @@ def to_bytes(self, proto: Any, string: str) -> bytes:
Raises:
ValueError: If the string is not valid multibase or not a multihash.
"""
if not string.startswith("u"):
raise ValueError("certhash must use base64url multibase prefix 'u'")

try:
# Decode the multibase string to get the raw multihash bytes.
decoded_bytes = cast(bytes, multibase.decode(string))
decoded = multibase.decode(string)
except Exception as e:
raise ValueError(f"Failed to decode multibase string: {string}") from e

# Some multibase implementations expose decode metadata as a tuple.
# Normalize to raw bytes for consistent validation and return type.
decoded_bytes = decoded[1] if isinstance(decoded, tuple) else decoded
if not isinstance(decoded_bytes, (bytes, bytearray)):
raise ValueError("Failed to decode multibase string to bytes")
decoded_bytes = bytes(decoded_bytes)

# Validate that the decoded bytes are a valid multihash.
self.validate(decoded_bytes)
return decoded_bytes
Expand Down
13 changes: 0 additions & 13 deletions multiaddr/codecs/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,3 @@ def to_string(self, proto: Any, buf: bytes) -> str:
return value
except (UnicodeDecodeError, idna.IDNAError) as e:
raise BinaryParseError(f"Invalid domain name encoding: {e!s}", buf, proto.name, e)


def to_bytes(proto: Any, string: str) -> bytes:
# Validate using IDNA, but store as UTF-8
idna.encode(string, uts46=True)
return string.encode("utf-8")


def to_string(proto: Any, buf: bytes) -> str:
string = buf.decode("utf-8")
# Validate using IDNA
idna.encode(string, uts46=True)
return string
2 changes: 2 additions & 0 deletions newsfragments/100.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Removed the module-level ``to_bytes`` and ``to_string`` helpers from ``multiaddr.codecs.domain``.
Domain codec behavior remains available through ``multiaddr.codecs.domain.Codec``.
Loading