yann300
10 years ago
committed by
yann300
28 changed files with 619 additions and 103 deletions
@ -0,0 +1,135 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum 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. |
|||
|
|||
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file ContractCallDataEncoder.cpp
|
|||
* @author Yann yann@ethdev.com |
|||
* @date 2014 |
|||
* Ethereum IDE client. |
|||
*/ |
|||
|
|||
#include <QMap> |
|||
#include <QStringList> |
|||
#include <libdevcore/CommonJS.h> |
|||
#include "libsolidity/AST.h" |
|||
#include "QVariableDeclaration.h" |
|||
#include "QVariableDefinition.h" |
|||
#include "ContractCallDataEncoder.h" |
|||
using namespace dev; |
|||
using namespace dev::solidity; |
|||
using namespace dev::mix; |
|||
|
|||
ContractCallDataEncoder::ContractCallDataEncoder() |
|||
{ |
|||
} |
|||
|
|||
bytes ContractCallDataEncoder::encodedData() |
|||
{ |
|||
return m_encodedData; |
|||
} |
|||
|
|||
void ContractCallDataEncoder::encode(int _functionIndex) |
|||
{ |
|||
bytes i = jsToBytes(std::to_string(_functionIndex)); |
|||
m_encodedData.insert(m_encodedData.end(), i.begin(), i.end()); |
|||
} |
|||
|
|||
void ContractCallDataEncoder::encode(QVariableDeclaration* _dec, bool _value) |
|||
{ |
|||
return encode(_dec, QString(formatBool(_value))); |
|||
} |
|||
|
|||
void ContractCallDataEncoder::encode(QVariableDeclaration* _dec, QString _value) |
|||
{ |
|||
int padding = this->padding(_dec->type()); |
|||
bytes data = padded(jsToBytes(_value.toStdString()), padding); |
|||
m_encodedData.insert(m_encodedData.end(), data.begin(), data.end()); |
|||
} |
|||
|
|||
QList<QVariableDefinition*> ContractCallDataEncoder::decode(QList<QObject*> _returnParameters, bytes _value) |
|||
{ |
|||
QList<QVariableDefinition*> r; |
|||
std::string returnValue = toJS(_value); |
|||
returnValue = returnValue.substr(2, returnValue.length() - 1); |
|||
for (int k = 0; k <_returnParameters.length(); k++) |
|||
{ |
|||
QVariableDeclaration* dec = (QVariableDeclaration*)_returnParameters.at(k); |
|||
int padding = this->padding(dec->type()); |
|||
std::string rawParam = returnValue.substr(0, padding * 2); |
|||
r.append(new QVariableDefinition(dec, convertToReadable(unpadded(rawParam), dec))); |
|||
returnValue = returnValue.substr(padding, returnValue.length() - 1); |
|||
} |
|||
return r; |
|||
} |
|||
|
|||
int ContractCallDataEncoder::padding(QString type) |
|||
{ |
|||
// TODO : to be improved (load types automatically from solidity library).
|
|||
if (type.indexOf("uint") != 1) |
|||
{ |
|||
return integerPadding(type.remove("uint").toInt()); |
|||
} |
|||
else if (type.indexOf("int") != -1) |
|||
{ |
|||
//int
|
|||
return integerPadding(type.remove("int").toInt()); |
|||
} |
|||
else if (type.indexOf("bool") != -1) |
|||
{ |
|||
return 1; |
|||
} |
|||
else if ((type.indexOf("address") != -1)) |
|||
{ |
|||
return 20; |
|||
} |
|||
return 0; |
|||
} |
|||
|
|||
int ContractCallDataEncoder::integerPadding(int bitValue) |
|||
{ |
|||
return bitValue / 8; |
|||
} |
|||
|
|||
QString ContractCallDataEncoder::formatBool(bool _value) |
|||
{ |
|||
return (_value ? "1" : "0"); |
|||
} |
|||
|
|||
QString ContractCallDataEncoder::convertToReadable(std::string _v, QVariableDeclaration* _dec) |
|||
{ |
|||
if (_dec->type().indexOf("int") != -1) |
|||
{ |
|||
return convertToInt(_v); |
|||
} |
|||
else if (_dec->type().indexOf("bool") != -1) |
|||
{ |
|||
return convertToBool(_v); |
|||
} |
|||
else |
|||
{ |
|||
return QString::fromStdString(_v); |
|||
} |
|||
} |
|||
|
|||
QString ContractCallDataEncoder::convertToBool(std::string _v) |
|||
{ |
|||
return _v == "1" ? "true" : "false"; |
|||
} |
|||
|
|||
QString ContractCallDataEncoder::convertToInt(std::string _v) |
|||
{ |
|||
return QString::fromStdString(_v); |
|||
} |
|||
|
@ -0,0 +1,56 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum 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. |
|||
|
|||
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file ContractCallDataEncoder.h
|
|||
* @author Yann yann@ethdev.com |
|||
* @date 2014 |
|||
* Ethereum IDE client. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include "QVariableDeclaration.h" |
|||
#include "QVariableDefinition.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace mix |
|||
{ |
|||
|
|||
class ContractCallDataEncoder |
|||
{ |
|||
public: |
|||
ContractCallDataEncoder(); |
|||
void encode(QVariableDeclaration* _dec, QString _value); |
|||
QList<QVariableDefinition*> decode(QList<QObject*> _dec, bytes _value); |
|||
void encode(QVariableDeclaration* _dec, bool _value); |
|||
void encode(int _functionIndex); |
|||
bytes encodedData(); |
|||
|
|||
private: |
|||
QMap<QString, int> typePadding; |
|||
int padding(QString _type); |
|||
static QString convertToReadable(std::string _v, QVariableDeclaration* _dec); |
|||
static QString convertToBool(std::string _v); |
|||
static QString convertToInt(std::string _v); |
|||
static int integerPadding(int _bitValue); |
|||
static QString formatBool(bool _value); |
|||
bytes m_encodedData; |
|||
|
|||
}; |
|||
|
|||
} |
|||
} |
@ -0,0 +1,55 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum 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. |
|||
|
|||
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file QVariableDefinition.h
|
|||
* @author Yann yann@ethdev.com |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#include "QVariableDefinition.h" |
|||
|
|||
using namespace dev::mix; |
|||
int QVariableDefinitionList::rowCount(const QModelIndex& parent) const |
|||
{ |
|||
return m_def.size(); |
|||
} |
|||
|
|||
QVariant QVariableDefinitionList::data(const QModelIndex& index, int role) const |
|||
{ |
|||
if (role != Qt::DisplayRole) |
|||
return QVariant(); |
|||
|
|||
int i = index.row(); |
|||
if (i < 0 || i >= m_def.size()) |
|||
return QVariant(QVariant::Invalid); |
|||
|
|||
return QVariant::fromValue(m_def.at(i)); |
|||
} |
|||
|
|||
QHash<int, QByteArray> QVariableDefinitionList::roleNames() const |
|||
{ |
|||
QHash<int, QByteArray> roles; |
|||
roles[Qt::DisplayRole] = "variable"; |
|||
return roles; |
|||
} |
|||
|
|||
QVariableDefinition* QVariableDefinitionList::val(int idx) |
|||
{ |
|||
if (idx < 0 || idx >= m_def.size()) |
|||
return nullptr; |
|||
|
|||
return m_def.at(idx); |
|||
} |
@ -0,0 +1,71 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum 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. |
|||
|
|||
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file QVariableDefinition.h
|
|||
* @author Yann yann@ethdev.com |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <QAbstractListModel> |
|||
#include "QVariableDeclaration.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace mix |
|||
{ |
|||
|
|||
class QVariableDefinition: public QObject |
|||
{ |
|||
Q_OBJECT |
|||
Q_PROPERTY(QString value READ value) |
|||
Q_PROPERTY(QVariableDeclaration* declaration READ declaration) |
|||
|
|||
public: |
|||
QVariableDefinition(QVariableDeclaration* _def, QString _value): QObject(_def->parent()), m_value(_value), m_dec(_def) {} |
|||
|
|||
QVariableDeclaration* declaration() { return m_dec; } |
|||
QString value() { return m_value; } |
|||
|
|||
private: |
|||
QVariableDeclaration* m_dec; |
|||
QString m_value; |
|||
}; |
|||
|
|||
class QVariableDefinitionList: public QAbstractListModel |
|||
{ |
|||
Q_OBJECT |
|||
//Q_PROPERTY(QList<QVariableDefinition*> def READ def)
|
|||
|
|||
public: |
|||
QVariableDefinitionList(QList<QVariableDefinition*> _def): m_def(_def) {} |
|||
int rowCount(const QModelIndex& parent = QModelIndex()) const override; |
|||
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; |
|||
QHash<int, QByteArray> roleNames() const override; |
|||
QVariableDefinition* val(int idx); |
|||
QList<QVariableDefinition*> def() { |
|||
return m_def; |
|||
} |
|||
|
|||
private: |
|||
QList<QVariableDefinition*> m_def; |
|||
}; |
|||
|
|||
} |
|||
} |
|||
|
|||
Q_DECLARE_METATYPE(dev::mix::QVariableDefinition*) |
Loading…
Reference in new issue