Skip to content

finsig/smoldot-swift

Repository files navigation

Smoldot Swift

A Swift wrapper for the smoldot Rust-based Polkadot light client.

Installation

Add the package declaration to your project's manifest dependencies array:

.package(url: "https://github.com/finsig/smoldot-swift.git", from: "0.1.0")

Usage

A Chain Specification file must be provided to initialize a chain. A Chain Specification is a JSON Object that describes a Polkadot-based blockchain network.

Example Chain Specification JSON files for Polkadot, Kusama, Rococo, and Westend can be copied for use from /Tests/SmoldotSwiftTests/Resources.

Initialize a chain from a specification file:

var chain = Chain(specificationFile: {Resource file URL})

Add the chain to the client to connect to the network:

try Client.shared.add(chain: &chain)

RPC requests must conform to the JSON-RPC 2.0 protocol. A request can be built programatically:

let request = try JSONRPC2Request(method: "chain_getHeader", identifier: .int(1))

or initialized from a String in the JSON data format:

let request = try JSONRPC2Request(string: "{\"id\":1,\"jsonrpc\":\"2.0\",\"method\":\"chain_getHeader\",\"params\":[]}")

To send the request use:

try Client.shared.send(request: request, to: chain)

To wait for a response use:

try await Client.shared.response(from: chain)

Alternatively, an asynchronous stream of responses may be read using:

try await Client.shared.responses(from: chain)

To disconnect the client from the network use:

try Client.shared.remove(chain: &chain)

For additional information about usage see reference documentation.

Logging

You may enable logging of the smoldot Rust FFI library with an environment variable at runtime (RUST_LOG). The library uses the Rustenv_logger framework and levels can be set accordingly.

Building locally

There is a build_xcframework.sh script in the repo which can be used to build the XCode Framework target from the smoldot Rust FFI library.

$ zsh build_xcframework.sh dev

Omitting the dev argument will modify the package settings to use a remote binary target and create a compressed framework file (along with the checksum value).

Testing

In addition to unit tests please see this project for memory usage profiling.

Bug Reports

See GitHub issues regarding log noise.