diff --git a/substrate/demo/runtime/src/lib.rs b/substrate/demo/runtime/src/lib.rs index 5c630342e0..838fb9c330 100644 --- a/substrate/demo/runtime/src/lib.rs +++ b/substrate/demo/runtime/src/lib.rs @@ -204,28 +204,9 @@ impl treasury::Trait for Runtime { /// Treasury module for this concrete runtime. pub type Treasury = treasury::Module; -/// Address calculated from the code (of the constructor), input data to the constructor -/// and account id which requested the account creation. -/// -/// Formula: `blake2_256(blake2_256(code) + blake2_256(data) + origin)` -pub struct DetermineContractAddress; -impl contract::ContractAddressFor for DetermineContractAddress { - fn contract_address_for(code: &[u8], data: &[u8], origin: &AccountId) -> AccountId { - use runtime_primitives::traits::Hash; - - let code_hash = BlakeTwo256::hash(code); - let data_hash = BlakeTwo256::hash(data); - let mut buf = [0u8, 32 + 32 + 32]; - &mut buf[0..32].copy_from_slice(&code_hash); - &mut buf[32..64].copy_from_slice(&data_hash); - &mut buf[64..96].copy_from_slice(origin); - AccountId::from(BlakeTwo256::hash(&buf[..])) - } -} - impl contract::Trait for Runtime { type Gas = u64; - type DetermineContractAddress = DetermineContractAddress; + type DetermineContractAddress = contract::SimpleAddressDeterminator; } /// Contract module for this concrete runtime. diff --git a/substrate/substrate/runtime/contract/src/lib.rs b/substrate/substrate/runtime/contract/src/lib.rs index 8114d26516..34acb7dcc0 100644 --- a/substrate/substrate/runtime/contract/src/lib.rs +++ b/substrate/substrate/runtime/contract/src/lib.rs @@ -103,8 +103,9 @@ use account_db::{AccountDb, OverlayAccountDb}; use double_map::StorageDoubleMap; use rstd::prelude::*; +use rstd::marker::PhantomData; use codec::Codec; -use runtime_primitives::traits::{As, SimpleArithmetic, OnFinalise}; +use runtime_primitives::traits::{Hash, As, SimpleArithmetic, OnFinalise}; use runtime_support::dispatch::Result; use runtime_support::{Parameter, StorageMap, StorageValue}; use system::ensure_signed; @@ -121,6 +122,31 @@ pub trait ContractAddressFor { fn contract_address_for(code: &[u8], data: &[u8], origin: &AccountId) -> AccountId; } +/// Simple contract address determintator. +/// +/// Address calculated from the code (of the constructor), input data to the constructor +/// and account id which requested the account creation. +/// +/// Formula: `blake2_256(blake2_256(code) + blake2_256(data) + origin)` +pub struct SimpleAddressDeterminator(PhantomData); + +impl ContractAddressFor for SimpleAddressDeterminator +where + T::AccountId: From + AsRef<[u8]> +{ + fn contract_address_for(code: &[u8], data: &[u8], origin: &T::AccountId) -> T::AccountId { + let code_hash = T::Hashing::hash(code); + let data_hash = T::Hashing::hash(data); + + let mut buf = Vec::new(); + buf.extend_from_slice(code_hash.as_ref()); + buf.extend_from_slice(data_hash.as_ref()); + buf.extend_from_slice(origin.as_ref()); + + T::Hashing::hash(&buf[..]).into() + } +} + decl_module! { /// Contracts module. pub struct Module for enum Call where origin: T::Origin {