Unverified Commit ef3b1375 authored by Hero Bird's avatar Hero Bird Committed by GitHub

Remove ink_primitives::hash module (#450)

* [primitives, lang/macro] remove ink_primitives::hash module

# Conflicts:
#	primitives/src/lib.rs

* [core] remove ink_core's Selector::{from_str, from<&[u8]>} constructors

* [examples] fix DNS contract

* [examples] fix ers20

* [examples] fix other example smart contracts

* [examples] apply rustfmt
parent 921d3c62
Pipeline #97072 failed with stages
in 7 minutes and 30 seconds
......@@ -33,27 +33,7 @@ pub struct Selector {
bytes: [u8; 4],
}
impl<'a> From<&'a [u8]> for Selector {
/// Computes the selector from the given input bytes.
///
/// # Note
///
/// Normally this is invoked through `Selector::from_str`.
fn from(input: &'a [u8]) -> Self {
let keccak = ink_primitives::hash::keccak256(input);
Self {
bytes: [keccak[0], keccak[1], keccak[2], keccak[3]],
}
}
}
impl Selector {
/// Returns the selector for the given name.
#[allow(clippy::should_implement_trait)]
pub fn from_str(name: &str) -> Self {
From::from(name.as_bytes())
}
/// Creates a selector directly from 4 bytes.
pub const fn new(bytes: [u8; 4]) -> Self {
Self { bytes }
......
......@@ -202,13 +202,16 @@ impl EnvInstance {
CallData,
Selector,
};
// The below selector bytes are incorrect but since calling doesn't work
// yet we do not have to fix this now.
let selector_bytes_for_call = [0x00; 4];
self.exec_context.push(
ExecContext::build::<T>()
.caller(default_accounts.alice)
.callee(contract_account_id)
.gas(T::Balance::from(500_000))
.transferred_value(T::Balance::from(500))
.call_data(CallData::new(Selector::from_str("call")))
.call_data(CallData::new(Selector::new(selector_bytes_for_call)))
.finish(),
);
Ok(())
......
......@@ -213,7 +213,7 @@ mod dns {
AccountId::from(DEFAULT_CALLEE_HASH),
DEFAULT_ENDOWMENT,
DEFAULT_GAS_LIMIT,
env::call::CallData::new(env::call::Selector::from_str("")),
env::call::CallData::new(env::call::Selector::new([0x00; 4])),
)
}
......
......@@ -260,7 +260,7 @@ mod erc20 {
env::account_id::<env::DefaultEnvTypes>().unwrap_or([0x0; 32].into());
// Create call
let mut data =
env::call::CallData::new(env::call::Selector::from_str("balance_of"));
env::call::CallData::new(env::call::Selector::new([0x00; 4]));
data.push_arg(&accounts.bob);
// Push the new execution context to set Bob as caller
assert_eq!(
......@@ -306,7 +306,7 @@ mod erc20 {
env::account_id::<env::DefaultEnvTypes>().unwrap_or([0x0; 32].into());
// Create call.
let mut data =
env::call::CallData::new(env::call::Selector::from_str("balance_of"));
env::call::CallData::new(env::call::Selector::new([0x00; 4]));
data.push_arg(&accounts.bob);
// Push the new execution context to set Bob as caller.
assert_eq!(
......
......@@ -525,7 +525,7 @@ mod erc721 {
env::account_id::<env::DefaultEnvTypes>().unwrap_or([0x0; 32].into());
// Create call
let mut data =
env::call::CallData::new(env::call::Selector::from_str("balance_of"));
env::call::CallData::new(env::call::Selector::new([0x00; 4]));
data.push_arg(&accounts.bob);
// Push the new execution context to set Bob as caller
assert_eq!(
......@@ -561,7 +561,7 @@ mod erc721 {
env::account_id::<env::DefaultEnvTypes>().unwrap_or([0x0; 32].into());
// Create call
let mut data =
env::call::CallData::new(env::call::Selector::from_str("balance_of"));
env::call::CallData::new(env::call::Selector::new([0x00; 4]));
data.push_arg(&accounts.bob);
// Push the new execution context to set Bob as caller
assert_eq!(
......@@ -615,7 +615,7 @@ mod erc721 {
env::account_id::<env::DefaultEnvTypes>().unwrap_or([0x0; 32].into());
// Create call
let mut data =
env::call::CallData::new(env::call::Selector::from_str("balance_of"));
env::call::CallData::new(env::call::Selector::new([0x00; 4]));
data.push_arg(&accounts.bob);
// Push the new execution context to set Bob as caller
assert_eq!(
......@@ -678,7 +678,7 @@ mod erc721 {
env::account_id::<env::DefaultEnvTypes>().unwrap_or([0x0; 32].into());
// Create call
let mut data =
env::call::CallData::new(env::call::Selector::from_str("balance_of"));
env::call::CallData::new(env::call::Selector::new([0x00; 4]));
data.push_arg(&accounts.bob);
// Push the new execution context to set Eve as caller
assert_eq!(
......
......@@ -672,8 +672,7 @@ mod multisig_plain {
impl Transaction {
fn change_requirement(requirement: u32) -> Self {
let mut call =
call::CallData::new(call::Selector::from_str("change_requirement"));
let mut call = call::CallData::new(call::Selector::new([0x00; 4])); // change_requirement
call.push_arg(&requirement);
Self {
callee: WALLET.into(),
......@@ -691,7 +690,7 @@ mod multisig_plain {
WALLET.into(),
1000000,
1000000,
call::CallData::new(call::Selector::from_str("dummy")),
call::CallData::new(call::Selector::new([0x00; 4])),
);
}
......
......@@ -28,6 +28,7 @@ serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = "1.0"
derive_more = { version = "0.99", default-features = false, features = ["from"] }
regex = "1.3"
sha3 = "0.8"
[dev-dependencies]
ink_abi = { version = "2.1.0", path = "../../abi/" }
......
......@@ -366,8 +366,11 @@ impl From<&'_ Ident> for FunctionSelector {
impl From<&'_ str> for FunctionSelector {
fn from(name: &str) -> Self {
let sha3_hash = ink_primitives::hash::keccak256(name.as_bytes());
Self([sha3_hash[0], sha3_hash[1], sha3_hash[2], sha3_hash[3]])
use sha3::digest::Digest as _;
let mut hasher = sha3::Keccak256::default();
hasher.input(name.as_bytes());
let hash = hasher.result();
Self([hash[0], hash[1], hash[2], hash[3]])
}
}
......
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Hashing utilities around the Keccak256 hasher.
use core::hash::{
Hash,
Hasher,
};
use tiny_keccak::Hasher as TinyKeccakHasher;
/// Keccak256 hasher.
#[derive(Clone)]
pub struct Keccak256Hasher {
/// The internal keccak hasher.
hasher: tiny_keccak::Keccak,
}
impl Default for Keccak256Hasher {
fn default() -> Self {
Keccak256Hasher {
hasher: tiny_keccak::Keccak::v256(),
}
}
}
impl Keccak256Hasher {
/// Returns the hash value for the values written so far.
///
/// If you need to start a fresh hash value, you will have to create a new hasher.
pub fn finish256(self) -> [u8; 32] {
let mut res = [0; 32];
self.hasher.finalize(&mut res);
res
}
pub fn finish64(self) -> [u8; 8] {
let mut arr = [0; 8];
let res = self.finish256();
arr[..8].copy_from_slice(&res[..8]);
arr
}
}
fn bytes8_to_u64(bytes: [u8; 8]) -> u64 {
bytes
.iter()
.enumerate()
.map(|(n, &ext)| u64::from(ext) << (n * 8))
.fold(0u64, |acc, ext| acc | ext)
}
impl Hasher for Keccak256Hasher {
/// Returns the hash value for the values written so far.
///
/// If you need to start a fresh hash value, you will have to create a new hasher.
fn finish(&self) -> u64 {
bytes8_to_u64(self.clone().finish64())
}
/// Writes some data into the hasher.
fn write(&mut self, bytes: &[u8]) {
self.hasher.update(bytes)
}
}
/// Returns the keccak-256 hash for the given byte slice.
pub fn keccak256<T>(val: &T) -> [u8; 32]
where
T: ?Sized + Hash,
{
let mut hasher = Keccak256Hasher::default();
val.hash(&mut hasher);
hasher.finish256()
}
......@@ -24,7 +24,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
mod byte_utils;
pub mod hash;
mod key;
mod key2;
mod key_ptr;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment