Skip to content

Commit

Permalink
fix(evmos): Fix signing, prehash, compile for Native evmos
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan committed Sep 1, 2023
1 parent 9deedbb commit a636283
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 8 deletions.
2 changes: 1 addition & 1 deletion registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -3892,7 +3892,7 @@
}
],
"curve": "secp256k1",
"publicKeyType": "secp256k1Extended",
"publicKeyType": "secp256k1",
"hrp": "evmos",
"addressHasher": "keccak256",
"explorer": {
Expand Down
5 changes: 1 addition & 4 deletions src/Cosmos/Signer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@ Proto::SigningOutput Signer::sign(const Proto::SigningInput& input, TWCoinType c
}

std::string Signer::signaturePreimage(const Proto::SigningInput& input, const Data& publicKey, TWCoinType coin) const {
auto isEvmCosmosChain = [coin]() {
return coin == TWCoinTypeNativeInjective || coin == TWCoinTypeNativeEvmos || coin == TWCoinTypeNativeCanto;
};
switch (input.signing_mode()) {
case Proto::JSON:
return Json::signaturePreimageJSON(input).dump();

case Proto::Protobuf:
default:
auto pbk = isEvmCosmosChain() ? PublicKey(publicKey, TWPublicKeyTypeSECP256k1Extended) : PublicKey(publicKey, TWPublicKeyTypeSECP256k1);
auto pbk = PublicKey(publicKey, TWCoinTypePublicKeyType(coin));
return Protobuf::signaturePreimageProto(input, pbk, coin);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Greenfield/ProtobufSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ static SigningResult<Any> convertMessage(const Proto::Message& msg) {
any.PackFrom(msgTransferOut, ProtobufAnyNamespacePrefix);
break;
}
default: {
return SigningResult<Any>::failure(Common::Proto::SigningError::Error_invalid_params);
}
}

return SigningResult<Any>::success(std::move(any));
Expand Down
2 changes: 1 addition & 1 deletion swift/Tests/Blockchains/EvmosTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class EvmosTests: XCTestCase {
let output: CosmosSigningOutput = AnySigner.sign(input: input, coin: .nativeEvmos)
// https://www.mintscan.io/evmos/txs/B05D2047086B158665EC552879270AEF40AEAAFEE7D275B63E9674E3CC4C4E55
let expected = """
{"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"CpoBCpcBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEncKLGV2bW9zMXJrMzlkazN3ZmY1bnBzN2VtdWh2M250a24zbnN6NnoyZXJxZnIwEixldm1vczEwazlscnJydWFwOW51OTZteHd3eWUyZjZhNXdhemVoMzNrcTY3ehoZCgZhZXZtb3MSDzIwMDAwMDAwMDAwMDAwMBKbAQp3Cm8KKC9ldGhlcm1pbnQuY3J5cHRvLnYxLmV0aHNlY3AyNTZrMS5QdWJLZXkSQwpBBJR1yfoj7Gk2Z7qnbE2mm0nMz98FjE3LJ7pnz7yQgtntkHR4ZWCqaYsZu5cpUmscdZNPPUp4975xnkOGt0mzYxASBAoCCAESIAoaCgZhZXZtb3MSEDE0MDAwMDAwMDAwMDAwMDAQ4MUIGkDBSFkYlIx1/6ZvRtEHyq6r0EEFDb/5T2Cb9zVYWtYZE1e7YteG2GuYMX546PvBQ7CYAFZEkr+rU6okC0nAT0UK"}
{"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"CpoBCpcBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEncKLGV2bW9zMXJrMzlkazN3ZmY1bnBzN2VtdWh2M250a24zbnN6NnoyZXJxZnIwEixldm1vczEwazlscnJydWFwOW51OTZteHd3eWUyZjZhNXdhemVoMzNrcTY3ehoZCgZhZXZtb3MSDzIwMDAwMDAwMDAwMDAwMBJ7ClcKTwooL2V0aGVybWludC5jcnlwdG8udjEuZXRoc2VjcDI1NmsxLlB1YktleRIjCiEClHXJ+iPsaTZnuqdsTaabSczP3wWMTcsnumfPvJCC2e0SBAoCCAESIAoaCgZhZXZtb3MSEDE0MDAwMDAwMDAwMDAwMDAQ4MUIGkAz9vh1EutbLrLZmRA4eK72bA6bhfMX0YnhtRl5jeaL3AYmk0qdrwG9XzzleBsZ++IokJIk47cgOOyvEjl92Jhj"}
"""
XCTAssertJSONEqual(output.serialized, expected)
XCTAssertEqual(output.errorMessage, "")
Expand Down
2 changes: 1 addition & 1 deletion tests/chains/Evmos/SignerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ TEST(EvmosSigner, CompoundingAuthz) {
auto output = Signer::sign(input, TWCoinTypeNativeEvmos);
auto expected = R"(
{
"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"CvUBCvIBCh4vY29zbW9zLmF1dGh6LnYxYmV0YTEuTXNnR3JhbnQSzwEKLGV2bW9zMTJtOWdyZ2FzNjB5azBrdWx0MDc2dnhuc3Jxejh4cGp5OXJwZjNlEixldm1vczE4ZnpxNG5hYzI4Z2ZtYTZncWZ2a3B3cmdwbTVjdGFyMno5bXhmMxpxCmcKKi9jb3Ntb3Muc3Rha2luZy52MWJldGExLlN0YWtlQXV0aG9yaXphdGlvbhI5EjUKM2V2bW9zdmFsb3BlcjF1bWs0MDdlZWQ3YWY2YW52dXQ2bGxnMnpldm5mMGRuMGZlcXFueSABEgYI4LD6pgYSnQEKeQpvCigvZXRoZXJtaW50LmNyeXB0by52MS5ldGhzZWNwMjU2azEuUHViS2V5EkMKQQSAdlh24+rB/xlhO8/2FuT0Z12BRmhkQKL+4GWSNhb9p0uO6cUkpw8dpkYb5Wx+YsQgPvyUNSmiaO75siYg72ylEgQKAggBGAMSIAoaCgZhZXZtb3MSEDQ1MjE0NzUwMDAwMDAwMDAQ+4QLGkArFy2yWnZbP9aqxyx9KN8xlTVyWT5jolnrY5fbMtXeijVOGZIrFCtEg+xgjv6XpMTKK9A3cMMMcBAcuv2S+nN8"
"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"CvUBCvIBCh4vY29zbW9zLmF1dGh6LnYxYmV0YTEuTXNnR3JhbnQSzwEKLGV2bW9zMTJtOWdyZ2FzNjB5azBrdWx0MDc2dnhuc3Jxejh4cGp5OXJwZjNlEixldm1vczE4ZnpxNG5hYzI4Z2ZtYTZncWZ2a3B3cmdwbTVjdGFyMno5bXhmMxpxCmcKKi9jb3Ntb3Muc3Rha2luZy52MWJldGExLlN0YWtlQXV0aG9yaXphdGlvbhI5EjUKM2V2bW9zdmFsb3BlcjF1bWs0MDdlZWQ3YWY2YW52dXQ2bGxnMnpldm5mMGRuMGZlcXFueSABEgYI4LD6pgYSfQpZCk8KKC9ldGhlcm1pbnQuY3J5cHRvLnYxLmV0aHNlY3AyNTZrMS5QdWJLZXkSIwohA4B2WHbj6sH/GWE7z/YW5PRnXYFGaGRAov7gZZI2Fv2nEgQKAggBGAMSIAoaCgZhZXZtb3MSEDQ1MjE0NzUwMDAwMDAwMDAQ+4QLGkAm17CZgB7m+CPVlITnrHosklMTL9zrUeGRs8FL8N0GcRami9zdJ+e3xuXOtJmwP7G6QNh85CRYjFj8a8lpmmJM"
})";
assertJSONEqual(output.serialized(), expected);
}
Expand Down
103 changes: 103 additions & 0 deletions tests/chains/Evmos/TransactionCompilerTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// 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.

#include "Base64.h"
#include "Cosmos/Signer.h"
#include "HexCoding.h"
#include "proto/Cosmos.pb.h"
#include "proto/TransactionCompiler.pb.h"
#include "TrustWalletCore/TWAnySigner.h"
#include "TestUtilities.h"
#include "TransactionCompiler.h"

namespace TW::Cosmos::evmos::tests {

TEST(EvmosCompiler, CompileWithSignatures) {
// Successfully broadcasted: https://www.mintscan.io/evmos/transactions/02105B186FCA473C9F467B2D3BF487F6CE5DB26EE54BCD1667DDB7A2DA0E2489

const auto coin = TWCoinTypeNativeEvmos;
TW::Cosmos::Proto::SigningInput input;

PrivateKey privateKey =
PrivateKey(parse_hex("727513ec3c54eb6fae24f2ff756bbc4c89b82945c6538bbd173613ae3de719d3"));
input.set_private_key(privateKey.bytes.data(), privateKey.bytes.size());

/// Step 1: Prepare transaction input (protobuf)
input.set_account_number(106619981);
input.set_chain_id("evmos_9001-2");
input.set_memo("");
input.set_sequence(0);

PublicKey publicKey = privateKey.getPublicKey(TWCoinTypePublicKeyType(coin));
const auto pubKeyBz = publicKey.bytes;
ASSERT_EQ(hex(pubKeyBz), "02088ac2919987d927368cb2be2ade44cd0ed3616745a9699cae264b3fc5a7c360");
input.set_public_key(pubKeyBz.data(), pubKeyBz.size());

auto msg = input.add_messages();
auto& message = *msg->mutable_send_coins_message();
message.set_from_address("evmos1d0jkrsd09c7pule43y3ylrul43lwwcqa7vpy0g");
message.set_to_address("evmos17dh3frt0m6kdd3m9lr6e6sr5zz0rz8cvxd7u5t");
auto amountOfTx = message.add_amounts();
amountOfTx->set_denom("aevmos");
amountOfTx->set_amount("10000000000000000");

auto& fee = *input.mutable_fee();
fee.set_gas(137840);
auto amountOfFee = fee.add_amounts();
amountOfFee->set_denom("aevmos");
amountOfFee->set_amount("5513600000000000");

/// Step 2: Obtain protobuf preimage hash
input.set_signing_mode(TW::Cosmos::Proto::Protobuf);
auto protoInputString = input.SerializeAsString();
auto protoInputData = TW::Data(protoInputString.begin(), protoInputString.end());

const auto preImageHashData = TransactionCompiler::preImageHashes(coin, protoInputData);
auto preSigningOutput = TW::TxCompiler::Proto::PreSigningOutput();
ASSERT_TRUE(
preSigningOutput.ParseFromArray(preImageHashData.data(), (int)preImageHashData.size()));
ASSERT_EQ(preSigningOutput.error(), Common::Proto::OK);
auto preImage = preSigningOutput.data();
auto preImageHash = preSigningOutput.data_hash();

EXPECT_EQ(
hex(preImage),
"0a9c010a99010a1c2f636f736d6f732e62616e6b2e763162657461312e4d736753656e6412790a2c65766d6f733164306a6b7273643039633770756c6534337933796c72756c34336c7777637161377670793067122c65766d6f733137646833667274306d366b6464336d396c723665367372357a7a30727a3863767864377535741a1b0a066165766d6f7312113130303030303030303030303030303030127b0a570a4f0a282f65746865726d696e742e63727970746f2e76312e657468736563703235366b312e5075624b657912230a2102088ac2919987d927368cb2be2ade44cd0ed3616745a9699cae264b3fc5a7c36012040a02080112200a1a0a066165766d6f7312103535313336303030303030303030303010f0b4081a0c65766d6f735f393030312d3220cdc8eb32");
EXPECT_EQ(hex(preImageHash),
"9912eb629e215027b8d587939b1af72a9f70ae326bcaf48dfe77a729fc4ac632");


auto expectedTx = R"({"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"CpwBCpkBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEnkKLGV2bW9zMWQwamtyc2QwOWM3cHVsZTQzeTN5bHJ1bDQzbHd3Y3FhN3ZweTBnEixldm1vczE3ZGgzZnJ0MG02a2RkM205bHI2ZTZzcjV6ejByejhjdnhkN3U1dBobCgZhZXZtb3MSETEwMDAwMDAwMDAwMDAwMDAwEnsKVwpPCigvZXRoZXJtaW50LmNyeXB0by52MS5ldGhzZWNwMjU2azEuUHViS2V5EiMKIQIIisKRmYfZJzaMsr4q3kTNDtNhZ0WpaZyuJks/xafDYBIECgIIARIgChoKBmFldm1vcxIQNTUxMzYwMDAwMDAwMDAwMBDwtAgaQKrmMaaSKnohf3ahyCOYdRJKBKJjr4WkkA/cbn6FRdF0Gd6FHSzBP8S4v4VNiy3KC47TD0C+sUBO413gCzjo8/U="})";
Data signature;

{
TW::Cosmos::Proto::SigningOutput output;
ANY_SIGN(input, coin);
assertJSONEqual(
output.serialized(),
expectedTx);

signature = data(output.signature());
EXPECT_EQ(hex(signature),
"aae631a6922a7a217f76a1c8239875124a04a263af85a4900fdc6e7e8545d17419de851d2cc13fc4b8bf854d8b2dca0b8ed30f40beb1404ee35de00b38e8f3f5");

ASSERT_TRUE(publicKey.verify(signature, data(preImageHash.data())));
}

{
const Data outputData = TransactionCompiler::compileWithSignatures(
coin, protoInputData, {signature}, {publicKey.bytes});
Cosmos::Proto::SigningOutput output;
ASSERT_TRUE(output.ParseFromArray(outputData.data(), (int)outputData.size()));

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

}
2 changes: 1 addition & 1 deletion tests/common/CoinAddressDerivationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ TEST(Coin, DeriveAddress) {
EXPECT_EQ(address, "nano_1qepdf4k95dhb5gsmhmq3iddqsxiafwkihunm7irn48jdiwdtnn6pe93k3f6");
break;
case TWCoinTypeNativeEvmos:
EXPECT_EQ(address, "evmos1nk9x9ajk4rgkzhqjjn7hr6w0k0jg2kj07me7uu");
EXPECT_EQ(address, "evmos1j80up364zkmh9l06xp3nxccufrngql83ghwyf0");
break;
case TWCoinTypeNebulas:
EXPECT_EQ(address, "n1XTciu9ZRYt3ni7SxNBmivk9Y6XpP6VrhT");
Expand Down

0 comments on commit a636283

Please sign in to comment.