Dogecoin Core  1.14.2
P2P Digital Currency
keystore.cpp
Go to the documentation of this file.
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.
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 bool CKeyStore::AddKey(const CKey &key) {
15  return AddKeyPubKey(key, key.GetPubKey());
16 }
17 
18 bool CBasicKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
19 {
20  CKey key;
21  if (!GetKey(address, key)) {
23  WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
24  if (it != mapWatchKeys.end()) {
25  vchPubKeyOut = it->second;
26  return true;
27  }
28  return false;
29  }
30  vchPubKeyOut = key.GetPubKey();
31  return true;
32 }
33 
34 bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
35 {
37  mapKeys[pubkey.GetID()] = key;
38  return true;
39 }
40 
41 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
42 {
43  if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
44  return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
45 
47  mapScripts[CScriptID(redeemScript)] = redeemScript;
48  return true;
49 }
50 
51 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
52 {
54  return mapScripts.count(hash) > 0;
55 }
56 
57 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
58 {
60  ScriptMap::const_iterator mi = mapScripts.find(hash);
61  if (mi != mapScripts.end())
62  {
63  redeemScriptOut = (*mi).second;
64  return true;
65  }
66  return false;
67 }
68 
69 static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
70 {
71  //TODO: Use Solver to extract this?
72  CScript::const_iterator pc = dest.begin();
73  opcodetype opcode;
74  std::vector<unsigned char> vch;
75  if (!dest.GetOp(pc, opcode, vch) || vch.size() < 33 || vch.size() > 65)
76  return false;
77  pubKeyOut = CPubKey(vch);
78  if (!pubKeyOut.IsFullyValid())
79  return false;
80  if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch))
81  return false;
82  return true;
83 }
84 
86 {
88  setWatchOnly.insert(dest);
89  CPubKey pubKey;
90  if (ExtractPubKey(dest, pubKey))
91  mapWatchKeys[pubKey.GetID()] = pubKey;
92  return true;
93 }
94 
96 {
98  setWatchOnly.erase(dest);
99  CPubKey pubKey;
100  if (ExtractPubKey(dest, pubKey))
101  mapWatchKeys.erase(pubKey.GetID());
102  return true;
103 }
104 
105 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
106 {
107  LOCK(cs_KeyStore);
108  return setWatchOnly.count(dest) > 0;
109 }
110 
112 {
113  LOCK(cs_KeyStore);
114  return (!setWatchOnly.empty());
115 }
virtual bool RemoveWatchOnly(const CScript &dest)
Definition: keystore.cpp:95
virtual bool AddWatchOnly(const CScript &dest)
Support for Watch-only addresses.
Definition: keystore.cpp:85
WatchKeyMap mapWatchKeys
Definition: keystore.h:59
virtual bool HaveCScript(const CScriptID &hash) const
Definition: keystore.cpp:51
virtual bool AddCScript(const CScript &redeemScript)
Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki.
Definition: keystore.cpp:41
bool GetKey(const CKeyID &address, CKey &keyOut) const
Definition: keystore.h:88
ScriptMap mapScripts
Definition: keystore.h:60
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const
Definition: keystore.cpp:57
virtual bool HaveWatchOnly() const
Definition: keystore.cpp:111
WatchOnlySet setWatchOnly
Definition: keystore.h:61
KeyMap mapKeys
Definition: keystore.h:58
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Add a key to the store.
Definition: keystore.cpp:34
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
Definition: keystore.cpp:18
An encapsulated private key.
Definition: key.h:36
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:155
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:30
virtual bool AddKey(const CKey &key)
Definition: keystore.cpp:14
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)=0
Add a key to the store.
CCriticalSection cs_KeyStore
Definition: keystore.h:22
An encapsulated public key.
Definition: pubkey.h:40
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:142
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid())
Definition: pubkey.cpp:207
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:377
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
Definition: script.h:475
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:23
size_type size() const
Definition: prevector.h:281
iterator begin()
Definition: prevector.h:289
opcodetype
Script opcodes.
Definition: script.h:45
@ OP_CHECKSIG
Definition: script.h:161
#define LOCK(cs)
Definition: sync.h:177
bool error(const char *fmt, const Args &... args)
Definition: util.h:87