diff --git a/eth/main.cpp b/eth/main.cpp index 1516478a5..21fc7f200 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -340,8 +340,7 @@ int main(int argc, char** argv) auto_ptr jsonrpcServer; if (jsonrpc > -1) { - jsonrpcServer = auto_ptr(new WebThreeStubServer(new jsonrpc::CorsHttpServer(jsonrpc), web3)); - jsonrpcServer->setKeys({us}); + jsonrpcServer = auto_ptr(new WebThreeStubServer(new jsonrpc::CorsHttpServer(jsonrpc), web3, {us})); jsonrpcServer->StartListening(); } #endif @@ -428,8 +427,7 @@ int main(int argc, char** argv) { if (jsonrpc < 0) jsonrpc = 8080; - jsonrpcServer = auto_ptr(new WebThreeStubServer(new jsonrpc::CorsHttpServer(jsonrpc), web3)); - jsonrpcServer->setKeys({us}); + jsonrpcServer = auto_ptr(new WebThreeStubServer(new jsonrpc::CorsHttpServer(jsonrpc), web3, {us})); jsonrpcServer->StartListening(); } else if (cmd == "jsonstop") diff --git a/libdevcore/CommonJS.h b/libdevcore/CommonJS.h index ad4cb43d4..e9528129f 100644 --- a/libdevcore/CommonJS.h +++ b/libdevcore/CommonJS.h @@ -116,7 +116,7 @@ inline double jsFromFixed(std::string const& _s) struct TransactionJS { - Secret from; + Address from; Address to; u256 value; bytes data; diff --git a/libethrpc/WebThreeStubServer.cpp b/libethrpc/WebThreeStubServer.cpp index 6a9521e88..33067dcd7 100644 --- a/libethrpc/WebThreeStubServer.cpp +++ b/libethrpc/WebThreeStubServer.cpp @@ -96,10 +96,18 @@ static Json::Value toJson(dev::eth::Transaction const& _t) return res; } -WebThreeStubServer::WebThreeStubServer(jsonrpc::AbstractServerConnector* _conn, WebThreeDirect& _web3): +WebThreeStubServer::WebThreeStubServer(jsonrpc::AbstractServerConnector* _conn, WebThreeDirect& _web3, std::vector _accounts): AbstractWebThreeStubServer(_conn), m_web3(_web3) { + setAccounts(_accounts); +} + +void WebThreeStubServer::setAccounts(std::vector const& _accounts) +{ + m_accounts.clear(); + for (auto i: _accounts) + m_accounts[i.address()] = i.secret(); } dev::eth::Interface* WebThreeStubServer::client() const @@ -107,6 +115,14 @@ dev::eth::Interface* WebThreeStubServer::client() const return m_web3.ethereum(); } +Json::Value WebThreeStubServer::accounts() +{ + Json::Value ret; + for (auto i: m_accounts) + ret.append(toJS(i.first)); + return ret; +} + std::string WebThreeStubServer::balanceAt(string const& _address, int const& _block) { return toJS(client()->balanceAt(jsToAddress(_address), _block)); @@ -139,7 +155,7 @@ static TransactionJS toTransaction(Json::Value const& _json) } if (!_json["from"].empty()) - ret.from = jsToSecret(_json["from"].asString()); + ret.from = jsToAddress(_json["from"].asString()); if (!_json["to"].empty()) ret.to = jsToAddress(_json["to"].asString()); if (!_json["value"].empty()) @@ -175,15 +191,21 @@ std::string WebThreeStubServer::call(Json::Value const& _json) if (!client()) return ret; TransactionJS t = toTransaction(_json); - if (!t.to) + if (!t.from && m_accounts.size()) + { + auto b = m_accounts.begin()->first; + for (auto a: m_accounts) + if (client()->balanceAt(a.first) > client()->balanceAt(b)) + b = a.first; + t.from = b; + } + if (!m_accounts.count(t.from)) return ret; - if (!t.from && m_keys.size()) - t.from = m_keys[0].secret(); if (!t.gasPrice) t.gasPrice = 10 * dev::eth::szabo; if (!t.gas) t.gas = client()->balanceAt(KeyPair(t.from).address()) / t.gasPrice; - ret = toJS(client()->call(t.from, t.value, t.to, t.data, t.gas, t.gasPrice)); + ret = toJS(client()->call(m_accounts[t.from].secret(), t.value, t.to, t.data, t.gas, t.gasPrice)); return ret; } @@ -232,21 +254,6 @@ bool WebThreeStubServer::mining() return client() ? client()->isMining() : false; } -std::string WebThreeStubServer::key() -{ - if (!m_keys.size()) - return std::string(); - return toJS(m_keys[0].sec()); -} - -Json::Value WebThreeStubServer::keys() -{ - Json::Value ret; - for (auto i: m_keys) - ret.append(toJS(i.secret())); - return ret; -} - std::string WebThreeStubServer::lll(string const& _s) { return toJS(dev::eth::compileLLL(_s)); @@ -386,22 +393,26 @@ std::string WebThreeStubServer::transact(Json::Value const& _json) if (!client()) return ret; TransactionJS t = toTransaction(_json); - if (!t.from && m_keys.size()) + if (!t.from && m_accounts.size()) { - auto b = m_keys.front(); - for (auto a: m_keys) - if (client()->balanceAt(KeyPair(a).address()) > client()->balanceAt(KeyPair(b).address())) - b = a; - t.from = b.secret(); + auto b = m_accounts.begin()->first; + for (auto a: m_accounts) + if (client()->balanceAt(a.first) > client()->balanceAt(b)) + b = a.first; + t.from = b; } + if (!m_accounts.count(t.from)) + return ret; if (!t.gasPrice) t.gasPrice = 10 * dev::eth::szabo; if (!t.gas) t.gas = min(client()->gasLimitRemaining(), client()->balanceAt(KeyPair(t.from).address()) / t.gasPrice); + cwarn << "Silently signing transaction from address" << t.from.abridged() << ": User validation hook goes here."; if (t.to) - client()->transact(t.from, t.value, t.to, t.data, t.gas, t.gasPrice); + // TODO: from qethereum, insert validification hook here. + client()->transact(m_accounts[t.from].secret(), t.value, t.to, t.data, t.gas, t.gasPrice); else - ret = toJS(client()->transact(t.from, t.value, t.data, t.gas, t.gasPrice)); + ret = toJS(client()->transact(m_accounts[t.from].secret(), t.value, t.data, t.gas, t.gasPrice)); client()->flushTransactions(); return ret; } diff --git a/libethrpc/WebThreeStubServer.h b/libethrpc/WebThreeStubServer.h index daae68ba0..c4fa66e78 100644 --- a/libethrpc/WebThreeStubServer.h +++ b/libethrpc/WebThreeStubServer.h @@ -36,8 +36,9 @@ namespace dev { class WebThreeDirect; namespace eth { class Interface; } class K class WebThreeStubServer: public AbstractWebThreeStubServer { public: - WebThreeStubServer(jsonrpc::AbstractServerConnector* _conn, dev::WebThreeDirect& _web3); + WebThreeStubServer(jsonrpc::AbstractServerConnector* _conn, dev::WebThreeDirect& _web3, std::vector _accounts); + virtual Json::Value accounts(); virtual std::string balanceAt(std::string const& _address, int const& _block); virtual Json::Value block(Json::Value const& _params); virtual std::string call(Json::Value const& _json); @@ -50,8 +51,6 @@ public: virtual std::string gasPrice(); virtual bool listening(); virtual bool mining(); - virtual std::string key(); - virtual Json::Value keys(); virtual std::string lll(std::string const& _s); virtual Json::Value messages(Json::Value const& _json); virtual int number(); @@ -73,10 +72,10 @@ public: virtual bool check(int const& _id); virtual bool killWatch(int const& _id); - void setKeys(std::vector _keys) { m_keys = _keys; } + void setAccounts(std::vector const& _accounts); private: dev::eth::Interface* client() const; dev::WebThreeDirect& m_web3; - std::vector m_keys; + std::map m_accounts; dev::FixedHash<32> numberOrHash(Json::Value const& _json) const; }; diff --git a/libethrpc/abstractwebthreestubserver.h b/libethrpc/abstractwebthreestubserver.h index aaadcff02..8b0878b50 100644 --- a/libethrpc/abstractwebthreestubserver.h +++ b/libethrpc/abstractwebthreestubserver.h @@ -13,6 +13,7 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServer(conn) { + this->bindAndAddMethod(new jsonrpc::Procedure("accounts", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_ARRAY, NULL), &AbstractWebThreeStubServer::accountsI); this->bindAndAddMethod(new jsonrpc::Procedure("balanceAt", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "address",jsonrpc::JSON_STRING,"block",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::balanceAtI); this->bindAndAddMethod(new jsonrpc::Procedure("block", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "params",jsonrpc::JSON_OBJECT, NULL), &AbstractWebThreeStubServer::blockI); this->bindAndAddMethod(new jsonrpc::Procedure("call", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "json",jsonrpc::JSON_OBJECT, NULL), &AbstractWebThreeStubServer::callI); @@ -24,8 +25,6 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServerbindAndAddMethod(new jsonrpc::Procedure("fromAscii", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "padding",jsonrpc::JSON_INTEGER,"s",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::fromAsciiI); this->bindAndAddMethod(new jsonrpc::Procedure("fromFixed", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_REAL, "s",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::fromFixedI); this->bindAndAddMethod(new jsonrpc::Procedure("gasPrice", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::gasPriceI); - this->bindAndAddMethod(new jsonrpc::Procedure("key", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::keyI); - this->bindAndAddMethod(new jsonrpc::Procedure("keys", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_ARRAY, NULL), &AbstractWebThreeStubServer::keysI); this->bindAndAddMethod(new jsonrpc::Procedure("killWatch", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_BOOLEAN, "id",jsonrpc::JSON_INTEGER, NULL), &AbstractWebThreeStubServer::killWatchI); this->bindAndAddMethod(new jsonrpc::Procedure("listening", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_BOOLEAN, NULL), &AbstractWebThreeStubServer::listeningI); this->bindAndAddMethod(new jsonrpc::Procedure("lll", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_STRING, "s",jsonrpc::JSON_STRING, NULL), &AbstractWebThreeStubServer::lllI); @@ -50,6 +49,11 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServeraccounts(); + } + inline virtual void balanceAtI(const Json::Value& request, Json::Value& response) { response = this->balanceAt(request["address"].asString(), request["block"].asInt()); @@ -105,16 +109,6 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServergasPrice(); } - inline virtual void keyI(const Json::Value& request, Json::Value& response) - { - response = this->key(); - } - - inline virtual void keysI(const Json::Value& request, Json::Value& response) - { - response = this->keys(); - } - inline virtual void killWatchI(const Json::Value& request, Json::Value& response) { response = this->killWatch(request["id"].asInt()); @@ -221,6 +215,7 @@ class AbstractWebThreeStubServer : public jsonrpc::AbstractServertransact(m_accounts[t.from].secret(), t.value, t.to, t.data, t.gas, t.gasPrice); else - ret = toQJS(client()->transact(t.from, t.value, t.data, t.gas, t.gasPrice)); + ret = toQJS(client()->transact(m_accounts[t.from].secret(), t.value, t.data, t.gas, t.gasPrice)); client()->flushTransactions(); return ret; } diff --git a/neth/main.cpp b/neth/main.cpp index 653b1b0ba..6d39db847 100644 --- a/neth/main.cpp +++ b/neth/main.cpp @@ -478,8 +478,7 @@ int main(int argc, char** argv) auto_ptr jsonrpcServer; if (jsonrpc > -1) { - jsonrpcServer = auto_ptr(new WebThreeStubServer(new jsonrpc::HttpServer(jsonrpc), web3)); - jsonrpcServer->setKeys({us}); + jsonrpcServer = auto_ptr(new WebThreeStubServer(new jsonrpc::HttpServer(jsonrpc), web3, {us})); jsonrpcServer->StartListening(); } #endif @@ -552,8 +551,7 @@ int main(int argc, char** argv) { if (jsonrpc < 0) jsonrpc = 8080; - jsonrpcServer = auto_ptr(new WebThreeStubServer(new jsonrpc::HttpServer(jsonrpc), web3)); - jsonrpcServer->setKeys({us}); + jsonrpcServer = auto_ptr(new WebThreeStubServer(new jsonrpc::HttpServer(jsonrpc), web3, {us})); jsonrpcServer->StartListening(); } else if (cmd == "jsonstop") diff --git a/test/jsonrpc.cpp b/test/jsonrpc.cpp index 72cbc9f02..3656abfb9 100644 --- a/test/jsonrpc.cpp +++ b/test/jsonrpc.cpp @@ -60,7 +60,7 @@ struct JsonrpcFixture { web3.setIdealPeerCount(5); web3.ethereum()->setForceMining(true); - jsonrpcServer = auto_ptr(new WebThreeStubServer(new jsonrpc::CorsHttpServer(8080), web3)); + jsonrpcServer = auto_ptr(new WebThreeStubServer(new jsonrpc::CorsHttpServer(8080), web3, {})); jsonrpcServer->StartListening(); jsonrpcClient = auto_ptr(new WebThreeStubClient(new jsonrpc::HttpClient("http://localhost:8080"))); diff --git a/test/webthreestubclient.h b/test/webthreestubclient.h index 80342b43d..86600a7f6 100644 --- a/test/webthreestubclient.h +++ b/test/webthreestubclient.h @@ -19,6 +19,18 @@ class WebThreeStubClient delete this->client; } + Json::Value accounts() throw (jsonrpc::JsonRpcException) + { + Json::Value p; + p = Json::nullValue; + Json::Value result = this->client->CallMethod("accounts",p); + if (result.isArray()) + return result; + else + throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); + + } + std::string balanceAt(const std::string& address, const int& block) throw (jsonrpc::JsonRpcException) { Json::Value p; @@ -163,30 +175,6 @@ p["s"] = s; } - std::string key() throw (jsonrpc::JsonRpcException) - { - Json::Value p; - p = Json::nullValue; - Json::Value result = this->client->CallMethod("key",p); - if (result.isString()) - return result.asString(); - else - throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); - - } - - Json::Value keys() throw (jsonrpc::JsonRpcException) - { - Json::Value p; - p = Json::nullValue; - Json::Value result = this->client->CallMethod("keys",p); - if (result.isArray()) - return result; - else - throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); - - } - bool killWatch(const int& id) throw (jsonrpc::JsonRpcException) { Json::Value p;