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 19 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
41 changes: 41 additions & 0 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use crate::{
},
topics::Topics,
types::Gas,
AccountId,
Environment,
Result,
};
Expand Down Expand Up @@ -511,6 +512,34 @@ pub fn ecdsa_recover(
})
}

/// Returns an Ethereum address from the ECDSA compressed public key.
///
/// # Example
///
/// ```
/// let pub_key = [
/// 2, 141, 181, 91, 5, 219, 134, 192, 177, 120, 108, 164, 159, 9, 93, 118,
/// 52, 76, 158, 96, 86, 178, 240, 39, 1, 167, 231, 243, 194, 10, 171, 253,
/// 145,
/// ];
/// let EXPECTED_ETH_ADDRESS = [
/// 9, 35, 29, 167, 177, 154, 1, 111, 158, 87, 109, 35, 177, 98, 119, 6, 47,
/// 77, 70, 168,
/// ];
/// let mut output = [0; 20];
/// ink_env::ecdsa_to_eth_address(&pub_key, &mut output);
/// assert_eq!(output, EXPECTED_ETH_ADDRESS);
/// ```
///
/// # Errors
///
/// - if cannot retrieve ECDSA public key from the provided input
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 Expand Up @@ -599,3 +628,15 @@ where
pub fn set_code_hash(code_hash: &[u8; 32]) -> Result<()> {
<EnvInstance as OnInstance>::on_instance(|instance| instance.set_code_hash(code_hash))
}

/// Returns the default Substrate's `AccountId` (`\[u8;32\]`) from the ECDSA compressed public key.
agryaznov marked this conversation as resolved.
Show resolved Hide resolved
/// It hashes the compressed public key with the `blake2b_256` algorithm like in substrate.
///
/// # Note
///
/// This function implies a standart `AccountId` type which is `\[u8;32\]`.
agryaznov marked this conversation as resolved.
Show resolved Hide resolved
pub fn ecdsa_to_default_account_id(pubkey: &[u8; 33]) -> AccountId {
<EnvInstance as OnInstance>::on_instance(|instance| {
instance.ecdsa_to_default_account_id(pubkey)
})
}
17 changes: 17 additions & 0 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::{
HashOutput,
},
topics::Topics,
AccountId,
Environment,
Result,
};
Expand Down Expand Up @@ -253,6 +254,14 @@ pub trait EnvBackend {
output: &mut [u8; 33],
) -> Result<()>;

/// Retrieves an 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 Expand Up @@ -295,6 +304,14 @@ pub trait EnvBackend {
///
/// - If the supplied `code_hash` cannot be found on-chain.
fn set_code_hash(&mut self, code_hash: &[u8]) -> Result<()>;

/// Returns the default Substrate's `AccountId` ([u8;32]) from the ECDSA compressed public key.
agryaznov marked this conversation as resolved.
Show resolved Hide resolved
/// It hashes the compressed public key with the `blake2b_256` algorithm like in substrate.
///
/// # Note
///
/// For more details visit: [`ecdsa_to_default_account_id`][`crate::ecdsa_to_default_account_id`]
fn ecdsa_to_default_account_id(&mut self, pubkey: &[u8; 33]) -> AccountId;
}

/// Environmental contract functionality.
Expand Down
21 changes: 21 additions & 0 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::{
Topics,
TopicsBuilderBackend,
},
AccountId,
Clear,
EnvBackend,
Environment,
Expand Down Expand Up @@ -293,6 +294,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_uncompressed();
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 Expand Up @@ -328,6 +343,12 @@ impl EnvBackend for EnvInstance {
fn set_code_hash(&mut self, _code_hash: &[u8]) -> Result<()> {
unimplemented!("off-chain environment does not support `set_code_hash`")
}

fn ecdsa_to_default_account_id(&mut self, pubkey: &[u8; 33]) -> AccountId {
let mut output = <Blake2x256 as HashOutput>::Type::default();
<Blake2x256 as CryptoHash>::hash(&pubkey[..], &mut output);
output.into()
}
}

impl TypedEnvBackend for EnvInstance {
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 @@ -378,6 +378,11 @@ mod sys {
message_hash_ptr: Ptr32<[u8]>,
output_ptr: Ptr32Mut<[u8]>,
) -> ReturnCode;

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

Expand Down Expand Up @@ -704,6 +709,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
15 changes: 15 additions & 0 deletions crates/env/src/engine/on_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::{
Topics,
TopicsBuilderBackend,
},
AccountId,
Clear,
EnvBackend,
Environment,
Expand Down Expand Up @@ -295,6 +296,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 All @@ -320,6 +329,12 @@ impl EnvBackend for EnvInstance {
fn set_code_hash(&mut self, code_hash_ptr: &[u8]) -> Result<()> {
ext::set_code_hash(code_hash_ptr).map_err(Into::into)
}

fn ecdsa_to_default_account_id(&mut self, pubkey: &[u8; 33]) -> AccountId {
let mut output = <Blake2x256 as HashOutput>::Type::default();
<Blake2x256 as CryptoHash>::hash(&pubkey[..], &mut output);
output.into()
}
}

impl TypedEnvBackend for EnvInstance {
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.

Loading