[tests] Add libFuzzer support.
[bitcoinplatinum.git] / src / keystore.cpp
blobb17567e99b72e679f2a219c48e1be670788a5ebd
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.
6 #include "keystore.h"
8 #include "key.h"
9 #include "pubkey.h"
10 #include "util.h"
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
20 CKey key;
21 if (!GetKey(address, key)) {
22 LOCK(cs_KeyStore);
23 WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
24 if (it != mapWatchKeys.end()) {
25 vchPubKeyOut = it->second;
26 return true;
28 return false;
30 vchPubKeyOut = key.GetPubKey();
31 return true;
34 bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
36 LOCK(cs_KeyStore);
37 mapKeys[pubkey.GetID()] = key;
38 return true;
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);
46 LOCK(cs_KeyStore);
47 mapScripts[CScriptID(redeemScript)] = redeemScript;
48 return true;
51 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
53 LOCK(cs_KeyStore);
54 return mapScripts.count(hash) > 0;
57 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
59 LOCK(cs_KeyStore);
60 ScriptMap::const_iterator mi = mapScripts.find(hash);
61 if (mi != mapScripts.end())
63 redeemScriptOut = (*mi).second;
64 return true;
66 return false;
69 static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
71 //TODO: Use Solver to extract this?
72 CScript::const_iterator pc = dest.begin();
73 opcodetype opcode;
74 std::vector<unsigned char> vch;
75 if (!dest.GetOp(pc, opcode, vch) || vch.size() < 33 || vch.size() > 65)
76 return false;
77 pubKeyOut = CPubKey(vch);
78 if (!pubKeyOut.IsFullyValid())
79 return false;
80 if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch))
81 return false;
82 return true;
85 bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
87 LOCK(cs_KeyStore);
88 setWatchOnly.insert(dest);
89 CPubKey pubKey;
90 if (ExtractPubKey(dest, pubKey))
91 mapWatchKeys[pubKey.GetID()] = pubKey;
92 return true;
95 bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
97 LOCK(cs_KeyStore);
98 setWatchOnly.erase(dest);
99 CPubKey pubKey;
100 if (ExtractPubKey(dest, pubKey))
101 mapWatchKeys.erase(pubKey.GetID());
102 return true;
105 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
107 LOCK(cs_KeyStore);
108 return setWatchOnly.count(dest) > 0;
111 bool CBasicKeyStore::HaveWatchOnly() const
113 LOCK(cs_KeyStore);
114 return (!setWatchOnly.empty());