Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Wallet] Some sapling wallet improvements #2887

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/qt/addresstablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ QString AddressTableModel::getAddressToShow(bool isShielded) const

for (auto it = wallet->NewAddressBookIterator(); it.IsValid(); it.Next()) {
const auto addrData = it.GetValue();

CWDestination x = *it.GetDestKey();
if (!isShielded) {
if (addrData.purpose == AddressBook::AddressBookPurpose::RECEIVE) {
const auto &address = *it.GetCTxDestKey();
Expand All @@ -622,10 +622,9 @@ QString AddressTableModel::getAddressToShow(bool isShielded) const
}
}
} else {
// todo: add shielded address support to IsUsed
if (addrData.purpose == AddressBook::AddressBookPurpose::SHIELDED_RECEIVE) {
const auto &address = *it.GetShieldedDestKey();
if (IsValidPaymentAddress(address) && IsMine(*wallet, address)) {
if (IsValidPaymentAddress(address) && IsMine(*wallet, address) && !wallet->IsUsed(address)) {
return QString::fromStdString(KeyIO::EncodePaymentAddress(address));
}
}
Expand Down
24 changes: 19 additions & 5 deletions src/sapling/saplingscriptpubkeyman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,11 @@ isminetype SaplingScriptPubKeyMan::IsMine(const CWalletTx& wtx, const SaplingOut

CAmount SaplingScriptPubKeyMan::GetCredit(const CWalletTx& tx, const isminefilter& filter, const bool fUnspent) const
{
// If we are not filtering shield data, return
if (!(filter & ISMINE_WATCH_ONLY_SHIELDED || filter & ISMINE_SPENDABLE_SHIELDED)) {
return 0;
}

if (!tx.tx->IsShieldedTx() || tx.tx->sapData->vShieldedOutput.empty()) {
return 0;
}
Expand All @@ -741,15 +746,21 @@ CAmount SaplingScriptPubKeyMan::GetCredit(const CWalletTx& tx, const isminefilte
(fUnspent && IsSaplingSpent(*noteData.nullifier))) {
continue; // only unspent
}
// todo: check if we can spend this note or not. (if not, then it's a watch only)

nCredit += noteData.amount ? *noteData.amount : 0;
// If we are filtering watch only or we have spend authority add the amount
if ((filter & ISMINE_WATCH_ONLY_SHIELDED) || (noteData.address && HaveSpendingKeyForPaymentAddress(*noteData.address))) {
nCredit += noteData.amount ? *noteData.amount : 0;
}
}
return nCredit;
}

CAmount SaplingScriptPubKeyMan::GetDebit(const CTransaction& tx, const isminefilter& filter) const
{
// If we are not filtering shield data, return
if (!(filter & ISMINE_WATCH_ONLY_SHIELDED || filter & ISMINE_SPENDABLE_SHIELDED)) {
return 0;
}

if (!tx.IsShieldedTx() || tx.sapData->vShieldedSpend.empty()) {
return 0;
}
Expand All @@ -766,9 +777,12 @@ CAmount SaplingScriptPubKeyMan::GetDebit(const CTransaction& tx, const isminefil
auto nit = wtx.mapSaplingNoteData.find(op);
assert(nit != wtx.mapSaplingNoteData.end());
const auto& nd = nit->second;
assert(nd.IsMyNote()); // todo: Add watch only check.
assert(nd.IsMyNote());
assert(static_cast<bool>(nd.amount));
nDebit += *(nd.amount);
// If we are filtering watch only or we have spend authority add the amount
if ((filter & ISMINE_WATCH_ONLY_SHIELDED) || (nd.address && HaveSpendingKeyForPaymentAddress(*nd.address))) {
nDebit += *(nd.amount);
}
if (!Params().GetConsensus().MoneyRange(nDebit))
throw std::runtime_error("SaplingScriptPubKeyMan::GetDebit() : value out of range");
}
Expand Down
17 changes: 17 additions & 0 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,23 @@ bool CWallet::IsUsed(const CTxDestination address) const
return false;
}

bool CWallet::IsUsed(const libzcash::SaplingPaymentAddress address) const
{
LOCK(cs_wallet);
if (!::IsMine(*this, address)) {
return false;
}

for (const auto& it : mapWallet) {
const CWalletTx& wtx = it.second;
for (const auto& txout : wtx.mapSaplingNoteData) {
if (txout.second.address && *txout.second.address == address)
return true;
}
}
return false;
}

CAmount CWallet::GetDebit(const CTxIn& txin, const isminefilter& filter) const
{
{
Expand Down
1 change: 1 addition & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
bool CreateBudgetFeeTX(CTransactionRef& tx, const uint256& hash, CReserveKey& keyChange, CAmount fee);

bool IsUsed(const CTxDestination address) const;
bool IsUsed(const libzcash::SaplingPaymentAddress address) const;

isminetype IsMine(const CTxIn& txin) const;
CAmount GetDebit(const CTxIn& txin, const isminefilter& filter) const;
Expand Down