Skip to content

Commit

Permalink
[Cosmos]: Fix C++ tests
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan committed Nov 15, 2023
1 parent ec8eb78 commit 5adcb13
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn test_sign_native_evmos_tx_protobuf() {
address: vec!["evmosvaloper1umk407eed7af6anvut6llg2zevnf0dn0feqqny".into()],
};
let stake_authorization = Proto::mod_Message::StakeAuthorization {
authorization_type: Proto::mod_Message::AuthorizationType::UNDELEGATE,
authorization_type: Proto::mod_Message::AuthorizationType::DELEGATE,
validators: ProtoValidatorsType::allow_list(allow_list),
..Proto::mod_Message::StakeAuthorization::default()
};
Expand Down Expand Up @@ -117,6 +117,6 @@ fn test_sign_native_evmos_tx_protobuf() {
// Previous: CvUBCvIBCh4vY29zbW9zLmF1dGh6LnYxYmV0YTEuTXNnR3JhbnQSzwEKLGV2bW9zMTJtOWdyZ2FzNjB5azBrdWx0MDc2dnhuc3Jxejh4cGp5OXJwZjNlEixldm1vczE4ZnpxNG5hYzI4Z2ZtYTZncWZ2a3B3cmdwbTVjdGFyMno5bXhmMxpxCmcKKi9jb3Ntb3Muc3Rha2luZy52MWJldGExLlN0YWtlQXV0aG9yaXphdGlvbhI5EjUKM2V2bW9zdmFsb3BlcjF1bWs0MDdlZWQ3YWY2YW52dXQ2bGxnMnpldm5mMGRuMGZlcXFueSABEgYI4LD6pgYSfQpZCk8KKC9ldGhlcm1pbnQuY3J5cHRvLnYxLmV0aHNlY3AyNTZrMS5QdWJLZXkSIwohA4B2WHbj6sH/GWE7z/YW5PRnXYFGaGRAov7gZZI2Fv2nEgQKAggBGAMSIAoaCgZhZXZtb3MSEDQ1MjE0NzUwMDAwMDAwMDAQ+4QLGkAm17CZgB7m+CPVlITnrHosklMTL9zrUeGRs8FL8N0GcRami9zdJ+e3xuXOtJmwP7G6QNh85CRYjFj8a8lpmmJM
assert_eq!(
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yNTZrMS5QdWJLZXkSIwohA4B2WHbj6sH/GWE7z/YW5PRnXYFGaGRAov7gZZI2Fv2nEgQKAggBGAMSIAoaCgZhZXZtb3MSEDQ1MjE0NzUwMDAwMDAwMDAQ+4QLGkBXaTo3nk5EMFW9Euheez5ADx2bWo7XisNJ5vuGj1fKXh6CGNJGfJj/q1XUkBzaCvPNg+EcFHgtJdVSyF4cJZTg"}"#
);
}
24 changes: 18 additions & 6 deletions rust/tw_misc/src/test_utils/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,35 @@
// file LICENSE at the root of the source code distribution tree.

use serde_json::Value as Json;
use std::borrow::Cow;

pub trait ToJson {
fn to_json(&self) -> Json;
}

impl<T> ToJson for T
where
T: AsRef<str>,
{
impl ToJson for Json {
#[track_caller]
fn to_json(&self) -> Json {
serde_json::from_str(self.as_ref()).expect("Error on deserializing JSON from string")
self.clone()
}
}

impl<'a> ToJson for Cow<'a, str> {
#[track_caller]
fn to_json(&self) -> Json {
self.as_ref().to_json()
}
}

impl<'a> ToJson for &'a str {
#[track_caller]
fn to_json(&self) -> Json {
serde_json::from_str(self).expect("Error on deserializing JSON from string")
}
}

#[macro_export]
macro_rules! assert_json {
macro_rules! assert_eq_json {
($left:expr, $right:expr) => {{
use $crate::test_utils::json::ToJson;

Expand Down
6 changes: 1 addition & 5 deletions tests/chains/Cosmos/TransactionCompilerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,6 @@ TEST(CosmosCompiler, CompileWithSignatures) {
"0a31f6cd50f1a5c514929ba68a977e222a7df2dc11e8470e93118cc3545e6b37");

signature = Base64::decode("tTyOrburrHEHa14qiw78e9StoZyyGmoku98IxYrWCmtN8Qo5mTeKa0BKKDfgG4LmmNdwYcrXtqQQ7F4dL3c26g==");
{
auto result = TW::anySignJSON(coin, jsonPreImage, privateKey.bytes);
EXPECT_EQ(result, "{\"mode\":\"block\",\"tx\":{\"fee\":{\"amount\":[],\"gas\":\"0\"},\"memo\":\"\",\"msg\":[],\"signatures\":[{\"pub_key\":{\"type\":\"tendermint/PubKeySecp256k1\",\"value\":\"AuzvXOQ3owLGf5VGjeSzHzbpEfRn1+alK0HB4T4dVjZJ\"},\"signature\":\"tTyOrburrHEHa14qiw78e9StoZyyGmoku98IxYrWCmtN8Qo5mTeKa0BKKDfgG4LmmNdwYcrXtqQQ7F4dL3c26g==\"}]}}");
}

{ // JSON
const Data outputData = TransactionCompiler::compileWithSignatures(
Expand All @@ -180,6 +176,6 @@ TEST(CosmosCompiler, CompileWithSignatures) {
ASSERT_TRUE(output.ParseFromArray(outputData.data(), (int)outputData.size()));

EXPECT_EQ(output.error(), Common::Proto::OK);
EXPECT_EQ(hex(output.serialized()), "7b226d6f6465223a22626c6f636b222c227478223a7b22666565223a7b22616d6f756e74223a5b7b22616d6f756e74223a2231303030222c2264656e6f6d223a227561746f6d227d5d2c22676173223a22323030303030227d2c226d656d6f223a22222c226d7367223a5b7b2274797065223a22636f736d6f732d73646b2f4d736753656e64222c2276616c7565223a7b22616d6f756e74223a5b7b22616d6f756e74223a22343030303030222c2264656e6f6d223a227561746f6d227d5d2c2266726f6d5f61646472657373223a22636f736d6f73316d6b793639636e38656b74777930383435766563397570736470686b7478743033676b776c78222c22746f5f61646472657373223a22636f736d6f733138733068646e736c6c6763636c7765753961796d77346e676b7472326b30726b7967647a6470227d7d5d2c227369676e617475726573223a5b7b227075625f6b6579223a7b2274797065223a2274656e6465726d696e742f5075624b6579536563703235366b31222c2276616c7565223a2241757a76584f51336f774c47663556476a65537a487a62704566526e312b616c4b30484234543464566a5a4a227d2c227369676e6174757265223a227454794f72627572724845486131347169773738653953746f5a7979476d6f6b7539384978597257436d744e38516f356d54654b6130424b4b44666747344c6d6d4e64775963725874715151374634644c33633236673d3d227d5d7d7d");
EXPECT_EQ(hex(output.json()), "7b226d6f6465223a22626c6f636b222c227478223a7b22666565223a7b22616d6f756e74223a5b7b22616d6f756e74223a2231303030222c2264656e6f6d223a227561746f6d227d5d2c22676173223a22323030303030227d2c226d656d6f223a22222c226d7367223a5b7b2274797065223a22636f736d6f732d73646b2f4d736753656e64222c2276616c7565223a7b22616d6f756e74223a5b7b22616d6f756e74223a22343030303030222c2264656e6f6d223a227561746f6d227d5d2c2266726f6d5f61646472657373223a22636f736d6f73316d6b793639636e38656b74777930383435766563397570736470686b7478743033676b776c78222c22746f5f61646472657373223a22636f736d6f733138733068646e736c6c6763636c7765753961796d77346e676b7472326b30726b7967647a6470227d7d5d2c227369676e617475726573223a5b7b227075625f6b6579223a7b2274797065223a2274656e6465726d696e742f5075624b6579536563703235366b31222c2276616c7565223a2241757a76584f51336f774c47663556476a65537a487a62704566526e312b616c4b30484234543464566a5a4a227d2c227369676e6174757265223a227454794f72627572724845486131347169773738653953746f5a7979476d6f6b7539384978597257436d744e38516f356d54654b6130424b4b44666747344c6d6d4e64775963725874715151374634644c33633236673d3d227d5d7d7d");
}
}
11 changes: 7 additions & 4 deletions tests/chains/Evmos/SignerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "Cosmos/Signer.h"
#include "TestUtilities.h"

#include <TrustWalletCore/TWAnySigner.h>
#include <nlohmann/json.hpp>

#include <gtest/gtest.h>
Expand Down Expand Up @@ -46,7 +47,8 @@ TEST(EvmosSigner, SignTxJsonEthermintKeyType) {
auto privateKey = parse_hex("80e81ea269e66a0a05b11236df7919fb7fbeedba87452d667489d7403a02f005");
input.set_private_key(privateKey.data(), privateKey.size());

auto output = Signer::sign(input, TWCoinTypeNativeEvmos);
auto output = Proto::SigningOutput();
ANY_SIGN(input, TWCoinTypeNativeEvmos);
auto anotherExpectedJson =R"(
{
"mode":"block",
Expand All @@ -62,7 +64,7 @@ TEST(EvmosSigner, SignTxJsonEthermintKeyType) {
"pub_key":
{
"type":"ethermint/PubKeyEthSecp256k1",
"value":"BFcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3FWj3JBJiUsav6oXsWRf05KLv9Yt53nXEmm6fjB8Zl0wI="
"value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"
},
"signature":"1hMFtRqKjB8tiuyHYVYZundPdomebIIvHLC1gj9uXtFc+iO3UAHBysBjFB4brd9AD5yriS3uUDTAqqfg6fNGNg=="
}
Expand Down Expand Up @@ -115,12 +117,13 @@ TEST(EvmosSigner, CompoundingAuthz) {
auto privateKey = parse_hex("79bcbded1a5678ab34e6d9db9ad78e4e480e7b22723cc5fbf59e843732e1a8e5");
input.set_private_key(privateKey.data(), privateKey.size());

auto output = Signer::sign(input, TWCoinTypeNativeEvmos);
auto output = Proto::SigningOutput();
ANY_SIGN(input, TWCoinTypeNativeEvmos);
// TODO the signature has been updated according to the serialization of the `StakeAuthorization` message.
// Previous: CvUBCvIBCh4vY29zbW9zLmF1dGh6LnYxYmV0YTEuTXNnR3JhbnQSzwEKLGV2bW9zMTJtOWdyZ2FzNjB5azBrdWx0MDc2dnhuc3Jxejh4cGp5OXJwZjNlEixldm1vczE4ZnpxNG5hYzI4Z2ZtYTZncWZ2a3B3cmdwbTVjdGFyMno5bXhmMxpxCmcKKi9jb3Ntb3Muc3Rha2luZy52MWJldGExLlN0YWtlQXV0aG9yaXphdGlvbhI5EjUKM2V2bW9zdmFsb3BlcjF1bWs0MDdlZWQ3YWY2YW52dXQ2bGxnMnpldm5mMGRuMGZlcXFueSABEgYI4LD6pgYSfQpZCk8KKC9ldGhlcm1pbnQuY3J5cHRvLnYxLmV0aHNlY3AyNTZrMS5QdWJLZXkSIwohA4B2WHbj6sH/GWE7z/YW5PRnXYFGaGRAov7gZZI2Fv2nEgQKAggBGAMSIAoaCgZhZXZtb3MSEDQ1MjE0NzUwMDAwMDAwMDAQ+4QLGkAm17CZgB7m+CPVlITnrHosklMTL9zrUeGRs8FL8N0GcRami9zdJ+e3xuXOtJmwP7G6QNh85CRYjFj8a8lpmmJM
auto expected = R"(
{
"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"CvUBCvIBCh4vY29zbW9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QKAggBGAMSIAoaCgZhZXZtb3MSEDQ1MjE0NzUwMDAwMDAwMDAQ+4QLGkBXaTo3nk5EMFW9Euheez5ADx2bWo7XisNJ5vuGj1fKXh6CGNJGfJj/q1XUkBzaCvPNg+EcFHgtJdVSyF4cJZTg"
})";
assertJSONEqual(output.serialized(), expected);
}
Expand Down
3 changes: 1 addition & 2 deletions tests/chains/Evmos/TransactionCompilerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ TEST(EvmosCompiler, CompileWithSignatures) {

EXPECT_EQ(output.error(), Common::Proto::OK);
EXPECT_EQ(output.serialized(), expectedTx);
EXPECT_EQ(output.signature(), "");
EXPECT_EQ(hex(output.signature()), "");
EXPECT_EQ(hex(output.signature()), hex(signature));
}
}

Expand Down

0 comments on commit 5adcb13

Please sign in to comment.