From 1febd8c28c9a30310d6e1d049f2cbc829f6cf9fc Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Tue, 20 Jan 2015 11:03:04 +0100 Subject: [PATCH] init natspec.js --- alethzero/MainWin.cpp | 7 +++++- alethzero/MainWin.h | 4 +++ alethzero/OurWebThreeStubServer.cpp | 33 +++++++++++++++++++++--- alethzero/OurWebThreeStubServer.h | 14 +++++++++++ libjsqrc/js.qrc | 1 + libjsqrc/natspec.js | 39 +++++++++++++++++++++++++++++ 6 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 libjsqrc/natspec.js diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index 7ed961a77..6e5661190 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -97,7 +97,7 @@ static vector keysAsVector(QList 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()) diff --git a/alethzero/MainWin.h b/alethzero/MainWin.h index bcfa8a0fc..6f8ee7e2b 100644 --- a/alethzero/MainWin.h +++ b/alethzero/MainWin.h @@ -71,6 +71,8 @@ struct WorldState using WatchHandler = std::function; +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 owned() const { return m_myIdentities + m_myKeys; } + + QVariant evalRaw(QString const& _js); public slots: void load(QString _file); diff --git a/alethzero/OurWebThreeStubServer.cpp b/alethzero/OurWebThreeStubServer.cpp index ce1f28507..8eb89d1c6 100644 --- a/alethzero/OurWebThreeStubServer.cpp +++ b/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(); +} + + + + + + diff --git a/alethzero/OurWebThreeStubServer.h b/alethzero/OurWebThreeStubServer.h index a67af0827..bcd23fd46 100644 --- a/alethzero/OurWebThreeStubServer.h +++ b/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; +}; \ No newline at end of file diff --git a/libjsqrc/js.qrc b/libjsqrc/js.qrc index 329b3356a..24db8e19d 100644 --- a/libjsqrc/js.qrc +++ b/libjsqrc/js.qrc @@ -4,5 +4,6 @@ bignumber.min.js setup.js ethereumjs/dist/ethereum.js + natspec.js diff --git a/libjsqrc/natspec.js b/libjsqrc/natspec.js new file mode 100644 index 000000000..18f57924a --- /dev/null +++ b/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(); +}; +