Skip to content

Commit 211ef5a

Browse files
committed
feat(qt): add /clearhistory command
Closes #204
1 parent eeb9cc1 commit 211ef5a

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/qt/rpcconsole.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,55 @@ void RPCConsole::WriteCommandHistory()
691691
settings.endArray();
692692
}
693693

694+
void RPCConsole::ClearCommandHistory()
695+
{
696+
// Overwrite each command in history with dummy data before clearing
697+
// This helps ensure the sensitive data is overwritten in memory
698+
for (QString& cmd : history) {
699+
// Overwrite with characters of same length (don't store new length info)
700+
cmd.fill('x'); // First pass: fill with 'x'
701+
cmd.fill('\0'); // Second pass: fill with null chars
702+
}
703+
704+
// Overwrite saved history in settings with dummy data
705+
QSettings settings;
706+
707+
// First pass: read existing commands and overwrite with dummy data of same length
708+
QStringList dummy_commands;
709+
int size = settings.beginReadArray("nRPCConsoleWindowHistory");
710+
for (int i = 0; i < size; ++i) {
711+
settings.setArrayIndex(i);
712+
QString cmd = settings.value("cmd").toString();
713+
// Store dummy command with same length as original (don't leak length info)
714+
dummy_commands.append(QString(cmd.size(), 'x'));
715+
}
716+
settings.endArray();
717+
718+
// Write dummy data to overwrite the original commands
719+
if (!dummy_commands.empty()) {
720+
settings.beginWriteArray("nRPCConsoleWindowHistory");
721+
for (int i = 0; i < dummy_commands.size(); ++i) {
722+
settings.setArrayIndex(i);
723+
settings.setValue("cmd", dummy_commands[i]);
724+
}
725+
settings.endArray();
726+
settings.sync(); // Force to disk
727+
}
728+
729+
// Clear the history list
730+
history.clear();
731+
historyPtr = 0;
732+
cmdBeforeBrowsing.clear();
733+
734+
// Second pass: write empty history to settings
735+
settings.beginWriteArray("nRPCConsoleWindowHistory");
736+
// Empty array - no entries written
737+
settings.endArray();
738+
739+
// Force settings to sync to disk
740+
settings.sync();
741+
}
742+
694743
RPCConsole::~RPCConsole()
695744
{
696745
QSettings settings;
@@ -1182,6 +1231,26 @@ void RPCConsole::on_lineEdit_returnPressed()
11821231
return;
11831232
}
11841233

1234+
// Special command to clear command history
1235+
if (cmd == QLatin1String("/clearhistory")) {
1236+
QMessageBox::StandardButton reply = QMessageBox::question(this,
1237+
tr("Clear Command History"),
1238+
tr("This will permanently clear your command history and console output.<br><br>"
1239+
"While this action is irreversible, it cannot be guaranteed to be "
1240+
"completely irrecoverable from disk.<br><br>"
1241+
"Are you sure you want to proceed?"),
1242+
QMessageBox::Yes | QMessageBox::No,
1243+
QMessageBox::No);
1244+
1245+
if (reply == QMessageBox::Yes) {
1246+
ClearCommandHistory();
1247+
clear(false); // Clear console output too
1248+
message(CMD_REPLY, tr("Command history and console output cleared."));
1249+
}
1250+
ui->lineEdit->clear();
1251+
return;
1252+
}
1253+
11851254
if (m_is_executing) {
11861255
return;
11871256
}

src/qt/rpcconsole.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ public Q_SLOTS:
166166
void startExecutor();
167167
void setTrafficGraphRange(int mins);
168168
void WriteCommandHistory();
169+
void ClearCommandHistory();
169170

170171
enum ColumnWidths
171172
{

0 commit comments

Comments
 (0)