Coder Social home page Coder Social logo

lukereichold / swift-multiaddr Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 1.0 87 KB

Composable and self-describing network addresses as a modern Swift library

Home Page: https://multiformats.io/multiaddr/

License: MIT License

Swift 100.00%
multiformats ipfs multiaddr protocol codec swift multicodec onion

swift-multiaddr's Introduction

swift-multiaddr

Carthage compatible GitHub license

multiaddr: Composable and future-proof network addresses, available as a modern, zero-dependency Swift library.

Table of Contents

Install

Swift Package Manager

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.

Carthage

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>.

Usage

Human-readable encoding

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

Binary encoding with packing

let serializedData = try addr.binaryPacked()
/// (`Data`) 14 bytes

Binary decoding

let addrFromData = try Multiaddr(serializedData)
/// `[Multiaddr.Address]`  3 values

Equatable Support

assert(addr == addrFromData) /// true

Protocol / address enumeration

print(addr.protocols())
/// `["dns6", "tcp", "https"]`

Encapsulate

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`

Decapsulate

let tcpComponent = try Multiaddr("tcp/80")
let decapsulated = encapsulated.decapsulate(tcpComponent)
print(decapsulated) /// `/dns4/foo.com`

pop()

let popped = addr.pop()
Optional<Address>
   some : /https
    - addrProtocol : Multiaddr.Protocol.https
    - address : nil

Contribute

Contributions welcome. Please check out the issues.

License

MIT © 2019 Luke Reichold

swift-multiaddr's People

Contributors

lukereichold avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.