Line data Source code
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.
5 :
6 : #include "keystore.h"
7 :
8 : #include "key.h"
9 : #include "pubkey.h"
10 : #include "util.h"
11 :
12 : #include <boost/foreach.hpp>
13 :
14 39 : bool CKeyStore::AddKey(const CKey &key) {
15 39 : return AddKeyPubKey(key, key.GetPubKey());
16 : }
17 :
18 356 : bool CBasicKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
19 : {
20 : CKey key;
21 356 : if (!GetKey(address, key)) {
22 24 : WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
23 24 : if (it != mapWatchKeys.end()) {
24 11 : vchPubKeyOut = it->second;
25 11 : return true;
26 : }
27 : return false;
28 : }
29 344 : vchPubKeyOut = key.GetPubKey();
30 : return true;
31 : }
32 :
33 3986 : bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
34 : {
35 3986 : LOCK(cs_KeyStore);
36 3986 : mapKeys[pubkey.GetID()] = key;
37 7972 : return true;
38 : }
39 :
40 29 : bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
41 : {
42 58 : if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
43 0 : return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
44 :
45 29 : LOCK(cs_KeyStore);
46 58 : mapScripts[CScriptID(redeemScript)] = redeemScript;
47 29 : return true;
48 : }
49 :
50 1 : bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
51 : {
52 1 : LOCK(cs_KeyStore);
53 3 : return mapScripts.count(hash) > 0;
54 : }
55 :
56 439 : bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
57 : {
58 439 : LOCK(cs_KeyStore);
59 878 : ScriptMap::const_iterator mi = mapScripts.find(hash);
60 878 : if (mi != mapScripts.end())
61 : {
62 105 : redeemScriptOut = (*mi).second;
63 : return true;
64 : }
65 : return false;
66 : }
67 :
68 6 : static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
69 : {
70 : //TODO: Use Solver to extract this?
71 12 : CScript::const_iterator pc = dest.begin();
72 : opcodetype opcode;
73 : std::vector<unsigned char> vch;
74 12 : if (!dest.GetOp(pc, opcode, vch) || vch.size() < 33 || vch.size() > 65)
75 : return false;
76 2 : pubKeyOut = CPubKey(vch);
77 2 : if (!pubKeyOut.IsFullyValid())
78 : return false;
79 4 : if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch))
80 : return false;
81 2 : return true;
82 : }
83 :
84 6 : bool CBasicKeyStore::AddWatchOnly(const CScript &dest)
85 : {
86 6 : LOCK(cs_KeyStore);
87 6 : setWatchOnly.insert(dest);
88 : CPubKey pubKey;
89 6 : if (ExtractPubKey(dest, pubKey))
90 2 : mapWatchKeys[pubKey.GetID()] = pubKey;
91 12 : return true;
92 : }
93 :
94 0 : bool CBasicKeyStore::RemoveWatchOnly(const CScript &dest)
95 : {
96 0 : LOCK(cs_KeyStore);
97 0 : setWatchOnly.erase(dest);
98 : CPubKey pubKey;
99 0 : if (ExtractPubKey(dest, pubKey))
100 0 : mapWatchKeys.erase(pubKey.GetID());
101 0 : return true;
102 : }
103 :
104 20696 : bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
105 : {
106 20696 : LOCK(cs_KeyStore);
107 62088 : return setWatchOnly.count(dest) > 0;
108 : }
109 :
110 0 : bool CBasicKeyStore::HaveWatchOnly() const
111 : {
112 0 : LOCK(cs_KeyStore);
113 0 : return (!setWatchOnly.empty());
114 330 : }
|