From 300206b95d4347e79a1330151feb070f768b3ebc Mon Sep 17 00:00:00 2001 From: Michael Snoyman Date: Sun, 24 Nov 2024 16:07:18 +0200 Subject: [PATCH 1/2] Add support for MsgSend on THORChain --- packages/cosmos/src/messages.rs | 52 ++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/packages/cosmos/src/messages.rs b/packages/cosmos/src/messages.rs index c2d3d9b..028b461 100644 --- a/packages/cosmos/src/messages.rs +++ b/packages/cosmos/src/messages.rs @@ -199,16 +199,54 @@ impl From for TxMessage { impl From for TxMessage { fn from(msg: MsgSend) -> Self { - TxMessage::new( - "/cosmos.bank.v1beta1.MsgSend", - msg.encode_to_vec(), - format!( - "{} sending {} to {}", + // Very hacky approach to sending the alternative MsgSend + // on THORChain. For simplicity, we simply look for an appropriate + // HRP prefix on the destination address. + if msg.to_address.starts_with("sthor1") { + #[derive(::prost::Message)] + struct ThorMsgSend { + /// Source of funds + #[prost(bytes, tag = "1")] + from_address: Vec, + /// Destination of funds + #[prost(bytes, tag = "2")] + to_address: Vec, + /// Funds to be sent + #[prost(message, repeated, tag = "3")] + amount: ::prost::alloc::vec::Vec, + } + let description = format!( + "{} sending (via THORChain message) {} to {}", msg.from_address, PrettyCoins(msg.amount.as_slice()), msg.to_address, - ), - ) + ); + fn get_acc_address(s: &str) -> Vec { + // This code will panic if an invalid address is provided. + // We could fix that, but given how corner a case this is, + // I'm not interested in changing the overall API to accommodate + // it. + bech32::decode(s).unwrap().1 + } + let thormsg = ThorMsgSend { + from_address: get_acc_address(&msg.from_address), + to_address: get_acc_address(&msg.to_address), + amount: msg.amount, + }; + + TxMessage::new("/types.MsgSend", thormsg.encode_to_vec(), description) + } else { + TxMessage::new( + "/cosmos.bank.v1beta1.MsgSend", + msg.encode_to_vec(), + format!( + "{} sending {} to {}", + msg.from_address, + PrettyCoins(msg.amount.as_slice()), + msg.to_address, + ), + ) + } } } From 29412f0e5f2844d79396a5feb632667b8e07950a Mon Sep 17 00:00:00 2001 From: Michael Snoyman Date: Mon, 25 Nov 2024 10:07:04 +0200 Subject: [PATCH 2/2] Use expect for better error message --- packages/cosmos/src/messages.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/cosmos/src/messages.rs b/packages/cosmos/src/messages.rs index 028b461..27f812a 100644 --- a/packages/cosmos/src/messages.rs +++ b/packages/cosmos/src/messages.rs @@ -226,7 +226,9 @@ impl From for TxMessage { // We could fix that, but given how corner a case this is, // I'm not interested in changing the overall API to accommodate // it. - bech32::decode(s).unwrap().1 + bech32::decode(s) + .expect("Invalid THORChain address provided for MsgSend") + .1 } let thormsg = ThorMsgSend { from_address: get_acc_address(&msg.from_address),