|
1 | 1 | """Re-export of the multiaddr library."""
|
2 | 2 |
|
3 | 3 | from multiaddr import Multiaddr
|
4 |
| -from multiaddr.protocols import ( |
5 |
| - P_TCP, |
6 |
| - P_IP4, |
7 |
| - P_IP6, |
8 |
| - P_DNS, |
9 |
| - P_UDP, |
10 |
| - P_P2P, |
11 |
| - Protocol |
12 |
| -) |
13 |
| - |
14 |
| -__all__ = [ |
15 |
| - "Multiaddr", |
16 |
| - "Protocol", |
17 |
| - "is_valid_address" |
18 |
| -] |
19 |
| - |
20 |
| -PROTOCOL_CONFIG = { |
21 |
| - P_IP4: { |
22 |
| - 'transports': [P_TCP, P_UDP], |
23 |
| - 'overlays': [P_P2P] |
24 |
| - }, |
25 |
| - P_IP6: { |
26 |
| - 'transports': [P_TCP, P_UDP], |
27 |
| - 'overlays': [P_P2P] |
28 |
| - }, |
29 |
| - P_DNS: { |
30 |
| - 'transports': [P_TCP], |
31 |
| - 'overlays': [P_P2P] |
32 |
| - } |
| 4 | +from multiaddr.protocols import (P_DNS, P_IP4, P_IP6, P_P2P, P_TCP, P_UDP, |
| 5 | + Protocol) |
| 6 | + |
| 7 | +__all__ = ["Multiaddr", "Protocol", "is_valid_address"] |
| 8 | + |
| 9 | +_PROTOCOL_CONFIG = { |
| 10 | + P_IP4: {"transports": [P_TCP, P_UDP], "overlays": [P_P2P]}, |
| 11 | + P_IP6: {"transports": [P_TCP, P_UDP], "overlays": [P_P2P]}, |
| 12 | + P_DNS: {"transports": [P_TCP], "overlays": [P_P2P]}, |
33 | 13 | }
|
34 | 14 |
|
| 15 | + |
35 | 16 | def _generate_supported_protocols():
|
36 | 17 | """Generate all valid protocol combinations."""
|
37 | 18 | protocols = set()
|
38 |
| - |
39 |
| - for network, config in PROTOCOL_CONFIG.items(): |
40 |
| - for transport in config['transports']: |
| 19 | + |
| 20 | + for network, config in _PROTOCOL_CONFIG.items(): |
| 21 | + for transport in config["transports"]: |
41 | 22 |
|
42 | 23 | base_combo = (network, transport)
|
43 | 24 | protocols.add(base_combo)
|
44 |
| - |
45 |
| - for overlay in config['overlays']: |
| 25 | + |
| 26 | + for overlay in config["overlays"]: |
46 | 27 | protocols.add(base_combo + (overlay,))
|
47 |
| - |
| 28 | + |
48 | 29 | return protocols
|
49 | 30 |
|
50 |
| -SUPPORTED_PROTOCOLS = _generate_supported_protocols() |
| 31 | + |
| 32 | +_SUPPORTED_PROTOCOLS = _generate_supported_protocols() |
| 33 | + |
51 | 34 |
|
52 | 35 | def is_valid_address(addr: Multiaddr) -> bool:
|
53 | 36 | """
|
54 | 37 | Check if a multiaddr uses only supported protocols.
|
55 |
| - |
| 38 | +
|
56 | 39 | synlink network only supports a subset of multiaddr protocols.
|
57 |
| - |
58 |
| - |
59 |
| - Example |
| 40 | +
|
| 41 | +
|
| 42 | + ### Example: |
60 | 43 | ```
|
61 | 44 | "/ip4/127.0.0.1/tcp/8080",
|
62 | 45 | "/ip6/::1/tcp/8080",
|
63 | 46 | "/dns/example.com/tcp/443/p2p/QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N"
|
64 | 47 | ```
|
65 | 48 | """
|
66 | 49 | protocol_tuple = tuple(protocol.code for protocol in addr.protocols())
|
67 |
| - return protocol_tuple in SUPPORTED_PROTOCOLS |
68 |
| - |
69 |
| - |
70 |
| - |
| 50 | + return protocol_tuple in _SUPPORTED_PROTOCOLS |
0 commit comments