-
Notifications
You must be signed in to change notification settings - Fork 568
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add evm-utiltity * add keccak256 macro * use keccak256 macro * fix tests * remove primitives/proc-macro * avoid additional allocation * fix tests
- Loading branch information
Showing
20 changed files
with
168 additions
and
63 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "module-evm-utiltity" | ||
version = "1.5.1" | ||
authors = ["Acala Developers"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
sha3 = { version = "0.9.1" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "module-evm-utiltity-macro" | ||
version = "1.5.1" | ||
authors = ["Acala Developers"] | ||
edition = "2018" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
quote = "1.0.10" | ||
syn = { version = "1.0.80", features = ["full", "fold", "extra-traits", "visit"] } | ||
proc-macro2 = "1.0.30" | ||
module-evm-utiltity = { path = ".." } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// This file is part of Acala. | ||
|
||
// Copyright (C) 2020-2021 Acala Foundation. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn generate_function_selector_works() { | ||
#[module_evm_utiltity_macro::generate_function_selector] | ||
#[derive(Debug, Eq, PartialEq)] | ||
#[repr(u32)] | ||
pub enum Action { | ||
Name = "name()", | ||
Symbol = "symbol()", | ||
Decimals = "decimals()", | ||
TotalSupply = "totalSupply()", | ||
BalanceOf = "balanceOf(address)", | ||
Transfer = "transfer(address,uint256)", | ||
} | ||
|
||
assert_eq!(Action::Name as u32, 0x06fdde03_u32); | ||
assert_eq!(Action::Symbol as u32, 0x95d89b41_u32); | ||
assert_eq!(Action::Decimals as u32, 0x313ce567_u32); | ||
assert_eq!(Action::TotalSupply as u32, 0x18160ddd_u32); | ||
assert_eq!(Action::BalanceOf as u32, 0x70a08231_u32); | ||
assert_eq!(Action::Transfer as u32, 0xa9059cbb_u32); | ||
} | ||
|
||
#[test] | ||
fn keccak256_works() { | ||
assert_eq!( | ||
module_evm_utiltity_macro::keccak256!(""), | ||
&module_evm_utiltity::sha3_256("") | ||
); | ||
assert_eq!( | ||
module_evm_utiltity_macro::keccak256!("keccak256"), | ||
&module_evm_utiltity::sha3_256("keccak256") | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// This file is part of Acala. | ||
|
||
// Copyright (C) 2020-2021 Acala Foundation. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
//! # Evm utiltity Module | ||
//! | ||
//! A pallet provides some utility methods. | ||
use sha3::{Digest, Keccak256}; | ||
use std::convert::TryInto; | ||
|
||
pub fn sha3_256(s: &str) -> [u8; 32] { | ||
let mut result = [0u8; 32]; | ||
|
||
// create a SHA3-256 object | ||
let mut hasher = Keccak256::new(); | ||
// write input message | ||
hasher.update(s); | ||
// read hash digest | ||
result.copy_from_slice(&hasher.finalize()[..32]); | ||
|
||
result | ||
} | ||
|
||
pub fn get_function_selector(s: &str) -> u32 { | ||
let result = sha3_256(s); | ||
u32::from_be_bytes(result[..4].try_into().unwrap()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.