Bitcoin ABC  0.22.13
P2P Digital Currency
signingprovider.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 <script/keyorigin.h>
8 
9 #include <util/system.h>
10 
12 
13 template <typename M, typename K, typename V>
14 bool LookupHelper(const M &map, const K &key, V &value) {
15  auto it = map.find(key);
16  if (it != map.end()) {
17  value = it->second;
18  return true;
19  }
20  return false;
21 }
22 
24  CScript &script) const {
25  return m_provider->GetCScript(scriptid, script);
26 }
27 
29  CPubKey &pubkey) const {
30  return m_provider->GetPubKey(keyid, pubkey);
31 }
32 
33 bool HidingSigningProvider::GetKey(const CKeyID &keyid, CKey &key) const {
34  if (m_hide_secret) {
35  return false;
36  }
37  return m_provider->GetKey(keyid, key);
38 }
39 
41  KeyOriginInfo &info) const {
42  if (m_hide_origin) {
43  return false;
44  }
45  return m_provider->GetKeyOrigin(keyid, info);
46 }
47 
49  CScript &script) const {
50  return LookupHelper(scripts, scriptid, script);
51 }
53  CPubKey &pubkey) const {
54  return LookupHelper(pubkeys, keyid, pubkey);
55 }
57  KeyOriginInfo &info) const {
58  std::pair<CPubKey, KeyOriginInfo> out;
59  bool ret = LookupHelper(origins, keyid, out);
60  if (ret) {
61  info = std::move(out.second);
62  }
63  return ret;
64 }
65 bool FlatSigningProvider::GetKey(const CKeyID &keyid, CKey &key) const {
66  return LookupHelper(keys, keyid, key);
67 }
68 
70  const FlatSigningProvider &b) {
72  ret.scripts = a.scripts;
73  ret.scripts.insert(b.scripts.begin(), b.scripts.end());
74  ret.pubkeys = a.pubkeys;
75  ret.pubkeys.insert(b.pubkeys.begin(), b.pubkeys.end());
76  ret.keys = a.keys;
77  ret.keys.insert(b.keys.begin(), b.keys.end());
78  ret.origins = a.origins;
79  ret.origins.insert(b.origins.begin(), b.origins.end());
80  return ret;
81 }
82 
84  CPubKey &vchPubKeyOut) const {
85  CKey key;
86  if (!GetKey(address, key)) {
87  return false;
88  }
89  vchPubKeyOut = key.GetPubKey();
90  return true;
91 }
92 
94  const CPubKey &pubkey) {
95  LOCK(cs_KeyStore);
96  mapKeys[pubkey.GetID()] = key;
97  return true;
98 }
99 
100 bool FillableSigningProvider::HaveKey(const CKeyID &address) const {
101  LOCK(cs_KeyStore);
102  return mapKeys.count(address) > 0;
103 }
104 
105 std::set<CKeyID> FillableSigningProvider::GetKeys() const {
106  LOCK(cs_KeyStore);
107  std::set<CKeyID> set_address;
108  for (const auto &mi : mapKeys) {
109  set_address.insert(mi.first);
110  }
111  return set_address;
112 }
113 
115  CKey &keyOut) const {
116  LOCK(cs_KeyStore);
117  KeyMap::const_iterator mi = mapKeys.find(address);
118  if (mi != mapKeys.end()) {
119  keyOut = mi->second;
120  return true;
121  }
122  return false;
123 }
124 
125 bool FillableSigningProvider::AddCScript(const CScript &redeemScript) {
126  if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE) {
127  return error(
128  "FillableSigningProvider::AddCScript(): redeemScripts > %i bytes "
129  "are invalid",
131  }
132 
133  LOCK(cs_KeyStore);
134  mapScripts[CScriptID(redeemScript)] = redeemScript;
135  return true;
136 }
137 
139  LOCK(cs_KeyStore);
140  return mapScripts.count(hash) > 0;
141 }
142 
143 std::set<CScriptID> FillableSigningProvider::GetCScripts() const {
144  LOCK(cs_KeyStore);
145  std::set<CScriptID> set_script;
146  for (const auto &mi : mapScripts) {
147  set_script.insert(mi.first);
148  }
149  return set_script;
150 }
151 
153  CScript &redeemScriptOut) const {
154  LOCK(cs_KeyStore);
155  ScriptMap::const_iterator mi = mapScripts.find(hash);
156  if (mi != mapScripts.end()) {
157  redeemScriptOut = (*mi).second;
158  return true;
159  }
160  return false;
161 }
162 
164  const CTxDestination &dest) {
165  // Only supports destinations which map to single public keys, i.e. P2PKH.
166  if (auto id = boost::get<PKHash>(&dest)) {
167  return CKeyID(*id);
168  }
169  return CKeyID();
170 }
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
const SigningProvider * m_provider
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:210
std::map< CKeyID, CKey > keys
FlatSigningProvider Merge(const FlatSigningProvider &a, const FlatSigningProvider &b)
virtual bool AddCScript(const CScript &redeemScript)
virtual std::set< CScriptID > GetCScripts() const
virtual std::set< CKeyID > GetKeys() const
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > origins
CKeyID GetKeyForDestination(const SigningProvider &store, const CTxDestination &dest)
Return the CKeyID of the key involved in a script (if there is a unique one).
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:23
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:137
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
std::map< CScriptID, CScript > scripts
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
#define LOCK(cs)
Definition: sync.h:230
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
const SigningProvider & DUMMY_SIGNING_PROVIDER
An encapsulated public key.
Definition: pubkey.h:31
bool GetKey(const CKeyID &keyid, CKey &key) const override
std::map< CKeyID, CPubKey > pubkeys
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
virtual bool GetKey(const CKeyID &address, CKey &key) const
virtual bool GetKey(const CKeyID &address, CKey &keyOut) const override
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
bool GetKey(const CKeyID &keyid, CKey &key) const override
An interface to be implemented by keystores that support signing.
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
virtual bool HaveCScript(const CScriptID &hash) const override
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:22
size_type size() const
Definition: prevector.h:384
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
An encapsulated secp256k1 private key.
Definition: key.h:25
bool LookupHelper(const M &map, const K &key, V &value)
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
bool error(const char *fmt, const Args &... args)
Definition: system.h:47
boost::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:87
virtual bool HaveKey(const CKeyID &address) const override