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

Implement ecdsa_to_eth_address() and remove eth_compatibility crate #1233

Merged
merged 22 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ members = [
"crates/primitives",
"crates/engine",
"crates/env",
"crates/eth_compatibility",
"crates/storage",
"crates/storage/derive",
]
Expand Down
7 changes: 7 additions & 0 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,13 @@ pub fn ecdsa_recover(
})
}

/// Returns Ethereum address from the ECDSA compressed public key.
agryaznov marked this conversation as resolved.
Show resolved Hide resolved
pub fn ecdsa_to_eth_address(pubkey: &[u8; 33], output: &mut [u8; 20]) -> Result<()> {
<EnvInstance as OnInstance>::on_instance(|instance| {
instance.ecdsa_to_eth_address(pubkey, output)
})
}

/// Checks whether the specified account is a contract.
///
/// # Errors
Expand Down
8 changes: 8 additions & 0 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ pub trait EnvBackend {
output: &mut [u8; 33],
) -> Result<()>;

/// Retrieves Ethereum address from the ECDSA compressed `pubkey`,
agryaznov marked this conversation as resolved.
Show resolved Hide resolved
/// and stores the result in `output`.
fn ecdsa_to_eth_address(
&mut self,
pubkey: &[u8; 33],
output: &mut [u8; 20],
) -> Result<()>;

/// Low-level interface to call a chain extension method.
///
/// Returns the output of the chain extension of the specified type.
Expand Down
14 changes: 14 additions & 0 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,20 @@ impl EnvBackend for EnvInstance {
}
}

fn ecdsa_to_eth_address(
&mut self,
pubkey: &[u8; 33],
output: &mut [u8; 20],
) -> Result<()> {
let pk = secp256k1::PublicKey::from_slice(pubkey)
.map_err(|_| Error::EcdsaRecoveryFailed)?;
let uncompressed = pk.serialize();
let mut hash = <Keccak256 as HashOutput>::Type::default();
<Keccak256>::hash(&uncompressed[1..], &mut hash);
output.as_mut().copy_from_slice(&hash[12..]);
Ok(())
}

fn call_chain_extension<I, T, E, ErrorCode, F, D>(
&mut self,
func_id: u32,
Expand Down
15 changes: 15 additions & 0 deletions crates/env/src/engine/on_chain/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ mod sys {
output_ptr: Ptr32Mut<[u8]>,
) -> ReturnCode;

pub fn seal_ecdsa_to_eth_address(
pubkey_ptr: Ptr32<[u8]>,
output_ptr: Ptr32Mut<[u8]>,
) -> ReturnCode;

pub fn seal_set_code_hash(code_hash_ptr: Ptr32<[u8]>) -> ReturnCode;

pub fn seal_code_hash(
Expand Down Expand Up @@ -680,6 +685,16 @@ pub fn ecdsa_recover(
ret_code.into()
}

pub fn ecdsa_to_eth_address(pubkey: &[u8; 33], output: &mut [u8; 20]) -> Result {
let ret_code = unsafe {
sys::seal_ecdsa_to_eth_address(
Ptr32::from_slice(pubkey),
Ptr32Mut::from_slice(output),
)
};
ret_code.into()
}

pub fn is_contract(account_id: &[u8]) -> bool {
let ret_val = unsafe { sys::seal_is_contract(Ptr32::from_slice(account_id)) };
ret_val.into_bool()
Expand Down
8 changes: 8 additions & 0 deletions crates/env/src/engine/on_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,14 @@ impl EnvBackend for EnvInstance {
ext::ecdsa_recover(signature, message_hash, output).map_err(Into::into)
}

fn ecdsa_to_eth_address(
&mut self,
pubkey: &[u8; 33],
output: &mut [u8; 20],
) -> Result<()> {
ext::ecdsa_to_eth_address(pubkey, output).map_err(Into::into)
}

fn call_chain_extension<I, T, E, ErrorCode, F, D>(
&mut self,
func_id: u32,
Expand Down
30 changes: 0 additions & 30 deletions crates/eth_compatibility/Cargo.toml

This file was deleted.

1 change: 0 additions & 1 deletion crates/eth_compatibility/LICENSE

This file was deleted.

1 change: 0 additions & 1 deletion crates/eth_compatibility/README.md

This file was deleted.

150 changes: 0 additions & 150 deletions crates/eth_compatibility/src/lib.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ ink_storage = { version = "3.0.1", path = "../storage", default-features = false
ink_primitives = { version = "3.0.1", path = "../primitives", default-features = false }
ink_metadata = { version = "3.0.1", path = "../metadata", default-features = false, optional = true }
ink_prelude = { version = "3.0.1", path = "../prelude", default-features = false }
ink_eth_compatibility = { version = "3.0.1", path = "../eth_compatibility", default-features = false }
ink_lang_macro = { version = "3.0.1", path = "macro", default-features = false }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive", "full"] }
Expand Down
12 changes: 9 additions & 3 deletions crates/lang/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use ink_env::{
Error,
Result,
};
use ink_eth_compatibility::ECDSAPublicKey;

/// The API behind the `self.env()` and `Self::env()` syntax in ink!.
///
Expand Down Expand Up @@ -831,10 +830,17 @@ where
self,
signature: &[u8; 65],
message_hash: &[u8; 32],
) -> Result<ECDSAPublicKey> {
) -> Result<[u8; 33]> {
let mut output = [0; 33];
ink_env::ecdsa_recover(signature, message_hash, &mut output)
.map(|_| output.into())
.map(|_| output)
.map_err(|_| Error::EcdsaRecoveryFailed)
}

pub fn ecdsa_to_eth_address(self, pubkey: &[u8; 33]) -> Result<[u8; 20]> {
athei marked this conversation as resolved.
Show resolved Hide resolved
let mut output = [0; 20];
ink_env::ecdsa_to_eth_address(pubkey, &mut output)
.map(|_| output)
.map_err(|_| Error::EcdsaRecoveryFailed)
}

Expand Down