From 295a27741873cea1bbeb486cac4fb192c4a20c2f Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Tue, 27 Jan 2015 13:15:08 +0100 Subject: [PATCH] Fixes and cleanup in OurWebThreeStubServer - All fixes are Natspec related --- alethzero/OurWebThreeStubServer.cpp | 53 ++++++++++++++++++----------- alethzero/OurWebThreeStubServer.h | 11 +++--- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/alethzero/OurWebThreeStubServer.cpp b/alethzero/OurWebThreeStubServer.cpp index 6c129dcd9..37d37bce1 100644 --- a/alethzero/OurWebThreeStubServer.cpp +++ b/alethzero/OurWebThreeStubServer.cpp @@ -31,19 +31,19 @@ using namespace std; using namespace dev; using namespace dev::eth; -OurWebThreeStubServer::OurWebThreeStubServer(jsonrpc::AbstractServerConnector& _conn, dev::WebThreeDirect& _web3, - std::vector const& _accounts, Main* main): +OurWebThreeStubServer::OurWebThreeStubServer(jsonrpc::AbstractServerConnector& _conn, WebThreeDirect& _web3, + vector const& _accounts, Main* main): WebThreeStubServer(_conn, _web3, _accounts), m_web3(&_web3), m_main(main) {} -std::string OurWebThreeStubServer::shh_newIdentity() +string OurWebThreeStubServer::shh_newIdentity() { - dev::KeyPair kp = dev::KeyPair::create(); + KeyPair kp = dev::KeyPair::create(); emit onNewId(QString::fromStdString(toJS(kp.sec()))); return toJS(kp.pub()); } -bool OurWebThreeStubServer::showAuthenticationPopup(std::string const& _title, std::string const& _text) const +bool OurWebThreeStubServer::showAuthenticationPopup(string const& _title, string const& _text) const { QMessageBox userInput; userInput.setText(QString::fromStdString(_title)); @@ -55,16 +55,31 @@ bool OurWebThreeStubServer::showAuthenticationPopup(std::string const& _title, s return userInput.exec() == QMessageBox::Ok; } -bool OurWebThreeStubServer::authenticate(dev::TransactionSkeleton const& _t) +void OurWebThreeStubServer::showBasicValueTransferNotice(u256 _value) const +{ + QMessageBox notice; + notice.setText("Basic Value Transfer Transaction"); + notice.setInformativeText(QString::fromStdString("Value is " + toString(_value))); + notice.setStandardButtons(QMessageBox::Ok); + notice.exec(); +} + +bool OurWebThreeStubServer::authenticate(TransactionSkeleton const& _t) { h256 contractCodeHash = m_web3->ethereum()->postState().codeHash(_t.to); if (contractCodeHash == EmptySHA3) - // recipient has no code - nothing special about this transaction. - // TODO: show basic message for value transfer. + // contract creation return true; - std::string userNotice = m_main->lookupNatSpecUserNotice(contractCodeHash, _t.data); - + if (false) //TODO: When is is just a value transfer? + { + // recipient has no code - nothing special about this transaction, show basic value transfer info + showBasicValueTransferNotice(_t.value); + return true; + } + + string userNotice = m_main->lookupNatSpecUserNotice(contractCodeHash, _t.data); + if (userNotice.empty()) return showAuthenticationPopup("Unverified Pending Transaction", "An undocumented transaction is about to be executed."); @@ -83,17 +98,17 @@ QNatspecExpressionEvaluator::QNatspecExpressionEvaluator(OurWebThreeStubServer* QNatspecExpressionEvaluator::~QNatspecExpressionEvaluator() {} -QString QNatspecExpressionEvaluator::evalExpression(QString const& _expression) +QString QNatspecExpressionEvaluator::evalExpression(QString const& _expression) const { - - // evaluate the natspec + // load natspec.js only when we need it for natspec evaluation m_main->evalRaw(contentsOfQResource(":/js/natspec.js")); - - // _expression should be in the format like this - // auto toEval = QString::fromStdString("the result of calling multiply(4) is `multiply(4)`"); - auto toEval = _expression; - auto result = m_main->evalRaw("evaluateExpression('" + toEval + "')"); - + QVariant result = m_main->evalRaw("evaluateExpression('" + _expression + "')"); + if (result.type() == QVariant::Invalid) + { + cerr << "Could not evaluate natspec expression: \"" << _expression.toStdString() << "\"" << endl; + // return the expression unevaluated + return _expression; + } return result.toString(); } diff --git a/alethzero/OurWebThreeStubServer.h b/alethzero/OurWebThreeStubServer.h index 53558b121..5223d79bd 100644 --- a/alethzero/OurWebThreeStubServer.h +++ b/alethzero/OurWebThreeStubServer.h @@ -42,6 +42,7 @@ signals: private: bool showAuthenticationPopup(std::string const& _title, std::string const& _text) const; + void showBasicValueTransferNotice(dev::u256 _value) const; dev::WebThreeDirect* m_web3; Main* m_main; @@ -51,14 +52,14 @@ private: class QNatspecExpressionEvaluator: public QObject { Q_OBJECT - + public: QNatspecExpressionEvaluator(OurWebThreeStubServer* _server, Main* _main); virtual ~QNatspecExpressionEvaluator(); - - QString evalExpression(QString const& _expression); - + + QString evalExpression(QString const& _expression) const; + private: OurWebThreeStubServer* m_server; Main* m_main; -}; \ No newline at end of file +};