Skip to content

Commit

Permalink
Merge branch 'master' into cmichi-use-new-mapping-api-functions-in-ex…
Browse files Browse the repository at this point in the history
…amples
  • Loading branch information
cmichi committed May 13, 2022
2 parents 64c80b1 + 90c7327 commit a27a53b
Show file tree
Hide file tree
Showing 38 changed files with 273 additions and 336 deletions.
4 changes: 2 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ spellcheck:
<<: *docker-env
<<: *test-refs
script:
- cargo spellcheck check -vvvv --cfg=.config/cargo_spellcheck.toml --checkers hunspell --code 1 -- recursive .
- cargo spellcheck check -vvvv --cfg=.config/cargo_spellcheck.toml --checkers hunspell --code 1 -- recursive ./examples/
- cargo spellcheck check -v --cfg=.config/cargo_spellcheck.toml --checkers hunspell --code 1 -- recursive .
- cargo spellcheck check -v --cfg=.config/cargo_spellcheck.toml --checkers hunspell --code 1 -- recursive ./examples/
allow_failure: true

fmt:
Expand Down
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
28 changes: 28 additions & 0 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,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 the ECDSA public key cannot be recovered from the provided public key.
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 @@ -253,6 +253,14 @@ pub trait EnvBackend {
output: &mut [u8; 33],
) -> Result<()>;

/// Retrieves an Ethereum address from the ECDSA compressed `pubkey`
/// 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
4 changes: 2 additions & 2 deletions crates/env/src/call/execution_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl ExecutionInput<EmptyArgumentList> {
}
}

impl<'a, Head, Rest> ExecutionInput<ArgumentList<Argument<Head>, Rest>> {
impl<Head, Rest> ExecutionInput<ArgumentList<Argument<Head>, Rest>> {
/// Pushes an argument to the execution input.
#[inline]
pub fn push_arg<T>(self, arg: T) -> ExecutionInput<ArgsList<T, ArgsList<Head, Rest>>>
Expand Down Expand Up @@ -167,7 +167,7 @@ impl scale::Encode for EmptyArgumentList {
fn encode_to<O: scale::Output + ?Sized>(&self, _output: &mut O) {}
}

impl<'a, Head, Rest> scale::Encode for ArgumentList<Argument<Head>, Rest>
impl<Head, Rest> scale::Encode for ArgumentList<Argument<Head>, Rest>
where
Head: scale::Encode,
Rest: scale::Encode,
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 @@ -291,6 +291,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
51 changes: 33 additions & 18 deletions crates/env/src/engine/on_chain/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ mod sys {
output_len_ptr: Ptr32Mut<u32>,
) -> ReturnCode;

pub fn seal_contains_storage(key_ptr: Ptr32<[u8]>) -> ReturnCode;

pub fn seal_clear_storage(key_ptr: Ptr32<[u8]>);

pub fn seal_call_chain_extension(
Expand Down Expand Up @@ -298,6 +300,19 @@ mod sys {

pub fn seal_caller_is_origin() -> ReturnCode;

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

pub fn seal_code_hash(
account_id_ptr: Ptr32<[u8]>,
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
) -> ReturnCode;

pub fn seal_own_code_hash(
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
);

#[cfg(feature = "ink-debug")]
pub fn seal_debug_message(str_ptr: Ptr32<[u8]>, str_len: u32) -> ReturnCode;

Expand Down Expand Up @@ -346,6 +361,12 @@ mod sys {
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
) -> ReturnCode;

pub fn seal_set_storage(
key_ptr: Ptr32<[u8]>,
value_ptr: Ptr32<[u8]>,
value_len: u32,
) -> ReturnCode;
}

#[link(wasm_import_module = "__unstable__")]
Expand All @@ -358,25 +379,9 @@ mod sys {
output_ptr: Ptr32Mut<[u8]>,
) -> ReturnCode;

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

pub fn seal_code_hash(
account_id_ptr: Ptr32<[u8]>,
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
) -> ReturnCode;

pub fn seal_own_code_hash(
pub fn seal_ecdsa_to_eth_address(
public_key_ptr: Ptr32<[u8]>,
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
);

pub fn seal_contains_storage(key_ptr: Ptr32<[u8]>) -> ReturnCode;

pub fn seal_set_storage(
key_ptr: Ptr32<[u8]>,
value_ptr: Ptr32<[u8]>,
value_len: u32,
) -> 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
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 @@ -295,6 +295,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.

0 comments on commit a27a53b

Please sign in to comment.