[doc][trivial] Remove source forge from Debian watch.
[bitcoinplatinum.git] / src / keystore.cpp
blobcf49ba83ade40e9c77e454dff13aa2ee39afb167
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.
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 WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
23 if (it != mapWatchKeys.end()) {
24 vchPubKeyOut = it->second;
25 return true;
27 return false;
29 vchPubKeyOut = key.GetPubKey();
30 return true;
33 bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
35 LOCK(cs_KeyStore);
36 mapKeys[pubkey.GetID()] = key;
37 return true;
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);
45 LOCK(cs_KeyStore);
46 mapScripts[CScriptID(redeemScript)] = redeemScript;
47 return true;
50 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
52 LOCK(cs_KeyStore);
53 return mapScripts.count(hash) > 0;
56 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
58 LOCK(cs_KeyStore);
59 ScriptMap::const_iterator mi = mapScripts.find(hash);
60 if (mi != mapScripts.end())
62 redeemScriptOut = (*mi).second;
63 return true;
65 return false;
68 static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
70 //TODO: Use Solver to extract this?
71 CScript::const_iterator pc = dest.begin();
72 opcodetype opcode;
73 std::vector<unsigned char> vch;
74 if (!dest.GetOp(pc, opcode, vch) || vch.size() < 33 || vch.size() > 65)
75 return false;
76 pubKeyOut = CPubKey(vch);
77 if (!pubKeyOut.IsFullyValid())
78 return false;
79 if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch))
80 return false;
81 return true;
84 bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
86 LOCK(cs_KeyStore);
87 setWatchOnly.insert(dest);
88 CPubKey pubKey;
89 if (ExtractPubKey(dest, pubKey))
90 mapWatchKeys[pubKey.GetID()] = pubKey;
91 return true;
94 bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
96 LOCK(cs_KeyStore);
97 setWatchOnly.erase(dest);
98 CPubKey pubKey;
99 if (ExtractPubKey(dest, pubKey))
100 mapWatchKeys.erase(pubKey.GetID());
101 return true;
104 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
106 LOCK(cs_KeyStore);
107 return setWatchOnly.count(dest) > 0;
110 bool CBasicKeyStore::HaveWatchOnly() const
112 LOCK(cs_KeyStore);
113 return (!setWatchOnly.empty());