2017-05-02 22:34:12 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity 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.
|
|
|
|
|
|
|
|
solidity 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 solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @date 2017
|
|
|
|
* Metadata processing helpers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <libdevcore/JSON.h>
|
2019-02-05 22:04:35 +00:00
|
|
|
#include <test/Metadata.h>
|
2017-05-02 22:34:12 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
2019-02-05 22:37:02 +00:00
|
|
|
bytes bytecodeSansMetadata(bytes const& _bytecode)
|
|
|
|
{
|
|
|
|
unsigned size = _bytecode.size();
|
|
|
|
if (size < 5)
|
|
|
|
return bytes{};
|
|
|
|
size_t metadataSize = (_bytecode[size - 2] << 8) + _bytecode[size - 1];
|
2019-02-05 22:57:25 +00:00
|
|
|
if (size < (metadataSize + 2))
|
|
|
|
return bytes{};
|
|
|
|
// Sanity check: assume the first byte is a fixed-size CBOR array with either 1 or 2 entries
|
|
|
|
unsigned char firstByte = _bytecode[size - metadataSize - 2];
|
|
|
|
if (firstByte != 0xa1 && firstByte != 0xa2)
|
2019-02-05 22:37:02 +00:00
|
|
|
return bytes{};
|
|
|
|
return bytes(_bytecode.begin(), _bytecode.end() - metadataSize - 2);
|
|
|
|
}
|
|
|
|
|
2017-05-02 22:34:12 +00:00
|
|
|
string bytecodeSansMetadata(string const& _bytecode)
|
|
|
|
{
|
2019-02-05 22:53:25 +00:00
|
|
|
return toHex(bytecodeSansMetadata(fromHex(_bytecode, WhenError::Throw)));
|
2017-05-02 22:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isValidMetadata(string const& _metadata)
|
|
|
|
{
|
|
|
|
Json::Value metadata;
|
2018-02-07 01:05:20 +00:00
|
|
|
if (!jsonParseStrict(_metadata, metadata))
|
2017-05-02 22:34:12 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (
|
|
|
|
!metadata.isObject() ||
|
|
|
|
!metadata.isMember("version") ||
|
|
|
|
!metadata.isMember("language") ||
|
|
|
|
!metadata.isMember("compiler") ||
|
|
|
|
!metadata.isMember("settings") ||
|
|
|
|
!metadata.isMember("sources") ||
|
2018-02-22 13:47:36 +00:00
|
|
|
!metadata.isMember("output") ||
|
|
|
|
!metadata["settings"].isMember("evmVersion")
|
2017-05-02 22:34:12 +00:00
|
|
|
)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!metadata["version"].isNumeric() || metadata["version"] != 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!metadata["language"].isString() || metadata["language"].asString() != "Solidity")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/// @TODO add more strict checks
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} // end namespaces
|