Claims module uses a hex representation of the address not raw (#371)

* Claims module uses a hex representation of the address not raw

* Fix test
This commit is contained in:
Gavin Wood
2019-08-15 16:56:12 +02:00
committed by GitHub
parent 2e4ce7f558
commit b4bb9c771c
+24 -10
View File
@@ -107,7 +107,8 @@ decl_module! {
fn claim(origin, dest: T::AccountId, ethereum_signature: EcdsaSignature) { fn claim(origin, dest: T::AccountId, ethereum_signature: EcdsaSignature) {
ensure_none(origin)?; ensure_none(origin)?;
let signer = dest.using_encoded(|data| Self::eth_recover(&ethereum_signature, data)) let data = dest.using_encoded(to_ascii_hex);
let signer = Self::eth_recover(&ethereum_signature, &data)
.ok_or("Invalid Ethereum signature")?; .ok_or("Invalid Ethereum signature")?;
let balance_due = <Claims<T>>::take(&signer) let balance_due = <Claims<T>>::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<u8> {
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<T: Trait> Module<T> { impl<T: Trait> Module<T> {
// Constructs the message that Ethereum RPC's `personal_sign` and `eth_sign` would sign. // Constructs the message that Ethereum RPC's `personal_sign` and `eth_sign` would sign.
fn ethereum_signable_message(what: &[u8]) -> Vec<u8> { fn ethereum_signable_message(what: &[u8]) -> Vec<u8> {
@@ -167,9 +179,10 @@ impl<T: Trait> ValidateUnsigned for Module<T> {
match call { match call {
Call::claim(account, ethereum_signature) => { Call::claim(account, ethereum_signature) => {
let signer = account.using_encoded(|data| Self::eth_recover(&ethereum_signature, data)); let data = account.using_encoded(to_ascii_hex);
let signer = if let Some(signer) = signer { let maybe_signer = Self::eth_recover(&ethereum_signature, &data);
signer let signer = if let Some(s) = maybe_signer {
s
} else { } else {
return TransactionValidity::Invalid(INVALID_ETHEREUM_SIGNATURE); return TransactionValidity::Invalid(INVALID_ETHEREUM_SIGNATURE);
}; };
@@ -264,7 +277,7 @@ mod tests {
} }
parameter_types!{ 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 { impl Trait for Test {
@@ -287,7 +300,7 @@ mod tests {
res res
} }
fn alice_sig(what: &[u8]) -> EcdsaSignature { 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, recovery_id) = secp256k1::sign(&secp256k1::Message::parse(&msg), &alice_secret()).unwrap();
let sig: ([u8; 32], [u8; 32]) = Decode::decode(&mut &sig.serialize()[..]).unwrap(); let sig: ([u8; 32], [u8; 32]) = Decode::decode(&mut &sig.serialize()[..]).unwrap();
EcdsaSignature(sig.0, sig.1, recovery_id.serialize() as i8) EcdsaSignature(sig.0, sig.1, recovery_id.serialize() as i8)
@@ -296,7 +309,7 @@ mod tests {
secp256k1::SecretKey::parse(&keccak256(b"Bob")).unwrap() secp256k1::SecretKey::parse(&keccak256(b"Bob")).unwrap()
} }
fn bob_sig(what: &[u8]) -> EcdsaSignature { 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, recovery_id) = secp256k1::sign(&secp256k1::Message::parse(&msg), &bob_secret()).unwrap();
let sig: ([u8; 32], [u8; 32]) = Decode::decode(&mut &sig.serialize()[..]).unwrap(); let sig: ([u8; 32], [u8; 32]) = Decode::decode(&mut &sig.serialize()[..]).unwrap();
EcdsaSignature(sig.0, sig.1, recovery_id.serialize() as i8) EcdsaSignature(sig.0, sig.1, recovery_id.serialize() as i8)
@@ -369,11 +382,12 @@ mod tests {
#[test] #[test]
fn real_eth_sig_works() { fn real_eth_sig_works() {
with_externalities(&mut new_test_ext(), || { 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 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(); let signer = Claims::eth_recover(&sig, &who).unwrap();
assert_eq!(signer, hex!["DF67EC7EAe23D2459694685257b6FC59d1BAA1FE"]); assert_eq!(signer, hex!["6d31165d5d932d571f3b44695653b46dcc327e84"]);
}); });
} }