Skip to content

Commit

Permalink
[Cosmos]: evmos fix (#3404)
Browse files Browse the repository at this point in the history
* feat(cosmos_signer): support pre-hash for evm cosmos chain(s)

* feat(cosmos_signer): support pre-hash for evm cosmos chain(s)

* feat(cosmos_signer): fix evm cosmos chain(s) serialized output

* feat(evmos): fix evmos unit test
  • Loading branch information
Milerius authored Aug 31, 2023
1 parent 3694d7f commit 9deedbb
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ class TestNativeInjectiveSigner {
val output = AnySigner.sign(signingInput, CoinType.NATIVEINJECTIVE, Cosmos.SigningOutput.parser())

// https://www.mintscan.io/injective/txs/135DD2C4A1910E4334A9C0F15125DA992E724EBF23FEB9638FCB71218BB064A5
assertEquals(output.serialized, "{\"mode\":\"BROADCAST_MODE_BLOCK\",\"tx_bytes\":\"Co8BCowBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEmwKKmluajEzdTZnN3ZxZ3cwNzRtZ21mMnplMmNhZHp2a3o5c25sd2NydHE4YRIqaW5qMXhtcGtteHI0YXMwMGVtMjN0YzJ6Z211eXkyZ3I0aDN3Z2NsNnZkGhIKA2luahILMTAwMDAwMDAwMDASfgpeClQKLS9pbmplY3RpdmUuY3J5cHRvLnYxYmV0YTEuZXRoc2VjcDI1NmsxLlB1YktleRIjCiEDWgxrg7i9mCflBycMrbSZt+OpCVJG9qIhMoH3g9h3yYsSBAoCCAEYARIcChYKA2luahIPMTAwMDAwMDAwMDAwMDAwELDbBhpArNDBBEwHVKwuSDozLIwvOOhDQ/i7bXC6Av5ZefSQf7RS2ejrapX/JKXPsYrtMWadhKCedomODhujvWzzGwmXVA==\"}")
assertEquals(output.serialized, "{\"mode\":\"BROADCAST_MODE_BLOCK\",\"tx_bytes\":\"Co8BCowBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEmwKKmluajEzdTZnN3ZxZ3cwNzRtZ21mMnplMmNhZHp2a3o5c25sd2NydHE4YRIqaW5qMXhtcGtteHI0YXMwMGVtMjN0YzJ6Z211eXkyZ3I0aDN3Z2NsNnZkGhIKA2luahILMTAwMDAwMDAwMDASngEKfgp0Ci0vaW5qZWN0aXZlLmNyeXB0by52MWJldGExLmV0aHNlY3AyNTZrMS5QdWJLZXkSQwpBBFoMa4O4vZgn5QcnDK20mbfjqQlSRvaiITKB94PYd8mLJWdCdBsGOfMXdo/k9MJ2JmDCESKDp2hdgVUH3uMikXMSBAoCCAEYARIcChYKA2luahIPMTAwMDAwMDAwMDAwMDAwELDbBhpAx2vkplmzeK7n3puCFGPWhLd0l/ZC/CYkGl+stH+3S3hiCvIe7uwwMpUlNaSwvT8HwF1kNUp+Sx2m0Uo1x5xcFw==\"}")
}
}
5 changes: 4 additions & 1 deletion src/Cosmos/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ Data Entry::preImageHashes(TWCoinType coin, const Data& txInputData) const {
txInputData, [&coin](const auto& input, auto& output) {
auto pkVec = Data(input.public_key().begin(), input.public_key().end());
auto preimage = Signer().signaturePreimage(input, pkVec, coin);
auto imageHash = Hash::sha256(preimage);
auto isEvmCosmosChain = [coin]() {
return coin == TWCoinTypeNativeInjective || coin == TWCoinTypeNativeEvmos || coin == TWCoinTypeNativeCanto;
};
auto imageHash = isEvmCosmosChain() ? Hash::keccak256(preimage) : Hash::sha256(preimage);
output.set_data(preimage.data(), preimage.size());
output.set_data_hash(imageHash.data(), imageHash.size());
});
Expand Down
2 changes: 1 addition & 1 deletion src/Cosmos/ProtobufSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ std::string buildProtoTxBody(const Proto::SigningInput& input) {
std::string buildAuthInfo(const Proto::SigningInput& input, TWCoinType coin) {
// AuthInfo
const auto privateKey = PrivateKey(input.private_key());
const auto publicKey = privateKey.getPublicKey(TWPublicKeyTypeSECP256k1);
const auto publicKey = privateKey.getPublicKey(TWCoinTypePublicKeyType(coin));
return buildAuthInfo(input, publicKey, coin);
}

Expand Down
1 change: 0 additions & 1 deletion src/Hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ TW::Hash::HasherSimpleType Hash::functionPointerFromEnum(TW::Hash::Hasher hasher
switch (hasher) {
case Hash::HasherSha1:
return Hash::sha1;
default:
case Hash::HasherSha256:
return Hash::sha256;
case Hash::HasherSha512:
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":"CpoBCpcBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEncKLGV2bW9zMXJrMzlkazN3ZmY1bnBzN2VtdWh2M250a24zbnN6NnoyZXJxZnIwEixldm1vczEwazlscnJydWFwOW51OTZteHd3eWUyZjZhNXdhemVoMzNrcTY3ehoZCgZhZXZtb3MSDzIwMDAwMDAwMDAwMDAwMBJ7ClcKTwooL2V0aGVybWludC5jcnlwdG8udjEuZXRoc2VjcDI1NmsxLlB1YktleRIjCiEClHXJ+iPsaTZnuqdsTaabSczP3wWMTcsnumfPvJCC2e0SBAoCCAESIAoaCgZhZXZtb3MSEDE0MDAwMDAwMDAwMDAwMDAQ4MUIGkAz9vh1EutbLrLZmRA4eK72bA6bhfMX0YnhtRl5jeaL3AYmk0qdrwG9XzzleBsZ++IokJIk47cgOOyvEjl92Jhj"}
{"mode":"BROADCAST_MODE_BLOCK","tx_bytes":"CpoBCpcBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEncKLGV2bW9zMXJrMzlkazN3ZmY1bnBzN2VtdWh2M250a24zbnN6NnoyZXJxZnIwEixldm1vczEwazlscnJydWFwOW51OTZteHd3eWUyZjZhNXdhemVoMzNrcTY3ehoZCgZhZXZtb3MSDzIwMDAwMDAwMDAwMDAwMBKbAQp3Cm8KKC9ldGhlcm1pbnQuY3J5cHRvLnYxLmV0aHNlY3AyNTZrMS5QdWJLZXkSQwpBBJR1yfoj7Gk2Z7qnbE2mm0nMz98FjE3LJ7pnz7yQgtntkHR4ZWCqaYsZu5cpUmscdZNPPUp4975xnkOGt0mzYxASBAoCCAESIAoaCgZhZXZtb3MSEDE0MDAwMDAwMDAwMDAwMDAQ4MUIGkDBSFkYlIx1/6ZvRtEHyq6r0EEFDb/5T2Cb9zVYWtYZE1e7YteG2GuYMX546PvBQ7CYAFZEkr+rU6okC0nAT0UK"}
"""
XCTAssertJSONEqual(output.serialized, expected)
XCTAssertEqual(output.errorMessage, "")
Expand Down
2 changes: 1 addition & 1 deletion swift/Tests/Blockchains/NativeInjectiveTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ class NativeInjectiveTests: XCTestCase {

let output: CosmosSigningOutput = AnySigner.sign(input: input, coin: .nativeInjective)

XCTAssertJSONEqual(output.serialized, "{\"mode\":\"BROADCAST_MODE_BLOCK\",\"tx_bytes\":\"Co8BCowBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEmwKKmluajEzdTZnN3ZxZ3cwNzRtZ21mMnplMmNhZHp2a3o5c25sd2NydHE4YRIqaW5qMXhtcGtteHI0YXMwMGVtMjN0YzJ6Z211eXkyZ3I0aDN3Z2NsNnZkGhIKA2luahILMTAwMDAwMDAwMDASfgpeClQKLS9pbmplY3RpdmUuY3J5cHRvLnYxYmV0YTEuZXRoc2VjcDI1NmsxLlB1YktleRIjCiEDWgxrg7i9mCflBycMrbSZt+OpCVJG9qIhMoH3g9h3yYsSBAoCCAEYARIcChYKA2luahIPMTAwMDAwMDAwMDAwMDAwELDbBhpArNDBBEwHVKwuSDozLIwvOOhDQ/i7bXC6Av5ZefSQf7RS2ejrapX/JKXPsYrtMWadhKCedomODhujvWzzGwmXVA==\"}")
XCTAssertJSONEqual(output.serialized, "{\"mode\":\"BROADCAST_MODE_BLOCK\",\"tx_bytes\":\"Co8BCowBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEmwKKmluajEzdTZnN3ZxZ3cwNzRtZ21mMnplMmNhZHp2a3o5c25sd2NydHE4YRIqaW5qMXhtcGtteHI0YXMwMGVtMjN0YzJ6Z211eXkyZ3I0aDN3Z2NsNnZkGhIKA2luahILMTAwMDAwMDAwMDASngEKfgp0Ci0vaW5qZWN0aXZlLmNyeXB0by52MWJldGExLmV0aHNlY3AyNTZrMS5QdWJLZXkSQwpBBFoMa4O4vZgn5QcnDK20mbfjqQlSRvaiITKB94PYd8mLJWdCdBsGOfMXdo/k9MJ2JmDCESKDp2hdgVUH3uMikXMSBAoCCAEYARIcChYKA2luahIPMTAwMDAwMDAwMDAwMDAwELDbBhpAx2vkplmzeK7n3puCFGPWhLd0l/ZC/CYkGl+stH+3S3hiCvIe7uwwMpUlNaSwvT8HwF1kNUp+Sx2m0Uo1x5xcFw==\"}")
}
}
2 changes: 1 addition & 1 deletion tests/chains/Cosmos/NativeInjective/SignerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ TEST(NativeInjectiveSigner, Sign) {
auto output = Signer::sign(input, TWCoinTypeNativeInjective);

// https://www.mintscan.io/injective/txs/135DD2C4A1910E4334A9C0F15125DA992E724EBF23FEB9638FCB71218BB064A5
assertJSONEqual(output.serialized(), "{\"tx_bytes\":\"Co8BCowBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEmwKKmluajEzdTZnN3ZxZ3cwNzRtZ21mMnplMmNhZHp2a3o5c25sd2NydHE4YRIqaW5qMXhtcGtteHI0YXMwMGVtMjN0YzJ6Z211eXkyZ3I0aDN3Z2NsNnZkGhIKA2luahILMTAwMDAwMDAwMDASfgpeClQKLS9pbmplY3RpdmUuY3J5cHRvLnYxYmV0YTEuZXRoc2VjcDI1NmsxLlB1YktleRIjCiEDWgxrg7i9mCflBycMrbSZt+OpCVJG9qIhMoH3g9h3yYsSBAoCCAEYARIcChYKA2luahIPMTAwMDAwMDAwMDAwMDAwELDbBhpArNDBBEwHVKwuSDozLIwvOOhDQ/i7bXC6Av5ZefSQf7RS2ejrapX/JKXPsYrtMWadhKCedomODhujvWzzGwmXVA==\",\"mode\":\"BROADCAST_MODE_BLOCK\"}");
assertJSONEqual(output.serialized(), "{\"mode\":\"BROADCAST_MODE_BLOCK\",\"tx_bytes\":\"Co8BCowBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEmwKKmluajEzdTZnN3ZxZ3cwNzRtZ21mMnplMmNhZHp2a3o5c25sd2NydHE4YRIqaW5qMXhtcGtteHI0YXMwMGVtMjN0YzJ6Z211eXkyZ3I0aDN3Z2NsNnZkGhIKA2luahILMTAwMDAwMDAwMDASngEKfgp0Ci0vaW5qZWN0aXZlLmNyeXB0by52MWJldGExLmV0aHNlY3AyNTZrMS5QdWJLZXkSQwpBBFoMa4O4vZgn5QcnDK20mbfjqQlSRvaiITKB94PYd8mLJWdCdBsGOfMXdo/k9MJ2JmDCESKDp2hdgVUH3uMikXMSBAoCCAEYARIcChYKA2luahIPMTAwMDAwMDAwMDAwMDAwELDbBhpAx2vkplmzeK7n3puCFGPWhLd0l/ZC/CYkGl+stH+3S3hiCvIe7uwwMpUlNaSwvT8HwF1kNUp+Sx2m0Uo1x5xcFw==\"}");
}

}
3 changes: 1 addition & 2 deletions tests/chains/Evmos/SignerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ TEST(EvmosSigner, CompoundingAuthz) {
auto output = Signer::sign(input, TWCoinTypeNativeEvmos);
auto expected = R"(
{
"mode":"BROADCAST_MODE_BLOCK",
"tx_bytes":"CvUBCvIBCh4vY29zbW9zLmF1dGh6LnYxYmV0YTEuTXNn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iaO75siYg72ylEgQKAggBGAMSIAoaCgZhZXZtb3MSEDQ1MjE0NzUwMDAwMDAwMDAQ+4QLGkArFy2yWnZbP9aqxyx9KN8xlTVyWT5jolnrY5fbMtXeijVOGZIrFCtEg+xgjv6XpMTKK9A3cMMMcBAcuv2S+nN8"
})";
assertJSONEqual(output.serialized(), expected);
}
Expand Down

0 comments on commit 9deedbb

Please sign in to comment.