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

Profiles refactoring #785

Merged
merged 7 commits into from
Mar 29, 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
4 changes: 2 additions & 2 deletions agents/rust/aries-vcx-agent/src/agent/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use aries_vcx::{
agency_client::{agency_client::AgencyClient, configuration::AgentProvisionConfig},
core::profile::{indy_profile::IndySdkProfile, profile::Profile},
core::profile::{profile::Profile, vdrtools_profile::VdrtoolsProfile},
global::settings::init_issuer_config,
indy::{
ledger::pool::{create_pool_ledger_config, open_pool_ledger, PoolConfigBuilder},
Expand Down Expand Up @@ -86,7 +86,7 @@ impl Agent {
.await
.unwrap();

let indy_profile = IndySdkProfile::new(wallet_handle, pool_handle);
let indy_profile = VdrtoolsProfile::new(wallet_handle, pool_handle);
let profile: Arc<dyn Profile> = Arc::new(indy_profile);
let wallet = profile.inject_wallet();

Expand Down
4 changes: 2 additions & 2 deletions aries_vcx/src/common/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::common::credentials::encoding::encode_attributes;
use crate::common::primitives::credential_definition::CredentialDef;
use crate::common::primitives::credential_definition::CredentialDefConfigBuilder;
use crate::common::primitives::revocation_registry::RevocationRegistry;
use crate::core::profile::indy_profile::IndySdkProfile;
use crate::core::profile::profile::Profile;
use crate::core::profile::vdrtools_profile::VdrtoolsProfile;
use crate::global::settings;
use crate::utils::constants::{DEFAULT_SCHEMA_ATTRS, TAILS_DIR, TEST_TAILS_URL, TRUSTEE_SEED};
use crate::utils::get_temp_dir_path;
Expand Down Expand Up @@ -437,5 +437,5 @@ pub fn mock_profile() -> Arc<dyn Profile> {

// TODO - FUTURE - should only be used for quick mock setups, should be removable after full detachment from vdrtools dep
pub fn indy_handles_to_profile(wallet_handle: WalletHandle, pool_handle: PoolHandle) -> Arc<dyn Profile> {
Arc::new(IndySdkProfile::new(wallet_handle, pool_handle))
Arc::new(VdrtoolsProfile::new(wallet_handle, pool_handle))
}
43 changes: 0 additions & 43 deletions aries_vcx/src/core/profile/indy_profile.rs

This file was deleted.

4 changes: 2 additions & 2 deletions aries_vcx/src/core/profile/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod indy_profile;
pub mod modular_wallet_profile;
pub mod modular_libs_profile;
pub mod profile;
pub mod vdrtools_profile;
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use crate::errors::error::VcxResult;
use crate::plugins::ledger::indy_vdr_ledger::LedgerPoolConfig;
use crate::plugins::{
anoncreds::{base_anoncreds::BaseAnonCreds, credx_anoncreds::IndyCredxAnonCreds},
ledger::{
Expand All @@ -12,38 +13,37 @@ use crate::plugins::{

use super::profile::Profile;

pub struct LedgerPoolConfig {
pub genesis_file_path: String,
}

#[allow(dead_code)]
#[derive(Debug)]
pub struct ModularWalletProfile {
pub struct ModularLibsProfile {
wallet: Arc<dyn BaseWallet>,
ledger_pool: Arc<IndyVdrLedgerPool>,
ledger: Arc<dyn BaseLedger>,
anoncreds: Arc<dyn BaseAnonCreds>,
}

impl ModularWalletProfile {
impl ModularLibsProfile {
pub fn new(wallet: Arc<dyn BaseWallet>, ledger_pool_config: LedgerPoolConfig) -> VcxResult<Self> {
let ledger_pool = Arc::new(IndyVdrLedgerPool::new(ledger_pool_config)?);
Ok(ModularWalletProfile { wallet, ledger_pool })
let ledger = Arc::new(IndyVdrLedger::new(Arc::clone(&wallet), ledger_pool));
let anoncreds = Arc::new(IndyCredxAnonCreds::new(Arc::clone(&wallet)));
Ok(ModularLibsProfile {
wallet,
ledger,
anoncreds,
})
}
}

impl Profile for ModularWalletProfile {
impl Profile for ModularLibsProfile {
fn inject_ledger(self: Arc<Self>) -> Arc<dyn BaseLedger> {
// todo - in the future we should lazy eval and avoid creating a new instance each time
Arc::clone(&self.ledger)
}

let ledger_pool = Arc::clone(&self.ledger_pool);
Arc::new(IndyVdrLedger::new(self, ledger_pool))
fn inject_anoncreds(self: Arc<Self>) -> Arc<dyn BaseAnonCreds> {
Arc::clone(&self.anoncreds)
}

fn inject_wallet(&self) -> Arc<dyn BaseWallet> {
Arc::clone(&self.wallet)
}

fn inject_anoncreds(self: Arc<Self>) -> Arc<dyn BaseAnonCreds> {
// todo - in the future we should lazy eval and avoid creating a new instance each time
Arc::new(IndyCredxAnonCreds::new(self))
}
}
45 changes: 45 additions & 0 deletions aries_vcx/src/core/profile/vdrtools_profile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::sync::Arc;

use vdrtools::{PoolHandle, WalletHandle};

use crate::plugins::{
anoncreds::{base_anoncreds::BaseAnonCreds, indy_anoncreds::IndySdkAnonCreds},
ledger::{base_ledger::BaseLedger, indy_ledger::IndySdkLedger},
wallet::{base_wallet::BaseWallet, indy_wallet::IndySdkWallet},
};

use super::profile::Profile;

#[derive(Debug)]
pub struct VdrtoolsProfile {
wallet: Arc<dyn BaseWallet>,
ledger: Arc<dyn BaseLedger>,
anoncreds: Arc<dyn BaseAnonCreds>,
}

impl VdrtoolsProfile {
pub fn new(indy_wallet_handle: WalletHandle, indy_pool_handle: PoolHandle) -> Self {
let wallet = Arc::new(IndySdkWallet::new(indy_wallet_handle));
let ledger = Arc::new(IndySdkLedger::new(indy_wallet_handle, indy_pool_handle));
let anoncreds = Arc::new(IndySdkAnonCreds::new(indy_wallet_handle, indy_pool_handle));
VdrtoolsProfile {
wallet,
ledger,
anoncreds,
}
}
}

impl Profile for VdrtoolsProfile {
fn inject_ledger(self: Arc<Self>) -> Arc<dyn BaseLedger> {
Arc::clone(&self.ledger)
}

fn inject_anoncreds(self: Arc<Self>) -> Arc<dyn BaseAnonCreds> {
Arc::clone(&self.anoncreds)
}

fn inject_wallet(&self) -> Arc<dyn BaseWallet> {
Arc::clone(&self.wallet)
}
}
4 changes: 2 additions & 2 deletions aries_vcx/src/handlers/proof_presentation/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl Verifier {
#[cfg(test)]
#[cfg(feature = "general_test")]
mod unit_tests {
use crate::core::profile::indy_profile::IndySdkProfile;
use crate::core::profile::vdrtools_profile::VdrtoolsProfile;
use crate::utils::constants::{REQUESTED_ATTRS, REQUESTED_PREDICATES};
use crate::utils::devsetup::*;
use crate::utils::mockdata::mock_settings::MockBuilder;
Expand All @@ -232,7 +232,7 @@ mod unit_tests {
use super::*;

fn _dummy_profile() -> Arc<dyn Profile> {
Arc::new(IndySdkProfile::new(WalletHandle(0), 0))
Arc::new(VdrtoolsProfile::new(WalletHandle(0), 0))
}

async fn _verifier() -> Verifier {
Expand Down
41 changes: 18 additions & 23 deletions aries_vcx/src/plugins/anoncreds/credx_anoncreds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::{
};

use crate::errors::error::{AriesVcxError, AriesVcxErrorKind, VcxResult};
use crate::plugins::wallet::base_wallet::BaseWallet;
use crate::{
core::profile::profile::Profile,
plugins::wallet::base_wallet::AsyncFnIteratorCollect,
utils::{
constants::ATTRS,
Expand Down Expand Up @@ -37,18 +37,17 @@ const CATEGORY_LINK_SECRET: &str = "VCX_LINK_SECRET";

#[derive(Debug)]
pub struct IndyCredxAnonCreds {
profile: Arc<dyn Profile>,
wallet: Arc<dyn BaseWallet>,
}

impl IndyCredxAnonCreds {
pub fn new(profile: Arc<dyn Profile>) -> Self {
IndyCredxAnonCreds { profile }
pub fn new(wallet: Arc<dyn BaseWallet>) -> Self {
IndyCredxAnonCreds { wallet }
}

async fn get_link_secret(&self, link_secret_id: &str) -> VcxResult<MasterSecret> {
let wallet = self.profile.inject_wallet();

let record = wallet
let record = self
.wallet
.get_wallet_record(CATEGORY_LINK_SECRET, link_secret_id, "{}")
.await?;

Expand All @@ -68,9 +67,8 @@ impl IndyCredxAnonCreds {
}

async fn _get_credential(&self, credential_id: &str) -> VcxResult<CredxCredential> {
let wallet = self.profile.inject_wallet();

let cred_record = wallet
let cred_record = self
.wallet
.get_wallet_record(CATEGORY_CREDENTIAL, credential_id, "{}")
.await?;
let cred_record: Value = serde_json::from_str(&cred_record)?;
Expand All @@ -84,9 +82,10 @@ impl IndyCredxAnonCreds {
}

async fn _get_credentials(&self, wql: &str) -> VcxResult<Vec<(String, CredxCredential)>> {
let wallet = self.profile.inject_wallet();

let mut record_iterator = wallet.iterate_wallet_records(CATEGORY_CREDENTIAL, wql, "{}").await?;
let mut record_iterator = self
.wallet
.iterate_wallet_records(CATEGORY_CREDENTIAL, wql, "{}")
.await?;
let records = record_iterator.collect().await?;

let id_cred_tuple_list: VcxResult<Vec<(String, CredxCredential)>> = records
Expand Down Expand Up @@ -608,18 +607,16 @@ impl BaseAnonCreds for IndyCredxAnonCreds {
let record_value = serde_json::to_string(&credential)?;
let tags_json = serde_json::to_string(&tags)?;

self.profile
.inject_wallet()
self.wallet
.add_wallet_record(CATEGORY_CREDENTIAL, &credential_id, &record_value, Some(&tags_json))
.await?;

Ok(credential_id)
}

async fn prover_create_link_secret(&self, link_secret_id: &str) -> VcxResult<String> {
let wallet = self.profile.inject_wallet();

let existing_record = wallet
let existing_record = self
.wallet
.get_wallet_record(CATEGORY_LINK_SECRET, link_secret_id, "{}")
.await
.ok(); // ignore error, as we only care about whether it exists or not
Expand Down Expand Up @@ -655,17 +652,15 @@ impl BaseAnonCreds for IndyCredxAnonCreds {
)
})?;

wallet
self.wallet
.add_wallet_record(CATEGORY_LINK_SECRET, link_secret_id, &ms_decimal, None)
.await?;

return Ok(link_secret_id.to_string());
}

async fn prover_delete_credential(&self, cred_id: &str) -> VcxResult<()> {
let wallet = self.profile.inject_wallet();

wallet.delete_wallet_record(CATEGORY_CREDENTIAL, cred_id).await
self.wallet.delete_wallet_record(CATEGORY_CREDENTIAL, cred_id).await
}

async fn issuer_create_schema(
Expand Down Expand Up @@ -823,7 +818,7 @@ mod unit_tests {
}

let profile = mock_profile();
let anoncreds: Box<dyn BaseAnonCreds> = Box::new(IndyCredxAnonCreds::new(profile));
let anoncreds: Box<dyn BaseAnonCreds> = Box::new(IndyCredxAnonCreds::new(profile.inject_wallet()));

assert_unimplemented(anoncreds.issuer_create_and_store_revoc_reg("", "", "", 0, "").await);
assert_unimplemented(
Expand Down
Loading