Crates.io | did_common |
lib.rs | did_common |
version | 0.3.0 |
source | src |
created_at | 2019-09-04 23:03:42.795494 |
updated_at | 2019-09-10 22:13:00.283236 |
description | A rust library for parsing Decentralized Identifiers (DIDs), with no_std support. |
homepage | https://github.com/stiiifff/did-common-rust |
repository | https://github.com/stiiifff/did-common-rust |
max_upload_size | |
id | 162301 |
size | 78,269 |
A rust library for parsing Decentralized Identifiers (DIDs) following the DID specification by the W3C.
Add this to your Cargo.toml
:
[dependencies]
did_common = "^0.3"
and this to your crate root (if you're using Rust 2015):
extern crate did_common;
Here is how to to parse or validate a simple DID string:
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.
You can also build a DID using a builder:
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
Here is how to parse a DID Document:
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
You can also build a DID Document using a builder:
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
This project is licensed under either of
at your option.