multiaddr 0.0.12__tar.gz → 0.1.1__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.
Files changed (47) hide show
  1. {multiaddr-0.0.12 → multiaddr-0.1.1}/HISTORY.rst +28 -0
  2. {multiaddr-0.0.12 → multiaddr-0.1.1}/PKG-INFO +4 -2
  3. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/__init__.py +1 -1
  4. multiaddr-0.1.1/multiaddr/codecs/certhash.py +85 -0
  5. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/multiaddr.py +92 -48
  6. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/protocols.py +8 -0
  7. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr.egg-info/PKG-INFO +4 -2
  8. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr.egg-info/SOURCES.txt +1 -0
  9. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr.egg-info/requires.txt +2 -0
  10. {multiaddr-0.0.12 → multiaddr-0.1.1}/pyproject.toml +5 -3
  11. {multiaddr-0.0.12 → multiaddr-0.1.1}/tests/test_multiaddr.py +93 -0
  12. {multiaddr-0.0.12 → multiaddr-0.1.1}/tests/test_protocols.py +67 -1
  13. {multiaddr-0.0.12 → multiaddr-0.1.1}/tests/test_thin_waist_addresses.py +0 -9
  14. {multiaddr-0.0.12 → multiaddr-0.1.1}/AUTHORS +0 -0
  15. {multiaddr-0.0.12 → multiaddr-0.1.1}/CONTRIBUTING.rst +0 -0
  16. {multiaddr-0.0.12 → multiaddr-0.1.1}/LICENSE-APACHE2 +0 -0
  17. {multiaddr-0.0.12 → multiaddr-0.1.1}/LICENSE-MIT +0 -0
  18. {multiaddr-0.0.12 → multiaddr-0.1.1}/MANIFEST.in +0 -0
  19. {multiaddr-0.0.12 → multiaddr-0.1.1}/README.rst +0 -0
  20. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/codecs/__init__.py +0 -0
  21. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/codecs/cid.py +0 -0
  22. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/codecs/domain.py +0 -0
  23. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/codecs/fspath.py +0 -0
  24. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/codecs/garlic32.py +0 -0
  25. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/codecs/garlic64.py +0 -0
  26. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/codecs/http_path.py +0 -0
  27. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/codecs/ip4.py +0 -0
  28. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/codecs/ip6.py +0 -0
  29. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/codecs/ipcidr.py +0 -0
  30. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/codecs/memory.py +0 -0
  31. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/codecs/onion.py +0 -0
  32. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/codecs/onion3.py +0 -0
  33. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/codecs/uint16be.py +0 -0
  34. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/codecs/utf8.py +0 -0
  35. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/exceptions.py +0 -0
  36. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/resolvers/__init__.py +0 -0
  37. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/resolvers/base.py +0 -0
  38. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/resolvers/dns.py +0 -0
  39. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/transforms.py +0 -0
  40. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr/utils.py +0 -0
  41. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr.egg-info/dependency_links.txt +0 -0
  42. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr.egg-info/not-zip-safe +0 -0
  43. {multiaddr-0.0.12 → multiaddr-0.1.1}/multiaddr.egg-info/top_level.txt +0 -0
  44. {multiaddr-0.0.12 → multiaddr-0.1.1}/setup.cfg +0 -0
  45. {multiaddr-0.0.12 → multiaddr-0.1.1}/tests/__init__.py +0 -0
  46. {multiaddr-0.0.12 → multiaddr-0.1.1}/tests/test_resolvers.py +0 -0
  47. {multiaddr-0.0.12 → multiaddr-0.1.1}/tests/test_transforms.py +0 -0
@@ -3,6 +3,34 @@ History
3
3
 
4
4
  .. towncrier release notes start
5
5
 
6
+ py-multiaddr v0.1.1 (2025-12-07)
7
+ --------------------------------
8
+
9
+ Bugfixes
10
+ ~~~~~~~~
11
+
12
+ - Fixed validation for tag-only protocols (protocols that do not accept values).
13
+ Tag-only protocols like ``http``, ``https``, ``tls``, ``noise``, ``webrtc``, etc.
14
+ now correctly reject invalid value assignments via both ``/tag/value`` and
15
+ ``/tag=value`` syntax, raising clear error messages that do not include the
16
+ invalid value. (`#98 <https://github.com/multiformats/py-multiaddr/issues/98>`__)
17
+
18
+
19
+ py-multiaddr v0.1.0 (2025-10-27)
20
+ --------------------------------
21
+
22
+ Features
23
+ ~~~~~~~~
24
+
25
+ - Added the following protocols in reference with go-multiaddr
26
+
27
+ - SNI: 0x01C1
28
+ - NOISE: 0x01C6
29
+ - CERTHASH:
30
+ - WEBRTC:
31
+ - WEBRTC-DIRECT: (`#181 <https://github.com/multiformats/py-multiaddr/issues/181>`__)
32
+
33
+
6
34
  py-multiaddr v0.0.12 (2025-10-21)
7
35
  ---------------------------------
8
36
 
@@ -1,13 +1,13 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: multiaddr
3
- Version: 0.0.12
3
+ Version: 0.1.1
4
4
  Summary: Python implementation of jbenet's multiaddr
5
5
  Author-email: Steven Buss <steven.buss@gmail.com>
6
6
  Maintainer: acul71
7
7
  Maintainer-email: pacrob <pacrob@protonmail.com>, Manu Sheel Gupta <manu@seeta.in>
8
8
  License: MIT License
9
9
  Project-URL: Homepage, https://github.com/multiformats/py-multiaddr
10
- Project-URL: Download, https://github.com/multiformats/py-multiaddr/tarball/0.0.12
10
+ Project-URL: Download, https://github.com/multiformats/py-multiaddr/tarball/0.1.1
11
11
  Keywords: multiaddr
12
12
  Classifier: Development Status :: 2 - Pre-Alpha
13
13
  Classifier: Intended Audience :: Developers
@@ -30,6 +30,8 @@ Requires-Dist: netaddr
30
30
  Requires-Dist: psutil
31
31
  Requires-Dist: py-cid>=0.3.1
32
32
  Requires-Dist: py-multicodec>=0.2.0
33
+ Requires-Dist: py-multibase
34
+ Requires-Dist: py-multihash
33
35
  Requires-Dist: trio-typing>=0.0.4
34
36
  Requires-Dist: trio>=0.26.0
35
37
  Requires-Dist: varint
@@ -2,4 +2,4 @@ from .multiaddr import Multiaddr # NOQA
2
2
 
3
3
  __author__ = "Steven Buss"
4
4
  __email__ = "steven.buss@gmail.com"
5
- __version__ = "0.0.12"
5
+ __version__ = "0.1.1"
@@ -0,0 +1,85 @@
1
+ from typing import Any
2
+
3
+ import multibase
4
+ import multihash
5
+
6
+ from ..codecs import CodecBase
7
+
8
+ SIZE = -1
9
+ IS_PATH = False
10
+
11
+
12
+ class Codec(CodecBase):
13
+ """
14
+ Codec for certificate hashes (certhash).
15
+
16
+ A certhash is a multihash of a certificate, encoded as a multibase string
17
+ using the 'base64url' encoding.
18
+ """
19
+
20
+ SIZE = SIZE
21
+ IS_PATH = IS_PATH
22
+
23
+ def validate(self, b: bytes) -> None:
24
+ """
25
+ Validates that the byte representation is a valid multihash.
26
+
27
+ Args:
28
+ b: The bytes to validate.
29
+
30
+ Raises:
31
+ ValueError: If the bytes cannot be decoded as a multihash.
32
+ """
33
+ try:
34
+ multihash.decode(b)
35
+ except Exception as e:
36
+ raise ValueError("Invalid certhash: not a valid multihash") from e
37
+
38
+ def to_bytes(self, proto: Any, string: str) -> bytes:
39
+ """
40
+ Converts the multibase string representation of a certhash to bytes.
41
+
42
+ This involves decoding the multibase string and then validating that
43
+ the resulting bytes are a valid multihash.
44
+
45
+ Args:
46
+ proto: The multiaddr protocol code (unused).
47
+ string: The string representation of the certhash.
48
+
49
+ Returns:
50
+ The raw multihash bytes.
51
+
52
+ Raises:
53
+ ValueError: If the string is not valid multibase or not a multihash.
54
+ """
55
+ try:
56
+ # Decode the multibase string to get the raw multihash bytes.
57
+ decoded_bytes = multibase.decode(string)
58
+ except Exception as e:
59
+ raise ValueError(f"Failed to decode multibase string: {string}") from e
60
+
61
+ # Validate that the decoded bytes are a valid multihash.
62
+ self.validate(decoded_bytes)
63
+ return decoded_bytes
64
+
65
+ def to_string(self, proto: Any, buf: bytes) -> str:
66
+ """
67
+ Converts the raw multihash bytes of a certhash to its string form.
68
+
69
+ This involves validating the bytes first and then encoding them as a
70
+ 'base64url' multibase string.
71
+
72
+ Args:
73
+ proto: The multiaddr protocol code (unused).
74
+ buf: The raw multihash bytes.
75
+
76
+ Returns:
77
+ The multibase string representation of the certhash.
78
+ """
79
+ # Validate the bytes before encoding.
80
+ self.validate(buf)
81
+
82
+ # Encode the bytes using base64url, which is standard for certhash.
83
+ # The result from `multibase.encode` is bytes, so we decode to a string.
84
+ encoded_string = multibase.encode("base64url", buf)
85
+ return encoded_string.decode("utf-8")
@@ -344,56 +344,54 @@ class Multiaddr(collections.abc.Mapping[Any, Any]):
344
344
  return
345
345
 
346
346
  # Handle other protocols
347
- parts = iter(addr.strip("/").split("/"))
348
- if not parts:
347
+ # Convert to list to allow peeking ahead for validation
348
+ parts_list = addr.strip("/").split("/")
349
+ if not parts_list:
349
350
  raise exceptions.StringParseError("empty multiaddr", addr)
350
351
 
351
352
  self._bytes = b""
352
- for part in parts:
353
+ idx: int = 0
354
+ while idx < len(parts_list):
355
+ part = parts_list[idx]
353
356
  if not part:
357
+ idx += 1
354
358
  continue
355
359
 
356
360
  # Special handling for unix paths
357
361
  if part in ("unix",):
358
- try:
359
- # Get the next part as the path value
360
- protocol_path_value = next(parts)
361
- if not protocol_path_value:
362
- raise exceptions.StringParseError("empty protocol path", addr)
363
-
364
- # Join any remaining parts as part of the path
365
- remaining_parts = []
366
- while True:
367
- try:
368
- next_part = next(parts)
369
- if not next_part:
370
- continue
371
- remaining_parts.append(next_part)
372
- except StopIteration:
373
- break
374
-
375
- if remaining_parts:
376
- protocol_path_value = protocol_path_value + "/" + "/".join(remaining_parts)
377
-
378
- proto = protocol_with_name(part)
379
- codec = codec_by_name(proto.codec)
380
- if not codec:
381
- raise exceptions.StringParseError(f"unknown codec: {proto.codec}", addr)
382
-
383
- try:
384
- self._bytes += varint.encode(proto.code)
385
- buf = codec.to_bytes(proto, protocol_path_value)
386
- # Add length prefix for variable-sized or zero-sized codecs
387
- if codec.SIZE <= 0:
388
- self._bytes += varint.encode(len(buf))
389
- if buf: # Only append buffer if it's not empty
390
- self._bytes += buf
391
- except Exception as e:
392
- raise exceptions.StringParseError(str(e), addr) from e
393
- continue
394
- except StopIteration:
362
+ # Get the next part as the path value
363
+ if idx + 1 >= len(parts_list):
395
364
  raise exceptions.StringParseError("missing value for unix protocol", addr)
396
365
 
366
+ protocol_path_value = parts_list[idx + 1]
367
+ if not protocol_path_value:
368
+ raise exceptions.StringParseError("empty protocol path", addr)
369
+
370
+ # Join any remaining parts as part of the path (collect and consume the rest)
371
+ remaining_parts = [p for p in parts_list[idx + 2 :] if p]
372
+ # Consume all remaining parts so outer loop ends
373
+ idx = len(parts_list)
374
+
375
+ if remaining_parts:
376
+ protocol_path_value = protocol_path_value + "/" + "/".join(remaining_parts)
377
+
378
+ proto = protocol_with_name(part)
379
+ codec = codec_by_name(proto.codec)
380
+ if not codec:
381
+ raise exceptions.StringParseError(f"unknown codec: {proto.codec}", addr)
382
+
383
+ try:
384
+ self._bytes += varint.encode(proto.code)
385
+ buf = codec.to_bytes(proto, protocol_path_value)
386
+ # Add length prefix for variable-sized or zero-sized codecs
387
+ if codec.SIZE <= 0:
388
+ self._bytes += varint.encode(len(buf))
389
+ if buf: # Only append buffer if it's not empty
390
+ self._bytes += buf
391
+ except Exception as e:
392
+ raise exceptions.StringParseError(str(e), addr) from e
393
+ continue # Already advanced idx above
394
+
397
395
  # Handle other protocols
398
396
  # Split protocol name and value if present
399
397
  protocol_value: str | None = None
@@ -407,15 +405,30 @@ class Multiaddr(collections.abc.Mapping[Any, Any]):
407
405
  except Exception as exc:
408
406
  raise exceptions.StringParseError(f"unknown protocol: {proto_name}", addr) from exc
409
407
 
408
+ # Fix 2: Validate that tag-only protocols don't accept values via = syntax
409
+ if proto.codec is None and protocol_value is not None:
410
+ # Construct address string without the invalid value
411
+ # to avoid including it in error message
412
+ addr_parts_before = parts_list[:idx]
413
+ if addr_parts_before or proto_name:
414
+ addr_up_to_protocol = "/" + "/".join([*addr_parts_before, proto_name])
415
+ else:
416
+ addr_up_to_protocol = "/"
417
+ raise exceptions.StringParseError(
418
+ f"Protocol '{proto.name}' does not take an argument",
419
+ addr_up_to_protocol,
420
+ proto.name,
421
+ )
422
+
410
423
  # If the protocol expects a value, get it
411
424
  if proto.codec is not None:
412
425
  if protocol_value is None:
413
- try:
414
- protocol_value = next(parts)
415
- except StopIteration:
426
+ if idx + 1 >= len(parts_list):
416
427
  raise exceptions.StringParseError(
417
428
  f"missing value for protocol: {proto_name}", addr
418
429
  )
430
+ protocol_value = parts_list[idx + 1]
431
+ idx += 1 # Consume the value part
419
432
  # Validate value (optional: could add more checks here)
420
433
  # If value looks like a protocol name, that's an error
421
434
  if protocol_value is not None:
@@ -434,13 +447,42 @@ class Multiaddr(collections.abc.Mapping[Any, Any]):
434
447
  if not codec:
435
448
  raise exceptions.StringParseError(f"unknown codec: {proto.codec}", addr)
436
449
 
437
- try:
450
+ # Special case: protocols with codec=None are flag protocols
451
+ # (no value, no length prefix, no buffer)
452
+ if proto.codec is None:
453
+ # Encode the protocol code first
438
454
  self._bytes += varint.encode(proto.code)
439
455
 
440
- # Special case: protocols with codec=None are flag protocols
441
- # (no value, no length prefix, no buffer)
442
- if proto.codec is None:
443
- continue
456
+ # Fix 1: Check if next part exists and is not a valid protocol name
457
+ # If it's not a valid protocol, it's an invalid value
458
+ # Look ahead to find the next non-empty part
459
+ next_idx = idx + 1
460
+ while next_idx < len(parts_list) and not parts_list[next_idx]:
461
+ next_idx += 1
462
+
463
+ if next_idx < len(parts_list):
464
+ next_part = parts_list[next_idx]
465
+ try:
466
+ protocol_with_name(next_part)
467
+ # It's a valid protocol name, so advance idx to that part
468
+ idx = next_idx
469
+ continue
470
+ except exceptions.ProtocolNotFoundError:
471
+ # Not a valid protocol name, so it's an invalid value
472
+ # Construct address string up to (but not including) the invalid value
473
+ # to avoid including it in the error message
474
+ addr_up_to_protocol = "/" + "/".join(parts_list[: idx + 1])
475
+ raise exceptions.StringParseError(
476
+ f"Protocol '{proto.name}' does not take an argument",
477
+ addr_up_to_protocol,
478
+ proto.name,
479
+ )
480
+ # No next part, continue normally
481
+ idx += 1
482
+ continue
483
+
484
+ try:
485
+ self._bytes += varint.encode(proto.code)
444
486
 
445
487
  buf = codec.to_bytes(proto, protocol_value or "")
446
488
  if codec.SIZE <= 0: # Add length prefix for variable-sized or zero-sized codecs
@@ -450,6 +492,8 @@ class Multiaddr(collections.abc.Mapping[Any, Any]):
450
492
  except Exception as e:
451
493
  raise exceptions.StringParseError(str(e), addr) from e
452
494
 
495
+ idx += 1 # Move to next part
496
+
453
497
  def _from_bytes(self, addr: bytes) -> None:
454
498
  """Parse a binary multiaddr.
455
499
 
@@ -81,7 +81,10 @@ P_HTTP_PATH = 0x01E1
81
81
  P_SNI = 0x01C1
82
82
  P_NOISE = 0x01C6
83
83
  P_WEBTRANSPORT = 0x01D1
84
+ P_WEBRTC_DIRECT = 0x118
85
+ P_WEBRTC = 0x119
84
86
  P_MEMORY = 0x309
87
+ P_CERTHASH = 0x1D2
85
88
 
86
89
 
87
90
  class Protocol:
@@ -152,6 +155,8 @@ PROTOCOLS = [
152
155
  Protocol(P_DNS4, "dns4", "domain"),
153
156
  Protocol(P_DNS6, "dns6", "domain"),
154
157
  Protocol(P_DNSADDR, "dnsaddr", "domain"),
158
+ Protocol(P_SNI, "sni", "domain"),
159
+ Protocol(P_NOISE, "noise", None),
155
160
  Protocol(P_SCTP, "sctp", "uint16be"),
156
161
  Protocol(P_UDT, "udt", None),
157
162
  Protocol(P_UTP, "utp", None),
@@ -174,7 +179,10 @@ PROTOCOLS = [
174
179
  Protocol(P_P2P_CIRCUIT, "p2p-circuit", None),
175
180
  Protocol(P_WEBTRANSPORT, "webtransport", None),
176
181
  Protocol(P_UNIX, "unix", "fspath"),
182
+ Protocol(P_WEBRTC_DIRECT, "webrtc-direct", None),
183
+ Protocol(P_WEBRTC, "webrtc", None),
177
184
  Protocol(P_MEMORY, "memory", "memory"),
185
+ Protocol(P_CERTHASH, "certhash", "certhash"),
178
186
  ]
179
187
 
180
188
 
@@ -1,13 +1,13 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: multiaddr
3
- Version: 0.0.12
3
+ Version: 0.1.1
4
4
  Summary: Python implementation of jbenet's multiaddr
5
5
  Author-email: Steven Buss <steven.buss@gmail.com>
6
6
  Maintainer: acul71
7
7
  Maintainer-email: pacrob <pacrob@protonmail.com>, Manu Sheel Gupta <manu@seeta.in>
8
8
  License: MIT License
9
9
  Project-URL: Homepage, https://github.com/multiformats/py-multiaddr
10
- Project-URL: Download, https://github.com/multiformats/py-multiaddr/tarball/0.0.12
10
+ Project-URL: Download, https://github.com/multiformats/py-multiaddr/tarball/0.1.1
11
11
  Keywords: multiaddr
12
12
  Classifier: Development Status :: 2 - Pre-Alpha
13
13
  Classifier: Intended Audience :: Developers
@@ -30,6 +30,8 @@ Requires-Dist: netaddr
30
30
  Requires-Dist: psutil
31
31
  Requires-Dist: py-cid>=0.3.1
32
32
  Requires-Dist: py-multicodec>=0.2.0
33
+ Requires-Dist: py-multibase
34
+ Requires-Dist: py-multihash
33
35
  Requires-Dist: trio-typing>=0.0.4
34
36
  Requires-Dist: trio>=0.26.0
35
37
  Requires-Dist: varint
@@ -19,6 +19,7 @@ multiaddr.egg-info/not-zip-safe
19
19
  multiaddr.egg-info/requires.txt
20
20
  multiaddr.egg-info/top_level.txt
21
21
  multiaddr/codecs/__init__.py
22
+ multiaddr/codecs/certhash.py
22
23
  multiaddr/codecs/cid.py
23
24
  multiaddr/codecs/domain.py
24
25
  multiaddr/codecs/fspath.py
@@ -5,6 +5,8 @@ netaddr
5
5
  psutil
6
6
  py-cid>=0.3.1
7
7
  py-multicodec>=0.2.0
8
+ py-multibase
9
+ py-multihash
8
10
  trio-typing>=0.0.4
9
11
  trio>=0.26.0
10
12
  varint
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "multiaddr"
7
- version = "0.0.12"
7
+ version = "0.1.1"
8
8
  description = "Python implementation of jbenet's multiaddr"
9
9
  readme = "README.rst"
10
10
  authors = [{ name = "Steven Buss", email = "steven.buss@gmail.com" }]
@@ -35,6 +35,8 @@ dependencies = [
35
35
  "psutil",
36
36
  "py-cid >= 0.3.1",
37
37
  "py-multicodec >= 0.2.0",
38
+ "py-multibase",
39
+ "py-multihash",
38
40
  "trio-typing>=0.0.4",
39
41
  "trio>=0.26.0",
40
42
  "varint",
@@ -42,7 +44,7 @@ dependencies = [
42
44
 
43
45
  [project.urls]
44
46
  Homepage = "https://github.com/multiformats/py-multiaddr"
45
- Download = "https://github.com/multiformats/py-multiaddr/tarball/0.0.12"
47
+ Download = "https://github.com/multiformats/py-multiaddr/tarball/0.1.1"
46
48
 
47
49
  [project.optional-dependencies]
48
50
  dev = [
@@ -135,7 +137,7 @@ name = "Removals"
135
137
  showcontent = true
136
138
 
137
139
  [tool.bumpversion]
138
- current_version = "0.0.12"
140
+ current_version = "0.1.1"
139
141
  parse = """
140
142
  (?P<major>\\d+)
141
143
  \\.(?P<minor>\\d+)
@@ -67,6 +67,8 @@ from multiaddr.protocols import (
67
67
  "/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzu:80",
68
68
  "/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzuq:-1",
69
69
  "/garlic32/566niximlxdzpanmn4qouucvua3k7neniwss47li5r6ugoertzu@",
70
+ "/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/b2uaraocy6yrdblb4sfptaddgimjmmpy",
71
+ "/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/b2uaraocy6yrdblb4sfptaddgimjmmpy/certhash/zQmbWTwYGcmdyK9CYfNBcfs9nhZs17a6FQ4Y8oea278xx41",
70
72
  "/udp/1234/sctp",
71
73
  "/udp/1234/udt/1234",
72
74
  "/udp/1234/utp/1234",
@@ -125,10 +127,21 @@ def test_invalid(addr_str):
125
127
  "/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
126
128
  "/unix/a/b/c/d/e",
127
129
  "/unix/stdio",
130
+ "/ip4/127.0.0.1/tcp/127/noise",
128
131
  "/ip4/1.2.3.4/tcp/80/unix/a/b/c/d/e/f",
129
132
  "/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234/unix/stdio",
130
133
  "/dns/example.com",
131
134
  "/dns4/موقع.وزارة-الاتصالات.مصر",
135
+ "/ip4/127.0.0.1/tcp/443/tls/sni/example.com/http/http-path/foo",
136
+ "/memory/4",
137
+ "/http-path/tmp%2Fbar",
138
+ "/http-path/tmp%2Fbar%2Fbaz",
139
+ "/http-path/foo",
140
+ "/ip4/127.0.0.1/tcp/9090/http/p2p-webrtc-direct",
141
+ "/ip4/127.0.0.1/tcp/127/webrtc-direct",
142
+ "/ip4/127.0.0.1/tcp/127/webrtc",
143
+ "/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g"
144
+ "/ip4/127.0.0.1/udp/9090/webrtc-direct/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g",
132
145
  ],
133
146
  ) # nopep8
134
147
  def test_valid(addr_str):
@@ -1009,3 +1022,83 @@ def test_http_path_raw_value_access():
1009
1022
  from urllib.parse import quote
1010
1023
 
1011
1024
  assert quote(raw_value, safe="") == encoded_value
1025
+
1026
+
1027
+ def test_tag_only_protocol_rejects_value_slash_syntax():
1028
+ """Test that tag-only protocols reject values using /tag/value syntax"""
1029
+ tag_only_protocols = [
1030
+ "webrtc",
1031
+ "webrtc-direct",
1032
+ "noise",
1033
+ "quic",
1034
+ "quic-v1",
1035
+ "tls",
1036
+ "http",
1037
+ "https",
1038
+ "ws",
1039
+ "wss",
1040
+ "p2p-circuit",
1041
+ "webtransport",
1042
+ ]
1043
+
1044
+ for proto_name in tag_only_protocols:
1045
+ # Should fail with clear error message
1046
+ with pytest.raises(StringParseError) as exc_info:
1047
+ Multiaddr(f"/{proto_name}/value")
1048
+ assert "does not take an argument" in str(exc_info.value)
1049
+ assert proto_name in str(exc_info.value)
1050
+
1051
+
1052
+ def test_tag_only_protocol_rejects_value_equals_syntax():
1053
+ """Test that tag-only protocols reject values using /tag=value syntax"""
1054
+ tag_only_protocols = [
1055
+ "webrtc",
1056
+ "webrtc-direct",
1057
+ "noise",
1058
+ "quic",
1059
+ "tls",
1060
+ "http",
1061
+ ]
1062
+
1063
+ for proto_name in tag_only_protocols:
1064
+ # Should fail with clear error message
1065
+ with pytest.raises(StringParseError) as exc_info:
1066
+ Multiaddr(f"/{proto_name}=value")
1067
+ assert "does not take an argument" in str(exc_info.value)
1068
+ assert proto_name in str(exc_info.value)
1069
+
1070
+
1071
+ def test_tag_only_protocol_allows_valid_combinations():
1072
+ """Test that tag-only protocols work correctly in valid combinations"""
1073
+ # Single tag protocol
1074
+ assert str(Multiaddr("/webrtc")) == "/webrtc"
1075
+ assert str(Multiaddr("/webrtc-direct")) == "/webrtc-direct"
1076
+
1077
+ # Multiple tag protocols chained
1078
+ assert str(Multiaddr("/webrtc/noise")) == "/webrtc/noise"
1079
+ assert str(Multiaddr("/webrtc-direct/webrtc")) == "/webrtc-direct/webrtc"
1080
+
1081
+ # Tag protocol followed by value protocol
1082
+ assert str(Multiaddr("/webrtc-direct/ip4/127.0.0.1")) == "/webrtc-direct/ip4/127.0.0.1"
1083
+
1084
+ # Complex valid address
1085
+ addr = "/ip4/127.0.0.1/udp/9090/webrtc-direct/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g"
1086
+ assert str(Multiaddr(addr)) == addr
1087
+
1088
+
1089
+ def test_tag_only_protocol_error_message_format():
1090
+ """Test that error messages for tag-only protocols are clear and helpful"""
1091
+ # Test /tag/value syntax
1092
+ with pytest.raises(StringParseError) as exc_info:
1093
+ Multiaddr("/webrtc-direct/invalidvalue")
1094
+ error_msg = str(exc_info.value)
1095
+ assert "does not take an argument" in error_msg
1096
+ assert "webrtc-direct" in error_msg
1097
+ assert "invalidvalue" not in error_msg # Should not mention the invalid value
1098
+
1099
+ # Test /tag=value syntax
1100
+ with pytest.raises(StringParseError) as exc_info:
1101
+ Multiaddr("/webrtc=somevalue")
1102
+ error_msg = str(exc_info.value)
1103
+ assert "does not take an argument" in error_msg
1104
+ assert "webrtc" in error_msg
@@ -1,11 +1,13 @@
1
1
  import base64
2
2
  import os
3
3
 
4
+ import multibase
5
+ import multihash
4
6
  import pytest
5
7
  import varint
6
8
 
7
9
  from multiaddr import Multiaddr, exceptions, protocols
8
- from multiaddr.codecs import garlic32, garlic64, http_path, ipcidr, memory
10
+ from multiaddr.codecs import certhash, garlic32, garlic64, http_path, ipcidr, memory
9
11
  from multiaddr.exceptions import BinaryParseError, StringParseError
10
12
 
11
13
 
@@ -559,3 +561,67 @@ def test_ipcidr_invalid_bytes_inputs():
559
561
 
560
562
  with pytest.raises(ValueError):
561
563
  codec.validate(b"\x01\x02")
564
+
565
+
566
+ # --------CERT-HASH---------
567
+
568
+ # The multihash package provides `encode` at runtime, but some static
569
+ # checkers (ruff/pyright) may not see it. Ignore the attribute check here.
570
+ VALID_MULTIHASH_BYTES = multihash.encode(b"hello world", "sha2-256") # type: ignore[attr-defined]
571
+ VALID_CERTHASH_STRING = multibase.encode("base64url", VALID_MULTIHASH_BYTES).decode("utf-8")
572
+
573
+ INVALID_BYTES = b"this is not a multihash"
574
+ INVALID_CONTENT_STRING = multibase.encode("base64url", INVALID_BYTES).decode("utf-8")
575
+
576
+
577
+ def test_certhash_valid_roundtrip():
578
+ codec = certhash.Codec()
579
+ b = codec.to_bytes(None, VALID_CERTHASH_STRING)
580
+ assert isinstance(b, bytes)
581
+ assert b == VALID_MULTIHASH_BYTES
582
+
583
+
584
+ def test_certhash_invalid_multihash_bytes_raises():
585
+ """
586
+ Tests that calling to_string() with bytes that are not a valid
587
+ multihash raises a ValueError.
588
+ """
589
+ codec = certhash.Codec()
590
+ with pytest.raises(ValueError):
591
+ codec.to_string(None, INVALID_BYTES)
592
+
593
+
594
+ def test_certhash_valid_multibase_but_invalid_content_raises():
595
+ """
596
+ Tests that to_bytes() raises an error if the string is valid multibase
597
+ but its decoded content is not a valid multihash.
598
+ """
599
+ codec = certhash.Codec()
600
+ with pytest.raises(ValueError):
601
+ codec.to_bytes(None, INVALID_CONTENT_STRING)
602
+
603
+
604
+ def test_certhash_invalid_multibase_string_raises():
605
+ """
606
+ Tests that passing a string with an invalid multibase prefix or
607
+ encoding raises an error.
608
+ """
609
+ codec = certhash.Codec()
610
+ # 'z' is a valid multibase prefix, but the content is not valid base58.
611
+ invalid_string = "z-this-is-not-valid"
612
+ with pytest.raises(Exception): # Catches errors from the multibase library
613
+ codec.to_bytes(None, invalid_string)
614
+
615
+
616
+ def test_certhash_validate_function():
617
+ """
618
+ Directly tests the validate method.
619
+ """
620
+ codec = certhash.Codec()
621
+
622
+ # A valid multihash should not raise an error
623
+ codec.validate(VALID_MULTIHASH_BYTES)
624
+
625
+ # Invalid bytes should raise a ValueError
626
+ with pytest.raises(ValueError):
627
+ codec.validate(INVALID_BYTES)
@@ -1,7 +1,4 @@
1
- import pytest
2
-
3
1
  from multiaddr import Multiaddr
4
- from multiaddr.exceptions import StringParseError
5
2
  from multiaddr.utils import get_thin_waist_addresses
6
3
 
7
4
 
@@ -22,12 +19,6 @@ def test_specific_address_override_port():
22
19
  assert addrs == [Multiaddr("/ip4/123.123.123.123/tcp/100")]
23
20
 
24
21
 
25
- def test_ignore_non_thin_waist():
26
- # Should raise StringParseError for unknown protocol (e.g. /webrtc)
27
- with pytest.raises(StringParseError):
28
- Multiaddr("/ip4/123.123.123.123/udp/1234/webrtc")
29
-
30
-
31
22
  def test_ipv4_wildcard():
32
23
  input_addr = Multiaddr("/ip4/0.0.0.0/tcp/1234")
33
24
  addrs = get_thin_waist_addresses(input_addr)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes