Skip to content

Commit 97a965d

Browse files
committed
refactor: extract descriptor ID calculation from spkm GetID()
This allows us to verify the descriptor ID on the descriptors unit tests in different software versions without requiring to use the entire DescriptorScriptPubKeyMan machinery. Note: The unit test changes are introduced after the bugfix commit but this commit + the unit test commit can be cherry-picked on top of the v25 branch to verify IDs correctness. IDs must be the same for v25 and after the bugfix commit.
1 parent 1d207e3 commit 97a965d

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/script/descriptor.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,14 @@ std::unique_ptr<Descriptor> InferDescriptor(const CScript& script, const Signing
17781778
return InferScript(script, ParseScriptContext::TOP, provider);
17791779
}
17801780

1781+
uint256 DescriptorID(const Descriptor& desc)
1782+
{
1783+
std::string desc_str = desc.ToString();
1784+
uint256 id;
1785+
CSHA256().Write((unsigned char*)desc_str.data(), desc_str.size()).Finalize(id.begin());
1786+
return id;
1787+
}
1788+
17811789
void DescriptorCache::CacheParentExtPubKey(uint32_t key_exp_pos, const CExtPubKey& xpub)
17821790
{
17831791
m_parent_xpubs[key_exp_pos] = xpub;

src/script/descriptor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,9 @@ std::string GetDescriptorChecksum(const std::string& descriptor);
182182
*/
183183
std::unique_ptr<Descriptor> InferDescriptor(const CScript& script, const SigningProvider& provider);
184184

185+
/** Unique identifier that may not change over time, unless explicitly marked as not backwards compatible.
186+
* This is not part of BIP 380, not guaranteed to be interoperable and should not be exposed to the user.
187+
*/
188+
uint256 DescriptorID(const Descriptor& desc);
189+
185190
#endif // BITCOIN_SCRIPT_DESCRIPTOR_H

src/wallet/scriptpubkeyman.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2584,10 +2584,7 @@ std::unique_ptr<CKeyMetadata> DescriptorScriptPubKeyMan::GetMetadata(const CTxDe
25842584
uint256 DescriptorScriptPubKeyMan::GetID() const
25852585
{
25862586
LOCK(cs_desc_man);
2587-
std::string desc_str = m_wallet_descriptor.descriptor->ToString();
2588-
uint256 id;
2589-
CSHA256().Write((unsigned char*)desc_str.data(), desc_str.size()).Finalize(id.begin());
2590-
return id;
2587+
return DescriptorID(*m_wallet_descriptor.descriptor);
25912588
}
25922589

25932590
void DescriptorScriptPubKeyMan::SetCache(const DescriptorCache& cache)

0 commit comments

Comments
 (0)