Skip to content

Commit b7485f1

Browse files
committed
descriptors: Check result of InferPubkey
InferPubkey can return a nullptr, so check it's result before continuing with creating the inferred descriptor.
1 parent db283a6 commit b7485f1

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

src/script/descriptor.cpp

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,13 +1491,17 @@ struct KeyParser {
14911491
if (miniscript::IsTapscript(m_script_ctx) && end - begin == 32) {
14921492
XOnlyPubKey pubkey;
14931493
std::copy(begin, end, pubkey.begin());
1494-
m_keys.push_back(InferPubkey(pubkey.GetEvenCorrespondingCPubKey(), ParseContext(), *m_in));
1495-
return key;
1494+
if (auto pubkey_provider = InferPubkey(pubkey.GetEvenCorrespondingCPubKey(), ParseContext(), *m_in)) {
1495+
m_keys.push_back(std::move(pubkey_provider));
1496+
return key;
1497+
}
14961498
} else if (!miniscript::IsTapscript(m_script_ctx)) {
1497-
CPubKey pubkey{begin, end};
1499+
CPubKey pubkey(begin, end);
14981500
if (pubkey.IsValidNonHybrid()) {
1499-
m_keys.push_back(InferPubkey(pubkey, ParseContext(), *m_in));
1500-
return key;
1501+
if (auto pubkey_provider = InferPubkey(pubkey, ParseContext(), *m_in)) {
1502+
m_keys.push_back(std::move(pubkey_provider));
1503+
return key;
1504+
}
15011505
}
15021506
}
15031507
return {};
@@ -1512,9 +1516,11 @@ struct KeyParser {
15121516
CKeyID keyid(hash);
15131517
CPubKey pubkey;
15141518
if (m_in->GetPubKey(keyid, pubkey)) {
1515-
Key key = m_keys.size();
1516-
m_keys.push_back(InferPubkey(pubkey, ParseContext(), *m_in));
1517-
return key;
1519+
if (auto pubkey_provider = InferPubkey(pubkey, ParseContext(), *m_in)) {
1520+
Key key = m_keys.size();
1521+
m_keys.push_back(std::move(pubkey_provider));
1522+
return key;
1523+
}
15181524
}
15191525
return {};
15201526
}
@@ -1850,32 +1856,44 @@ std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptCo
18501856
if (txntype == TxoutType::PUBKEY && (ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH || ctx == ParseScriptContext::P2WSH)) {
18511857
CPubKey pubkey(data[0]);
18521858
if (pubkey.IsValidNonHybrid()) {
1853-
return std::make_unique<PKDescriptor>(InferPubkey(pubkey, ctx, provider));
1859+
if (auto pubkey_provider = InferPubkey(pubkey, ctx, provider)) {
1860+
return std::make_unique<PKDescriptor>(std::move(pubkey_provider));
1861+
}
18541862
}
18551863
}
18561864
if (txntype == TxoutType::PUBKEYHASH && (ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH || ctx == ParseScriptContext::P2WSH)) {
18571865
uint160 hash(data[0]);
18581866
CKeyID keyid(hash);
18591867
CPubKey pubkey;
18601868
if (provider.GetPubKey(keyid, pubkey)) {
1861-
return std::make_unique<PKHDescriptor>(InferPubkey(pubkey, ctx, provider));
1869+
if (auto pubkey_provider = InferPubkey(pubkey, ctx, provider)) {
1870+
return std::make_unique<PKHDescriptor>(std::move(pubkey_provider));
1871+
}
18621872
}
18631873
}
18641874
if (txntype == TxoutType::WITNESS_V0_KEYHASH && (ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH)) {
18651875
uint160 hash(data[0]);
18661876
CKeyID keyid(hash);
18671877
CPubKey pubkey;
18681878
if (provider.GetPubKey(keyid, pubkey)) {
1869-
return std::make_unique<WPKHDescriptor>(InferPubkey(pubkey, ctx, provider));
1879+
if (auto pubkey_provider = InferPubkey(pubkey, ctx, provider)) {
1880+
return std::make_unique<WPKHDescriptor>(std::move(pubkey_provider));
1881+
}
18701882
}
18711883
}
18721884
if (txntype == TxoutType::MULTISIG && (ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH || ctx == ParseScriptContext::P2WSH)) {
1885+
bool ok = true;
18731886
std::vector<std::unique_ptr<PubkeyProvider>> providers;
18741887
for (size_t i = 1; i + 1 < data.size(); ++i) {
18751888
CPubKey pubkey(data[i]);
1876-
providers.push_back(InferPubkey(pubkey, ctx, provider));
1889+
if (auto pubkey_provider = InferPubkey(pubkey, ctx, provider)) {
1890+
providers.push_back(std::move(pubkey_provider));
1891+
} else {
1892+
ok = false;
1893+
break;
1894+
}
18771895
}
1878-
return std::make_unique<MultisigDescriptor>((int)data[0][0], std::move(providers));
1896+
if (ok) return std::make_unique<MultisigDescriptor>((int)data[0][0], std::move(providers));
18791897
}
18801898
if (txntype == TxoutType::SCRIPTHASH && ctx == ParseScriptContext::TOP) {
18811899
uint160 hash(data[0]);

0 commit comments

Comments
 (0)