diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index c046dba..23cd3d5 100644 --- a/libp2p/multiaddress.nim +++ b/libp2p/multiaddress.nim @@ -34,7 +34,7 @@ type coder*: Transcoder MultiAddress* = object - data*: VBuffer + data: VBuffer MaPatternOp* = enum Eq, Or, And @@ -63,6 +63,10 @@ const IPPROTO_TCP = Protocol.IPPROTO_TCP IPPROTO_UDP = Protocol.IPPROTO_UDP +proc data*(ma: MultiAddress): VBuffer = + ## Returns the data buffer of the MultiAddress. + return ma.data + proc hash*(a: MultiAddress): Hash = var h: Hash = 0 h = h !& hash(a.data.buffer) @@ -873,6 +877,8 @@ proc getProtocol(name: string): MAProtocol {.inline.} = proc init*(mtype: typedesc[MultiAddress], value: string): MaResult[MultiAddress] = ## Initialize MultiAddress object from string representation ``value``. + if len(value) == 0 or value == "/": + return err("multiaddress: Address must not be empty!") var parts = value.trimRight('/').split('/') if len(parts[0]) != 0: err("multiaddress: Invalid MultiAddress, must start with `/`") @@ -920,7 +926,7 @@ proc init*(mtype: typedesc[MultiAddress], data: openArray[byte]): MaResult[MultiAddress] = ## Initialize MultiAddress with array of bytes ``data``. if len(data) == 0: - err("multiaddress: Address could not be empty!") + err("multiaddress: Address must not be empty!") else: var res: MultiAddress res.data = initVBuffer() diff --git a/tests/testmultiaddress.nim b/tests/testmultiaddress.nim index 3a0b9af..845424c 100644 --- a/tests/testmultiaddress.nim +++ b/tests/testmultiaddress.nim @@ -1,4 +1,5 @@ import unittest2 +import stew/byteutils import ../libp2p/[multicodec, multiaddress] when defined(nimHasUsed): {.used.} @@ -62,6 +63,8 @@ const ] FailureVectors = [ + "", + "/", "/ip4", "/ip4/::1", "/ip4/fdpsofodsajfdoisa", @@ -299,6 +302,7 @@ suite "MultiAddress test suite": test "go-multiaddr failure test vectors": for item in FailureVectors: check MultiAddress.init(item).isErr() + check MultiAddress.init(item.toBytes()).isErr() test "rust-multiaddr success test vectors": ## Rust test vectors are with changed UDP encoding and without WSS diff --git a/tests/testnameresolve.nim b/tests/testnameresolve.nim index 4a4dd40..4c5da1f 100644 --- a/tests/testnameresolve.nim +++ b/tests/testnameresolve.nim @@ -144,7 +144,6 @@ suite "Name resolving": test "getHostname": check: MultiAddress.init("/dnsaddr/bootstrap.libp2p.io/").tryGet().getHostname == "bootstrap.libp2p.io" - MultiAddress.init("").tryGet().getHostname == "" MultiAddress.init("/ip4/147.75.69.143/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN").tryGet().getHostname == "147.75.69.143" MultiAddress.init("/ip6/2604:1380:1000:6000::1/tcp/4001/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN").tryGet().getHostname == "2604:1380:1000:6000::1" MultiAddress.init("/dns/localhost/udp/0").tryGet().getHostname == "localhost"