Skip to content

Commit

Permalink
Merge pull request #6 from toints/xcm
Browse files Browse the repository at this point in the history
Fixed: fixed eth recover function
  • Loading branch information
toints authored Oct 30, 2023
2 parents baa05a9 + 825817c commit 7a8491c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pallets/assets-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,17 @@ pub mod pallet {
ensure!(!SubAccounts::<T>::contains_key(eth_address), Error::<T>::EthAddressHasMapped);

// recover evm address from signature
let address = eth_recover(&eth_signature, &who.using_encoded(to_ascii_hex), &[][..])
// let address = eth_recover(&eth_signature, &who.using_encoded(to_ascii_hex), &[][..])
// .ok_or(Error::<T>::BadSignature)?;

let address = beta_eth_recover(&eth_signature, &who.using_encoded(to_ascii_hex))
.ok_or(Error::<T>::BadSignature)?;

// let address = Self::eth_recover(&eth_signature).ok_or(Error::<T>::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::<T>::InvalidSignature);

<T as pallet_assets::Config>::Currency::reserve(&who, T::ClaimBond::get())?;
Expand Down
19 changes: 19 additions & 0 deletions pallets/assets-bridge/src/recover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,22 @@ pub fn ethereum_signable_message(what: &[u8], extra: &[u8]) -> Vec<u8> {
v.extend_from_slice(extra);
v
}

pub fn beta_eth_recover(signature: &ecdsa::Signature, what: &[u8]) -> Option<H160> {
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]))
}

0 comments on commit 7a8491c

Please sign in to comment.