Browse Source

Natspechandler: Get function hash from transaction data

cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
e7ac3ba869
  1. 5
      alethzero/MainWin.cpp
  2. 1
      alethzero/MainWin.h
  3. 33
      alethzero/NatspecHandler.cpp
  4. 6
      alethzero/NatspecHandler.h
  5. 8
      alethzero/OurWebThreeStubServer.cpp
  6. 2
      libjsqrc/ethereumjs/example/natspec_contract.html

5
alethzero/MainWin.cpp

@ -2294,6 +2294,11 @@ std::string Main::lookupNatSpecUserNotice(dev::h256 const& _contractHash, std::s
return m_natspecDB.getUserNotice(_contractHash, _methodName);
}
std::string Main::lookupNatSpecUserNotice(dev::h256 const& _contractHash, dev::bytes const& _transactionData)
{
return m_natspecDB.getUserNotice(_contractHash, _transactionData);
}
void Main::refreshWhispers()
{
ui->whispers->clear();

1
alethzero/MainWin.h

@ -84,6 +84,7 @@ public:
std::string lookupNatSpec(dev::h256 const& _contractHash) const;
std::string lookupNatSpecUserNotice(dev::h256 const& _contractHash, std::string const& _methodName);
std::string lookupNatSpecUserNotice(dev::h256 const& _contractHash, dev::bytes const& _transactionData);
QList<dev::KeyPair> owned() const { return m_myIdentities + m_myKeys; }

33
alethzero/NatspecHandler.cpp

@ -25,7 +25,9 @@
#include <libethereum/Defaults.h>
#include <libdevcore/Common.h>
#include <libdevcore/CommonData.h>
#include <libdevcore/Log.h>
#include <libdevcrypto/SHA3.h>
using namespace dev;
@ -62,14 +64,43 @@ std::string NatspecHandler::getUserNotice(std::string const& json, std::string c
Json::Value natspec, userNotice;
std::string retStr;
m_reader.parse(json, natspec);
retStr = natspec["methods"][_methodName]["notice"].toStyledString();
retStr = natspec["methods"][_methodName]["notice"].asString();
return (retStr == "null\n") ? "" : retStr;
}
std::string NatspecHandler::getUserNotice(std::string const& json, const dev::bytes& _transactionData)
{
Json::Value natspec, userNotice;
std::string retStr;
m_reader.parse(json, natspec);
bytes transactionFunctionPart(_transactionData.begin(), _transactionData.begin() + 4);
FixedHash<4> transactionFunctionHash(transactionFunctionPart);
// for (auto const& it: natspec["methods"])
Json::Value methods = natspec["methods"];
for (Json::ValueIterator it= methods.begin(); it != methods.end(); ++it )
{
std::string functionSig = it.key().asString();
FixedHash<4> functionHash(dev::sha3(functionSig));
if (functionHash == transactionFunctionHash) {
return (*it)["notice"].asString();
}
}
// not found
return "";
}
std::string NatspecHandler::getUserNotice(dev::h256 const& _contractHash, std::string const& _methodName)
{
return getUserNotice(retrieve(_contractHash), _methodName);
}
std::string NatspecHandler::getUserNotice(dev::h256 const& _contractHash, dev::bytes const& _transactionData)
{
return getUserNotice(retrieve(_contractHash), _transactionData);
}

6
alethzero/NatspecHandler.h

@ -43,10 +43,14 @@ class NatspecHandler
/// Given a json natspec string, retrieve the user notice string
std::string getUserNotice(std::string const& json, std::string const& _methodName);
std::string getUserNotice(std::string const& json, const dev::bytes& _transactionData);
/// Given a contract code hash, retrieve the natspec documentation's user notice for that contract
std::string getUserNotice(dev::h256 const& _contractHash, std::string const& _methodName);
/// Given a contract code hash and the transaction's data retrieve the natspec documention's
/// user notice for that transaction.
/// @returns The user notice or an empty string if no natspec for the contract exists
/// or if the existing natspec does not document the @c _methodName
std::string getUserNotice(dev::h256 const& _contractHash, std::string const& _methodName);
std::string getUserNotice(dev::h256 const& _contractHash, dev::bytes const& _transactionDacta);
private:
ldb::ReadOptions m_readOptions;

8
alethzero/OurWebThreeStubServer.cpp

@ -22,6 +22,7 @@
#include "OurWebThreeStubServer.h"
#include <QMessageBox>
#include <QAbstractButton>
#include <libwebthree/WebThree.h>
#include "MainWin.h"
@ -62,7 +63,8 @@ bool OurWebThreeStubServer::authenticate(dev::TransactionSkeleton const& _t) con
//LTODO: Actually find and use the method name here
std::string userNotice = m_main->lookupNatSpecUserNotice(contractCodeHash, "multiply");
// std::string userNotice = m_main->lookupNatSpecUserNotice(contractCodeHash, "multiply");
std::string userNotice = m_main->lookupNatSpecUserNotice(contractCodeHash, _t.data);
if (userNotice.empty())
{
QMessageBox userInput;
@ -77,8 +79,10 @@ bool OurWebThreeStubServer::authenticate(dev::TransactionSkeleton const& _t) con
// otherwise it's a transaction to a contract for which we have the natspec
QMessageBox userInput;
userInput.setText("Pending Transaction");
userInput.setInformativeText(QString::fromStdString(userNotice));
userInput.setInformativeText(QString::fromStdString(userNotice + "\n Do you wish to allow this?"));
userInput.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
userInput.button(QMessageBox::Ok)->setText("Allow");
userInput.button(QMessageBox::Cancel)->setText("Reject");
userInput.setDefaultButton(QMessageBox::Cancel);
return userInput.exec() == QMessageBox::Ok;

2
libjsqrc/ethereumjs/example/natspec_contract.html

@ -12,7 +12,7 @@
// solidity source code
var source = "" +
"contract test {\n" +
" /// @notice This is an awesome function for multiplication. \n" +
" /// @notice Will multiplty `a` by 7. \n" +
" function multiply(uint a) returns(uint d) {\n" +
" return a * 7;\n" +
" }\n" +

Loading…
Cancel
Save