Skip to content

Commit

Permalink
try arc wrapper
Browse files Browse the repository at this point in the history
Signed-off-by: George Mulhearn <[email protected]>
  • Loading branch information
gmulhearn-anonyome committed Dec 11, 2024
1 parent 47cccec commit 4af894c
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
100 changes: 100 additions & 0 deletions aries/aries_vcx_ledger/src/ledger/arc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//! Contains convenience wrappers for the aries_vcx_ledger traits when working with [Arc]s.
use std::sync::Arc;

use anoncreds_types::data_types::{
identifiers::{
cred_def_id::CredentialDefinitionId, rev_reg_def_id::RevocationRegistryDefinitionId,
schema_id::SchemaId,
},
ledger::{
cred_def::CredentialDefinition, rev_reg::RevocationRegistry,
rev_reg_def::RevocationRegistryDefinition, rev_reg_delta::RevocationRegistryDelta,
rev_status_list::RevocationStatusList, schema::Schema,
},
};
use async_trait::async_trait;
use did_parser_nom::Did;

use super::base_ledger::AnoncredsLedgerRead;
use crate::errors::error::VcxLedgerResult;

/// Convenience trait to convert something into an [AnoncredsLedgerRead] implementation.
pub trait IntoAnoncredsLedgerRead {
fn into_impl(self) -> impl AnoncredsLedgerRead;
}

/// Convenience to convert any Arc<AnoncredsLedgerRead> into AnoncredsLedgerRead.
/// This is possible because all methods of [AnoncredsLedgerRead] only require a reference
/// of self.
impl<T> IntoAnoncredsLedgerRead for Arc<T>
where
T: AnoncredsLedgerRead,
{
fn into_impl(self) -> impl AnoncredsLedgerRead {
ArcAnoncredsLedgerRead(self)
}
}

#[derive(Debug)]
struct ArcAnoncredsLedgerRead<T: AnoncredsLedgerRead>(Arc<T>);

#[async_trait]
impl<T> AnoncredsLedgerRead for ArcAnoncredsLedgerRead<T>
where
T: AnoncredsLedgerRead,
{
type RevocationRegistryDefinitionAdditionalMetadata =
T::RevocationRegistryDefinitionAdditionalMetadata;

async fn get_schema(
&self,
schema_id: &SchemaId,
submitter_did: Option<&Did>,
) -> VcxLedgerResult<Schema> {
self.0.get_schema(schema_id, submitter_did).await
}

async fn get_cred_def(
&self,
cred_def_id: &CredentialDefinitionId,
submitter_did: Option<&Did>,
) -> VcxLedgerResult<CredentialDefinition> {
self.get_cred_def(cred_def_id, submitter_did).await
}
async fn get_rev_reg_def_json(
&self,
rev_reg_id: &RevocationRegistryDefinitionId,
) -> VcxLedgerResult<(
RevocationRegistryDefinition,
Self::RevocationRegistryDefinitionAdditionalMetadata,
)> {
self.get_rev_reg_def_json(rev_reg_id).await
}

async fn get_rev_reg_delta_json(
&self,
rev_reg_id: &RevocationRegistryDefinitionId,
from: Option<u64>,
to: Option<u64>,
) -> VcxLedgerResult<(RevocationRegistryDelta, u64)> {
#[allow(deprecated)]
self.get_rev_reg_delta_json(rev_reg_id, from, to).await
}

async fn get_rev_status_list(
&self,
rev_reg_id: &RevocationRegistryDefinitionId,
timestamp: u64,
rev_reg_def_meta: Option<&Self::RevocationRegistryDefinitionAdditionalMetadata>,
) -> VcxLedgerResult<(RevocationStatusList, u64)> {
self.get_rev_status_list(rev_reg_id, timestamp, rev_reg_def_meta)
.await
}
async fn get_rev_reg(
&self,
rev_reg_id: &RevocationRegistryDefinitionId,
timestamp: u64,
) -> VcxLedgerResult<(RevocationRegistry, u64)> {
self.get_rev_reg(rev_reg_id, timestamp).await
}
}
2 changes: 1 addition & 1 deletion aries/aries_vcx_ledger/src/ledger/base_ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub trait AnoncredsLedgerRead: Debug + Send + Sync {
/// `get_rev_status_list`. Depending on the ledger anoncreds-method, this metadata may be
/// used in the subsequent revocation status list fetch as an optimization (e.g. to save an
/// additional ledger call).
type RevocationRegistryDefinitionAdditionalMetadata;
type RevocationRegistryDefinitionAdditionalMetadata: Send + Sync;

async fn get_schema(
&self,
Expand Down
1 change: 1 addition & 0 deletions aries/aries_vcx_ledger/src/ledger/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::errors::error::VcxLedgerError;

pub mod arc;
pub mod base_ledger;
pub mod common;

Expand Down

0 comments on commit 4af894c

Please sign in to comment.