Skip to content

Commit 454fc48

Browse files
committed
console ui implementation of generate
1 parent 8ab1923 commit 454fc48

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/qt/rpcconsole.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <QVariant>
4949

5050
#include <chrono>
51+
#include <regex>
5152

5253
const int CONSOLE_HISTORY = 50;
5354
const int INITIAL_TRAFFIC_GRAPH_MINS = 30;
@@ -414,7 +415,34 @@ void RPCExecutor::request(const QString &command, const WalletModel* wallet_mode
414415
{
415416
std::string result;
416417
std::string executableCommand = command.toStdString() + "\n";
417-
418+
std::regex validRegex("generate\\s*[\\(]?\\s*(\\d+)?\\s*[,]?\\s*(\\d+)?\\s*[\\)]?\\s*");
419+
std::regex invalidRegex("generate[\\s\\(]+.*");
420+
std::smatch match;
421+
if (std::regex_match(executableCommand, match, validRegex)) {
422+
std::string nblocks=match[1];
423+
std::string maxtries=match[2];
424+
if (nblocks=="") nblocks = "1";
425+
if (maxtries=="") maxtries = "1000000";
426+
if (!RPCConsole::RPCExecuteCommandLine(m_node, result, "getnewaddress\n", nullptr, wallet_model)) {
427+
Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
428+
}
429+
else {
430+
std::string address = result;
431+
if (!RPCConsole::RPCExecuteCommandLine(m_node, result, "generatetoaddress " + nblocks + " " + address + " " + maxtries + "\n", nullptr, wallet_model)) {
432+
Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
433+
} else {
434+
std::string answer = "{\n \"address\": \"" + address + "\",\n \"blocks\": "+ result + "\n}";
435+
Q_EMIT reply(RPCConsole::CMD_REPLY, QString::fromStdString( "\n" + answer + "\n\n" ));
436+
}
437+
}
438+
return;
439+
}else {
440+
//If the previous regex failed, but the following is valid - i.e. starts with 'generate' - there is a syntax error with parameters
441+
if (std::regex_search(executableCommand, match, invalidRegex)) {
442+
Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error."));
443+
return;
444+
}
445+
}
418446
// Catch the console-only-help command before RPC call is executed and reply with help text as-if a RPC reply.
419447
if(executableCommand == "help-console\n") {
420448
Q_EMIT reply(RPCConsole::CMD_REPLY, QString(("\n"

0 commit comments

Comments
 (0)