solidity/test/Metadata.cpp

85 lines
2.2 KiB
C++
Raw Normal View History

/*
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>
using namespace std;
namespace dev
{
namespace test
{
bytes bytecodeSansMetadata(bytes const& _bytecode)
{
unsigned size = _bytecode.size();
if (size < 5)
return bytes{};
size_t metadataSize = (_bytecode[size - 2] << 8) + _bytecode[size - 1];
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)
return bytes{};
return bytes(_bytecode.begin(), _bytecode.end() - metadataSize - 2);
}
string bytecodeSansMetadata(string const& _bytecode)
{
return toHex(bytecodeSansMetadata(fromHex(_bytecode, WhenError::Throw)));
}
bool isValidMetadata(string const& _metadata)
{
Json::Value metadata;
if (!jsonParseStrict(_metadata, metadata))
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")
)
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