Skip to content

Commit

Permalink
Expose filter api and add a basic test
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Kovar <miroslavkovar@protonmail.com>
  • Loading branch information
mirgee committed Oct 9, 2020
1 parent df0a5ba commit 412315e
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 5 deletions.
67 changes: 67 additions & 0 deletions libvcx/src/api/filters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::ptr;

use error::prelude::*;
use utils::error;
use indy_sys::CommandHandle;
use libc::c_char;
use filters;
use utils::threadpool::spawn;
use utils::cstring::CStringUtils;


/// Filters proof requests based on name selected by verifier when creating the request.
///
/// #Params
/// command_handle: command handle to map callback to user context.
/// requests: Serialized array of proof requests JSONs.
///
/// # Example
///
/// match_name: Name of the request to match.
/// #Returns
/// Error code as a u32
#[no_mangle]
pub extern fn vcx_filter_proof_requests_by_name(command_handle: CommandHandle,
requests: *const c_char,
match_name: *const c_char,
cb: Option<extern fn(xcommand_handle: CommandHandle, err: u32, requests: *const c_char)>) -> u32 {
info!("vcx_filter_proof_requests_by_name >>>");

check_useful_c_str!(requests, VcxErrorKind::InvalidOption);
check_useful_c_str!(match_name, VcxErrorKind::InvalidOption);
check_useful_c_callback!(cb, VcxErrorKind::InvalidOption);

trace!("vcx_filter_proof_requests_by_name(command_handle: {}, requests: {}, match_name: {})",
command_handle, requests, match_name);

spawn(move || {
match filters::filter_proof_requests_by_name(&requests, &match_name) {
Ok(x) => {
trace!("vcx_filter_proof_requests_by_name_cb(command_handle: {}, requests: {}, rc: {}, requests: {})",
command_handle, requests, error::SUCCESS.message, x);
let x = CStringUtils::string_to_cstring(x);
cb(command_handle,error::SUCCESS.code_num, x.as_ptr());
}
Err(err) => {
error!("vcx_filter_proof_requests_by_name_cb(command_handle: {}, rc: {}, msg: {})",
command_handle, error::SUCCESS.message, err);
cb(command_handle, err.into(), ptr::null_mut());
}
};

Ok(())
});

error::SUCCESS.code_num
}


#[cfg(test)]
mod tests {
use super::*;

#[test]
#[cfg(feature = "general_test")]
fn test_vcx_filter_proof_requests_by_name() {}

}
1 change: 1 addition & 0 deletions libvcx/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod disclosed_proof;
pub mod wallet;
pub mod logger;
pub mod return_types_u32;
mod filters;

use std::fmt;

Expand Down
10 changes: 5 additions & 5 deletions libvcx/src/aries/handlers/connection/agent_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ impl AgentInfo {
}

#[cfg(feature = "warnlog_fetched_messages")]
{
for message in a2a_messages.values() {
let serialized_msg = serde_json::to_string_pretty(message).unwrap_or_else(|_err| String::from("Failed to serialize A2AMessage."));
warn!("Fetched decrypted connection messages:\n{}", serialized_msg);
}
{
for message in a2a_messages.values() {
let serialized_msg = serde_json::to_string_pretty(message).unwrap_or_else(|_err| String::from("Failed to serialize A2AMessage."));
warn!("Fetched decrypted connection messages:\n{}", serialized_msg);
}
}
Ok(a2a_messages)
}

Expand Down
70 changes: 70 additions & 0 deletions libvcx/src/filters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use serde_json;

use error::prelude::*;
use utils::error;

use aries::messages::proof_presentation::presentation_request::PresentationRequest;

// TODO: Log errors
fn _filter_proof_requests_by_name(requests: &str, match_name: &str) -> VcxResult<Vec<PresentationRequest>> {
let presentation_requests: Vec<PresentationRequest> = serde_json::from_str(requests)
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidJson, format!("Failed to deserialize Vec<PresentationRequest>: {}\nObtained error: {:?}", requests, err)))?;
let filtered = presentation_requests
.into_iter()
.filter_map(|presentation_request| {
match presentation_request.request_presentations_attach.content().ok() {
Some(content) => {
match serde_json::from_str(&content).map(|value: serde_json::Value| value.get("name").unwrap_or(&serde_json::Value::Null).as_str().unwrap_or("").to_string()) {
Ok(name) if name == String::from(match_name) => Some(presentation_request),
_ => None
}
}
_ => None
}
})
.collect();
Ok(filtered)
}

pub fn filter_proof_requests_by_name(requests: &str, name: &str) -> VcxResult<String> {
let presentation_requests: Vec<PresentationRequest> = _filter_proof_requests_by_name(requests, name)?;
let filtered: String = serde_json::to_string(&presentation_requests)
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidJson, format!("Failed to serialize filtered proof requests: {}\nObtained error: {:?}", requests, err)))?;
Ok(filtered)
}

#[cfg(test)]
pub mod tests {
use super::*;
use utils::constants::*;
use utils::devsetup::*;
use utils::httpclient::HttpClientMockResponse;
use utils::mockdata::mockdata_proof;

#[test]
#[cfg(feature = "general_test")]
fn test_filter_proof_requests_by_name() {
let filtered = _filter_proof_requests_by_name(mockdata_proof::presentation_request_message_array, "request1").unwrap();
assert_eq!(filtered.len(), 1);

let filtered = _filter_proof_requests_by_name(mockdata_proof::presentation_request_message_array, "request2").unwrap();
assert_eq!(filtered.len(), 1);

let filtered = _filter_proof_requests_by_name(mockdata_proof::presentation_request_message_array, "not there").unwrap();
assert_eq!(filtered.len(), 0);

let filtered = _filter_proof_requests_by_name(mockdata_proof::presentation_request_message_array, "").unwrap();
assert_eq!(filtered.len(), 0);

let filtered = _filter_proof_requests_by_name(mockdata_proof::presentation_request_message_array_empty_attach, "not there").unwrap();
assert_eq!(filtered.len(), 0);

// TODO: fix the behavior so that this passes
let filtered = _filter_proof_requests_by_name(mockdata_proof::presentation_request_message_array_empty_attach, "").unwrap();
assert_eq!(filtered.len(), 0);
}

#[test]
#[cfg(feature = "general_test")]
fn test_filter_proof_requests_by_name_serialize_deserialize() {}
}
1 change: 1 addition & 0 deletions libvcx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub mod disclosed_proof;
pub mod aries;
mod proof_utils;
mod disclosed_proof_utils;
mod filters;

#[allow(unused_imports)]
#[allow(dead_code)]
Expand Down
62 changes: 62 additions & 0 deletions libvcx/src/utils/mockdata/mockdata_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,65 @@ pub static SERIALIZIED_PROOF_REVOKED: &str = r#"
}
}
"#;

pub static PRESENTATION_REQUEST_MESSAGE_ARRAY: &str = r#"
[
{
"@id": "testid",
"comment": "institution wants you to share request1",
"request_presentations~attach": [
{
"@id": "libindy-request-presentation-0",
"data": {
"base64": "eyJuYW1lIjoicmVxdWVzdDEiLCJub25fcmV2b2tlZCI6bnVsbCwibm9uY2UiOiI5OTQwMTI3MjM1MDE3MTg4NTgyMzI3MTEiLCJyZXF1ZXN0ZWRfYXR0cmlidXRlcyI6eyJhdHRyaWJ1dGVfMCI6eyJuYW1lIjoiQWRkcmVzczEiLCJub25fcmV2b2tlZCI6eyJmcm9tIjpudWxsLCJ0byI6bnVsbH0sInJlc3RyaWN0aW9ucyI6eyIkb3IiOlt7IiRhbmQiOlt7ImNyZWRfZGVmX2lkIjoiMjRVaHhMYWVRRU40cG1vQkNydkFScjozOkNMOjEyNzp0YWcxIn0seyJpc3N1ZXJfZGlkIjoiMjRVaHhMYWVRRU40cG1vQkNydkFSciJ9LHsic2NoZW1hX2lkIjoiMjRVaHhMYWVRRU40cG1vQkNydkFScjoyOnlzbHo5TnJqSG1pUlJ0Um1XdDhrdWZHZmU6Mzk0MjY3MDIuNTIzNDEzNzEwIn1dfV19fSwiYXR0cmlidXRlXzEiOnsibmFtZSI6ImFkZHJlc3MyIiwibm9uX3Jldm9rZWQiOnsiZnJvbSI6bnVsbCwidG8iOm51bGx9LCJyZXN0cmljdGlvbnMiOnsiJG9yIjpbeyIkYW5kIjpbeyJjcmVkX2RlZl9pZCI6IjI0VWh4TGFlUUVONHBtb0JDcnZBUnI6MzpDTDoxMjc6dGFnMSJ9LHsiaXNzdWVyX2RpZCI6IjI0VWh4TGFlUUVONHBtb0JDcnZBUnIifSx7InNjaGVtYV9pZCI6IjI0VWh4TGFlUUVONHBtb0JDcnZBUnI6Mjp5c2x6OU5yakhtaVJSdFJtV3Q4a3VmR2ZlOjM5NDI2NzAyLjUyMzQxMzcxMCJ9XX1dfX0sImF0dHJpYnV0ZV8yIjp7Im5hb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x6OU5yakhtaVJSdFJtV3Q4a3VmR2ZlOjM5NDI2NzAyLjUyMzQxMzcxMCJ9XX1dfX19LCJyZXF1ZXN0ZWRfcHJlZGljYXRlcyI6e30sInZlciI6IjEuMCIsInZlcnNpb24iOiIxLjAifQ=="
},
"mime-type": "application/json"
}
]
},
{
"@id": "testid",
"comment": "institution wants you to share request2",
"request_presentations~attach": [
{
"@id": "libindy-request-presentation-0",
"data": {
"base64": "eyJuYW1lIjoicmVxdWVzdDIiLCJub25fcmV2b2tlZCI6bnVsbCwibm9uY2UiOiI3NjI0MTI1ODE0NDgxMDU3OTY1NjUxMTkiLCJyZXF1ZXN0ZWRfYXR0cmlidXRlcyI6eyJhdHRyaWJ1dGVfMCI6eyJuYW1lIjoiQWRkcmVzczEiLCJub25fcmV2b2tlZCI6eyJmcm9tIjpudWxsLCJ0byI6bnVsbH0sInJlc3RyaWN0aW9ucyI6eyIkb3IiOlt7IiRhbmQiOlt7ImNyZWRfZGVmX2lkIjoiMjRVaHhMYWVRRU40cG1vQkNydkFScjozOkNMOjEyNzp0YWcxIn0seyJpc3N1ZXJfZGlkIjoiMjRVaHhMYWVRRU40cG1vQkNydkFSciJ9LHsic2NoZW1hX2lkIjoiMjRVaHhMYWVRRU40cG1vQkNydkFScjoyOnlzbHo5TnJqSG1pUlJ0Um1XdDhrdWZHZmU6Mzk0MjY3MDIuNTIzNDEzNzEwIn1dfV19fSwiYXR0cmlidXRlXzEiOnsibmFtZSI6ImFkZHJlc3MyIiwibm9uX3Jldm9rZWQiOnsiZnJvbSI6bnVsbCwidG8iOm51bGx9LCJyZXN0cmljdGlvbnMiOnsiJG9yIjpbeyIkYW5kIjpbeyJjcmVkX2RlZl9pZCI6IjI0VWh4TGFlUUVONHBtb0JDcnZBUnI6MzpDTDoxMjc6dGFnMSJ9LHsiaXNzdWVyX2RpZCI6IjI0VWh4TGFlUUVONHBtb0JDcnZBUnIifSx7InNjaGVtYV9pZCI6IjI0VWh4TGFlUUVONHBtb0JDc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LHsiaXNzdWVyX2RpZCI6IjI0VWh4TGFlUUVONHBtb0JDcnZBUnIifSx7InNjaGVtYV9pZCI6IjI0VWh4TGFlUUVONHBtb0JDcnZBUnI6Mjp5c2x6OU5yakhtaVJSdFJtV3Q4a3VmR2ZlOjM5NDI2NzAyLjUyMzQxMzcxMCJ9XX1dfX19LCJyZXF1ZXN0ZWRfcHJlZGljYXRlcyI6e30sInZlciI6IjEuMCIsInZlcnNpb24iOiIxLjAifQ=="
},
"mime-type": "application/json"
}
]
}
]
"#;

pub static PRESENTATION_REQUEST_MESSAGE_ARRAY_EMPTY_ATTACH: &str = r#"
[
{
"@id": "testid",
"comment": "institution wants you to share request1",
"request_presentations~attach": [
{
"@id": "libindy-request-presentation-0",
"data": {
"base64": "e30="
},
"mime-type": "application/json"
}
]
},
{
"@id": "testid",
"comment": "institution wants you to share request2",
"request_presentations~attach": [
{
"@id": "libindy-request-presentation-0",
"data": {
"base64": "e30="
},
"mime-type": "application/json"
}
]
}
]
"#;

0 comments on commit 412315e

Please sign in to comment.