From b31e4b119981caa2cb88d0a70054c9ff542efcb9 Mon Sep 17 00:00:00 2001 From: Satoshi Otomakan Date: Mon, 13 Nov 2023 13:40:35 +0100 Subject: [PATCH] [Cosmos]: Test Stride messages --- rust/tw_cosmos_sdk/src/modules/tx_builder.rs | 6 +- .../src/transaction/message/stride_message.rs | 12 +- rust/tw_cosmos_sdk/src/transaction/mod.rs | 2 +- rust/tw_cosmos_sdk/tests/sign.rs | 313 ++---------------- rust/tw_cosmos_sdk/tests/sign_staking.rs | 10 +- rust/tw_cosmos_sdk/tests/sign_stride.rs | 104 ++++++ 6 files changed, 141 insertions(+), 306 deletions(-) create mode 100644 rust/tw_cosmos_sdk/tests/sign_stride.rs diff --git a/rust/tw_cosmos_sdk/src/modules/tx_builder.rs b/rust/tw_cosmos_sdk/src/modules/tx_builder.rs index 5f5fac4cb4e..1a1f54ff268 100644 --- a/rust/tw_cosmos_sdk/src/modules/tx_builder.rs +++ b/rust/tw_cosmos_sdk/src/modules/tx_builder.rs @@ -585,15 +585,15 @@ where } pub fn stride_redeem_msg_from_proto( - coin: &dyn CoinContext, + _coin: &dyn CoinContext, redeem: &Proto::mod_Message::MsgStrideLiquidStakingRedeem<'_>, ) -> SigningResult { use crate::transaction::message::stride_message::StrideLiquidRedeemMessage; let msg = StrideLiquidRedeemMessage { - creator: Address::from_str_with_coin(coin, &redeem.creator)?, + creator: redeem.creator.to_string(), amount: U256::from_str(&redeem.amount)?, - receiver: Address::from_str_with_coin(coin, &redeem.receiver)?, + receiver: redeem.receiver.to_string(), host_zone: redeem.host_zone.to_string(), }; Ok(msg.into_boxed()) diff --git a/rust/tw_cosmos_sdk/src/transaction/message/stride_message.rs b/rust/tw_cosmos_sdk/src/transaction/message/stride_message.rs index c2d8f1fa2dd..fe99e32c155 100644 --- a/rust/tw_cosmos_sdk/src/transaction/message/stride_message.rs +++ b/rust/tw_cosmos_sdk/src/transaction/message/stride_message.rs @@ -28,19 +28,19 @@ impl CosmosMessage for StrideLiquidStakeMessage
} } -pub struct StrideLiquidRedeemMessage { - pub creator: Address, +pub struct StrideLiquidRedeemMessage { + pub creator: String, pub amount: U256, - pub receiver: Address, + pub receiver: String, pub host_zone: String, } -impl CosmosMessage for StrideLiquidRedeemMessage
{ +impl CosmosMessage for StrideLiquidRedeemMessage { fn to_proto(&self) -> SigningResult { let proto_msg = stride::stakeibc::MsgRedeemStake { - creator: self.creator.to_string(), + creator: self.creator.clone(), amount: self.amount.to_string(), - receiver: self.receiver.to_string(), + receiver: self.receiver.clone(), host_zone: self.host_zone.clone(), }; Ok(to_any(&proto_msg)) diff --git a/rust/tw_cosmos_sdk/src/transaction/mod.rs b/rust/tw_cosmos_sdk/src/transaction/mod.rs index 789fd2407b2..0d3d02812de 100644 --- a/rust/tw_cosmos_sdk/src/transaction/mod.rs +++ b/rust/tw_cosmos_sdk/src/transaction/mod.rs @@ -14,7 +14,7 @@ pub mod message; use message::CosmosMessageBox; /// At this moment, TW only supports the Direct signing mode. -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy)] pub enum SignMode { Direct, } diff --git a/rust/tw_cosmos_sdk/tests/sign.rs b/rust/tw_cosmos_sdk/tests/sign.rs index bceeb16a2b0..bba48c53f34 100644 --- a/rust/tw_cosmos_sdk/tests/sign.rs +++ b/rust/tw_cosmos_sdk/tests/sign.rs @@ -7,11 +7,12 @@ use std::borrow::Cow; use tw_coin_entry::test_utils::test_context::TestCoinContext; use tw_cosmos_sdk::context::StandardCosmosContext; +use tw_cosmos_sdk::modules::tx_builder::TxBuilder; use tw_cosmos_sdk::test_utils::sign_utils::{ test_sign_json, test_sign_json_error, test_sign_protobuf, test_sign_protobuf_error, TestErrorInput, TestInput, }; -use tw_encoding::hex::DecodeHex; +use tw_encoding::hex::{DecodeHex, ToHex}; use tw_keypair::tw::PublicKeyType; use tw_proto::Common::Proto::SigningError; use tw_proto::Cosmos::Proto; @@ -38,13 +39,6 @@ fn account_1366160_private_key() -> Cow<'static, [u8]> { .into() } -fn account_1290826_private_key() -> Cow<'static, [u8]> { - "c7764249cdf77f8f1d840fa8af431579e5e41cf1af937e1e23afa22f3f4f0ccc" - .decode_hex() - .unwrap() - .into() -} - fn make_fee(gas: u64, amount: Proto::Amount<'_>) -> Proto::Fee<'_> { Proto::Fee { amounts: vec![amount], @@ -310,294 +304,29 @@ fn test_sign_vote() { } #[test] -fn test_staking_compounding_authz() { - use Proto::mod_Message::mod_AuthGrant::OneOfgrant_type as ProtoGrantType; - use Proto::mod_Message::mod_StakeAuthorization::OneOfvalidators as ProtoValidatorsType; - +fn test_vote_payload() { let coin = TestCoinContext::default() .with_public_key_type(PublicKeyType::Secp256k1) .with_hrp("cosmos"); - let allow_list = Proto::mod_Message::mod_StakeAuthorization::Validators { - address: vec!["cosmosvaloper1gjtvly9lel6zskvwtvlg5vhwpu9c9waw7sxzwx".into()], - }; - let stake_authorization = Proto::mod_Message::StakeAuthorization { - authorization_type: Proto::mod_Message::AuthorizationType::UNDELEGATE, - validators: ProtoValidatorsType::allow_list(allow_list), - ..Proto::mod_Message::StakeAuthorization::default() - }; - let auth_grant = Proto::mod_Message::AuthGrant { - granter: "cosmos13k0q0l7lg2kr32kvt7ly236ppldy8v9dzwh3gd".into(), - grantee: "cosmos1fs7lu28hx5m9akm7rp0c2422cn8r2f7gurujhf".into(), - grant_type: ProtoGrantType::grant_stake(stake_authorization), - expiration: 1692309600, - }; - let input = Proto::SigningInput { - account_number: 1290826, - chain_id: "cosmoshub-4".into(), - sequence: 5, - fee: Some(make_fee(96681, make_amount("uatom", "2418"))), - private_key: account_1290826_private_key(), - messages: vec![make_message(MessageEnum::auth_grant(auth_grant))], - ..Proto::SigningInput::default() - }; - - // Successfully broadcasted https://www.mintscan.io/cosmos/txs/C4629BC7C88690518D8F448E7A8D239C9D63975B11F8E1CE2F95CC2ADA3CCF67 - test_sign_protobuf::(TestInput { - coin: &coin, - input: input.clone(), - // TODO the signature has been updated according to the serialization of the `StakeAuthorization` message. - // Previous: CvgBCvUBCh4vY29zbW9zLmF1dGh6LnYxYmV0YTEuTXNnR3JhbnQS0gEKLWNvc21vczEzazBxMGw3bGcya3IzMmt2dDdseTIzNnBwbGR5OHY5ZHp3aDNnZBItY29zbW9zMWZzN2x1MjhoeDVtOWFrbTdycDBjMjQyMmNuOHIyZjdndXJ1amhmGnIKaAoqL2Nvc21vcy5zdGFraW5nLnYxYmV0YTEuU3Rha2VBdXRob3JpemF0aW9uEjoSNgo0Y29zbW9zdmFsb3BlcjFnanR2bHk5bGVsNnpza3Z3dHZsZzV2aHdwdTljOXdhdzdzeHp3eCABEgYI4LD6pgYSZwpQCkYKHy9jb3Ntb3MuY3J5cHRvLnNlY3AyNTZrMS5QdWJLZXkSIwohA/fcQw1hCVUx904t+kCXTiiziaLIY8lyssu1ENfzaN1KEgQKAggBGAUSEwoNCgV1YXRvbRIEMjQxOBCp8wUaQIFyfuijGKf87Hz61ZqxasfLI1PZnNge4RDq/tRyB/tZI6p80iGRqHecoV6+84EQkc9GTlNRQOSlApRCsivT9XI= - tx: r#"{"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"CvgBCvUBCh4vY29zbW9zLmF1dGh6LnYxYmV0YTEuTXNnR3JhbnQS0gEKLWNvc21vczEzazBxMGw3bGcya3IzMmt2dDdseTIzNnBwbGR5OHY5ZHp3aDNnZBItY29zbW9zMWZzN2x1MjhoeDVtOWFrbTdycDBjMjQyMmNuOHIyZjdndXJ1amhmGnIKaAoqL2Nvc21vcy5zdGFraW5nLnYxYmV0YTEuU3Rha2VBdXRob3JpemF0aW9uEjogAhI2CjRjb3Ntb3N2YWxvcGVyMWdqdHZseTlsZWw2enNrdnd0dmxnNXZod3B1OWM5d2F3N3N4end4EgYI4LD6pgYSZwpQCkYKHy9jb3Ntb3MuY3J5cHRvLnNlY3AyNTZrMS5QdWJLZXkSIwohA/fcQw1hCVUx904t+kCXTiiziaLIY8lyssu1ENfzaN1KEgQKAggBGAUSEwoNCgV1YXRvbRIEMjQxOBCp8wUaQLmIllMlQEFVxjPbK2SNitPNm/7c9nmouNwucybOik6lJs+CR1+/hWhDX7ZKltFNk46c5xWvS/zWfAkCfX5qTr8="}"#, - // Previous: 81727ee8a318a7fcec7cfad59ab16ac7cb2353d99cd81ee110eafed47207fb5923aa7cd22191a8779ca15ebef3811091cf464e535140e4a5029442b22bd3f572 - signature: "b988965325404155c633db2b648d8ad3cd9bfedcf679a8b8dc2e7326ce8a4ea526cf82475fbf8568435fb64a96d14d938e9ce715af4bfcd67c09027d7e6a4ebf", - // Previous: gXJ+6KMYp/zsfPrVmrFqx8sjU9mc2B7hEOr+1HIH+1kjqnzSIZGod5yhXr7zgRCRz0ZOU1FA5KUClEKyK9P1cg== - signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"A/fcQw1hCVUx904t+kCXTiiziaLIY8lyssu1ENfzaN1K"},"signature":"uYiWUyVAQVXGM9srZI2K082b/tz2eai43C5zJs6KTqUmz4JHX7+FaENftkqW0U2TjpznFa9L/NZ8CQJ9fmpOvw=="}]"#, - }); - - // `AuthGrant` doesn't support JSON serialization and signing. - test_sign_json_error::(TestErrorInput { - coin: &coin, - input, - error: SigningError::Error_not_supported, - }); -} - -#[test] -fn test_staking_remove_compounding_authz() { - let coin = TestCoinContext::default() - .with_public_key_type(PublicKeyType::Secp256k1) - .with_hrp("cosmos"); - - let auth_revoke = Proto::mod_Message::AuthRevoke { - granter: "cosmos13k0q0l7lg2kr32kvt7ly236ppldy8v9dzwh3gd".into(), - grantee: "cosmos1fs7lu28hx5m9akm7rp0c2422cn8r2f7gurujhf".into(), - msg_type_url: "/cosmos.staking.v1beta1.MsgDelegate".into(), - }; - let input = Proto::SigningInput { - account_number: 1290826, - chain_id: "cosmoshub-4".into(), - sequence: 4, - fee: Some(make_fee(87735, make_amount("uatom", "2194"))), - private_key: account_1290826_private_key(), - messages: vec![make_message(MessageEnum::auth_revoke(auth_revoke))], - ..Proto::SigningInput::default() - }; - - // Successfully broadcasted: https://www.mintscan.io/cosmos/txs/E3218F634BB6A1BE256545EBE38275D5B02D41E88F504A43F97CD9CD2B624D44 - test_sign_protobuf::(TestInput { - coin: &coin, - input: input.clone(), - tx: r#"{"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"CqoBCqcBCh8vY29zbW9zLmF1dGh6LnYxYmV0YTEuTXNnUmV2b2tlEoMBCi1jb3Ntb3MxM2swcTBsN2xnMmtyMzJrdnQ3bHkyMzZwcGxkeTh2OWR6d2gzZ2QSLWNvc21vczFmczdsdTI4aHg1bTlha203cnAwYzI0MjJjbjhyMmY3Z3VydWpoZhojL2Nvc21vcy5zdGFraW5nLnYxYmV0YTEuTXNnRGVsZWdhdGUSZwpQCkYKHy9jb3Ntb3MuY3J5cHRvLnNlY3AyNTZrMS5QdWJLZXkSIwohA/fcQw1hCVUx904t+kCXTiiziaLIY8lyssu1ENfzaN1KEgQKAggBGAQSEwoNCgV1YXRvbRIEMjE5NBC3rQUaQI7K+W7MMBoD6FbFZxRBqs9VTjErztjWTy57+fvrLaTCIZ+eBs7CuaKqfUZdSN8otjubSHVTQID3k9DpPAX0yDo="}"#, - signature: "8ecaf96ecc301a03e856c5671441aacf554e312bced8d64f2e7bf9fbeb2da4c2219f9e06cec2b9a2aa7d465d48df28b63b9b4875534080f793d0e93c05f4c83a", - // Previous: gXJ+6KMYp/zsfPrVmrFqx8sjU9mc2B7hEOr+1HIH+1kjqnzSIZGod5yhXr7zgRCRz0ZOU1FA5KUClEKyK9P1cg== - signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"A/fcQw1hCVUx904t+kCXTiiziaLIY8lyssu1ENfzaN1K"},"signature":"jsr5bswwGgPoVsVnFEGqz1VOMSvO2NZPLnv5++stpMIhn54GzsK5oqp9Rl1I3yi2O5tIdVNAgPeT0Ok8BfTIOg=="}]"#, - }); - - // `AuthRevoke` doesn't support JSON serialization and signing. - test_sign_json_error::(TestErrorInput { - coin: &coin, - input, - error: SigningError::Error_not_supported, - }); -} - -#[test] -fn test_staking_delegate() { - let coin = TestCoinContext::default() - .with_public_key_type(PublicKeyType::Secp256k1) - .with_hrp("cosmos"); - - let delegate = Proto::mod_Message::Delegate { - delegator_address: "cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02".into(), - validator_address: "cosmosvaloper1zkupr83hrzkn3up5elktzcq3tuft8nxsmwdqgp".into(), - amount: Some(make_amount("muon", "10")), - ..Proto::mod_Message::Delegate::default() - }; - let input = Proto::SigningInput { - account_number: 1037, - chain_id: "gaia-13003".into(), - sequence: 7, - fee: Some(make_fee(101721, make_amount("muon", "1018"))), - private_key: account_1337_private_key(), - messages: vec![make_message(MessageEnum::stake_message(delegate))], - mode: Proto::BroadcastMode::ASYNC, - ..Proto::SigningInput::default() - }; - - test_sign_protobuf::(TestInput { - coin: &coin, - input: input.clone(), - tx: r#"{"mode":"BROADCAST_MODE_ASYNC","tx_bytes":"CpsBCpgBCiMvY29zbW9zLnN0YWtpbmcudjFiZXRhMS5Nc2dEZWxlZ2F0ZRJxCi1jb3Ntb3MxaHNrNmpyeXlxamZocDVkaGM1NXRjOWp0Y2t5Z3gwZXBoNmRkMDISNGNvc21vc3ZhbG9wZXIxemt1cHI4M2hyemtuM3VwNWVsa3R6Y3EzdHVmdDhueHNtd2RxZ3AaCgoEbXVvbhICMTASZgpQCkYKHy9jb3Ntb3MuY3J5cHRvLnNlY3AyNTZrMS5QdWJLZXkSIwohAlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3FEgQKAggBGAcSEgoMCgRtdW9uEgQxMDE4ENmaBhpA8O9Jm/kL6Za2I3poDs5vpMowYJgNvYCJBRU/vxAjs0lNZYsq40qpTbwOTbORjJA5UjQ6auc40v6uCFT4q4z+uA=="}"#, - signature: "f0ef499bf90be996b6237a680ece6fa4ca3060980dbd808905153fbf1023b3494d658b2ae34aa94dbc0e4db3918c903952343a6ae738d2feae0854f8ab8cfeb8", - signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"8O9Jm/kL6Za2I3poDs5vpMowYJgNvYCJBRU/vxAjs0lNZYsq40qpTbwOTbORjJA5UjQ6auc40v6uCFT4q4z+uA=="}]"#, - }); - - test_sign_json::(TestInput { - coin: &coin, - input, - tx: r#"{"mode":"async","tx":{"fee":{"amount":[{"amount":"1018","denom":"muon"}],"gas":"101721"},"memo":"","msg":[{"type":"cosmos-sdk/MsgDelegate","value":{"amount":{"amount":"10","denom":"muon"},"delegator_address":"cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02","validator_address":"cosmosvaloper1zkupr83hrzkn3up5elktzcq3tuft8nxsmwdqgp"}}],"signatures":[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"wIvfbCsLRCjzeXXoXTKfHLGXRbAAmUp0O134HVfVc6pfdVNJvvzISMHRUHgYcjsSiFlLyR32heia/yLgMDtIYQ=="}]}}"#, - signature: "c08bdf6c2b0b4428f37975e85d329f1cb19745b000994a743b5df81d57d573aa5f755349befcc848c1d1507818723b1288594bc91df685e89aff22e0303b4861", - signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"wIvfbCsLRCjzeXXoXTKfHLGXRbAAmUp0O134HVfVc6pfdVNJvvzISMHRUHgYcjsSiFlLyR32heia/yLgMDtIYQ=="}]"#, - }); -} - -#[test] -fn test_staking_undelegate() { - let coin = TestCoinContext::default() - .with_public_key_type(PublicKeyType::Secp256k1) - .with_hrp("cosmos"); - - let undelegate = Proto::mod_Message::Undelegate { - delegator_address: "cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02".into(), - validator_address: "cosmosvaloper1zkupr83hrzkn3up5elktzcq3tuft8nxsmwdqgp".into(), - amount: Some(make_amount("muon", "10")), - ..Proto::mod_Message::Undelegate::default() - }; - let input = Proto::SigningInput { - account_number: 1037, - chain_id: "gaia-13003".into(), - sequence: 7, - fee: Some(make_fee(101721, make_amount("muon", "1018"))), - private_key: account_1337_private_key(), - messages: vec![make_message(MessageEnum::unstake_message(undelegate))], - mode: Proto::BroadcastMode::SYNC, - ..Proto::SigningInput::default() - }; - - test_sign_protobuf::(TestInput { - coin: &coin, - input: input.clone(), - tx: r#"{"mode":"BROADCAST_MODE_SYNC","tx_bytes":"Cp0BCpoBCiUvY29zbW9zLnN0YWtpbmcudjFiZXRhMS5Nc2dVbmRlbGVnYXRlEnEKLWNvc21vczFoc2s2anJ5eXFqZmhwNWRoYzU1dGM5anRja3lneDBlcGg2ZGQwMhI0Y29zbW9zdmFsb3BlcjF6a3VwcjgzaHJ6a24zdXA1ZWxrdHpjcTN0dWZ0OG54c213ZHFncBoKCgRtdW9uEgIxMBJmClAKRgofL2Nvc21vcy5jcnlwdG8uc2VjcDI1NmsxLlB1YktleRIjCiECVyhuw/N9M1V7u6oACyd0SskCOqmWfK51oYHR/5H6ncUSBAoCCAEYBxISCgwKBG11b24SBDEwMTgQ2ZoGGkBhlxHFnjBERxLtjLbMCKXcrDctaSZ9djtWCa3ely1bpV6m+6aAFjpr8aEZH+q2AtjJSEdgpQRJxP+9/gQsRTnZ"}"#, - signature: "619711c59e30444712ed8cb6cc08a5dcac372d69267d763b5609adde972d5ba55ea6fba680163a6bf1a1191feab602d8c9484760a50449c4ffbdfe042c4539d9", - signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"YZcRxZ4wREcS7Yy2zAil3Kw3LWkmfXY7Vgmt3pctW6VepvumgBY6a/GhGR/qtgLYyUhHYKUEScT/vf4ELEU52Q=="}]"#, - }); - - test_sign_json::(TestInput { - coin: &coin, - input, - tx: r#"{"mode":"sync","tx":{"fee":{"amount":[{"amount":"1018","denom":"muon"}],"gas":"101721"},"memo":"","msg":[{"type":"cosmos-sdk/MsgUndelegate","value":{"amount":{"amount":"10","denom":"muon"},"delegator_address":"cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02","validator_address":"cosmosvaloper1zkupr83hrzkn3up5elktzcq3tuft8nxsmwdqgp"}}],"signatures":[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"j4WpUVohGIHa6/s0bCvuyjq1wtQGqbOtQCz92qPQjisTN44Tz++Ozx1lAP6F0M4+eTA03XerqQ8hZCeAfL/3nw=="}]}}"#, - signature: "8f85a9515a211881daebfb346c2beeca3ab5c2d406a9b3ad402cfddaa3d08e2b13378e13cfef8ecf1d6500fe85d0ce3e793034dd77aba90f216427807cbff79f", - signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"j4WpUVohGIHa6/s0bCvuyjq1wtQGqbOtQCz92qPQjisTN44Tz++Ozx1lAP6F0M4+eTA03XerqQ8hZCeAfL/3nw=="}]"#, - }); -} - -#[test] -fn test_staking_restake() { - let coin = TestCoinContext::default() - .with_public_key_type(PublicKeyType::Secp256k1) - .with_hrp("cosmos"); - - let redelegate = Proto::mod_Message::BeginRedelegate { - delegator_address: "cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02".into(), - validator_src_address: "cosmosvaloper1zkupr83hrzkn3up5elktzcq3tuft8nxsmwdqgp".into(), - validator_dst_address: "cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02".into(), - amount: Some(make_amount("muon", "10")), - ..Proto::mod_Message::BeginRedelegate::default() - }; - let input = Proto::SigningInput { - account_number: 1037, - chain_id: "gaia-13003".into(), - sequence: 7, - fee: Some(make_fee(101721, make_amount("muon", "1018"))), - private_key: account_1337_private_key(), - messages: vec![make_message(MessageEnum::restake_message(redelegate))], - ..Proto::SigningInput::default() - }; - - test_sign_protobuf::(TestInput { - coin: &coin, - input: input.clone(), - tx: r#"{"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"CtIBCs8BCiovY29zbW9zLnN0YWtpbmcudjFiZXRhMS5Nc2dCZWdpblJlZGVsZWdhdGUSoAEKLWNvc21vczFoc2s2anJ5eXFqZmhwNWRoYzU1dGM5anRja3lneDBlcGg2ZGQwMhI0Y29zbW9zdmFsb3BlcjF6a3VwcjgzaHJ6a24zdXA1ZWxrdHpjcTN0dWZ0OG54c213ZHFncBotY29zbW9zMWhzazZqcnl5cWpmaHA1ZGhjNTV0YzlqdGNreWd4MGVwaDZkZDAyIgoKBG11b24SAjEwEmYKUApGCh8vY29zbW9zLmNyeXB0by5zZWNwMjU2azEuUHViS2V5EiMKIQJXKG7D830zVXu7qgALJ3RKyQI6qZZ8rnWhgdH/kfqdxRIECgIIARgHEhIKDAoEbXVvbhIEMTAxOBDZmgYaQJ52qO5xdtBkNUeFeWrnqUXkngyHFKCXnOPPClyVI0HrULdp5jbwGra2RujEOn4BrbFCb3JFnpc2o1iuLXbKQxg="}"#, - signature: "9e76a8ee7176d064354785796ae7a945e49e0c8714a0979ce3cf0a5c952341eb50b769e636f01ab6b646e8c43a7e01adb1426f72459e9736a358ae2d76ca4318", - signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"nnao7nF20GQ1R4V5auepReSeDIcUoJec488KXJUjQetQt2nmNvAatrZG6MQ6fgGtsUJvckWelzajWK4tdspDGA=="}]"#, - }); - - test_sign_json::(TestInput { - coin: &coin, - input, - tx: r#"{"mode":"block","tx":{"fee":{"amount":[{"amount":"1018","denom":"muon"}],"gas":"101721"},"memo":"","msg":[{"type":"cosmos-sdk/MsgBeginRedelegate","value":{"amount":{"amount":"10","denom":"muon"},"delegator_address":"cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02","validator_dst_address":"cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02","validator_src_address":"cosmosvaloper1zkupr83hrzkn3up5elktzcq3tuft8nxsmwdqgp"}}],"signatures":[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"5k03Yb0loovvzagMCg4gjQJP2woriZVRcOZaXF1FSros6B1X4B8MEm3lpZwrWBJMEJVgyYA9ZaF6FLVI3WxQ2w=="}]}}"#, - signature: "e64d3761bd25a28befcda80c0a0e208d024fdb0a2b89955170e65a5c5d454aba2ce81d57e01f0c126de5a59c2b58124c109560c9803d65a17a14b548dd6c50db", - signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"5k03Yb0loovvzagMCg4gjQJP2woriZVRcOZaXF1FSros6B1X4B8MEm3lpZwrWBJMEJVgyYA9ZaF6FLVI3WxQ2w=="}]"#, - }); -} - -#[test] -fn test_staking_withdraw_rewards() { - let coin = TestCoinContext::default() - .with_public_key_type(PublicKeyType::Secp256k1) - .with_hrp("cosmos"); - - let withdraw = Proto::mod_Message::WithdrawDelegationReward { - delegator_address: "cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02".into(), - validator_address: "cosmosvaloper1zkupr83hrzkn3up5elktzcq3tuft8nxsmwdqgp".into(), - ..Proto::mod_Message::WithdrawDelegationReward::default() - }; - let input = Proto::SigningInput { - account_number: 1037, - chain_id: "gaia-13003".into(), - sequence: 7, - fee: Some(make_fee(101721, make_amount("muon", "1018"))), - private_key: account_1337_private_key(), - messages: vec![make_message(MessageEnum::withdraw_stake_reward_message( - withdraw, - ))], - ..Proto::SigningInput::default() - }; - - test_sign_protobuf::(TestInput { - coin: &coin, - input: input.clone(), - tx: r#"{"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"CqMBCqABCjcvY29zbW9zLmRpc3RyaWJ1dGlvbi52MWJldGExLk1zZ1dpdGhkcmF3RGVsZWdhdG9yUmV3YXJkEmUKLWNvc21vczFoc2s2anJ5eXFqZmhwNWRoYzU1dGM5anRja3lneDBlcGg2ZGQwMhI0Y29zbW9zdmFsb3BlcjF6a3VwcjgzaHJ6a24zdXA1ZWxrdHpjcTN0dWZ0OG54c213ZHFncBJmClAKRgofL2Nvc21vcy5jcnlwdG8uc2VjcDI1NmsxLlB1YktleRIjCiECVyhuw/N9M1V7u6oACyd0SskCOqmWfK51oYHR/5H6ncUSBAoCCAEYBxISCgwKBG11b24SBDEwMTgQ2ZoGGkBW1Cd+0pNfMPEVXQtqG1VIijDjZP2UOiDlvUF478axnxlF8PaOAsY0S5OdUE3Wz7+nu8YVmrLZQS/8mlqLaK05"}"#, - signature: "56d4277ed2935f30f1155d0b6a1b55488a30e364fd943a20e5bd4178efc6b19f1945f0f68e02c6344b939d504dd6cfbfa7bbc6159ab2d9412ffc9a5a8b68ad39", - signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"VtQnftKTXzDxFV0LahtVSIow42T9lDog5b1BeO/GsZ8ZRfD2jgLGNEuTnVBN1s+/p7vGFZqy2UEv/Jpai2itOQ=="}]"#, - }); - - test_sign_json::(TestInput { - coin: &coin, - input, - tx: r#"{"mode":"block","tx":{"fee":{"amount":[{"amount":"1018","denom":"muon"}],"gas":"101721"},"memo":"","msg":[{"type":"cosmos-sdk/MsgWithdrawDelegationReward","value":{"delegator_address":"cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02","validator_address":"cosmosvaloper1zkupr83hrzkn3up5elktzcq3tuft8nxsmwdqgp"}}],"signatures":[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"VG8NZzVvavlM+1qyK5dOSZwzEj8sLCkvTw5kh44Oco9GQxBf13FVC+s/I3HwiICqo4+o8jNMEDp3nx2C0tuY1g=="}]}}"#, - signature: "546f0d67356f6af94cfb5ab22b974e499c33123f2c2c292f4f0e64878e0e728f4643105fd771550beb3f2371f08880aaa38fa8f2334c103a779f1d82d2db98d6", - signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"VG8NZzVvavlM+1qyK5dOSZwzEj8sLCkvTw5kh44Oco9GQxBf13FVC+s/I3HwiICqo4+o8jNMEDp3nx2C0tuY1g=="}]"#, - }); -} - -#[test] -fn test_staking_set_withdraw_address() { - let coin = TestCoinContext::default() - .with_public_key_type(PublicKeyType::Secp256k1) - .with_hrp("cosmos"); - - let set_address = Proto::mod_Message::SetWithdrawAddress { - delegator_address: "cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02".into(), - withdraw_address: "cosmosvaloper1zkupr83hrzkn3up5elktzcq3tuft8nxsmwdqgp".into(), - ..Proto::mod_Message::SetWithdrawAddress::default() - }; - let input = Proto::SigningInput { - account_number: 1037, - chain_id: "gaia-13003".into(), - sequence: 7, - fee: Some(make_fee(101721, make_amount("muon", "1018"))), - private_key: account_1337_private_key(), - messages: vec![make_message(MessageEnum::set_withdraw_address_message( - set_address, - ))], - ..Proto::SigningInput::default() + let mut vote_msg = Proto::mod_Message::MsgVote { + proposal_id: 123, + voter: "cosmos1mry47pkga5tdswtluy0m8teslpalkdq07pswu4".into(), + option: Proto::mod_Message::VoteOption::_UNSPECIFIED, }; - test_sign_protobuf::(TestInput { - coin: &coin, - input: input.clone(), - tx: r#"{"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"Cp4BCpsBCjIvY29zbW9zLmRpc3RyaWJ1dGlvbi52MWJldGExLk1zZ1NldFdpdGhkcmF3QWRkcmVzcxJlCi1jb3Ntb3MxaHNrNmpyeXlxamZocDVkaGM1NXRjOWp0Y2t5Z3gwZXBoNmRkMDISNGNvc21vc3ZhbG9wZXIxemt1cHI4M2hyemtuM3VwNWVsa3R6Y3EzdHVmdDhueHNtd2RxZ3ASZgpQCkYKHy9jb3Ntb3MuY3J5cHRvLnNlY3AyNTZrMS5QdWJLZXkSIwohAlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3FEgQKAggBGAcSEgoMCgRtdW9uEgQxMDE4ENmaBhpAkm2TJLw4FcIwN5bkqVaGbmAgkTSHeYD8sUkIyJHLa89cPvThkFO/lKlxBMl2UAMs06hL6cYcl4Px+B6rpFdBpA=="}"#, - signature: "926d9324bc3815c2303796e4a956866e60209134877980fcb14908c891cb6bcf5c3ef4e19053bf94a97104c97650032cd3a84be9c61c9783f1f81eaba45741a4", - signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"km2TJLw4FcIwN5bkqVaGbmAgkTSHeYD8sUkIyJHLa89cPvThkFO/lKlxBMl2UAMs06hL6cYcl4Px+B6rpFdBpA=="}]"#, - }); - - test_sign_json::(TestInput { - coin: &coin, - input, - tx: r#"{"mode":"block","tx":{"fee":{"amount":[{"amount":"1018","denom":"muon"}],"gas":"101721"},"memo":"","msg":[{"type":"cosmos-sdk/MsgSetWithdrawAddress","value":{"delegator_address":"cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02","withdraw_address":"cosmosvaloper1zkupr83hrzkn3up5elktzcq3tuft8nxsmwdqgp"}}],"signatures":[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"Is+87DPQbtQmIyZASdEdb7hlZhA9ViGiOxREAi6xqs46B5ChxGtIwCGGiWFtr5f5mucsNYmWYgXeRbVxlPutog=="}]}}"#, - signature: "22cfbcec33d06ed42623264049d11d6fb86566103d5621a23b1444022eb1aace3a0790a1c46b48c0218689616daf97f99ae72c3589966205de45b57194fbada2", - signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"Is+87DPQbtQmIyZASdEdb7hlZhA9ViGiOxREAi6xqs46B5ChxGtIwCGGiWFtr5f5mucsNYmWYgXeRbVxlPutog=="}]"#, - }); + let tests = [ + (Proto::mod_Message::VoteOption::ABSTAIN, "087b122d636f736d6f73316d72793437706b67613574647377746c7579306d387465736c70616c6b6471303770737775341802"), + (Proto::mod_Message::VoteOption::NO, "087b122d636f736d6f73316d72793437706b67613574647377746c7579306d387465736c70616c6b6471303770737775341803"), + (Proto::mod_Message::VoteOption::NO_WITH_VETO, "087b122d636f736d6f73316d72793437706b67613574647377746c7579306d387465736c70616c6b6471303770737775341804"), + (Proto::mod_Message::VoteOption::_UNSPECIFIED, "087b122d636f736d6f73316d72793437706b67613574647377746c7579306d387465736c70616c6b647130377073777534"), + ]; + + for (option, expected) in tests { + vote_msg.option = option; + let payload = + TxBuilder::::vote_msg_from_proto(&coin, &vote_msg).unwrap(); + let actual = payload.to_proto().unwrap(); + assert_eq!(actual.value.to_hex(), expected); + } } diff --git a/rust/tw_cosmos_sdk/tests/sign_staking.rs b/rust/tw_cosmos_sdk/tests/sign_staking.rs index 6d4beb4617e..f7e46dc96f3 100644 --- a/rust/tw_cosmos_sdk/tests/sign_staking.rs +++ b/rust/tw_cosmos_sdk/tests/sign_staking.rs @@ -143,13 +143,14 @@ fn test_staking_delegate() { fee: Some(make_fee(101721, make_amount("muon", "1018"))), private_key: account_1337_private_key(), messages: vec![make_message(MessageEnum::stake_message(delegate))], + mode: Proto::BroadcastMode::ASYNC, ..Proto::SigningInput::default() }; test_sign_protobuf::(TestInput { coin: &coin, input: input.clone(), - tx: r#"{"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"CpsBCpgBCiMvY29zbW9zLnN0YWtpbmcudjFiZXRhMS5Nc2dEZWxlZ2F0ZRJxCi1jb3Ntb3MxaHNrNmpyeXlxamZocDVkaGM1NXRjOWp0Y2t5Z3gwZXBoNmRkMDISNGNvc21vc3ZhbG9wZXIxemt1cHI4M2hyemtuM3VwNWVsa3R6Y3EzdHVmdDhueHNtd2RxZ3AaCgoEbXVvbhICMTASZgpQCkYKHy9jb3Ntb3MuY3J5cHRvLnNlY3AyNTZrMS5QdWJLZXkSIwohAlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3FEgQKAggBGAcSEgoMCgRtdW9uEgQxMDE4ENmaBhpA8O9Jm/kL6Za2I3poDs5vpMowYJgNvYCJBRU/vxAjs0lNZYsq40qpTbwOTbORjJA5UjQ6auc40v6uCFT4q4z+uA=="}"#, + tx: r#"{"mode":"BROADCAST_MODE_ASYNC","tx_bytes":"CpsBCpgBCiMvY29zbW9zLnN0YWtpbmcudjFiZXRhMS5Nc2dEZWxlZ2F0ZRJxCi1jb3Ntb3MxaHNrNmpyeXlxamZocDVkaGM1NXRjOWp0Y2t5Z3gwZXBoNmRkMDISNGNvc21vc3ZhbG9wZXIxemt1cHI4M2hyemtuM3VwNWVsa3R6Y3EzdHVmdDhueHNtd2RxZ3AaCgoEbXVvbhICMTASZgpQCkYKHy9jb3Ntb3MuY3J5cHRvLnNlY3AyNTZrMS5QdWJLZXkSIwohAlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3FEgQKAggBGAcSEgoMCgRtdW9uEgQxMDE4ENmaBhpA8O9Jm/kL6Za2I3poDs5vpMowYJgNvYCJBRU/vxAjs0lNZYsq40qpTbwOTbORjJA5UjQ6auc40v6uCFT4q4z+uA=="}"#, signature: "f0ef499bf90be996b6237a680ece6fa4ca3060980dbd808905153fbf1023b3494d658b2ae34aa94dbc0e4db3918c903952343a6ae738d2feae0854f8ab8cfeb8", signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"8O9Jm/kL6Za2I3poDs5vpMowYJgNvYCJBRU/vxAjs0lNZYsq40qpTbwOTbORjJA5UjQ6auc40v6uCFT4q4z+uA=="}]"#, }); @@ -157,7 +158,7 @@ fn test_staking_delegate() { test_sign_json::(TestInput { coin: &coin, input, - tx: r#"{"mode":"block","tx":{"fee":{"amount":[{"amount":"1018","denom":"muon"}],"gas":"101721"},"memo":"","msg":[{"type":"cosmos-sdk/MsgDelegate","value":{"amount":{"amount":"10","denom":"muon"},"delegator_address":"cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02","validator_address":"cosmosvaloper1zkupr83hrzkn3up5elktzcq3tuft8nxsmwdqgp"}}],"signatures":[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"wIvfbCsLRCjzeXXoXTKfHLGXRbAAmUp0O134HVfVc6pfdVNJvvzISMHRUHgYcjsSiFlLyR32heia/yLgMDtIYQ=="}]}}"#, + tx: r#"{"mode":"async","tx":{"fee":{"amount":[{"amount":"1018","denom":"muon"}],"gas":"101721"},"memo":"","msg":[{"type":"cosmos-sdk/MsgDelegate","value":{"amount":{"amount":"10","denom":"muon"},"delegator_address":"cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02","validator_address":"cosmosvaloper1zkupr83hrzkn3up5elktzcq3tuft8nxsmwdqgp"}}],"signatures":[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"wIvfbCsLRCjzeXXoXTKfHLGXRbAAmUp0O134HVfVc6pfdVNJvvzISMHRUHgYcjsSiFlLyR32heia/yLgMDtIYQ=="}]}}"#, signature: "c08bdf6c2b0b4428f37975e85d329f1cb19745b000994a743b5df81d57d573aa5f755349befcc848c1d1507818723b1288594bc91df685e89aff22e0303b4861", signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"wIvfbCsLRCjzeXXoXTKfHLGXRbAAmUp0O134HVfVc6pfdVNJvvzISMHRUHgYcjsSiFlLyR32heia/yLgMDtIYQ=="}]"#, }); @@ -182,13 +183,14 @@ fn test_staking_undelegate() { fee: Some(make_fee(101721, make_amount("muon", "1018"))), private_key: account_1337_private_key(), messages: vec![make_message(MessageEnum::unstake_message(undelegate))], + mode: Proto::BroadcastMode::SYNC, ..Proto::SigningInput::default() }; test_sign_protobuf::(TestInput { coin: &coin, input: input.clone(), - tx: r#"{"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"Cp0BCpoBCiUvY29zbW9zLnN0YWtpbmcudjFiZXRhMS5Nc2dVbmRlbGVnYXRlEnEKLWNvc21vczFoc2s2anJ5eXFqZmhwNWRoYzU1dGM5anRja3lneDBlcGg2ZGQwMhI0Y29zbW9zdmFsb3BlcjF6a3VwcjgzaHJ6a24zdXA1ZWxrdHpjcTN0dWZ0OG54c213ZHFncBoKCgRtdW9uEgIxMBJmClAKRgofL2Nvc21vcy5jcnlwdG8uc2VjcDI1NmsxLlB1YktleRIjCiECVyhuw/N9M1V7u6oACyd0SskCOqmWfK51oYHR/5H6ncUSBAoCCAEYBxISCgwKBG11b24SBDEwMTgQ2ZoGGkBhlxHFnjBERxLtjLbMCKXcrDctaSZ9djtWCa3ely1bpV6m+6aAFjpr8aEZH+q2AtjJSEdgpQRJxP+9/gQsRTnZ"}"#, + tx: r#"{"mode":"BROADCAST_MODE_SYNC","tx_bytes":"Cp0BCpoBCiUvY29zbW9zLnN0YWtpbmcudjFiZXRhMS5Nc2dVbmRlbGVnYXRlEnEKLWNvc21vczFoc2s2anJ5eXFqZmhwNWRoYzU1dGM5anRja3lneDBlcGg2ZGQwMhI0Y29zbW9zdmFsb3BlcjF6a3VwcjgzaHJ6a24zdXA1ZWxrdHpjcTN0dWZ0OG54c213ZHFncBoKCgRtdW9uEgIxMBJmClAKRgofL2Nvc21vcy5jcnlwdG8uc2VjcDI1NmsxLlB1YktleRIjCiECVyhuw/N9M1V7u6oACyd0SskCOqmWfK51oYHR/5H6ncUSBAoCCAEYBxISCgwKBG11b24SBDEwMTgQ2ZoGGkBhlxHFnjBERxLtjLbMCKXcrDctaSZ9djtWCa3ely1bpV6m+6aAFjpr8aEZH+q2AtjJSEdgpQRJxP+9/gQsRTnZ"}"#, signature: "619711c59e30444712ed8cb6cc08a5dcac372d69267d763b5609adde972d5ba55ea6fba680163a6bf1a1191feab602d8c9484760a50449c4ffbdfe042c4539d9", signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"YZcRxZ4wREcS7Yy2zAil3Kw3LWkmfXY7Vgmt3pctW6VepvumgBY6a/GhGR/qtgLYyUhHYKUEScT/vf4ELEU52Q=="}]"#, }); @@ -196,7 +198,7 @@ fn test_staking_undelegate() { test_sign_json::(TestInput { coin: &coin, input, - tx: r#"{"mode":"block","tx":{"fee":{"amount":[{"amount":"1018","denom":"muon"}],"gas":"101721"},"memo":"","msg":[{"type":"cosmos-sdk/MsgUndelegate","value":{"amount":{"amount":"10","denom":"muon"},"delegator_address":"cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02","validator_address":"cosmosvaloper1zkupr83hrzkn3up5elktzcq3tuft8nxsmwdqgp"}}],"signatures":[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"j4WpUVohGIHa6/s0bCvuyjq1wtQGqbOtQCz92qPQjisTN44Tz++Ozx1lAP6F0M4+eTA03XerqQ8hZCeAfL/3nw=="}]}}"#, + tx: r#"{"mode":"sync","tx":{"fee":{"amount":[{"amount":"1018","denom":"muon"}],"gas":"101721"},"memo":"","msg":[{"type":"cosmos-sdk/MsgUndelegate","value":{"amount":{"amount":"10","denom":"muon"},"delegator_address":"cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02","validator_address":"cosmosvaloper1zkupr83hrzkn3up5elktzcq3tuft8nxsmwdqgp"}}],"signatures":[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"j4WpUVohGIHa6/s0bCvuyjq1wtQGqbOtQCz92qPQjisTN44Tz++Ozx1lAP6F0M4+eTA03XerqQ8hZCeAfL/3nw=="}]}}"#, signature: "8f85a9515a211881daebfb346c2beeca3ab5c2d406a9b3ad402cfddaa3d08e2b13378e13cfef8ecf1d6500fe85d0ce3e793034dd77aba90f216427807cbff79f", signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"j4WpUVohGIHa6/s0bCvuyjq1wtQGqbOtQCz92qPQjisTN44Tz++Ozx1lAP6F0M4+eTA03XerqQ8hZCeAfL/3nw=="}]"#, }); diff --git a/rust/tw_cosmos_sdk/tests/sign_stride.rs b/rust/tw_cosmos_sdk/tests/sign_stride.rs new file mode 100644 index 00000000000..a38706a35c4 --- /dev/null +++ b/rust/tw_cosmos_sdk/tests/sign_stride.rs @@ -0,0 +1,104 @@ +// Copyright © 2017-2023 Trust Wallet. +// +// This file is part of Trust. The full Trust copyright notice, including +// terms governing use, modification, and redistribution, is contained in the +// file LICENSE at the root of the source code distribution tree. + +use std::borrow::Cow; +use tw_coin_entry::test_utils::test_context::TestCoinContext; +use tw_cosmos_sdk::context::StandardCosmosContext; +use tw_cosmos_sdk::test_utils::proto_utils::{make_amount, make_fee, make_message}; +use tw_cosmos_sdk::test_utils::sign_utils::{ + test_sign_json_error, test_sign_protobuf, TestErrorInput, TestInput, +}; +use tw_encoding::hex::DecodeHex; +use tw_keypair::tw::PublicKeyType; +use tw_proto::Common::Proto::SigningError; +use tw_proto::Cosmos::Proto; +use tw_proto::Cosmos::Proto::mod_Message::OneOfmessage_oneof as MessageEnum; + +fn account_136412_private_key() -> Cow<'static, [u8]> { + "a498a9ee41af9bab5ef2a8be63d5c970135c3c109e70efc8c56c534e6636b433" + .decode_hex() + .unwrap() + .into() +} + +#[test] +fn test_stride_liquid_staking_stake() { + let coin = TestCoinContext::default() + .with_public_key_type(PublicKeyType::Secp256k1) + .with_hrp("stride"); + + let stake = Proto::mod_Message::MsgStrideLiquidStakingStake { + creator: "stride1mry47pkga5tdswtluy0m8teslpalkdq0a2sjge".into(), + amount: "100000".into(), + host_denom: "uatom".into(), + }; + let input = Proto::SigningInput { + account_number: 136412, + chain_id: "stride-1".into(), + sequence: 0, + fee: Some(make_fee(500000, make_amount("ustrd", "0"))), + private_key: account_136412_private_key(), + messages: vec![make_message(MessageEnum::msg_stride_liquid_staking_stake( + stake, + ))], + ..Proto::SigningInput::default() + }; + + // Successfully broadcasted: https://www.mintscan.io/stride/txs/48E51A2571D99453C4581B30CECA2A1156C0D1EBACCD3619729B5A35AD67CC94?height=3485243 + test_sign_protobuf::(TestInput { + coin: &coin, + input: input.clone(), + tx: r#"{"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"CmMKYQofL3N0cmlkZS5zdGFrZWliYy5Nc2dMaXF1aWRTdGFrZRI+Ci1zdHJpZGUxbXJ5NDdwa2dhNXRkc3d0bHV5MG04dGVzbHBhbGtkcTBhMnNqZ2USBjEwMDAwMBoFdWF0b20SYgpOCkYKHy9jb3Ntb3MuY3J5cHRvLnNlY3AyNTZrMS5QdWJLZXkSIwohAsv9teRyiTMiKU5gzwiD1D30MeEInSnstEep5tVQRarlEgQKAggBEhAKCgoFdXN0cmQSATAQoMIeGkCDaZHV5/Z3CAQC5DXkaHmF6OKUiS5XKDsl3ZnBaaVuJjlSWV2vA7MPwGbC17P6jbVJt58ZLcxIWFt76UO3y1ix"}"#, + signature: "836991d5e7f677080402e435e4687985e8e294892e57283b25dd99c169a56e263952595daf03b30fc066c2d7b3fa8db549b79f192dcc48585b7be943b7cb58b1", + signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"Asv9teRyiTMiKU5gzwiD1D30MeEInSnstEep5tVQRarl"},"signature":"g2mR1ef2dwgEAuQ15Gh5hejilIkuVyg7Jd2ZwWmlbiY5UlldrwOzD8Bmwtez+o21SbefGS3MSFhbe+lDt8tYsQ=="}]"#, + }); + + test_sign_json_error::(TestErrorInput { + coin: &coin, + input, + error: SigningError::Error_not_supported, + }); +} + +#[test] +fn test_stride_liquid_staking_redeem() { + let coin = TestCoinContext::default() + .with_public_key_type(PublicKeyType::Secp256k1) + .with_hrp("stride"); + + let redeem = Proto::mod_Message::MsgStrideLiquidStakingRedeem { + creator: "stride1mry47pkga5tdswtluy0m8teslpalkdq0a2sjge".into(), + amount: "40000".into(), + receiver: "cosmos1mry47pkga5tdswtluy0m8teslpalkdq07pswu4".into(), + host_zone: "cosmoshub-4".into(), + }; + let input = Proto::SigningInput { + account_number: 136412, + chain_id: "stride-1".into(), + sequence: 1, + fee: Some(make_fee(1000000, make_amount("ustrd", "0"))), + private_key: account_136412_private_key(), + messages: vec![make_message(MessageEnum::msg_stride_liquid_staking_redeem( + redeem, + ))], + ..Proto::SigningInput::default() + }; + + // Successfully broadcasted: https://www.mintscan.io/stride/txs/B3D3A92A2FFB92A480A4B547A4303E6932204972A965D687DB4FB6B4E16B2C42?height=3485343 + test_sign_protobuf::(TestInput { + coin: &coin, + input: input.clone(), + tx: r#"{"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"CpgBCpUBCh8vc3RyaWRlLnN0YWtlaWJjLk1zZ1JlZGVlbVN0YWtlEnIKLXN0cmlkZTFtcnk0N3BrZ2E1dGRzd3RsdXkwbTh0ZXNscGFsa2RxMGEyc2pnZRIFNDAwMDAaC2Nvc21vc2h1Yi00Ii1jb3Ntb3MxbXJ5NDdwa2dhNXRkc3d0bHV5MG04dGVzbHBhbGtkcTA3cHN3dTQSZApQCkYKHy9jb3Ntb3MuY3J5cHRvLnNlY3AyNTZrMS5QdWJLZXkSIwohAsv9teRyiTMiKU5gzwiD1D30MeEInSnstEep5tVQRarlEgQKAggBGAESEAoKCgV1c3RyZBIBMBDAhD0aQKf84TYoPqwnXw22r0dok2fYplUFu003TlIfpoT+wqTZF1lHPC+RTAoJob6x50CnfvGlgJFBEQYPD+Ccv659VVA="}"#, + signature: "a7fce136283eac275f0db6af47689367d8a65505bb4d374e521fa684fec2a4d91759473c2f914c0a09a1beb1e740a77ef1a580914111060f0fe09cbfae7d5550", + signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"Asv9teRyiTMiKU5gzwiD1D30MeEInSnstEep5tVQRarl"},"signature":"p/zhNig+rCdfDbavR2iTZ9imVQW7TTdOUh+mhP7CpNkXWUc8L5FMCgmhvrHnQKd+8aWAkUERBg8P4Jy/rn1VUA=="}]"#, + }); + + test_sign_json_error::(TestErrorInput { + coin: &coin, + input, + error: SigningError::Error_not_supported, + }); +}