Merge bitcoin/bitcoin#31242: wallet, desc spkm: Return SigningProvider only if we have the privkey
Some checks are pending
CI / macOS 14 native, arm64, fuzz (push) Waiting to run
CI / Win64 native, VS 2022 (push) Waiting to run
CI / Win64 native fuzz, VS 2022 (push) Waiting to run
CI / ASan + LSan + UBSan + integer, no depends, USDT (push) Waiting to run
CI / test each commit (push) Waiting to run
CI / macOS 14 native, arm64, no depends, sqlite only, gui (push) Waiting to run

f6a6d91205 test: add check for getting SigningProvider for a CPubKey (Sebastian Falbesoner)
62a95f5af9 test: refactor: move `CreateDescriptor` helper to wallet test util module (Sebastian Falbesoner)
493656763f desc spkm: Return SigningProvider only if we have the privkey (Ava Chow)

Pull request description:

  If we know about a pubkey that's in our descriptor, but we don't have the private key, don't return a SigningProvider for that pubkey.

  This is specifically an issue for Taproot outputs that use the H point as the resulting PSBTs may end up containing irrelevant information because the H point was detected as a pubkey each unrelated descriptor knew about.

  Split from #29675

ACKs for top commit:
  fjahr:
    ACK f6a6d91205
  theStack:
    re-ACK f6a6d91205
  furszy:
    utACK f6a6d91205. Only reviewed the actual change in detail, not the test commit.

Tree-SHA512: 30a196e611a0c5d9ebe5baf6d896caaa6af66f1615463dbb0c31e52604d53cf342922bb9967b3c697b47083d76b0485c77a5f545bd6381247c8bc44321c70f97
This commit is contained in:
merge-script 2025-01-16 17:30:36 +00:00
commit f9032a4abb
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1
8 changed files with 60 additions and 24 deletions

View file

@ -62,6 +62,11 @@ bool FlatSigningProvider::GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info)
if (ret) info = std::move(out.second);
return ret;
}
bool FlatSigningProvider::HaveKey(const CKeyID &keyid) const
{
CKey key;
return LookupHelper(keys, keyid, key);
}
bool FlatSigningProvider::GetKey(const CKeyID& keyid, CKey& key) const { return LookupHelper(keys, keyid, key); }
bool FlatSigningProvider::GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const
{

View file

@ -215,6 +215,7 @@ struct FlatSigningProvider final : public SigningProvider
bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
bool HaveKey(const CKeyID &keyid) const override;
bool GetKey(const CKeyID& keyid, CKey& key) const override;
bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const override;
bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const override;

View file

@ -2468,7 +2468,11 @@ std::unique_ptr<FlatSigningProvider> DescriptorScriptPubKeyMan::GetSigningProvid
int32_t index = it->second;
// Always try to get the signing provider with private keys. This function should only be called during signing anyways
return GetSigningProvider(index, true);
std::unique_ptr<FlatSigningProvider> out = GetSigningProvider(index, true);
if (!out->HaveKey(pubkey.GetID())) {
return nullptr;
}
return out;
}
std::unique_ptr<FlatSigningProvider> DescriptorScriptPubKeyMan::GetSigningProvider(int32_t index, bool include_private) const

View file

@ -613,8 +613,6 @@ private:
mutable std::map<int32_t, FlatSigningProvider> m_map_signing_providers;
// Fetch the SigningProvider for the given script and optionally include private keys
std::unique_ptr<FlatSigningProvider> GetSigningProvider(const CScript& script, bool include_private = false) const;
// Fetch the SigningProvider for the given pubkey and always include private keys. This should only be called by signing code.
std::unique_ptr<FlatSigningProvider> GetSigningProvider(const CPubKey& pubkey) const;
// Fetch the SigningProvider for a given index and optionally include private keys. Called by the above functions.
std::unique_ptr<FlatSigningProvider> GetSigningProvider(int32_t index, bool include_private = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
@ -678,6 +676,9 @@ public:
bool CanProvide(const CScript& script, SignatureData& sigdata) override;
// Fetch the SigningProvider for the given pubkey and always include private keys. This should only be called by signing code.
std::unique_ptr<FlatSigningProvider> GetSigningProvider(const CPubKey& pubkey) const;
bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors) const override;
SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const override;
std::optional<common::PSBTError> FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, int sighash_type = SIGHASH_DEFAULT, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr, bool finalize = true) const override;

View file

@ -20,26 +20,6 @@ using namespace util::hex_literals;
namespace wallet {
BOOST_FIXTURE_TEST_SUITE(ismine_tests, BasicTestingSetup)
wallet::ScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, const bool success)
{
keystore.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
FlatSigningProvider keys;
std::string error;
auto parsed_descs = Parse(desc_str, keys, error, false);
BOOST_CHECK(success == (!parsed_descs.empty()));
if (!success) return nullptr;
auto& desc = parsed_descs.at(0);
const int64_t range_start = 0, range_end = 1, next_index = 0, timestamp = 1;
WalletDescriptor w_desc(std::move(desc), timestamp, range_start, range_end, next_index);
LOCK(keystore.cs_wallet);
return Assert(keystore.AddWalletDescriptor(w_desc, keys,/*label=*/"", /*internal=*/false));
};
BOOST_AUTO_TEST_CASE(ismine_standard)
{
CKey keys[2];

View file

@ -3,6 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <key.h>
#include <key_io.h>
#include <test/util/setup_common.h>
#include <script/solver.h>
#include <wallet/scriptpubkeyman.h>
@ -40,5 +41,27 @@ BOOST_AUTO_TEST_CASE(CanProvide)
BOOST_CHECK(keyman.CanProvide(p2sh_script, data));
}
BOOST_AUTO_TEST_CASE(DescriptorScriptPubKeyManTests)
{
std::unique_ptr<interfaces::Chain>& chain = m_node.chain;
CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
auto key_scriptpath = GenerateRandomKey();
// Verify that a SigningProvider for a pubkey is only returned if its corresponding private key is available
auto key_internal = GenerateRandomKey();
std::string desc_str = "tr(" + EncodeSecret(key_internal) + ",pk(" + HexStr(key_scriptpath.GetPubKey()) + "))";
auto spk_man1 = dynamic_cast<DescriptorScriptPubKeyMan*>(CreateDescriptor(keystore, desc_str, true));
BOOST_CHECK(spk_man1 != nullptr);
auto signprov_keypath_spendable = spk_man1->GetSigningProvider(key_internal.GetPubKey());
BOOST_CHECK(signprov_keypath_spendable != nullptr);
desc_str = "tr(" + HexStr(XOnlyPubKey::NUMS_H) + ",pk(" + HexStr(key_scriptpath.GetPubKey()) + "))";
auto spk_man2 = dynamic_cast<DescriptorScriptPubKeyMan*>(CreateDescriptor(keystore, desc_str, true));
BOOST_CHECK(spk_man2 != nullptr);
auto signprov_keypath_nums_h = spk_man2->GetSigningProvider(XOnlyPubKey::NUMS_H.GetEvenCorrespondingCPubKey());
BOOST_CHECK(signprov_keypath_nums_h == nullptr);
}
BOOST_AUTO_TEST_SUITE_END()
} // namespace wallet

View file

@ -192,4 +192,24 @@ MockableDatabase& GetMockableDatabase(CWallet& wallet)
{
return dynamic_cast<MockableDatabase&>(wallet.GetDatabase());
}
wallet::ScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, const bool success)
{
keystore.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
FlatSigningProvider keys;
std::string error;
auto parsed_descs = Parse(desc_str, keys, error, false);
Assert(success == (!parsed_descs.empty()));
if (!success) return nullptr;
auto& desc = parsed_descs.at(0);
const int64_t range_start = 0, range_end = 1, next_index = 0, timestamp = 1;
WalletDescriptor w_desc(std::move(desc), timestamp, range_start, range_end, next_index);
LOCK(keystore.cs_wallet);
return Assert(keystore.AddWalletDescriptor(w_desc, keys,/*label=*/"", /*internal=*/false));
};
} // namespace wallet

View file

@ -9,6 +9,7 @@
#include <addresstype.h>
#include <wallet/db.h>
#include <wallet/scriptpubkeyman.h>
#include <memory>
@ -127,8 +128,9 @@ public:
};
std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records = {});
MockableDatabase& GetMockableDatabase(CWallet& wallet);
ScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, const bool success);
} // namespace wallet
#endif // BITCOIN_WALLET_TEST_UTIL_H