Skip to content

Commit

Permalink
Add new addr_to_string function to ParseCoinAssocTypes trait and use …
Browse files Browse the repository at this point in the history
…it in state machine, instead of to_string
  • Loading branch information
laruh committed Nov 30, 2024
1 parent c1a5063 commit dc21ac6
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 3 deletions.
6 changes: 4 additions & 2 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6999,10 +6999,10 @@ impl ToBytes for SignedEthTx {

#[derive(Debug, Display, EnumFromStringify)]
pub enum EthAssocTypesError {
InvalidHexString(String),
#[from_stringify("DecoderError")]
TxParseError(String),
ParseSignatureError(String),
ParseAddressError(String),
}

#[derive(Debug, Display)]
Expand Down Expand Up @@ -7041,8 +7041,10 @@ impl ParseCoinAssocTypes for EthCoin {
}
}

fn addr_to_string(&self, address: &Self::Address) -> String { eth_addr_to_hex(address) }

fn parse_address(&self, address: &str) -> Result<Self::Address, Self::AddressParseError> {
Address::from_str(address).map_to_mm(|e| EthAssocTypesError::InvalidHexString(e.to_string()))
addr_from_str(address).map_to_mm(EthAssocTypesError::ParseAddressError)
}

/// As derive_htlc_pubkey_v2 returns coin specific pubkey we can use [Public::from_slice] directly
Expand Down
8 changes: 8 additions & 0 deletions mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,14 @@ pub trait ParseCoinAssocTypes {

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

/// Converts the coin `Address` type into a properly formatted string representation.
///
/// Don't use `to_string` directly on `Self::Address` types in generic TPU code!
/// It may produce abbreviated or non-standard formats (e.g. `ethereum_types::Address` will be like this `0x7cc9…3874`),
/// which are not guaranteed to be parsable back into the original `Address` type.
/// This function ensures the resulting string is consistently formatted and fully reversible.
fn addr_to_string(&self, address: &Self::Address) -> String;

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

fn parse_pubkey(&self, pubkey: &[u8]) -> Result<Self::Pubkey, Self::PubkeyParseError>;
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/test_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@ impl ParseCoinAssocTypes for TestCoin {

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

fn addr_to_string(&self, address: &Self::Address) -> String { unimplemented!() }

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

fn parse_pubkey(&self, pubkey: &[u8]) -> Result<Self::Pubkey, Self::PubkeyParseError> { unimplemented!() }
Expand Down
2 changes: 2 additions & 0 deletions mm2src/coins/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,8 @@ impl<T: UtxoCommonOps> ParseCoinAssocTypes for T {
}
}

fn addr_to_string(&self, address: &Self::Address) -> String { address.to_string() }

fn parse_address(&self, address: &str) -> Result<Self::Address, Self::AddressParseError> {
self.address_from_str(address)
}
Expand Down
3 changes: 2 additions & 1 deletion mm2src/mm2_main/src/lp_swap/maker_swap_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,7 @@ impl<MakerCoin: MmCoin + MakerCoinSwapOpsV2, TakerCoin: MmCoin + TakerCoinSwapOp

async fn on_changed(self: Box<Self>, state_machine: &mut Self::StateMachine) -> StateResult<Self::StateMachine> {
let unique_data = state_machine.unique_data();
let taker_coin_address = state_machine.taker_coin.my_addr().await;

let maker_negotiation_msg = MakerNegotiation {
started_at: state_machine.started_at,
Expand All @@ -922,7 +923,7 @@ impl<MakerCoin: MmCoin + MakerCoinSwapOpsV2, TakerCoin: MmCoin + TakerCoinSwapOp
taker_coin_htlc_pub: state_machine.taker_coin.derive_htlc_pubkey_v2_bytes(&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().await.to_string(),
taker_coin_address: state_machine.taker_coin.addr_to_string(&taker_coin_address),
};
debug!("Sending maker negotiation message {:?}", maker_negotiation_msg);
let swap_msg = SwapMessage {
Expand Down

0 comments on commit dc21ac6

Please sign in to comment.