3个版本 (破坏性更新)
0.3.0 | 2019年9月10日 |
---|---|
0.2.0 | 2019年9月8日 |
0.1.0 | 2019年9月4日 |
#1241 in 解析器实现
48KB
1.5K SLoC
did-common
遵循W3CDID规范的Rust库,用于解析去中心化标识符(DIDs)。
用法:DID
将以下内容添加到您的Cargo.toml
[dependencies]
did_common = "^0.3"
并将以下内容添加到您的crate根目录(如果您使用的是Rust 2015)
extern crate did_common;
以下是解析或验证简单DID字符串的示例
use did_common::did::Did;
let did = Did::parse("did:example:123456789abcdefghi#keys-1").unwrap();
if Did::is_valid("did:ethr:0xf3beac30c498d9e26865f34fcaa57dbb935b0d74") {
println!("DID is valid.");
}
// output: DID is valid.
您还可以使用构建器构建DID
use did_common::did::DidBuilder;
let did =
DidBuilder::new("example", "1234")
.with_params(&[("service", Some("agent"))])
.with_fragment("keys-1")
.build();
println!("{}", did);
// output: did:example:1234;service=agent#keys-1
用法:DID文档
以下是解析DID文档的示例
use did_common::did_doc::DidDocument;
use did_common::json_parse;
let did_doc = DidDocument::parse(
&json_parse(
r#"
{
"@context": "https://www.w3.org/2019/did/v1",
"id": "did:example:123456789abcdefghi",
"created": "2002-10-10T17:00:00Z"
"updated": "2002-10-10T17:00:00Z"
"publicKey": [
{
"id": "did:example:123456789abcdefghi#keys-1",
"type": "Secp256k1VerificationKey2018",
"controller": "did:example:123456789abcdefghi",
"publicKeyHex": "02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71"
}
],
"authentication": [
{
"id": "did:example:123456789abcdefghi#keys-2",
"type": "Ed25519VerificationKey2018",
"controller": "did:example:123456789abcdefghi",
"publicKeyBase58": "H3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV"
}
]
"service": [
{
"id": "did:example:123456789abcdefghi#openid",
"type": "OpenIdConnectVersion1.0Service",
"serviceEndpoint": "https://openid.example.com/"
}
}
"#).unwrap()
);
println!("{}", did_doc.id());
// output: did:example:123456789abcdefghi
您还可以使用构建器构建DID文档
use did_common::did_doc::DidDocumentBuilder;
let did_doc =
DidDocumentBuilder::new("did:example:123456789abcdefghi")
.with_pubkeys(vec![
PublicKeyBuilder::new(
"did:example:123456789abcdefghi#keys-1",
PublicKeyType::EcdsaSecp256k1,
"did:example:123456789abcdefghi"
)
.with_encoded_key(PublicKeyEncoded::Hex(
"02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71"
))
.build()
])
.build();
let key = did_doc.pub_keys().first().unwrap();
println!("{}", key.id());
// output: did:example:123456789abcdefghi#keys-1
许可证
本项目采用以下任一许可证
- Apache许可证2.0版本,(LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT许可证 (LICENSE-MIT 或 https://open-source.org.cn/licenses/MIT)
任选其一。
依赖关系
~0.1–0.9MB
~15K SLoC