diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index 4c3f8c1ff..438703c6b 100644 --- a/libp2p/multiaddress.nim +++ b/libp2p/multiaddress.nim @@ -632,6 +632,23 @@ proc validate*(ma: MultiAddress): bool = discard result = true +proc init*(mtype: typedesc[MultiAddress], address: TransportAddress): MultiAddress = + ## Returns a multiaddr of ``address``. + case address.family + of AddressFamily.IPv4: + var a = IpAddress( + family: IpAddressFamily.IPv4, + address_v4: address.address_v4 + ) + result = MultiAddress.init(a, Protocol.IPPROTO_TCP, address.port) + of AddressFamily.IPv6: + var a = IpAddress(family: IpAddressFamily.IPv6, + address_v6: address.address_v6) + result = MultiAddress.init(a, Protocol.IPPROTO_TCP, address.port) + else: + raise newException(TransportAddressError, + "Invalid address for transport!") + proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, value: openarray[byte]): MultiAddress = ## Initialize MultiAddress object from protocol id ``protocol`` and array diff --git a/libp2p/transports/tcptransport.nim b/libp2p/transports/tcptransport.nim index d171bee2b..095a396b8 100644 --- a/libp2p/transports/tcptransport.nim +++ b/libp2p/transports/tcptransport.nim @@ -28,7 +28,7 @@ proc connHandler*(t: Transport, Future[Connection] {.async, gcsafe.} = trace "handling connection for", address = $client.remoteAddress let conn: Connection = newConnection(newChronosStream(server, client)) - conn.observedAddrs = client.remoteAddress.toMultiAddr() + conn.observedAddrs = MultiAddress.init(client.remoteAddress) if not initiator: let handlerFut = if t.handler == nil: nil else: t.handler(conn) let connHolder: ConnHolder = ConnHolder(connection: conn, @@ -65,7 +65,7 @@ method listen*(t: TcpTransport, t.server.start() # always get the resolved address in case we're bound to 0.0.0.0:0 - t.ma = t.server.sock.getLocalAddress().toMultiAddr() + t.ma = MultiAddress.init(t.server.sock.getLocalAddress()) result = t.server.join() debug "started node on", address = t.ma diff --git a/libp2p/wire.nim b/libp2p/wire.nim index 511286aab..1b7dc7256 100644 --- a/libp2p/wire.nim +++ b/libp2p/wire.nim @@ -140,19 +140,3 @@ proc getLocalAddress*(sock: AsyncFD): TransportAddress = if getsockname(SocketHandle(sock), cast[ptr SockAddr](addr saddr), addr slen) == 0: fromSAddr(addr saddr, slen, result) - -proc toMultiAddr*(address: TransportAddress): MultiAddress = - ## Returns a multiaddr of ``address``. - case address.family - of AddressFamily.IPv4: - var a = IpAddress( - family: IpAddressFamily.IPv4, - address_v4: address.address_v4 - ) - result = MultiAddress.init(a, Protocol.IPPROTO_TCP, address.port) - of AddressFamily.IPv6: - var a = IpAddress(family: IpAddressFamily.IPv6, - address_v6: address.address_v6) - result = MultiAddress.init(a, Protocol.IPPROTO_TCP, address.port) - else: - raise newException(TransportAddressError, "Invalid address for transport!")