Browse Source

init natspec.js

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
1febd8c28c
  1. 7
      alethzero/MainWin.cpp
  2. 4
      alethzero/MainWin.h
  3. 33
      alethzero/OurWebThreeStubServer.cpp
  4. 14
      alethzero/OurWebThreeStubServer.h
  5. 1
      libjsqrc/js.qrc
  6. 39
      libjsqrc/natspec.js

7
alethzero/MainWin.cpp

@ -97,7 +97,7 @@ static vector<KeyPair> keysAsVector(QList<KeyPair> const& keys)
return {begin(list), end(list)};
}
static QString contentsOfQResource(string const& res)
QString contentsOfQResource(string const& res)
{
QFile file(QString::fromStdString(res));
if (!file.open(QFile::ReadOnly))
@ -429,6 +429,11 @@ void Main::on_jsInput_returnPressed()
ui->jsInput->setText("");
}
QVariant Main::evalRaw(QString const& _js)
{
return ui->webView->page()->currentFrame()->evaluateJavaScript(_js);
}
void Main::eval(QString const& _js)
{
if (_js.trimmed().isEmpty())

4
alethzero/MainWin.h

@ -71,6 +71,8 @@ struct WorldState
using WatchHandler = std::function<void(dev::eth::LocalisedLogEntries const&)>;
QString contentsOfQResource(std::string const& res);
class Main : public QMainWindow
{
Q_OBJECT
@ -87,6 +89,8 @@ public:
std::string lookupNatSpecUserNotice(dev::h256 const& _contractHash, dev::bytes const& _transactionData);
QList<dev::KeyPair> owned() const { return m_myIdentities + m_myKeys; }
QVariant evalRaw(QString const& _js);
public slots:
void load(QString _file);

33
alethzero/OurWebThreeStubServer.cpp

@ -63,10 +63,37 @@ bool OurWebThreeStubServer::authenticate(dev::TransactionSkeleton const& _t) con
return true;
std::string userNotice = m_main->lookupNatSpecUserNotice(contractCodeHash, _t.data);
if (userNotice.empty())
return showAuthenticationPopup("Unverified Pending Transaction",
"An undocumented transaction is about to be executed.");
// TODO: uncomment this
// if (userNotice.empty())
// return showAuthenticationPopup("Unverified Pending Transaction",
// "An undocumented transaction is about to be executed.");
auto evaluator = QNatspecExpressionEvaluator(*m_web3, m_main);
userNotice = evaluator.evalExpression(QString::fromStdString(userNotice)).toStdString();
// otherwise it's a transaction to a contract for which we have the natspec
return showAuthenticationPopup("Pending Transaction", userNotice);
}
QNatspecExpressionEvaluator::QNatspecExpressionEvaluator(dev::WebThreeDirect& _web3, Main* _main):
m_web3(&_web3), m_main(_main)
{}
QNatspecExpressionEvaluator::~QNatspecExpressionEvaluator()
{}
QString QNatspecExpressionEvaluator::evalExpression(QString const& _expression)
{
// evaluate the natspec
m_main->evalRaw(contentsOfQResource(":/js/natspec.js"));
auto result = m_main->evalRaw("evaluateExpression(\"2 + 1\")");
// auto result = m_main->evalRaw(_expression);
return result.toString();
}

14
alethzero/OurWebThreeStubServer.h

@ -46,3 +46,17 @@ private:
dev::WebThreeDirect* m_web3;
Main* m_main;
};
class QNatspecExpressionEvaluator
{
public:
QNatspecExpressionEvaluator(dev::WebThreeDirect& _web3, Main* _main);
~QNatspecExpressionEvaluator();
QString evalExpression(QString const& _expression);
private:
dev::WebThreeDirect* m_web3;
Main* m_main;
};

1
libjsqrc/js.qrc

@ -4,5 +4,6 @@
<file>bignumber.min.js</file>
<file>setup.js</file>
<file alias="webthree.js">ethereumjs/dist/ethereum.js</file>
<file>natspec.js</file>
</qresource>
</RCC>

39
libjsqrc/natspec.js

@ -0,0 +1,39 @@
/**
* This plugin should be reloaded each time we want to evaluate set of expressions
* Just because of security reasons
*/
/// helper variable used by 'copyToGlobalContext' function to copy everything to global context
var _eth_global = this;
/// Should be called to copy values from object to global context
var copyToGlobalContext = function (obj) {
var keys = Object.keys(obj);
keys.forEach(function (key) {
_eth_global[key] = obj[key];
});
}
/// Function called to get all contract's storage values
/// In future can be improved be getting storage values on demand
/// @returns hashmap with contract storage
var getContractStorage = function () {
return {};
};
/// Should be called to evaluate single expression
/// Is internally using javascript's 'eval' method
/// Should be checked if it is safe
var evaluateExpression = function (expression) {
// in future may be replaced with getting storage values based on expression
var storage = getContractStorage();
copyToGlobalContext(storage);
// TODO: check if it is safe
return eval(expression).toString();
};
Loading…
Cancel
Save