-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new Wallet trait, implement for indy wallet
Signed-off-by: Ondrej Prazak <ondrej.prazak@absa.africa>
- Loading branch information
Ondrej Prazak
committed
Dec 20, 2023
1 parent
db8c27a
commit 7c399a9
Showing
13 changed files
with
627 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
aries/aries_vcx_core/src/wallet2/indy_wallet/indy_did_wallet.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.