From 4fd7c14c765087644223f01145e69b8092758e95 Mon Sep 17 00:00:00 2001 From: Hernan Marino Date: Sun, 25 Dec 2022 22:51:50 -0300 Subject: [PATCH] console ui implementation of generate --- src/qt/rpcconsole.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index a07686ab2b7..03b017b36ff 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -48,6 +48,7 @@ #include #include +#include const int CONSOLE_HISTORY = 50; const int INITIAL_TRAFFIC_GRAPH_MINS = 30; @@ -415,6 +416,37 @@ void RPCExecutor::request(const QString &command, const WalletModel* wallet_mode std::string result; std::string executableCommand = command.toStdString() + "\n"; + std::regex validRegex("generate\\s*[\\(]?\\s*(\\d+)?\\s*[,]?\\s*(\\d+)?\\s*[\\)]?\\s*"); + std::regex invalidRegex("generate[\\s\\(]+.*"); + std::smatch match; + + + if (std::regex_match(executableCommand, match, validRegex)) { + std::string nblocks=match[1]; + std::string maxtries=match[2]; + if (nblocks=="") nblocks = "1"; + if (maxtries=="") maxtries = "1000000"; + + if (!RPCConsole::RPCExecuteCommandLine(m_node, result, "getnewaddress\n", nullptr, wallet_model)) { + Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \"")); + } + else { + std::string address = result; + if (!RPCConsole::RPCExecuteCommandLine(m_node, result, "generatetoaddress " + nblocks + " " + address + " " + maxtries + "\n", nullptr, wallet_model)) { + Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \"")); + } else { + std::string answer = "{\n \"address\": \"" + address + "\",\n \"blocks\": "+ result + "\n}"; + Q_EMIT reply(RPCConsole::CMD_REPLY, QString::fromStdString( "\n" + answer + "\n\n" )); + } + } + return; + }else { + //If the previous regex failed, but the following is valid - i.e. starts with 'generate' - there is a syntax error with parameters + if (std::regex_search(executableCommand, match, invalidRegex)) { + Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error.")); + return; + } + } // Catch the console-only-help command before RPC call is executed and reply with help text as-if a RPC reply. if(executableCommand == "help-console\n") { Q_EMIT reply(RPCConsole::CMD_REPLY, QString(("\n"