Skip to content

Commit

Permalink
Merge pull request hyperledger-archives#1545 from Artemkaaas/bugfix/w…
Browse files Browse the repository at this point in the history
…ire-credentials

IS-1206: Corrected preparation of A2A messages for 2.0 protocol version
  • Loading branch information
jovfer authored Mar 21, 2019
2 parents 2a3c221 + a6ea640 commit 585a3b3
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 119 deletions.
56 changes: 27 additions & 29 deletions libindy/tests/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1851,18 +1851,24 @@ mod high_cases {

#[test]
#[cfg(feature = "local_nodes_pool")]
fn indy_auth_rule_request_works() {
fn indy_auth_rule_requests_work() {
let (wallet_handle, pool_handle, trustee_did) = utils::setup_trustee();

let auth_rule_request = ledger::build_auth_rule_request(&trustee_did,
constants::NYM,
&ADD_AUTH_ACTION,
FIELD,
None,
NEW_VALUE,
ROLE_CONSTRAINT).unwrap();
let response = ledger::sign_and_submit_request(pool_handle, wallet_handle, &trustee_did, &auth_rule_request).unwrap();
pool::check_response_type(&response, ResponseType::REPLY);
let constraint_id = _build_constraint_id(ADD_AUTH_ACTION, constants::NYM, FIELD, None, NEW_VALUE);

let default_constraint = _get_constraint(pool_handle, &constraint_id);

_change_constraint(pool_handle, wallet_handle, &trustee_did, ROLE_CONSTRAINT);

::std::thread::sleep(::std::time::Duration::from_secs(1));

let actual_constraint = _get_constraint(pool_handle, &constraint_id);

let expected_constraint: serde_json::Value = serde_json::from_str(ROLE_CONSTRAINT).unwrap();

assert_eq!(expected_constraint, actual_constraint);

_change_constraint(pool_handle, wallet_handle, &trustee_did, &serde_json::to_string(&default_constraint).unwrap());

utils::tear_down_with_wallet_and_pool(wallet_handle, pool_handle);
}
Expand All @@ -1875,44 +1881,36 @@ mod high_cases {
format!("{}--{}--{}--{}--{}", auth_action, auth_type, field, old_value.unwrap_or("*"), new_value)
}

#[test]
#[cfg(feature = "local_nodes_pool")]
fn indy_get_auth_rule_request_works_for_one() {
let (wallet_handle, pool_handle, trustee_did) = utils::setup_trustee();

fn _change_constraint(pool_handle: i32, wallet_handle: i32, trustee_did: &str, constraint: &str) {
let auth_rule_request = ledger::build_auth_rule_request(&trustee_did,
constants::NYM,
&ADD_AUTH_ACTION,
FIELD,
None,
NEW_VALUE,
ROLE_CONSTRAINT).unwrap();
constraint).unwrap();
let response = ledger::sign_and_submit_request(pool_handle, wallet_handle, &trustee_did, &auth_rule_request).unwrap();
pool::check_response_type(&response, ResponseType::REPLY);
}

fn _get_constraint(pool_handle: i32, constraint_id: &str) -> serde_json::Value {
let get_auth_rule_request = ledger::build_get_auth_rule_request(None,
Some(constants::NYM),
Some(ADD_AUTH_ACTION),
Some(FIELD),
None,
Some(NEW_VALUE)).unwrap();

let constraint_id = _build_constraint_id(ADD_AUTH_ACTION, constants::NYM, FIELD, None, NEW_VALUE);

let response = ledger::submit_request(pool_handle, &get_auth_rule_request).unwrap();
let response: Reply<serde_json::Value> = serde_json::from_str(&response).unwrap();

let expected_constraint: serde_json::Value = serde_json::from_str(ROLE_CONSTRAINT).unwrap();
_extract_constraint(&response, constraint_id)
}

fn _extract_constraint(response: &str, constraint_id: &str) -> serde_json::Value {
let response: Reply<serde_json::Value> = serde_json::from_str(response).unwrap();
let constraints = response.result["data"].as_object().unwrap();
assert_eq!(constraints.len(), 1);

assert!(constraints.contains_key(&constraint_id));

let actual_constraint = constraints[&constraint_id].clone();

assert_eq!(expected_constraint, actual_constraint);

utils::tear_down_with_wallet_and_pool(wallet_handle, pool_handle);
assert!(constraints.contains_key(constraint_id));
constraints[constraint_id].clone()
}

#[test]
Expand Down
14 changes: 11 additions & 3 deletions vcx/dummy-cloud-agent/src/actors/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,23 @@ impl Agent {
})
.and_then(|(sender_vk, mut msgs), slf, _| {
match msgs.pop() {
Some(A2AMessage::Version1(A2AMessageV1::Forward(msg))) |
Some(A2AMessage::Version2(A2AMessageV2::Forward(msg))) => {
Some(A2AMessage::Version1(A2AMessageV1::Forward(msg))) => {
slf.router
.send(RouteA2AMsg(msg.fwd, msg.msg))
.from_err()
.and_then(|res| res)
.into_actor(slf)
.into_box()
}
Some(A2AMessage::Version2(A2AMessageV2::Forward(msg))) => {
let msg_ = ftry_act!(slf, serde_json::to_vec(&msg.msg));
slf.router
.send(RouteA2AMsg(msg.fwd, msg_))
.from_err()
.and_then(|res| res)
.into_actor(slf)
.into_box()
}
Some(msg) => slf.handle_agent_msg(sender_vk, msg),
_ => err_act!(slf, err_msg("Unsupported message"))
}
Expand Down Expand Up @@ -751,7 +759,7 @@ mod tests {
status_code: MessageStatusCode::Created,
sender_did: EDGE_PAIRWISE_DID.to_string(),
type_: RemoteMessageType::CredOffer,
payload: Some(to_i8(&PAYLOAD.to_vec())),
payload: Some(MessageDetailPayload::V1(to_i8(&PAYLOAD.to_vec()))),
ref_msg_id: None,
}]
};
Expand Down
28 changes: 22 additions & 6 deletions vcx/dummy-cloud-agent/src/actors/agent_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,18 @@ impl AgentConnection {
let reply_to_msg_id = msg.reply_to_msg_id.clone();
let sender_verkey = sender_verkey.to_string();

let msg_ = ftry_act!(self, {serde_json::to_vec(&msg.msg)});

let msg_detail = GeneralMessageDetail {
msg: msg_,
title: msg.title,
detail: msg.detail,
};

future::ok(())
.into_actor(self)
.and_then(move |_, slf, _| {
slf.handle_create_general_message(mtype, msg.into(), reply_to_msg_id.clone(), Some(uid), sender_verkey)
slf.handle_create_general_message(mtype, msg_detail, reply_to_msg_id.clone(), Some(uid), sender_verkey)
.map(|(msg_uid, a2a_msgs), _, _| (msg_uid, a2a_msgs, reply_to_msg_id))
})
.and_then(move |(msg_uid, a2a_msgs, reply_to_msg_id), slf, _| {
Expand Down Expand Up @@ -518,7 +526,12 @@ impl AgentConnection {
type_: message._type.clone(),
payload: match exclude_payload.as_ref().map(String::as_str) {
Some("Y") => None,
_ => message.payload.as_ref().map(|payload| to_i8(payload))
_ => message.payload.as_ref().map(|payload| {
match ProtocolType::get() {
ProtocolTypes::V1 => MessageDetailPayload::V1(to_i8(payload)),
ProtocolTypes::V2 => MessageDetailPayload::V2(serde_json::from_slice(&payload).unwrap()), // TODO: FIXME
}
})
},
ref_msg_id: message.ref_msg_id.clone(),
}
Expand Down Expand Up @@ -1182,8 +1195,11 @@ impl AgentConnection {
fwd, message);

let message = match ProtocolType::get() {
ProtocolTypes::V1 => A2AMessage::Version1(A2AMessageV1::Forward(Forward { fwd: fwd.to_string(), msg: message })),
ProtocolTypes::V2 => A2AMessage::Version2(A2AMessageV2::Forward(Forward { fwd: fwd.to_string(), msg: message }))
ProtocolTypes::V1 => A2AMessage::Version1(A2AMessageV1::Forward(ForwardV1 { fwd: fwd.to_string(), msg: message })),
ProtocolTypes::V2 => A2AMessage::Version2(A2AMessageV2::Forward(ForwardV2 {
fwd: fwd.to_string(),
msg: serde_json::from_slice(message.as_slice())?
}))
};

Ok(vec![message])
Expand Down Expand Up @@ -1322,7 +1338,7 @@ impl AgentConnection {
id: message.uid,
send_msg: false,
reply_to_msg_id: reply_to.map(String::from),
msg,
msg: serde_json::from_slice(&msg)?,
title,
detail
};
Expand Down Expand Up @@ -1515,7 +1531,7 @@ mod tests {
status_code: MessageStatusCode::Created,
sender_did: EDGE_PAIRWISE_DID.to_string(),
type_: RemoteMessageType::CredOffer,
payload: Some(to_i8(&PAYLOAD.to_vec())),
payload: Some(MessageDetailPayload::V1(to_i8(&PAYLOAD.to_vec()))),
ref_msg_id: None,
};
assert_eq!(expected_message, messages[0]);
Expand Down
24 changes: 16 additions & 8 deletions vcx/dummy-cloud-agent/src/actors/forward_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,23 @@ impl ForwardAgent {
.into_actor(slf)
})
.and_then(move |mut msgs, slf, _| {
let send_to_router = |fwd: String, msg: Vec<u8>| {
slf.router
.send(RouteA2AMsg(fwd, msg))
.from_err()
.and_then(|res| res)
.into_actor(slf)
.into_box()
};


match msgs.pop() {
Some(A2AMessage::Version1(A2AMessageV1::Forward(msg))) |
Some(A2AMessage::Version1(A2AMessageV1::Forward(msg))) => {
send_to_router(msg.fwd, msg.msg)
}
Some(A2AMessage::Version2(A2AMessageV2::Forward(msg))) => {
slf.router
.send(RouteA2AMsg(msg.fwd, msg.msg))
.from_err()
.and_then(|res| res)
.into_actor(slf)
.into_box()
let msg_ = ftry_act!(slf, serde_json::to_vec(&msg.msg));
send_to_router(msg.fwd, msg_)
}
_ => err_act!(slf, err_msg("Unsupported message"))
}
Expand Down Expand Up @@ -264,7 +272,7 @@ impl ForwardAgent {
with_pairwise_did_verkey: my_verkey,
});

A2AMessage::pack_v2(slf.wallet_handle, Some(&slf.verkey), &their_verkey,& msg)
A2AMessage::pack_v2(slf.wallet_handle, Some(&slf.verkey), &their_verkey, &msg)
.map_err(|err| err.context("Can't bundle and authcrypt connected message.").into())
.into_actor(slf)
})
Expand Down
43 changes: 24 additions & 19 deletions vcx/dummy-cloud-agent/src/domain/a2a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use domain::payload::Thread;
#[derive(Debug)]
pub enum A2AMessageV1 {
/// base
Forward(Forward),
Forward(ForwardV1),

/// onboarding
Connect(Connect),
Expand Down Expand Up @@ -62,7 +62,7 @@ pub enum A2AMessageV1 {
#[derive(Debug)]
pub enum A2AMessageV2 {
/// base
Forward(Forward),
Forward(ForwardV2),

/// onboarding
Connect(Connect),
Expand Down Expand Up @@ -112,14 +112,22 @@ pub enum A2AMessage {
Version2(A2AMessageV2),
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Forward {
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct ForwardV1 {
#[serde(rename = "@fwd")]
pub fwd: String,
#[serde(rename = "@msg")]
pub msg: Vec<u8>,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct ForwardV2 {
#[serde(rename = "@fwd")]
pub fwd: String,
#[serde(rename = "@msg")]
pub msg: Value,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Connect {
#[serde(rename = "fromDID")]
Expand Down Expand Up @@ -270,7 +278,14 @@ pub struct MessagesByConnections {
pub msgs: Vec<MessagesByConnection>,
}

#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
#[derive(Debug, Deserialize, Serialize, PartialEq)]
#[serde(untagged)]
pub enum MessageDetailPayload {
V1(Vec<i8>),
V2(serde_json::Value),
}

#[derive(Debug, Deserialize, Serialize, PartialEq)]
pub struct GetMessagesDetailResponse {
pub uid: String,
#[serde(rename = "statusCode")]
Expand All @@ -279,7 +294,7 @@ pub struct GetMessagesDetailResponse {
pub sender_did: String,
#[serde(rename = "type")]
pub type_: RemoteMessageType,
pub payload: Option<Vec<i8>>,
pub payload: Option<MessageDetailPayload>,
#[serde(rename = "refMsgId")]
pub ref_msg_id: Option<String>,
}
Expand Down Expand Up @@ -421,16 +436,6 @@ pub struct GeneralMessageDetail {
pub detail: Option<String>,
}

impl From<SendRemoteMessage> for GeneralMessageDetail {
fn from(message: SendRemoteMessage) -> GeneralMessageDetail {
GeneralMessageDetail {
msg: message.msg,
title: message.title,
detail: message.detail,
}
}
}

#[derive(Debug, Deserialize, Serialize)]
pub struct SendMessageDetail {
#[serde(rename = "@msg")]
Expand Down Expand Up @@ -562,7 +567,7 @@ pub struct SendRemoteMessage {
#[serde(rename = "sendMsg")]
pub send_msg: bool,
#[serde(rename = "@msg")]
pub msg: Vec<u8>,
pub msg: serde_json::Value,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -713,7 +718,7 @@ impl<'de> Deserialize<'de> for A2AMessageV1 {

match message_type.name.as_str() {
"FWD" => {
Forward::deserialize(value)
ForwardV1::deserialize(value)
.map(|msg| A2AMessageV1::Forward(msg))
.map_err(de::Error::custom)
}
Expand Down Expand Up @@ -884,7 +889,7 @@ impl<'de> Deserialize<'de> for A2AMessageV2 {

match message_type.type_.as_str() {
"FWD" => {
Forward::deserialize(value)
ForwardV2::deserialize(value)
.map(|msg| A2AMessageV2::Forward(msg))
.map_err(de::Error::custom)
}
Expand Down
2 changes: 1 addition & 1 deletion vcx/dummy-cloud-agent/src/domain/a2connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub enum A2ConnMessage {
MessageStatusUpdatedByConnection(UidByConnection),
}

#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
#[derive(Debug, Deserialize, Serialize, PartialEq)]
pub struct MessagesByConnection {
#[serde(rename = "pairwiseDID")]
#[serde(default)]
Expand Down
4 changes: 2 additions & 2 deletions vcx/dummy-cloud-agent/src/utils/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ pub fn compose_remove_configs(wallet_handle: i32, agent_did: &str, agent_verkey:

pub fn compose_forward(wallet_handle: i32, recipient_did: &str, recipient_vk: &str, msg: Vec<u8>) -> BoxedFuture<Vec<u8>, Error> {
let msgs = [A2AMessage::Version1(A2AMessageV1::Forward(
Forward {
ForwardV1 {
fwd: recipient_did.into(),
msg,
}))];
Expand All @@ -633,7 +633,7 @@ pub fn compose_forward(wallet_handle: i32, recipient_did: &str, recipient_vk: &s

pub fn compose_authcrypted_forward(wallet_handle: i32, sender_vk: &str, recipient_did: &str, recipient_vk: &str, msg: Vec<u8>) -> BoxedFuture<Vec<u8>, Error> {
let msgs = [A2AMessage::Version1(A2AMessageV1::Forward(
Forward {
ForwardV1 {
fwd: recipient_did.into(),
msg,
}))];
Expand Down
Loading

0 comments on commit 585a3b3

Please sign in to comment.