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

Cleanup/ffi leftovers #825

Merged
merged 3 commits into from
May 5, 2023
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
5 changes: 1 addition & 4 deletions aries_vcx/src/common/primitives/credential_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use aries_vcx_core::ledger::base_ledger::BaseLedger;
use crate::core::profile::profile::Profile;
use crate::errors::error::{AriesVcxError, AriesVcxErrorKind, VcxResult};
use crate::utils::constants::{CRED_DEF_ID, CRED_DEF_JSON, DEFAULT_SERIALIZE_VERSION};
use crate::utils::mockdata::mock_settings::StatusCodeMock;
use crate::utils::serialization::ObjectWithVersion;

use crate::global::settings::{self, indy_mocks_enabled};
Expand Down Expand Up @@ -109,10 +108,8 @@ async fn _try_get_cred_def_from_ledger(
issuer_did: &str,
cred_def_id: &str,
) -> VcxResult<Option<String>> {
// TODO - future - may require more customized logic. We set the rc to 309, as the mock for ledger.get_cred_def will return a valid
// mock cred def unless it reads an rc of 309. Returning a valid mock cred def will result in this method returning an error.
if indy_mocks_enabled() {
StatusCodeMock::set_next_result(309)
return Ok(None);
}
match ledger.get_cred_def(cred_def_id, Some(issuer_did)).await {
Ok(cred_def) => Ok(Some(cred_def)),
Expand Down
18 changes: 18 additions & 0 deletions aries_vcx/src/global/author_agreement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::errors::error::VcxResult;
use aries_vcx_core::global::author_agreement::{
get_txn_author_agreement, set_txn_author_agreement, TxnAuthorAgreementAcceptanceData,
};

pub fn proxy_set_txn_author_agreement(
text: Option<String>,
version: Option<String>,
taa_digest: Option<String>,
acc_mech_type: String,
time_of_acceptance: u64,
) -> VcxResult<()> {
set_txn_author_agreement(text, version, taa_digest, acc_mech_type, time_of_acceptance).map_err(|err| err.into())
}

pub fn proxy_get_txn_author_agreement() -> VcxResult<Option<TxnAuthorAgreementAcceptanceData>> {
get_txn_author_agreement().map_err(|err| err.into())
}
1 change: 1 addition & 0 deletions aries_vcx/src/global/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod author_agreement;
pub mod settings;
22 changes: 0 additions & 22 deletions aries_vcx/src/utils/author_agreement.rs

This file was deleted.

29 changes: 0 additions & 29 deletions aries_vcx/src/utils/mockdata/mock_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ static MOCKED_VALIDATE_INDY_PROOF: &str = "mocked_validate_indy_proof";
lazy_static! {
static ref MOCK_SETTINGS: RwLock<HashMap<String, String>> = RwLock::new(HashMap::new());
static ref MOCK_SETTINGS_RESULT_BOOL: RwLock<HashMap<String, VcxResult<bool>>> = RwLock::new(HashMap::new());
// todo: get rid of this, we no longer deal with rc return codes from vdrtools
// (this is leftover from times when we talked to vdrtool via FFI)
static ref STATUS_CODE_MOCK: Mutex<StatusCodeMock> = Mutex::new(StatusCodeMock::default());
}

pub struct MockBuilder; // empty
Expand Down Expand Up @@ -87,29 +84,3 @@ pub fn get_mock_result_for_validate_indy_proof() -> Option<VcxResult<bool>> {
Err(err) => Err(AriesVcxError::from_msg(err.kind(), err.to_string())),
})
}

#[derive(Default)]
pub struct StatusCodeMock {
results: Vec<u32>,
}

// todo: get rid of this, we no longer deal with rc return codes from vdrtools
// (this is leftover from times when we talked to vdrtool via FFI)
impl StatusCodeMock {
pub fn set_next_result(rc: u32) {
STATUS_CODE_MOCK
.lock()
.expect("Unabled to access LIBINDY_MOCK")
.results
.push(rc);
}

pub fn get_result() -> u32 {
STATUS_CODE_MOCK
.lock()
.expect("Unable to access LIBINDY_MOCK")
.results
.pop()
.unwrap_or_default()
}
}
7 changes: 0 additions & 7 deletions aries_vcx/src/utils/mockdata/profile/mock_anoncreds.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::utils::mockdata::mock_settings::StatusCodeMock;
use aries_vcx_core::errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind, VcxCoreResult};
use async_trait::async_trait;

Expand Down Expand Up @@ -65,12 +64,6 @@ impl BaseAnonCreds for MockAnoncreds {
}

async fn issuer_create_credential_offer(&self, _cred_def_id: &str) -> VcxCoreResult<String> {
if StatusCodeMock::get_result() != 0 {
return Err(AriesVcxCoreError::from_msg(
AriesVcxCoreErrorKind::InvalidState,
"Mocked error result of issuer_create_credential_offer: issuer_create_credential_offer",
));
};
Ok(LIBINDY_CRED_OFFER.to_string())
}

Expand Down
9 changes: 0 additions & 9 deletions aries_vcx/src/utils/mockdata/profile/mock_ledger.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::utils::mockdata::mock_settings::StatusCodeMock;
use aries_vcx_core::errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind, VcxCoreResult};
use aries_vcx_core::ledger::base_ledger::BaseLedger;
use async_trait::async_trait;
Expand Down Expand Up @@ -64,14 +63,6 @@ impl BaseLedger for MockLedger {
}

async fn get_cred_def(&self, cred_def_id: &str, submitter_did: Option<&str>) -> VcxCoreResult<String> {
// TODO - FUTURE - below error is required for tests to pass which require a cred def to not exist (libvcx)
// ideally we can migrate away from it
if StatusCodeMock::get_result() == 309 {
return Err(AriesVcxCoreError::from_msg(
AriesVcxCoreErrorKind::LedgerItemNotFound,
"Mocked error".to_string(),
));
};
Ok(CRED_DEF_JSON.to_string())
}

Expand Down
1 change: 0 additions & 1 deletion aries_vcx/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ macro_rules! map (
};
);

pub mod author_agreement;
#[rustfmt::skip]
pub mod constants;
pub mod file;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct TxnAuthorAgreementAcceptanceData {
pub time_of_acceptance: u64,
}

pub fn set_global_txn_author_agreement(
pub fn set_txn_author_agreement(
text: Option<String>,
version: Option<String>,
taa_digest: Option<String>,
Expand All @@ -40,7 +40,7 @@ pub fn set_global_txn_author_agreement(
Ok(())
}

pub fn get_global_txn_author_agreement() -> VcxCoreResult<Option<TxnAuthorAgreementAcceptanceData>> {
pub fn get_txn_author_agreement() -> VcxCoreResult<Option<TxnAuthorAgreementAcceptanceData>> {
trace!("get_txn_author_agreement >>>");
match settings::get_config_value(settings::CONFIG_TXN_AUTHOR_AGREEMENT) {
Ok(value) => {
Expand All @@ -67,7 +67,7 @@ mod unit_tests {
settings::reset_config_values().unwrap();
assert!(settings::get_config_value(settings::CONFIG_TXN_AUTHOR_AGREEMENT).is_err());

set_global_txn_author_agreement(
set_txn_author_agreement(
Some(TEXT.to_string()),
Some(VERSION.to_string()),
None,
Expand All @@ -82,7 +82,7 @@ mod unit_tests {
#[test]
fn get_txn_author_agreement_works() {
settings::reset_config_values().unwrap();
set_global_txn_author_agreement(
set_txn_author_agreement(
Some(TEXT.to_string()),
Some(VERSION.to_string()),
None,
Expand All @@ -91,7 +91,7 @@ mod unit_tests {
)
.unwrap();

let meta = get_global_txn_author_agreement().unwrap().unwrap();
let meta = get_txn_author_agreement().unwrap().unwrap();

let expected_meta = TxnAuthorAgreementAcceptanceData {
text: Some(TEXT.to_string()),
Expand All @@ -107,6 +107,6 @@ mod unit_tests {
#[test]
fn get_txn_author_agreement_works_for_not_set() {
settings::reset_config_values().unwrap();
assert!(get_global_txn_author_agreement().unwrap().is_none());
assert!(get_txn_author_agreement().unwrap().is_none());
}
}
16 changes: 16 additions & 0 deletions aries_vcx_core/src/global/mockdata/mock_settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::sync::RwLock;
use std::{collections::HashMap, sync::Mutex};

use crate::errors::error::VcxCoreResult;

static MOCKED_RETRIEVED_CREDS: &str = "mocked_retrieved_creds";

lazy_static! {
static ref MOCK_SETTINGS: RwLock<HashMap<String, String>> = RwLock::new(HashMap::new());
static ref MOCK_SETTINGS_RESULT_BOOL: RwLock<HashMap<String, VcxCoreResult<bool>>> = RwLock::new(HashMap::new());
}

pub fn get_mock_creds_retrieved_for_proof_request() -> Option<String> {
let config = MOCK_SETTINGS.read().expect("Unable to access MOCK_SETTINGS");
config.get(MOCKED_RETRIEVED_CREDS).map(String::from)
}
2 changes: 2 additions & 0 deletions aries_vcx_core/src/global/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pub mod author_agreement;
pub(crate) mod mockdata;
pub mod settings;
4 changes: 4 additions & 0 deletions aries_vcx_core/src/global/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,7 @@ pub fn get_protocol_version() -> usize {
protocol_version
}
}

pub fn get_sample_did() -> String {
DEFAULT_DID.to_string()
}
8 changes: 0 additions & 8 deletions aries_vcx_core/src/indy/credentials/issuer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,13 @@ use crate::global::settings;
use crate::indy::anoncreds;
use crate::indy::utils::parse_and_validate;
use crate::utils::constants::LIBINDY_CRED_OFFER;
use crate::utils::mockdata::mock_settings::StatusCodeMock;
use crate::{utils, WalletHandle};

pub async fn libindy_issuer_create_credential_offer(
wallet_handle: WalletHandle,
cred_def_id: &str,
) -> VcxCoreResult<String> {
if settings::indy_mocks_enabled() {
let rc = StatusCodeMock::get_result();
if rc != 0 {
return Err(AriesVcxCoreError::from_msg(
AriesVcxCoreErrorKind::InvalidState,
"Mocked error result of libindy_issuer_create_credential_offer",
));
};
return Ok(LIBINDY_CRED_OFFER.to_string());
}

Expand Down
12 changes: 8 additions & 4 deletions aries_vcx_core/src/indy/ledger/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,18 @@ pub async fn delete(pool_name: &str) -> VcxCoreResult<()> {
pub mod test_utils {
use std::fs;
use std::io::Write;
use std::{env, path::PathBuf};

use crate::utils::{
constants::{GENESIS_PATH, POOL},
get_temp_dir_path,
};
use crate::utils::constants::{GENESIS_PATH, POOL};

use super::*;

pub fn get_temp_dir_path(filename: &str) -> PathBuf {
let mut path = env::temp_dir();
path.push(filename);
path
}

pub async fn create_test_ledger_config() {
create_tmp_genesis_txn_file();
create_pool_ledger_config(POOL, get_temp_dir_path(GENESIS_PATH).to_str().unwrap()).unwrap();
Expand Down
19 changes: 10 additions & 9 deletions aries_vcx_core/src/indy/ledger/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use vdrtools::{DidValue, Locator};

use crate::common::ledger::transactions::{Request, Response};
use crate::errors::error::prelude::*;
use crate::global::author_agreement::get_txn_author_agreement;
use crate::global::settings;
use crate::global::settings::get_sample_did;
use crate::indy::utils::mocks::pool_mocks::PoolMocks;
use crate::indy::utils::parse_and_validate;
use crate::utils::constants::{
rev_def_json, CRED_DEF_ID, CRED_DEF_JSON, CRED_DEF_REQ, REVOC_REG_TYPE, REV_REG_DELTA_JSON, REV_REG_ID,
REV_REG_JSON, SCHEMA_ID, SCHEMA_JSON, SCHEMA_TXN, SUBMIT_SCHEMA_RESPONSE,
};
use crate::utils::random::generate_random_did;
use crate::{utils, PoolHandle, WalletHandle};

pub async fn multisign_request(wallet_handle: WalletHandle, did: &str, request: &str) -> VcxCoreResult<String> {
Expand Down Expand Up @@ -101,7 +102,7 @@ pub async fn libindy_get_txn_author_agreement(pool_handle: PoolHandle) -> VcxCor
return Ok(utils::constants::DEFAULT_AUTHOR_AGREEMENT.to_string());
}

let did = &generate_random_did();
let did = &get_sample_did();

let get_author_agreement_request = Locator::instance()
.ledger_controller
Expand Down Expand Up @@ -139,7 +140,7 @@ pub async fn libindy_get_txn_author_agreement(pool_handle: PoolHandle) -> VcxCor
pub async fn append_txn_author_agreement_to_request(request_json: &str) -> VcxCoreResult<String> {
trace!("append_txn_author_agreement_to_request >>> request_json: ...");

if let Some(author_agreement) = utils::author_agreement::get_global_txn_author_agreement()? {
if let Some(author_agreement) = get_txn_author_agreement()? {
Locator::instance()
.ledger_controller
.append_txn_author_agreement_acceptance_to_request(
Expand Down Expand Up @@ -211,7 +212,7 @@ pub async fn libindy_build_nym_request(
}

pub async fn get_nym(pool_handle: PoolHandle, did: &str) -> VcxCoreResult<String> {
let submitter_did = generate_random_did();
let submitter_did = get_sample_did();

let get_nym_req = libindy_build_get_nym_request(Some(&submitter_did), did).await?;

Expand Down Expand Up @@ -252,7 +253,7 @@ async fn libindy_get_cred_def(
pool_handle: PoolHandle,
cred_def_id: &str,
) -> VcxCoreResult<String> {
let submitter_did = &generate_random_did();
let submitter_did = &get_sample_did();
trace!(
"libindy_get_cred_def >>> pool_handle: {}, wallet_handle: {:?}, submitter_did: {}",
pool_handle,
Expand Down Expand Up @@ -546,7 +547,7 @@ pub async fn get_rev_reg_def_json(pool_handle: PoolHandle, rev_reg_id: &str) ->
return Ok((REV_REG_ID.to_string(), rev_def_json()));
}

let submitter_did = generate_random_did();
let submitter_did = get_sample_did();

let req = libindy_build_get_revoc_reg_def_request(&submitter_did, rev_reg_id).await?;
let res = libindy_submit_request(pool_handle, &req).await?;
Expand Down Expand Up @@ -592,7 +593,7 @@ pub async fn get_rev_reg_delta_json(
return Ok((REV_REG_ID.to_string(), REV_REG_DELTA_JSON.to_string(), 1));
}

let submitter_did = generate_random_did();
let submitter_did = get_sample_did();

let from: i64 = if let Some(_from) = from { _from as i64 } else { -1 };
let to = if let Some(_to) = to {
Expand All @@ -617,7 +618,7 @@ pub async fn get_rev_reg(
return Ok((REV_REG_ID.to_string(), REV_REG_JSON.to_string(), 1));
}

let submitter_did = generate_random_did();
let submitter_did = get_sample_did();

let req = libindy_build_get_revoc_reg_request(&submitter_did, rev_reg_id, timestamp).await?;

Expand Down Expand Up @@ -705,7 +706,7 @@ pub async fn get_schema_json(
return Ok((SCHEMA_ID.to_string(), SCHEMA_JSON.to_string()));
}

let submitter_did = generate_random_did();
let submitter_did = get_sample_did();

let schema_json = libindy_get_schema(wallet_handle, pool_handle, &submitter_did, schema_id).await?;

Expand Down
2 changes: 1 addition & 1 deletion aries_vcx_core/src/indy/proofs/prover/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use serde_json::{Map, Value};
use vdrtools::{Locator, SearchHandle};

use crate::errors::error::prelude::*;
use crate::global::mockdata::mock_settings::get_mock_creds_retrieved_for_proof_request;
use crate::global::settings;
use crate::indy::anoncreds::close_search_handle;
use crate::indy::utils::parse_and_validate;
Expand Down Expand Up @@ -89,7 +90,6 @@ pub async fn libindy_prover_get_credentials_for_proof_req(
);

{
use crate::utils::mockdata::mock_settings::get_mock_creds_retrieved_for_proof_request;
match get_mock_creds_retrieved_for_proof_request() {
None => {}
Some(mocked_creds) => {
Expand Down
Loading