Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove legacy issuer credential #116

Merged
merged 5 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 19 additions & 99 deletions libvcx/src/api/issuer_credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ use utils::threadpool::spawn;
There are two communication methods: `proprietary` and `aries`. The default communication method is `proprietary`.
The communication method can be specified as a config option on one of *_init functions.

proprietary:
VcxStateType::VcxStateInitialized - once `vcx_issuer_create_credential` (create IssuerCredential object) is called.

VcxStateType::VcxStateOfferSent - once `vcx_issuer_send_credential_offer` (send `CRED_OFFER` message) is called.

VcxStateType::VcxStateRequestReceived - once `CRED_REQ` messages is received.
use `vcx_issuer_credential_update_state` or `vcx_issuer_credential_update_state_with_message` functions for state updates.
VcxStateType::VcxStateAccepted - once `vcx_issuer_send_credential` (send `CRED` message) is called.

aries:
VcxStateType::VcxStateInitialized - once `vcx_issuer_create_credential` (create IssuerCredential object) is called.

VcxStateType::VcxStateOfferSent - once `vcx_issuer_send_credential_offer` (send `CredentialOffer` message) is called.
Expand All @@ -43,17 +33,7 @@ use utils::threadpool::spawn;
VcxStateType::VcxStateAccepted - once `vcx_issuer_send_credential` (send `Credential` message) is called.

# Transitions

proprietary:
VcxStateType::None - `vcx_issuer_create_credential` - VcxStateType::VcxStateInitialized

VcxStateType::VcxStateInitialized - `vcx_issuer_send_credential_offer` - VcxStateType::VcxStateOfferSent

VcxStateType::VcxStateOfferSent - received `CRED_REQ` - VcxStateType::VcxStateRequestReceived

VcxStateType::VcxStateRequestReceived - `vcx_issuer_send_credential` - VcxStateType::VcxStateAccepted

aries: RFC - https://github.com/hyperledger/aries-rfcs/tree/7b6b93acbaf9611d3c892c4bada142fe2613de6e/features/0036-issue-credential
RFC - https://github.com/hyperledger/aries-rfcs/tree/7b6b93acbaf9611d3c892c4bada142fe2613de6e/features/0036-issue-credential
VcxStateType::None - `vcx_issuer_create_credential` - VcxStateType::VcxStateInitialized

VcxStateType::VcxStateInitialized - `vcx_issuer_send_credential_offer` - VcxStateType::VcxStateOfferSent
Expand All @@ -66,13 +46,6 @@ use utils::threadpool::spawn;
VcxStateType::VcxStateAccepted - received `Ack` - VcxStateType::VcxStateAccepted

# Messages

proprietary:
CredentialOffer (`CRED_OFFER`)
CredentialRequest (`CRED_REQ`)
Credential (`CRED`)

aries:
CredentialProposal - https://github.com/hyperledger/aries-rfcs/tree/7b6b93acbaf9611d3c892c4bada142fe2613de6e/features/0036-issue-credential#propose-credential
CredentialOffer - https://github.com/hyperledger/aries-rfcs/tree/7b6b93acbaf9611d3c892c4bada142fe2613de6e/features/0036-issue-credential#offer-credential
CredentialRequest - https://github.com/hyperledger/aries-rfcs/tree/7b6b93acbaf9611d3c892c4bada142fe2613de6e/features/0036-issue-credential#request-credential
Expand Down Expand Up @@ -724,26 +697,6 @@ pub extern fn vcx_issuer_credential_release(credential_handle: u32) -> u32 {
}
}

/// Retrieve the payment transaction associated with this credential. This can be used to get the txn that
/// was used to pay the issuer from the holder.
/// This could be considered a receipt of payment from the payer to the issuer.
///
/// #param
/// handle: issuer_credential handle that was provided during creation. Used to access issuer_credential object.
///
/// #Callback returns
/// PaymentTxn json
/// example: {
/// "amount":25,
/// "inputs":[
/// "pay:null:1_3FvPC7dzFbQKzfG",
/// "pay:null:1_lWVGKc07Pyc40m6"
/// ],
/// "outputs":[
/// {"recipient":"pay:null:FrSVC3IrirScyRh","amount":5,"extra":null},
/// {"recipient":"pov:null:OsdjtGKavZDBuG2xFw2QunVwwGs5IB3j","amount":25,"extra":null}
/// ]
/// }
#[no_mangle]
pub extern fn vcx_issuer_credential_get_payment_txn(command_handle: CommandHandle,
handle: u32,
Expand All @@ -756,31 +709,8 @@ pub extern fn vcx_issuer_credential_get_payment_txn(command_handle: CommandHandl
trace!("vcx_issuer_credential_get_payment_txn(command_handle: {}) source_id: {}", command_handle, source_id);

spawn(move || {
match issuer_credential::get_payment_txn(handle) {
Ok(x) => {
match serde_json::to_string(&x) {
Ok(x) => {
trace!("vcx_issuer_credential_get_payment_txn_cb(command_handle: {}, rc: {}, : {}) source_id: {}",
command_handle, error::SUCCESS.message, x, source_id);

let msg = CStringUtils::string_to_cstring(x);
cb(command_handle, 0, msg.as_ptr());
}
Err(e) => {
let err = VcxError::from_msg(VcxErrorKind::InvalidJson, format!("Cannot serialize payment txn: {}", e));
error!("vcx_issuer_credential_get_payment_txn_cb(command_handle: {}, rc: {}, txn: {}) source_id: {}",
command_handle, err, "null", source_id);
cb(command_handle, err.into(), ptr::null_mut());
}
}
}
Err(x) => {
error!("vcx_issuer_credential_get_payment_txn_cb(command_handle: {}, rc: {}, txn: {}) source_id: {}",
command_handle, x, "null", source_id);
cb(command_handle, x.into(), ptr::null());
}
};

error!("Payments not supported yet");
mirgee marked this conversation as resolved.
Show resolved Hide resolved
cb(command_handle, 1, ptr::null());
Ok(())
});

Expand Down Expand Up @@ -883,7 +813,7 @@ pub mod tests {
use utils::constants::*;
use utils::devsetup::*;
use utils::httpclient::AgencyMockDecrypted;
use utils::mockdata::mockdata_credex::{ARIES_CREDENTIAL_REQUEST};
use utils::mockdata::mockdata_credex::{ARIES_CREDENTIAL_REQUEST, CREDENTIAL_ISSUER_SM_FINISHED};
use utils::timeout::TimeoutUtils;

use super::*;
Expand Down Expand Up @@ -975,8 +905,7 @@ pub mod tests {
#[test]
#[cfg(feature = "general_test")]
fn test_vcx_issuer_create_credential_success() {
let _setup = SetupAriesMocks::init();
settings::set_config_value(settings::CONFIG_PROTOCOL_TYPE, "4.0");
let _setup = SetupStrictAriesMocks::init();

let handle = _vcx_issuer_create_credential_c_closure().unwrap();
assert!(handle > 0);
Expand All @@ -985,8 +914,7 @@ pub mod tests {
#[test]
#[cfg(feature = "general_test")]
fn test_vcx_issuer_create_credential_fails() {
let _setup = SetupAriesMocks::init();
settings::set_config_value(settings::CONFIG_PROTOCOL_TYPE, "4.0");
let _setup = SetupStrictAriesMocks::init();

let cb = return_types_u32::Return_U32_U32::new().unwrap();
assert_eq!(vcx_issuer_create_credential(cb.command_handle,
Expand All @@ -1005,8 +933,7 @@ pub mod tests {
#[test]
#[cfg(feature = "general_test")]
fn test_vcx_issuer_credential_serialize_deserialize() {
let _setup = SetupAriesMocks::init();
settings::set_config_value(settings::CONFIG_PROTOCOL_TYPE, "4.0");
let _setup = SetupStrictAriesMocks::init();

let handle = _vcx_issuer_create_credential_c_closure().unwrap();

Expand All @@ -1031,8 +958,7 @@ pub mod tests {
#[test]
#[cfg(feature = "general_test")]
fn test_vcx_issuer_send_credential_offer() {
let _setup = SetupAriesMocks::init();
settings::set_config_value(settings::CONFIG_PROTOCOL_TYPE, "4.0");
let _setup = SetupStrictAriesMocks::init();

let connection_handle = ::connection::tests::build_test_connection_inviter_requested();

Expand All @@ -1058,8 +984,7 @@ pub mod tests {
#[test]
#[cfg(feature = "general_test")]
fn test_vcx_issuer_update_state_v2() {
let _setup = SetupAriesMocks::init();
settings::set_config_value(settings::CONFIG_PROTOCOL_TYPE, "4.0");
let _setup = SetupStrictAriesMocks::init();

let connection_handle = ::connection::tests::build_test_connection_inviter_requested();
let handle = _vcx_issuer_create_credential_c_closure().unwrap();
Expand Down Expand Up @@ -1094,8 +1019,7 @@ pub mod tests {
#[cfg(feature = "general_test")]
#[cfg(feature = "to_restore")] // TODO: generate_credential_offer_msg() not implemented for V3 aries credential
fn test_vcx_issuer_get_credential_offer_msg() {
let _setup = SetupAriesMocks::init();
settings::set_config_value(settings::CONFIG_PROTOCOL_TYPE, "4.0");
let _setup = SetupStrictAriesMocks::init();

let handle = _vcx_issuer_create_credential_c_closure().unwrap();

Expand All @@ -1114,8 +1038,7 @@ pub mod tests {
// instead it should be using connection handle passed to vcx_issuer_send_credential
// but in Aries, it's not doing so
fn test_vcx_issuer_send_a_credential() {
let _setup = SetupAriesMocks::init();
settings::set_config_value(settings::CONFIG_PROTOCOL_TYPE, "4.0");
let _setup = SetupStrictAriesMocks::init();

info!("test_vcx_issuer_send_a_credential:: going to build_test_connection");
let handle_conn = ::connection::tests::build_test_connection_inviter_invited();
Expand All @@ -1136,11 +1059,12 @@ pub mod tests {
cb.receive(TimeoutUtils::some_medium()).unwrap();
}

// TODO: get_credential_msg not implemented yet for v3
#[test]
#[cfg(feature = "general_test")]
#[cfg(feature = "to_restore")]
fn test_vcx_issuer_get_credential_msg() {
let _setup = SetupAriesMocks::init();
settings::set_config_value(settings::CONFIG_PROTOCOL_TYPE, "4.0");
let _setup = SetupStrictAriesMocks::init();

let handle = issuer_credential::from_string(&issuer_credential_state_accepted()).unwrap();

Expand All @@ -1157,8 +1081,7 @@ pub mod tests {
#[test]
#[cfg(feature = "general_test")]
fn test_create_credential_arguments_correct() {
let _setup = SetupAriesMocks::init();
settings::set_config_value(settings::CONFIG_PROTOCOL_TYPE, "4.0");
let _setup = SetupStrictAriesMocks::init();

let handle = _vcx_issuer_create_credential_c_closure().unwrap();

Expand All @@ -1173,8 +1096,7 @@ pub mod tests {
#[test]
#[cfg(feature = "general_test")]
fn test_vcx_issuer_credential_get_state() {
let _setup = SetupAriesMocks::init();
settings::set_config_value(settings::CONFIG_PROTOCOL_TYPE, "4.0");
let _setup = SetupStrictAriesMocks::init();

let handle = _vcx_issuer_create_credential_c_closure().unwrap();

Expand All @@ -1190,11 +1112,10 @@ pub mod tests {
#[test]
#[cfg(feature = "general_test")]
fn test_vcx_issuer_revoke_credential() {
let _setup = SetupAriesMocks::init();
settings::set_config_value(settings::CONFIG_PROTOCOL_TYPE, "4.0");
let _setup = SetupStrictAriesMocks::init();

settings::set_config_value(settings::CONFIG_INSTITUTION_DID, DEFAULT_DID);
let handle = issuer_credential::from_string(&issuer_credential_state_accepted()).unwrap();
let handle = issuer_credential::from_string(CREDENTIAL_ISSUER_SM_FINISHED).unwrap();

// send the credential
let cb = return_types_u32::Return_U32::new().unwrap();
Expand All @@ -1208,8 +1129,7 @@ pub mod tests {
#[test]
#[cfg(feature = "general_test")]
fn test_vcx_issuer_credential_release() {
let _setup = SetupAriesMocks::init();
settings::set_config_value(settings::CONFIG_PROTOCOL_TYPE, "4.0");
let _setup = SetupStrictAriesMocks::init();

let handle = _vcx_issuer_create_credential_c_closure().unwrap();
assert_eq!(vcx_issuer_credential_release(handle + 1), error::INVALID_ISSUER_CREDENTIAL_HANDLE.code_num);
Expand Down
89 changes: 8 additions & 81 deletions libvcx/src/credential.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
use std::convert::TryInto;

use serde_json::Value;

use ::{serde_json, settings};
use api::VcxStateType;
use connection;
use ::{serde_json};
use error::prelude::*;
use issuer_credential::{CredentialMessage, CredentialOffer, PaymentInfo};
use messages::{
self,
get_message::{
get_connection_messages,
get_ref_msg,
MessagePayload,
},
payload::{
PayloadKinds,
Payloads,
},
RemoteMessageType,
thread::Thread,
};
use object_cache::ObjectCache;
use utils::{constants, error};
use utils::agent_info::{get_agent_attr, get_agent_info, MyAgentInfo};
use utils::error;
use utils::constants::GET_MESSAGES_DECRYPTED_RESPONSE;
use utils::httpclient::{AgencyMock, AgencyMockDecrypted};
use credential_request::CredentialRequest;
use utils::libindy::payments::{pay_a_payee, PaymentTxn};
use utils::mockdata::mockdata_credex::ARIES_CREDENTIAL_OFFER;
use utils::httpclient::AgencyMockDecrypted;
use v3::{
handlers::issuance::Holder,
messages::issuance::credential_offer::CredentialOffer as CredentialOfferV3,
Expand Down Expand Up @@ -190,46 +166,6 @@ pub fn get_credential_offer_messages(connection_handle: u32) -> VcxResult<String
Ok(json!(credential_offers).to_string())
}

fn _set_cred_offer_ref_message(payload: &MessagePayload, my_vk: &str, msg_id: &str) -> VcxResult<Vec<Value>> {
let (offer, thread) = Payloads::decrypt(my_vk, payload)?;

let (mut offer, payment_info) = parse_json_offer(&offer)?;

offer.msg_ref_id = Some(msg_id.to_owned());
if let Some(tr) = thread {
offer.thread_id = tr.thid.clone();
}

let mut payload = Vec::new();
payload.push(json!(offer));
if let Some(p) = payment_info { payload.push(json!(p)); }

Ok(payload)
}

pub fn parse_json_offer(offer: &str) -> VcxResult<(CredentialOffer, Option<PaymentInfo>)> {
let paid_offer: Value = serde_json::from_str(offer)
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidJson, format!("Cannot deserialize offer: {}", err)))?;

let mut payment: Option<PaymentInfo> = None;
let mut offer: Option<CredentialOffer> = None;

if let Some(i) = paid_offer.as_array() {
for entry in i.iter() {
if entry.get("libindy_offer").is_some() {
offer = Some(serde_json::from_value(entry.clone())
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidJson, format!("Cannot deserialize offer: {}", err)))?);
}

if entry.get("payment_addr").is_some() {
payment = Some(serde_json::from_value(entry.clone())
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidJson, format!("Cannot deserialize payment address: {}", err)))?);
}
}
}
Ok((offer.ok_or(VcxError::from(VcxErrorKind::InvalidJson))?, payment))
}

pub fn release(handle: u32) -> VcxResult<()> {
HANDLE_MAP.release(handle).map_err(handle_err)
}
Expand Down Expand Up @@ -271,18 +207,6 @@ pub fn is_payment_required(handle: u32) -> VcxResult<bool> {
}).map_err(handle_err)
}

pub fn submit_payment(handle: u32) -> VcxResult<(PaymentTxn, String)> {
HANDLE_MAP.get(handle, |_| {
Err(VcxError::from_msg(VcxErrorKind::ActionNotSupported, "Aries protocol currently doesn't support payments"))
}).map_err(handle_err)
}

pub fn get_payment_information(handle: u32) -> VcxResult<Option<PaymentInfo>> {
HANDLE_MAP.get(handle, |_| {
Ok(None)
}).map_err(handle_err)
}

pub fn get_credential_status(handle: u32) -> VcxResult<u32> {
HANDLE_MAP.get(handle, |credential| {
credential.get_credential_status()
Expand All @@ -293,6 +217,9 @@ pub fn get_credential_status(handle: u32) -> VcxResult<u32> {
pub mod tests {
use super::*;
use utils::devsetup::*;
use api::VcxStateType;
use connection;
use credential_request::CredentialRequest;
use utils::mockdata::mockdata_credex::{ARIES_CREDENTIAL_RESPONSE, CREDENTIAL_SM_FINISHED, CREDENTIAL_SM_OFFER_RECEIVED};

pub const BAD_CREDENTIAL_OFFER: &str = r#"{"version": "0.1","to_did": "LtMgSjtFcyPwenK9SHCyb8","from_did": "LtMgSjtFcyPwenK9SHCyb8","claim": {"account_num": ["8BEaoLf8TBmK4BUyX8WWnA"],"name_on_account": ["Alice"]},"schema_seq_no": 48,"issuer_did": "Pd4fnFtRBcMKRVC2go5w3j","claim_name": "Account Certificate","claim_id": "3675417066","msg_ref_id": "ymy5nth"}"#;
Expand Down Expand Up @@ -370,7 +297,7 @@ pub mod tests {
let msg = get_credential(handle_cred).unwrap();
let msg_value: serde_json::Value = serde_json::from_str(&msg).unwrap();

info!("full_credential_test:: going to deserialize CredentialMessage: {:?}", msg_value);
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();
}

Expand Down Expand Up @@ -425,7 +352,7 @@ pub mod tests {

let handle = from_string(CREDENTIAL_SM_FINISHED).unwrap();
let cred_string: String = get_credential(handle).unwrap();
let cred_value: Value = serde_json::from_str(&cred_string).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();
}
}
Loading