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.
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) } }
import MultiformatsKit do { let base58 = Multibase.base58btc let result = multibase.encode("Hello from Multibase!") print(result) // Will return as "4QBebtZN9VvWV59DoFiqFLTfq2CJp". } catch { throw error }
// 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) }
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 } }
Sendable
-safe and concurrency-ready.dag-pb
with sha2-256
).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") ] ) ]
To use MultiformatsKit in your apps, your app should target the specific version numbers:
For Linux, you need to use Swift 6.0 or later. On Linux, the minimum requirements include:
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 FeedbackWhile 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.ca). And while you're at it, give me a follow! I'm also active on the Bluesky API Touchers Discord server.
This Swift package is using the Apache 2.0 License. Please view LICENSE.md for more details.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4