Skip to content

Commit

Permalink
Work on VcIssuer impl
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Mircea <mirceapetrebogdan@gmail.com>
  • Loading branch information
bobozaur committed Nov 1, 2023
1 parent 9d4f44c commit 98239c9
Show file tree
Hide file tree
Showing 13 changed files with 879 additions and 393 deletions.
135 changes: 1 addition & 134 deletions aries_vcx_core/src/anoncreds/credx_anoncreds.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
borrow::Borrow,
collections::{HashMap, HashSet},
sync::Arc,
};

use async_trait::async_trait;
Expand All @@ -26,14 +25,10 @@ use super::base_anoncreds::BaseAnonCreds;
use crate::{
errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind, VcxCoreResult},
utils::{
async_fn_iterator::AsyncFnIterator,
constants::ATTRS,
json::{AsTypeOrDeserializationError, TryGetIndex},
},
wallet::{
base_wallet::{AsyncFnIteratorCollect, BaseWallet},
structs_io::UnpackMessageOutput,
},
wallet::base_wallet::{AsyncFnIteratorCollect, BaseWallet},
};

pub const CATEGORY_LINK_SECRET: &str = "VCX_LINK_SECRET";
Expand All @@ -60,134 +55,6 @@ pub struct RevocationRegistryInfo {
pub used_ids: HashSet<u32>,
}

/// Adapter used so that credx does not depend strictly on the vdrtools-wallet
/// Will get removed when the wallet and anoncreds interfaces are de-coupled.
#[derive(Debug)]
struct WalletAdapter(Arc<dyn BaseWallet>);

#[async_trait]
impl BaseWallet for WalletAdapter {
#[cfg(feature = "vdrtools_wallet")]
fn get_wallet_handle(&self) -> indy_api_types::WalletHandle {
self.0.get_wallet_handle()
}

async fn create_and_store_my_did(
&self,
seed: Option<&str>,
kdf_method_name: Option<&str>,
) -> VcxCoreResult<(String, String)> {
self.0.create_and_store_my_did(seed, kdf_method_name).await
}

async fn key_for_local_did(&self, did: &str) -> VcxCoreResult<String> {
self.0.key_for_local_did(did).await
}

async fn replace_did_keys_start(&self, target_did: &str) -> VcxCoreResult<String> {
self.0.replace_did_keys_start(target_did).await
}

async fn replace_did_keys_apply(&self, target_did: &str) -> VcxCoreResult<()> {
self.0.replace_did_keys_apply(target_did).await
}

async fn add_wallet_record(
&self,
xtype: &str,
id: &str,
value: &str,
tags: Option<HashMap<String, String>>,
) -> VcxCoreResult<()> {
self.0.add_wallet_record(xtype, id, value, tags).await
}

async fn get_wallet_record(
&self,
xtype: &str,
id: &str,
options: &str,
) -> VcxCoreResult<String> {
self.0.get_wallet_record(xtype, id, options).await
}

async fn get_wallet_record_value(&self, xtype: &str, id: &str) -> VcxCoreResult<String> {
self.0.get_wallet_record_value(xtype, id).await
}

async fn delete_wallet_record(&self, xtype: &str, id: &str) -> VcxCoreResult<()> {
self.0.delete_wallet_record(xtype, id).await
}

async fn update_wallet_record_value(
&self,
xtype: &str,
id: &str,
value: &str,
) -> VcxCoreResult<()> {
self.0.update_wallet_record_value(xtype, id, value).await
}

async fn add_wallet_record_tags(
&self,
xtype: &str,
id: &str,
tags: HashMap<String, String>,
) -> VcxCoreResult<()> {
self.0.add_wallet_record_tags(xtype, id, tags).await
}

async fn update_wallet_record_tags(
&self,
xtype: &str,
id: &str,
tags: HashMap<String, String>,
) -> VcxCoreResult<()> {
self.0.update_wallet_record_tags(xtype, id, tags).await
}

async fn delete_wallet_record_tags(
&self,
xtype: &str,
id: &str,
tag_names: &str,
) -> VcxCoreResult<()> {
self.0.delete_wallet_record_tags(xtype, id, tag_names).await
}

async fn iterate_wallet_records(
&self,
xtype: &str,
query: &str,
options: &str,
) -> VcxCoreResult<Box<dyn AsyncFnIterator<Item = VcxCoreResult<String>>>> {
self.0.iterate_wallet_records(xtype, query, options).await
}

// ---- crypto

async fn sign(&self, my_vk: &str, msg: &[u8]) -> VcxCoreResult<Vec<u8>> {
self.0.sign(my_vk, msg).await
}

async fn verify(&self, vk: &str, msg: &[u8], signature: &[u8]) -> VcxCoreResult<bool> {
self.0.verify(vk, msg, signature).await
}

async fn pack_message(
&self,
sender_vk: Option<&str>,
receiver_keys: &str,
msg: &[u8],
) -> VcxCoreResult<Vec<u8>> {
self.0.pack_message(sender_vk, receiver_keys, msg).await
}

async fn unpack_message(&self, msg: &[u8]) -> VcxCoreResult<UnpackMessageOutput> {
self.0.unpack_message(msg).await
}
}

#[derive(Debug, Copy, Clone)]
pub struct IndyCredxAnonCreds;

Expand Down
1 change: 1 addition & 0 deletions aries_vcx_core/src/credx/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use crate::wallet2::WalletRecord;
20 changes: 17 additions & 3 deletions aries_vcx_core/src/ledger/request_submitter/vdr_ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ use std::{

use async_trait::async_trait;
use indy_vdr::{
common::error::VdrError,
common::error::{VdrError, VdrResult},
config::PoolConfig,
pool::{PoolBuilder, PoolRunner, PoolTransactions, PreparedRequest, RequestResult},
pool::{
PoolBuilder, PoolRunner, PoolTransactions, PreparedRequest, RequestResult, TimingResult,
},
resolver::types::Callback,
};
use tokio::sync::oneshot;

Expand Down Expand Up @@ -75,6 +78,17 @@ impl IndyVdrSubmitter {
pub fn new(pool: IndyVdrLedgerPool) -> Self {
Self { pool }
}

pub(crate) fn send_request(
&self,
request: PreparedRequest,
callback: Callback<VdrResult<(RequestResult<String>, Option<TimingResult>)>>,
) -> VcxCoreResult<()> {
self.pool
.runner
.send_request(request, callback)
.map_err(From::from)
}
}

#[async_trait]
Expand All @@ -90,7 +104,7 @@ impl RequestSubmitter for IndyVdrSubmitter {
VdrError,
>;
let (sender, recv) = oneshot::channel::<VdrSendRequestResult>();
self.pool.runner.send_request(
self.send_request(
request,
Box::new(move |result| {
// unable to handle a failure from `send` here
Expand Down
46 changes: 46 additions & 0 deletions aries_vcx_core/src/ledger2/indy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::collections::HashMap;

use async_trait::async_trait;
use futures::channel::oneshot;
use indy_vdr::{
common::error::VdrError,
pool::{PreparedRequest, RequestResult},
};

use super::{Ledger, LedgerRequest};
use crate::{
errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind, VcxCoreResult},
ledger::request_submitter::vdr_ledger::IndyVdrSubmitter,
};

#[async_trait]
impl Ledger for IndyVdrSubmitter {
type Request = PreparedRequest;

type Response = RequestResult<String>;

async fn submit<R>(&self, request: Self::Request) -> VcxCoreResult<R>
where
R: LedgerRequest<Self>,
{
// indyvdr send_request is Async via a callback.
// Use oneshot channel to send result from callback, converting the fn to future.
type VdrSendRequestResult =
Result<(RequestResult<String>, Option<HashMap<String, f32>>), VdrError>;
let (sender, recv) = oneshot::channel::<VdrSendRequestResult>();
self.send_request(
request,
Box::new(move |result| {
// unable to handle a failure from `send` here
sender.send(result).ok();
}),
)?;

let send_req_result: VdrSendRequestResult = recv
.await
.map_err(|e| AriesVcxCoreError::from_msg(AriesVcxCoreErrorKind::InvalidState, e))?;
let (result, _) = send_req_result?;

R::from_ledger_response(result)
}
}
12 changes: 5 additions & 7 deletions aries_vcx_core/src/ledger2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod indy;

use async_trait::async_trait;

use crate::errors::error::VcxCoreResult;
Expand All @@ -8,19 +10,15 @@ pub trait Ledger {

type Response: Send + Sync;

async fn submit<R>(&self, request: Self::Request) -> VcxCoreResult<R::Response>
async fn submit<R>(&self, request: Self::Request) -> VcxCoreResult<R>
where
R: LedgerRequest<Self>;
}

pub trait LedgerRequest<L: Ledger + ?Sized> {
type RequestParams<'a>;

type Response;

fn into_ledger_request(self, params: Self::RequestParams<'_>) -> VcxCoreResult<L::Request>;
fn into_ledger_request(self) -> VcxCoreResult<L::Request>;

fn from_ledger_response(response: L::Response) -> VcxCoreResult<Self::Response>
fn from_ledger_response(response: L::Response) -> VcxCoreResult<Self>
where
Self: Sized;
}
8 changes: 4 additions & 4 deletions aries_vcx_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ extern crate log;
extern crate derive_builder;

pub mod anoncreds;
#[cfg(feature = "credx")]
pub mod credx;
pub mod errors;
pub mod global;
pub mod ledger;
pub mod ledger2;
pub mod utils;
pub mod vc;
pub mod wallet;
pub mod wallet2;
pub mod ledger2;
pub mod vc_issuer;
pub mod vc_prover;
pub mod vc_verifier;

pub use indy_ledger_response_parser::ResponseParser;
pub use indy_vdr::config::PoolConfig;
Expand Down
Loading

0 comments on commit 98239c9

Please sign in to comment.