diff --git a/src/wallet/gtest/test_wallet.cpp b/src/wallet/gtest/test_wallet.cpp index d06406e27..cf418a596 100644 --- a/src/wallet/gtest/test_wallet.cpp +++ b/src/wallet/gtest/test_wallet.cpp @@ -291,6 +291,34 @@ TEST(wallet_tests, navigate_from_nullifier_to_note) { EXPECT_EQ(1, wallet.mapNullifiersToNotes[nullifier].n); } +TEST(wallet_tests, spent_note_is_from_me) { + CWallet wallet; + + auto sk = libzcash::SpendingKey::random(); + wallet.AddSpendingKey(sk); + + auto wtx = GetValidReceive(sk, 10, true); + auto note = GetNote(sk, wtx, 0, 1); + auto nullifier = note.nullifier(sk); + auto wtx2 = GetValidSpend(sk, note, 5); + + EXPECT_FALSE(wallet.IsFromMe(wtx)); + EXPECT_FALSE(wallet.IsFromMe(wtx2)); + + mapNoteData_t noteData; + JSOutPoint jsoutpt {wtx.GetTxid(), 0, 1}; + CNoteData nd {sk.address(), nullifier}; + noteData[jsoutpt] = nd; + + wtx.SetNoteData(noteData); + EXPECT_FALSE(wallet.IsFromMe(wtx)); + EXPECT_FALSE(wallet.IsFromMe(wtx2)); + + wallet.AddToWallet(wtx, true, NULL); + EXPECT_FALSE(wallet.IsFromMe(wtx)); + EXPECT_TRUE(wallet.IsFromMe(wtx2)); +} + TEST(wallet_tests, cached_witnesses_empty_chain) { TestWallet wallet; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index f0ac15df3..fe2771e66 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1060,6 +1060,18 @@ mapNoteData_t CWallet::FindMyNotes(const CTransaction& tx) const return noteData; } +bool CWallet::IsFromMe(const uint256& nullifier) const +{ + { + LOCK(cs_wallet); + if (mapNullifiersToNotes.count(nullifier) && + mapWallet.count(mapNullifiersToNotes.at(nullifier).hash)) { + return true; + } + } + return false; +} + void CWallet::GetNoteWitnesses(std::vector notes, std::vector>& witnesses, uint256 &final_anchor) @@ -1171,7 +1183,17 @@ bool CWallet::IsMine(const CTransaction& tx) const bool CWallet::IsFromMe(const CTransaction& tx) const { - return (GetDebit(tx, ISMINE_ALL) > 0); + if (GetDebit(tx, ISMINE_ALL) > 0) { + return true; + } + for (const JSDescription& jsdesc : tx.vjoinsplit) { + for (const uint256& nullifier : jsdesc.nullifiers) { + if (IsFromMe(nullifier)) { + return true; + } + } + } + return false; } CAmount CWallet::GetDebit(const CTransaction& tx, const isminefilter& filter) const diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 231c4aa49..7fa88ab1d 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -795,6 +795,7 @@ public: std::set GetAccountAddresses(std::string strAccount) const; mapNoteData_t FindMyNotes(const CTransaction& tx) const; + bool IsFromMe(const uint256& nullifier) const; void GetNoteWitnesses( std::vector notes, std::vector>& witnesses,