forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathmasternodewidgets.cpp
More file actions
347 lines (304 loc) · 11.9 KB
/
Copy pathmasternodewidgets.cpp
File metadata and controls
347 lines (304 loc) · 11.9 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
// Copyright (c) 2026 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <qt/masternodewidgets.h>
#include <bls/bls.h>
#include <interfaces/wallet.h>
#include <key_io.h>
#include <script/standard.h>
#include <support/cleanse.h>
#include <util/strencodings.h>
#include <qt/bitcoinunits.h>
#include <qt/guiutil.h>
#include <qt/optionsmodel.h>
#include <qt/qvalidatedlineedit.h>
#include <qt/walletmodel.h>
#include <QButtonGroup>
#include <QFrame>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QRadioButton>
#include <QRegularExpression>
#include <QVBoxLayout>
#include <QVariant>
#include <algorithm>
#include <map>
#include <string>
#include <utility>
#include <variant>
#include <vector>
namespace MasternodeWidgetUtil {
QStringList tokenizeEndpointList(const QString& input)
{
static const QRegularExpression separator{"[,\\s]+"};
return input.split(separator, Qt::SkipEmptyParts);
}
bool isP2PKHAddress(const QString& address)
{
const CTxDestination dest{DecodeDestination(address.trimmed().toStdString())};
return std::holds_alternative<PKHash>(dest);
}
bool isP2PKHorP2SHAddress(const QString& address)
{
const CTxDestination dest{DecodeDestination(address.trimmed().toStdString())};
return std::holds_alternative<PKHash>(dest) || std::holds_alternative<ScriptHash>(dest);
}
QLabel* makeTitle(const QString& text, QWidget* parent, double point_size)
{
auto* label{new QLabel(text, parent)};
label->setWordWrap(true);
GUIUtil::setFont({label}, GUIUtil::FontWeight::Bold, point_size);
return label;
}
QLabel* makeHint(const QString& text, QWidget* parent)
{
auto* label{new QLabel(text, parent)};
label->setWordWrap(true);
label->setStyleSheet(GUIUtil::getThemedStyleQString(GUIUtil::ThemedStyle::TS_SECONDARY));
return label;
}
QLabel* makeValue(const QString& text, QWidget* parent, bool monospace)
{
auto* label{new QLabel(text, parent)};
label->setTextFormat(Qt::PlainText);
label->setWordWrap(true);
label->setTextInteractionFlags(Qt::TextSelectableByMouse);
if (monospace) label->setFont(GUIUtil::fixedPitchFont());
return label;
}
QString chunked(const QString& text, int chunk_size)
{
if (chunk_size <= 0) return text;
QString ret;
ret.reserve(text.size() + text.size() / chunk_size);
for (int pos = 0; pos < text.size(); pos += chunk_size) {
if (pos > 0) ret += QLatin1Char(' ');
ret += text.mid(pos, chunk_size);
}
return ret;
}
QWidget* makeCopyableValue(const QString& display, const QString& copy_text, QWidget* parent)
{
auto* row_widget{new QWidget(parent)};
auto* row{new QHBoxLayout(row_widget)};
row->setContentsMargins(0, 0, 0, 0);
row->setSpacing(TITLE_SPACING);
auto* value{makeValue(display, row_widget, /*monospace=*/true)};
value->setToolTip(copy_text);
row->addWidget(value, /*stretch=*/1);
auto* copy{new QPushButton(QObject::tr("Copy"), row_widget)};
copy->setToolTip(QObject::tr("Copy to clipboard"));
QObject::connect(copy, &QPushButton::clicked, copy, [copy_text] { GUIUtil::setClipboard(copy_text); });
row->addWidget(copy, /*stretch=*/0, Qt::AlignTop);
return row_widget;
}
QFrame* makeCard(QWidget* parent)
{
auto* card{new QFrame(parent)};
card->setObjectName("mnCard");
return card;
}
OptionCard makeOptionCard(QWidget* parent, QWidget* header, const QString& hint)
{
OptionCard ret;
ret.card = makeCard(parent);
auto* card_layout{new QVBoxLayout(ret.card)};
card_layout->setContentsMargins(CARD_PADDING, CARD_PADDING, CARD_PADDING, CARD_PADDING);
card_layout->setSpacing(TITLE_SPACING);
card_layout->addWidget(header);
if (!hint.isEmpty()) {
auto* hint_row{new QHBoxLayout()};
hint_row->setContentsMargins(BODY_INDENT, 0, 0, 0);
hint_row->addWidget(makeHint(hint, ret.card), /*stretch=*/1);
card_layout->addLayout(hint_row);
}
ret.body = new QWidget(ret.card);
ret.body_layout = new QVBoxLayout(ret.body);
ret.body_layout->setContentsMargins(BODY_INDENT, 0, 0, 0);
ret.body_layout->setSpacing(ROW_SPACING);
card_layout->addWidget(ret.body);
return ret;
}
} // namespace MasternodeWidgetUtil
FeeSourcePicker::FeeSourcePicker(QWidget* parent) :
QComboBox(parent)
{
setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon);
setMinimumContentsLength(40);
}
void FeeSourcePicker::setWalletModel(WalletModel* wallet_model)
{
m_wallet_model = wallet_model;
refresh();
}
void FeeSourcePicker::setAutomaticOption(const QString& label)
{
if (m_automatic_label == label) return;
m_automatic_label = label;
refresh();
}
void FeeSourcePicker::setMinimumBalance(CAmount minimum)
{
if (m_minimum_balance == minimum) return;
m_minimum_balance = minimum;
refresh();
}
void FeeSourcePicker::setExcludedOutpoint(std::optional<COutPoint> outpoint)
{
if (m_excluded_outpoint == outpoint) return;
m_excluded_outpoint = std::move(outpoint);
refresh();
}
void FeeSourcePicker::refresh()
{
clear();
if (!m_automatic_label.isEmpty()) addItem(m_automatic_label, QVariant{});
if (m_wallet_model == nullptr) return;
// Group each coin under its own scriptPubKey destination: the RPCs'
// FundSpecialTx only selects coins paying exactly the chosen address, while
// listCoins() groups change outputs under their parent non-change address,
// which would overstate what a protx call can actually spend.
std::map<CTxDestination, CAmount> balances;
for (const auto& [dest, coins] : m_wallet_model->wallet().listCoins()) {
for (const auto& [outpoint, txout] : coins) {
if (m_excluded_outpoint && outpoint == *m_excluded_outpoint) continue;
if (txout.is_spent || txout.depth_in_main_chain < 0) continue;
if (m_wallet_model->wallet().isLockedCoin(outpoint)) continue;
if (!m_wallet_model->wallet().isSpendable(txout.txout.scriptPubKey)) continue;
CTxDestination coin_dest;
if (!ExtractDestination(txout.txout.scriptPubKey, coin_dest)) continue;
balances[coin_dest] += txout.txout.nValue;
}
}
std::vector<std::pair<QString, CAmount>> entries;
for (const auto& [dest, total] : balances) {
if (total <= 0 || total < m_minimum_balance) continue;
entries.emplace_back(QString::fromStdString(EncodeDestination(dest)), total);
}
std::sort(entries.begin(), entries.end(),
[](const auto& lhs, const auto& rhs) { return lhs.second > rhs.second; });
const auto unit{m_wallet_model->getOptionsModel()->getDisplayUnit()};
for (const auto& [address, balance] : entries) {
addItem(QString("%1 (%2)").arg(address, BitcoinUnits::formatWithUnit(unit, balance, /*plussign=*/false,
BitcoinUnits::SeparatorStyle::ALWAYS)),
address);
}
if (count() > 0) setCurrentIndex(0);
}
QString FeeSourcePicker::selectedAddress() const
{
return currentData().toString();
}
OperatorKeyWidget::OperatorKeyWidget(QWidget* parent) :
QWidget(parent)
{
using namespace MasternodeWidgetUtil;
// Generate the key pair up front so toggling the radios never discards a
// key the user may already have seen.
CBLSSecretKey secret_key;
secret_key.MakeNewKey();
auto generated_secret{secret_key.ToByteVector(/*specificLegacyScheme=*/false)};
m_generated_secret.assign(generated_secret.begin(), generated_secret.end());
if (!generated_secret.empty()) memory_cleanse(generated_secret.data(), generated_secret.size());
m_generated_public = QString::fromStdString(secret_key.GetPublicKey().ToString(/*specificLegacyScheme=*/false));
m_generate_radio = new QRadioButton(tr("Generate a new operator key"), this);
m_generate_radio->setChecked(true);
auto generate_card{makeOptionCard(this, m_generate_radio,
tr("The secret key is shown and confirmed before registering, then kept "
"nowhere else."))};
m_generate_body = generate_card.body;
// The key is 96 hex characters: it gets its own full-width line, otherwise it
// wraps against the edge of whatever sits beside it
generate_card.body_layout->addWidget(makeHint(tr("Public key"), m_generate_body));
generate_card.body_layout->addWidget(makeValue(chunked(m_generated_public), m_generate_body,
/*monospace=*/true));
m_existing_radio = new QRadioButton(tr("Use an existing operator public key"), this);
auto existing_card{makeOptionCard(this, m_existing_radio,
tr("For a hosted node: the operator keeps the secret key and gives you the "
"public key."))};
m_existing_body = existing_card.body;
m_existing_edit = new QValidatedLineEdit(m_existing_body);
m_existing_edit->setPlaceholderText(tr("Operator BLS public key (96 hexadecimal characters, basic scheme)"));
m_existing_edit->setEnabled(false);
existing_card.body_layout->addWidget(m_existing_edit);
// The radios live in separate cards, so auto-exclusivity by parent no
// longer applies and a button group has to keep them exclusive.
auto* group{new QButtonGroup(this)};
group->addButton(m_generate_radio);
group->addButton(m_existing_radio);
auto* layout{new QVBoxLayout(this)};
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(TITLE_SPACING);
layout->addWidget(generate_card.card);
layout->addWidget(existing_card.card);
connect(m_generate_radio, &QRadioButton::toggled, this, &OperatorKeyWidget::updateState);
connect(m_existing_radio, &QRadioButton::toggled, this, &OperatorKeyWidget::updateState);
connect(m_existing_edit, &QLineEdit::textChanged, this, &OperatorKeyWidget::updateState);
updateState();
}
OperatorKeyWidget::~OperatorKeyWidget()
{
clearSecret(m_generated_secret);
}
void OperatorKeyWidget::updateState()
{
const Mode current{mode()};
m_generate_body->setVisible(current == Mode::GenerateOnly);
m_existing_body->setVisible(current == Mode::Existing);
m_existing_edit->setEnabled(current == Mode::Existing);
if (current == Mode::Existing) {
const QString text{m_existing_edit->text().trimmed()};
// Leave the neutral style while empty; only flag actual invalid input
m_existing_edit->setValid(text.isEmpty() || isValid());
}
Q_EMIT changed();
}
OperatorKeyWidget::Mode OperatorKeyWidget::mode() const
{
if (m_existing_radio->isChecked()) return Mode::Existing;
return Mode::GenerateOnly;
}
QString OperatorKeyWidget::publicKeyHex() const
{
switch (mode()) {
case Mode::GenerateOnly:
return m_generated_public;
case Mode::Existing:
return isValid() ? m_existing_edit->text().trimmed() : QString();
}
return {};
}
QString OperatorKeyWidget::secretHex() const
{
switch (mode()) {
case Mode::GenerateOnly:
return secretDisplay(m_generated_secret);
case Mode::Existing:
return {};
}
return {};
}
QString OperatorKeyWidget::secretDisplay(Span<const unsigned char> secret)
{
std::string encoded{HexStr(secret)};
QString display{QString::fromStdString(encoded)};
if (!encoded.empty()) memory_cleanse(encoded.data(), encoded.size());
return display;
}
void OperatorKeyWidget::clearSecret(SecureVector& secret)
{
if (!secret.empty()) memory_cleanse(secret.data(), secret.size());
secret.clear();
secret.shrink_to_fit();
}
bool OperatorKeyWidget::hasGeneratedSecret() const
{
return mode() == Mode::GenerateOnly;
}
bool OperatorKeyWidget::isValid() const
{
if (hasGeneratedSecret()) return !m_generated_public.isEmpty();
CBLSPublicKey pubkey;
return pubkey.SetHexStr(m_existing_edit->text().trimmed().toStdString(), /*specificLegacyScheme=*/false);
}