Skip to content

Commit

Permalink
Continue removing profiles from aries-vcx
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Jun 7, 2023
1 parent 3c96562 commit 68fee40
Show file tree
Hide file tree
Showing 17 changed files with 193 additions and 138 deletions.
2 changes: 1 addition & 1 deletion agents/rust/aries-vcx-agent/src/services/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl ServiceConnections {
pub async fn receive_invitation(&self, invite: AnyInvitation) -> AgentResult<String> {
let pairwise_info = PairwiseInfo::create(&self.profile.inject_wallet()).await?;
let invitee = Connection::new_invitee("".to_owned(), pairwise_info)
.accept_invitation(&self.profile, invite)
.accept_invitation(&self.profile.inject_indy_ledger_read(), invite)
.await?;

let thread_id = invitee.thread_id().to_owned();
Expand Down
35 changes: 23 additions & 12 deletions agents/rust/aries-vcx-agent/src/services/mediated_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ impl ServiceMediatedConnections {
}

pub async fn create_invitation(&self) -> AgentResult<AnyInvitation> {
let mut connection = MediatedConnection::create("", &self.profile, &self.agency_client()?, true).await?;
connection.connect(&self.profile, &self.agency_client()?, None).await?;
let mut connection =
MediatedConnection::create("", &self.profile.inject_wallet(), &self.agency_client()?, true).await?;
connection
.connect(&self.profile.inject_wallet(), &self.agency_client()?, None)
.await?;
let invite = connection
.get_invite_details()
.ok_or_else(|| AgentError::from_kind(AgentErrorKind::InviteDetails))?
Expand All @@ -61,19 +64,27 @@ impl ServiceMediatedConnections {
}

pub async fn receive_invitation(&self, invite: AnyInvitation) -> AgentResult<String> {
let ddo = into_did_doc(&self.profile, &invite).await?;
let connection =
MediatedConnection::create_with_invite("", &self.profile, &self.agency_client()?, invite, ddo, true)
.await?;
let ddo = into_did_doc(&self.profile.inject_indy_ledger_read(), &invite).await?;
let connection = MediatedConnection::create_with_invite(
"",
&self.profile.inject_wallet(),
&self.agency_client()?,
invite,
ddo,
true,
)
.await?;
self.mediated_connections
.insert(&connection.get_thread_id(), connection)
}

pub async fn send_request(&self, thread_id: &str) -> AgentResult<()> {
let mut connection = self.mediated_connections.get(thread_id)?;
connection.connect(&self.profile, &self.agency_client()?, None).await?;
connection
.find_message_and_update_state(&self.profile, &self.agency_client()?)
.connect(&self.profile.inject_wallet(), &self.agency_client()?, None)
.await?;
connection
.find_message_and_update_state(&self.profile.inject_wallet(), &self.agency_client()?)
.await?;
self.mediated_connections.insert(thread_id, connection)?;
Ok(())
Expand All @@ -82,16 +93,16 @@ impl ServiceMediatedConnections {
pub async fn accept_request(&self, thread_id: &str, request: Request) -> AgentResult<()> {
let mut connection = self.mediated_connections.get(thread_id)?;
connection
.process_request(&self.profile, &self.agency_client()?, request)
.process_request(&self.profile.inject_wallet(), &self.agency_client()?, request)
.await?;
connection.send_response(&self.profile).await?;
connection.send_response(&self.profile.inject_wallet()).await?;
self.mediated_connections.insert(thread_id, connection)?;
Ok(())
}

pub async fn send_ping(&self, thread_id: &str) -> AgentResult<()> {
let mut connection = self.mediated_connections.get(thread_id)?;
connection.send_ping(&self.profile, None).await?;
connection.send_ping(self.profile.inject_wallet(), None).await?;
self.mediated_connections.insert(thread_id, connection)?;
Ok(())
}
Expand All @@ -103,7 +114,7 @@ impl ServiceMediatedConnections {
pub async fn update_state(&self, thread_id: &str) -> AgentResult<ConnectionState> {
let mut connection = self.mediated_connections.get(thread_id)?;
connection
.find_message_and_update_state(&self.profile, &self.agency_client()?)
.find_message_and_update_state(&self.profile.inject_wallet(), &self.agency_client()?)
.await?;
self.mediated_connections.insert(thread_id, connection)?;
Ok(self.mediated_connections.get(thread_id)?.get_state())
Expand Down
7 changes: 3 additions & 4 deletions aries_vcx/src/common/keys.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use aries_vcx_core::ledger::base_ledger::IndyLedgerRead;
use serde_json::Value;

use crate::core::profile::profile::Profile;
Expand Down Expand Up @@ -40,10 +41,8 @@ pub async fn rotate_verkey(profile: &Arc<dyn Profile>, did: &str) -> VcxResult<(
rotate_verkey_apply(profile, did, &trustee_temp_verkey).await
}

pub async fn get_verkey_from_ledger(profile: &Arc<dyn Profile>, did: &str) -> VcxResult<String> {
let ledger = Arc::clone(profile).inject_indy_ledger_read();

let nym_response: String = ledger.get_nym(did).await?;
pub async fn get_verkey_from_ledger(indy_ledger: &Arc<dyn IndyLedgerRead>, did: &str) -> VcxResult<String> {
let nym_response: String = indy_ledger.get_nym(did).await?;
let nym_json: Value = serde_json::from_str(&nym_response).map_err(|err| {
AriesVcxError::from_msg(
AriesVcxErrorKind::SerializationError,
Expand Down
49 changes: 25 additions & 24 deletions aries_vcx/src/common/ledger/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{collections::HashMap, sync::Arc};

use crate::common::ledger::service_didsov::EndpointDidSov;
use crate::handlers::util::AnyInvitation;
use aries_vcx_core::ledger::base_ledger::{IndyLedgerRead, IndyLedgerWrite};
use serde_json::Value;

use crate::errors::error::{AriesVcxError, AriesVcxErrorKind, VcxResult};
Expand Down Expand Up @@ -64,10 +65,10 @@ pub struct ReplyDataV1 {
const DID_KEY_PREFIX: &str = "did:key:";
const ED25519_MULTIBASE_CODEC: [u8; 2] = [0xed, 0x01];

pub async fn resolve_service(profile: &Arc<dyn Profile>, service: &OobService) -> VcxResult<AriesService> {
pub async fn resolve_service(indy_ledger: &Arc<dyn IndyLedgerRead>, service: &OobService) -> VcxResult<AriesService> {
match service {
OobService::AriesService(service) => Ok(service.clone()),
OobService::Did(did) => get_service(profile, did).await,
OobService::Did(did) => get_service(indy_ledger, did).await,
}
}

Expand All @@ -88,12 +89,12 @@ pub async fn add_new_did(
Ok((did, verkey))
}

pub async fn into_did_doc(profile: &Arc<dyn Profile>, invitation: &AnyInvitation) -> VcxResult<AriesDidDoc> {
pub async fn into_did_doc(indy_ledger: &Arc<dyn IndyLedgerRead>, invitation: &AnyInvitation) -> VcxResult<AriesDidDoc> {
let mut did_doc: AriesDidDoc = AriesDidDoc::default();
let (service_endpoint, recipient_keys, routing_keys) = match invitation {
AnyInvitation::Con(Invitation::Public(invitation)) => {
did_doc.set_id(invitation.content.did.to_string());
let service = get_service(profile, &invitation.content.did)
let service = get_service(indy_ledger, &invitation.content.did)
.await
.unwrap_or_else(|err| {
error!("Failed to obtain service definition from the ledger: {}", err);
Expand All @@ -117,7 +118,7 @@ pub async fn into_did_doc(profile: &Arc<dyn Profile>, invitation: &AnyInvitation
}
AnyInvitation::Oob(invitation) => {
did_doc.set_id(invitation.id.clone());
let service = resolve_service(profile, &invitation.content.services[0])
let service = resolve_service(indy_ledger, &invitation.content.services[0])
.await
.unwrap_or_else(|err| {
error!("Failed to obtain service definition from the ledger: {}", err);
Expand Down Expand Up @@ -184,31 +185,32 @@ fn normalize_keys_as_naked(keys_list: Vec<String>) -> VcxResult<Vec<String>> {
Ok(result)
}

pub async fn get_service(profile: &Arc<dyn Profile>, did: &String) -> VcxResult<AriesService> {
pub async fn get_service(ledger: &Arc<dyn IndyLedgerRead>, did: &String) -> VcxResult<AriesService> {
let did_raw = did.to_string();
let did_raw = match did_raw.rsplit_once(':') {
None => did_raw,
Some((_, value)) => value.to_string(),
};
let ledger = Arc::clone(profile).inject_indy_ledger_read();
let attr_resp = ledger.get_attr(&did_raw, "endpoint").await?;
let data = get_data_from_response(&attr_resp)?;
if data["endpoint"].is_object() {
let endpoint: EndpointDidSov = serde_json::from_value(data["endpoint"].clone())?;
let recipient_keys = vec![get_verkey_from_ledger(profile, &did_raw).await?];
let recipient_keys = vec![get_verkey_from_ledger(ledger, &did_raw).await?];
let endpoint_url = endpoint.endpoint;

return Ok(AriesService::create()
.set_recipient_keys(recipient_keys)
.set_service_endpoint(endpoint_url)
.set_routing_keys(endpoint.routing_keys.unwrap_or_default()));
}
parse_legacy_endpoint_attrib(profile, &did_raw).await
parse_legacy_endpoint_attrib(ledger, &did_raw).await
}

pub async fn parse_legacy_endpoint_attrib(profile: &Arc<dyn Profile>, did_raw: &str) -> VcxResult<AriesService> {
let ledger = Arc::clone(profile).inject_indy_ledger_read();
let attr_resp = ledger.get_attr(did_raw, "service").await?;
pub async fn parse_legacy_endpoint_attrib(
indy_ledger: &Arc<dyn IndyLedgerRead>,
did_raw: &str,
) -> VcxResult<AriesService> {
let attr_resp = indy_ledger.get_attr(did_raw, "service").await?;
let data = get_data_from_response(&attr_resp)?;
let ser_service = match data["service"].as_str() {
Some(ser_service) => ser_service.to_string(),
Expand All @@ -233,22 +235,23 @@ pub async fn write_endpoint_legacy(profile: &Arc<dyn Profile>, did: &str, servic
Ok(res)
}

pub async fn write_endpoint(profile: &Arc<dyn Profile>, did: &str, service: &EndpointDidSov) -> VcxResult<String> {
pub async fn write_endpoint(
indy_ledger_write: &Arc<dyn IndyLedgerWrite>,
did: &str,
service: &EndpointDidSov,
) -> VcxResult<String> {
let attrib_json = json!({ "endpoint": service }).to_string();
let ledger = Arc::clone(profile).inject_indy_ledger_write();
let res = ledger.add_attr(did, &attrib_json).await?;
let res = indy_ledger_write.add_attr(did, &attrib_json).await?;
check_response(&res)?;
Ok(res)
}

pub async fn add_attr(profile: &Arc<dyn Profile>, did: &str, attr: &str) -> VcxResult<()> {
let ledger = Arc::clone(profile).inject_indy_ledger_write();
let res = ledger.add_attr(did, &attr).await?;
pub async fn add_attr(indy_ledger_write: &Arc<dyn IndyLedgerWrite>, did: &str, attr: &str) -> VcxResult<()> {
let res = indy_ledger_write.add_attr(did, &attr).await?;
check_response(&res)
}

pub async fn get_attr(profile: &Arc<dyn Profile>, did: &str, attr_name: &str) -> VcxResult<String> {
let ledger = Arc::clone(profile).inject_indy_ledger_read();
pub async fn get_attr(ledger: &Arc<dyn IndyLedgerRead>, did: &str, attr_name: &str) -> VcxResult<String> {
let attr_resp = ledger.get_attr(did, attr_name).await?;
let data = get_data_from_response(&attr_resp)?;
match data.get(attr_name) {
Expand All @@ -258,10 +261,8 @@ pub async fn get_attr(profile: &Arc<dyn Profile>, did: &str, attr_name: &str) ->
}
}

pub async fn clear_attr(profile: &Arc<dyn Profile>, did: &str, attr_name: &str) -> VcxResult<String> {
let ledger = Arc::clone(profile).inject_indy_ledger_write();

ledger
pub async fn clear_attr(indy_ledger_write: &Arc<dyn IndyLedgerWrite>, did: &str, attr_name: &str) -> VcxResult<String> {
indy_ledger_write
.add_attr(did, &json!({ attr_name: Value::Null }).to_string())
.await
.map_err(|err| err.into())
Expand Down
Loading

0 comments on commit 68fee40

Please sign in to comment.