Merge #10767: [wallet] Clarify wallet initialization / destruction interface
[bitcoinplatinum.git] / src / keystore.cpp
blob8454175ca81a0e5538a8b1ce054e46b65b3d67ea
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::AddCScript(const CScript& redeemScript)
41 if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
42 return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
44 LOCK(cs_KeyStore);
45 mapScripts[CScriptID(redeemScript)] = redeemScript;
46 return true;
49 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
51 LOCK(cs_KeyStore);
52 return mapScripts.count(hash) > 0;
55 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
57 LOCK(cs_KeyStore);
58 ScriptMap::const_iterator mi = mapScripts.find(hash);
59 if (mi != mapScripts.end())
61 redeemScriptOut = (*mi).second;
62 return true;
64 return false;
67 static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
69 //TODO: Use Solver to extract this?
70 CScript::const_iterator pc = dest.begin();
71 opcodetype opcode;
72 std::vector<unsigned char> vch;
73 if (!dest.GetOp(pc, opcode, vch) || vch.size() < 33 || vch.size() > 65)
74 return false;
75 pubKeyOut = CPubKey(vch);
76 if (!pubKeyOut.IsFullyValid())
77 return false;
78 if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch))
79 return false;
80 return true;
83 bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
85 LOCK(cs_KeyStore);
86 setWatchOnly.insert(dest);
87 CPubKey pubKey;
88 if (ExtractPubKey(dest, pubKey))
89 mapWatchKeys[pubKey.GetID()] = pubKey;
90 return true;
93 bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
95 LOCK(cs_KeyStore);
96 setWatchOnly.erase(dest);
97 CPubKey pubKey;
98 if (ExtractPubKey(dest, pubKey))
99 mapWatchKeys.erase(pubKey.GetID());
100 return true;
103 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
105 LOCK(cs_KeyStore);
106 return setWatchOnly.count(dest) > 0;
109 bool CBasicKeyStore::HaveWatchOnly() const
111 LOCK(cs_KeyStore);
112 return (!setWatchOnly.empty());