multiaddr: Composable and future-proof network addresses, available as a modern, zero-dependency Swift library.
To use Swift Package Manager, add Multiaddr
to your Package.swift
file. Alternatively, add it from Xcode directly.
let package = Package(
name: "myproject",
dependencies: [
.package(url: "https://github.com/lukereichold/swift-multiaddr.git", from: "1.0.0"),
],
targets: [
.target(
name: "myproject",
dependencies: ["Multiaddr"]),
]
)
Then run swift build
.
To use Carthage, add Multiaddr
to your Cartfile
:
github "lukereichold/swift-multiaddr" ~> 1.0.0
Then run carthage update
and use the framework in Carthage/Build/<platform>
.
let addr = try Multiaddr("/dns6/foo.com/tcp/443/https")
dump(addr)
▿ /dns6/foo.com/tcp/443/https
▿ addresses : 3 elements
▿ 0 : /dns6/foo.com
- addrProtocol : Multiaddr.Protocol.dns6
▿ address : Optional<String>
- some : "foo.com"
▿ 1 : /tcp/443
- addrProtocol : Multiaddr.Protocol.tcp
▿ address : Optional<String>
- some : "443"
▿ 2 : /https
- addrProtocol : Multiaddr.Protocol.https
- address : nil
let serializedData = try addr.binaryPacked()
/// (`Data`) 14 bytes
let addrFromData = try Multiaddr(serializedData)
/// `[Multiaddr.Address]` 3 values
assert(addr == addrFromData) /// true
print(addr.protocols())
/// `["dns6", "tcp", "https"]`
let encapsulated = try Multiaddr("/dns4/foo.com").encapsulate("tcp/80/http/bar/baz.jpg")
print(encapsulated) /// `/dns4/foo.com/tcp/80/http/bar/baz.jpg`
let tcpComponent = try Multiaddr("tcp/80")
let decapsulated = encapsulated.decapsulate(tcpComponent)
print(decapsulated) /// `/dns4/foo.com`
let popped = addr.pop()
Optional<Address>
▿ some : /https
- addrProtocol : Multiaddr.Protocol.https
- address : nil
Contributions welcome. Please check out the issues.
MIT © 2019 Luke Reichold