Skip to content

Commit cbffa80

Browse files
committed
Add logic to track pubkeys as watch-only, not just scripts
- backports bitcoin/bitcoin@cfc3dd3
1 parent 12b38b0 commit cbffa80

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

src/crypter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,16 @@ bool CCryptoKeyStore::GetPubKey(const CKeyID& address, CPubKey& vchPubKeyOut) co
199199
{
200200
LOCK(cs_KeyStore);
201201
if (!IsCrypted())
202-
return CKeyStore::GetPubKey(address, vchPubKeyOut);
202+
return CBasicKeyStore::GetPubKey(address, vchPubKeyOut);
203203

204204
CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
205205
if (mi != mapCryptedKeys.end()) {
206206
vchPubKeyOut = (*mi).second.first;
207207
return true;
208208
}
209+
210+
// Check for watch-only pubkeys
211+
return CBasicKeyStore::GetPubKey(address, vchPubKeyOut);
209212
}
210213
return false;
211214
}

src/keystore.cpp

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,26 @@
1313
#include "util.h"
1414

1515

16-
bool CKeyStore::GetPubKey(const CKeyID& address, CPubKey& vchPubKeyOut) const
16+
bool CKeyStore::AddKey(const CKey& key)
17+
{
18+
return AddKeyPubKey(key, key.GetPubKey());
19+
}
20+
21+
bool CBasicKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
1722
{
1823
CKey key;
19-
if (!GetKey(address, key))
24+
if (!GetKey(address, key)) {
25+
WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
26+
if (it != mapWatchKeys.end()) {
27+
vchPubKeyOut = it->second;
28+
return true;
29+
}
2030
return false;
31+
}
2132
vchPubKeyOut = key.GetPubKey();
2233
return true;
2334
}
2435

25-
bool CKeyStore::AddKey(const CKey& key)
26-
{
27-
return AddKeyPubKey(key, key.GetPubKey());
28-
}
29-
3036
bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey& pubkey)
3137
{
3238
LOCK(cs_KeyStore);
@@ -61,17 +67,39 @@ bool CBasicKeyStore::GetCScript(const CScriptID& hash, CScript& redeemScriptOut)
6167
return false;
6268
}
6369

70+
static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
71+
{
72+
//TODO: Use Solver to extract this?
73+
CScript::const_iterator pc = dest.begin();
74+
opcodetype opcode;
75+
std::vector<unsigned char> vch;
76+
if (!dest.GetOp(pc, opcode, vch) || vch.size() < 33 || vch.size() > 65)
77+
return false;
78+
pubKeyOut = CPubKey(vch);
79+
if (!pubKeyOut.IsFullyValid())
80+
return false;
81+
if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch))
82+
return false;
83+
return true;
84+
}
85+
6486
bool CBasicKeyStore::AddWatchOnly(const CScript& dest)
6587
{
6688
LOCK(cs_KeyStore);
6789
setWatchOnly.insert(dest);
90+
CPubKey pubKey;
91+
if (ExtractPubKey(dest, pubKey))
92+
mapWatchKeys[pubKey.GetID()] = pubKey;
6893
return true;
6994
}
7095

7196
bool CBasicKeyStore::RemoveWatchOnly(const CScript& dest)
7297
{
7398
LOCK(cs_KeyStore);
7499
setWatchOnly.erase(dest);
100+
CPubKey pubKey;
101+
if (ExtractPubKey(dest, pubKey))
102+
mapWatchKeys.erase(pubKey.GetID());
75103
return true;
76104
}
77105

src/keystore.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class CKeyStore
3434
virtual bool HaveKey(const CKeyID& address) const = 0;
3535
virtual bool GetKey(const CKeyID& address, CKey& keyOut) const = 0;
3636
virtual void GetKeys(std::set<CKeyID>& setAddress) const = 0;
37-
virtual bool GetPubKey(const CKeyID& address, CPubKey& vchPubKeyOut) const;
37+
virtual bool GetPubKey(const CKeyID& address, CPubKey& vchPubKeyOut) const = 0;
3838

3939
//! Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki
4040
virtual bool AddCScript(const CScript& redeemScript) = 0;
@@ -49,6 +49,7 @@ class CKeyStore
4949
};
5050

5151
typedef std::map<CKeyID, CKey> KeyMap;
52+
typedef std::map<CKeyID, CPubKey> WatchKeyMap;
5253
typedef std::map<CScriptID, CScript> ScriptMap;
5354
typedef std::set<CScript> WatchOnlySet;
5455

@@ -57,11 +58,13 @@ class CBasicKeyStore : public CKeyStore
5758
{
5859
protected:
5960
KeyMap mapKeys;
61+
WatchKeyMap mapWatchKeys;
6062
ScriptMap mapScripts;
6163
WatchOnlySet setWatchOnly;
6264

6365
public:
6466
bool AddKeyPubKey(const CKey& key, const CPubKey& pubkey);
67+
bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
6568
bool HaveKey(const CKeyID& address) const;
6669
void GetKeys(std::set<CKeyID>& setAddress) const;
6770
bool GetKey(const CKeyID& address, CKey& keyOut) const;

0 commit comments

Comments
 (0)