Ethereum  PoC-8
The C++ Implementation of Ethereum
CommonJS.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum 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  cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #pragma once
19 
20 #include "CommonData.h"
21 #include "CommonIO.h"
22 #include "FixedHash.h"
23 
24 #include <string>
25 
26 namespace dev
27 {
28 inline std::string toJS(byte _b)
29 {
30  return "0x" + std::to_string(_b);
31 }
32 
33 template <unsigned S> std::string toJS(FixedHash<S> const& _h)
34 {
35  return toHexPrefixed(_h.ref());
36 }
37 
38 template <unsigned N> std::string toJS(boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N, N, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>> const& _n)
39 {
40  std::string h = toHex(toCompactBigEndian(_n, 1));
41  // remove first 0, if it is necessary;
42  std::string res = h[0] != '0' ? h : h.substr(1);
43  return "0x" + res;
44 }
45 
46 inline std::string toJS(bytes const& _n, std::size_t _padding = 0)
47 {
48  if (_n.empty())
49  return {};
50 
51  bytes n = _n;
52  n.resize(std::max<unsigned>(n.size(), _padding));
53  return toHexPrefixed(n);
54 }
55 
56 template<unsigned T> std::string toJS(SecureFixedHash<T> const& _i)
57 {
58  std::stringstream stream;
59  stream << "0x" << _i.makeInsecure().hex();
60  return stream.str();
61 }
62 
63 template<typename T> std::string toJS(T const& _i)
64 {
65  std::stringstream stream;
66  stream << "0x" << std::hex << _i;
67  return stream.str();
68 }
69 
70 enum class OnFailed { InterpretRaw, Empty, Throw };
71 
74 bytes jsToBytes(std::string const& _s, OnFailed _f = OnFailed::Empty);
76 bytes padded(bytes _b, unsigned _l);
78 bytes paddedRight(bytes _b, unsigned _l);
80 bytes unpadded(bytes _s);
84 std::string fromRaw(h256 _n);
85 
86 template <unsigned N> FixedHash<N> jsToFixed(std::string const& _s)
87 {
88  if (_s.substr(0, 2) == "0x")
89  // Hex
90  return FixedHash<N>(_s.substr(2 + std::max<unsigned>(N * 2, _s.size() - 2) - N * 2));
91  else if (_s.find_first_not_of("0123456789") == std::string::npos)
92  // Decimal
93  return (typename FixedHash<N>::Arith)(_s);
94  else
95  // Binary
96  return FixedHash<N>(); // FAIL
97 }
98 
99 template <unsigned N> boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N * 8, N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>> jsToInt(std::string const& _s)
100 {
101  if (_s.substr(0, 2) == "0x")
102  // Hex
103  return fromBigEndian<boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N * 8, N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>>(fromHex(_s.substr(2)));
104  else if (_s.find_first_not_of("0123456789") == std::string::npos)
105  // Decimal
106  return boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N * 8, N * 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>(_s);
107  else
108  // Binary
109  return 0; // FAIL
110 }
111 
112 inline u256 jsToU256(std::string const& _s) { return jsToInt<32>(_s); }
113 
117 inline int jsToInt(std::string const& _s)
118 {
119  int ret = 0;
120  DEV_IGNORE_EXCEPTIONS(ret = std::stoi(_s, nullptr, 0));
121  return ret;
122 }
123 
124 inline std::string jsToDecimal(std::string const& _s)
125 {
126  return toString(jsToU256(_s));
127 }
128 
129 }
dev::fromRaw
string fromRaw(h256 _n)
Convert h256 into user-readable string (by directly using std::string constructor)....
Definition: CommonJS.cpp:81
dev::FixedHash::Arith
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< N *8, N *8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > Arith
The corresponding arithmetic type.
Definition: FixedHash.h:50
dev::SecureFixedHash
Definition: FixedHash.h:233
FixedHash.h
dev::FixedHash::ref
bytesRef ref()
Definition: FixedHash.h:132
dev::toHexPrefixed
std::string toHexPrefixed(T const &_data)
Definition: CommonData.h:72
dev::OnFailed::InterpretRaw
@ InterpretRaw
dev::toString
std::string toString(std::chrono::time_point< T > const &_e, std::string const &_format="%F %T")
Definition: CommonIO.h:86
dev::FixedHash
Definition: FixedHash.h:47
dev::padded
bytes padded(bytes _b, unsigned _l)
Add '0' on, or remove items from, the front of _b until it is of length _l.
Definition: CommonJS.cpp:47
dev::jsToInt
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< N *8, N *8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > jsToInt(std::string const &_s)
Definition: CommonJS.h:99
dev::paddedRight
bytes paddedRight(bytes _b, unsigned _l)
Add '0' on, or remove items from, the back of _b until it is of length _l.
Definition: CommonJS.cpp:54
dev::SecureFixedHash::makeInsecure
FixedHash< T > const & makeInsecure() const
Definition: FixedHash.h:263
CommonIO.h
dev::jsToDecimal
std::string jsToDecimal(std::string const &_s)
Definition: CommonJS.h:124
dev::OnFailed::Throw
@ Throw
dev::OnFailed::Empty
@ Empty
dev::jsToFixed
FixedHash< N > jsToFixed(std::string const &_s)
Definition: CommonJS.h:86
dev::toCompactBigEndian
bytes toCompactBigEndian(T _val, unsigned _min=0)
Definition: CommonData.h:155
dev::OnFailed
OnFailed
Definition: CommonJS.h:70
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::jsToBytes
bytes jsToBytes(string const &_s, OnFailed _f)
Definition: CommonJS.cpp:31
CommonData.h
dev::u256
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition: Common.h:121
dev
Definition: Address.cpp:21
dev::FixedHash::hex
std::string hex() const
Definition: FixedHash.h:129
dev::jsToU256
u256 jsToU256(std::string const &_s)
Definition: CommonJS.h:112
dev::toJS
std::string toJS(byte _b)
Definition: CommonJS.h:28
DEV_IGNORE_EXCEPTIONS
#define DEV_IGNORE_EXCEPTIONS(X)
Definition: Common.h:59
dev::toHex
std::string toHex(Iterator _it, Iterator _end, std::string const &_prefix)
Definition: CommonData.h:46
dev::unpadLeft
bytes unpadLeft(bytes _b)
Remove all 0 byte on the head of _s.
Definition: CommonJS.cpp:67
dev::unpadded
bytes unpadded(bytes _b)
Removing all trailing '0'. Returns empty array if input contains only '0' char.
Definition: CommonJS.cpp:60
dev::fromHex
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
Definition: CommonData.cpp:81