Merge #12079: Improve prioritisetransaction test coverage
[bitcoinplatinum.git] / src / keystore.cpp
blob55c96564380000ad6700b7877446c30f65ef1e90
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 <util.h>
10 bool CKeyStore::AddKey(const CKey &key) {
11 return AddKeyPubKey(key, key.GetPubKey());
14 bool CBasicKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
16 CKey key;
17 if (!GetKey(address, key)) {
18 LOCK(cs_KeyStore);
19 WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
20 if (it != mapWatchKeys.end()) {
21 vchPubKeyOut = it->second;
22 return true;
24 return false;
26 vchPubKeyOut = key.GetPubKey();
27 return true;
30 bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
32 LOCK(cs_KeyStore);
33 mapKeys[pubkey.GetID()] = key;
34 return true;
37 bool CBasicKeyStore::HaveKey(const CKeyID &address) const
39 LOCK(cs_KeyStore);
40 return mapKeys.count(address) > 0;
43 std::set<CKeyID> CBasicKeyStore::GetKeys() const
45 LOCK(cs_KeyStore);
46 std::set<CKeyID> set_address;
47 for (const auto& mi : mapKeys) {
48 set_address.insert(mi.first);
50 return set_address;
53 bool CBasicKeyStore::GetKey(const CKeyID &address, CKey &keyOut) const
55 LOCK(cs_KeyStore);
56 KeyMap::const_iterator mi = mapKeys.find(address);
57 if (mi != mapKeys.end()) {
58 keyOut = mi->second;
59 return true;
61 return false;
64 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
66 if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
67 return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
69 LOCK(cs_KeyStore);
70 mapScripts[CScriptID(redeemScript)] = redeemScript;
71 return true;
74 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
76 LOCK(cs_KeyStore);
77 return mapScripts.count(hash) > 0;
80 std::set<CScriptID> CBasicKeyStore::GetCScripts() const
82 LOCK(cs_KeyStore);
83 std::set<CScriptID> set_script;
84 for (const auto& mi : mapScripts) {
85 set_script.insert(mi.first);
87 return set_script;
90 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
92 LOCK(cs_KeyStore);
93 ScriptMap::const_iterator mi = mapScripts.find(hash);
94 if (mi != mapScripts.end())
96 redeemScriptOut = (*mi).second;
97 return true;
99 return false;
102 static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
104 //TODO: Use Solver to extract this?
105 CScript::const_iterator pc = dest.begin();
106 opcodetype opcode;
107 std::vector<unsigned char> vch;
108 if (!dest.GetOp(pc, opcode, vch) || vch.size() < 33 || vch.size() > 65)
109 return false;
110 pubKeyOut = CPubKey(vch);
111 if (!pubKeyOut.IsFullyValid())
112 return false;
113 if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch))
114 return false;
115 return true;
118 bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
120 LOCK(cs_KeyStore);
121 setWatchOnly.insert(dest);
122 CPubKey pubKey;
123 if (ExtractPubKey(dest, pubKey))
124 mapWatchKeys[pubKey.GetID()] = pubKey;
125 return true;
128 bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
130 LOCK(cs_KeyStore);
131 setWatchOnly.erase(dest);
132 CPubKey pubKey;
133 if (ExtractPubKey(dest, pubKey))
134 mapWatchKeys.erase(pubKey.GetID());
135 return true;
138 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
140 LOCK(cs_KeyStore);
141 return setWatchOnly.count(dest) > 0;
144 bool CBasicKeyStore::HaveWatchOnly() const
146 LOCK(cs_KeyStore);
147 return (!setWatchOnly.empty());