Skip to content

Commit

Permalink
unified interface for creating wallet
Browse files Browse the repository at this point in the history
Signed-off-by: Ondrej Prazak <[email protected]>
  • Loading branch information
Ondrej Prazak committed Feb 28, 2024
1 parent ea4a137 commit a9a1d5a
Show file tree
Hide file tree
Showing 18 changed files with 285 additions and 178 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion aries/aries_vcx_core/src/wallet/askar/askar_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use aries_askar::{
kms::{KeyAlg, LocalKey},
};
use public_key::{Key, KeyType};

use serde::Deserialize;

use crate::{
Expand Down
61 changes: 61 additions & 0 deletions aries/aries_vcx_core/src/wallet/askar/askar_wallet_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::sync::Arc;

use async_trait::async_trait;
use serde::Deserialize;

use super::{key_method::KeyMethod, AskarWallet};
use crate::{
errors::error::VcxCoreResult,
wallet::base_wallet::{BaseWallet, ManageWallet},
};

#[derive(Clone, Debug, Deserialize)]
pub struct AskarWalletConfig {
db_url: String,
key_method: KeyMethod,
pass_key: String,
profile: String,
}

impl AskarWalletConfig {
pub fn new(db_url: &str, key_method: KeyMethod, pass_key: &str, profile: &str) -> Self {
Self {
db_url: db_url.into(),
key_method: key_method.into(),
pass_key: pass_key.into(),
profile: profile.into(),
}
}

pub fn db_url(&self) -> &str {
&self.db_url
}

pub fn key_method(&self) -> &KeyMethod {
&self.key_method
}

pub fn pass_key(&self) -> &str {
&self.pass_key
}

pub fn profile(&self) -> &str {
&self.profile
}
}

#[async_trait]
impl ManageWallet for AskarWalletConfig {
async fn create_wallet(&self) -> VcxCoreResult<Arc<dyn BaseWallet>> {
let askar_wallet = AskarWallet::create(&self, false).await?;
Ok(Arc::new(askar_wallet))
}

async fn open_wallet(&self) -> VcxCoreResult<Arc<dyn BaseWallet>> {
Ok(Arc::new(AskarWallet::open(&self).await?))
}

async fn delete_wallet(&self) -> VcxCoreResult<()> {
todo!();
}
}
50 changes: 50 additions & 0 deletions aries/aries_vcx_core/src/wallet/askar/key_method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use aries_askar::{
storage::{Argon2Level, KdfMethod},
StoreKeyMethod,
};
use serde::Deserialize;

#[derive(Debug, Copy, Clone, Deserialize)]
pub enum KeyMethod {
DeriveKey(AskarKdfMethod),
RawKey,
Unprotected,
}

impl From<KeyMethod> for StoreKeyMethod {
fn from(value: KeyMethod) -> Self {
match value {
KeyMethod::DeriveKey(payload) => StoreKeyMethod::DeriveKey(payload.into()),
KeyMethod::RawKey => StoreKeyMethod::RawKey,
KeyMethod::Unprotected => StoreKeyMethod::Unprotected,
}
}
}

#[derive(Copy, Clone, Debug, Deserialize)]
pub enum AskarKdfMethod {
Argon2i(ArgonLevel),
}

impl From<AskarKdfMethod> for KdfMethod {
fn from(value: AskarKdfMethod) -> Self {
match value {
AskarKdfMethod::Argon2i(payload) => KdfMethod::Argon2i(payload.into()),
}
}
}

#[derive(Copy, Clone, Debug, Deserialize)]
pub enum ArgonLevel {
Interactive,
Moderate,
}

impl From<ArgonLevel> for Argon2Level {
fn from(value: ArgonLevel) -> Self {
match value {
ArgonLevel::Interactive => Argon2Level::Interactive,
ArgonLevel::Moderate => Argon2Level::Moderate,
}
}
}
73 changes: 40 additions & 33 deletions aries/aries_vcx_core/src/wallet/askar/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
use aries_askar::{
entry::EntryTag,
kms::{KeyAlg, KeyEntry, LocalKey},
PassKey, Session, Store, StoreKeyMethod,
Session, Store,
};
use async_trait::async_trait;
use public_key::Key;

use self::{askar_utils::local_key_to_bs58_name, rng_method::RngMethod};
use self::{
askar_utils::local_key_to_bs58_name, askar_wallet_config::AskarWalletConfig,
rng_method::RngMethod,
};
use super::base_wallet::{did_value::DidValue, record_category::RecordCategory, BaseWallet};

use crate::errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind, VcxCoreResult};

mod all_askar_records;
mod askar_did_wallet;
mod askar_record_wallet;
mod askar_utils;
pub mod askar_wallet_config;
mod entry;
mod entry_tags;
mod key_method;
mod key_value;
mod pack;
mod packing_types;
Expand Down Expand Up @@ -44,30 +48,34 @@ impl BaseWallet for AskarWallet {

impl AskarWallet {
pub async fn create(
db_url: &str,
key_method: StoreKeyMethod,
pass_key: PassKey<'_>,
wallet_config: &AskarWalletConfig,
recreate: bool,
profile: &str,
) -> Result<Self, AriesVcxCoreError> {
let backend =
Store::provision(db_url, key_method, pass_key, Some(profile.into()), recreate).await?;
let backend = Store::provision(
wallet_config.db_url(),
wallet_config.key_method().clone().into(),
wallet_config.pass_key().into(),
Some(wallet_config.profile().to_owned()),
recreate,
)
.await?;

Ok(Self {
backend,
profile: profile.into(),
profile: wallet_config.profile().into(),
})
}

pub async fn open(
db_url: &str,
key_method: Option<StoreKeyMethod>,
pass_key: PassKey<'_>,
profile: &str,
) -> Result<Self, AriesVcxCoreError> {
pub async fn open(wallet_config: &AskarWalletConfig) -> Result<Self, AriesVcxCoreError> {
Ok(Self {
backend: Store::open(db_url, key_method, pass_key, Some(profile.into())).await?,
profile: profile.into(),
backend: Store::open(
wallet_config.db_url(),
Some(wallet_config.key_method().clone().into()),
wallet_config.pass_key().into(),
Some(wallet_config.profile().into()),
)
.await?,
profile: wallet_config.profile().into(),
})
}

Expand Down Expand Up @@ -191,24 +199,23 @@ impl AskarWallet {

#[cfg(test)]
pub mod tests {
use crate::wallet::base_wallet::BaseWallet;
use std::sync::Arc;

use crate::wallet::{
askar::{askar_wallet_config::AskarWalletConfig, key_method::KeyMethod},
base_wallet::{BaseWallet, ManageWallet},
};

pub async fn dev_setup_askar_wallet() -> Box<dyn BaseWallet> {
use aries_askar::StoreKeyMethod;
pub async fn dev_setup_askar_wallet() -> Arc<dyn BaseWallet> {
use uuid::Uuid;

use crate::wallet::askar::AskarWallet;
let config = AskarWalletConfig::new(
"sqlite://:memory:",
KeyMethod::Unprotected,
"",
&Uuid::new_v4().to_string(),
);

Box::new(
AskarWallet::create(
"sqlite://:memory:",
StoreKeyMethod::Unprotected,
None.into(),
true,
&Uuid::new_v4().to_string(),
)
.await
.unwrap(),
)
config.create_wallet().await.unwrap()
}
}
3 changes: 1 addition & 2 deletions aries/aries_vcx_core/src/wallet/askar/partial_record.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::{askar_utils::value_from_entry, key_value::KeyValue};
use crate::{
errors::error::VcxCoreResult,
wallet::{
Expand All @@ -6,8 +7,6 @@ use crate::{
},
};

use super::{askar_utils::value_from_entry, key_value::KeyValue};

impl PartialRecord {
pub fn from_askar_entry(entry: aries_askar::entry::Entry) -> VcxCoreResult<Self> {
Ok(Self::builder()
Expand Down
31 changes: 24 additions & 7 deletions aries/aries_vcx_core/src/wallet/base_wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,28 @@ pub trait ImportWallet {

#[async_trait]
pub trait ManageWallet {
async fn create_wallet(&self) -> VcxCoreResult<()>;
async fn create_wallet(&self) -> VcxCoreResult<Arc<dyn BaseWallet>>;

async fn open_wallet(&self) -> VcxCoreResult<Arc<dyn BaseWallet>>;

async fn delete_wallet(&self) -> VcxCoreResult<()>;
}

#[async_trait]
impl ManageWallet for Box<dyn ManageWallet + Send + Sync> {
async fn create_wallet(&self) -> VcxCoreResult<Arc<dyn BaseWallet>> {
self.as_ref().create_wallet().await
}

async fn open_wallet(&self) -> VcxCoreResult<Arc<dyn BaseWallet>> {
self.as_ref().open_wallet().await
}

async fn delete_wallet(&self) -> VcxCoreResult<()> {
self.as_ref().delete_wallet().await
}
}

#[async_trait]
pub trait BaseWallet: RecordWallet + DidWallet + Send + Sync + std::fmt::Debug {
async fn export_wallet(&self, path: &str, backup_key: &str) -> VcxCoreResult<()>;
Expand Down Expand Up @@ -63,7 +78,7 @@ impl BaseWallet for Arc<dyn BaseWallet> {

#[cfg(test)]
mod tests {
use std::str::FromStr;
use std::{str::FromStr, sync::Arc};

use super::BaseWallet;
use crate::{
Expand All @@ -76,7 +91,7 @@ mod tests {
};

#[allow(unused_variables)]
async fn build_test_wallet() -> Box<dyn BaseWallet> {
async fn build_test_wallet() -> Arc<dyn BaseWallet> {
#[cfg(feature = "vdrtools_wallet")]
let wallet = {
use crate::wallet::indy::tests::dev_setup_indy_wallet;
Expand Down Expand Up @@ -489,14 +504,16 @@ mod tests {
#[cfg(test)]
#[cfg(all(feature = "vdrtools_wallet", feature = "askar_wallet"))]
mod compat_tests {
use std::sync::Arc;

use crate::wallet::{
askar::tests::dev_setup_askar_wallet, base_wallet::BaseWallet,
indy::tests::dev_setup_indy_wallet,
};

async fn pack_and_unpack_anoncrypt(
sender: Box<dyn BaseWallet>,
recipient: Box<dyn BaseWallet>,
sender: Arc<dyn BaseWallet>,
recipient: Arc<dyn BaseWallet>,
) {
let did_data = recipient.create_and_store_my_did(None, None).await.unwrap();

Expand All @@ -513,8 +530,8 @@ mod compat_tests {
}

async fn pack_and_unpack_authcrypt(
sender: Box<dyn BaseWallet>,
recipient: Box<dyn BaseWallet>,
sender: Arc<dyn BaseWallet>,
recipient: Arc<dyn BaseWallet>,
) {
let sender_did_data = sender.create_and_store_my_did(None, None).await.unwrap();
let recipient_did_data = recipient.create_and_store_my_did(None, None).await.unwrap();
Expand Down
39 changes: 1 addition & 38 deletions aries/aries_vcx_core/src/wallet/base_wallet/record.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use async_trait::async_trait;
use typed_builder::TypedBuilder;

use crate::{errors::error::VcxCoreResult, wallet::record_tags::RecordTags};

use super::record_category::RecordCategory;
use crate::{errors::error::VcxCoreResult, wallet::record_tags::RecordTags};

#[derive(Debug, Default, Clone, TypedBuilder)]
pub struct Record {
Expand Down Expand Up @@ -57,42 +56,6 @@ impl PartialRecord {
pub fn tags(&self) -> &Option<RecordTags> {
&self.tags
}

// #[cfg(feature = "askar_wallet")]
// pub fn from_askar_entry(entry: aries_askar::entry::Entry) -> VcxCoreResult<Self> {
// use crate::wallet::askar::askar_utils::value_from_entry;

// Ok(Self::builder()
// .name(entry.name.clone())
// .category(Some(entry.category.clone()))
// .value(Some(value_from_entry(entry.clone())?))
// .tags(Some(entry.tags.into()))
// .build())
// }

// #[cfg(feature = "askar_wallet")]
// pub fn from_askar_key_entry(key_entry: aries_askar::kms::KeyEntry) -> VcxCoreResult<Self> {
// use crate::wallet::askar::KeyValue;

// let local_key = key_entry.load_local_key()?;
// let name = key_entry.name();
// let tags = key_entry.tags_as_slice();

// // check for private key length!!!!
// let value = KeyValue::new(
// local_key_to_bs58_private_key(&local_key)?,
// local_key_to_bs58_public_key(&local_key)?,
// );

// let value = serde_json::to_string(&value)?;

// Ok(Self::builder()
// .name(name.into())
// .category(Some(INDY_KEY.into()))
// .value(Some(value))
// .tags(Some(tags.to_vec().into()))
// .build())
// }
}

#[async_trait]
Expand Down
Loading

0 comments on commit a9a1d5a

Please sign in to comment.