Skip to content

Commit 30a27dc

Browse files
committed
Expose method to find key for a single-key destination
1 parent 985c795 commit 30a27dc

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/keystore.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,25 @@ bool CBasicKeyStore::HaveWatchOnly() const
136136
LOCK(cs_KeyStore);
137137
return (!setWatchOnly.empty());
138138
}
139+
140+
CKeyID GetKeyForDestination(const CKeyStore& store, const CTxDestination& dest)
141+
{
142+
// Only supports destinations which map to single public keys, i.e. P2PKH,
143+
// P2WPKH, and P2SH-P2WPKH.
144+
if (auto id = boost::get<CKeyID>(&dest)) {
145+
return *id;
146+
}
147+
if (auto witness_id = boost::get<WitnessV0KeyHash>(&dest)) {
148+
return CKeyID(*witness_id);
149+
}
150+
if (auto script_id = boost::get<CScriptID>(&dest)) {
151+
CScript script;
152+
CTxDestination inner_dest;
153+
if (store.GetCScript(*script_id, script) && ExtractDestination(script, inner_dest)) {
154+
if (auto inner_witness_id = boost::get<WitnessV0KeyHash>(&inner_dest)) {
155+
return CKeyID(*inner_witness_id);
156+
}
157+
}
158+
}
159+
return CKeyID();
160+
}

src/keystore.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,7 @@ class CBasicKeyStore : public CKeyStore
7878
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
7979
typedef std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char> > > CryptedKeyMap;
8080

81+
/** Return the CKeyID of the key involved in a script (if there is a unique one). */
82+
CKeyID GetKeyForDestination(const CKeyStore& store, const CTxDestination& dest);
83+
8184
#endif // BITCOIN_KEYSTORE_H

0 commit comments

Comments
 (0)