-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Evmos]: Fix signing, prehash, compile for Native Evmos (#3406)
- Loading branch information
1 parent
9deedbb
commit a369164
Showing
8 changed files
with
229 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
tests/chains/Cosmos/NativeInjective/TransactionCompilerTests.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::nativeInjective::tests { | ||
|
||
TEST(NativeInjectiveCompiler, CompileWithSignatures) { | ||
// Successfully broadcasted: https://www.mintscan.io/injective/transactions/B77D61590353C4AEDEAE2BBFF9E406DCF90E8D3A1A723BF22860F1E0A2617058 | ||
|
||
const auto coin = TWCoinTypeNativeInjective; | ||
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(88701); | ||
input.set_chain_id("injective-1"); | ||
input.set_memo(""); | ||
input.set_sequence(0); | ||
|
||
PublicKey publicKey = privateKey.getPublicKey(TWCoinTypePublicKeyType(coin)); | ||
const auto pubKeyBz = publicKey.bytes; | ||
ASSERT_EQ(hex(pubKeyBz), "04088ac2919987d927368cb2be2ade44cd0ed3616745a9699cae264b3fc5a7c3607d99f441b8340990ee990cb3eaf560f1f0bafe600c7e94a4be8392166984f728"); | ||
input.set_public_key(pubKeyBz.data(), pubKeyBz.size()); | ||
|
||
auto msg = input.add_messages(); | ||
auto& message = *msg->mutable_send_coins_message(); | ||
message.set_from_address("inj1d0jkrsd09c7pule43y3ylrul43lwwcqaky8w8c"); | ||
message.set_to_address("inj1xmpkmxr4as00em23tc2zgmuyy2gr4h3wgcl6vd"); | ||
auto amountOfTx = message.add_amounts(); | ||
amountOfTx->set_denom("inj"); | ||
amountOfTx->set_amount("10000000000"); | ||
|
||
auto& fee = *input.mutable_fee(); | ||
fee.set_gas(110000); | ||
auto amountOfFee = fee.add_amounts(); | ||
amountOfFee->set_denom("inj"); | ||
amountOfFee->set_amount("100000000000000"); | ||
|
||
/// 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), | ||
"0a8f010a8c010a1c2f636f736d6f732e62616e6b2e763162657461312e4d736753656e64126c0a2a696e6a3164306a6b7273643039633770756c6534337933796c72756c34336c77776371616b7938773863122a696e6a31786d706b6d78723461733030656d32337463327a676d7579793267723468337767636c3676641a120a03696e6a120b3130303030303030303030129c010a7c0a740a2d2f696e6a6563746976652e63727970746f2e763162657461312e657468736563703235366b312e5075624b657912430a4104088ac2919987d927368cb2be2ade44cd0ed3616745a9699cae264b3fc5a7c3607d99f441b8340990ee990cb3eaf560f1f0bafe600c7e94a4be8392166984f72812040a020801121c0a160a03696e6a120f31303030303030303030303030303010b0db061a0b696e6a6563746976652d3120fdb405"); | ||
EXPECT_EQ(hex(preImageHash), | ||
"57dc10c3d1893ff16e1f5a47fa4b2e96f37b9c57d98a42d88c971debb4947ec9"); | ||
|
||
|
||
auto expectedTx = R"({"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"Co8BCowBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEmwKKmluajFkMGprcnNkMDljN3B1bGU0M3kzeWxydWw0M2x3d2NxYWt5OHc4YxIqaW5qMXhtcGtteHI0YXMwMGVtMjN0YzJ6Z211eXkyZ3I0aDN3Z2NsNnZkGhIKA2luahILMTAwMDAwMDAwMDASnAEKfAp0Ci0vaW5qZWN0aXZlLmNyeXB0by52MWJldGExLmV0aHNlY3AyNTZrMS5QdWJLZXkSQwpBBAiKwpGZh9knNoyyvireRM0O02FnRalpnK4mSz/Fp8NgfZn0Qbg0CZDumQyz6vVg8fC6/mAMfpSkvoOSFmmE9ygSBAoCCAESHAoWCgNpbmoSDzEwMDAwMDAwMDAwMDAwMBCw2wYaQPep7ApSEXC7VWbKlz08c6G2mxYtmc4CIFkYmZHsRAY3MzOU/xyedfrYTrEUOTlp8gmJsDbx3+0olJ6QbcAHdCE="})"; | ||
Data signature; | ||
|
||
{ | ||
TW::Cosmos::Proto::SigningOutput output; | ||
ANY_SIGN(input, coin); | ||
assertJSONEqual( | ||
output.serialized(), | ||
expectedTx); | ||
|
||
signature = data(output.signature()); | ||
EXPECT_EQ(hex(signature), | ||
"f7a9ec0a521170bb5566ca973d3c73a1b69b162d99ce022059189991ec440637333394ff1c9e75fad84eb114393969f20989b036f1dfed28949e906dc0077421"); | ||
|
||
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()), ""); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), "04088ac2919987d927368cb2be2ade44cd0ed3616745a9699cae264b3fc5a7c3607d99f441b8340990ee990cb3eaf560f1f0bafe600c7e94a4be8392166984f728"); | ||
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()), ""); | ||
} | ||
} | ||
|
||
} |