diff --git a/alethzero/OurWebThreeStubServer.cpp b/alethzero/OurWebThreeStubServer.cpp index 8eb89d1c6..843ed2baf 100644 --- a/alethzero/OurWebThreeStubServer.cpp +++ b/alethzero/OurWebThreeStubServer.cpp @@ -69,7 +69,7 @@ bool OurWebThreeStubServer::authenticate(dev::TransactionSkeleton const& _t) con // return showAuthenticationPopup("Unverified Pending Transaction", // "An undocumented transaction is about to be executed."); - auto evaluator = QNatspecExpressionEvaluator(*m_web3, m_main); + QNatspecExpressionEvaluator evaluator(*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 @@ -83,6 +83,16 @@ QNatspecExpressionEvaluator::QNatspecExpressionEvaluator(dev::WebThreeDirect& _w QNatspecExpressionEvaluator::~QNatspecExpressionEvaluator() {} +QString QNatspecExpressionEvaluator::stateAt(QString _key) +{ + return "1"; +} + +QString QNatspecExpressionEvaluator::call(QString _method) +{ + return "2"; +} + QString QNatspecExpressionEvaluator::evalExpression(QString const& _expression) { // evaluate the natspec diff --git a/alethzero/OurWebThreeStubServer.h b/alethzero/OurWebThreeStubServer.h index bcd23fd46..fe7d432cf 100644 --- a/alethzero/OurWebThreeStubServer.h +++ b/alethzero/OurWebThreeStubServer.h @@ -48,14 +48,19 @@ private: }; -class QNatspecExpressionEvaluator +class QNatspecExpressionEvaluator: public QObject { + Q_OBJECT + public: QNatspecExpressionEvaluator(dev::WebThreeDirect& _web3, Main* _main); - ~QNatspecExpressionEvaluator(); + virtual ~QNatspecExpressionEvaluator(); QString evalExpression(QString const& _expression); + Q_INVOKABLE QString stateAt(QString _key); + Q_INVOKABLE QString call(QString _method); + private: dev::WebThreeDirect* m_web3; Main* m_main; diff --git a/libjsqrc/natspec.js b/libjsqrc/natspec.js index 18f57924a..0807fecb8 100644 --- a/libjsqrc/natspec.js +++ b/libjsqrc/natspec.js @@ -4,34 +4,51 @@ * 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 copyToContext = function (obj, context) { var keys = Object.keys(obj); keys.forEach(function (key) { - _eth_global[key] = obj[key]; + context[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 {}; +/// @returns hashmap with contract properties which are used +var getContractProperties = function (expression, abi) { + var keys = ['test']; + + return keys.reduce(function (acc, current) { + acc[current] = natspec.stateAt(current); + return acc; + }, {}); }; +/// Function called to get all contract's methods +/// @returns hashmap with used contract's methods +var getContractMethods = function (expression, abi) { + var keys = ['testMethod']; + + return keys.reduce(function (acc, current) { + acc[current] = function () { + // TODO: connect parser + }; + return acc; + }, {}); +}; /// 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(); + var self = this; + var abi = web3._currentAbi; + + var storage = getContractProperties(expression, abi); + var methods = getContractMethods(expression, abi); - copyToGlobalContext(storage); + copyToContext(storage, self); + copyToContext(methods, self); // TODO: check if it is safe return eval(expression).toString();