Skip to content

Commit

Permalink
Merge pull request #137 from AbsaOSS/refactor/accept-output-a2a
Browse files Browse the repository at this point in the history
Return / accept strictly A2AMessages
  • Loading branch information
Patrik-Stas authored Oct 15, 2020
2 parents 2820502 + b6a76f2 commit 676d793
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 146 deletions.
32 changes: 14 additions & 18 deletions libvcx/src/aries/handlers/issuance/holder/holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Holder {
self.holder_sm.get_source_id()
}

pub fn get_credential(&self) -> VcxResult<(String, Credential)> {
pub fn get_credential(&self) -> VcxResult<(String, A2AMessage)> {
self.holder_sm.get_credential()
}

Expand All @@ -66,34 +66,30 @@ impl Holder {
Ok(())
}

pub fn get_credential_offer_message(connection_handle: u32, msg_id: &str) -> VcxResult<CredentialOffer> {
let message = connection::get_message_by_id(connection_handle, msg_id.to_string())?;

let credential_offer: CredentialOffer = match message {
A2AMessage::CredentialOffer(credential_offer) => credential_offer,
msg => {
return Err(VcxError::from_msg(VcxErrorKind::InvalidMessages,
format!("Message of different type was received: {:?}", msg)));
pub fn get_credential_offer_message(connection_handle: u32, msg_id: &str) -> VcxResult<A2AMessage> {
match connection::get_message_by_id(connection_handle, msg_id.to_string()) {
Ok(message) => match message {
A2AMessage::CredentialOffer(_) => Ok(message),
msg => {
return Err(VcxError::from_msg(VcxErrorKind::InvalidMessages,
format!("Message of different type was received: {:?}", msg)));
}
}
};

Ok(credential_offer)
Err(err) => Err(err)
}
}

pub fn get_credential_offer_messages(conn_handle: u32) -> VcxResult<Vec<CredentialOffer>> {
pub fn get_credential_offer_messages(conn_handle: u32) -> VcxResult<Vec<A2AMessage>> {
let messages = connection::get_messages(conn_handle)?;
let msgs: Vec<CredentialOffer> = messages
let msgs: Vec<A2AMessage> = messages
.into_iter()
.filter_map(|(_, a2a_message)| {
match a2a_message {
A2AMessage::CredentialOffer(credential_offer) => {
Some(credential_offer)
}
A2AMessage::CredentialOffer(_) => Some(a2a_message),
_ => None
}
})
.collect();

Ok(msgs)
}
}
4 changes: 2 additions & 2 deletions libvcx/src/aries/handlers/issuance/holder/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,12 @@ impl HolderSM {
}
}

pub fn get_credential(&self) -> VcxResult<(String, Credential)> {
pub fn get_credential(&self) -> VcxResult<(String, A2AMessage)> {
match self.state {
HolderState::Finished(ref state) => {
let cred_id = state.cred_id.clone().ok_or(VcxError::from_msg(VcxErrorKind::InvalidState, "Cannot get credential: Credential Id not found"))?;
let credential = state.credential.clone().ok_or(VcxError::from_msg(VcxErrorKind::InvalidState, "Cannot get credential: Credential not found"))?;
Ok((cred_id, credential))
Ok((cred_id, credential.to_a2a_message()))
}
_ => Err(VcxError::from_msg(VcxErrorKind::NotReady, "Cannot get credential: Credential Issuance is not finished yet"))
}
Expand Down
6 changes: 3 additions & 3 deletions libvcx/src/aries/handlers/proof_presentation/prover/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ impl Prover {
Ok(presentation_request)
}

pub fn get_presentation_request_messages(connection_handle: u32) -> VcxResult<Vec<PresentationRequest>> {
pub fn get_presentation_request_messages(connection_handle: u32) -> VcxResult<Vec<A2AMessage>> {
trace!("Prover::get_presentation_request_messages >>> connection_handle: {:?}", connection_handle);

let presentation_requests: Vec<PresentationRequest> =
let presentation_requests: Vec<A2AMessage> =
connection::get_messages(connection_handle)?
.into_iter()
.filter_map(|(_, message)| {
match message {
A2AMessage::PresentationRequest(presentation_request) => Some(presentation_request),
A2AMessage::PresentationRequest(_) => Some(message),
_ => None
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl Verifier {
pub fn get_presentation(&self) -> VcxResult<String> {
trace!("Verifier::get_presentation >>>");

let proof = self.verifier_sm.presentation()?;
let proof = self.verifier_sm.presentation()?.to_a2a_message();
Ok(json!(proof).to_string())
}

Expand Down
20 changes: 10 additions & 10 deletions libvcx/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use serde_json;

use aries::handlers::connection::agent_info::AgentInfo;
use aries::handlers::connection::connection::{Connection as ConnectionV3, SmConnectionState};
use aries::handlers::connection::connection::{Connection, SmConnectionState};
use aries::messages::a2a::A2AMessage;
use aries::messages::connection::did_doc::DidDoc;
use aries::messages::connection::invite::Invitation as InvitationV3;
Expand All @@ -17,7 +17,7 @@ use utils::error;
use utils::object_cache::ObjectCache;

lazy_static! {
static ref CONNECTION_MAP: ObjectCache<ConnectionV3> = ObjectCache::<ConnectionV3>::new("connections-cache");
static ref CONNECTION_MAP: ObjectCache<Connection> = ObjectCache::<Connection>::new("connections-cache");
}

pub fn create_agent_keys(source_id: &str, pw_did: &str, pw_verkey: &str) -> VcxResult<(String, String)> {
Expand Down Expand Up @@ -90,21 +90,21 @@ pub fn get_source_id(handle: u32) -> VcxResult<String> {
})
}

fn store_connection(connection: ConnectionV3) -> VcxResult<u32> {
fn store_connection(connection: Connection) -> VcxResult<u32> {
CONNECTION_MAP.add(connection)
.or(Err(VcxError::from(VcxErrorKind::CreateConnection)))
}

pub fn create_connection(source_id: &str) -> VcxResult<u32> {
trace!("create_connection >>> source_id: {}", source_id);
let connection = ConnectionV3::create(source_id);
let connection = Connection::create(source_id);
return store_connection(connection);
}

pub fn create_connection_with_invite(source_id: &str, details: &str) -> VcxResult<u32> {
debug!("create connection {} with invite {}", source_id, details);
if let Some(invitation) = serde_json::from_str::<InvitationV3>(details).ok() {
let connection = ConnectionV3::create_with_invite(source_id, invitation)?;
let connection = Connection::create_with_invite(source_id, invitation)?;
store_connection(connection)
} else {
Err(VcxError::from_msg(VcxErrorKind::InvalidJson, "Used invite has invalid structure")) // TODO: Specific error type
Expand Down Expand Up @@ -188,15 +188,15 @@ pub fn get_invite_details(handle: u32) -> VcxResult<String> {
}).or(Err(VcxError::from(VcxErrorKind::InvalidConnectionHandle)))
}

impl Into<(SmConnectionState, AgentInfo, String)> for ConnectionV3 {
impl Into<(SmConnectionState, AgentInfo, String)> for Connection {
fn into(self) -> (SmConnectionState, AgentInfo, String) {
(self.state_object(), self.agent_info().to_owned(), self.source_id())
}
}

impl From<(SmConnectionState, AgentInfo, String)> for ConnectionV3 {
fn from((state, agent_info, source_id): (SmConnectionState, AgentInfo, String)) -> ConnectionV3 {
ConnectionV3::from_parts(source_id, agent_info, state)
impl From<(SmConnectionState, AgentInfo, String)> for Connection {
fn from((state, agent_info, source_id): (SmConnectionState, AgentInfo, String)) -> Connection {
Connection::from_parts(source_id, agent_info, state)
}
}

Expand Down Expand Up @@ -232,7 +232,7 @@ pub fn send_message(handle: u32, message: A2AMessage) -> VcxResult<()> {
}

pub fn send_message_to_self_endpoint(message: A2AMessage, did_doc: &DidDoc) -> VcxResult<()> {
ConnectionV3::send_message_to_self_endpoint(&message, did_doc)
Connection::send_message_to_self_endpoint(&message, did_doc)
}

pub fn is_v3_connection(connection_handle: u32) -> VcxResult<bool> {
Expand Down
24 changes: 12 additions & 12 deletions libvcx/src/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde_json;

use aries::{
handlers::issuance::holder::holder::Holder,
messages::issuance::credential_offer::CredentialOffer as CredentialOfferV3,
messages::issuance::credential_offer::CredentialOffer,
};
use error::prelude::*;
use settings::indy_mocks_enabled;
Expand Down Expand Up @@ -35,8 +35,8 @@ fn handle_err(err: VcxError) -> VcxError {
}
}

fn create_credential_v3(source_id: &str, offer: &str) -> VcxResult<Option<Holder>> {
trace!("create_credential_v3 >>> source_id: {}, offer: {}", source_id, secret!(&offer));
fn create_credential(source_id: &str, offer: &str) -> VcxResult<Option<Holder>> {
trace!("create_credential >>> source_id: {}, offer: {}", source_id, secret!(&offer));

let offer_message = ::serde_json::from_str::<serde_json::Value>(offer)
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidJson, format!("Cannot deserialize Message: {:?}", err)))?;
Expand All @@ -46,7 +46,7 @@ fn create_credential_v3(source_id: &str, offer: &str) -> VcxResult<Option<Holder
offer => offer
};

if let Ok(cred_offer) = serde_json::from_value::<CredentialOfferV3>(offer_message) {
if let Ok(cred_offer) = serde_json::from_value::<CredentialOffer>(offer_message) {
return Ok(Some(Holder::create(cred_offer, source_id)?));
}

Expand All @@ -57,7 +57,7 @@ fn create_credential_v3(source_id: &str, offer: &str) -> VcxResult<Option<Holder
pub fn credential_create_with_offer(source_id: &str, offer: &str) -> VcxResult<u32> {
trace!("credential_create_with_offer >>> source_id: {}, offer: {}", source_id, secret!(&offer));

let cred_offer: CredentialOfferV3 = serde_json::from_str(offer)
let cred_offer: CredentialOffer = serde_json::from_str(offer)
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidJson,
format!("Strict `aries` protocol is enabled. Can not parse `aries` formatted Credential Offer: {}", err)))?;

Expand All @@ -71,7 +71,7 @@ pub fn credential_create_with_msgid(source_id: &str, connection_handle: u32, msg
let offer = get_credential_offer_msg(connection_handle, &msg_id)?;
trace!("credential_create_with_msgid ::: for msg_id {} found offer {}", msg_id, offer);

let credential = create_credential_v3(source_id, &offer)?
let credential = create_credential(source_id, &offer)?
.ok_or(VcxError::from_msg(VcxErrorKind::InvalidConnectionHandle, format!("Connection can not be used for Proprietary Issuance protocol")))?;

let handle = HANDLE_MAP.add(credential)?;
Expand Down Expand Up @@ -214,9 +214,8 @@ pub fn get_credential_status(handle: u32) -> VcxResult<u32> {
#[cfg(test)]
pub mod tests {
use api::VcxStateType;
use aries::messages::issuance::credential::Credential as CredentialV3;
use aries::messages::issuance::credential::Credential as Credential;
use connection;
use credential_request::CredentialRequest;
use utils::devsetup::*;
use utils::mockdata::mockdata_credex::{ARIES_CREDENTIAL_RESPONSE, CREDENTIAL_SM_FINISHED, CREDENTIAL_SM_OFFER_RECEIVED};

Expand Down Expand Up @@ -295,10 +294,11 @@ pub mod tests {

info!("full_credential_test:: going to get_credential");
let msg = get_credential(handle_cred).unwrap();
info!("full_credential_test:: get_credential returned {}", msg);
let msg_value: serde_json::Value = serde_json::from_str(&msg).unwrap();

info!("full_credential_test:: going to deserialize credential: {:?}", msg_value);
let _credential_struct: CredentialV3 = serde_json::from_str(msg_value.to_string().as_str()).unwrap();
let _credential_struct: Credential = serde_json::from_str(msg_value.to_string().as_str()).unwrap();
}

#[test]
Expand All @@ -318,7 +318,7 @@ pub mod tests {
assert_eq!(VcxStateType::VcxStateRequestReceived as u32, get_state(c_h).unwrap());

let msg = generate_credential_request_msg(c_h, &my_pw_did, &their_pw_did).unwrap();
::serde_json::from_str::<CredentialRequest>(&msg).unwrap();
// ::serde_json::from_str::<CredentialRequest>(&msg).unwrap();
}

#[test]
Expand All @@ -331,7 +331,7 @@ pub mod tests {
let offer = get_credential_offer_messages(connection_h).unwrap();
let o: serde_json::Value = serde_json::from_str(&offer).unwrap();
println!("Serialized credential offer: {:?}", &o[0]);
let _credential_offer: CredentialOfferV3 = serde_json::from_str(&o[0].to_string()).unwrap();
let _credential_offer: CredentialOffer = serde_json::from_str(&o[0].to_string()).unwrap();
}

#[test]
Expand All @@ -353,6 +353,6 @@ pub mod tests {
let handle = from_string(CREDENTIAL_SM_FINISHED).unwrap();
let cred_string: String = get_credential(handle).unwrap();
let cred_value: serde_json::Value = serde_json::from_str(&cred_string).unwrap();
let _credential_struct: CredentialV3 = serde_json::from_str(cred_value.to_string().as_str()).unwrap();
let _credential_struct: Credential = serde_json::from_str(cred_value.to_string().as_str()).unwrap();
}
}
93 changes: 0 additions & 93 deletions libvcx/src/credential_request.rs

This file was deleted.

5 changes: 2 additions & 3 deletions libvcx/src/disclosed_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ pub fn is_valid_handle(handle: u32) -> bool {
HANDLE_MAP.has_handle(handle)
}

//TODO one function with credential
fn get_proof_request(connection_handle: u32, msg_id: &str) -> VcxResult<String> {
if !connection::is_v3_connection(connection_handle)? {
return Err(VcxError::from_msg(VcxErrorKind::InvalidConnectionHandle, format!("Connection can not be used for Proprietary Issuance protocol")));
Expand All @@ -177,8 +176,8 @@ fn get_proof_request(connection_handle: u32, msg_id: &str) -> VcxResult<String>
}

let presentation_request = Prover::get_presentation_request(connection_handle, msg_id)?;
return serde_json::to_string_pretty(&presentation_request)
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidJson, format!("Cannot serialize message: {}", err)));
serde_json::to_string_pretty(&presentation_request)
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidJson, format!("Cannot serialize message: {}", err)))
}

pub fn get_proof_request_messages(connection_handle: u32) -> VcxResult<String> {
Expand Down
1 change: 0 additions & 1 deletion libvcx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub mod messages;
pub mod api;
pub mod connection;
pub mod issuer_credential;
pub mod credential_request;
pub mod proof;
pub mod schema;
pub mod credential_def;
Expand Down
Loading

0 comments on commit 676d793

Please sign in to comment.