/src/solidity/libsolutil/FunctionSelector.h
Line | Count | Source |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | |
19 | | #pragma once |
20 | | |
21 | | #include <libsolutil/Keccak256.h> |
22 | | #include <libsolutil/FixedHash.h> |
23 | | |
24 | | #include <string> |
25 | | |
26 | | namespace solidity::util |
27 | | { |
28 | | |
29 | | /// @returns the ABI selector for a given function signature, as a 32 bit number. |
30 | | inline uint32_t selectorFromSignature32(std::string const& _signature) |
31 | 46.4k | { |
32 | 46.4k | return uint32_t(FixedHash<4>::Arith(util::FixedHash<4>(util::keccak256(_signature)))); |
33 | 46.4k | } |
34 | | |
35 | | /// @returns the ABI selector for a given function signature, as a u256 (left aligned) number. |
36 | | inline u256 selectorFromSignature(std::string const& _signature) |
37 | 17.6k | { |
38 | 17.6k | return u256(selectorFromSignature32(_signature)) << (256 - 32); |
39 | 17.6k | } |
40 | | |
41 | | |
42 | | } |