Skip to content

Commit

Permalink
dummy client (#1310)
Browse files Browse the repository at this point in the history
  • Loading branch information
UMR1352 authored Feb 20, 2024
1 parent e664a40 commit fb1b4f4
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
4 changes: 3 additions & 1 deletion identity_iota_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
5 changes: 5 additions & 0 deletions identity_iota_core/src/client/identity_client.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -192,7 +194,10 @@ pub trait IotaIdentityClientExt: IotaIdentityClient {
}
}

#[cfg(not(feature = "test"))]
impl<T> IotaIdentityClientExt for T where T: IotaIdentityClient {}
#[cfg(feature = "test")]
impl IotaIdentityClientExt for Client {}

pub(super) async fn validate_network<T>(client: &T, did: &IotaDID) -> Result<()>
where
Expand Down
2 changes: 2 additions & 0 deletions identity_resolver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
87 changes: 84 additions & 3 deletions identity_resolver/src/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -268,7 +267,7 @@ mod iota_handler {
/// See also [`attach_handler`](Self::attach_handler).
pub fn attach_iota_handler<CLI>(&mut self, client: CLI)
where
CLI: IotaClientExt + Send + Sync + 'static,
CLI: IotaIdentityClientExt + Send + Sync + 'static,
{
let arc_client: Arc<CLI> = Arc::new(client);

Expand Down Expand Up @@ -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<CLI>(&mut self, clients: Vec<(&'static str, CLI)>)
where
CLI: IotaClientExt + Send + Sync + 'static,
CLI: IotaIdentityClientExt + Send + Sync + 'static,
{
let arc_clients: Arc<Vec<(&str, CLI)>> = Arc::new(clients);

Expand Down Expand Up @@ -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<ProtocolParameters> {
todo!()
}
}

#[async_trait::async_trait]
impl IotaIdentityClientExt for DummyClient {
async fn resolve_did(&self, did: &IotaDID) -> identity_iota_core::Result<IotaDocument> {
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<IotaDocument> {
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::<IotaDocument>::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);
}
}

0 comments on commit fb1b4f4

Please sign in to comment.