From 825817c1bd5a499da8dae88e98db673b1ad58c4e Mon Sep 17 00:00:00 2001 From: toints Date: Mon, 30 Oct 2023 19:21:52 +0800 Subject: [PATCH] Fixed: fixed eth recover function --- pallets/assets-bridge/src/lib.rs | 10 +++++++++- pallets/assets-bridge/src/recover.rs | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pallets/assets-bridge/src/lib.rs b/pallets/assets-bridge/src/lib.rs index 61fec21..cef45af 100644 --- a/pallets/assets-bridge/src/lib.rs +++ b/pallets/assets-bridge/src/lib.rs @@ -377,9 +377,17 @@ pub mod pallet { ensure!(!SubAccounts::::contains_key(eth_address), Error::::EthAddressHasMapped); // recover evm address from signature - let address = eth_recover(ð_signature, &who.using_encoded(to_ascii_hex), &[][..]) + // let address = eth_recover(ð_signature, &who.using_encoded(to_ascii_hex), &[][..]) + // .ok_or(Error::::BadSignature)?; + + let address = beta_eth_recover(ð_signature, &who.using_encoded(to_ascii_hex)) .ok_or(Error::::BadSignature)?; + // let address = Self::eth_recover(ð_signature).ok_or(Error::::BadSignature)?; + + // frame_support::runtime_print!("\n\n========================================================================who {:?},eth_address {:?}, address {:?}=============================\n\n", + // &who.using_encoded(to_ascii_hex),eth_address.as_bytes(),address.as_bytes()); + ensure!(eth_address == address, Error::::InvalidSignature); ::Currency::reserve(&who, T::ClaimBond::get())?; diff --git a/pallets/assets-bridge/src/recover.rs b/pallets/assets-bridge/src/recover.rs index 1d46ee1..3739685 100644 --- a/pallets/assets-bridge/src/recover.rs +++ b/pallets/assets-bridge/src/recover.rs @@ -52,3 +52,22 @@ pub fn ethereum_signable_message(what: &[u8], extra: &[u8]) -> Vec { v.extend_from_slice(extra); v } + +pub fn beta_eth_recover(signature: &ecdsa::Signature, what: &[u8]) -> Option { + let mut v = Vec::new(); + let prefix = b"evm:"; + v.extend_from_slice(&prefix[..]); + v.extend_from_slice(what); + + // frame_support::runtime_print!("\n\n=================msg : {:?}============================\n\n", &v); + let message_hash: [u8; 32] = keccak_256(v.as_ref()); + // frame_support::runtime_print!("\n\n=================msg hash : {:?}============================\n\n", &message_hash); + let mut sig = [0u8; 65]; + sig[0..64].copy_from_slice(&signature.0[0..64]); + sig[64] = signature.0[64]; + + let public_key = sp_io::crypto::secp256k1_ecdsa_recover(&sig, &message_hash).ok()?; + + let address_hash = keccak_256(&public_key); + Some(H160::from_slice(&address_hash[12..32])) +}