Skip to content

Commit

Permalink
refactor: ledger & anoncreds typing, part 1 (#1116)
Browse files Browse the repository at this point in the history
* refactor: Add typing for ledger & anoncreds

---------

Signed-off-by: Miroslav Kovar <miroslav.kovar@absa.africa>
  • Loading branch information
mirgee authored Feb 6, 2024
1 parent 8b77c06 commit 7f15000
Show file tree
Hide file tree
Showing 92 changed files with 7,183 additions and 497 deletions.
131 changes: 77 additions & 54 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions aries/agents/rust/aries-vcx-agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ aries_vcx_core = { path = "../../../aries_vcx_core", features = [
"credx",
"vdrtools_wallet",
] }
anoncreds_types = { path = "../../../misc/anoncreds_types" }
shared = { path = "../../../misc/shared" }
did_resolver_registry = { path = "../../../../did_core/did_resolver_registry" }
did_resolver_sov = { path = "../../../../did_core/did_methods/did_resolver_sov" }
Expand Down
4 changes: 2 additions & 2 deletions aries/agents/rust/aries-vcx-agent/src/agent/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Agent {
let (public_did, _verkey) = add_new_did(
wallet.as_ref(),
ledger_write.as_ref(),
&config_issuer.institution_did,
&config_issuer.institution_did.parse()?,
None,
)
.await?;
Expand Down Expand Up @@ -134,7 +134,7 @@ impl Agent {
wallet.clone(),
did_resolver_registry,
init_config.service_endpoint.clone(),
public_did,
public_did.to_string(),
));
let out_of_band = Arc::new(ServiceOutOfBand::new(
wallet.clone(),
Expand Down
8 changes: 8 additions & 0 deletions aries/agents/rust/aries-vcx-agent/src/error/convertors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,11 @@ impl From<(GenericDidExchange, AriesVcxError)> for AgentError {
AgentError { message, kind }
}
}

impl From<anoncreds_types::Error> for AgentError {
fn from(err: anoncreds_types::Error) -> Self {
let kind = AgentErrorKind::GenericAriesVcxError;
let message = format!("AnoncredsTypesError; err: {:?}", err.to_string());
AgentError { message, kind }
}
}
2 changes: 1 addition & 1 deletion aries/agents/rust/aries-vcx-agent/src/services/holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl ServiceCredentialsHolder {
self.wallet.as_ref(),
self.ledger_read.as_ref(),
&self.anoncreds,
pw_did,
pw_did.parse()?,
)
.await?;
send_closure(msg_response).await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
sync::{Arc, Mutex},
};

use aries_vcx::common::primitives::revocation_registry::RevocationRegistry;
use aries_vcx::{common::primitives::revocation_registry::RevocationRegistry, did_parser::Did};
use aries_vcx_core::{
anoncreds::credx_anoncreds::IndyCredxAnonCreds,
ledger::indy_vdr_ledger::{DefaultIndyLedgerRead, DefaultIndyLedgerWrite},
Expand All @@ -20,7 +20,7 @@ pub struct ServiceRevocationRegistries {
ledger_read: Arc<DefaultIndyLedgerRead>,
anoncreds: IndyCredxAnonCreds,
wallet: Arc<IndySdkWallet>,
issuer_did: String,
issuer_did: Did,
rev_regs: ObjectCache<RevocationRegistry>,
}

Expand All @@ -33,7 +33,7 @@ impl ServiceRevocationRegistries {
issuer_did: String,
) -> Self {
Self {
issuer_did,
issuer_did: Did::parse(issuer_did).unwrap(), // TODO
rev_regs: ObjectCache::new("rev-regs"),
ledger_write,
ledger_read,
Expand Down
15 changes: 10 additions & 5 deletions aries/agents/rust/aries-vcx-agent/src/services/schema.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::{Arc, Mutex};

use aries_vcx::common::primitives::credential_schema::Schema;
use aries_vcx::{common::primitives::credential_schema::Schema, did_parser::Did};
use aries_vcx_core::{
anoncreds::credx_anoncreds::IndyCredxAnonCreds,
ledger::{
Expand All @@ -20,7 +20,7 @@ pub struct ServiceSchemas {
ledger_write: Arc<DefaultIndyLedgerWrite>,
anoncreds: IndyCredxAnonCreds,
wallet: Arc<IndySdkWallet>,
issuer_did: String,
issuer_did: Did,
schemas: ObjectCache<Schema>,
}

Expand All @@ -33,7 +33,7 @@ impl ServiceSchemas {
issuer_did: String,
) -> Self {
Self {
issuer_did,
issuer_did: Did::parse(issuer_did).unwrap(), // TODO
schemas: ObjectCache::new("schemas"),
ledger_read,
ledger_write,
Expand All @@ -57,7 +57,8 @@ impl ServiceSchemas {
attributes,
)
.await?;
self.schemas.insert(&schema.get_schema_id(), schema)
self.schemas
.insert(&schema.get_schema_id().to_string(), schema)
}

pub async fn publish_schema(&self, thread_id: &str) -> AgentResult<()> {
Expand All @@ -71,7 +72,11 @@ impl ServiceSchemas {

pub async fn schema_json(&self, thread_id: &str) -> AgentResult<String> {
let ledger = self.ledger_read.as_ref();
Ok(ledger.get_schema(thread_id, None).await?)
Ok(serde_json::to_string(
&ledger
.get_schema(&thread_id.to_string().try_into()?, None)
.await?,
)?)
}

pub fn find_by_name_and_version(&self, name: &str, version: &str) -> AgentResult<Vec<String>> {
Expand Down
1 change: 1 addition & 0 deletions aries/aries_vcx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ messages = { path = "../messages" }
diddoc_legacy = { path = "../misc/legacy/diddoc_legacy" }
aries_vcx_core = { path = "../aries_vcx_core" }
shared = { path = "../misc/shared" }
anoncreds_types = { path = "../misc/anoncreds_types" }
did_parser = { path = "../../did_core/did_parser" }
did_resolver = { path = "../../did_core/did_resolver" }
did_doc = { path = "../../did_core/did_doc" }
Expand Down
11 changes: 6 additions & 5 deletions aries/aries_vcx/src/common/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ use aries_vcx_core::{
ledger::base_ledger::{IndyLedgerRead, IndyLedgerWrite},
wallet::base_wallet::BaseWallet,
};
use did_parser::Did;
use serde_json::Value;

use crate::errors::error::prelude::*;

pub async fn rotate_verkey_apply(
wallet: &impl BaseWallet,
indy_ledger_write: &impl IndyLedgerWrite,
did: &str,
did: &Did,
temp_vk: &str,
) -> VcxResult<()> {
let nym_result = indy_ledger_write
Expand Down Expand Up @@ -42,21 +43,21 @@ pub async fn rotate_verkey_apply(
));
}

Ok(wallet.replace_did_key_apply(did).await?)
Ok(wallet.replace_did_key_apply(&did.to_string()).await?)
}

pub async fn rotate_verkey(
wallet: &impl BaseWallet,
indy_ledger_write: &impl IndyLedgerWrite,
did: &str,
did: &Did,
) -> VcxResult<()> {
let trustee_verkey = wallet.replace_did_key_start(did, None).await?;
let trustee_verkey = wallet.replace_did_key_start(&did.to_string(), None).await?;
rotate_verkey_apply(wallet, indy_ledger_write, did, &trustee_verkey.base58()).await
}

pub async fn get_verkey_from_ledger(
indy_ledger: &impl IndyLedgerRead,
did: &str,
did: &Did,
) -> VcxResult<String> {
let nym_response: String = indy_ledger.get_nym(did).await?;
let nym_json: Value = serde_json::from_str(&nym_response).map_err(|err| {
Expand Down
40 changes: 18 additions & 22 deletions aries/aries_vcx/src/common/ledger/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use aries_vcx_core::{
},
wallet::base_wallet::BaseWallet,
};
use did_parser::Did;
use diddoc_legacy::aries::service::AriesService;
use messages::msg_fields::protocols::out_of_band::invitation::OobService;
use serde_json::Value;
Expand Down Expand Up @@ -73,57 +74,52 @@ pub async fn resolve_service(
match service {
OobService::AriesService(service) => Ok(service.clone()),
OobService::SovService(service) => Ok(from_service_sov_to_legacy(service.to_owned())),
OobService::Did(did) => get_service(indy_ledger, did).await,
OobService::Did(did) => get_service(indy_ledger, &did.clone().parse()?).await,
}
}

pub async fn add_new_did(
wallet: &impl BaseWallet,
indy_ledger_write: &impl IndyLedgerWrite,
submitter_did: &str,
submitter_did: &Did,
role: Option<&str>,
) -> VcxResult<(String, String)> {
) -> VcxResult<(Did, String)> {
let did_data = wallet.create_and_store_my_did(None, None).await?;

let res = indy_ledger_write
.publish_nym(
wallet,
submitter_did,
did_data.did(),
&did_data.did().parse()?,
Some(&did_data.verkey().base58()),
None,
role,
)
.await?;
check_response(&res)?;

Ok((did_data.did().into(), did_data.verkey().base58()))
Ok((did_data.did().parse()?, did_data.verkey().base58()))
}

pub async fn get_service(ledger: &impl IndyLedgerRead, did: &String) -> VcxResult<AriesService> {
let did_raw = did.to_string();
let did_raw = match did_raw.rsplit_once(':') {
None => did_raw,
Some((_, value)) => value.to_string(),
};
let attr_resp = ledger.get_attr(&did_raw, "endpoint").await?;
pub async fn get_service(ledger: &impl IndyLedgerRead, did: &Did) -> VcxResult<AriesService> {
let attr_resp = ledger.get_attr(did, "endpoint").await?;
let data = get_data_from_response(&attr_resp)?;
if data["endpoint"].is_object() {
let endpoint: EndpointDidSov = serde_json::from_value(data["endpoint"].clone())?;
let recipient_keys = vec![get_verkey_from_ledger(ledger, &did_raw).await?];
let recipient_keys = vec![get_verkey_from_ledger(ledger, did).await?];
let endpoint_url = endpoint.endpoint;

return Ok(AriesService::create()
.set_recipient_keys(recipient_keys)
.set_service_endpoint(endpoint_url)
.set_routing_keys(endpoint.routing_keys.unwrap_or_default()));
}
parse_legacy_endpoint_attrib(ledger, &did_raw).await
parse_legacy_endpoint_attrib(ledger, did).await
}

pub async fn parse_legacy_endpoint_attrib(
indy_ledger: &impl IndyLedgerRead,
did_raw: &str,
did_raw: &Did,
) -> VcxResult<AriesService> {
let attr_resp = indy_ledger.get_attr(did_raw, "service").await?;
let data = get_data_from_response(&attr_resp)?;
Expand Down Expand Up @@ -152,8 +148,8 @@ pub async fn parse_legacy_endpoint_attrib(
pub async fn write_endorser_did(
wallet: &impl BaseWallet,
indy_ledger_write: &impl IndyLedgerWrite,
submitter_did: &str,
target_did: &str,
submitter_did: &Did,
target_did: &Did,
target_vk: &str,
alias: Option<String>,
) -> VcxResult<String> {
Expand All @@ -174,7 +170,7 @@ pub async fn write_endorser_did(
pub async fn write_endpoint_legacy(
wallet: &impl BaseWallet,
indy_ledger_write: &impl IndyLedgerWrite,
did: &str,
did: &Did,
service: &AriesService,
) -> VcxResult<String> {
let attrib_json = json!({ "service": service }).to_string();
Expand All @@ -188,7 +184,7 @@ pub async fn write_endpoint_legacy(
pub async fn write_endpoint(
wallet: &impl BaseWallet,
indy_ledger_write: &impl IndyLedgerWrite,
did: &str,
did: &Did,
service: &EndpointDidSov,
) -> VcxResult<String> {
let attrib_json = json!({ "endpoint": service }).to_string();
Expand All @@ -202,7 +198,7 @@ pub async fn write_endpoint(
pub async fn add_attr(
wallet: &impl BaseWallet,
indy_ledger_write: &impl IndyLedgerWrite,
did: &str,
did: &Did,
attr: &str,
) -> VcxResult<()> {
let res = indy_ledger_write.add_attr(wallet, did, attr).await?;
Expand All @@ -211,7 +207,7 @@ pub async fn add_attr(

pub async fn get_attr(
ledger: &impl IndyLedgerRead,
did: &str,
did: &Did,
attr_name: &str,
) -> VcxResult<String> {
let attr_resp = ledger.get_attr(did, attr_name).await?;
Expand All @@ -226,7 +222,7 @@ pub async fn get_attr(
pub async fn clear_attr(
wallet: &impl BaseWallet,
indy_ledger_write: &impl IndyLedgerWrite,
did: &str,
did: &Did,
attr_name: &str,
) -> VcxResult<String> {
indy_ledger_write
Expand Down
24 changes: 13 additions & 11 deletions aries/aries_vcx/src/common/primitives/credential_definition.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use anoncreds_types::data_types::{identifiers::schema_id::SchemaId, ledger::schema::Schema};
use aries_vcx_core::{
anoncreds::base_anoncreds::BaseAnonCreds,
errors::error::AriesVcxCoreErrorKind,
global::settings::DEFAULT_SERIALIZE_VERSION,
ledger::base_ledger::{AnoncredsLedgerRead, AnoncredsLedgerWrite},
wallet::base_wallet::BaseWallet,
};
use did_parser::Did;

use crate::{
errors::error::{AriesVcxError, AriesVcxErrorKind, VcxResult},
Expand Down Expand Up @@ -51,20 +53,20 @@ pub struct CredentialDef {
id: String,
tag: String,
source_id: String,
issuer_did: String,
issuer_did: Did,
cred_def_json: String,
support_revocation: bool,
#[serde(default)]
schema_id: String,
schema_id: SchemaId,
#[serde(default)]
pub state: PublicEntityStateType,
}

#[derive(Clone, Debug, Deserialize, Serialize, Builder, Default)]
#[builder(setter(into), default)]
pub struct CredentialDefConfig {
issuer_did: String,
schema_id: String,
issuer_did: Did,
schema_id: SchemaId,
tag: String,
}

Expand All @@ -78,7 +80,7 @@ pub struct RevocationDetails {

async fn _try_get_cred_def_from_ledger(
ledger: &impl AnoncredsLedgerRead,
issuer_did: &str,
issuer_did: &Did,
cred_def_id: &str,
) -> VcxResult<Option<String>> {
match ledger.get_cred_def(cred_def_id, Some(issuer_did)).await {
Expand Down Expand Up @@ -120,7 +122,7 @@ impl CredentialDef {
anoncreds,
&issuer_did,
&schema_id,
&schema_json,
schema_json,
&tag,
None,
Some(support_revocation),
Expand Down Expand Up @@ -216,7 +218,7 @@ impl CredentialDef {
}

pub fn get_schema_id(&self) -> String {
self.schema_id.clone()
self.schema_id.to_string()
}

pub fn set_source_id(&mut self, source_id: String) {
Expand All @@ -239,15 +241,15 @@ impl CredentialDef {
pub async fn generate_cred_def(
wallet: &impl BaseWallet,
anoncreds: &impl BaseAnonCreds,
issuer_did: &str,
schema_id: &str,
schema_json: &str,
issuer_did: &Did,
schema_id: &SchemaId,
schema_json: Schema,
tag: &str,
sig_type: Option<&str>,
support_revocation: Option<bool>,
) -> VcxResult<(String, String)> {
trace!(
"generate_cred_def >>> issuer_did: {}, schema_json: {}, tag: {}, sig_type: {:?}, \
"generate_cred_def >>> issuer_did: {}, schema_json: {:?}, tag: {}, sig_type: {:?}, \
support_revocation: {:?}",
issuer_did,
schema_json,
Expand Down
Loading

0 comments on commit 7f15000

Please sign in to comment.