Skip to content

Commit 3b8691b

Browse files
pinheadmzluke-jr
authored andcommitted
wallet: implement IsKeyActive() in scriptpubkeyman
This new method returns true if the given CScript key is derived from the SPKM. For Legacy that means checking the hd_seed_id in the key's metadata. Also patches PKDescriptor to return associated public keys in MakeScripts() Github-Pull: bitcoin#27216 Rebased-From: f39957f
1 parent 1248d0d commit 3b8691b

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/script/descriptor.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,11 @@ class PKDescriptor final : public DescriptorImpl
823823
private:
824824
const bool m_xonly;
825825
protected:
826-
std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, Span<const CScript>, FlatSigningProvider&) const override
826+
std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, Span<const CScript>, FlatSigningProvider& out) const override
827827
{
828+
CKeyID id = keys[0].GetID();
829+
out.pubkeys.emplace(id, keys[0]);
830+
828831
if (m_xonly) {
829832
CScript script = CScript() << ToByteVector(XOnlyPubKey(keys[0])) << OP_CHECKSIG;
830833
return Vector(std::move(script));

src/wallet/scriptpubkeyman.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,23 @@ std::vector<WalletDestination> LegacyScriptPubKeyMan::MarkUnusedAddresses(const
402402
return result;
403403
}
404404

405+
bool LegacyScriptPubKeyMan::IsKeyActive(const CScript& script) const
406+
{
407+
LOCK(cs_KeyStore);
408+
409+
if (!IsMine(script)) return false; // Not in the keystore at all
410+
411+
for (const auto& key_id : GetAffectedKeys(script, *this)) {
412+
const auto it = mapKeyMetadata.find(key_id);
413+
if (it == mapKeyMetadata.end()) return false; // This key must be really old
414+
415+
if (!it->second.hd_seed_id.IsNull() && it->second.hd_seed_id == m_hd_chain.seed_id) return true;
416+
}
417+
418+
// Imported or dumped for a new keypool
419+
return false;
420+
}
421+
405422
void LegacyScriptPubKeyMan::UpgradeKeyMetadata()
406423
{
407424
LOCK(cs_KeyStore);

src/wallet/scriptpubkeyman.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ class ScriptPubKeyMan
205205
*/
206206
virtual std::vector<WalletDestination> MarkUnusedAddresses(const CScript& script) { return {}; }
207207

208+
/* Determines if address is derived from active key manager */
209+
virtual bool IsKeyActive(const CScript& script) const = 0;
210+
208211
/** Sets up the key generation stuff, i.e. generates new HD seeds and sets them as active.
209212
* Returns false if already setup or setup fails, true if setup is successful
210213
* Set force=true to make it re-setup if already setup, used for upgrades
@@ -462,6 +465,8 @@ class LegacyScriptPubKeyMan : public LegacyDataSPKM
462465

463466
std::vector<WalletDestination> MarkUnusedAddresses(const CScript& script) override;
464467

468+
[[nodiscard]] bool IsKeyActive(const CScript& script) const override;
469+
465470
//! Upgrade stored CKeyMetadata objects to store key origin info as KeyOriginInfo
466471
void UpgradeKeyMetadata();
467472

@@ -650,6 +655,8 @@ class DescriptorScriptPubKeyMan : public ScriptPubKeyMan
650655

651656
std::vector<WalletDestination> MarkUnusedAddresses(const CScript& script) override;
652657

658+
[[nodiscard]] bool IsKeyActive(const CScript& script) const override { return IsMine(script); }
659+
653660
bool IsHDEnabled() const override;
654661

655662
//! Setup descriptors based on the given CExtkey

0 commit comments

Comments
 (0)