Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ethereum]: Properly handle hex encoded EIP-191 messages #3833

Merged
merged 2 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ class TestEthereumMessageSigner {
assertTrue(EthereumMessageSigner.verifyMessage(publicKey, msg, signature))
}

@Test
fun testEthereumSignAndVerifyMessageLegacyHex() {
val data = Numeric.hexStringToByteArray("9066aa168c379a403becb235c15e7129c133c244e56a757ab07bc369288bcab0")
val privateKey = PrivateKey(data)
val publicKey = privateKey.getPublicKey(CoinType.ETHEREUM)
val msg = "0xc0a96273d5c3fbe4d4000491f08daef9c17f88df846c1d6f57eb5f33c1fbd035"
val signature = EthereumMessageSigner.signMessage(privateKey, msg)
assertEquals(signature, "b18a666ad08bf9bfcd39920b26b5a5d1486b67b45119810b3c7bda22e41e5c4c1bfbe0c932f6c14df4947a18ba310831a37b7307d724a3ac2a4935b99d7075141b");
assertTrue(EthereumMessageSigner.verifyMessage(publicKey, msg, signature))
}

@Test
fun testEthereumSignAndVerifyMessage712Legacy() {
val data = Numeric.hexStringToByteArray("03a9ca895dca1623c7dfd69693f7b4111f5d819d2e145536e0b03c136025a25d")
Expand Down
15 changes: 13 additions & 2 deletions rust/tw_evm/src/message/eip191.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Copyright © 2017 Trust Wallet.

use crate::message::{EthMessage, MessageSigningResult};
use tw_encoding::hex;
use tw_hash::sha3::keccak256;
use tw_hash::H256;

Expand All @@ -27,8 +28,18 @@ impl Eip191Message {

data.push(ETHEREUM_PREFIX);
data.extend_from_slice(ETHEREUM_MESSAGE_PREFIX.as_bytes());
data.extend_from_slice(self.user_message.len().to_string().as_bytes());
data.extend_from_slice(self.user_message.as_bytes());

let mut do_extend = |bytes: &[u8]| {
data.extend_from_slice(bytes.len().to_string().as_bytes());
data.extend_from_slice(bytes);
};

match hex::decode(&self.user_message) {
// Handle the message as hex-encoded.
Ok(raw_msg) => do_extend(&raw_msg),
// Handle as a regular string.
Err(_) => do_extend(self.user_message.as_bytes()),
}

data
}
Expand Down
13 changes: 13 additions & 0 deletions rust/tw_evm/tests/message_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ fn test_message_signer_sign_verify_legacy() {
});
}

/// https://github.com/trustwallet/wallet-core/issues/3831
#[test]
fn test_message_signer_sign_verify_legacy_hex() {
test_message_signer_sign_verify(SignVerifyTestInput {
// 0x9d1d97adfcd324bbd603d3872bd78e04098510b1
private_key: "9066aa168c379a403becb235c15e7129c133c244e56a757ab07bc369288bcab0",
msg: "0xc0a96273d5c3fbe4d4000491f08daef9c17f88df846c1d6f57eb5f33c1fbd035",
msg_type: Proto::MessageType::MessageType_legacy,
chain_id: None,
signature: "b18a666ad08bf9bfcd39920b26b5a5d1486b67b45119810b3c7bda22e41e5c4c1bfbe0c932f6c14df4947a18ba310831a37b7307d724a3ac2a4935b99d7075141b",
});
}

#[test]
fn test_message_signer_sign_verify_eip155() {
test_message_signer_sign_verify(SignVerifyTestInput {
Expand Down
Loading