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

draft: refactored primitives interfaces #1042

Closed
wants to merge 13 commits into from
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion agency_client/src/internal/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl AgencyClient {
agent_vk: &str,
) -> AgencyClientResult<Vec<u8>> {
debug!("prepare_message_for_connection_agent >> {:?}", messages);
let message = messages.get(0).ok_or(AgencyClientError::from_msg(
let message = messages.first().ok_or(AgencyClientError::from_msg(
AgencyClientErrorKind::SerializationError,
"Cannot get message",
))?;
Expand Down
2 changes: 0 additions & 2 deletions agents/rust/mediator/src/didcomm_handlers/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
pub mod prelude {
pub use std::sync::Arc;

pub use aries_vcx::utils::encryption_envelope::EncryptionEnvelope;
pub use aries_vcx_core::wallet::base_wallet::BaseWallet;
pub use mediation::storage::MediatorPersistence;
Expand Down
2 changes: 1 addition & 1 deletion agents/rust/mediator/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ pub mod test_setup {

pub mod prelude {
pub use anyhow::Result;
pub use log::{debug, error, info};
pub use log::info;
pub use url::Url;
}
2 changes: 1 addition & 1 deletion aries_vcx/src/common/credentials/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn encode_attributes(attributes: &str) -> VcxResult<String> {
// old style input such as {"address2":["101 Wilson Lane"]}
serde_json::Value::Array(array_type) => {
let attrib_value: &str =
match array_type.get(0).and_then(serde_json::Value::as_str) {
match array_type.first().and_then(serde_json::Value::as_str) {
Some(x) => x,
None => {
return Err(AriesVcxError::from_msg(
Expand Down
2 changes: 1 addition & 1 deletion aries_vcx/src/handlers/out_of_band/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl OutOfBandReceiver {
.content
.requests_attach
.as_ref()
.and_then(|v| v.get(0))
.and_then(|v| v.first())
{
attachment_to_aries_message(attach)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl SmConnectionInvitee {
))?;
did_did
.recipient_keys()?
.get(0)
.first()
.ok_or(AriesVcxError::from_msg(
AriesVcxErrorKind::NotReady,
"Can't resolve recipient key from the counterparty diddoc.",
Expand Down Expand Up @@ -380,7 +380,7 @@ impl SmConnectionInvitee {

let state = match self.state {
InviteeFullState::Requested(state) => {
let remote_vk: String = state.did_doc.recipient_keys()?.get(0).cloned().ok_or(
let remote_vk: String = state.did_doc.recipient_keys()?.first().cloned().ok_or(
AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidState,
"Cannot handle response: remote verkey not found",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl SmConnectionInviter {
))?;
did_did
.recipient_keys()?
.get(0)
.first()
.ok_or(AriesVcxError::from_msg(
AriesVcxErrorKind::NotReady,
"Can't resolve recipient key from the counterparty diddoc.",
Expand Down
2 changes: 1 addition & 1 deletion aries_vcx/src/utils/encryption_envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl EncryptionEnvelope {
let routing_keys = did_doc.routing_keys();

let mut to = recipient_keys
.get(0)
.first()
.map(String::from)
.ok_or(AriesVcxError::from_msg(
AriesVcxErrorKind::InvalidState,
Expand Down
2 changes: 1 addition & 1 deletion aries_vcx_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ indy-credx = { git = "https://github.com/hyperledger/indy-shared-rs", tag = "v1.
libvdrtools = { path = "../libvdrtools", optional = true }
indy-api-types = { path = "../libvdrtools/indy-api-types", optional = true }
async-trait = "0.1.68"
futures = { version = "0.3", default-features = false }
futures = { version = "0.3" }
serde_json = "1.0.95"
time = "0.3.20"
serde = { version = "1.0.159", features = ["derive"] }
Expand Down
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
2 changes: 1 addition & 1 deletion aries_vcx_core/src/errors/mapping_indy_api_types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use indy_api_types::{errors, ErrorCode};
pub use indy_api_types::ErrorCode;
use indy_api_types::{errors::IndyErrorKind, IndyError};

use crate::errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind};
Expand Down
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)
}
}
26 changes: 26 additions & 0 deletions aries_vcx_core/src/ledger2/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
mod indy;

use async_trait::async_trait;

use crate::errors::error::VcxCoreResult;

#[async_trait]
pub trait Ledger {
type Request: Send + Sync;

type Response: Send + Sync;

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

pub trait LedgerRequest<L: Ledger + ?Sized> {
fn into_ledger_request(self) -> VcxCoreResult<L::Request>;

fn as_ledger_request(&self) -> VcxCoreResult<L::Request>;

fn from_ledger_response(response: L::Response) -> VcxCoreResult<Self>
where
Self: Sized;
}
3 changes: 3 additions & 0 deletions aries_vcx_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ pub mod anoncreds;
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 use indy_ledger_response_parser::ResponseParser;
pub use indy_vdr::config::PoolConfig;
Expand Down
Loading
Loading