1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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
)) {
22 WatchKeyMap::const_iterator it
= mapWatchKeys
.find(address
);
23 if (it
!= mapWatchKeys
.end()) {
24 vchPubKeyOut
= it
->second
;
29 vchPubKeyOut
= key
.GetPubKey();
33 bool CBasicKeyStore::AddKeyPubKey(const CKey
& key
, const CPubKey
&pubkey
)
36 mapKeys
[pubkey
.GetID()] = key
;
40 bool CBasicKeyStore::AddCScript(const CScript
& redeemScript
)
42 if (redeemScript
.size() > MAX_SCRIPT_ELEMENT_SIZE
)
43 return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE
);
46 mapScripts
[CScriptID(redeemScript
)] = redeemScript
;
50 bool CBasicKeyStore::HaveCScript(const CScriptID
& hash
) const
53 return mapScripts
.count(hash
) > 0;
56 bool CBasicKeyStore::GetCScript(const CScriptID
&hash
, CScript
& redeemScriptOut
) const
59 ScriptMap::const_iterator mi
= mapScripts
.find(hash
);
60 if (mi
!= mapScripts
.end())
62 redeemScriptOut
= (*mi
).second
;
68 static bool ExtractPubKey(const CScript
&dest
, CPubKey
& pubKeyOut
)
70 //TODO: Use Solver to extract this?
71 CScript::const_iterator pc
= dest
.begin();
73 std::vector
<unsigned char> vch
;
74 if (!dest
.GetOp(pc
, opcode
, vch
) || vch
.size() < 33 || vch
.size() > 65)
76 pubKeyOut
= CPubKey(vch
);
77 if (!pubKeyOut
.IsFullyValid())
79 if (!dest
.GetOp(pc
, opcode
, vch
) || opcode
!= OP_CHECKSIG
|| dest
.GetOp(pc
, opcode
, vch
))
84 bool CBasicKeyStore::AddWatchOnly(const CScript
&dest
)
87 setWatchOnly
.insert(dest
);
89 if (ExtractPubKey(dest
, pubKey
))
90 mapWatchKeys
[pubKey
.GetID()] = pubKey
;
94 bool CBasicKeyStore::RemoveWatchOnly(const CScript
&dest
)
97 setWatchOnly
.erase(dest
);
99 if (ExtractPubKey(dest
, pubKey
))
100 mapWatchKeys
.erase(pubKey
.GetID());
104 bool CBasicKeyStore::HaveWatchOnly(const CScript
&dest
) const
107 return setWatchOnly
.count(dest
) > 0;
110 bool CBasicKeyStore::HaveWatchOnly() const
113 return (!setWatchOnly
.empty());