MultiformatsKit

0.3.0

A Swift implementation of the Multiformats spec, including Multibase, Multicodec, Multihash, and CID.
ATProtoKit/MultiformatsKit

What's New

v0.3.0

2025-05-12T05:17:51Z

Version 0.3.0 includes the following changes:

  • Conform CID to Codable.
    • You can now use JSONDecoder and JSONEncoder to convert to and from a String object.
    • Revamped Multicodec.
      • There is no longer a registry. Now, you can use one of the pre-built prefixes available.
      • More pre-built prefixes will be added overtime.
      • You can create custom prefixes, but the mechanism to grab it has not been added yet. It will be in the future. At this time, there are no plans to do this. However, you can create the custom prefixes and pass it into the appropriate initializer if you need to.

A icon for MultiformatsKit, which contains three stacks of rounded rectangles in an isometric top view. At the top stack, the Multiformats logo is displayed. The three stacks are in various shades of blue.

MultiformatsKit

GitHub Repo stars

Static Badge GitHub Sponsors

MultiformatsKit is a native Swift implementation of the Multiformats protocol suite — including Multibase, Multicodec, Multihash, and CID.

This package enables content-addressing, self-describing data structures, and CID generation — all with Sendable-safe Swift code.

Quick Examples

CID

import MultiformatsKit

Task {
    do {
        let cid = try await CID(content: "Hello, World!")

        // CIDv1 encoded with base32
        print("CIDv1:", try cid.encode())

        // Decode back into a CID
        let decoded = try await CID.decode(from: cid.encode())
        print("Decoded multihash:", decoded.multihash)
    } catch {
        print("Error:", error)
    }
}

Multibase

import MultiformatsKit

do {
    let base58 = Multibase.base58btc
    let result = multibase.encode("Hello from Multibase!")
    
    print(result) // Will return as "4QBebtZN9VvWV59DoFiqFLTfq2CJp".
} catch {
    throw error
}

Multicodec

// Original binary data
let rawData = Data([0xde, 0xad, 0xbe, 0xef])

// Select a codec (e.g., raw binary)
let codec = Multicodec.raw

// Wrap the raw data with the codec’s prefix
let wrapped = codec.wrap(rawData)
print("Wrapped: \(wrapped)")

// Unwrap the data
do {
    let unwrapped = try codec.unwrap(wrapped)
    print("Unwrapped: \(unwrapped)")
} catch {
    print(error)
}

Multihash

Task {
    do {
        let data = Data("hello".utf8)
        let algorithm = try SHA256Multihash()
        try await MultihashFactory.shared.register(algorithm)

        let multihash = try await MultihashFactory.shared.hash(using: "sha2-256", data: data)
        // Prints as [0xdd, 0x7d, 0x93, 0xb5, 0xcc, 0xe6, 0x1c, 0x9e, 0xf6, 0x36, 0x5b, 0xf0, 0x9b, 0x41, 0xa8, 0xb0, 0x6f, 0xce, 0x69, 0x9a, 0xf4, 0x58, 0x76, 0xe3, 0x27, 0x0c, 0xb4, 0x65, 0xa1, 0x7a, 0xec, 0xb4]
        print(multihash.encoded.map { String(format: "%02x", $0) }.joined())
    } catch {
        throw error
    }
}

Features

  • Fully Sendable-safe and concurrency-ready.
  • Supports CIDv0 and CIDv1 (including dag-pb with sha2-256).
  • Multibase encoding/decoding with support for:
    • Base2, Base8, Base10, Base16
    • Base58 (BTC, Flickr)
    • Base32 (lower/upper, padded/unpadded, hex variants)
  • Multicodec registration.
  • Multihash support with plug-and-play hashing algorithms.
  • RFC 4648 compliance for fixed-bit encoding.
  • Varint encoding/decoding.
  • Written entirely in Swift — no C or unsafe code.

Installation

You can use the Swift Package Manager to download and import the library into your project:

dependencies: [
    .package(url: "https://github.com/ATProtoKit/MultiformatsKit.git", from: "0.3.0")
]

Then under targets:

targets: [
    .target(
        // name: "[name of target]",
        dependencies: [
            .product(name: "MultiformatsKit", package: "multiformatskit")
        ]
    )
]

Requirements

To use MultiformatsKit in your apps, your app should target the specific version numbers:

  • iOS and iPadOS 14 or later.
  • macOS 12 or later.
  • tvOS 14 or later.
  • visionOS 1 or later.
  • watchOS 9 or later.

For Linux, you need to use Swift 6.0 or later. On Linux, the minimum requirements include:

  • Amazon Linux 2
  • Debian 12
  • Fedora 39
  • Red Hat UBI 9
  • Ubuntu 20.04

You can also use this project for any programs you make using Swift and running on Docker.

Warning

As of right now, Windows support is theoretically possible, but not has not been tested to work. Contributions and feedback on making it fully compatible for Windows and Windows Server are welcomed.

Submitting Contributions and Feedback

While this project will change significantly, feedback, issues, and contributions are highly welcomed and encouraged. If you'd like to contribute to this project, please be sure to read both the API Guidelines as well as the Contributor Guidelines before submitting a pull request. Any issues (such as bug reports or feedback) can be submitted in the Issues tab. Finally, if there are any security vulnerabilities, please read SECURITY.md for how to report it.

If you have any questions, you can ask me on Bluesky (@cjrriley.com). And while you're at it, give me a follow! I'm also active on the Bluesky API Touchers Discord server.

License

This Swift package is using the Apache 2.0 License. Please view LICENSE.md for more details.

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Sun May 11 2025 21:29:02 GMT-0900 (Hawaii-Aleutian Daylight Time)