Skip to content

Commit

Permalink
[Cosmos]: Refactor tests
Browse files Browse the repository at this point in the history
* Add tests for WASM Terra and standard WASM contract exec messages
* TODO continue adding tests for WASM exec messages
  • Loading branch information
satoshiotomakan committed Nov 10, 2023
1 parent 2a9371e commit e31495a
Show file tree
Hide file tree
Showing 7 changed files with 494 additions and 1 deletion.
8 changes: 8 additions & 0 deletions rust/tw_cosmos_sdk/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// 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.

pub mod sign_utils;
pub mod proto_utils;
26 changes: 26 additions & 0 deletions rust/tw_cosmos_sdk/src/test_utils/proto_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 tw_proto::Cosmos::Proto;
use tw_proto::Cosmos::Proto::mod_Message::OneOfmessage_oneof as MessageEnum;

pub fn make_fee(gas: u64, amount: Proto::Amount<'_>) -> Proto::Fee<'_> {
Proto::Fee {
amounts: vec![amount],
gas,
}
}

pub fn make_message<'a>(message_oneof: MessageEnum<'a>) -> Proto::Message<'a> {
Proto::Message { message_oneof }
}

pub fn make_amount<'a>(denom: &'a str, amount: &'a str) -> Proto::Amount<'a> {
Proto::Amount {
denom: denom.into(),
amount: amount.into(),
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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::{
use tw_cosmos_sdk::test_utils::sign_utils::{
test_sign_json, test_sign_json_error, test_sign_protobuf, test_sign_protobuf_error,
TestErrorInput, TestInput,
};
Expand Down
323 changes: 323 additions & 0 deletions rust/tw_cosmos_sdk/tests/sign_staking.rs

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions rust/tw_cosmos_sdk/tests/sign_terra_wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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, test_sign_protobuf, TestInput};
use tw_encoding::hex::DecodeHex;
use tw_keypair::tw::PublicKeyType;
use tw_number::U256;
use tw_proto::Cosmos::Proto;
use tw_proto::Cosmos::Proto::mod_Message::OneOfmessage_oneof as MessageEnum;

fn account_3407705_private_key() -> Cow<'static, [u8]> {
"cf08ee8493e6f6a53f9721b9045576e80f371c0e36d08fdaf78b27a7afd8e616"
.decode_hex()
.unwrap()
.into()
}

#[test]
fn test_terra_wasm_transfer() {
let coin = TestCoinContext::default()
.with_public_key_type(PublicKeyType::Secp256k1)
.with_hrp("terra");

let transfer = Proto::mod_Message::WasmTerraExecuteContractTransfer {
sender_address: "terra18wukp84dq227wu4mgh0jm6n9nlnj6rs82pp9wf".into(),
// ANC
contract_address: "terra14z56l0fp2lsf86zy3hty2z47ezkhnthtr9yq76".into(),
amount: U256::encode_be_compact(250000),
recipient_address: "terra1jlgaqy9nvn2hf5t2sra9ycz8s77wnf9l0kmgcp".into(),
};

let input = Proto::SigningInput {
account_number: 3407705,
chain_id: "columbus-5".into(),
sequence: 3,
fee: Some(make_fee(200000, make_amount("uluna", "3000"))),
private_key: account_3407705_private_key(),
messages: vec![make_message(
MessageEnum::wasm_terra_execute_contract_transfer_message(transfer),
)],
..Proto::SigningInput::default()
};

test_sign_protobuf::<StandardCosmosContext>(TestInput {
coin: &coin,
input: input.clone(),
tx: r#"{"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"CucBCuQBCiYvdGVycmEud2FzbS52MWJldGExLk1zZ0V4ZWN1dGVDb250cmFjdBK5AQosdGVycmExOHd1a3A4NGRxMjI3d3U0bWdoMGptNm45bmxuajZyczgycHA5d2YSLHRlcnJhMTR6NTZsMGZwMmxzZjg2enkzaHR5Mno0N2V6a2hudGh0cjl5cTc2Glt7InRyYW5zZmVyIjp7ImFtb3VudCI6IjI1MDAwMCIsInJlY2lwaWVudCI6InRlcnJhMWpsZ2FxeTludm4yaGY1dDJzcmE5eWN6OHM3N3duZjlsMGttZ2NwIn19EmcKUApGCh8vY29zbW9zLmNyeXB0by5zZWNwMjU2azEuUHViS2V5EiMKIQNwZjrHsPmJKW/rXOWfukpQ1+lOHOJW3/IlFFnKLNmsABIECgIIARgDEhMKDQoFdWx1bmESBDMwMDAQwJoMGkAaprIEMLPH2HmFdwFGoaipb2GIyhXt6ombz+WMnG2mORBI6gFt0M+IymYgzZz6w1SW52R922yafDnn7yXfutRw"}"#,
signature: "1aa6b20430b3c7d87985770146a1a8a96f6188ca15edea899bcfe58c9c6da6391048ea016dd0cf88ca6620cd9cfac35496e7647ddb6c9a7c39e7ef25dfbad470",
signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"A3BmOsew+Ykpb+tc5Z+6SlDX6U4c4lbf8iUUWcos2awA"},"signature":"GqayBDCzx9h5hXcBRqGoqW9hiMoV7eqJm8/ljJxtpjkQSOoBbdDPiMpmIM2c+sNUludkfdtsmnw55+8l37rUcA=="}]"#,
});

// `AuthGrant` doesn't support JSON serialization and signing.
test_sign_json::<StandardCosmosContext>(TestInput {
coin: &coin,
input,
tx: r#"{"mode":"block","tx":{"fee":{"amount":[{"amount":"3000","denom":"uluna"}],"gas":"200000"},"memo":"","msg":[{"type":"wasm/MsgExecuteContract","value":{"coins":[],"contract":"terra14z56l0fp2lsf86zy3hty2z47ezkhnthtr9yq76","execute_msg":{"transfer":{"amount":"250000","recipient":"terra1jlgaqy9nvn2hf5t2sra9ycz8s77wnf9l0kmgcp"}},"sender":"terra18wukp84dq227wu4mgh0jm6n9nlnj6rs82pp9wf"}}],"signatures":[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"A3BmOsew+Ykpb+tc5Z+6SlDX6U4c4lbf8iUUWcos2awA"},"signature":"UjjcalhMY7PenaJ3oWBIiO1QXhOMx0rBX6lXFmlc58ZHKsnLtM8EeN2CFRaPM9gdmCk+0yTwRpQb3TMQ7GfgzQ=="}]}}"#,
signature: "5238dc6a584c63b3de9da277a1604888ed505e138cc74ac15fa95716695ce7c6472ac9cbb4cf0478dd8215168f33d81d98293ed324f046941bdd3310ec67e0cd",
signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"A3BmOsew+Ykpb+tc5Z+6SlDX6U4c4lbf8iUUWcos2awA"},"signature":"UjjcalhMY7PenaJ3oWBIiO1QXhOMx0rBX6lXFmlc58ZHKsnLtM8EeN2CFRaPM9gdmCk+0yTwRpQb3TMQ7GfgzQ=="}]"#,
});
}
69 changes: 69 additions & 0 deletions rust/tw_cosmos_sdk/tests/sign_wasm_contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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, test_sign_protobuf, TestInput};
use tw_encoding::hex::DecodeHex;
use tw_keypair::tw::PublicKeyType;
use tw_proto::Cosmos::Proto;
use tw_proto::Cosmos::Proto::mod_Message::OneOfmessage_oneof as MessageEnum;

fn account_336_private_key() -> Cow<'static, [u8]> {
"37f0af5bc20adb6832d39368a15492cd1e9e0cc1556d4317a5f75f9ccdf525ee"
.decode_hex()
.unwrap()
.into()
}

#[test]
fn test_wasm_execute_contract() {
let coin = TestCoinContext::default()
.with_public_key_type(PublicKeyType::Secp256k1)
.with_hrp("neutron");

let execute_msg = r#"{"claim":{"address":"neutron19h42zjnls2tpmg6yylcg6nr56cjxcx35q6xt57", "proof":["404ae2093edcca979ccb6ae4a36689cebc9c2c6a2b00b106c5396b079bf6dcf5","282fee30a25a60904f54d4f74aee8fcf8dd2822799c43be733e18e15743d4ece","e10de4202fe6532329d0d463d9669f1b659920868b9ea87d6715bfd223a86a40","564b4122c6f98653153d8e09d5a5f659fa7ebea740aa6b689c94211f8a11cc4b"], "amount":"2000000"}}"#;
let contract = Proto::mod_Message::WasmExecuteContractGeneric {
sender_address: "neutron19h42zjnls2tpmg6yylcg6nr56cjxcx35q6xt57".into(),
// ANC
contract_address: "neutron1465d8udjudl6cd8kgdlh2s37p7q0cf9x7yveumqwqk6ng94qwnmq7n79qn"
.into(),
execute_msg: execute_msg.into(),
..Proto::mod_Message::WasmExecuteContractGeneric::default()
};

let input = Proto::SigningInput {
account_number: 336,
chain_id: "pion-1".into(),
sequence: 0,
fee: Some(make_fee(666666, make_amount("untrn", "1000"))),
private_key: account_336_private_key(),
messages: vec![make_message(MessageEnum::wasm_execute_contract_generic(
contract,
))],
..Proto::SigningInput::default()
};

// Successfully broadcasted: https://explorer.rs-testnet.polypore.xyz/pion-1/tx/28F25164B1E2556844C227819B1D5437960B7E91181B37460EC6792588FF7E4E
test_sign_protobuf::<StandardCosmosContext>(TestInput {
coin: &coin,
input: input.clone(),
tx: r#"{"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"CpQECpEECiQvY29zbXdhc20ud2FzbS52MS5Nc2dFeGVjdXRlQ29udHJhY3QS6AMKLm5ldXRyb24xOWg0MnpqbmxzMnRwbWc2eXlsY2c2bnI1NmNqeGN4MzVxNnh0NTcSQm5ldXRyb24xNDY1ZDh1ZGp1ZGw2Y2Q4a2dkbGgyczM3cDdxMGNmOXg3eXZldW1xd3FrNm5nOTRxd25tcTduNzlxbhrxAnsiY2xhaW0iOnsiYWRkcmVzcyI6Im5ldXRyb24xOWg0MnpqbmxzMnRwbWc2eXlsY2c2bnI1NmNqeGN4MzVxNnh0NTciLCAicHJvb2YiOlsiNDA0YWUyMDkzZWRjY2E5NzljY2I2YWU0YTM2Njg5Y2ViYzljMmM2YTJiMDBiMTA2YzUzOTZiMDc5YmY2ZGNmNSIsIjI4MmZlZTMwYTI1YTYwOTA0ZjU0ZDRmNzRhZWU4ZmNmOGRkMjgyMjc5OWM0M2JlNzMzZTE4ZTE1NzQzZDRlY2UiLCJlMTBkZTQyMDJmZTY1MzIzMjlkMGQ0NjNkOTY2OWYxYjY1OTkyMDg2OGI5ZWE4N2Q2NzE1YmZkMjIzYTg2YTQwIiwiNTY0YjQxMjJjNmY5ODY1MzE1M2Q4ZTA5ZDVhNWY2NTlmYTdlYmVhNzQwYWE2YjY4OWM5NDIxMWY4YTExY2M0YiJdLCAiYW1vdW50IjoiMjAwMDAwMCJ9fRJlCk4KRgofL2Nvc21vcy5jcnlwdG8uc2VjcDI1NmsxLlB1YktleRIjCiECqPwhojhpWpB3vDr8R+qyUnDkcK3BPxS35F8OrHPq5WwSBAoCCAESEwoNCgV1bnRybhIEMTAwMBCq2CgaQMIEXC8zyuuXWuIeX7dZBBzxMjmheOP1ONitBrVZdwmuQUgClmwhOdW0JwRe8CJ5NUKqtDYZjKFAPKGEWQ2veDs="}"#,
signature: "c2045c2f33caeb975ae21e5fb759041cf13239a178e3f538d8ad06b5597709ae414802966c2139d5b427045ef022793542aab436198ca1403ca184590daf783b",
signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"Aqj8IaI4aVqQd7w6/EfqslJw5HCtwT8Ut+RfDqxz6uVs"},"signature":"wgRcLzPK65da4h5ft1kEHPEyOaF44/U42K0GtVl3Ca5BSAKWbCE51bQnBF7wInk1Qqq0NhmMoUA8oYRZDa94Ow=="}]"#,
});

// `AuthGrant` doesn't support JSON serialization and signing.
test_sign_json::<StandardCosmosContext>(TestInput {
coin: &coin,
input,
tx: r#"{"mode":"block","tx":{"fee":{"amount":[{"amount":"1000","denom":"untrn"}],"gas":"666666"},"memo":"","msg":[{"type":"wasm/MsgExecuteContract","value":{"coins":[],"contract":"neutron1465d8udjudl6cd8kgdlh2s37p7q0cf9x7yveumqwqk6ng94qwnmq7n79qn","msg":"{\"claim\":{\"address\":\"neutron19h42zjnls2tpmg6yylcg6nr56cjxcx35q6xt57\", \"proof\":[\"404ae2093edcca979ccb6ae4a36689cebc9c2c6a2b00b106c5396b079bf6dcf5\",\"282fee30a25a60904f54d4f74aee8fcf8dd2822799c43be733e18e15743d4ece\",\"e10de4202fe6532329d0d463d9669f1b659920868b9ea87d6715bfd223a86a40\",\"564b4122c6f98653153d8e09d5a5f659fa7ebea740aa6b689c94211f8a11cc4b\"], \"amount\":\"2000000\"}}","sender":"neutron19h42zjnls2tpmg6yylcg6nr56cjxcx35q6xt57"}}],"signatures":[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"Aqj8IaI4aVqQd7w6/EfqslJw5HCtwT8Ut+RfDqxz6uVs"},"signature":"IYbqX5Mh8atLwMxFL2qvWG+18EXLXy7Z4gZYIOMFqCUZWhLjQ6D2zFDKqKYoLqJD8ZH7VyVDmRUTdDzN0zS+WA=="}]}}"#,
signature: "2186ea5f9321f1ab4bc0cc452f6aaf586fb5f045cb5f2ed9e2065820e305a825195a12e343a0f6cc50caa8a6282ea243f191fb572543991513743ccdd334be58",
signature_json: r#"[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"Aqj8IaI4aVqQd7w6/EfqslJw5HCtwT8Ut+RfDqxz6uVs"},"signature":"IYbqX5Mh8atLwMxFL2qvWG+18EXLXy7Z4gZYIOMFqCUZWhLjQ6D2zFDKqKYoLqJD8ZH7VyVDmRUTdDzN0zS+WA=="}]"#,
});
}

0 comments on commit e31495a

Please sign in to comment.