forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathmnemonicverificationdialog.cpp
More file actions
475 lines (415 loc) · 17.4 KB
/
mnemonicverificationdialog.cpp
File metadata and controls
475 lines (415 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// Copyright (c) 2024-2025 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#if defined(HAVE_CONFIG_H)
#include <config/bitcoin-config.h>
#endif
#include <qt/mnemonicverificationdialog.h>
#include <qt/forms/ui_mnemonicverificationdialog.h>
#include <qt/guiutil.h>
#include <QDialogButtonBox>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QRandomGenerator>
#include <QRegularExpression>
#include <QScrollArea>
#include <QSet>
#include <QMessageBox>
#include <algorithm>
MnemonicVerificationDialog::MnemonicVerificationDialog(const SecureString& mnemonic, QWidget *parent, bool viewOnly) :
QDialog(parent, GUIUtil::dialog_flags),
ui(new Ui::MnemonicVerificationDialog),
m_mnemonic(mnemonic),
m_view_only(viewOnly)
{
ui->setupUi(this);
if (auto w = findChild<QWidget*>("mnemonicGridWidget")) {
m_gridLayout = qobject_cast<QGridLayout*>(w->layout());
}
// Keep minimum small so the page can compress when users scale down
setMinimumSize(QSize(550, 360));
resize(minimumSize());
setWindowTitle(m_view_only ? tr("Your Recovery Phrase") : tr("Save Your Mnemonic"));
// Words will be parsed on-demand to minimize exposure time in non-secure memory
// m_words is intentionally left empty initially
// Trim outer paddings and inter-item spacing to avoid over-padded look
if (auto mainLayout = findChild<QVBoxLayout*>("verticalLayout")) {
mainLayout->setContentsMargins(8, 4, 8, 6);
mainLayout->setSpacing(6);
}
if (auto s1 = findChild<QVBoxLayout*>("verticalLayout_step1")) {
s1->setContentsMargins(8, 4, 8, 6);
s1->setSpacing(6);
}
if (auto s2 = findChild<QVBoxLayout*>("verticalLayout_step2")) {
s2->setContentsMargins(8, 2, 8, 6);
s2->setSpacing(4);
s2->setAlignment(Qt::AlignTop);
}
if (ui->formLayout) {
ui->formLayout->setContentsMargins(0, 0, 0, 0);
ui->formLayout->setVerticalSpacing(3);
ui->formLayout->setHorizontalSpacing(8);
}
if (ui->buttonBox) {
ui->buttonBox->setContentsMargins(0, 0, 0, 0);
ui->buttonBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
}
// Prefer compact default; we will adjust per-step to sizeHint
ui->stackedWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
// Ensure buttonBox is hidden initially (will be shown in step2)
ui->buttonBox->hide();
setupStep1();
adjustSize();
m_defaultSize = size();
// Connections
connect(ui->showMnemonicButton, &QPushButton::clicked, this, &MnemonicVerificationDialog::onShowMnemonicClicked);
connect(ui->hideMnemonicButton, &QPushButton::clicked, this, &MnemonicVerificationDialog::onHideMnemonicClicked);
if (!m_view_only) {
connect(ui->writtenDownCheckbox, &QCheckBox::toggled, this, [this](bool checked) {
if (checked && m_has_ever_revealed) setupStep2();
});
connect(ui->word1Edit, &QLineEdit::textChanged, this, &MnemonicVerificationDialog::onWord1Changed);
connect(ui->word2Edit, &QLineEdit::textChanged, this, &MnemonicVerificationDialog::onWord2Changed);
connect(ui->word3Edit, &QLineEdit::textChanged, this, &MnemonicVerificationDialog::onWord3Changed);
}
// Button box
ui->buttonBox->button(QDialogButtonBox::Ok)->setText(m_view_only ? tr("Close") : tr("Continue"));
GUIUtil::handleCloseWindowShortcut(this);
}
MnemonicVerificationDialog::~MnemonicVerificationDialog()
{
delete ui;
}
void MnemonicVerificationDialog::setupStep1()
{
ui->stackedWidget->setCurrentIndex(0);
buildMnemonicGrid(false);
ui->hideMnemonicButton->hide();
ui->showMnemonicButton->show();
ui->writtenDownCheckbox->setEnabled(false);
ui->writtenDownCheckbox->setChecked(false);
m_mnemonic_revealed = false;
// In view-only mode, hide the checkbox and show buttonBox immediately
if (m_view_only) {
ui->writtenDownCheckbox->hide();
ui->buttonBox->show();
// Hide Cancel button in view-only mode - only Close button is needed
if (QAbstractButton* cancelBtn = ui->buttonBox->button(QDialogButtonBox::Cancel)) {
cancelBtn->hide();
}
} else {
ui->writtenDownCheckbox->show();
ui->buttonBox->hide();
}
// Restore original minimum size (in case we came back from Step 2)
setMinimumSize(QSize(550, 360));
// Restore to default size if we have it (when coming back from Step 2)
if (m_defaultSize.isValid() && !m_defaultSize.isEmpty()) {
resize(m_defaultSize);
} else {
// Compact to content on first load
adjustSize();
}
// Set warning and instruction text with themed colors
// Font sizes and weights are defined in general.css
if (m_view_only) {
ui->warningLabel->setText(
tr("WARNING: Never share your recovery phrase with anyone. Store it securely offline."));
ui->warningLabel->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR));
ui->instructionLabel->setText(
tr("These words can restore your wallet. Keep them safe and private."));
} else {
ui->warningLabel->setText(
tr("WARNING: If you lose your mnemonic seed phrase, you will lose access to your wallet forever. Write it down in a safe place and never share it with anyone."));
ui->warningLabel->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR));
ui->instructionLabel->setText(
tr("Please write down these words in order. You will need them to restore your wallet."));
}
// Reduce extra padding to avoid an over-padded look
if (auto outer = findChild<QVBoxLayout*>("verticalLayout_step1")) {
outer->setContentsMargins(12, 6, 12, 6);
outer->setSpacing(6);
}
if (auto hb = findChild<QHBoxLayout*>("horizontalLayout_buttons")) {
hb->setContentsMargins(0, 4, 0, 0);
hb->setSpacing(10);
}
}
void MnemonicVerificationDialog::setupStep2()
{
ui->stackedWidget->setCurrentIndex(1);
// Parse words for validation (needed in step 2)
parseWords();
// Validate mnemonic has at least 3 words before proceeding
const int wordCount = getWordCount();
if (wordCount < 3) {
QMessageBox::critical(this, tr("Invalid Mnemonic"),
tr("Mnemonic phrase has fewer than 3 words (found %1). Verification cannot proceed.").arg(wordCount));
reject();
return;
}
generateRandomPositions();
// Safety check: ensure positions were generated successfully
if (m_selected_positions.size() < 3) {
QMessageBox::critical(this, tr("Verification Error"),
tr("Failed to generate verification positions. Please try again."));
reject();
return;
}
ui->word1Edit->clear();
ui->word2Edit->clear();
ui->word3Edit->clear();
// Widget sizes are defined in general.css
ui->word1Label->setText(tr("Word #%1:").arg(m_selected_positions[0]));
ui->word2Label->setText(tr("Word #%1:").arg(m_selected_positions[1]));
ui->word3Label->setText(tr("Word #%1:").arg(m_selected_positions[2]));
ui->word1Status->clear();
ui->word2Status->clear();
ui->word3Status->clear();
ui->buttonBox->show();
if (QAbstractButton* cancel = ui->buttonBox->button(QDialogButtonBox::Cancel)) {
cancel->setText(tr("Back"));
}
if (QAbstractButton* cont = ui->buttonBox->button(QDialogButtonBox::Ok)) cont->setEnabled(false);
// Verification label styling is defined in general.css
// Hide any existing title label if present
if (auto titleLabel = findChild<QLabel*>("verifyTitleLabel")) {
titleLabel->hide();
}
// Align content toward top and remove any layout spacers expanding height FIRST
if (auto v = findChild<QVBoxLayout*>("verticalLayout_step2")) {
v->setAlignment(Qt::AlignTop);
// Minimize top padding/margin to eliminate gap at top
QMargins m = v->contentsMargins();
v->setContentsMargins(m.left(), 2, m.right(), m.bottom());
for (int i = 0; i < v->count(); ++i) {
QLayoutItem* it = v->itemAt(i);
if (it && it->spacerItem()) it->spacerItem()->changeSize(0, 0, QSizePolicy::Fixed, QSizePolicy::Fixed);
}
// Force immediate layout update
v->invalidate();
v->update();
}
// Also ensure the main dialog layout has minimal top padding
if (auto mainLayout = findChild<QVBoxLayout*>("verticalLayout")) {
QMargins m = mainLayout->contentsMargins();
mainLayout->setContentsMargins(m.left(), 4, m.right(), m.bottom());
mainLayout->invalidate();
mainLayout->update();
}
// Force widget to recalculate layout immediately
updateGeometry();
// Reduce minimums for verify; open exactly at the minimum size AFTER layout is fixed
setMinimumSize(QSize(460, 280));
resize(minimumSize());
adjustSize();
}
void MnemonicVerificationDialog::generateRandomPositions()
{
m_selected_positions.clear();
const int n = getWordCount();
if (n < 3) {
// Unable to verify; bail out so the dialog can surface an error message upstream.
return;
}
QSet<int> used;
QRandomGenerator* rng = QRandomGenerator::global();
while (m_selected_positions.size() < 3) {
int pos = rng->bounded(1, n + 1);
if (!used.contains(pos)) { used.insert(pos); m_selected_positions.append(pos); }
}
std::sort(m_selected_positions.begin(), m_selected_positions.end());
}
void MnemonicVerificationDialog::onShowMnemonicClicked()
{
buildMnemonicGrid(true);
ui->showMnemonicButton->hide();
ui->hideMnemonicButton->show();
if (!m_view_only) {
ui->writtenDownCheckbox->setEnabled(true);
}
m_mnemonic_revealed = true;
m_has_ever_revealed = true;
}
void MnemonicVerificationDialog::onHideMnemonicClicked()
{
buildMnemonicGrid(false);
ui->hideMnemonicButton->hide();
ui->showMnemonicButton->show();
m_mnemonic_revealed = false;
// Clear parsed words from memory immediately when hiding
m_words.clear();
}
void MnemonicVerificationDialog::reject()
{
// Eagerly clear parsed words to minimize exposure time in memory.
// They will be re-parsed on demand via parseWords() if needed.
m_words.clear();
// close dialog for step-1; return back to step-1 for step-2
if (ui->stackedWidget->currentIndex() == 0) {
QDialog::reject();
} else {
setupStep1();
}
}
void MnemonicVerificationDialog::onWord1Changed() { updateWordValidation(); }
void MnemonicVerificationDialog::onWord2Changed() { updateWordValidation(); }
void MnemonicVerificationDialog::onWord3Changed() { updateWordValidation(); }
bool MnemonicVerificationDialog::validateWord(const QString& word, int position)
{
// Parse words on-demand for validation (minimizes exposure time)
// Words are kept in memory during step 2 (verification) and step 1 (when revealed)
// They are only cleared when explicitly hiding in step 1 or on dialog destruction
std::vector<SecureString> words = parseWords();
if (position < 1 || position > static_cast<int>(words.size())) {
return false;
}
// Convert SecureString to QString temporarily for comparison
QString secureWord{QString::fromUtf8(words[position - 1].data(), words[position - 1].size()).toLower()};
const bool result{word == secureWord};
// Clear temporary QString immediately
secureWord.fill(QChar(0));
secureWord.clear();
secureWord.squeeze();
return result;
}
void MnemonicVerificationDialog::updateWordValidation()
{
const QString t1 = ui->word1Edit->text().trimmed().toLower();
const QString t2 = ui->word2Edit->text().trimmed().toLower();
const QString t3 = ui->word3Edit->text().trimmed().toLower();
const bool ok1 = !t1.isEmpty() && validateWord(t1, m_selected_positions[0]);
const bool ok2 = !t2.isEmpty() && validateWord(t2, m_selected_positions[1]);
const bool ok3 = !t3.isEmpty() && validateWord(t3, m_selected_positions[2]);
// Status labels show checkmarks/X marks with themed colors
// Font weight is defined in general.css
auto setStatus = [](QLabel* lbl, bool filled, bool valid) {
if (!lbl) return;
if (!filled) { lbl->clear(); lbl->setStyleSheet(""); return; }
lbl->setText(valid ? "✓" : "✗");
lbl->setStyleSheet(valid ?
GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_SUCCESS) :
GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_ERROR));
};
setStatus(ui->word1Status, !t1.isEmpty(), ok1);
setStatus(ui->word2Status, !t2.isEmpty(), ok2);
setStatus(ui->word3Status, !t3.isEmpty(), ok3);
if (ui->buttonBox && ui->stackedWidget->currentIndex() == 1) {
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ok1 && ok2 && ok3);
}
}
void MnemonicVerificationDialog::accept()
{
// In view-only mode, skip verification
if (!m_view_only) {
if (!validateWord(ui->word1Edit->text().trimmed().toLower(), m_selected_positions[0]) ||
!validateWord(ui->word2Edit->text().trimmed().toLower(), m_selected_positions[1]) ||
!validateWord(ui->word3Edit->text().trimmed().toLower(), m_selected_positions[2])) {
QMessageBox::warning(this, tr("Verification Failed"), tr("One or more words are incorrect. Please try again."));
return;
}
}
QDialog::accept();
}
std::vector<SecureString> MnemonicVerificationDialog::parseWords()
{
// If words are already parsed, reuse them (for step 2 validation or step 1 display)
if (!m_words.empty()) {
return m_words;
}
// Parse words from secure mnemonic string
QString mnemonicStr{QString::fromUtf8(m_mnemonic.data(), m_mnemonic.size())};
QStringList wordList = mnemonicStr.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
// Convert to SecureString vector for secure storage
m_words.clear();
m_words.reserve(wordList.size());
for (const QString& word : wordList) {
QByteArray utf8 = word.toUtf8();
SecureString secureWord;
secureWord.assign(utf8.constData(), utf8.size());
m_words.push_back(std::move(secureWord));
utf8.fill(0);
}
// Clear the temporary QString immediately after parsing
mnemonicStr.fill(QChar(0));
mnemonicStr.clear();
mnemonicStr.squeeze(); // Release memory
wordList.clear();
return m_words;
}
int MnemonicVerificationDialog::getWordCount() const
{
// Count words without parsing them into vector
// This avoids storing words in non-secure memory unnecessarily
if (m_words.empty()) {
QString mnemonicStr{QString::fromUtf8(m_mnemonic.data(), m_mnemonic.size())};
QStringList words = mnemonicStr.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
int count = words.size();
// Clear immediately
mnemonicStr.clear();
mnemonicStr.squeeze();
words.clear();
return count;
}
return m_words.size();
}
void MnemonicVerificationDialog::buildMnemonicGrid(bool reveal)
{
if (!m_gridLayout) return;
QLayoutItem* child;
while ((child = m_gridLayout->takeAt(0)) != nullptr) {
if (child->widget()) child->widget()->deleteLater();
delete child;
}
// Parse words only when revealing (when needed for display)
std::vector<SecureString> words;
if (reveal) {
words = parseWords();
} else {
// For hidden view, just get count without parsing words
const int n = getWordCount();
const int columns = (n >= 24) ? 4 : 3;
const int rows = (n + columns - 1) / columns;
// Font styling is defined in general.css
m_gridLayout->setContentsMargins(6, 2, 6, 8);
m_gridLayout->setHorizontalSpacing(32);
m_gridLayout->setVerticalSpacing(7);
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < columns; ++c) {
int idx = r * columns + c; if (idx >= n) break;
const QString text = QString("%1. •••••••").arg(idx + 1, 2);
QLabel* lbl = new QLabel(text);
lbl->setTextInteractionFlags(Qt::TextSelectableByMouse);
m_gridLayout->addWidget(lbl, r, c);
}
}
m_gridLayout->setRowMinimumHeight(rows, 12);
return;
}
// Revealed view - words are already parsed
const int n = words.size();
const int columns = (n >= 24) ? 4 : 3;
const int rows = (n + columns - 1) / columns;
// Font styling is defined in general.css
m_gridLayout->setContentsMargins(6, 2, 6, 8);
m_gridLayout->setHorizontalSpacing(32);
m_gridLayout->setVerticalSpacing(7);
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < columns; ++c) {
int idx = r * columns + c; if (idx >= n) break;
// Convert SecureString to QString temporarily for display
QString wordStr{QString::fromUtf8(words[idx].data(), words[idx].size())};
const QString text = QString("%1. %2").arg(idx + 1, 2).arg(wordStr);
QLabel* lbl = new QLabel(text);
lbl->setTextInteractionFlags(Qt::TextSelectableByMouse);
m_gridLayout->addWidget(lbl, r, c);
// Clear temporary QString immediately after use
wordStr.fill(QChar(0));
wordStr.clear();
wordStr.squeeze();
}
}
m_gridLayout->setRowMinimumHeight(rows, 12);
}