diff --git a/runtime/src/claims.rs b/runtime/src/claims.rs index c2a6d3b471f04..c7e5681b7ba44 100644 --- a/runtime/src/claims.rs +++ b/runtime/src/claims.rs @@ -107,7 +107,8 @@ decl_module! { fn claim(origin, dest: T::AccountId, ethereum_signature: EcdsaSignature) { ensure_none(origin)?; - let signer = dest.using_encoded(|data| Self::eth_recover(ðereum_signature, data)) + let data = dest.using_encoded(to_ascii_hex); + let signer = Self::eth_recover(ðereum_signature, &data) .ok_or("Invalid Ethereum signature")?; let balance_due = >::take(&signer) @@ -127,6 +128,17 @@ decl_module! { } } +/// Converts the given binary data into ASCII-encoded hex. It will be twice the length. +fn to_ascii_hex(data: &[u8]) -> Vec { + let mut r = Vec::with_capacity(data.len() * 2); + let mut push_nibble = |n| r.push(if n < 10 { b'0' + n } else { b'a' - 10 + n }); + for &b in data.iter() { + push_nibble(b / 16); + push_nibble(b % 16); + } + r +} + impl Module { // Constructs the message that Ethereum RPC's `personal_sign` and `eth_sign` would sign. fn ethereum_signable_message(what: &[u8]) -> Vec { @@ -167,9 +179,10 @@ impl ValidateUnsigned for Module { match call { Call::claim(account, ethereum_signature) => { - let signer = account.using_encoded(|data| Self::eth_recover(ðereum_signature, data)); - let signer = if let Some(signer) = signer { - signer + let data = account.using_encoded(to_ascii_hex); + let maybe_signer = Self::eth_recover(ðereum_signature, &data); + let signer = if let Some(s) = maybe_signer { + s } else { return TransactionValidity::Invalid(INVALID_ETHEREUM_SIGNATURE); }; @@ -264,7 +277,7 @@ mod tests { } parameter_types!{ - pub const Prefix: &'static [u8] = b"Pay DOTs to the Polkadot account:"; + pub const Prefix: &'static [u8] = b"Pay RUSTs to the TEST account:"; } impl Trait for Test { @@ -287,7 +300,7 @@ mod tests { res } fn alice_sig(what: &[u8]) -> EcdsaSignature { - let msg = keccak256(&Claims::ethereum_signable_message(what)); + let msg = keccak256(&Claims::ethereum_signable_message(&to_ascii_hex(what)[..])); let (sig, recovery_id) = secp256k1::sign(&secp256k1::Message::parse(&msg), &alice_secret()).unwrap(); let sig: ([u8; 32], [u8; 32]) = Decode::decode(&mut &sig.serialize()[..]).unwrap(); EcdsaSignature(sig.0, sig.1, recovery_id.serialize() as i8) @@ -296,7 +309,7 @@ mod tests { secp256k1::SecretKey::parse(&keccak256(b"Bob")).unwrap() } fn bob_sig(what: &[u8]) -> EcdsaSignature { - let msg = keccak256(&Claims::ethereum_signable_message(what)); + let msg = keccak256(&Claims::ethereum_signable_message(&to_ascii_hex(what)[..])); let (sig, recovery_id) = secp256k1::sign(&secp256k1::Message::parse(&msg), &bob_secret()).unwrap(); let sig: ([u8; 32], [u8; 32]) = Decode::decode(&mut &sig.serialize()[..]).unwrap(); EcdsaSignature(sig.0, sig.1, recovery_id.serialize() as i8) @@ -369,11 +382,12 @@ mod tests { #[test] fn real_eth_sig_works() { with_externalities(&mut new_test_ext(), || { - let sig = hex!["7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff1c"]; + // "Pay RUSTs to the TEST account:2a00000000000000" + let sig = hex!["444023e89b67e67c0562ed0305d252a5dd12b2af5ac51d6d3cb69a0b486bc4b3191401802dc29d26d586221f7256cd3329fe82174bdf659baea149a40e1c495d1c"]; let sig = EcdsaSignature::from_blob(&sig); - let who = 42u64.encode(); + let who = 42u64.using_encoded(to_ascii_hex); let signer = Claims::eth_recover(&sig, &who).unwrap(); - assert_eq!(signer, hex!["DF67EC7EAe23D2459694685257b6FC59d1BAA1FE"]); + assert_eq!(signer, hex!["6d31165d5d932d571f3b44695653b46dcc327e84"]); }); }