Multicodec 规范的 Swift 实现
在您的 Package.swift 文件中包含以下依赖项
let package = Package(
...
dependencies: [
...
.package(url: "https://github.com/swift-libp2p/swift-multicodec.git", .from("0.0.1"))
],
...
.target(
name: "...",
dependencies: [
...
.product(name: "Multicodec", package: "swift-multicodec"),
]),
)
import Multicodec
let prefixedProtobuf = addPrefix(.protobuf, protobuf)
// prefixedProtobuf 0x50...
// The multicodec codec values can be accessed directly:
print(Codecs.DAG_CBOR.code) //113
// To get the string representation and description of a codec (e.g. for error messages):
print(Codecs(113).name) // dag-cbor
print(Codecs(113).description) // optional("MerkleDAG cbor")
此软件包符合此处概述的 JS-Multicodec API
编码值的真实数据来源是multicodec 默认表
更新 Codec 枚举需要通过在项目根目录运行以下命令...
swift run update-codecs
欢迎贡献!这段代码很大程度上只是一个概念验证。我敢保证有更好/更安全的方法来实现相同的结果。 欢迎任何建议、改进,甚至只是批评!
让我们一起让这段代码变得更好!🤝
非常感谢 js-multicodec 团队的工作,他们编写了清晰的代码,并提供了文档和测试,这使得将该库移植到 Swift 相对容易。
MIT © 2022 Breth Inc.