From fb1b4f4039844309a1b20acff60f1f7441640b0f Mon Sep 17 00:00:00 2001 From: Enrico Marconi <31142849+UMR1352@users.noreply.github.com> Date: Tue, 20 Feb 2024 10:08:51 +0100 Subject: [PATCH] dummy client (#1310) --- identity_iota_core/Cargo.toml | 4 +- .../src/client/identity_client.rs | 5 ++ identity_resolver/Cargo.toml | 2 + identity_resolver/src/resolution/resolver.rs | 87 ++++++++++++++++++- 4 files changed, 94 insertions(+), 4 deletions(-) diff --git a/identity_iota_core/Cargo.toml b/identity_iota_core/Cargo.toml index 6d09722b5e..46541234fc 100644 --- a/identity_iota_core/Cargo.toml +++ b/identity_iota_core/Cargo.toml @@ -45,9 +45,11 @@ rustdoc-args = ["--cfg", "docsrs"] default = ["client", "iota-client", "revocation-bitmap", "send-sync-client-ext"] # Exposes the IotaIdentityClient and IotaIdentityClientExt traits. client = ["dep:async-trait", "iota-sdk"] -# Enbales the implementation of the extension traits on the iota-sdk's Client. +# Enables the implementation of the extension traits on the iota-sdk's Client. iota-client = ["client", "iota-sdk/client", "iota-sdk/tls"] # Enables revocation with `RevocationBitmap2022`. revocation-bitmap = ["identity_credential/revocation-bitmap"] # Adds Send bounds on the futures produces by the client extension traits. send-sync-client-ext = [] +# Disables the blanket implementation of `IotaIdentityClientExt`. +test = ["client"] diff --git a/identity_iota_core/src/client/identity_client.rs b/identity_iota_core/src/client/identity_client.rs index 94b0cf88b2..8706739da8 100644 --- a/identity_iota_core/src/client/identity_client.rs +++ b/identity_iota_core/src/client/identity_client.rs @@ -1,6 +1,8 @@ // Copyright 2020-2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 +use iota_sdk::client::Client; + use crate::block::protocol::ProtocolParameters; use crate::block::address::Address; @@ -192,7 +194,10 @@ pub trait IotaIdentityClientExt: IotaIdentityClient { } } +#[cfg(not(feature = "test"))] impl IotaIdentityClientExt for T where T: IotaIdentityClient {} +#[cfg(feature = "test")] +impl IotaIdentityClientExt for Client {} pub(super) async fn validate_network(client: &T, did: &IotaDID) -> Result<()> where diff --git a/identity_resolver/Cargo.toml b/identity_resolver/Cargo.toml index 20ec82cd1b..0b674460f3 100644 --- a/identity_resolver/Cargo.toml +++ b/identity_resolver/Cargo.toml @@ -32,6 +32,8 @@ optional = true [dev-dependencies] tokio = { version = "1.29.0", default-features = false, features = ["rt-multi-thread", "macros"] } +identity_iota_core = { version = "=1.1.0", path = "../identity_iota_core", features = ["test"] } +iota-sdk = { version = "1.0.2" } [features] default = ["revocation-bitmap", "iota"] diff --git a/identity_resolver/src/resolution/resolver.rs b/identity_resolver/src/resolution/resolver.rs index e7506e00a0..3cae7be7b7 100644 --- a/identity_resolver/src/resolution/resolver.rs +++ b/identity_resolver/src/resolution/resolver.rs @@ -253,7 +253,6 @@ mod iota_handler { use super::Resolver; use identity_document::document::CoreDocument; - use identity_iota_core::IotaClientExt; use identity_iota_core::IotaDID; use identity_iota_core::IotaDocument; use identity_iota_core::IotaIdentityClientExt; @@ -268,7 +267,7 @@ mod iota_handler { /// See also [`attach_handler`](Self::attach_handler). pub fn attach_iota_handler(&mut self, client: CLI) where - CLI: IotaClientExt + Send + Sync + 'static, + CLI: IotaIdentityClientExt + Send + Sync + 'static, { let arc_client: Arc = Arc::new(client); @@ -307,7 +306,7 @@ mod iota_handler { /// network name corresponds with the client, possibly by using `client.network_name()`. pub fn attach_multiple_iota_handlers(&mut self, clients: Vec<(&'static str, CLI)>) where - CLI: IotaClientExt + Send + Sync + 'static, + CLI: IotaIdentityClientExt + Send + Sync + 'static, { let arc_clients: Arc> = Arc::new(clients); @@ -356,3 +355,85 @@ where .finish() } } + +#[cfg(test)] +mod tests { + use identity_iota_core::block::address::Address; + use identity_iota_core::block::output::AliasId; + use identity_iota_core::block::output::AliasOutput; + use identity_iota_core::block::output::OutputId; + use identity_iota_core::block::protocol::ProtocolParameters; + use identity_iota_core::IotaClientExt; + use identity_iota_core::IotaDID; + use identity_iota_core::IotaDocument; + use identity_iota_core::IotaIdentityClient; + use identity_iota_core::IotaIdentityClientExt; + use iota_sdk::client::secret::SecretManager; + + use super::*; + + struct DummyClient(IotaDocument); + + #[async_trait::async_trait] + impl IotaIdentityClient for DummyClient { + async fn get_alias_output(&self, _id: AliasId) -> identity_iota_core::Result<(OutputId, AliasOutput)> { + todo!() + } + async fn get_protocol_parameters(&self) -> identity_iota_core::Result { + todo!() + } + } + + #[async_trait::async_trait] + impl IotaIdentityClientExt for DummyClient { + async fn resolve_did(&self, did: &IotaDID) -> identity_iota_core::Result { + if self.0.id().as_str() == did.as_str() { + Ok(self.0.clone()) + } else { + Err(identity_iota_core::Error::DIDResolutionError( + iota_sdk::client::error::Error::NoOutput(did.to_string()), + )) + } + } + } + + #[async_trait::async_trait] + impl IotaClientExt for DummyClient { + async fn publish_did_output( + &self, + _secret_manager: &SecretManager, + _alias_output: AliasOutput, + ) -> identity_iota_core::Result { + todo!() + } + async fn delete_did_output( + &self, + _secret_manager: &SecretManager, + _address: Address, + _did: &IotaDID, + ) -> identity_iota_core::Result<()> { + todo!() + } + } + + #[tokio::test] + async fn test_multiple_handlers() { + let did1 = + IotaDID::parse("did:iota:smr:0x0101010101010101010101010101010101010101010101010101010101010101").unwrap(); + let document = IotaDocument::new_with_id(did1.clone()); + let dummy_smr_client = DummyClient(document); + + let did2 = IotaDID::parse("did:iota:0x0101010101010101010101010101010101010101010101010101010101010101").unwrap(); + let document = IotaDocument::new_with_id(did2.clone()); + let dummy_iota_client = DummyClient(document); + + let mut resolver = Resolver::::new(); + resolver.attach_multiple_iota_handlers(vec![("iota", dummy_iota_client), ("smr", dummy_smr_client)]); + + let doc = resolver.resolve(&did1).await.unwrap(); + assert_eq!(doc.id(), &did1); + + let doc = resolver.resolve(&did2).await.unwrap(); + assert_eq!(doc.id(), &did2); + } +}