Skip to content

Commit f6a6d91

Browse files
theStackachow101
authored andcommitted
test: add check for getting SigningProvider for a CPubKey
Verify that the DescriptorSPKM method `GetSigningProvider` should only return a signing provider for the passed public key if its corresponding private key of the passed public key is available.
1 parent 62a95f5 commit f6a6d91

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/wallet/scriptpubkeyman.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,6 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
611611
mutable std::map<int32_t, FlatSigningProvider> m_map_signing_providers;
612612
// Fetch the SigningProvider for the given script and optionally include private keys
613613
std::unique_ptr<FlatSigningProvider> GetSigningProvider(const CScript& script, bool include_private = false) const;
614-
// Fetch the SigningProvider for the given pubkey and always include private keys. This should only be called by signing code.
615-
std::unique_ptr<FlatSigningProvider> GetSigningProvider(const CPubKey& pubkey) const;
616614
// Fetch the SigningProvider for a given index and optionally include private keys. Called by the above functions.
617615
std::unique_ptr<FlatSigningProvider> GetSigningProvider(int32_t index, bool include_private = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_desc_man);
618616

@@ -675,6 +673,9 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
675673

676674
bool CanProvide(const CScript& script, SignatureData& sigdata) override;
677675

676+
// Fetch the SigningProvider for the given pubkey and always include private keys. This should only be called by signing code.
677+
std::unique_ptr<FlatSigningProvider> GetSigningProvider(const CPubKey& pubkey) const;
678+
678679
bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors) const override;
679680
SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const override;
680681
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;

src/wallet/test/scriptpubkeyman_tests.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <key.h>
6+
#include <key_io.h>
67
#include <test/util/setup_common.h>
78
#include <script/solver.h>
89
#include <wallet/scriptpubkeyman.h>
@@ -40,5 +41,27 @@ BOOST_AUTO_TEST_CASE(CanProvide)
4041
BOOST_CHECK(keyman.CanProvide(p2sh_script, data));
4142
}
4243

44+
BOOST_AUTO_TEST_CASE(DescriptorScriptPubKeyManTests)
45+
{
46+
std::unique_ptr<interfaces::Chain>& chain = m_node.chain;
47+
48+
CWallet keystore(chain.get(), "", CreateMockableWalletDatabase());
49+
auto key_scriptpath = GenerateRandomKey();
50+
51+
// Verify that a SigningProvider for a pubkey is only returned if its corresponding private key is available
52+
auto key_internal = GenerateRandomKey();
53+
std::string desc_str = "tr(" + EncodeSecret(key_internal) + ",pk(" + HexStr(key_scriptpath.GetPubKey()) + "))";
54+
auto spk_man1 = dynamic_cast<DescriptorScriptPubKeyMan*>(CreateDescriptor(keystore, desc_str, true));
55+
BOOST_CHECK(spk_man1 != nullptr);
56+
auto signprov_keypath_spendable = spk_man1->GetSigningProvider(key_internal.GetPubKey());
57+
BOOST_CHECK(signprov_keypath_spendable != nullptr);
58+
59+
desc_str = "tr(" + HexStr(XOnlyPubKey::NUMS_H) + ",pk(" + HexStr(key_scriptpath.GetPubKey()) + "))";
60+
auto spk_man2 = dynamic_cast<DescriptorScriptPubKeyMan*>(CreateDescriptor(keystore, desc_str, true));
61+
BOOST_CHECK(spk_man2 != nullptr);
62+
auto signprov_keypath_nums_h = spk_man2->GetSigningProvider(XOnlyPubKey::NUMS_H.GetEvenCorrespondingCPubKey());
63+
BOOST_CHECK(signprov_keypath_nums_h == nullptr);
64+
}
65+
4366
BOOST_AUTO_TEST_SUITE_END()
4467
} // namespace wallet

0 commit comments

Comments
 (0)