From 5aea90ce3662e9685147b00928603995b16d20ba Mon Sep 17 00:00:00 2001 From: Abdulrahim Al Methiab Date: Wed, 14 Feb 2024 17:42:03 +0100 Subject: [PATCH] Support multiple IOTA network in the Resolver --- identity_resolver/src/error.rs | 3 ++ identity_resolver/src/resolution/resolver.rs | 53 ++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/identity_resolver/src/error.rs b/identity_resolver/src/error.rs index 5a8c3c63f4..a319fb26e8 100644 --- a/identity_resolver/src/error.rs +++ b/identity_resolver/src/error.rs @@ -68,4 +68,7 @@ pub enum ErrorCause { /// The method that is unsupported. method: String, }, + /// No client attached to the specific network. + #[error("none of the attached clients support the netowk {0}")] + UnsupportedNetowrk(String), } diff --git a/identity_resolver/src/resolution/resolver.rs b/identity_resolver/src/resolution/resolver.rs index 10d5359db8..98b148c567 100644 --- a/identity_resolver/src/resolution/resolver.rs +++ b/identity_resolver/src/resolution/resolver.rs @@ -249,6 +249,8 @@ impl Resolver> { #[cfg(feature = "iota")] mod iota_handler { + use crate::ErrorCause; + use super::Resolver; use identity_document::document::CoreDocument; use identity_iota_core::IotaClientExt; @@ -277,6 +279,57 @@ mod iota_handler { self.attach_handler(IotaDID::METHOD.to_owned(), handler); } + + /// Convenience method for attaching multiple handlers responsible for resolving IOTA DIDs + /// on multiple networks. + /// + /// + /// # Arguments + /// + /// * `clients` - A vector of tuples where each tuple contains the name of the network name and its corresponding + /// client. + /// + /// # Examples + /// + /// ``` + /// // Assume `smr_client` and `iota_client` are instances IOTA clients `iota_sdk::client::Client`. + /// attach_multiple_iota_handlers(vec![("smr", smr_client), ("iota", iota_client)]); + /// ``` + /// + /// # See Also + /// - [`attach_handler`](Self::attach_handler). + /// + /// # Note + /// + /// Using `attach_iota_handler` or `attach_handler` for the IOTA method would override all + /// clients added in this method. + pub fn attach_multiple_iota_handlers(&mut self, clients: Vec<(&'static str, CLI)>) + where + CLI: IotaClientExt + Send + Sync + 'static, + { + let arc_clients: Arc> = Arc::new(clients); + + let handler = move |did: IotaDID| { + let future_client = arc_clients.clone(); + async move { + let did_network = did.network_str(); + let client: &CLI = future_client + .as_ref() + .iter() + .find(|(netowrk, _)| *netowrk == did_network) + .map(|(_, client)| client) + .ok_or(crate::Error::new(ErrorCause::UnsupportedNetowrk( + did_network.to_string(), + )))?; + client + .resolve_did(&did) + .await + .map_err(|err| crate::Error::new(ErrorCause::HandlerError { source: Box::new(err) })) + } + }; + + self.attach_handler(IotaDID::METHOD.to_owned(), handler); + } } }