From 005f8a92ccb5bc10c8daa106d75e1c21390461d3 Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Tue, 4 Feb 2020 17:24:48 -0500 Subject: [PATCH] wallet: Improve LegacyScriptPubKeyMan::CanProvide script recognition Make LegacyScriptPubKeyMan::CanProvide method able to recognize p2sh scripts when the redeem script is present in the mapScripts map without the p2sh script also having to be added to the mapScripts map. This restores behavior prior to https://github.com/bitcoin/bitcoin/pull/17261, which I think broke backwards compatibility with old wallet files by no longer treating addresses created by `addmultisigaddress` calls before #17261 as solvable. The reason why tests didn't fail with the CanProvide implementation in #17261 is because of a workaround added in 4a7e43e8460127a40a7895519587399feff3b682 "Store p2sh scripts in AddAndGetDestinationForScript", which masked the problem for new `addmultisigaddress` RPC calls without fixing it for multisig addresses already created in old wallet files. This change adds a lot of comments and allows reverting commit 4a7e43e8460127a40a7895519587399feff3b682 "Store p2sh scripts in AddAndGetDestinationForScript", so the AddAndGetDestinationForScript() function, CanProvide() method, and mapScripts map should all be more comprehensible --- src/Makefile.test.include | 3 +- src/script/signingprovider.h | 46 +++++++++++++++++++++++ src/wallet/scriptpubkeyman.cpp | 24 ++++++++---- src/wallet/test/scriptpubkeyman_tests.cpp | 43 +++++++++++++++++++++ 4 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 src/wallet/test/scriptpubkeyman_tests.cpp diff --git a/src/Makefile.test.include b/src/Makefile.test.include index c76f30de8e..e7d7d76d3c 100644 --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -209,7 +209,8 @@ BITCOIN_TESTS += \ wallet/test/wallet_crypto_tests.cpp \ wallet/test/coinselector_tests.cpp \ wallet/test/init_tests.cpp \ - wallet/test/ismine_tests.cpp + wallet/test/ismine_tests.cpp \ + wallet/test/scriptpubkeyman_tests.cpp BITCOIN_TEST_SUITE += \ wallet/test/wallet_test_fixture.cpp \ diff --git a/src/script/signingprovider.h b/src/script/signingprovider.h index 6ad20480a7..76f31d2f6f 100644 --- a/src/script/signingprovider.h +++ b/src/script/signingprovider.h @@ -66,7 +66,53 @@ protected: using KeyMap = std::map; using ScriptMap = std::map; + /** + * Map of key id to unencrypted private keys known by the signing provider. + * Map may be empty if the provider has another source of keys, like an + * encrypted store. + */ KeyMap mapKeys GUARDED_BY(cs_KeyStore); + + /** + * Map of script id to scripts known by the signing provider. + * + * This map originally just held P2SH redeemScripts, and was used by wallet + * code to look up script ids referenced in "OP_HASH160