Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

keystore.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 <util.h>
9 
10 bool CKeyStore::AddKey(const CKey &key) {
11  return AddKeyPubKey(key, key.GetPubKey());
12 }
13 
14 bool CBasicKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
15 {
16  CKey key;
17  if (!GetKey(address, key)) {
19  WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
20  if (it != mapWatchKeys.end()) {
21  vchPubKeyOut = it->second;
22  return true;
23  }
24  return false;
25  }
26  vchPubKeyOut = key.GetPubKey();
27  return true;
28 }
29 
30 bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
31 {
33  mapKeys[pubkey.GetID()] = key;
34  return true;
35 }
36 
37 bool CBasicKeyStore::HaveKey(const CKeyID &address) const
38 {
40  return mapKeys.count(address) > 0;
41 }
42 
43 std::set<CKeyID> CBasicKeyStore::GetKeys() const
44 {
46  std::set<CKeyID> set_address;
47  for (const auto& mi : mapKeys) {
48  set_address.insert(mi.first);
49  }
50  return set_address;
51 }
52 
53 bool CBasicKeyStore::GetKey(const CKeyID &address, CKey &keyOut) const
54 {
56  KeyMap::const_iterator mi = mapKeys.find(address);
57  if (mi != mapKeys.end()) {
58  keyOut = mi->second;
59  return true;
60  }
61  return false;
62 }
63 
64 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
65 {
66  if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
67  return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
68 
70  mapScripts[CScriptID(redeemScript)] = redeemScript;
71  return true;
72 }
73 
74 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
75 {
77  return mapScripts.count(hash) > 0;
78 }
79 
80 std::set<CScriptID> CBasicKeyStore::GetCScripts() const
81 {
83  std::set<CScriptID> set_script;
84  for (const auto& mi : mapScripts) {
85  set_script.insert(mi.first);
86  }
87  return set_script;
88 }
89 
90 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
91 {
93  ScriptMap::const_iterator mi = mapScripts.find(hash);
94  if (mi != mapScripts.end())
95  {
96  redeemScriptOut = (*mi).second;
97  return true;
98  }
99  return false;
100 }
101 
102 static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
103 {
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;
116 }
117 
119 {
120  LOCK(cs_KeyStore);
121  setWatchOnly.insert(dest);
122  CPubKey pubKey;
123  if (ExtractPubKey(dest, pubKey))
124  mapWatchKeys[pubKey.GetID()] = pubKey;
125  return true;
126 }
127 
129 {
130  LOCK(cs_KeyStore);
131  setWatchOnly.erase(dest);
132  CPubKey pubKey;
133  if (ExtractPubKey(dest, pubKey))
134  mapWatchKeys.erase(pubKey.GetID());
135  return true;
136 }
137 
138 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
139 {
140  LOCK(cs_KeyStore);
141  return setWatchOnly.count(dest) > 0;
142 }
143 
145 {
146  LOCK(cs_KeyStore);
147  return (!setWatchOnly.empty());
148 }
149 
150 bool CBasicKeyStore::GetHDChain(CHDChain& hdChainRet) const
151 {
152  hdChainRet = hdChain;
153  return !hdChain.IsNull();
154 }
CCriticalSection cs_KeyStore
Definition: keystore.h:22
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)=0
Add a key to the store.
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:179
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
Definition: keystore.cpp:14
bool HaveCScript(const CScriptID &hash) const override
Definition: keystore.cpp:74
std::set< CScriptID > GetCScripts() const override
Definition: keystore.cpp:80
bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
Definition: keystore.cpp:90
std::set< CKeyID > GetKeys() const override
Definition: keystore.cpp:43
bool AddCScript(const CScript &redeemScript) override
Support for BIP 0013 : see https://github.com/bitcoin/bips/blob/master/bip-0013.mediawiki.
Definition: keystore.cpp:64
static bool ExtractPubKey(const CScript &dest, CPubKey &pubKeyOut)
Definition: keystore.cpp:102
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:23
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:149
CHDChain hdChain
Definition: keystore.h:64
opcodetype
Script opcodes.
Definition: script.h:48
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
Definition: pubkey.cpp:206
WatchKeyMap mapWatchKeys
Definition: keystore.h:60
#define LOCK(cs)
Definition: sync.h:178
An encapsulated public key.
Definition: pubkey.h:30
ScriptMap mapScripts
Definition: keystore.h:61
bool IsNull() const
Definition: hdchain.cpp:27
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override
Add a key to the store.
Definition: keystore.cpp:30
WatchOnlySet setWatchOnly
Definition: keystore.h:62
bool GetKey(const CKeyID &address, CKey &keyOut) const override
Definition: keystore.cpp:53
virtual bool AddKey(const CKey &key)
Definition: keystore.cpp:10
bool RemoveWatchOnly(const CScript &dest) override
Definition: keystore.cpp:128
KeyMap mapKeys
Definition: keystore.h:59
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:389
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
bool error(const char *fmt, const Args &... args)
Definition: util.h:222
iterator begin()
Definition: prevector.h:318
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:22
size_type size() const
Definition: prevector.h:310
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
Definition: script.h:496
An encapsulated private key.
Definition: key.h:27
bool HaveWatchOnly() const override
Definition: keystore.cpp:144
bool HaveKey(const CKeyID &address) const override
Check whether a key corresponding to a given address is present in the store.
Definition: keystore.cpp:37
bool AddWatchOnly(const CScript &dest) override
Support for Watch-only addresses.
Definition: keystore.cpp:118
virtual bool GetHDChain(CHDChain &hdChainRet) const
Definition: keystore.cpp:150
Released under the MIT license