9 #define ReadJsonString(json, param, result) if(json.exists(#param) && json[#param].isStr())\ 10 result.param = json[#param].get_str(); 11 #define ReadJsonBool(json, param, result) if(json.exists(#param) && json[#param].isBool())\ 12 result.param = json[#param].get_bool(); 13 #define ReadJsonArray(json, param, result) if(json.exists(#param) && json[#param].isArray())\ 14 result = json[#param].get_array(); 17 inline bool startsWithString(
const std::string& str,
const std::string& s,
size_t& pos)
19 if(pos >= str.length())
return false;
21 size_t length = s.length();
22 bool ret = (str.substr(pos, length) == s);
23 if(ret) pos += length;
29 if(pos >= str.length())
return "";
32 for(
size_t i = pos; i < str.size(); i++)
35 if(c >=
'0' && c <=
'9')
41 std::string s = ss.str();
47 const static int HEX_INSTRUCTION_SIZE = 64;
59 bool ret = json_contract.
read(json_data);
60 if(ret && json_contract.
isArray())
64 for(
size_t i = 0; i <
size; i++)
66 const UniValue& json_function = json_contract[i];
76 for(
size_t j = 0; j < json_inputs.
size(); j++)
78 const UniValue& json_param = json_inputs[j];
82 function.inputs.push_back(param);
87 for(
size_t j = 0; j < json_outputs.
size(); j++)
89 const UniValue& json_param = json_outputs[j];
94 function.outputs.push_back(param);
97 functions.push_back(
function);
102 function.
type =
"default";
103 function.payable =
true;
104 functions.push_back(
function);
115 const std::string &_type,
116 const std::vector<ParameterABI> &_inputs,
117 const std::vector<ParameterABI> &_outputs,
118 bool _payable,
bool _constant,
bool _anonymous):
125 anonymous(_anonymous)
128 bool FunctionABI::abiIn(
const std::vector<std::vector<std::string>> &values, std::string &
data, std::vector<ParameterABI::ErrorType>& errors)
const 130 bool ret =
inputs.size() == values.size();
132 std::map<int, std::string> mapDynamic;
133 for(
size_t i = 0; i <
inputs.size(); i++)
135 ret &=
inputs[i].abiIn(values[i], params, mapDynamic);
136 errors.push_back(
inputs[i].lastError());
146 bool FunctionABI::abiOut(
const std::string &
data, std::vector<std::vector<std::string>> &values, std::vector<ParameterABI::ErrorType>& errors)
const 150 for(
size_t i = 0; i <
outputs.size(); i++)
152 std::vector<std::string> value;
153 ret &=
outputs[i].abiOut(data, pos, value);
154 values.push_back(value);
155 errors.push_back(
outputs[i].lastError());
162 if(
type ==
"default")
172 std::stringstream id;
179 for(
size_t i = 1; i <
inputs.size(); i++)
181 id <<
"," <<
inputs[i].type;
184 std::string sig =
id.str();
193 hash =
dev::sha3(sig).ref().cropped(0, 4).toBytes();
206 if(in && errors.size() !=
inputs.size())
208 if(!in && errors.size() !=
outputs.size())
210 const std::vector<ParameterABI>& params = in ?
inputs :
outputs;
212 QStringList messages;
213 messages.append(QObject::tr(
"ABI parsing error:"));
214 for(
size_t i = 0; i < errors.size(); i++)
219 QString _type = QString::fromStdString(param.
type);
220 QString _name = QString::fromStdString(param.
name);
224 messages.append(QObject::tr(
"Unsupported type %1 %2.").arg(_type, _name));
227 messages.append(QObject::tr(
"Error encoding parameter %1 %2.").arg(_type, _name));
230 messages.append(QObject::tr(
"Error decoding parameter %1 %2.").arg(_type, _name));
236 return messages.join(
'\n');
241 for(
auto i = mapDynamic.begin(); i != mapDynamic.end(); i++)
244 std::string value = i->second;
248 data.replace(pos, strRef.size(), strRef);
274 value = value ==
"false" ?
"0" :
"1";
313 value = outData.str();
321 value = outData.str();
330 dev::toBigEndian<dev::u160, dev::bytes>(outData, rawAddress);
339 value = outData == 0 ?
"false" :
"true";
347 pos += HEX_INSTRUCTION_SIZE;
354 int key = data.size();
355 data += paramData.substr(0, HEX_INSTRUCTION_SIZE);
356 mapDynamic[key] = paramData.substr(HEX_INSTRUCTION_SIZE);
359 bool ParameterABI::abiIn(
const std::vector<std::string> &value, std::string &
data, std::map<int, std::string>& mapDynamic)
const 368 std::string _value = value[0];
391 std::string paramData;
393 size_t length = value.size();
395 for(
size_t i = 0; i < length; i++)
405 for(
size_t i = 0; i < length; i++)
456 pos += HEX_INSTRUCTION_SIZE;
461 std::string paramValue;
464 value.push_back(paramValue);
476 std::string paramValue;
485 size_t length =
std::atoi(paramValue.c_str());
488 for(
size_t i = 0; i < length; i++)
492 value.push_back(paramValue);
501 std::string paramValue;
505 for(
size_t i = 0; i < length; i++)
509 value.push_back(paramValue);
531 switch (paramType.
type()) {
655 if(!strWhole.empty())
664 if(!strDecimal.empty())
678 if(!strLength.empty())
string32 toString32(std::string const &_s)
std::vector< ParameterABI > outputs
void clean()
clean Set the value to the defaults
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
bool startsWithString(const std::string &str, const std::string &s, size_t &pos)
size_t decimalBits() const
decimalBits Get the number of bits for the decimal part of the number
size_t length() const
length Length of the list, applicable for list only
#define ReadJsonString(json, param, result)
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void >> s256
std::string startsWithNumber(const std::string &str, size_t &pos)
bool read(const char *raw, size_t len)
bool determine(const std::string &_type)
determine Determine the type from the string
bool isList() const
isList Is the data type list
QString errorMessage(std::vector< ParameterABI::ErrorType > &errors, bool in) const
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
bool isDynamic() const
isDynamic Check if the type is dynamic.
bool abiIn(const std::vector< std::vector< std::string >> &values, std::string &data, std::vector< ParameterABI::ErrorType > &errors) const
bool abiOut(const std::string &data, size_t &pos, std::vector< std::string > &value) const
void addDynamic(const std::string ¶mData, std::string &data, std::map< int, std::string > &mapDynamic) const
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 160, 160, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u160
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
ErrorType lastError() const
bool abiOut(const std::string &data, std::vector< std::vector< std::string >> &values, std::vector< ParameterABI::ErrorType > &errors) const
The ParameterType class Decode the api parameter type, provide more informations about the data type ...
std::vector< ParameterABI > inputs
ParameterABI(const std::string &_name="", const std::string &_type="", bool _indexed=false)
std::vector< byte > bytes
bool abiInBasic(ParameterType::Type abiType, std::string value, std::string &data) const
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
bool abiOutBasic(ParameterType::Type abiType, const std::string &data, size_t &pos, std::string &value) const
const std::string & canonical() const
canonical Get the canonical type
void processDynamicParams(const std::map< int, std::string > &mapDynamic, std::string &data) const
const ParameterType & decodeType() const
std::array< char, 32 > string32
bool abiIn(const std::vector< std::string > &value, std::string &data, std::map< int, std::string > &mapDynamic) const
s256 u2s(u256 _u)
Interprets _u as a two's complement signed number and returns the resulting s256. ...
#define ReadJsonBool(json, param, result)
static bool getRegularExpession(const ParameterType ¶mType, QRegularExpression ®Ex)
ParameterType m_decodeType
PlatformStyle::TableColorType type
uint8_t const size_t const size
size_t wholeBits() const
wholeBits Get the number of bits for the whole part of the number
ParameterType(const std::string &_type="")
ParameterType Constructor.
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
bool loads(const std::string &json_data)
size_t totalBytes() const
totalBytes Get the total size in bytes
static std::string defaultSelector()
Type
The Type enum ABI data types.
bool isValid() const
isValid Check if the type is valid
#define ReadJsonArray(json, param, result)
FunctionABI(const std::string &_name="", const std::string &_type="function", const std::vector< ParameterABI > &_inputs=std::vector< ParameterABI >(), const std::vector< ParameterABI > &_outputs=std::vector< ParameterABI >(), bool _payable=false, bool _constant=false, bool _anonymous=false)
std::string asString(bytes const &_b)
Converts byte array to a string containing the same (binary) data.
Type type() const
type Get the type
int atoi(const std::string &str)
std::string selector() const