forked from argotorg/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadata.cpp
More file actions
84 lines (69 loc) · 2.18 KB
/
Copy pathMetadata.cpp
File metadata and controls
84 lines (69 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
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>
#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") ||
!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