Skip to content

Commit 93ddb49

Browse files
committed
Implement onion3 string parsing
1 parent 5ef81e3 commit 93ddb49

2 files changed

Lines changed: 31 additions & 13 deletions

File tree

src/main/java/io/ipfs/multiaddr/Protocol.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,32 @@ public byte[] addressToBytes(String addr) {
140140
dout.flush();
141141
return b.toByteArray();
142142
}
143+
case ONION3: {
144+
String[] split = addr.split(":");
145+
if (split.length != 2)
146+
throw new IllegalStateException("Onion3 address needs a port: " + addr);
147+
148+
// onion3 address without the ".onion" substring
149+
if (split[0].length() != 56)
150+
throw new IllegalStateException("failed to parse " + name() + " addr: " + addr + " not a Tor onion3 address.");
151+
152+
byte[] onionHostBytes = Multibase.decode(Multibase.Base.Base32.prefix + split[0]);
153+
if (onionHostBytes.length != 35)
154+
throw new IllegalStateException("Invalid onion3 address host: " + split[0]);
155+
int port = Integer.parseInt(split[1]);
156+
if (port > 65535)
157+
throw new IllegalStateException("Port is > 65535: " + port);
158+
159+
if (port < 1)
160+
throw new IllegalStateException("Port is < 1: " + port);
161+
162+
ByteArrayOutputStream b = new ByteArrayOutputStream();
163+
DataOutputStream dout = new DataOutputStream(b);
164+
dout.write(onionHostBytes);
165+
dout.writeShort(port);
166+
dout.flush();
167+
return b.toByteArray();
168+
}
143169
case UNIX: {
144170
if (addr.startsWith("/"))
145171
addr = addr.substring(1);

src/test/java/io/ipfs/api/MultiAddressTest.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,21 +141,13 @@ public void equalsTests() {
141141
throw new IllegalStateException("Should be equal!");
142142
}
143143

144-
@Test
145-
public void stringToBytes() {
146-
BiConsumer<String, String> test = (s, h) -> {
147-
if (!Arrays.equals(new MultiAddress(s).getBytes(), fromHex(h))) throw new IllegalStateException(s + " bytes != " + new MultiAddress(fromHex(h)));
148-
};
149-
150-
test.accept("/ip4/127.0.0.1/udp/1234", "047f000001910204d2");
151-
test.accept("/ip4/127.0.0.1/tcp/4321", "047f0000010610e1");
152-
test.accept("/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321", "047f000001910204d2047f0000010610e1");
153-
}
154-
155144
@Test
156-
public void bytesToString() {
145+
public void conversion() {
157146
BiConsumer<String, String> test = (s, h) -> {
158-
if (!s.equals(new MultiAddress(fromHex(h)).toString())) throw new IllegalStateException(s + " != " + new MultiAddress(fromHex(h)));
147+
if (!s.equals(new MultiAddress(fromHex(h)).toString()))
148+
throw new IllegalStateException(s + " != " + new MultiAddress(fromHex(h)));
149+
if (! Arrays.equals(new MultiAddress(s).getBytes(), fromHex(h)))
150+
throw new IllegalStateException("bytes for " + s + " != " + new MultiAddress(fromHex(h)));
159151
};
160152

161153
test.accept("/ip4/159.89.141.29/udp/5491/quic", "049f598d1d91021573cc03");

0 commit comments

Comments
 (0)