Summary
py-multiaddr has no CLI for inspecting multiaddrs. go-multiaddr ships a multiaddr binary that decodes a multiaddr (string or hex-packed bytes) into structured JSON, which is useful for debugging, scripting, and cross-implementation testing. Python users currently have to write a throwaway script and import the library just to see what a multiaddr decodes to.
Motivation
- Debugging: Quickly inspect an opaque multiaddr string or hex blob from logs without spinning up a Python shell.
- Interop testing: Compare Python's decoding of a multiaddr against
go-multiaddr's output for the same input, which is valuable for the multiaddr spec conformance work across implementations.
- Scripting/tooling: Other CLI tools (shell scripts, CI checks) can pipe a multiaddr string into this and consume JSON output rather than parsing custom text.
- Parity: Every other major multiaddr implementation (Go, at least) exposes this developer convenience; Python is the odd one out.
Current Behavior
There's no multiaddr executable and no documented way to get a structured, per-protocol breakdown of a multiaddr from the command line.
Proposed Solution
Add a multiaddr/cli.py module and register it as a console script:
[project.scripts]
multiaddr = "multiaddr.cli:main"
The CLI should:
- Accept a multiaddr as either a string (
/ip4/1.2.3.4/tcp/80) or hex-packed bytes (0x...).
- Decode it and print JSON containing:
- the canonical string form
- the packed byte size
- the packed hex representation
- a
components array, one entry per protocol segment, each with protocol name, code, decoded value, and raw hex value
Example (matching go-multiaddr's output shape)
$ multiaddr /ip4/1.2.3.4/tcp/80
{
"string": "/ip4/1.2.3.4/tcp/80",
"packed": "0x04010203040650",
"packedSize": 9,
"components": [
{"protocol": "ip4", "code": 4, "value": "1.2.3.4", "rawValue": "0x01020304"},
{"protocol": "tcp", "code": 6, "value": "80", "rawValue": "0x0050"}
]
}
A draft implementation is sketched below (needs review against the actual internal API — bytes_iter/transforms naming may not match current internals):
import argparse
import json
from .multiaddr import Multiaddr
from .transforms import bytes_iter
def main():
parser = argparse.ArgumentParser(description="Inspect multiaddrs")
parser.add_argument("addr", help="Multiaddr string or hex bytes (0x...)")
args = parser.parse_args()
addr = Multiaddr(bytes.fromhex(args.addr[2:])) if args.addr.startswith("0x") else Multiaddr(args.addr)
components = [
{
"protocol": proto.name,
"code": proto.code,
"value": codec.to_string(proto, value) if codec.SIZE != 0 else None,
"rawValue": "0x" + value.hex(),
}
for offset, proto, codec, value in bytes_iter(addr.to_bytes())
]
print(json.dumps({
"string": str(addr),
"packed": "0x" + addr.to_bytes().hex(),
"packedSize": len(addr.to_bytes()),
"components": components,
}, indent=2))
Open Questions / Acceptance Criteria
References
Summary
py-multiaddrhas no CLI for inspecting multiaddrs.go-multiaddrships amultiaddrbinary that decodes a multiaddr (string or hex-packed bytes) into structured JSON, which is useful for debugging, scripting, and cross-implementation testing. Python users currently have to write a throwaway script and import the library just to see what a multiaddr decodes to.Motivation
go-multiaddr's output for the same input, which is valuable for the multiaddr spec conformance work across implementations.Current Behavior
There's no
multiaddrexecutable and no documented way to get a structured, per-protocol breakdown of a multiaddr from the command line.Proposed Solution
Add a
multiaddr/cli.pymodule and register it as a console script:The CLI should:
/ip4/1.2.3.4/tcp/80) or hex-packed bytes (0x...).componentsarray, one entry per protocol segment, each with protocol name, code, decoded value, and raw hex valueExample (matching go-multiaddr's output shape)
A draft implementation is sketched below (needs review against the actual internal API —
bytes_iter/transformsnaming may not match current internals):Open Questions / Acceptance Criteria
--compact/-cflag for single-line JSON?--versionflag reporting the installedmultiaddrpackage version?References
go-multiaddr/multiaddr/main.go