From c8bbf5a0b097d61afcd91f52ae9da5c72e19b573 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Sun, 26 Jul 2015 00:08:46 +0200 Subject: [PATCH] ethconsole improvements. Store session key in user's homedir for ease of use of ethconsole. --- eth/main.cpp | 16 +++++++++------- ethconsole/CMakeLists.txt | 1 + ethconsole/main.cpp | 26 ++++++++++++++++++++++---- libjsconsole/JSConsole.h | 6 ++++-- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/eth/main.cpp b/eth/main.cpp index b50729197..5a3fec6df 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -1085,7 +1085,7 @@ int main(int argc, char** argv) NodeMode nodeMode = NodeMode::Full; bool interactive = false; #if ETH_JSONRPC || !ETH_TRUE - int jsonrpc = -1; + int jsonRPCURL = -1; #endif string jsonAdmin; string genesisJSON; @@ -1434,9 +1434,9 @@ int main(int argc, char** argv) interactive = true; #if ETH_JSONRPC || !ETH_TRUE else if ((arg == "-j" || arg == "--json-rpc")) - jsonrpc = jsonrpc == -1 ? SensibleHttpPort : jsonrpc; + jsonRPCURL = jsonRPCURL == -1 ? SensibleHttpPort : jsonRPCURL; else if (arg == "--json-rpc-port" && i + 1 < argc) - jsonrpc = atoi(argv[++i]); + jsonRPCURL = atoi(argv[++i]); else if (arg == "--json-admin" && i + 1 < argc) jsonAdmin = argv[++i]; #endif @@ -1743,15 +1743,15 @@ int main(int argc, char** argv) else cout << "Networking disabled. To start, use netstart or pass -b or a remote host." << endl; - if (useConsole && jsonrpc == -1) - jsonrpc = SensibleHttpPort; + if (useConsole && jsonRPCURL == -1) + jsonRPCURL = SensibleHttpPort; #if ETH_JSONRPC || !ETH_TRUE shared_ptr jsonrpcServer; unique_ptr jsonrpcConnector; - if (jsonrpc > -1) + if (jsonRPCURL > -1) { - jsonrpcConnector = unique_ptr(new jsonrpc::HttpServer(jsonrpc, "", "", SensibleHttpThreads)); + jsonrpcConnector = unique_ptr(new jsonrpc::HttpServer(jsonRPCURL, "", "", SensibleHttpThreads)); jsonrpcServer = make_shared(*jsonrpcConnector.get(), web3, make_shared([&](){ return web3.ethereum(); }, getAccountPassword, keyManager), vector(), keyManager, *gasPricer); jsonrpcServer->setMiningBenefactorChanger([&](Address const& a) { beneficiary = a; }); jsonrpcServer->StartListening(); @@ -1760,6 +1760,8 @@ int main(int argc, char** argv) else jsonrpcServer->addSession(jsonAdmin, SessionPermissions{{Priviledge::Admin}}); cout << "JSONRPC Admin Session Key: " << jsonAdmin << endl; + writeFile(getDataDir("web3") + "/session.key", jsonAdmin); + writeFile(getDataDir("web3") + "/session.url", "http://localhost:" + toString(jsonRPCURL)); } #endif diff --git a/ethconsole/CMakeLists.txt b/ethconsole/CMakeLists.txt index 0101320d0..c817add37 100644 --- a/ethconsole/CMakeLists.txt +++ b/ethconsole/CMakeLists.txt @@ -20,6 +20,7 @@ if (DEFINED WIN32 AND NOT DEFINED CMAKE_COMPILER_IS_MINGW) eth_copy_dlls(${EXECUTABLE} CURL_DLLS) endif() target_link_libraries(${EXECUTABLE} jsconsole) +target_link_libraries(${EXECUTABLE} devcore) if (APPLE) install(TARGETS ${EXECUTABLE} DESTINATION bin) diff --git a/ethconsole/main.cpp b/ethconsole/main.cpp index 5df3444eb..435a8d716 100644 --- a/ethconsole/main.cpp +++ b/ethconsole/main.cpp @@ -20,6 +20,7 @@ */ #include +#include #include using namespace std; using namespace dev; @@ -27,13 +28,30 @@ using namespace dev::eth; int main(int argc, char** argv) { - string remote; - if (argc == 1) + string remote = contentsString(getDataDir("web3") + "/session.rpc"); + if (remote.empty()) remote = "http://localhost:8545"; - else if (argc == 2) - remote = argv[1]; + string sessionKey = contentsString(getDataDir("web3") + "/session.url"); + + for (int i = 1; i < argc; ++i) + { + string arg = argv[i]; + if (arg == "--url" && i + 1 < argc) + remote = argv[++i]; + else if (arg == "--session-key" && i + 1 < argc) + sessionKey = argv[++i]; + else + { + cerr << "Invalid argument: " << arg << endl; + exit(-1); + } + } JSRemoteConsole console(remote); + + if (!sessionKey.empty()) + console.eval("web3.admin.setSessionKey('" + sessionKey + "')"); + while (true) console.readExpression(); diff --git a/libjsconsole/JSConsole.h b/libjsconsole/JSConsole.h index fc190a637..46726f6cb 100644 --- a/libjsconsole/JSConsole.h +++ b/libjsconsole/JSConsole.h @@ -46,9 +46,9 @@ public: std::string cmd = ""; g_logPost = [](std::string const& a, char const*) { - std::cout << "\r \r" << a << std::endl << std::flush; + std::cout << "\r \r" << a << std::endl << std::flush; #if ETH_READLINE - rl_forced_update_display(); + rl_forced_update_display(); #endif }; @@ -92,6 +92,8 @@ public: } } + void eval(std::string const& _expression) { m_engine.eval(_expression.c_str()); } + protected: Engine m_engine; Printer m_printer;