Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect address hashing + add TU for evm_get_address_from_pubkey #4785

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions massa-execution-worker/src/interface_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,13 +968,10 @@ impl Interface for InterfaceImpl {
/// Address is the last 20 bytes of the hash of the public key.
fn evm_get_address_from_pubkey(&self, public_key_: &[u8]) -> Result<Vec<u8>> {
// parse the public key
let public_key = libsecp256k1::PublicKey::parse_slice(
public_key_,
Some(libsecp256k1::PublicKeyFormat::Raw),
)?;
let public_key = libsecp256k1::PublicKey::parse_slice(public_key_, None)?;

// compute the hash of the public key
let hash = sha3::Keccak256::digest(public_key.serialize());
let hash = sha3::Keccak256::digest(&public_key.serialize()[1..]);

// ignore the first 12 bytes of the hash
let address = hash[12..].to_vec();
Expand Down
37 changes: 35 additions & 2 deletions massa-execution-worker/src/tests/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use massa_sc_runtime::Interface;

#[test]
fn test_hash_sha256() {
// test hashing using sha256 algo

let interface = InterfaceImpl::new_default(
Address::from_str("AU12cMW9zRKFDS43Z2W88VCmdQFxmHjAo54XvuVV34UzJeXRLXW9M").unwrap(),
None,
Expand All @@ -25,6 +27,8 @@ fn test_hash_sha256() {

#[test]
fn test_evm_signature_verify() {
// Test that we can verify an Ethereum signature

let interface = InterfaceImpl::new_default(
Address::from_str("AU12cMW9zRKFDS43Z2W88VCmdQFxmHjAo54XvuVV34UzJeXRLXW9M").unwrap(),
None,
Expand Down Expand Up @@ -55,6 +59,8 @@ fn test_evm_signature_verify() {

#[test]
fn test_evm_get_pubkey_from_signature() {
// Test that we can retrieve the public key from an Ethereum signature

let interface = InterfaceImpl::new_default(
Address::from_str("AU12cMW9zRKFDS43Z2W88VCmdQFxmHjAo54XvuVV34UzJeXRLXW9M").unwrap(),
None,
Expand All @@ -79,7 +85,7 @@ fn test_evm_get_pubkey_from_signature() {
assert!(result.is_ok());
assert_eq!(public_key.serialize(), result.unwrap().as_ref());

// Invalid s
// Invalid s - expect failure
{
let mut signature_2 =
libsecp256k1::Signature::parse_standard_slice(&signature_[..64]).unwrap();
Expand All @@ -89,7 +95,7 @@ fn test_evm_get_pubkey_from_signature() {
assert!(result.is_err());
}

// Invalid v
// Invalid v - expect failure
{
let mut signature_2_ = signature_;
signature_2_[64] ^= 1;
Expand All @@ -98,6 +104,33 @@ fn test_evm_get_pubkey_from_signature() {
}
}

#[test]
fn test_evm_address() {
// Test that we can retrieve an Ethereum address from an Ethereum public key (from a private key)

let interface = InterfaceImpl::new_default(
Address::from_str("AU12cMW9zRKFDS43Z2W88VCmdQFxmHjAo54XvuVV34UzJeXRLXW9M").unwrap(),
None,
None,
);

let _address = hex!("807a7bb5193edf9898b9092c1597bb966fe52514");
// let message_ = b"test";
// let signature_ = hex!("d0d05c35080635b5e865006c6c4f5b5d457ec342564d8fc67ce40edc264ccdab3f2f366b5bd1e38582538fed7fa6282148e86af97970a10cb3302896f5d68ef51b");
let private_key_ = hex!("ed6602758bdd68dc9df67a6936ed69807a74b8cc89bdc18f3939149d02db17f3");

// build original public key
let private_key = libsecp256k1::SecretKey::parse_slice(&private_key_).unwrap();

let public_key = libsecp256k1::PublicKey::from_secret_key(&private_key);
let res = interface
.evm_get_address_from_pubkey(&public_key.serialize())
.unwrap();
// println!("***result: {:?}", res.to_vec());
// println!("***add: {:?}", _address);
assert_eq!(res, _address);
}

#[test]
fn test_emit_event() {
// emit 2 events and check that the 2nd event is rejected (because the limit is reached)
Expand Down
Loading