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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- `ClientEnode` now parses and emits bracketed IPv6 endpoints, matching the
canonical `enode://` URL form produced by Ethereum clients.

## [v0.1.0] - 2026-08-31

Initial stable release of the Python Hive Simulator API with:
Expand Down
9 changes: 5 additions & 4 deletions src/hive/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,15 @@ def from_string(cls, enode: str):
prefix = "enode://"
enode = enode.removeprefix(prefix)

id, ip_port = enode.split("@")
id, ip_port = enode.split("@", maxsplit=1)

ip, port = ip_port.split(":")
ip, port = ip_port.rsplit(":", maxsplit=1)

return cls(id=id, ip=ip_address(ip), port=int(port))
return cls(id=id, ip=ip_address(ip.strip("[]")), port=int(port))

def __str__(self) -> str:
return f"enode://{self.id}@{self.ip}:{self.port}"
ip = f"[{self.ip}]" if isinstance(self.ip, IPv6Address) else self.ip
return f"enode://{self.id}@{ip}:{self.port}"

def to_json(self):
return str(self)
Expand Down
22 changes: 22 additions & 0 deletions src/hive/tests/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from ipaddress import IPv4Address, IPv6Address

import pytest

from hive.client import ClientEnode


@pytest.mark.parametrize(
"enode",
[
ClientEnode(id="ab" * 64, ip=IPv4Address("192.0.2.1"), port=30303),
ClientEnode(id="cd" * 64, ip=IPv6Address("2001:db8::1"), port=30303),
],
)
def test_client_enode_round_trip(enode: ClientEnode):
assert ClientEnode.from_string(str(enode)) == enode


def test_client_enode_formats_ipv6_with_brackets():
enode = ClientEnode(id="ab" * 64, ip=IPv6Address("2001:db8::1"), port=30303)

assert str(enode) == f"enode://{'ab' * 64}@[2001:db8::1]:30303"