A rust library for parsing Decentralized Identifiers (DIDs) following the DID specification by the W3C.
Add this to your Cargo.toml
:
toml
[dependencies]
did_common = "^0.3"
and this to your crate root (if you're using Rust 2015):
rust
extern crate did_common;
Here is how to to parse or validate a simple DID string: ```rust 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: ```rust use did_common::did::DidBuilder;
let did = DidBuilder::new("example", "1234") .withparams(&[("service", Some("agent"))]) .withfragment("keys-1") .build(); println!("{}", did); // output: did:example:1234;service=agent#keys-1 ```
Here is how to parse a DID Document: ```rust use didcommon::diddoc::DidDocument; use didcommon::jsonparse;
let diddoc = DidDocument::parse( &jsonparse( 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: ```rust use didcommon::diddoc::DidDocumentBuilder;
let diddoc = DidDocumentBuilder::new("did:example:123456789abcdefghi") .withpubkeys(vec![ PublicKeyBuilder::new( "did:example:123456789abcdefghi#keys-1", PublicKeyType::EcdsaSecp256k1, "did:example:123456789abcdefghi" ) .withencodedkey(PublicKeyEncoded::Hex( "02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71" )) .build() ]) .build();
let key = diddoc.pubkeys().first().unwrap(); println!("{}", key.id()); // output: did:example:123456789abcdefghi#keys-1 ```
This project is licensed under either of
at your option.