diff --git a/android/app/src/androidTest/java/com/trustwallet/core/app/blockchains/ethereum/TestEthereumMessageSigner.kt b/android/app/src/androidTest/java/com/trustwallet/core/app/blockchains/ethereum/TestEthereumMessageSigner.kt index 6b86f698769..6245751d124 100644 --- a/android/app/src/androidTest/java/com/trustwallet/core/app/blockchains/ethereum/TestEthereumMessageSigner.kt +++ b/android/app/src/androidTest/java/com/trustwallet/core/app/blockchains/ethereum/TestEthereumMessageSigner.kt @@ -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") diff --git a/rust/tw_evm/src/message/eip191.rs b/rust/tw_evm/src/message/eip191.rs index cb2bd715d8e..ed532f6cc2e 100644 --- a/rust/tw_evm/src/message/eip191.rs +++ b/rust/tw_evm/src/message/eip191.rs @@ -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; @@ -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 } diff --git a/rust/tw_evm/tests/message_signer.rs b/rust/tw_evm/tests/message_signer.rs index e09efef30fc..447e48df6db 100644 --- a/rust/tw_evm/tests/message_signer.rs +++ b/rust/tw_evm/tests/message_signer.rs @@ -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 {