/IPFSKit

An IPFS client/api Swift Package, with the ability to add and pin any data on iOS/iPadOS/macOS. Originally bundled with GraniteUI, pulled out for independant use by any party.

Primary LanguageSwiftMIT LicenseMIT

IPFSKit

Swift 5.7 xcode-version

Based on: https://github.com/ipfs-shipyard/swift-ipfs-http-client Converted into a standalone Swift Package.

Installation

Add this repo link as a swift package to your XCode project.

Requirements

- iOS 12 or later
- macOS BigSur or later

Usage

import IPFSKit

struct InfuraGateway: IPFSGateway {
    var host: IPFSHost {
        InfuraHost(id: "...",//On Infura, this is simply the API_KEY
                   secret: "...")//API_KEY_SECRET
    }
    
    var gateway: String {
        "https://neatia.infura-ipfs.io"
    }
}

IPFSKit.gateway = InfuraGateway()

InfuraHost is a predefined host that is ready to use:

// MARK: -- Known Gateways, Pre-defined
public struct InfuraHost: IPFSHost {
public var url: String {
"ipfs.infura.io"
}
public var port: Int
public var ssl: Bool { true }
public var version: String {
"/api/v0/"
}
public var id: String?
public var secret: String?
public init(port: Int = 5001,
id: String,
secret: String) {
self.port = port
self.id = id
self.secret = secret
}
}

Adding data to IPFS

let data: Data = "Hello World".data(using: .utf8)!

let response = await IPFS.upload(data)

print(IPFSKit.gateway?.url(for: response))

Convert any type of object into a Data type to prepare for adding.

Pinning data to IPFS

Infura seems to have updated their services to automatically pin data that is added via the /add endpoint (the previous step). This step is uncessary depending on the provider used or edge cases involved.

let data: Data = "Hello World".data(using: .utf8)!

let response = await IPFS.upload(data)

let gatewayHash = await IPFS.pin(response)