Skip to content

Commit

Permalink
[Ethereum]: Properly handle hex encoded EIP-191 messages (#3833)
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan authored May 10, 2024
1 parent 2c4eb4a commit 484dc2a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
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

0 comments on commit 484dc2a

Please sign in to comment.