Skip to content

Commit

Permalink
Return a vector of stakable shield notes
Browse files Browse the repository at this point in the history
  • Loading branch information
panleone committed Apr 7, 2023
1 parent af30a31 commit a73c70d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/sapling/saplingscriptpubkeyman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "sapling/saplingscriptpubkeyman.h"

#include "chain.h" // for CBlockIndex
#include "saplingscriptpubkeyman.h"
#include "validation.h" // for ReadBlockFromDisk()

void SaplingScriptPubKeyMan::AddToSaplingSpends(const uint256& nullifier, const uint256& wtxid)
Expand Down Expand Up @@ -540,6 +541,49 @@ void SaplingScriptPubKeyMan::GetFilteredNotes(
}
}
}
void SaplingScriptPubKeyMan::GetStakableNotes(std::vector<CShieldStake>& shieldInputs, int minDepth)
{
LOCK(wallet->cs_wallet);
for (auto& p : wallet->mapWallet) {
const CWalletTx& wtx = p.second;
// Filter coinbase/coinstakes transactions that don't have Sapling outputs
if ((wtx.IsCoinBase() || wtx.IsCoinStake()) && wtx.mapSaplingNoteData.empty()) {
continue;
}
// Filter the transactions before checking for notes
const int depth = wtx.GetDepthInMainChain();
if (!IsFinalTx(wtx.tx, wallet->GetLastBlockHeight() + 1, GetAdjustedTime()) ||
depth < minDepth) {
continue;
}
for (const auto& it : wtx.mapSaplingNoteData) {
const SaplingOutPoint& op = it.first;
const SaplingNoteData& nd = it.second;

// skip sent notes
if (!nd.IsMyNote()) continue;

// recover plaintext and address
auto optNotePtAndAddress = wtx.DecryptSaplingNote(op);
assert(static_cast<bool>(optNotePtAndAddress));

const libzcash::SaplingIncomingViewingKey& ivk = *(nd.ivk);
const libzcash::SaplingNotePlaintext& notePt = optNotePtAndAddress->first;
const libzcash::SaplingPaymentAddress& pa = optNotePtAndAddress->second;
auto note = notePt.note(ivk).get();

// skip notes which cannot be spent
if (!HaveSpendingKeyForPaymentAddress(pa)) {
continue;
}

if (nd.nullifier && IsSaplingSpent(*nd.nullifier)) {
continue;
}
shieldInputs.emplace_back(CShieldStake(note.value(), *nd.nullifier));
}
}
}

/* Return list of available notes grouped by sapling address. */
std::map<libzcash::SaplingPaymentAddress, std::vector<SaplingNoteEntry>> SaplingScriptPubKeyMan::ListNotes() const
Expand Down
3 changes: 3 additions & 0 deletions src/sapling/saplingscriptpubkeyman.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ class SaplingScriptPubKeyMan {
bool requireSpendingKey=true,
bool ignoreLocked=true) const;

/* Return a list of notes that are stakable */
void GetStakableNotes(std::vector<CShieldStake>& shieldInputs, int minDepth);

/* Return list of available notes grouped by sapling address. */
std::map<libzcash::SaplingPaymentAddress, std::vector<SaplingNoteEntry>> ListNotes() const;

Expand Down

0 comments on commit a73c70d

Please sign in to comment.