Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

multiformats/SwiftMultiaddr

Repository files navigation

This project is no longer maintained and has been archived.

Swift Multiaddr

Carthage compatible

A Multiaddr implementation in Swift.

Table of Contents

Install

Carthage

Add the following to your Cartfile github "multiformats/Swiftmultiaddr" And in the root of your project type: carthage update .

For more information on how to install via Carthage see the README

Requirements

  • Swift 5

Usage

Add the SwiftMultiaddr.framework to your Xcode project:

  • Select your target's Build Phases tab.

  • Select the Embed Frameworks, select the Destination Frameworks and click the + to Add Other... buttons.

  • Navigate to the Carthage/Build/Mac directory in your project root and select all the frameworks in the folder.

In your code, import SwiftMultiaddr.

Example

Simple

import SwiftMultiaddr

/// This does not handle try throwing errors. You should.
/// Construct a Multiaddr from a string.
let addr = try! newMultiaddr("/ip4/127.0.0.1/udp/1234")

/// Construct a Multiaddr from bytes
let addr2 = try! newMultiaddrBytes(addr.bytes())

/// true
try! addr.string() == "/ip4/127.0.0.1/udp/1234"
try! addr.string() == addr2.string()
addr.bytes() == addr2.bytes()
addr == addr2

Protocols

/// Assuming the previous addr exists
addr.protocols()
/// [SwiftMultiaddr.Protocol(
///		code: 4, size: 32, name: "ip4", vCode: [4]), 
///SwiftMultiaddr.Protocol(
///		code: 17, size: 16, name: "udp", vCode: [17])] 

En/decapsulate

try! addr.encapsulate(newMultiaddr("/sctp/5678"))
/// Returns a SwiftMultiaddr /ip4/127.0.0.1/udp/1234/sctp/5678
try! addr.decapsulate(newMultiaddr("/udp/1234"))
/// Returns a SwiftMultiaddr /ip4/127.0.0.1

Tunneling

Multiaddr allows expressing tunnels in a very readable fashion.

let printer = try! newMultiaddr("/ip4/192.168.0.13/tcp/80")
let proxy   = try! newMultiaddr("/ip4/10.20.30.40/tcp/443")
let printerOverProxy = proxy.encapsulate(printer)
/// /ip4/10.20.30.40/tcp/443/ip4/192.168.0.13/tcp/80

let proxyAgain = printerOverProxy.decapsulate(printer) 
/// /ip4/10.20.30.40/tcp/443

Maintainers

Captain: @NeoTeo.

Contribute

Contributions are welcome! Check out the issues.

Check out our contributing document for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS Code of Conduct.

If editing this README, note that this README should be standard-readme compatible.

License

MIT © Matteo Sartori