Skip to content

Commit

Permalink
handle builder address cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Dec 11, 2023
1 parent 9900a5d commit 5aee8e9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
1 change: 0 additions & 1 deletion bindings/core/src/method/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use iota_sdk::{
client::{
api::{input_selection::Burn, PreparedTransactionDataDto, SignedTransactionDataDto},
node_manager::node::NodeAuth,
secret::GenerateAddressOptions,
},
types::block::{
address::{Bech32Address, Hrp},
Expand Down
2 changes: 1 addition & 1 deletion bindings/core/src/method_handler/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use iota_sdk::{
client::api::{
PreparedTransactionData, PreparedTransactionDataDto, SignedTransactionData, SignedTransactionDataDto,
},
types::{block::address::ToBech32Ext, TryFromDto},
types::TryFromDto,
wallet::{types::TransactionWithMetadataDto, PreparedCreateNativeTokenTransactionDto, Wallet},
};

Expand Down
46 changes: 22 additions & 24 deletions sdk/src/wallet/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ use crate::wallet::storage::adapter::memory::Memory;
#[cfg(feature = "storage")]
use crate::wallet::storage::{StorageManager, StorageOptions};
use crate::{
client::{
api::GetAddressesOptions,
secret::{GenerateAddressOptions, SecretManage, SecretManager},
},
types::block::address::{Address, Bech32Address},
client::secret::{GenerateAddressOptions, SecretManage, SecretManager},
types::block::address::Bech32Address,
wallet::{
core::{Bip44, WalletData, WalletInner},
operations::syncing::SyncOptions,
Expand Down Expand Up @@ -74,12 +71,11 @@ where
self
}

// TODO: only add this fn if the SecretManager is optional. Otherwise the address should be generated.
// /// Set the wallet address.
// pub fn with_address(mut self, address: impl Into<Option<Bech32Address>>) -> Self {
// self.address = address.into();
// self
// }
/// Set the wallet address.
pub fn with_address(mut self, address: impl Into<Option<Bech32Address>>) -> Self {
self.address = address.into();
self
}

/// Set the alias of the wallet.
pub fn with_alias(mut self, alias: impl Into<Option<String>>) -> Self {
Expand Down Expand Up @@ -204,23 +200,25 @@ where
self.alias = loaded_wallet_builder.as_ref().and_then(|builder| builder.alias.clone());
}

// May use a previously stored wallet address if it wasn't provided
// Since we don't allow setting it `address` must be None at this point.
self.address = loaded_wallet_builder
let provided_address = self.address;
let restored_address = loaded_wallet_builder
.as_ref()
.and_then(|builder| builder.address.clone());

let generated_address = Self::generate_wallet_address(client_options, bip_path, secret_manager).await?;
if let Some(address) = &self.address {
// Make sure the provided or loaded address doesn't conflict with the secret manager (private key) and the
// bip path.
if address != &generated_address {
// TODO: change to appropriate error, e.g. Error::ConflictingWalletAddress or smth.
return Err(crate::wallet::Error::MissingParameter("address"));
self.address = Some(match (provided_address, restored_address) {
(Some(provided), Some(restored)) => {
// error early if they conflict
if provided != restored {
// TODO: change to appropriate error, e.g. Error::ConflictingWalletAddress or smth.
return Err(crate::wallet::Error::MissingParameter("address"));
} else {
restored
}
}
} else {
self.address = Some(generated_address);
}
(Some(provided), None) => provided,
(None, Some(restored)) => restored,
(None, None) => Self::generate_wallet_address(client_options, bip_path, secret_manager).await?,
});

// Panic: an address must exist at this point
let address = self.address.as_ref().unwrap().clone();
Expand Down

0 comments on commit 5aee8e9

Please sign in to comment.