Skip to content

Commit

Permalink
fix: persist Linked Verifiable Presentations for did:web (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
nanderstabel authored Dec 5, 2024
1 parent e225131 commit 97b00ca
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
16 changes: 14 additions & 2 deletions agent_identity/src/service/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub enum ServiceResource {
pub struct Service {
#[serde(rename = "id")]
pub service_id: String,
pub presentation_ids: Vec<String>,
pub service: Option<DocumentService>,
pub resource: Option<ServiceResource>,
}
Expand Down Expand Up @@ -189,6 +190,7 @@ impl Aggregate for Service {
.type_("LinkedVerifiablePresentation")
.service_endpoint(ServiceEndpoint::from(OrderedSet::from_iter(
presentation_ids
.clone()
.into_iter()
.map(|presentation_id| {
// TODO: Find a better way to construct the URL
Expand All @@ -201,7 +203,11 @@ impl Aggregate for Service {
.build()
.expect("Failed to create Linked Verifiable Presentation Resource");

Ok(vec![LinkedVerifiablePresentationServiceCreated { service_id, service }])
Ok(vec![LinkedVerifiablePresentationServiceCreated {
service_id,
presentation_ids,
service,
}])
}
}
}
Expand All @@ -221,8 +227,13 @@ impl Aggregate for Service {
self.service.replace(service);
self.resource.replace(resource);
}
LinkedVerifiablePresentationServiceCreated { service_id, service } => {
LinkedVerifiablePresentationServiceCreated {
service_id,
service,
presentation_ids,
} => {
self.service_id = service_id;
self.presentation_ids = presentation_ids;
self.service.replace(service);
}
}
Expand Down Expand Up @@ -278,6 +289,7 @@ pub mod service_tests {
})
.then_expect_events(vec![ServiceEvent::LinkedVerifiablePresentationServiceCreated {
service_id: linked_verifiable_presentation_service_id,
presentation_ids: vec!["presentation-1".to_string()],
service: linked_verifiable_presentation_service,
}])
}
Expand Down
1 change: 1 addition & 0 deletions agent_identity/src/service/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum ServiceEvent {
},
LinkedVerifiablePresentationServiceCreated {
service_id: String,
presentation_ids: Vec<String>,
service: DocumentService,
},
}
Expand Down
7 changes: 6 additions & 1 deletion agent_identity/src/service/views/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ impl View<Service> for Service {
self.service.replace(service.clone());
self.resource.replace(resource.clone());
}
LinkedVerifiablePresentationServiceCreated { service_id, service } => {
LinkedVerifiablePresentationServiceCreated {
service_id,
presentation_ids,
service,
} => {
self.service_id.clone_from(service_id);
self.presentation_ids.clone_from(presentation_ids);
self.service.replace(service.clone());
}
}
Expand Down
31 changes: 22 additions & 9 deletions agent_identity/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,28 @@ pub async fn initialize(state: &IdentityState) {
// If the did:web method is enabled, create a document
if enable_did_web {
let did_method = DidMethod::Web;
let command = DocumentCommand::CreateDocument {
did_method: did_method.clone(),
};

if command_handler(&did_method.to_string(), &state.command.document, command)
.await
.is_err()
{
warn!("Failed to create document");

match query_handler(&did_method.to_string(), &state.query.document).await {
Ok(Some(Document {
document: Some(document),
..
})) => {
info!("DID Document for `did:web` already exists: {:?}", document);
}
_document_does_not_exist => {
info!("Creating new DID Document for `did:web`");

let command = DocumentCommand::CreateDocument {
did_method: did_method.clone(),
};

if command_handler(&did_method.to_string(), &state.command.document, command)
.await
.is_err()
{
warn!("Failed to create DID Document for `did:web`");
}
}
}

// If domain linkage is enabled, create the domain linkage service and add it to the document.
Expand Down

0 comments on commit 97b00ca

Please sign in to comment.