Skip to content

Commit

Permalink
Refactor aries-agent-rust (#1153)
Browse files Browse the repository at this point in the history
Refactor aries-agent-rust (#1153)

---------

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Mar 17, 2024
1 parent 232350f commit c56db65
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 220 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
*.code-workspace
**/tails.txt
.session.vim
**/aath-backchannel
#**/aath-backchannel
11 changes: 0 additions & 11 deletions aries/agents/aries-vcx-agent/src/agent/agent_config.rs

This file was deleted.

79 changes: 48 additions & 31 deletions aries/agents/aries-vcx-agent/src/agent/agent_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,55 @@ use aries_vcx_core::{
wallet::base_wallet::BaseWallet,
};

use crate::{
agent::agent_config::AgentConfig,
services::{
connection::ServiceConnections, credential_definition::ServiceCredentialDefinitions,
did_exchange::ServiceDidExchange, holder::ServiceCredentialsHolder,
issuer::ServiceCredentialsIssuer, out_of_band::ServiceOutOfBand, prover::ServiceProver,
revocation_registry::ServiceRevocationRegistries, schema::ServiceSchemas,
verifier::ServiceVerifier,
},
use crate::handlers::{
connection::ServiceConnections, credential_definition::ServiceCredentialDefinitions,
did_exchange::DidcommHandlerDidExchange, holder::ServiceCredentialsHolder,
issuer::ServiceCredentialsIssuer, out_of_band::ServiceOutOfBand, prover::ServiceProver,
revocation_registry::ServiceRevocationRegistries, schema::ServiceSchemas,
verifier::ServiceVerifier,
};

#[derive(Clone)]
pub struct Agent<T> {
pub struct Agent<W> {
pub(super) issuer_did: String,
pub(super) ledger_read: Arc<DefaultIndyLedgerRead>,
pub(super) ledger_write: Arc<DefaultIndyLedgerWrite>,
pub(super) anoncreds: IndyCredxAnonCreds,
pub(super) wallet: Arc<T>,
pub(super) config: AgentConfig,
pub(super) connections: Arc<ServiceConnections<T>>,
pub(super) schemas: Arc<ServiceSchemas<T>>,
pub(super) cred_defs: Arc<ServiceCredentialDefinitions<T>>,
pub(super) rev_regs: Arc<ServiceRevocationRegistries<T>>,
pub(super) holder: Arc<ServiceCredentialsHolder<T>>,
pub(super) issuer: Arc<ServiceCredentialsIssuer<T>>,
pub(super) verifier: Arc<ServiceVerifier<T>>,
pub(super) prover: Arc<ServiceProver<T>>,
pub(super) out_of_band: Arc<ServiceOutOfBand<T>>,
pub(super) did_exchange: Arc<ServiceDidExchange<T>>,
pub(super) wallet: Arc<W>,
pub(super) connections: Arc<ServiceConnections<W>>,
pub(super) schemas: Arc<ServiceSchemas<W>>,
pub(super) cred_defs: Arc<ServiceCredentialDefinitions<W>>,
pub(super) rev_regs: Arc<ServiceRevocationRegistries<W>>,
pub(super) holder: Arc<ServiceCredentialsHolder<W>>,
pub(super) issuer: Arc<ServiceCredentialsIssuer<W>>,
pub(super) verifier: Arc<ServiceVerifier<W>>,
pub(super) prover: Arc<ServiceProver<W>>,
pub(super) out_of_band: Arc<ServiceOutOfBand<W>>,
pub(super) did_exchange: Arc<DidcommHandlerDidExchange<W>>,
}

// Note: We do this manually, otherwise compiler is requesting us to implement Clone for generic
// type W, which is not in fact needed - W is wrapped in Arc. Underlying W has no reason to be
// cloned.
impl<W> Clone for Agent<W> {
fn clone(&self) -> Self {
Self {
issuer_did: self.issuer_did.clone(),
ledger_read: self.ledger_read.clone(),
ledger_write: self.ledger_write.clone(),
anoncreds: self.anoncreds,
wallet: self.wallet.clone(),
connections: self.connections.clone(),
schemas: self.schemas.clone(),
cred_defs: self.cred_defs.clone(),
rev_regs: self.rev_regs.clone(),
holder: self.holder.clone(),
issuer: self.issuer.clone(),
verifier: self.verifier.clone(),
prover: self.prover.clone(),
out_of_band: self.out_of_band.clone(),
did_exchange: self.did_exchange.clone(),
}
}
}

impl<T: BaseWallet> Agent<T> {
Expand All @@ -49,16 +70,12 @@ impl<T: BaseWallet> Agent<T> {
&self.anoncreds
}

pub fn wallet(&self) -> &Arc<T> {
&self.wallet
}

pub fn agent_config(&self) -> AgentConfig {
self.config.clone()
pub fn wallet(&self) -> Arc<T> {
self.wallet.clone()
}

pub fn issuer_did(&self) -> String {
self.config.config_issuer.institution_did.clone()
self.issuer_did.clone()
}

pub fn connections(&self) -> Arc<ServiceConnections<T>> {
Expand All @@ -69,7 +86,7 @@ impl<T: BaseWallet> Agent<T> {
self.out_of_band.clone()
}

pub fn did_exchange(&self) -> Arc<ServiceDidExchange<T>> {
pub fn did_exchange(&self) -> Arc<DidcommHandlerDidExchange<T>> {
self.did_exchange.clone()
}

Expand Down
157 changes: 73 additions & 84 deletions aries/agents/aries-vcx-agent/src/agent/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use aries_vcx::{
transactions::{add_new_did, write_endpoint},
},
did_doc::schema::service::typed::ServiceType,
did_parser::Did,
global::settings::DEFAULT_LINK_SECRET_ALIAS,
};
use aries_vcx_core::{
self,
anoncreds::{base_anoncreds::BaseAnonCreds, credx_anoncreds::IndyCredxAnonCreds},
ledger::indy_vdr_ledger::DefaultIndyLedgerRead,
ledger::indy_vdr_ledger::{build_ledger_components, DefaultIndyLedgerRead, VcxPoolConfig},
wallet::{
base_wallet::{BaseWallet, ManageWallet},
base_wallet::{issuer_config::IssuerConfig, BaseWallet, ManageWallet},
indy::{indy_wallet_config::IndyWalletConfig, IndySdkWallet},
},
};
Expand All @@ -22,20 +23,16 @@ use did_resolver_registry::ResolverRegistry;
use did_resolver_sov::resolution::DidSovResolver;
use display_as_json::Display;
use serde::Serialize;
use url::Url;

use crate::{
agent::{agent_config::AgentConfig, agent_struct::Agent},
agent::agent_struct::Agent,
error::AgentResult,
services::{
connection::{ServiceConnections, ServiceEndpoint},
credential_definition::ServiceCredentialDefinitions,
did_exchange::ServiceDidExchange,
holder::ServiceCredentialsHolder,
issuer::ServiceCredentialsIssuer,
out_of_band::ServiceOutOfBand,
prover::ServiceProver,
revocation_registry::ServiceRevocationRegistries,
schema::ServiceSchemas,
handlers::{
connection::ServiceConnections, credential_definition::ServiceCredentialDefinitions,
did_exchange::DidcommHandlerDidExchange, holder::ServiceCredentialsHolder,
issuer::ServiceCredentialsIssuer, out_of_band::ServiceOutOfBand, prover::ServiceProver,
revocation_registry::ServiceRevocationRegistries, schema::ServiceSchemas,
verifier::ServiceVerifier,
},
};
Expand All @@ -47,46 +44,69 @@ pub struct WalletInitConfig {
pub wallet_kdf: String,
}

#[derive(Serialize, Display)]
pub struct PoolInitConfig {
pub genesis_path: String,
pub pool_name: String,
}
#[derive(Serialize, Display)]
pub struct InitConfig {
pub enterprise_seed: String,
pub pool_config: PoolInitConfig,
pub wallet_config: WalletInitConfig,
pub service_endpoint: ServiceEndpoint,
}
pub async fn build_indy_wallet(
wallet_config: WalletInitConfig,
isser_seed: String,
) -> (IndySdkWallet, IssuerConfig) {
let config_wallet = IndyWalletConfig {
wallet_name: wallet_config.wallet_name,
wallet_key: wallet_config.wallet_key,
wallet_key_derivation: wallet_config.wallet_kdf,
wallet_type: None,
storage_config: None,
storage_credentials: None,
rekey: None,
rekey_derivation_method: None,
};
let wallet = config_wallet.create_wallet().await.unwrap();
let config_issuer = wallet.configure_issuer(&isser_seed).await.unwrap();

impl Agent<IndySdkWallet> {
pub async fn initialize(init_config: InitConfig) -> AgentResult<Self> {
let config_wallet = IndyWalletConfig {
wallet_name: init_config.wallet_config.wallet_name,
wallet_key: init_config.wallet_config.wallet_key,
wallet_key_derivation: init_config.wallet_config.wallet_kdf,
wallet_type: None,
storage_config: None,
storage_credentials: None,
rekey: None,
rekey_derivation_method: None,
};
let anoncreds = IndyCredxAnonCreds;
anoncreds
.prover_create_link_secret(&wallet, &DEFAULT_LINK_SECRET_ALIAS.to_string())
.await
.unwrap();

config_wallet.create_wallet().await.unwrap();
let wallet = Arc::new(config_wallet.open_wallet().await.unwrap());
let config_issuer = wallet
.configure_issuer(&init_config.enterprise_seed)
.await
.unwrap();
(wallet, config_issuer)
}

use aries_vcx_core::ledger::indy_vdr_ledger::{build_ledger_components, VcxPoolConfig};
impl<W: BaseWallet> Agent<W> {
pub async fn setup_ledger(
genesis_path: String,
wallet: Arc<W>,
service_endpoint: Url,
submiter_did: Did,
) -> AgentResult<Did> {
let vcx_pool_config = VcxPoolConfig {
indy_vdr_config: None,
response_cache_config: None,
genesis_file_path: genesis_path,
};
let (_, ledger_write) = build_ledger_components(vcx_pool_config.clone()).unwrap();
let (public_did, _verkey) =
add_new_did(wallet.as_ref(), &ledger_write, &submiter_did, None).await?;
let endpoint = EndpointDidSov::create()
.set_service_endpoint(service_endpoint.clone())
.set_types(Some(vec![ServiceType::DIDCommV1.to_string()]));
write_endpoint(wallet.as_ref(), &ledger_write, &public_did, &endpoint).await?;
info!(
"Agent::setup_ledger >> wrote data on ledger, public_did: {}, endpoint: {}",
public_did, service_endpoint
);
Ok(public_did)
}

pub async fn initialize(
genesis_path: String,
wallet: Arc<W>,
service_endpoint: Url,
issuer_did: Did,
) -> AgentResult<Agent<W>> {
info!("dev_build_profile_modular >>");
let vcx_pool_config = VcxPoolConfig {
indy_vdr_config: None,
response_cache_config: None,
genesis_file_path: init_config.pool_config.genesis_path,
genesis_file_path: genesis_path,
};

let anoncreds = IndyCredxAnonCreds;
Expand All @@ -95,31 +115,6 @@ impl Agent<IndySdkWallet> {
let ledger_read = Arc::new(ledger_read);
let ledger_write = Arc::new(ledger_write);

anoncreds
.prover_create_link_secret(wallet.as_ref(), &DEFAULT_LINK_SECRET_ALIAS.to_string())
.await
.unwrap();

// TODO: This setup should be easier
// The default issuer did can't be used - its verkey is not in base58 - TODO: double-check
let (public_did, _verkey) = add_new_did(
wallet.as_ref(),
ledger_write.as_ref(),
&config_issuer.institution_did.parse()?,
None,
)
.await?;
let endpoint = EndpointDidSov::create()
.set_service_endpoint(init_config.service_endpoint.clone())
.set_types(Some(vec![ServiceType::DIDCommV1.to_string()]));
write_endpoint(
wallet.as_ref(),
ledger_write.as_ref(),
&public_did,
&endpoint,
)
.await?;

let did_peer_resolver = PeerDidResolver::new();
let did_sov_resolver: DidSovResolver<Arc<DefaultIndyLedgerRead>, DefaultIndyLedgerRead> =
DidSovResolver::new(ledger_read.clone());
Expand All @@ -132,24 +127,21 @@ impl Agent<IndySdkWallet> {
let connections = Arc::new(ServiceConnections::new(
ledger_read.clone(),
wallet.clone(),
init_config.service_endpoint.clone(),
service_endpoint.clone(),
));
let did_exchange = Arc::new(ServiceDidExchange::new(
let did_exchange = Arc::new(DidcommHandlerDidExchange::new(
wallet.clone(),
did_resolver_registry,
init_config.service_endpoint.clone(),
public_did.to_string(),
));
let out_of_band = Arc::new(ServiceOutOfBand::new(
wallet.clone(),
init_config.service_endpoint,
service_endpoint.clone(),
issuer_did.to_string(),
));
let out_of_band = Arc::new(ServiceOutOfBand::new(wallet.clone(), service_endpoint));
let schemas = Arc::new(ServiceSchemas::new(
ledger_read.clone(),
ledger_write.clone(),
anoncreds,
wallet.clone(),
config_issuer.institution_did.clone(),
issuer_did.to_string(),
));
let cred_defs = Arc::new(ServiceCredentialDefinitions::new(
ledger_read.clone(),
Expand All @@ -162,7 +154,7 @@ impl Agent<IndySdkWallet> {
ledger_read.clone(),
anoncreds,
wallet.clone(),
config_issuer.institution_did.clone(),
issuer_did.to_string(),
));
let issuer = Arc::new(ServiceCredentialsIssuer::new(
anoncreds,
Expand Down Expand Up @@ -203,10 +195,7 @@ impl Agent<IndySdkWallet> {
holder,
verifier,
prover,
config: AgentConfig {
config_wallet,
config_issuer,
},
issuer_did: issuer_did.to_string(),
})
}
}
4 changes: 1 addition & 3 deletions aries/agents/aries-vcx-agent/src/agent/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
mod agent_config;
mod agent_struct;
mod init;

pub use agent_config::AgentConfig;
pub use agent_struct::Agent;
pub use init::{InitConfig, PoolInitConfig, WalletInitConfig};
pub use init::{build_indy_wallet, WalletInitConfig};

0 comments on commit c56db65

Please sign in to comment.