Skip to content

Commit

Permalink
ecdsa_to_default_account_id func ported from ink_eth_compatibility crate
Browse files Browse the repository at this point in the history
  • Loading branch information
agryaznov committed May 11, 2022
1 parent fa5c856 commit 70a9189
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
13 changes: 13 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 @@ -627,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.
/// 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].
pub fn ecdsa_to_default_account_id(pubkey: &[u8; 33]) -> AccountId {
<EnvInstance as OnInstance>::on_instance(|instance| {
instance.ecdsa_to_default_account_id(pubkey)
})
}
9 changes: 9 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 @@ -303,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.
/// 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
7 changes: 7 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 @@ -342,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
16 changes: 16 additions & 0 deletions crates/env/src/engine/on_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,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 Expand Up @@ -552,4 +558,14 @@ impl TypedEnvBackend for EnvInstance {
let hash = scale::Decode::decode(&mut &output[..])?;
Ok(hash)
}

fn ecdsa_to_default_account_id<E, H>(&mut self, pubkey: &[u8; 33]) -> E::AccountId
where
H: CryptoHash,
E: Environment,
{
let mut output = <hash::Blake2x256 as hash::HashOutput>::Type::default();
<H as CryptoHash>::hash(pubkey[..], &mut output);
output.into()
}
}
11 changes: 11 additions & 0 deletions crates/lang/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use ink_env::{
CryptoHash,
HashOutput,
},
AccountId,
Environment,
Error,
Result,
Expand Down Expand Up @@ -1013,4 +1014,14 @@ where
pub fn own_code_hash(self) -> Result<E::Hash> {
ink_env::own_code_hash::<E>()
}

/// Returns the default Substrate's `AccountId` from the ECDSA compressed public key.
/// It hashes the compressed public key with the `blake2b_256` algorithm like in substrate.
///
/// # Note
///
/// For more details visit: [`ink_env::ecdsa_to_default_account_id`]
pub fn ecdsa_to_default_account_id(self, pubkey: &[u8; 33]) -> AccountId {
ink_env::ecdsa_to_default_account_id(pubkey)
}
}

0 comments on commit 70a9189

Please sign in to comment.