Skip to content

Commit bfdd073

Browse files
committed
Implement GetNewDestination for DescriptorScriptPubKeyMan
1 parent 58c7651 commit bfdd073

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1502,7 +1502,44 @@ void LegacyScriptPubKeyMan::SetType(OutputType type, bool internal) {}
15021502

15031503
bool DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type, CTxDestination& dest, std::string& error)
15041504
{
1505-
return false;
1505+
// Returns true if this descriptor supports getting new addresses. Conditions where we may be unable to fetch them (e.g. locked) are caught later
1506+
if (!CanGetAddresses(m_internal)) {
1507+
error = "No addresses available";
1508+
return false;
1509+
}
1510+
{
1511+
LOCK(cs_desc_man);
1512+
assert(m_wallet_descriptor.descriptor->IsSingleType()); // This is a combo descriptor which should not be an active descriptor
1513+
if (type != m_address_type) {
1514+
throw std::runtime_error(std::string(__func__) + ": Types are inconsistent");
1515+
}
1516+
1517+
TopUp();
1518+
1519+
// Get the scriptPubKey from the descriptor
1520+
FlatSigningProvider out_keys;
1521+
std::vector<CScript> scripts_temp;
1522+
if (m_wallet_descriptor.range_end <= m_max_cached_index && !TopUp(1)) {
1523+
// We can't generate anymore keys
1524+
error = "Error: Keypool ran out, please call keypoolrefill first";
1525+
return false;
1526+
}
1527+
if (!m_wallet_descriptor.descriptor->ExpandFromCache(m_wallet_descriptor.next_index, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
1528+
// We can't generate anymore keys
1529+
error = "Error: Keypool ran out, please call keypoolrefill first";
1530+
return false;
1531+
}
1532+
1533+
Optional<OutputType> out_script_type = m_wallet_descriptor.descriptor->GetOutputType();
1534+
if (out_script_type && out_script_type == type) {
1535+
ExtractDestination(scripts_temp[0], dest);
1536+
} else {
1537+
throw std::runtime_error(std::string(__func__) + ": Types are inconsistent. Stored type does not match type of newly generated address");
1538+
}
1539+
m_wallet_descriptor.next_index++;
1540+
WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor);
1541+
return true;
1542+
}
15061543
}
15071544

15081545
isminetype DescriptorScriptPubKeyMan::IsMine(const CScript& script) const

0 commit comments

Comments
 (0)