Bitcoin Core  22.0.0
P2P Digital Currency
signingprovider.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 #include <script/standard.h>
9 
10 #include <util/system.h>
11 
13 
14 template<typename M, typename K, typename V>
15 bool LookupHelper(const M& map, const K& key, V& value)
16 {
17  auto it = map.find(key);
18  if (it != map.end()) {
19  value = it->second;
20  return true;
21  }
22  return false;
23 }
24 
25 bool HidingSigningProvider::GetCScript(const CScriptID& scriptid, CScript& script) const
26 {
27  return m_provider->GetCScript(scriptid, script);
28 }
29 
30 bool HidingSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const
31 {
32  return m_provider->GetPubKey(keyid, pubkey);
33 }
34 
35 bool HidingSigningProvider::GetKey(const CKeyID& keyid, CKey& key) const
36 {
37  if (m_hide_secret) return false;
38  return m_provider->GetKey(keyid, key);
39 }
40 
42 {
43  if (m_hide_origin) return false;
44  return m_provider->GetKeyOrigin(keyid, info);
45 }
46 
48 {
49  return m_provider->GetTaprootSpendData(output_key, spenddata);
50 }
51 
52 bool FlatSigningProvider::GetCScript(const CScriptID& scriptid, CScript& script) const { return LookupHelper(scripts, scriptid, script); }
53 bool FlatSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const { return LookupHelper(pubkeys, keyid, pubkey); }
55 {
56  std::pair<CPubKey, KeyOriginInfo> out;
57  bool ret = LookupHelper(origins, keyid, out);
58  if (ret) info = std::move(out.second);
59  return ret;
60 }
61 bool FlatSigningProvider::GetKey(const CKeyID& keyid, CKey& key) const { return LookupHelper(keys, keyid, key); }
63 {
64  return LookupHelper(tr_spenddata, output_key, spenddata);
65 }
66 
68 {
70  ret.scripts = a.scripts;
71  ret.scripts.insert(b.scripts.begin(), b.scripts.end());
72  ret.pubkeys = a.pubkeys;
73  ret.pubkeys.insert(b.pubkeys.begin(), b.pubkeys.end());
74  ret.keys = a.keys;
75  ret.keys.insert(b.keys.begin(), b.keys.end());
76  ret.origins = a.origins;
77  ret.origins.insert(b.origins.begin(), b.origins.end());
78  ret.tr_spenddata = a.tr_spenddata;
79  for (const auto& [output_key, spenddata] : b.tr_spenddata) {
80  ret.tr_spenddata[output_key].Merge(spenddata);
81  }
82  return ret;
83 }
84 
86 {
88  CKeyID key_id = pubkey.GetID();
89  // This adds the redeemscripts necessary to detect P2WPKH and P2SH-P2WPKH
90  // outputs. Technically P2WPKH outputs don't have a redeemscript to be
91  // spent. However, our current IsMine logic requires the corresponding
92  // P2SH-P2WPKH redeemscript to be present in the wallet in order to accept
93  // payment even to P2WPKH outputs.
94  // Also note that having superfluous scripts in the keystore never hurts.
95  // They're only used to guide recursion in signing and IsMine logic - if
96  // a script is present but we can't do anything with it, it has no effect.
97  // "Implicitly" refers to fact that scripts are derived automatically from
98  // existing keys, and are present in memory, even without being explicitly
99  // loaded (e.g. from a file).
100  if (pubkey.IsCompressed()) {
102  // This does not use AddCScript, as it may be overridden.
103  CScriptID id(script);
104  mapScripts[id] = std::move(script);
105  }
106 }
107 
108 bool FillableSigningProvider::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
109 {
110  CKey key;
111  if (!GetKey(address, key)) {
112  return false;
113  }
114  vchPubKeyOut = key.GetPubKey();
115  return true;
116 }
117 
118 bool FillableSigningProvider::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
119 {
120  LOCK(cs_KeyStore);
121  mapKeys[pubkey.GetID()] = key;
123  return true;
124 }
125 
126 bool FillableSigningProvider::HaveKey(const CKeyID &address) const
127 {
128  LOCK(cs_KeyStore);
129  return mapKeys.count(address) > 0;
130 }
131 
132 std::set<CKeyID> FillableSigningProvider::GetKeys() const
133 {
134  LOCK(cs_KeyStore);
135  std::set<CKeyID> set_address;
136  for (const auto& mi : mapKeys) {
137  set_address.insert(mi.first);
138  }
139  return set_address;
140 }
141 
142 bool FillableSigningProvider::GetKey(const CKeyID &address, CKey &keyOut) const
143 {
144  LOCK(cs_KeyStore);
145  KeyMap::const_iterator mi = mapKeys.find(address);
146  if (mi != mapKeys.end()) {
147  keyOut = mi->second;
148  return true;
149  }
150  return false;
151 }
152 
154 {
155  if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
156  return error("FillableSigningProvider::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
157 
158  LOCK(cs_KeyStore);
159  mapScripts[CScriptID(redeemScript)] = redeemScript;
160  return true;
161 }
162 
164 {
165  LOCK(cs_KeyStore);
166  return mapScripts.count(hash) > 0;
167 }
168 
169 std::set<CScriptID> FillableSigningProvider::GetCScripts() const
170 {
171  LOCK(cs_KeyStore);
172  std::set<CScriptID> set_script;
173  for (const auto& mi : mapScripts) {
174  set_script.insert(mi.first);
175  }
176  return set_script;
177 }
178 
179 bool FillableSigningProvider::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
180 {
181  LOCK(cs_KeyStore);
182  ScriptMap::const_iterator mi = mapScripts.find(hash);
183  if (mi != mapScripts.end())
184  {
185  redeemScriptOut = (*mi).second;
186  return true;
187  }
188  return false;
189 }
190 
192 {
193  // Only supports destinations which map to single public keys, i.e. P2PKH,
194  // P2WPKH, and P2SH-P2WPKH.
195  if (auto id = std::get_if<PKHash>(&dest)) {
196  return ToKeyID(*id);
197  }
198  if (auto witness_id = std::get_if<WitnessV0KeyHash>(&dest)) {
199  return ToKeyID(*witness_id);
200  }
201  if (auto script_hash = std::get_if<ScriptHash>(&dest)) {
202  CScript script;
203  CScriptID script_id(*script_hash);
204  CTxDestination inner_dest;
205  if (store.GetCScript(script_id, script) && ExtractDestination(script, inner_dest)) {
206  if (auto inner_witness_id = std::get_if<WitnessV0KeyHash>(&inner_dest)) {
207  return ToKeyID(*inner_witness_id);
208  }
209  }
210  }
211  return CKeyID();
212 }
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
AssertLockHeld(pool.cs)
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:213
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
const SigningProvider * m_provider
RecursiveMutex cs_KeyStore
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
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).
bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const override
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:160
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
virtual bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const
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:232
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
const SigningProvider & DUMMY_SIGNING_PROVIDER
An encapsulated public key.
Definition: pubkey.h:32
bool GetKey(const CKeyID &keyid, CKey &key) const override
std::map< CKeyID, CPubKey > pubkeys
void ImplicitlyLearnRelatedKeyScripts(const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:157
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:351
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
Map from output key to spend data.
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:404
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
std::map< XOnlyPubKey, TaprootSpendData > tr_spenddata
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:25
size_type size() const
Definition: prevector.h:282
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
An encapsulated private key.
Definition: key.h:27
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:49
bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const override
CKeyID ToKeyID(const PKHash &key_hash)
Definition: standard.cpp:34
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:194
virtual bool HaveKey(const CKeyID &address) const override