Skip to content

Commit

Permalink
Merge remote-tracking branch 'hyperledger-sdk/master' into feature/vc…
Browse files Browse the repository at this point in the history
…x-aries-mode
  • Loading branch information
Artemkaaas committed Jun 22, 2020
2 parents 43e3f20 + 1b9a5db commit d06bc78
Show file tree
Hide file tree
Showing 21 changed files with 153 additions and 329 deletions.
5 changes: 1 addition & 4 deletions libindy/mac.build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ if [[ "$OSTYPE" == "darwin"* ]]; then
export CARGO_INCREMENTAL=1
export RUST_LOG=indy=trace
export RUST_TEST_THREADS=1
for version in `ls -t /usr/local/Cellar/openssl/`; do
export OPENSSL_DIR=/usr/local/Cellar/openssl/$version
break
done
export OPENSSL_DIR=/usr/local/opt/`ls /usr/local/opt/ | grep openssl | sort | tail -1`
cargo build
export LIBRARY_PATH=$(pwd)/target/debug
cd ../cli
Expand Down
4 changes: 2 additions & 2 deletions vcx/libvcx/src/api/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,13 +803,13 @@ pub extern fn vcx_connection_send_message(command_handle: CommandHandle,
msg: *const c_char,
send_msg_options: *const c_char,
cb: Option<extern fn(xcommand_handle: CommandHandle, err: u32, msg_id: *const c_char)>) -> u32 {
info!("vcx_message_send >>>");
info!("vcx_connection_send_message >>>");

check_useful_c_callback!(cb, VcxErrorKind::InvalidOption);
check_useful_c_str!(msg, VcxErrorKind::InvalidOption);
check_useful_c_str!(send_msg_options, VcxErrorKind::InvalidOption);

trace!("vcx_message_send(command_handle: {}, connection_handle: {}, msg: {}, send_msg_options: {})",
trace!("vcx_connection_send_message(command_handle: {}, connection_handle: {}, msg: {}, send_msg_options: {})",
command_handle, connection_handle, msg, send_msg_options);

spawn(move || {
Expand Down
20 changes: 1 addition & 19 deletions vcx/libvcx/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ impl From<(Connection, ActorDidExchangeState)> for ConnectionV3 {
}
}

use v3::messages::a2a::{A2AMessage, MessageId};
use v3::messages::a2a::A2AMessage;
use v3::messages::connection::did_doc::DidDoc;

pub fn get_messages(handle: u32) -> VcxResult<HashMap<String, A2AMessage>> {
Expand Down Expand Up @@ -1294,24 +1294,6 @@ pub fn send_message_to_self_endpoint(message: A2AMessage, did_doc: &DidDoc) -> V
ConnectionV3::send_message_to_self_endpoint(&message, did_doc)
}

pub fn add_pending_messages(handle: u32, messages: HashMap<MessageId, String>) -> VcxResult<()> {
CONNECTION_MAP.get(handle, |connection| {
match connection {
Connections::V1(_) => Err(VcxError::from(VcxErrorKind::InvalidConnectionHandle)),
Connections::V3(ref connection) => connection.add_pending_messages(messages.clone())
}
})
}

pub fn remove_pending_message(handle: u32, id: &MessageId) -> VcxResult<()> {
CONNECTION_MAP.get(handle, |connection| {
match connection {
Connections::V1(_) => Err(VcxError::from(VcxErrorKind::InvalidConnectionHandle)),
Connections::V3(ref connection) => connection.remove_pending_message(id.clone())
}
})
}

pub fn is_v3_connection(connection_handle: u32) -> VcxResult<bool> {
CONNECTION_MAP.get(connection_handle, |connection| {
match connection {
Expand Down
2 changes: 1 addition & 1 deletion vcx/libvcx/src/messages/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub enum PayloadTypes {

#[derive(Clone, Deserialize, Serialize, Debug, PartialEq)]
pub struct PayloadTypeV1 {
name: String,
pub name: String,
ver: String,
fmt: String,
}
Expand Down
1 change: 1 addition & 0 deletions vcx/libvcx/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ macro_rules! secret {
($val:expr) => {{ "_" }};
}

#[cfg(test)]
macro_rules! map (
{ $($key:expr => $value:expr),+ } => {
{
Expand Down
76 changes: 47 additions & 29 deletions vcx/libvcx/src/v3/handlers/connection/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use error::prelude::*;
use v3::handlers::connection::states::{DidExchangeSM, Actor, ActorDidExchangeState};
use v3::handlers::connection::messages::DidExchangeMessages;
use v3::handlers::connection::agent::AgentInfo;
use v3::messages::a2a::{A2AMessage, MessageId};
use v3::messages::a2a::A2AMessage;
use v3::messages::connection::invite::Invitation;

use std::collections::HashMap;
Expand Down Expand Up @@ -126,16 +126,11 @@ impl Connection {
pub fn update_state_with_message(&mut self, message: &str) -> VcxResult<()> {
trace!("Connection: update_state_with_message: {}", message);

let agent_info = self.agent_info().clone();

let message: Message = ::serde_json::from_str(&message)
let message: A2AMessage = ::serde_json::from_str(&message)
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidOption,
format!("Cannot updated state with messages: Message deserialization failed: {:?}", err)))?;

let a2a_message = self.decode_message(&message)?;
self.handle_message(a2a_message.into())?;

agent_info.update_message_status(message.uid)?;
self.handle_message(message.into())?;

Ok(())
}
Expand Down Expand Up @@ -183,25 +178,22 @@ impl Connection {
AgentInfo::send_message_anonymously(message, did_doc)
}

pub fn send_generic_message(&self, message: &str, _message_options: &str) -> VcxResult<String> {
trace!("Connection::send_generic_message >>> message: {:?}", message);

let message = match ::serde_json::from_str::<A2AMessage>(message) {
Ok(A2AMessage::Generic(message)) => {
BasicMessage::create()
.set_content(message.to_string())
.set_time()
.to_a2a_message()
}
fn parse_generic_message(message: &str, _message_options: &str) -> A2AMessage {
match ::serde_json::from_str::<A2AMessage>(message) {
Ok(a2a_message) => a2a_message,
Err(_) => {
BasicMessage::create()
.set_content(message.to_string())
.set_time()
.to_a2a_message()
}
};
}
}

pub fn send_generic_message(&self, message: &str, _message_options: &str) -> VcxResult<String> {
trace!("Connection::send_generic_message >>> message: {:?}", message);

let message = Connection::parse_generic_message(message, _message_options);
self.send_message(&message).map(|_| String::new())
}

Expand All @@ -220,16 +212,6 @@ impl Connection {
Ok(())
}

pub fn add_pending_messages(&self, messages: HashMap<MessageId, String>) -> VcxResult<()> {
trace!("Connection::add_pending_messages >>> messages: {:?}", messages);
self.connection_sm.add_pending_messages(messages)
}

pub fn remove_pending_message(&self, id: MessageId) -> VcxResult<()> {
trace!("Connection::remove_pending_message >>> id: {:?}", id);
self.connection_sm.remove_pending_message(id)
}

pub fn send_discovery_features(&mut self, query: Option<String>, comment: Option<String>) -> VcxResult<()> {
trace!("Connection::send_discovery_features_query >>> query: {:?}, comment: {:?}", query, comment);
self.handle_message(DidExchangeMessages::DiscoverFeatures((query, comment)))
Expand Down Expand Up @@ -284,4 +266,40 @@ struct SideConnectionInfo {
service_endpoint: String,
#[serde(skip_serializing_if = "Option::is_none")]
protocols: Option<Vec<ProtocolDescriptor>>,
}

#[cfg(test)]
mod tests {
use v3::messages::a2a::A2AMessage;
use v3::handlers::connection::connection::Connection;

#[test]
fn test_parse_generic_message_plain_string_should_be_parsed_as_basic_msg() -> Result<(), String> {
let message = "Some plain text message";
let result = Connection::parse_generic_message(message, "");
match result {
A2AMessage::BasicMessage(basic_msg) => {
assert_eq!(basic_msg.content, message);
Ok(())
}
other => Err(format!("Result is not BasicMessage, but: {:?}", other))
}
}

#[test]
fn test_parse_generic_message_json_msg_should_be_parsed_as_generic() -> Result<(), String> {
let message = json!({
"@id": "some id",
"@type": "some type",
"content": "some content"
}).to_string();
let result = Connection::parse_generic_message(&message, "");
match result {
A2AMessage::Generic(value) => {
assert_eq!(value.to_string(), message);
Ok(())
}
other => Err(format!("Result is not Generic, but: {:?}", other))
}
}
}
21 changes: 0 additions & 21 deletions vcx/libvcx/src/v3/handlers/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,27 +138,6 @@ pub mod tests {
::connection::update_message_status(alice.connection_handle, uid).unwrap();
}

// Pending Message
{
faber.activate();

let message = _ack();
::connection::send_message(faber.connection_handle, message.to_a2a_message()).unwrap();

alice.activate();

let messages = ::connection::get_messages(alice.connection_handle).unwrap();
assert_eq!(1, messages.len());
let uid = messages.keys().next().unwrap().clone();

::connection::add_pending_messages(alice.connection_handle, map!( message.id.clone() => uid )).unwrap();

::connection::remove_pending_message(alice.connection_handle, &message.id).unwrap();

let messages = ::connection::get_messages(alice.connection_handle).unwrap();
assert_eq!(0, messages.len());
}

// Download Messages
{
use messages::get_message::{download_messages, MessageByConnection, Message};
Expand Down
28 changes: 0 additions & 28 deletions vcx/libvcx/src/v3/handlers/connection/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ use v3::messages::ack::Ack;
use v3::messages::connection::did_doc::DidDoc;
use v3::messages::discovery::query::Query;
use v3::messages::discovery::disclose::{Disclose, ProtocolDescriptor};
use v3::messages::a2a::MessageId;
use v3::messages::a2a::protocol_registry::ProtocolRegistry;

use std::collections::HashMap;

use error::prelude::*;
use v3::utils::pending_message::PendingMessage;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DidExchangeSM {
Expand Down Expand Up @@ -672,32 +670,6 @@ impl DidExchangeSM {
ActorDidExchangeState::Invitee(_) => Actor::Invitee
}
}

pub fn add_pending_messages(&self, messages: HashMap<MessageId, String>) -> VcxResult<()> {
match self.state {
ActorDidExchangeState::Inviter(DidExchangeState::Completed(ref _state)) |
ActorDidExchangeState::Invitee(DidExchangeState::Completed(ref _state)) => {
for (key, value) in messages {
PendingMessage::add(&key.0, &value).ok();
}
}
_ => {}
};
Ok(())
}

pub fn remove_pending_message(&self, id: MessageId) -> VcxResult<()> {
match self.state {
ActorDidExchangeState::Inviter(DidExchangeState::Completed(ref _state)) |
ActorDidExchangeState::Invitee(DidExchangeState::Completed(ref _state)) => {
if let Some(uid_) = PendingMessage::get(&id.0) {
self.agent_info.update_message_status(uid_.to_string()).ok();
}
}
_ => {}
};
Ok(())
}
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
Expand Down
5 changes: 3 additions & 2 deletions vcx/libvcx/src/v3/handlers/issuance/holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ impl HolderSM {

match self.find_message_to_handle(messages) {
Some((uid, msg)) => {
let state = self.handle_message(msg.into())?;
connection::update_message_status(conn_handle, uid)?;
self.handle_message(msg.into())
Ok(state)

}
None => Ok(self)
}
Expand Down Expand Up @@ -119,7 +121,6 @@ impl HolderSM {
Ok((cred_request, req_meta, cred_def_json)) => {
let cred_request = cred_request
.set_thread_id(&thread_id);
connection::remove_pending_message(connection_handle, &state_data.offer.id)?;
connection::send_message(connection_handle, cred_request.to_a2a_message())?;
HolderState::RequestSent((state_data, req_meta, cred_def_json, connection_handle).into())
}
Expand Down
7 changes: 4 additions & 3 deletions vcx/libvcx/src/v3/handlers/issuance/issuer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use issuer_credential::encode_attributes;
use v3::messages::status::Status;
use std::collections::HashMap;
use connection::{send_message, get_messages};
use connection::update_message_status;
use connection;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct IssuerSM {
Expand Down Expand Up @@ -54,8 +54,9 @@ impl IssuerSM {

match self.find_message_to_handle(messages) {
Some((uid, msg)) => {
update_message_status(conn_handle, uid)?;
self.handle_message(msg.into())
let state = self.handle_message(msg.into())?;
connection::update_message_status(conn_handle, uid)?;
Ok(state)
}
None => Ok(self)
}
Expand Down
36 changes: 14 additions & 22 deletions vcx/libvcx/src/v3/handlers/issuance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ pub mod states;
pub mod messages;
pub mod holder;

use std::collections::HashMap;
use error::prelude::*;
use messages::get_message::Message;
use v3::messages::a2a::{A2AMessage, MessageId};
use v3::messages::a2a::A2AMessage;
use v3::handlers::issuance::issuer::IssuerSM;
use v3::handlers::issuance::messages::CredentialIssuanceMessage;
use v3::handlers::issuance::holder::HolderSM;
Expand Down Expand Up @@ -51,10 +49,9 @@ impl Issuer {
pub fn update_status(&mut self, msg: Option<String>) -> VcxResult<()> {
match msg {
Some(msg) => {
let message: Message = ::serde_json::from_str(&msg)
let message: A2AMessage = ::serde_json::from_str(&msg)
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidOption, format!("Cannot deserialize Message: {:?}", err)))?;

let message = connection::decode_message(self.issuer_sm.get_connection_handle(), message)?;
self.step(message.into())
}
None => {
Expand Down Expand Up @@ -97,10 +94,9 @@ impl Holder {
pub fn update_state(&mut self, msg: Option<String>) -> VcxResult<()> {
match msg {
Some(msg) => {
let message: Message = ::serde_json::from_str(&msg)
let message: A2AMessage = ::serde_json::from_str(&msg)
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidOption, format!("Cannot update state: Message deserialization failed: {:?}", err)))?;

let message = connection::decode_message(self.holder_sm.get_connection_handle(), message)?;
self.step(message.into())
}
None => {
Expand Down Expand Up @@ -134,34 +130,30 @@ impl Holder {
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 (id, credential_offer): (MessageId, CredentialOffer) = match message {
A2AMessage::CredentialOffer(credential_offer) => (credential_offer.id.clone(), credential_offer),
msg => return Err(VcxError::from_msg(VcxErrorKind::InvalidMessages, format!("Message of different type was received: {:?}", msg)))
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)));
}
};

connection::add_pending_messages(connection_handle, map! { id => msg_id.to_string() })?;

Ok(credential_offer)
}

pub fn get_credential_offer_messages(conn_handle: u32) -> VcxResult<Vec<CredentialOffer>> {
let messages = connection::get_messages(conn_handle)?;
let (uids, msgs): (HashMap<MessageId, String>, Vec<CredentialOffer>) = messages
let msgs: Vec<CredentialOffer> = messages
.into_iter()
.filter_map(|(uid, a2a_message)| {
.filter_map(|(_, a2a_message)| {
match a2a_message {
A2AMessage::CredentialOffer(credential_offer) => {
Some((uid, credential_offer.id.clone(), credential_offer))
Some(credential_offer)
}
_ => None
}
}).fold((HashMap::new(), vec![]), |(mut uids, mut msgs), (uid, id, msg)| {
uids.insert(id, uid);
msgs.push(msg);
(uids, msgs)
});

connection::add_pending_messages(conn_handle, uids)?;
})
.collect();

Ok(msgs)
}
Expand Down
Loading

0 comments on commit d06bc78

Please sign in to comment.