Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple IOTA networks in the Resolver #1304

Merged
merged 13 commits into from
Feb 21, 2024
3 changes: 3 additions & 0 deletions identity_resolver/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 network {0}")]
UnsupportedNetwork(String),
}
55 changes: 55 additions & 0 deletions identity_resolver/src/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ impl<DOC: 'static> Resolver<DOC, SingleThreadedCommand<DOC>> {

#[cfg(feature = "iota")]
mod iota_handler {
use crate::ErrorCause;

use super::Resolver;
use identity_document::document::CoreDocument;
use identity_iota_core::IotaClientExt;
Expand Down Expand Up @@ -277,6 +279,59 @@ 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 collection of tuples where each tuple contains the name of the network name and its
/// corresponding client.
///
/// # Examples
///
/// ```ignore
/// // 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
abdulmth marked this conversation as resolved.
Show resolved Hide resolved
/// clients added in this method.
abdulmth marked this conversation as resolved.
Show resolved Hide resolved
/// - This function does not validate the provided configuration. Ensure that the provided
/// network name corresponds with the client, possibly by using `client.network_name()`.
pub fn attach_multiple_iota_handlers<CLI>(&mut self, clients: Vec<(&'static str, CLI)>)
where
CLI: IotaClientExt + Send + Sync + 'static,
{
let arc_clients: Arc<Vec<(&str, CLI)>> = 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(|(network, _)| *network == did_network)
.map(|(_, client)| client)
abdulmth marked this conversation as resolved.
Show resolved Hide resolved
.ok_or(crate::Error::new(ErrorCause::UnsupportedNetwork(
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);
}
}
}

Expand Down
Loading