Merge #11747: Fix: Open files read only if requested
[bitcoinplatinum.git] / src / keystore.cpp
blob5718a14ca237541afc3dead572b426fe669c0d93
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 bool CKeyStore::AddKey(const CKey &key) {
13 return AddKeyPubKey(key, key.GetPubKey());
16 bool CBasicKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
18 CKey key;
19 if (!GetKey(address, key)) {
20 LOCK(cs_KeyStore);
21 WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
22 if (it != mapWatchKeys.end()) {
23 vchPubKeyOut = it->second;
24 return true;
26 return false;
28 vchPubKeyOut = key.GetPubKey();
29 return true;
32 bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
34 LOCK(cs_KeyStore);
35 mapKeys[pubkey.GetID()] = key;
36 return true;
39 bool CBasicKeyStore::HaveKey(const CKeyID &address) const
41 LOCK(cs_KeyStore);
42 return mapKeys.count(address) > 0;
45 std::set<CKeyID> CBasicKeyStore::GetKeys() const
47 LOCK(cs_KeyStore);
48 std::set<CKeyID> set_address;
49 for (const auto& mi : mapKeys) {
50 set_address.insert(mi.first);
52 return set_address;
55 bool CBasicKeyStore::GetKey(const CKeyID &address, CKey &keyOut) const
57 LOCK(cs_KeyStore);
58 KeyMap::const_iterator mi = mapKeys.find(address);
59 if (mi != mapKeys.end()) {
60 keyOut = mi->second;
61 return true;
63 return false;
66 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
68 if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
69 return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
71 LOCK(cs_KeyStore);
72 mapScripts[CScriptID(redeemScript)] = redeemScript;
73 return true;
76 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
78 LOCK(cs_KeyStore);
79 return mapScripts.count(hash) > 0;
82 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
84 LOCK(cs_KeyStore);
85 ScriptMap::const_iterator mi = mapScripts.find(hash);
86 if (mi != mapScripts.end())
88 redeemScriptOut = (*mi).second;
89 return true;
91 return false;
94 static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
96 //TODO: Use Solver to extract this?
97 CScript::const_iterator pc = dest.begin();
98 opcodetype opcode;
99 std::vector<unsigned char> vch;
100 if (!dest.GetOp(pc, opcode, vch) || vch.size() < 33 || vch.size() > 65)
101 return false;
102 pubKeyOut = CPubKey(vch);
103 if (!pubKeyOut.IsFullyValid())
104 return false;
105 if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch))
106 return false;
107 return true;
110 bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
112 LOCK(cs_KeyStore);
113 setWatchOnly.insert(dest);
114 CPubKey pubKey;
115 if (ExtractPubKey(dest, pubKey))
116 mapWatchKeys[pubKey.GetID()] = pubKey;
117 return true;
120 bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
122 LOCK(cs_KeyStore);
123 setWatchOnly.erase(dest);
124 CPubKey pubKey;
125 if (ExtractPubKey(dest, pubKey))
126 mapWatchKeys.erase(pubKey.GetID());
127 return true;
130 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
132 LOCK(cs_KeyStore);
133 return setWatchOnly.count(dest) > 0;
136 bool CBasicKeyStore::HaveWatchOnly() const
138 LOCK(cs_KeyStore);
139 return (!setWatchOnly.empty());