solidity/libsolutil/CommonData.cpp

233 lines
5.6 KiB
C++
Raw Normal View History

Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
/*
This file is part of solidity.
Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
solidity is free software: you can redistribute it and/or modify
Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
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.
solidity is distributed in the hope that it will be useful,
Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
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 solidity. If not, see <http://www.gnu.org/licenses/>.
Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
*/
/** @file CommonData.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#include <libsolutil/CommonData.h>
#include <libsolutil/Exceptions.h>
#include <libsolutil/Assertions.h>
#include <libsolutil/Keccak256.h>
#include <libsolutil/FixedHash.h>
2017-01-24 22:36:07 +00:00
#include <boost/algorithm/string.hpp>
Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
using namespace std;
2019-12-11 16:31:36 +00:00
using namespace solidity;
using namespace solidity::util;
Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
2019-11-25 17:16:35 +00:00
namespace
{
static char const* upperHexChars = "0123456789ABCDEF";
static char const* lowerHexChars = "0123456789abcdef";
}
2019-12-11 16:31:36 +00:00
string solidity::util::toHex(uint8_t _data, HexCase _case)
2019-11-25 17:16:35 +00:00
{
assertThrow(_case != HexCase::Mixed, BadHexCase, "Mixed case can only be used for byte arrays.");
char const* chars = _case == HexCase::Upper ? upperHexChars : lowerHexChars;
return std::string{
chars[(unsigned(_data) / 16) & 0xf],
chars[unsigned(_data) & 0xf]
};
}
2019-12-11 16:31:36 +00:00
string solidity::util::toHex(bytes const& _data, HexPrefix _prefix, HexCase _case)
2018-12-05 19:35:45 +00:00
{
if (_data.empty())
return {};
2019-11-25 17:16:35 +00:00
std::string ret(_data.size() * 2 + (_prefix == HexPrefix::Add ? 2 : 0), 0);
size_t i = 0;
2018-12-05 21:13:41 +00:00
if (_prefix == HexPrefix::Add)
2019-11-25 17:16:35 +00:00
{
ret[i++] = '0';
ret[i++] = 'x';
}
2018-12-05 21:13:41 +00:00
2019-11-25 17:16:35 +00:00
// Mixed case will be handled inside the loop.
char const* chars = _case == HexCase::Upper ? upperHexChars : lowerHexChars;
size_t rix = _data.size() - 1;
2018-12-05 19:35:45 +00:00
for (uint8_t c: _data)
{
// switch hex case every four hexchars
2019-11-25 17:16:35 +00:00
if (_case == HexCase::Mixed)
chars = (rix-- & 2) == 0 ? lowerHexChars : upperHexChars;
2018-12-05 19:35:45 +00:00
2019-11-25 17:16:35 +00:00
ret[i++] = chars[(unsigned(c) / 16) & 0xf];
ret[i++] = chars[unsigned(c) & 0xf];
2018-12-05 19:35:45 +00:00
}
2019-11-25 17:16:35 +00:00
assertThrow(i == ret.size(), Exception, "");
2018-12-05 19:35:45 +00:00
2019-11-25 17:16:35 +00:00
return ret;
2018-12-05 19:35:45 +00:00
}
2019-12-11 16:31:36 +00:00
int solidity::util::fromHex(char _i, WhenError _throw)
Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
{
if (_i >= '0' && _i <= '9')
return _i - '0';
if (_i >= 'a' && _i <= 'f')
return _i - 'a' + 10;
if (_i >= 'A' && _i <= 'F')
return _i - 'A' + 10;
if (_throw == WhenError::Throw)
assertThrow(false, BadHexCharacter, to_string(_i));
Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
else
return -1;
}
2019-12-11 16:31:36 +00:00
bytes solidity::util::fromHex(std::string const& _s, WhenError _throw)
Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
{
if (_s.empty())
return {};
Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
unsigned s = (_s.size() >= 2 && _s[0] == '0' && _s[1] == 'x') ? 2 : 0;
std::vector<uint8_t> ret;
ret.reserve((_s.size() - s + 1) / 2);
if (_s.size() % 2)
{
int h = fromHex(_s[s++], _throw);
Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
if (h != -1)
ret.push_back(h);
else
return bytes();
}
for (unsigned i = s; i < _s.size(); i += 2)
{
int h = fromHex(_s[i], _throw);
int l = fromHex(_s[i + 1], _throw);
Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
if (h != -1 && l != -1)
ret.push_back((uint8_t)(h * 16 + l));
Make the Solidity repository standalone. This commit is the culmination of several months of work to decouple Solidity from the webthree-umbrella so that it can be developed in parallel with cpp-ethereum (the Ethereum C++ runtime) and so that even for the Solidity unit-tests there is no hard-dependency onto the C++ runtime. The Tests-over-IPC refactoring was a major step in the same process which was already committed. This commit contains the following changes: - A subset of the CMake functionality in webthree-helpers was extracted and tailored for Solidity into ./cmake. Further cleanup is certainly possible. - A subset of the libdevcore functionality in libweb3core was extracted and tailored for Solidity into ./libdevcore. Further cleanup is certainly possible - The gas price constants in EVMSchedule were orphaned into libevmasm. - Some other refactorings and cleanups were made to sever unnecessary EVM dependencies in the Solidity unit-tests. - TravisCI and Appveyor support was added, covering builds and running of the unit-tests (Linux and macOS only for now) - A bug-fix was made to get the Tests-over-IPC running on macOS. - There are still reliability issues in the unit-tests, which need immediate attention. The Travis build has been flipped to run the unit-tests 5 times, to try to flush these out. - The Emscripten automation which was previously in webthree-umbrella was merged into the TravisCI automation here. - The development ZIP deployment step has been commented out, but we will want to read that ONLY for release branch. Further iteration on these changes will definitely be needed, but I feel these have got to sufficient maturity than holding them back further isn't winning us anything. It is go time :-)
2016-08-01 05:25:37 +00:00
else
return bytes();
}
return ret;
}
2017-01-24 22:36:07 +00:00
2019-12-11 16:31:36 +00:00
bool solidity::util::passesAddressChecksum(string const& _str, bool _strict)
2017-01-24 22:36:07 +00:00
{
2018-10-09 07:12:04 +00:00
string s = _str.substr(0, 2) == "0x" ? _str : "0x" + _str;
2017-01-24 22:36:07 +00:00
2018-10-09 07:12:04 +00:00
if (s.length() != 42)
2017-01-24 22:36:07 +00:00
return false;
if (!_strict && (
2018-10-09 07:12:04 +00:00
s.find_first_of("abcdef") == string::npos ||
s.find_first_of("ABCDEF") == string::npos
2017-01-24 22:36:07 +00:00
))
return true;
2019-12-11 16:31:36 +00:00
return s == solidity::util::getChecksummedAddress(s);
}
2019-12-11 16:31:36 +00:00
string solidity::util::getChecksummedAddress(string const& _addr)
{
string s = _addr.substr(0, 2) == "0x" ? _addr.substr(2) : _addr;
assertThrow(s.length() == 40, InvalidAddress, "");
assertThrow(s.find_first_not_of("0123456789abcdefABCDEF") == string::npos, InvalidAddress, "");
2017-01-24 22:36:07 +00:00
h256 hash = keccak256(boost::algorithm::to_lower_copy(s, std::locale::classic()));
string ret = "0x";
for (size_t i = 0; i < 40; ++i)
2017-01-24 22:36:07 +00:00
{
char addressCharacter = s[i];
unsigned nibble = (unsigned(hash[i / 2]) >> (4 * (1 - (i % 2)))) & 0xf;
if (nibble >= 8)
ret += toupper(addressCharacter);
else
ret += tolower(addressCharacter);
2017-01-24 22:36:07 +00:00
}
return ret;
2017-01-24 22:36:07 +00:00
}
2019-12-11 16:31:36 +00:00
bool solidity::util::isValidHex(string const& _string)
{
if (_string.substr(0, 2) != "0x")
return false;
if (_string.find_first_not_of("0123456789abcdefABCDEF", 2) != string::npos)
return false;
return true;
}
2019-12-11 16:31:36 +00:00
bool solidity::util::isValidDecimal(string const& _string)
{
if (_string.empty())
return false;
if (_string == "0")
return true;
// No leading zeros
if (_string.front() == '0')
return false;
if (_string.find_first_not_of("0123456789") != string::npos)
return false;
return true;
}
2019-12-11 16:31:36 +00:00
string solidity::util::formatAsStringOrNumber(string const& _value)
{
assertThrow(_value.length() <= 32, StringTooLong, "String to be formatted longer than 32 bytes.");
for (auto const& c: _value)
if (c <= 0x1f || c >= 0x7f || c == '"')
return "0x" + h256(_value, h256::AlignLeft).hex();
2020-05-07 17:19:54 +00:00
return escapeAndQuoteString(_value);
}
string solidity::util::escapeAndQuoteString(string const& _input)
{
string out;
for (char c: _input)
if (c == '\\')
out += "\\\\";
else if (c == '"')
out += "\\\"";
else if (c == '\b')
out += "\\b";
else if (c == '\f')
out += "\\f";
else if (c == '\n')
out += "\\n";
else if (c == '\r')
out += "\\r";
else if (c == '\t')
out += "\\t";
else if (c == '\v')
out += "\\v";
else if (!isprint(c, locale::classic()))
{
ostringstream o;
o << "\\x" << std::hex << setfill('0') << setw(2) << (unsigned)(unsigned char)(c);
out += o.str();
}
else
out += c;
return "\"" + std::move(out) + "\"";
}