Skip to content

Commit

Permalink
Add IsLegacy to CWallet so that the GUI knows whether to show watchonly
Browse files Browse the repository at this point in the history
Summary: Backport of Core [[bitcoin/bitcoin#16528 | PR16528]] [35/43] : bitcoin/bitcoin@ce24a94

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D8458
  • Loading branch information
achow101 authored and deadalnix committed Nov 19, 2020
1 parent 73cf127 commit 36c7820
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/interfaces/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ namespace {
return m_wallet->m_default_max_tx_fee;
}
void remove() override { RemoveWallet(m_wallet); }
bool isLegacy() override { return m_wallet->IsLegacy(); }
std::unique_ptr<Handler> handleUnload(UnloadFn fn) override {
return MakeHandler(m_wallet->NotifyUnload.connect(fn));
}
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ class Wallet {
// Remove wallet.
virtual void remove() = 0;

//! Return whether is a legacy wallet
virtual bool isLegacy() = 0;

//! Register handler for unload message.
using UnloadFn = std::function<void()>;
virtual std::unique_ptr<Handler> handleUnload(UnloadFn fn) = 0;
Expand Down
78 changes: 47 additions & 31 deletions src/qt/overviewpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,22 +165,53 @@ OverviewPage::~OverviewPage() {
void OverviewPage::setBalance(const interfaces::WalletBalances &balances) {
int unit = walletModel->getOptionsModel()->getDisplayUnit();
m_balances = balances;
if (walletModel->wallet().privateKeysDisabled()) {
ui->labelBalance->setText(
BitcoinUnits::formatWithUnit(unit, balances.watch_only_balance,
false, BitcoinUnits::separatorAlways));
ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(
unit, balances.unconfirmed_watch_only_balance, false,
BitcoinUnits::separatorAlways));
ui->labelImmature->setText(BitcoinUnits::formatWithUnit(
unit, balances.immature_watch_only_balance, false,
BitcoinUnits::separatorAlways));
ui->labelTotal->setText(BitcoinUnits::formatWithUnit(
unit,
balances.watch_only_balance +
balances.unconfirmed_watch_only_balance +
balances.immature_watch_only_balance,
false, BitcoinUnits::separatorAlways));
if (walletModel->wallet().isLegacy()) {
if (walletModel->wallet().privateKeysDisabled()) {
ui->labelBalance->setText(BitcoinUnits::formatWithUnit(
unit, balances.watch_only_balance, false,
BitcoinUnits::separatorAlways));
ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(
unit, balances.unconfirmed_watch_only_balance, false,
BitcoinUnits::separatorAlways));
ui->labelImmature->setText(BitcoinUnits::formatWithUnit(
unit, balances.immature_watch_only_balance, false,
BitcoinUnits::separatorAlways));
ui->labelTotal->setText(BitcoinUnits::formatWithUnit(
unit,
balances.watch_only_balance +
balances.unconfirmed_watch_only_balance +
balances.immature_watch_only_balance,
false, BitcoinUnits::separatorAlways));
} else {
ui->labelBalance->setText(BitcoinUnits::formatWithUnit(
unit, balances.balance, false, BitcoinUnits::separatorAlways));
ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(
unit, balances.unconfirmed_balance, false,
BitcoinUnits::separatorAlways));
ui->labelImmature->setText(BitcoinUnits::formatWithUnit(
unit, balances.immature_balance, false,
BitcoinUnits::separatorAlways));
ui->labelTotal->setText(BitcoinUnits::formatWithUnit(
unit,
balances.balance + balances.unconfirmed_balance +
balances.immature_balance,
false, BitcoinUnits::separatorAlways));
ui->labelWatchAvailable->setText(BitcoinUnits::formatWithUnit(
unit, balances.watch_only_balance, false,
BitcoinUnits::separatorAlways));
ui->labelWatchPending->setText(BitcoinUnits::formatWithUnit(
unit, balances.unconfirmed_watch_only_balance, false,
BitcoinUnits::separatorAlways));
ui->labelWatchImmature->setText(BitcoinUnits::formatWithUnit(
unit, balances.immature_watch_only_balance, false,
BitcoinUnits::separatorAlways));
ui->labelWatchTotal->setText(BitcoinUnits::formatWithUnit(
unit,
balances.watch_only_balance +
balances.unconfirmed_watch_only_balance +
balances.immature_watch_only_balance,
false, BitcoinUnits::separatorAlways));
}
} else {
ui->labelBalance->setText(BitcoinUnits::formatWithUnit(
unit, balances.balance, false, BitcoinUnits::separatorAlways));
Expand All @@ -195,21 +226,6 @@ void OverviewPage::setBalance(const interfaces::WalletBalances &balances) {
balances.balance + balances.unconfirmed_balance +
balances.immature_balance,
false, BitcoinUnits::separatorAlways));
ui->labelWatchAvailable->setText(
BitcoinUnits::formatWithUnit(unit, balances.watch_only_balance,
false, BitcoinUnits::separatorAlways));
ui->labelWatchPending->setText(BitcoinUnits::formatWithUnit(
unit, balances.unconfirmed_watch_only_balance, false,
BitcoinUnits::separatorAlways));
ui->labelWatchImmature->setText(BitcoinUnits::formatWithUnit(
unit, balances.immature_watch_only_balance, false,
BitcoinUnits::separatorAlways));
ui->labelWatchTotal->setText(BitcoinUnits::formatWithUnit(
unit,
balances.watch_only_balance +
balances.unconfirmed_watch_only_balance +
balances.immature_watch_only_balance,
false, BitcoinUnits::separatorAlways));
}

// only show immature (newly mined) balance if it's non-zero, so as not to
Expand Down
3 changes: 2 additions & 1 deletion src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2252,7 +2252,8 @@ static UniValue keypoolrefill(const Config &config,
}
.Check(request);

if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
if (pwallet->IsLegacy() &&
pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
throw JSONRPCError(RPC_WALLET_ERROR,
"Error: Private keys are disabled for this wallet");
}
Expand Down
9 changes: 9 additions & 0 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4934,3 +4934,12 @@ void CWallet::SetActiveScriptPubKeyMan(uint256 id, OutputType type,
}
NotifyCanGetAddressesChanged();
}

bool CWallet::IsLegacy() const {
if (m_internal_spk_managers.count(OutputType::LEGACY) == 0) {
return false;
}
auto spk_man = dynamic_cast<LegacyScriptPubKeyMan *>(
m_internal_spk_managers.at(OutputType::LEGACY));
return spk_man != nullptr;
}
3 changes: 3 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,9 @@ class CWallet final : public WalletStorage,
*/
bool SetWalletFlags(uint64_t overwriteFlags, bool memOnly);

/** Determine if we are a legacy wallet */
bool IsLegacy() const;

/**
* Returns a bracketed wallet name for displaying in logs, will return
* [default wallet] if the wallet has no name.
Expand Down
3 changes: 2 additions & 1 deletion src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,8 @@ DBErrors WalletBatch::LoadWallet(CWallet *pwallet) {
wss.nKeys + wss.nCKeys, wss.m_unknown_records);

// nTimeFirstKey is only reliable if all keys have metadata
if ((wss.nKeys + wss.nCKeys + wss.nWatchKeys) != wss.nKeyMeta) {
if (pwallet->IsLegacy() &&
(wss.nKeys + wss.nCKeys + wss.nWatchKeys) != wss.nKeyMeta) {
auto spk_man = pwallet->GetOrCreateLegacyScriptPubKeyMan();
if (spk_man) {
LOCK(spk_man->cs_KeyStore);
Expand Down

0 comments on commit 36c7820

Please sign in to comment.