diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index a07686ab2b7..e1e8f737037 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; @@ -414,7 +415,33 @@ 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)) { + const std::string nblocks{match.str(1) == "" ? "1" : match.str(1)}; + const std::string maxtries{match.str(2) == "" ? "1000000" : match.str(2)}; + + 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"