Skip to content

Commit 7d75415

Browse files
random-zebrafurszy
authored andcommitted
[Refactoring] Use CWalletTx::DecryptSaplingNote in Get[Filtered]Notes
Remove some code duplication
1 parent 310db82 commit 7d75415

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

src/sapling/saplingscriptpubkeyman.cpp

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -399,28 +399,21 @@ void SaplingScriptPubKeyMan::GetNotes(const std::vector<SaplingOutPoint>& saplin
399399
if (!wtx) throw std::runtime_error("No transaction available for hash " + outpoint.hash.GetHex());
400400
const auto& it = wtx->mapSaplingNoteData.find(outpoint);
401401
if (it != wtx->mapSaplingNoteData.end()) {
402-
403402
const SaplingOutPoint& op = it->first;
404403
const SaplingNoteData& nd = it->second;
405404

406405
// skip sent notes
407406
if (!nd.IsMyNote()) continue;
408-
const libzcash::SaplingIncomingViewingKey& ivk = *(nd.ivk);
409-
410-
const OutputDescription& outDesc = wtx->tx->sapData->vShieldedOutput[op.n];
411-
auto maybe_pt = libzcash::SaplingNotePlaintext::decrypt(
412-
outDesc.encCiphertext,
413-
ivk,
414-
outDesc.ephemeralKey,
415-
outDesc.cmu);
416-
assert(static_cast<bool>(maybe_pt));
417-
auto notePt = maybe_pt.get();
418407

419-
auto maybe_pa = ivk.address(notePt.d);
420-
assert(static_cast<bool>(maybe_pa));
421-
auto pa = maybe_pa.get();
408+
// recover plaintext and address
409+
auto optNotePtAndAddress = wtx->DecryptSaplingNote(op);
410+
assert(static_cast<bool>(optNotePtAndAddress));
422411

412+
const libzcash::SaplingIncomingViewingKey& ivk = *(nd.ivk);
413+
const libzcash::SaplingNotePlaintext& notePt = optNotePtAndAddress->first;
414+
const libzcash::SaplingPaymentAddress& pa = optNotePtAndAddress->second;
423415
auto note = notePt.note(ivk).get();
416+
424417
saplingEntriesRet.emplace_back(op, pa, note, notePt.memo(), wtx->GetDepthInMainChain());
425418
}
426419
}
@@ -481,21 +474,17 @@ void SaplingScriptPubKeyMan::GetFilteredNotes(
481474
const SaplingOutPoint& op = it.first;
482475
const SaplingNoteData& nd = it.second;
483476

484-
// Skip sent notes
477+
// skip sent notes
485478
if (!nd.IsMyNote()) continue;
486-
const libzcash::SaplingIncomingViewingKey& ivk = *(nd.ivk);
487479

488-
auto maybe_pt = libzcash::SaplingNotePlaintext::decrypt(
489-
wtx.tx->sapData->vShieldedOutput[op.n].encCiphertext,
490-
ivk,
491-
wtx.tx->sapData->vShieldedOutput[op.n].ephemeralKey,
492-
wtx.tx->sapData->vShieldedOutput[op.n].cmu);
493-
assert(static_cast<bool>(maybe_pt));
494-
auto notePt = maybe_pt.get();
480+
// recover plaintext and address
481+
auto optNotePtAndAddress = wtx.DecryptSaplingNote(op);
482+
assert(static_cast<bool>(optNotePtAndAddress));
495483

496-
auto maybe_pa = ivk.address(notePt.d);
497-
assert(static_cast<bool>(maybe_pa));
498-
auto pa = maybe_pa.get();
484+
const libzcash::SaplingIncomingViewingKey& ivk = *(nd.ivk);
485+
const libzcash::SaplingNotePlaintext& notePt = optNotePtAndAddress->first;
486+
const libzcash::SaplingPaymentAddress& pa = optNotePtAndAddress->second;
487+
auto note = notePt.note(ivk).get();
499488

500489
// skip notes which belong to a different payment address in the wallet
501490
if (!(filterAddresses.empty() || filterAddresses.count(pa))) {
@@ -516,7 +505,6 @@ void SaplingScriptPubKeyMan::GetFilteredNotes(
516505
// continue;
517506
//}
518507

519-
auto note = notePt.note(ivk).get();
520508
saplingEntries.emplace_back(op, pa, note, notePt.memo(), wtx.GetDepthInMainChain());
521509
}
522510
}

0 commit comments

Comments
 (0)