Skip to content

Commit 37b9b73

Browse files
committed
descriptors: Move InferScript's pubkey validity checks to InferPubkey
1 parent b7485f1 commit 37b9b73

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/script/descriptor.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,8 +1413,16 @@ std::unique_ptr<PubkeyProvider> ParsePubkey(uint32_t key_exp_index, const Span<c
14131413
return std::make_unique<OriginPubkeyProvider>(key_exp_index, std::move(info), std::move(provider), apostrophe);
14141414
}
14151415

1416-
std::unique_ptr<PubkeyProvider> InferPubkey(const CPubKey& pubkey, ParseScriptContext, const SigningProvider& provider)
1416+
std::unique_ptr<PubkeyProvider> InferPubkey(const CPubKey& pubkey, ParseScriptContext ctx, const SigningProvider& provider)
14171417
{
1418+
// Key cannot be hybrid
1419+
if (!pubkey.IsValidNonHybrid()) {
1420+
return nullptr;
1421+
}
1422+
// Uncompressed is only allowed in TOP and P2SH contexts
1423+
if (ctx != ParseScriptContext::TOP && ctx != ParseScriptContext::P2SH && !pubkey.IsCompressed()) {
1424+
return nullptr;
1425+
}
14181426
std::unique_ptr<PubkeyProvider> key_provider = std::make_unique<ConstPubkeyProvider>(0, pubkey, false);
14191427
KeyOriginInfo info;
14201428
if (provider.GetKeyOrigin(pubkey.GetID(), info)) {
@@ -1497,11 +1505,9 @@ struct KeyParser {
14971505
}
14981506
} else if (!miniscript::IsTapscript(m_script_ctx)) {
14991507
CPubKey pubkey(begin, end);
1500-
if (pubkey.IsValidNonHybrid()) {
1501-
if (auto pubkey_provider = InferPubkey(pubkey, ParseContext(), *m_in)) {
1502-
m_keys.push_back(std::move(pubkey_provider));
1503-
return key;
1504-
}
1508+
if (auto pubkey_provider = InferPubkey(pubkey, ParseContext(), *m_in)) {
1509+
m_keys.push_back(std::move(pubkey_provider));
1510+
return key;
15051511
}
15061512
}
15071513
return {};
@@ -1855,10 +1861,8 @@ std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptCo
18551861

18561862
if (txntype == TxoutType::PUBKEY && (ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH || ctx == ParseScriptContext::P2WSH)) {
18571863
CPubKey pubkey(data[0]);
1858-
if (pubkey.IsValidNonHybrid()) {
1859-
if (auto pubkey_provider = InferPubkey(pubkey, ctx, provider)) {
1860-
return std::make_unique<PKDescriptor>(std::move(pubkey_provider));
1861-
}
1864+
if (auto pubkey_provider = InferPubkey(pubkey, ctx, provider)) {
1865+
return std::make_unique<PKDescriptor>(std::move(pubkey_provider));
18621866
}
18631867
}
18641868
if (txntype == TxoutType::PUBKEYHASH && (ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH || ctx == ParseScriptContext::P2WSH)) {
@@ -1876,7 +1880,7 @@ std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptCo
18761880
CKeyID keyid(hash);
18771881
CPubKey pubkey;
18781882
if (provider.GetPubKey(keyid, pubkey)) {
1879-
if (auto pubkey_provider = InferPubkey(pubkey, ctx, provider)) {
1883+
if (auto pubkey_provider = InferPubkey(pubkey, ParseScriptContext::P2WPKH, provider)) {
18801884
return std::make_unique<WPKHDescriptor>(std::move(pubkey_provider));
18811885
}
18821886
}

0 commit comments

Comments
 (0)