Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

NC-1921 - IPv6 Bootnode #280

Merged
merged 1 commit into from
Nov 19, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.net.InetAddresses;
import com.google.common.primitives.Ints;

/** The default, basic representation of an Ethereum {@link Peer}. */
Expand Down Expand Up @@ -67,6 +68,12 @@ public static DefaultPeer fromURI(final URI uri) {
// Process the peer's public key, in the host portion of the URI.
final BytesValue id = BytesValue.fromHexString(uri.getUserInfo());

// Process the host. If we have an IPv6 address in URL form translate to an address only form.
String host = uri.getHost();
if (!InetAddresses.isInetAddress(host) && InetAddresses.isUriInetAddress(host)) {
host = InetAddresses.toAddrString(InetAddresses.forUriString(host));
}

// Process the ports; falling back to the default port in both TCP and UDP.
int tcpPort = DEFAULT_PORT;
int udpPort = DEFAULT_PORT;
Expand All @@ -80,7 +87,7 @@ public static DefaultPeer fromURI(final URI uri) {
udpPort = extractUdpPortFromQuery(uri.getQuery()).orElse(tcpPort);
}

final Endpoint endpoint = new Endpoint(uri.getHost(), udpPort, OptionalInt.of(tcpPort));
final Endpoint endpoint = new Endpoint(host, udpPort, OptionalInt.of(tcpPort));
return new DefaultPeer(id, endpoint);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ public void createFromURI() {
assertEquals(30403, peer.getEndpoint().getTcpPort().getAsInt());
}

@Test
public void createFromIpv6URI() {
final Peer peer =
DefaultPeer.fromURI(
"enode://c7849b663d12a2b5bf05b1ebf5810364f4870d5f1053fbd7500d38bc54c705b453d7511ca8a4a86003d34d4c8ee0bbfcd387aa724f5b240b3ab4bbb994a1e09b@[2001:0DB8:85A3:0000::8A2E:370:7334]:30403");
assertEquals(
fromHexString(
"c7849b663d12a2b5bf05b1ebf5810364f4870d5f1053fbd7500d38bc54c705b453d7511ca8a4a86003d34d4c8ee0bbfcd387aa724f5b240b3ab4bbb994a1e09b"),
peer.getId());
// We expect bracket unwrapping, zero group removal via double colon, and leading zeros
// trimmed, and lowercase hex digits.
assertEquals("2001:db8:85a3::8a2e:370:7334", peer.getEndpoint().getHost());
assertEquals(30403, peer.getEndpoint().getUdpPort());
assertEquals(30403, peer.getEndpoint().getTcpPort().getAsInt());
}

@Test(expected = IllegalArgumentException.class)
public void createFromURIFailsForWrongScheme() {
DefaultPeer.fromURI("http://user@foo:80");
Expand Down