Raven Core  3.0.0
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 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "keystore.h"
8 
9 #include "key.h"
10 #include "pubkey.h"
11 #include "util.h"
12 
13 bool CKeyStore::AddKey(const CKey &key) {
14  return AddKeyPubKey(key, key.GetPubKey());
15 }
16 
17 bool CBasicKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
18 {
19  CKey key;
20  if (!GetKey(address, key)) {
22  WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
23  if (it != mapWatchKeys.end()) {
24  vchPubKeyOut = it->second;
25  return true;
26  }
27  return false;
28  }
29  vchPubKeyOut = key.GetPubKey();
30  return true;
31 }
32 
33 bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
34 {
36  mapKeys[pubkey.GetID()] = key;
37  return true;
38 }
39 
40 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
41 {
42  if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
43  return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
44 
46  mapScripts[CScriptID(redeemScript)] = redeemScript;
47  return true;
48 }
49 
50 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
51 {
53  return mapScripts.count(hash) > 0;
54 }
55 
56 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
57 {
59  ScriptMap::const_iterator mi = mapScripts.find(hash);
60  if (mi != mapScripts.end())
61  {
62  redeemScriptOut = (*mi).second;
63  return true;
64  }
65  return false;
66 }
67 
68 static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
69 {
70  //TODO: Use Solver to extract this?
71  CScript::const_iterator pc = dest.begin();
72  opcodetype opcode;
73  std::vector<unsigned char> vch;
74  if (!dest.GetOp(pc, opcode, vch) || vch.size() < 33 || vch.size() > 65)
75  return false;
76  pubKeyOut = CPubKey(vch);
77  if (!pubKeyOut.IsFullyValid())
78  return false;
79  if (!dest.GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG || dest.GetOp(pc, opcode, vch))
80  return false;
81  return true;
82 }
83 
85 {
87  setWatchOnly.insert(dest);
88  CPubKey pubKey;
89  if (ExtractPubKey(dest, pubKey))
90  mapWatchKeys[pubKey.GetID()] = pubKey;
91  return true;
92 }
93 
95 {
97  setWatchOnly.erase(dest);
98  CPubKey pubKey;
99  if (ExtractPubKey(dest, pubKey))
100  mapWatchKeys.erase(pubKey.GetID());
101  return true;
102 }
103 
104 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
105 {
106  LOCK(cs_KeyStore);
107  return setWatchOnly.count(dest) > 0;
108 }
109 
111 {
112  LOCK(cs_KeyStore);
113  return (!setWatchOnly.empty());
114 }
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:148
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
Definition: keystore.cpp:17
bool HaveCScript(const CScriptID &hash) const override
Definition: keystore.cpp:50
bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
Definition: keystore.cpp:56
bool AddCScript(const CScript &redeemScript) override
Support for BIP 0013 : see https://github.com/raven/bips/blob/master/bip-0013.mediawiki.
Definition: keystore.cpp:40
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:143
opcodetype
Script opcodes.
Definition: script.h:51
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
Definition: pubkey.cpp:204
#define LOCK(cs)
Definition: sync.h:176
An encapsulated public key.
Definition: pubkey.h:40
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override
Add a key to the store.
Definition: keystore.cpp:33
virtual bool GetKey(const CKeyID &address, CKey &keyOut) const =0
virtual bool AddKey(const CKey &key)
Definition: keystore.cpp:13
bool RemoveWatchOnly(const CScript &dest) override
Definition: keystore.cpp:94
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:396
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:30
bool error(const char *fmt, const Args &... args)
Definition: util.h:168
iterator begin()
Definition: prevector.h:291
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:23
size_type size() const
Definition: prevector.h:283
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
Definition: script.h:503
An encapsulated private key.
Definition: key.h:36
bool HaveWatchOnly() const override
Definition: keystore.cpp:110
bool AddWatchOnly(const CScript &dest) override
Support for Watch-only addresses.
Definition: keystore.cpp:84