Skip to content
Bitcoin Roots

v29.4-roots.3 · Commit 28: [PATCH 28/30] fix(wallet): secure asynchronous sweep cancellation

src/qt/walletmodel.cpp

Before: src/qt/walletmodel.cpp · After: src/qt/walletmodel.cpp

File metadata
index e051aae59971f7336bbc222bb35552ac293c10b0..268c8f351f0895bbb404115ea7317d9c6f6d69eb 100644

Download original patch · Release notes

Diff rows 1–35 of 35. Line numbers refer to the original before/after files.

+ Added− RemovedUnmarked lines are context.

Scroll the code area horizontally to read long lines. The two line-number columns refer to before and after.

src/qt/walletmodel.cpp CHANGED
@@ -67,19 +67,24 @@ WalletModel::~WalletModel()
67
67
  unsubscribeFromCoreSignals();
68
68
  }
69
69
 
70
- uint64_t WalletModel::invalidateSweepRequests()
70
+ bool WalletModel::invalidateSweepRequests()
71
71
  {
72
+ if (m_sweep_authorization && !m_sweep_authorization->Cancel()) return false;
72
73
  ++m_sweep_request_generation;
74
+ m_sweep_authorization.reset();
73
75
  Q_EMIT sweepRequestInvalidated(m_sweep_request_generation);
74
- return m_sweep_request_generation;
76
+ return true;
75
77
  }
76
78
 
77
79
  uint64_t WalletModel::requestSweep(SecureString private_key, CTxDestination destination, bool broadcast,
78
80
  std::optional<uint256> expected_txid)
79
81
  {
82
+ if (m_sweep_authorization && !m_sweep_authorization->Cancel()) return 0;
80
83
  const uint64_t generation{++m_sweep_request_generation};
81
- QMetaObject::invokeMethod(&m_sweep_context, [this, generation, private_key = std::move(private_key), destination = std::move(destination), broadcast, expected_txid]() mutable {
82
- auto result{m_wallet->sweepPrivateKey(private_key, destination, broadcast, expected_txid)};
84
+ auto authorization{std::make_shared<interfaces::SweepAuthorization>()};
85
+ m_sweep_authorization = authorization;
86
+ QMetaObject::invokeMethod(&m_sweep_context, [this, generation, authorization, private_key = std::move(private_key), destination = std::move(destination), broadcast, expected_txid]() mutable {
87
+ auto result{m_wallet->sweepPrivateKey(private_key, destination, broadcast, expected_txid, authorization)};
83
88
  private_key.clear();
84
89
 
85
90
  const bool success{result.has_value()};
@@ -92,6 +97,7 @@ uint64_t WalletModel::requestSweep(SecureString private_key, CTxDestination dest
92
97
  const QString txid{success && result->tx ? QString::fromStdString(result->tx->GetHash().ToString()) : QString{}};
93
98
  QMetaObject::invokeMethod(this, [this, generation, broadcast, success, error, amount, fee, inputs, vsize, rbf, txid] {
94
99
  if (generation != m_sweep_request_generation) return;
100
+ m_sweep_authorization.reset();
95
101
  Q_EMIT sweepFinished(generation, broadcast, success, error, amount, fee, inputs, vsize, rbf, txid);
96
102
  }, Qt::QueuedConnection);
97
103
  }, Qt::QueuedConnection);