diff --git a/src/gtest/test_noteencryption.cpp b/src/gtest/test_noteencryption.cpp index 594fd9d8b..f521cdb01 100644 --- a/src/gtest/test_noteencryption.cpp +++ b/src/gtest/test_noteencryption.cpp @@ -46,21 +46,25 @@ TEST(noteencryption, api) ASSERT_TRUE(plaintext == message); // Test wrong nonce - ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256(), (i == 0) ? 1 : (i - 1)), std::runtime_error); + ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256(), (i == 0) ? 1 : (i - 1)), + libzcash::note_decryption_failed); // Test wrong ephemeral key { ZCNoteEncryption c = ZCNoteEncryption(uint256()); - ASSERT_THROW(decrypter.decrypt(ciphertext, c.get_epk(), uint256(), i), std::runtime_error); + ASSERT_THROW(decrypter.decrypt(ciphertext, c.get_epk(), uint256(), i), + libzcash::note_decryption_failed); } // Test wrong seed - ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256S("11035d60bc1983e37950ce4803418a8fb33ea68d5b937ca382ecbae7564d6a77"), i), std::runtime_error); + ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256S("11035d60bc1983e37950ce4803418a8fb33ea68d5b937ca382ecbae7564d6a77"), i), + libzcash::note_decryption_failed); // Test corrupted ciphertext ciphertext[10] ^= 0xff; - ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256(), i), std::runtime_error); + ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256(), i), + libzcash::note_decryption_failed); ciphertext[10] ^= 0xff; } @@ -69,7 +73,8 @@ TEST(noteencryption, api) uint256 sk_enc_2 = ZCNoteEncryption::generate_privkey(uint252()); ZCNoteDecryption decrypter(sk_enc_2); - ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256(), i), std::runtime_error); + ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256(), i), + libzcash::note_decryption_failed); } { @@ -81,7 +86,8 @@ TEST(noteencryption, api) // Test wrong public key (test of KDF) decrypter.change_pk_enc(uint256()); - ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256(), i), std::runtime_error); + ASSERT_THROW(decrypter.decrypt(ciphertext, b.get_epk(), uint256(), i), + libzcash::note_decryption_failed); } } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 8c32dcb1a..2ee874e30 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1275,12 +1275,7 @@ mapNoteData_t CWallet::FindMyNotes(const CTransaction& tx) const noteData.insert(std::make_pair(jsoutpt, nd)); } break; - } catch (const std::runtime_error &err) { - if (memcmp("Could not decrypt message", err.what(), 25) != 0) { - // Unexpected failure - LogPrintf("FindMyNotes(): Unexpected runtime error while testing decrypt:\n"); - LogPrintf("%s\n", err.what()); - } // else + } catch (const note_decryption_failed &err) { // Couldn't decrypt with this decryptor } catch (const std::exception &exc) { // Unexpected failure @@ -3562,9 +3557,12 @@ void CWallet::GetFilteredNotes(std::vector & outEntries, st outEntries.push_back(CNotePlaintextEntry{jsop, plaintext}); - } catch (const std::exception &) { + } catch (const note_decryption_failed &err) { // Couldn't decrypt with this spending key throw std::runtime_error(strprintf("Could not decrypt note for payment address %s", CZCPaymentAddress(pa).ToString())); + } catch (const std::exception &exc) { + // Unexpected failure + throw std::runtime_error(strprintf("Error while decrypting note for payment address %s: %s", CZCPaymentAddress(pa).ToString(), exc.what())); } } } diff --git a/src/zcash/NoteEncryption.cpp b/src/zcash/NoteEncryption.cpp index 101023fad..a5ea2da15 100644 --- a/src/zcash/NoteEncryption.cpp +++ b/src/zcash/NoteEncryption.cpp @@ -129,7 +129,7 @@ typename NoteDecryption::Plaintext NoteDecryption::decrypt NULL, 0, cipher_nonce, K) != 0) { - throw std::runtime_error("Could not decrypt message"); + throw note_decryption_failed(); } return plaintext; diff --git a/src/zcash/NoteEncryption.hpp b/src/zcash/NoteEncryption.hpp index e1f3718b0..11346ebc1 100644 --- a/src/zcash/NoteEncryption.hpp +++ b/src/zcash/NoteEncryption.hpp @@ -82,6 +82,11 @@ public: uint256 random_uint256(); uint252 random_uint252(); +class note_decryption_failed : public std::runtime_error { +public: + note_decryption_failed() : std::runtime_error("Could not decrypt message") { } +}; + } typedef libzcash::NoteEncryption ZCNoteEncryption;