Skip to content

Commit

Permalink
Merge bitcoin#31242: wallet, desc spkm: Return SigningProvider only i…
Browse files Browse the repository at this point in the history
…f we have the privkey

f6a6d91 test: add check for getting SigningProvider for a CPubKey (Sebastian Falbesoner)
62a95f5 test: refactor: move `CreateDescriptor` helper to wallet test util module (Sebastian Falbesoner)
4936567 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 bitcoin#29675

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

Tree-SHA512: 30a196e611a0c5d9ebe5baf6d896caaa6af66f1615463dbb0c31e52604d53cf342922bb9967b3c697b47083d76b0485c77a5f545bd6381247c8bc44321c70f97
  • Loading branch information
fanquake committed Jan 16, 2025
2 parents 9dc4eed + f6a6d91 commit f9032a4
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 24 deletions.
5 changes: 5 additions & 0 deletions src/script/signingprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions src/script/signingprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/wallet/scriptpubkeyman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/wallet/scriptpubkeyman.h
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,6 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
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);

Expand Down Expand Up @@ -678,6 +676,9 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan

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;
Expand Down
20 changes: 0 additions & 20 deletions src/wallet/test/ismine_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
23 changes: 23 additions & 0 deletions src/wallet/test/scriptpubkeyman_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
20 changes: 20 additions & 0 deletions src/wallet/test/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion src/wallet/test/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <addresstype.h>
#include <wallet/db.h>
#include <wallet/scriptpubkeyman.h>

#include <memory>

Expand Down Expand Up @@ -127,8 +128,9 @@ class MockableDatabase : public WalletDatabase
};

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

0 comments on commit f9032a4

Please sign in to comment.