Skip to content

Commit

Permalink
feat: add new Wallet trait, implement for indy wallet
Browse files Browse the repository at this point in the history
Signed-off-by: Ondrej Prazak <ondrej.prazak@absa.africa>
  • Loading branch information
Ondrej Prazak committed Dec 20, 2023
1 parent db8c27a commit 7c399a9
Show file tree
Hide file tree
Showing 13 changed files with 627 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions aries/aries_vcx/src/errors/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ pub enum AriesVcxErrorKind {
#[error("Could not parse a value")]
ParsingError,

#[error("Unexpected wallet error")]
WalletUnexpected,

// A2A
#[error("Invalid HTTP response.")]
InvalidHttpResponse,
Expand Down
1 change: 1 addition & 0 deletions aries/aries_vcx/src/errors/mapping_others.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ impl From<AriesVcxCoreError> for AriesVcxError {
AriesVcxErrorKind::DuplicationMasterSecret
}
AriesVcxCoreErrorKind::DuplicationDid => AriesVcxErrorKind::DuplicationDid,
AriesVcxCoreErrorKind::WalletUnexpected => AriesVcxErrorKind::WalletUnexpected,
AriesVcxCoreErrorKind::LoggingError => AriesVcxErrorKind::LoggingError,
AriesVcxCoreErrorKind::EncodeError => AriesVcxErrorKind::EncodeError,
AriesVcxCoreErrorKind::UnknownError => AriesVcxErrorKind::UnknownError,
Expand Down
1 change: 1 addition & 0 deletions aries/aries_vcx_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ legacy_proof = []

[dependencies]
agency_client = { path = "../misc/legacy/agency_client" }
bs58 = { version = "0.5" }
indy-vdr = { git = "https://github.com/hyperledger/indy-vdr.git", rev = "c143268", default-features = false, features = ["log"] }
indy-credx = { git = "https://github.com/hyperledger/indy-shared-rs", tag = "v1.1.0", optional = true }
libvdrtools = { path = "../misc/legacy/libvdrtools", optional = true }
Expand Down
3 changes: 3 additions & 0 deletions aries/aries_vcx_core/src/errors/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ pub enum AriesVcxCoreErrorKind {
#[error("Attempted to add a DID to wallet when that DID already exists in wallet")]
DuplicationDid,

#[error("Unexpected wallet error")]
WalletUnexpected,

// Logger
#[error("Logging Error")]
LoggingError,
Expand Down
1 change: 1 addition & 0 deletions aries/aries_vcx_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub mod global;
pub mod ledger;
pub mod utils;
pub mod wallet;
pub mod wallet2;

pub use indy_ledger_response_parser::ResponseParser;
pub use indy_vdr::config::PoolConfig;
Expand Down
151 changes: 151 additions & 0 deletions aries/aries_vcx_core/src/wallet2/indy_wallet/indy_did_wallet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
use async_trait::async_trait;
use vdrtools::{DidMethod, DidValue, KeyInfo, Locator, MyDidInfo};

use crate::{
errors::error::{AriesVcxCoreError, VcxCoreResult},
wallet::indy::IndySdkWallet,
wallet2::{DidData, DidWallet, SigType},
};

#[async_trait]
impl DidWallet for IndySdkWallet {
async fn create_and_store_my_did(
&self,
seed: &str,
method_name: Option<&str>,
) -> VcxCoreResult<DidData> {
let res = Locator::instance()
.did_controller
.create_and_store_my_did(
self.wallet_handle,
MyDidInfo {
method_name: method_name.map(|m| DidMethod(m.into())),
seed: Some(seed.into()),
..MyDidInfo::default()
},
)
.await
.map_err::<AriesVcxCoreError, _>(From::from)?;

Ok(DidData {
did: res.0,
verkey: res.1,
})
}

async fn did_key(&self, did: &str) -> VcxCoreResult<String> {
Locator::instance()
.did_controller
.key_for_local_did(self.wallet_handle, DidValue(did.into()))
.await
.map_err(From::from)
}

async fn replace_did_key(&self, did: &str, seed: &str) -> VcxCoreResult<String> {
let key_info = KeyInfo {
seed: Some(seed.into()),
..Default::default()
};

let key = Locator::instance()
.did_controller
.replace_keys_start(self.wallet_handle, key_info, DidValue(did.into()))
.await?;

Locator::instance()
.did_controller
.replace_keys_apply(self.wallet_handle, DidValue(did.into()))
.await?;

Ok(key)
}

async fn sign(&self, key: &str, msg: &[u8], _sig_type: SigType) -> VcxCoreResult<Vec<u8>> {
Locator::instance()
.crypto_controller
.crypto_sign(self.wallet_handle, key, msg)
.await
.map_err(From::from)
}

async fn verify(
&self,
key: &str,
msg: &[u8],
signature: &[u8],
_sig_type: SigType,
) -> VcxCoreResult<bool> {
Locator::instance()
.crypto_controller
.crypto_verify(key, msg, signature)
.await
.map_err(From::from)
}
}

#[cfg(test)]
mod tests {
use rand::{distributions::Alphanumeric, Rng};

use crate::wallet2::{indy_wallet::test_helper::create_test_wallet, DidWallet, SigType};

#[tokio::test]
#[ignore]
async fn test_indy_should_sign_and_verify() {
let wallet = create_test_wallet().await;

let seed: String = rand::thread_rng()
.sample_iter(Alphanumeric)
.take(32)
.map(char::from)
.collect();

let did_data = DidWallet::create_and_store_my_did(&wallet, &seed, None)
.await
.unwrap();

let msg = "sign this".as_bytes();
let sig = DidWallet::sign(&wallet, &did_data.verkey, msg, SigType::EdDSA)
.await
.unwrap();

let res = DidWallet::verify(&wallet, &did_data.verkey, msg, &sig, SigType::EdDSA)
.await
.unwrap();
assert!(res);
}

#[tokio::test]
#[ignore]
async fn test_indy_should_rotate_keys() {
let wallet = create_test_wallet().await;

let seed: String = rand::thread_rng()
.sample_iter(Alphanumeric)
.take(32)
.map(char::from)
.collect();

let did_data = DidWallet::create_and_store_my_did(&wallet, &seed, None)
.await
.unwrap();

let key = wallet.did_key(&did_data.did).await.unwrap();

assert_eq!(did_data.verkey, key);

let new_seed: String = rand::thread_rng()
.sample_iter(Alphanumeric)
.take(32)
.map(char::from)
.collect();

let res = wallet
.replace_did_key(&did_data.did, &new_seed)
.await
.unwrap();

let new_key = wallet.did_key(&did_data.did).await.unwrap();
assert_eq!(res, new_key);
}
}
Loading

0 comments on commit 7c399a9

Please sign in to comment.