Skip to content

Commit

Permalink
* implement CoinAssocTypes::my_addr for utxo hd wallet
Browse files Browse the repository at this point in the history
* make enabled_address non-optional
  • Loading branch information
shamardy committed Mar 14, 2024
1 parent c538bb5 commit d085af1
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 25 deletions.
4 changes: 2 additions & 2 deletions mm2src/coins/eth/v2_activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ pub(crate) async fn build_address_and_priv_key_policy(
hd_wallet_storage,
derivation_path: path_to_coin.clone(),
accounts: HDAccountsMutex::new(accounts),
enabled_address: Some(*path_to_address),
enabled_address: *path_to_address,
gap_limit,
};
let derivation_method = DerivationMethod::HDWallet(hd_wallet);
Expand Down Expand Up @@ -656,7 +656,7 @@ pub(crate) async fn build_address_and_priv_key_policy(
hd_wallet_storage,
derivation_path: path_to_coin.clone(),
accounts: HDAccountsMutex::new(accounts),
enabled_address: Some(*path_to_address),
enabled_address: *path_to_address,
gap_limit,
};
let derivation_method = DerivationMethod::HDWallet(hd_wallet);
Expand Down
9 changes: 3 additions & 6 deletions mm2src/coins/hd_wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,9 @@ where
pub derivation_path: StandardHDPathToCoin,
/// Contains information about the accounts enabled for this HD wallet.
pub accounts: HDAccountsMutex<HDAccount>,
// Todo: This should be removed in the future to enable simultaneous swaps from multiple addresses
/// The address that's specifically enabled for certain operations, e.g. swaps.
///
/// For some wallet types, such as hardware wallets, this will be `None`
/// until these operations are supported for them. When set, this address
/// is ready to facilitate swap transactions and other specific operations.
pub enabled_address: Option<HDAccountAddressId>,
pub enabled_address: HDAccountAddressId,
/// Defines the maximum number of consecutive addresses that can be generated
/// without any associated transactions. If an address outside this limit
/// receives transactions, they won't be identified.
Expand Down Expand Up @@ -345,7 +342,7 @@ where
}

async fn get_enabled_address(&self) -> Option<<Self::HDAccount as HDAccountOps>::HDAddress> {
let enabled_address = self.enabled_address?;
let enabled_address = self.enabled_address;
let account = self.get_account(enabled_address.account_id).await?;
let hd_address_id = HDAddressId {
chain: enabled_address.chain,
Expand Down
3 changes: 2 additions & 1 deletion mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,7 @@ pub trait ToBytes {
}

/// Defines associated types specific to each coin (Pubkey, Address, etc.)
#[async_trait]
pub trait CoinAssocTypes {
type Address: Send + Sync + fmt::Display;
type AddressParseError: fmt::Debug + Send + fmt::Display;
Expand All @@ -1477,7 +1478,7 @@ pub trait CoinAssocTypes {
type Sig: ToBytes + Send + Sync;
type SigParseError: fmt::Debug + Send + fmt::Display;

fn my_addr(&self) -> &Self::Address;
async fn my_addr(&self) -> Self::Address;

fn parse_address(&self, address: &str) -> Result<Self::Address, Self::AddressParseError>;

Expand Down
3 changes: 2 additions & 1 deletion mm2src/coins/test_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ impl ToBytes for TestSig {
fn to_bytes(&self) -> Vec<u8> { vec![] }
}

#[async_trait]
impl CoinAssocTypes for TestCoin {
type Address = String;
type AddressParseError = String;
Expand All @@ -446,7 +447,7 @@ impl CoinAssocTypes for TestCoin {
type Sig = TestSig;
type SigParseError = String;

fn my_addr(&self) -> &Self::Address { todo!() }
async fn my_addr(&self) -> Self::Address { todo!() }

fn parse_address(&self, address: &str) -> Result<Self::Address, Self::AddressParseError> { todo!() }

Expand Down
16 changes: 11 additions & 5 deletions mm2src/coins/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ use super::{big_decimal_from_sat_unsigned, BalanceError, BalanceFut, BalanceResu
TransactionEnum, TransactionErr, UnexpectedDerivationMethod, VerificationError, WithdrawError,
WithdrawRequest};
use crate::coin_balance::{EnableCoinScanPolicy, EnabledCoinBalanceParams, HDAddressBalanceScanner};
use crate::hd_wallet::{HDAccountAddressId, HDAccountOps, HDWalletCoinOps, HDWalletStorageError};
use crate::hd_wallet::{HDAccountAddressId, HDAccountOps, HDAddressOps, HDWalletCoinOps, HDWalletOps,
HDWalletStorageError};
use crate::utxo::tx_cache::UtxoVerboseCacheShared;
use crate::{CoinAssocTypes, ToBytes};

Expand Down Expand Up @@ -1039,6 +1040,7 @@ impl ToBytes for Signature {
fn to_bytes(&self) -> Vec<u8> { self.to_vec() }
}

#[async_trait]
impl<T: UtxoCommonOps> CoinAssocTypes for T {
type Address = Address;
type AddressParseError = MmError<AddrFromStrError>;
Expand All @@ -1051,11 +1053,15 @@ impl<T: UtxoCommonOps> CoinAssocTypes for T {
type Sig = Signature;
type SigParseError = MmError<secp256k1::Error>;

fn my_addr(&self) -> &Self::Address {
async fn my_addr(&self) -> Self::Address {
match &self.as_ref().derivation_method {
DerivationMethod::SingleAddress(addr) => addr,
// Todo: implement for HD wallet enabled address, this is required for trade_v2_test_rick_and_morty
DerivationMethod::HDWallet(_) => todo!(),
DerivationMethod::SingleAddress(addr) => addr.clone(),
// Todo: Expect should not fail but we need to handle it properly
DerivationMethod::HDWallet(hd_wallet) => hd_wallet
.get_enabled_address()
.await
.expect("Getting enabled address should not fail!")
.address(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions mm2src/coins/utxo/utxo_builder/utxo_coin_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub trait UtxoFieldsWithGlobalHDBuilder: UtxoCoinBuilderCommonOps {
hd_wallet_storage,
derivation_path: path_to_coin.clone(),
accounts: HDAccountsMutex::new(accounts),
enabled_address: Some(path_to_address),
enabled_address: path_to_address,
gap_limit,
},
address_format,
Expand Down Expand Up @@ -368,7 +368,7 @@ pub trait UtxoFieldsWithHardwareWalletBuilder: UtxoCoinBuilderCommonOps {
hd_wallet_storage,
derivation_path: path_to_coin,
accounts: HDAccountsMutex::new(accounts),
enabled_address: None,
enabled_address: self.activation_params().path_to_address,
gap_limit,
},
address_format,
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo/utxo_common_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ pub(super) async fn test_hd_utxo_tx_history_impl(rpc_client: ElectrumClient) {
hd_wallet_storage: HDWalletCoinStorage::default(),
derivation_path: StandardHDPathToCoin::from_str("m/44'/141'").unwrap(),
accounts: HDAccountsMutex::new(hd_accounts),
enabled_address: None,
enabled_address: HDAccountAddressId::default(),
gap_limit: 20,
},
address_format: UtxoAddressFormat::Standard,
Expand Down
6 changes: 3 additions & 3 deletions mm2src/coins/utxo/utxo_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3532,7 +3532,7 @@ fn test_account_balance_rpc() {
hd_wallet_storage: HDWalletCoinStorage::default(),
derivation_path: StandardHDPathToCoin::from_str("m/44'/141'").unwrap(),
accounts: HDAccountsMutex::new(hd_accounts),
enabled_address: None,
enabled_address: HDAccountAddressId::default(),
gap_limit: 3,
},
address_format: UtxoAddressFormat::Standard,
Expand Down Expand Up @@ -3864,7 +3864,7 @@ fn test_scan_for_new_addresses() {
hd_wallet_storage: HDWalletCoinStorage::default(),
derivation_path: StandardHDPathToCoin::from_str("m/44'/141'").unwrap(),
accounts: HDAccountsMutex::new(hd_accounts),
enabled_address: None,
enabled_address: HDAccountAddressId::default(),
gap_limit: 3,
},
address_format: UtxoAddressFormat::Standard,
Expand Down Expand Up @@ -4006,7 +4006,7 @@ fn test_get_new_address() {
hd_wallet_storage: HDWalletCoinStorage::default(),
derivation_path: StandardHDPathToCoin::from_str("m/44'/141'").unwrap(),
accounts: HDAccountsMutex::new(hd_accounts),
enabled_address: None,
enabled_address: HDAccountAddressId::default(),
gap_limit: 2,
},
address_format: UtxoAddressFormat::Standard,
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_main/src/lp_swap/maker_swap_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ impl<MakerCoin: MmCoin + MakerCoinSwapOpsV2, TakerCoin: MmCoin + TakerCoinSwapOp
taker_coin_htlc_pub: state_machine.taker_coin.derive_htlc_pubkey(&unique_data),
maker_coin_swap_contract: state_machine.maker_coin.swap_contract_address().map(|bytes| bytes.0),
taker_coin_swap_contract: state_machine.taker_coin.swap_contract_address().map(|bytes| bytes.0),
taker_coin_address: state_machine.taker_coin.my_addr().to_string(),
taker_coin_address: state_machine.taker_coin.my_addr().await.to_string(),
};
debug!("Sending maker negotiation message {:?}", maker_negotiation_msg);
let swap_msg = SwapMessage {
Expand Down Expand Up @@ -1616,7 +1616,7 @@ impl<MakerCoin: MmCoin + MakerCoinSwapOpsV2, TakerCoin: MmCoin + TakerCoinSwapOp
time_lock: self.negotiation_data.taker_payment_locktime,
maker_secret_hash: &state_machine.secret_hash(),
maker_pub: &state_machine.taker_coin.derive_htlc_pubkey_v2(&unique_data),
maker_address: state_machine.taker_coin.my_addr(),
maker_address: &state_machine.taker_coin.my_addr().await,
taker_pub: &self.negotiation_data.taker_coin_htlc_pub_from_taker,
dex_fee: &state_machine.dex_fee,
premium_amount: Default::default(),
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_main/tests/docker_tests/swap_proto_v2_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ fn send_and_spend_taker_payment_dex_fee_burn() {
time_lock: 0,
maker_secret_hash,
maker_pub,
maker_address: maker_coin.my_addr(),
maker_address: &block_on(maker_coin.my_addr()),
taker_pub,
dex_fee_pub: &DEX_FEE_ADDR_RAW_PUBKEY,
dex_fee,
Expand Down Expand Up @@ -437,7 +437,7 @@ fn send_and_spend_taker_payment_standard_dex_fee() {
time_lock: 0,
maker_secret_hash,
maker_pub,
maker_address: maker_coin.my_addr(),
maker_address: &block_on(maker_coin.my_addr()),
taker_pub,
dex_fee_pub: &DEX_FEE_ADDR_RAW_PUBKEY,
dex_fee,
Expand Down

0 comments on commit d085af1

Please sign in to comment.