1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
12 #include <boost/foreach.hpp>
14 bool CKeyStore::AddKey(const CKey
&key
) {
15 return AddKeyPubKey(key
, key
.GetPubKey());
18 bool CBasicKeyStore::GetPubKey(const CKeyID
&address
, CPubKey
&vchPubKeyOut
) const
21 if (!GetKey(address
, key
)) {
23 WatchKeyMap::const_iterator it
= mapWatchKeys
.find(address
);
24 if (it
!= mapWatchKeys
.end()) {
25 vchPubKeyOut
= it
->second
;
30 vchPubKeyOut
= key
.GetPubKey();
34 bool CBasicKeyStore::AddKeyPubKey(const CKey
& key
, const CPubKey
&pubkey
)
37 mapKeys
[pubkey
.GetID()] = key
;
41 bool CBasicKeyStore::AddCScript(const CScript
& redeemScript
)
43 if (redeemScript
.size() > MAX_SCRIPT_ELEMENT_SIZE
)
44 return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE
);
47 mapScripts
[CScriptID(redeemScript
)] = redeemScript
;
51 bool CBasicKeyStore::HaveCScript(const CScriptID
& hash
) const
54 return mapScripts
.count(hash
) > 0;
57 bool CBasicKeyStore::GetCScript(const CScriptID
&hash
, CScript
& redeemScriptOut
) const
60 ScriptMap::const_iterator mi
= mapScripts
.find(hash
);
61 if (mi
!= mapScripts
.end())
63 redeemScriptOut
= (*mi
).second
;
69 static bool ExtractPubKey(const CScript
&dest
, CPubKey
& pubKeyOut
)
71 //TODO: Use Solver to extract this?
72 CScript::const_iterator pc
= dest
.begin();
74 std::vector
<unsigned char> vch
;
75 if (!dest
.GetOp(pc
, opcode
, vch
) || vch
.size() < 33 || vch
.size() > 65)
77 pubKeyOut
= CPubKey(vch
);
78 if (!pubKeyOut
.IsFullyValid())
80 if (!dest
.GetOp(pc
, opcode
, vch
) || opcode
!= OP_CHECKSIG
|| dest
.GetOp(pc
, opcode
, vch
))
85 bool CBasicKeyStore::AddWatchOnly(const CScript
&dest
)
88 setWatchOnly
.insert(dest
);
90 if (ExtractPubKey(dest
, pubKey
))
91 mapWatchKeys
[pubKey
.GetID()] = pubKey
;
95 bool CBasicKeyStore::RemoveWatchOnly(const CScript
&dest
)
98 setWatchOnly
.erase(dest
);
100 if (ExtractPubKey(dest
, pubKey
))
101 mapWatchKeys
.erase(pubKey
.GetID());
105 bool CBasicKeyStore::HaveWatchOnly(const CScript
&dest
) const
108 return setWatchOnly
.count(dest
) > 0;
111 bool CBasicKeyStore::HaveWatchOnly() const
114 return (!setWatchOnly
.empty());