Skip to content

Commit

Permalink
Resolve did:web (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
amika-sq authored Dec 5, 2023
1 parent b1f36ad commit bddbe25
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/dids/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license-file.workspace = true
async-trait = "0.1.74"
crypto = { path = "../crypto" }
did-jwk = "0.1.1"
did-web = "0.2.2"
serde = { workspace = true }
serde_json = { workspace = true }
ssi-dids = "0.1.1"
Expand Down
1 change: 1 addition & 0 deletions crates/dids/src/method/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod jwk;
pub mod web;

use crate::did::Did;
use crate::resolver::DidResolutionResult;
Expand Down
82 changes: 82 additions & 0 deletions crates/dids/src/method/web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use crate::did::Did;
use crate::method::{DidMethod, DidMethodError, DidResolutionResult};
use async_trait::async_trait;
use crypto::key_manager::KeyManager;
use did_web::DIDWeb as SpruceDidWebMethod;
use ssi_dids::did_resolve::{DIDResolver, ResolutionInputMetadata};
use std::sync::Arc;

/// Concrete implementation for a did:web DID
pub struct DidWeb {
uri: String,
key_manager: Arc<dyn KeyManager>,
}

impl Did for DidWeb {
fn uri(&self) -> &str {
&self.uri
}

fn key_manager(&self) -> &Arc<dyn KeyManager> {
&self.key_manager
}
}

/// Options that can be used to create a did:web DID.
/// This is currently a unit struct because did:web does not support key creation.
pub struct DidWebCreateOptions;

#[async_trait]
impl DidMethod<DidWeb, DidWebCreateOptions> for DidWeb {
const NAME: &'static str = "web";

fn create(
_key_manager: Arc<dyn KeyManager>,
_options: DidWebCreateOptions,
) -> Result<DidWeb, DidMethodError> {
Err(DidMethodError::DidCreationFailure(
"create operation not supported for did:web".to_string(),
))
}

async fn resolve_uri(did_uri: &str) -> DidResolutionResult {
let input_metadata = ResolutionInputMetadata::default();
let (did_resolution_metadata, did_document, did_document_metadata) =
SpruceDidWebMethod.resolve(did_uri, &input_metadata).await;

DidResolutionResult {
did_resolution_metadata,
did_document,
did_document_metadata,
..Default::default()
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn create_fails() {
let key_manager = Arc::new(crypto::key_manager::LocalKeyManager::new_in_memory());

Check failure on line 62 in crates/dids/src/method/web.rs

View workflow job for this annotation

GitHub Actions / cargo test (ubuntu-latest, stable)

failed to resolve: could not find `LocalKeyManager` in `key_manager`

Check failure on line 62 in crates/dids/src/method/web.rs

View workflow job for this annotation

GitHub Actions / cargo test (ubuntu-latest, nightly)

failed to resolve: could not find `LocalKeyManager` in `key_manager`
let result = DidWeb::create(key_manager, DidWebCreateOptions);
assert!(result.is_err());
}

#[tokio::test]
async fn resolution_success() {
let did_uri = "did:web:tbd.website";
let result = DidWeb::resolve_uri(did_uri).await;
assert!(result.did_resolution_metadata.error.is_none());

let did_document = result.did_document.expect("did_document not found");
assert_eq!(did_document.id, did_uri);
}

#[tokio::test]
async fn resolution_failure() {
let result = DidWeb::resolve_uri("did:web:does-not-exist").await;
assert!(result.did_resolution_metadata.error.is_some());
}
}
12 changes: 12 additions & 0 deletions crates/dids/src/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::method::jwk::DidJwk;
use crate::method::web::DidWeb;
use crate::method::DidMethod;
use serde::{Deserialize, Serialize};
use ssi_dids::did_resolve::{
Expand All @@ -18,6 +19,7 @@ impl DidResolver {

match method_name {
DidJwk::NAME => DidJwk::resolve_uri(did_uri).await,
DidWeb::NAME => DidWeb::resolve_uri(did_uri).await,
_ => return DidResolutionResult::from_error(ERROR_METHOD_NOT_SUPPORTED),
}
}
Expand Down Expand Up @@ -87,6 +89,16 @@ mod tests {
assert_eq!(did_document.id, did_uri);
}

#[tokio::test]
async fn resolve_did_web() {
let did_uri = "did:web:tbd.website";
let result = DidResolver::resolve_uri(did_uri).await;
assert!(result.did_resolution_metadata.error.is_none());

let did_document = result.did_document.unwrap();
assert_eq!(did_document.id, did_uri);
}

#[tokio::test]
async fn resolve_invalid_did() {
let did_uri = "did:jwk";
Expand Down

0 comments on commit bddbe25

Please sign in to comment.