Fabcoin Core  0.16.2
P2P Digital Currency
keystore.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 bool CKeyStore::AddKey(const CKey &key) {
13  return AddKeyPubKey(key, key.GetPubKey());
14 }
15 
16 bool CBasicKeyStore::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
17 {
18  CKey key;
19  if (!GetKey(address, key)) {
21  WatchKeyMap::const_iterator it = mapWatchKeys.find(address);
22  if (it != mapWatchKeys.end()) {
23  vchPubKeyOut = it->second;
24  return true;
25  }
26  return false;
27  }
28  vchPubKeyOut = key.GetPubKey();
29  return true;
30 }
31 
32 bool CBasicKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
33 {
35  mapKeys[pubkey.GetID()] = key;
36  return true;
37 }
38 
39 bool CBasicKeyStore::AddCScript(const CScript& redeemScript)
40 {
41  if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
42  return error("CBasicKeyStore::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
43 
45  mapScripts[CScriptID(redeemScript)] = redeemScript;
46  return true;
47 }
48 
49 bool CBasicKeyStore::HaveCScript(const CScriptID& hash) const
50 {
52  return mapScripts.count(hash) > 0;
53 }
54 
55 bool CBasicKeyStore::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
56 {
58  ScriptMap::const_iterator mi = mapScripts.find(hash);
59  if (mi != mapScripts.end())
60  {
61  redeemScriptOut = (*mi).second;
62  return true;
63  }
64  return false;
65 }
66 
67 static bool ExtractPubKey(const CScript &dest, CPubKey& pubKeyOut)
68 {
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;
81 }
82 
84 {
86  setWatchOnly.insert(dest);
87  CPubKey pubKey;
88  if (ExtractPubKey(dest, pubKey))
89  mapWatchKeys[pubKey.GetID()] = pubKey;
90  return true;
91 }
92 
94 {
96  setWatchOnly.erase(dest);
97  CPubKey pubKey;
98  if (ExtractPubKey(dest, pubKey))
99  mapWatchKeys.erase(pubKey.GetID());
100  return true;
101 }
102 
103 bool CBasicKeyStore::HaveWatchOnly(const CScript &dest) const
104 {
105  LOCK(cs_KeyStore);
106  return setWatchOnly.count(dest) > 0;
107 }
108 
110 {
111  LOCK(cs_KeyStore);
112  return (!setWatchOnly.empty());
113 }
bool error(const char *fmt, const Args &...args)
Definition: util.h:178
CCriticalSection cs_KeyStore
Definition: keystore.h:21
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)=0
Add a key to the store.
bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
Definition: keystore.cpp:16
virtual bool HaveCScript(const CScriptID &hash) const override
Definition: keystore.cpp:49
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
Definition: keystore.cpp:55
virtual bool AddCScript(const CScript &redeemScript) override
Support for BIP 0013 : see https://github.com/fabcoin/bips/blob/master/bip-0013.mediawiki.
Definition: keystore.cpp:39
size_type size() const
Definition: prevector.h:282
opcodetype
Script opcodes.
Definition: script.h:48
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:147
#define LOCK(cs)
Definition: sync.h:175
An encapsulated public key.
Definition: pubkey.h:39
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey) override
Add a key to the store.
Definition: keystore.cpp:32
virtual bool GetKey(const CKeyID &address, CKey &keyOut) const =0
virtual bool AddKey(const CKey &key)
Definition: keystore.cpp:12
virtual bool RemoveWatchOnly(const CScript &dest) override
Definition: keystore.cpp:93
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:417
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
Definition: pubkey.cpp:204
iterator begin()
Definition: prevector.h:290
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:28
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
Definition: script.h:523
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
An encapsulated private key.
Definition: key.h:35
virtual bool HaveWatchOnly() const override
Definition: keystore.cpp:109
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:146
virtual bool AddWatchOnly(const CScript &dest) override
Support for Watch-only addresses.
Definition: keystore.cpp:83