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

Dynamic secret manager #1219

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
54da871
Initial changes
Sep 12, 2023
25c0626
Merge branch '2.0' into feat/dyn-secret-manager
Sep 13, 2023
c4688b5
Merge branch '2.0' into feat/dyn-secret-manager
Sep 14, 2023
3a77426
Merge branch '2.0' into feat/dyn-secret-manager
Sep 18, 2023
ccf6682
fix some of the builder finish call sites
Sep 18, 2023
bf66ca4
fix load storage
Sep 18, 2023
11cab85
Merge branch '2.0' into feat/dyn-secret-manager
Sep 28, 2023
609c803
get juked rust compiler
Sep 28, 2023
00d80cc
feature sets
Sep 28, 2023
716dd4e
Merge branch '2.0' into feat/dyn-secret-manager
Sep 29, 2023
baa8d4c
Merge branch '2.0' into feat/dyn-secret-manager
Oct 2, 2023
f634674
Merge branch '2.0' into feat/dyn-secret-manager
Oct 3, 2023
71adbbd
Merge branch '2.0' into feat/dyn-secret-manager
Oct 4, 2023
9a481c5
Merge branch '2.0' into feat/dyn-secret-manager
Oct 5, 2023
e3cccd6
Merge branch '2.0' into feat/dyn-secret-manager
Oct 6, 2023
9ddf2d8
Merge branch '2.0' into feat/dyn-secret-manager
Oct 10, 2023
2258eb5
Merge branch '2.0' into feat/dyn-secret-manager
Oct 10, 2023
ed44c5c
Merge branch '2.0' into feat/dyn-secret-manager
Oct 10, 2023
0cb5d8e
Merge branch '2.0' into feat/dyn-secret-manager
Oct 11, 2023
21206a3
Merge branch '2.0' into feat/dyn-secret-manager
Oct 11, 2023
6507b4b
Merge branch '2.0' into feat/dyn-secret-manager
Oct 13, 2023
3f85f5f
Merge branch '2.0' into feat/dyn-secret-manager
Oct 13, 2023
48f89f4
Merge branch '2.0' into feat/dyn-secret-manager
Oct 16, 2023
49b0ca4
Merge branch '2.0' into feat/dyn-secret-manager
Oct 17, 2023
7cda312
Merge branch '2.0' into feat/dyn-secret-manager
Oct 18, 2023
16f021c
Merge branch '2.0' into feat/dyn-secret-manager
Oct 18, 2023
ff3923b
Merge branch '2.0' into feat/dyn-secret-manager
Oct 18, 2023
43047f5
Merge branch '2.0' into feat/dyn-secret-manager
Oct 19, 2023
49cefc9
Merge branch '2.0' into feat/dyn-secret-manager
Oct 20, 2023
e2fccd8
Merge branch '2.0' into feat/dyn-secret-manager
Oct 23, 2023
8cb5679
Merge branch '2.0' into feat/dyn-secret-manager
Oct 24, 2023
2facc3e
Merge branch '2.0' into feat/dyn-secret-manager
Oct 25, 2023
f73e6b2
Merge branch '2.0' into feat/dyn-secret-manager
Oct 25, 2023
3a8d6b1
Merge branch '2.0' into feat/dyn-secret-manager
Oct 31, 2023
aa9b377
Merge branch '2.0' into feat/dyn-secret-manager
Nov 29, 2023
5f2436c
Merge branch '2.0' into feat/dyn-secret-manager
Nov 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions bindings/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum Error {
/// SerdeJson errors.
#[error("{0}")]
SerdeJson(#[from] serde_json::error::Error),
#[error("secret manager was not provided")]
MissingSecretManager,
/// Unpack errors.
#[error("{0}")]
Unpack(#[from] packable::error::UnpackError<iota_sdk::types::block::Error, UnexpectedEOF>),
Expand Down
13 changes: 6 additions & 7 deletions bindings/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use derivative::Derivative;
use fern_logger::{logger_init, LoggerConfig, LoggerOutputConfigBuilder};
pub use iota_sdk;
use iota_sdk::{
client::secret::{SecretManager, SecretManagerDto},
client::secret::{SecretManage, SecretManagerDto},
types::block::address::Bech32Address,
utils::serde::bip44::option_bip44,
wallet::{ClientOptions, Wallet},
Expand Down Expand Up @@ -86,7 +86,10 @@ impl WalletOptions {
self
}

pub async fn build(self) -> iota_sdk::wallet::Result<Wallet> {
pub async fn build<S: SecretManage>(self, secret_manager: &S) -> iota_sdk::wallet::Result<Wallet>
where
iota_sdk::client::Error: From<S::Error>,
{
log::debug!("wallet options: {self:?}");
let mut builder = Wallet::builder()
.with_address(self.address)
Expand All @@ -99,11 +102,7 @@ impl WalletOptions {
builder = builder.with_storage_path(storage_path);
}

if let Some(secret_manager) = self.secret_manager {
builder = builder.with_secret_manager(SecretManager::try_from(secret_manager)?);
}

builder.finish().await
builder.finish(secret_manager).await
}
}

Expand Down
98 changes: 49 additions & 49 deletions bindings/core/src/method/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,53 +434,53 @@ pub enum WalletMethod {
/// Bech32 HRP
bech32_hrp: Option<Hrp>,
},
/// Get the ledger nano status
/// Expected response: [`LedgerNanoStatus`](crate::Response::LedgerNanoStatus)
#[cfg(feature = "ledger_nano")]
#[cfg_attr(docsrs, doc(cfg(feature = "ledger_nano")))]
GetLedgerNanoStatus,
/// Set the stronghold password.
/// Expected response: [`Ok`](crate::Response::Ok)
#[cfg(feature = "stronghold")]
#[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
SetStrongholdPassword {
#[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
password: String,
},
/// Set the stronghold password clear interval.
/// Expected response: [`Ok`](crate::Response::Ok)
#[cfg(feature = "stronghold")]
#[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
#[serde(rename_all = "camelCase")]
SetStrongholdPasswordClearInterval { interval_in_milliseconds: Option<u64> },
/// Store a mnemonic into the Stronghold vault.
/// Expected response: [`Ok`](crate::Response::Ok)
#[cfg(feature = "stronghold")]
#[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
StoreMnemonic {
#[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
mnemonic: String,
},
/// Change the Stronghold password to another one and also re-encrypt the values in the loaded snapshot with it.
/// Expected response: [`Ok`](crate::Response::Ok)
#[cfg(feature = "stronghold")]
#[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
#[serde(rename_all = "camelCase")]
ChangeStrongholdPassword {
#[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
current_password: String,
#[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
new_password: String,
},
/// Clears the Stronghold password from memory.
/// Expected response: [`Ok`](crate::Response::Ok)
#[cfg(feature = "stronghold")]
#[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
ClearStrongholdPassword,
/// Checks if the Stronghold password is available.
/// Expected response:
/// [`Bool`](crate::Response::Bool)
#[cfg(feature = "stronghold")]
#[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
IsStrongholdPasswordAvailable,
// /// Get the ledger nano status
// /// Expected response: [`LedgerNanoStatus`](crate::Response::LedgerNanoStatus)
// #[cfg(feature = "ledger_nano")]
// #[cfg_attr(docsrs, doc(cfg(feature = "ledger_nano")))]
// GetLedgerNanoStatus,
// /// Set the stronghold password.
// /// Expected response: [`Ok`](crate::Response::Ok)
// #[cfg(feature = "stronghold")]
// #[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
// SetStrongholdPassword {
// #[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
// password: String,
// },
// /// Set the stronghold password clear interval.
// /// Expected response: [`Ok`](crate::Response::Ok)
// #[cfg(feature = "stronghold")]
// #[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
// #[serde(rename_all = "camelCase")]
// SetStrongholdPasswordClearInterval { interval_in_milliseconds: Option<u64> },
// /// Store a mnemonic into the Stronghold vault.
// /// Expected response: [`Ok`](crate::Response::Ok)
// #[cfg(feature = "stronghold")]
// #[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
// StoreMnemonic {
// #[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
// mnemonic: String,
// },
// /// Change the Stronghold password to another one and also re-encrypt the values in the loaded snapshot with
// it. /// Expected response: [`Ok`](crate::Response::Ok)
// #[cfg(feature = "stronghold")]
// #[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
// #[serde(rename_all = "camelCase")]
// ChangeStrongholdPassword {
// #[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
// current_password: String,
// #[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
// new_password: String,
// },
// /// Clears the Stronghold password from memory.
// /// Expected response: [`Ok`](crate::Response::Ok)
// #[cfg(feature = "stronghold")]
// #[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
// ClearStrongholdPassword,
// /// Checks if the Stronghold password is available.
// /// Expected response:
// /// [`Bool`](crate::Response::Bool)
// #[cfg(feature = "stronghold")]
// #[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
// IsStrongholdPasswordAvailable,
}
22 changes: 11 additions & 11 deletions bindings/core/src/method_handler/call_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ impl CallMethod for Client {
}
}

impl CallMethod for Wallet {
type Method = WalletMethod;

fn call_method<'a>(&'a self, method: Self::Method) -> Pin<Box<dyn Future<Output = Response> + 'a>> {
Box::pin(call_wallet_method(self, method))
}
}

/// Call a client method.
pub async fn call_client_method(client: &Client, method: ClientMethod) -> Response {
log::debug!("Client method: {method:?}");
Expand All @@ -58,9 +50,17 @@ pub async fn call_client_method(client: &Client, method: ClientMethod) -> Respon
}

/// Call a wallet method.
pub async fn call_wallet_method(wallet: &Wallet, method: WalletMethod) -> Response {
pub async fn call_wallet_method<S: 'static + SecretManage + Clone>(
wallet: &Wallet,
secret_manager: &S,
method: WalletMethod,
) -> Response
where
iota_sdk::client::Error: From<S::Error>,
{
log::debug!("Wallet method: {method:?}");
let result = convert_async_panics(|| async { call_wallet_method_internal(wallet, method).await }).await;
let result =
convert_async_panics(|| async { call_wallet_method_internal(wallet, secret_manager, method).await }).await;

let response = result.unwrap_or_else(Response::Error);

Expand All @@ -80,7 +80,7 @@ pub fn call_utils_method(method: UtilsMethod) -> Response {
}

/// Call a secret manager method.
pub async fn call_secret_manager_method<S: SecretManage + DowncastSecretManager>(
pub async fn call_secret_manager_method<S: SecretManage + DowncastSecretManager + ?Sized>(
secret_manager: &S,
method: SecretManagerMethod,
) -> Response
Expand Down
25 changes: 5 additions & 20 deletions bindings/core/src/method_handler/secret_manager.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

#[cfg(feature = "ledger_nano")]
use iota_sdk::client::secret::ledger_nano::LedgerSecretManager;
#[cfg(feature = "stronghold")]
use iota_sdk::client::secret::{stronghold::StrongholdSecretManager, SecretManager};
use iota_sdk::{
client::{
api::{GetAddressesOptions, PreparedTransactionData},
Expand All @@ -19,7 +15,7 @@ use iota_sdk::{
use crate::{method::SecretManagerMethod, response::Response, Result};

/// Call a secret manager method.
pub(crate) async fn call_secret_manager_method_internal<S: SecretManage + DowncastSecretManager>(
pub(crate) async fn call_secret_manager_method_internal<S: SecretManage + DowncastSecretManager + ?Sized>(
secret_manager: &S,
method: SecretManagerMethod,
) -> Result<Response>
Expand Down Expand Up @@ -52,8 +48,8 @@ where
coin_type,
account_index,
range,
bech32_hrp: _,
options,
..
},
} => {
let addresses = secret_manager
Expand All @@ -67,11 +63,7 @@ where
}
#[cfg(feature = "ledger_nano")]
SecretManagerMethod::GetLedgerNanoStatus => {
if let Some(secret_manager) = secret_manager.downcast::<LedgerSecretManager>() {
Response::LedgerNanoStatus(secret_manager.get_ledger_nano_status().await)
} else {
return Err(iota_sdk::client::Error::SecretManagerMismatch.into());
}
Response::LedgerNanoStatus(secret_manager.as_ledger_nano()?.get_ledger_nano_status().await)
}
SecretManagerMethod::SignTransaction {
prepared_transaction_data,
Expand Down Expand Up @@ -121,15 +113,8 @@ where
#[cfg(feature = "stronghold")]
SecretManagerMethod::StoreMnemonic { mnemonic } => {
let mnemonic = crypto::keys::bip39::Mnemonic::from(mnemonic);
if let Some(secret_manager) = secret_manager.downcast::<StrongholdSecretManager>() {
secret_manager.store_mnemonic(mnemonic).await?;
Response::Ok
} else if let Some(SecretManager::Stronghold(secret_manager)) = secret_manager.downcast::<SecretManager>() {
secret_manager.store_mnemonic(mnemonic).await?;
Response::Ok
} else {
return Err(iota_sdk::client::Error::SecretManagerMismatch.into());
}
secret_manager.as_stronghold()?.store_mnemonic(mnemonic).await?;
Response::Ok
}
};
Ok(response)
Expand Down
Loading
Loading