Skip to content

Commit

Permalink
Support webtransport multiaddrs (#280)
Browse files Browse the repository at this point in the history
* add multiaddr dependency from jitpack
* Support webtransport multiaddrs
* Implement dns multiaddr
* Support quic-v1 and wss multiaddr
---------
Co-authored-by: ian <[email protected]>
  • Loading branch information
kevodwyer authored May 18, 2023
1 parent 441e620 commit f42740f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ configure(

implementation("com.google.guava:guava")
implementation("org.slf4j:slf4j-api")
implementation("com.github.multiformats:java-multibase:v1.1.1")

testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.junit.jupiter:junit-jupiter-params")
Expand Down
19 changes: 16 additions & 3 deletions libp2p/src/main/kotlin/io/libp2p/core/multiformats/Protocol.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package io.libp2p.core.multiformats

import com.google.common.base.Utf8
import com.google.common.net.InetAddresses
import io.ipfs.multibase.Multibase
import io.ipfs.multibase.binary.Base32
import io.libp2p.core.PeerId
import io.libp2p.etc.encode.Base58
import io.libp2p.etc.types.readUvarint
import io.libp2p.etc.types.toByteArray
import io.libp2p.etc.types.toByteBuf
import io.libp2p.etc.types.writeUvarint
import io.netty.buffer.ByteBuf
import org.bouncycastle.util.encoders.Base32
import java.net.Inet4Address
import java.net.Inet6Address
import java.net.InetAddress
Expand All @@ -36,6 +37,7 @@ enum class Protocol(
DCCP(33, 16, "dccp", UINT16_PARSER, UINT16_STRINGIFIER),
IP6(41, 128, "ip6", IP6_PARSER, IP6_STRINGIFIER),
IP6ZONE(42, LENGTH_PREFIXED_VAR_SIZE, "ip6zone", UTF8_PARSER, UTF8_STRINGIFIER, UTF8_VALIDATOR),
DNS(53, LENGTH_PREFIXED_VAR_SIZE, "dns", UTF8_PARSER, UTF8_STRINGIFIER, UTF8_VALIDATOR),
DNS4(54, LENGTH_PREFIXED_VAR_SIZE, "dns4", UTF8_PARSER, UTF8_STRINGIFIER, UTF8_VALIDATOR),
DNS6(55, LENGTH_PREFIXED_VAR_SIZE, "dns6", UTF8_PARSER, UTF8_STRINGIFIER, UTF8_VALIDATOR),
DNSADDR(56, LENGTH_PREFIXED_VAR_SIZE, "dnsaddr", UTF8_PARSER, UTF8_STRINGIFIER, UTF8_VALIDATOR),
Expand All @@ -48,7 +50,11 @@ enum class Protocol(
HTTPS(443, 0, "https"),
ONION(444, 96, "onion", ONION_PARSER, ONION_STRINGIFIER),
QUIC(460, 0, "quic"),
QUICV1(461, 0, "quic-v1"),
WEBTRANSPORT(465, 0, "webtransport"),
CERTHASH(466, LENGTH_PREFIXED_VAR_SIZE, "certhash", MULTIBASE_PARSER, MULTIBASE_BASE64_STRINGIFIER),
WS(477, 0, "ws"),
WSS(478, 0, "wss"),
P2PCIRCUIT(290, 0, "p2p-circuit"),
HTTP(480, 0, "http");

Expand Down Expand Up @@ -180,14 +186,21 @@ private val BASE58_PARSER: (Protocol, String) -> ByteArray = { _, addr ->
private val BASE58_STRINGIFIER: (Protocol, ByteArray) -> String = { _, bytes ->
Base58.encode(bytes)
}
private val MULTIBASE_PARSER: (Protocol, String) -> ByteArray = { _, addr ->
Multibase.decode(addr)
}
private val MULTIBASE_BASE64_STRINGIFIER: (Protocol, ByteArray) -> String = { _, bytes ->
Multibase.encode(Multibase.Base.Base64Url, bytes)
}
private val ONION_PARSER: (Protocol, String) -> ByteArray = { _, addr ->
val split = addr.split(":")
if (split.size != 2) throw IllegalArgumentException("Onion address needs a port: $addr")
// onion address without the ".onion" substring
if (split[0].length != 16) throw IllegalArgumentException("failed to parse addr: $addr not a Tor onion address.")

val base32 = Base32()
val base32Text = split[0].uppercase()
val onionHostBytes = Base32.decode(base32Text)
val onionHostBytes = base32.decode(base32Text)
val port = split[1].toInt()
if (port > 65535) throw IllegalArgumentException("Port is > 65535: $port")
if (port < 1) throw IllegalArgumentException("Port is < 1: $port")
Expand All @@ -201,5 +214,5 @@ private val ONION_STRINGIFIER: (Protocol, ByteArray) -> String = { _, bytes ->
val byteBuf = bytes.toByteBuf()
val host = byteBuf.readBytes(10).toByteArray()
val port = byteBuf.readUnsignedShort()
Base32.toBase32String(host).lowercase() + ":" + port
String(Base32().encode(host)).lowercase() + ":" + port
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ class MultiaddrTest {
"/ip4/1.2.3.4/tcp/80/unix/a/b/c/d/e/f",
"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234/unix/stdio",
"/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234/unix/stdio",
"/ip4/127.0.0.1/tcp/40001/p2p/16Uiu2HAkuqGKz8D6khfrnJnDrN5VxWWCoLU8Aq4eCFJuyXmfakB5"
"/ip4/127.0.0.1/tcp/40001/p2p/16Uiu2HAkuqGKz8D6khfrnJnDrN5VxWWCoLU8Aq4eCFJuyXmfakB5",
"/ip6/2001:6b0:30:1000:d00e:1dff:fe0b:c764/udp/4001/quic-v1/webtransport/certhash/uEiAEz_3prFf34VZff8XqA1iTdq2Ytp467ErTGr5dRFo60Q/certhash/uEiDyL7yksuIGJsYUvf0AHieLkTux5R5KBk-UsFtA1AG18A"
)

@JvmStatic
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencyResolutionManagement {
repositories {
mavenCentral()
maven { url "https://artifacts.consensys.net/public/maven/maven/" }
maven { url "https://jitpack.io" }
google()
}
}
Expand Down

0 comments on commit f42740f

Please sign in to comment.