Bitcoin Core  22.99.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 }
LookupHelper
bool LookupHelper(const M &map, const K &key, V &value)
Definition: signingprovider.cpp:15
FillableSigningProvider::GetKeys
virtual std::set< CKeyID > GetKeys() const
Definition: signingprovider.cpp:132
HidingSigningProvider::m_provider
const SigningProvider * m_provider
Definition: signingprovider.h:38
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:351
FlatSigningProvider::keys
std::map< CKeyID, CKey > keys
Definition: signingprovider.h:54
SigningProvider
An interface to be implemented by keystores that support signing.
Definition: signingprovider.h:18
XOnlyPubKey
Definition: pubkey.h:220
Merge
FlatSigningProvider Merge(const FlatSigningProvider &a, const FlatSigningProvider &b)
Definition: signingprovider.cpp:67
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
DUMMY_SIGNING_PROVIDER
const SigningProvider & DUMMY_SIGNING_PROVIDER
Definition: signingprovider.cpp:12
SigningProvider::GetPubKey
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
Definition: signingprovider.h:24
AssertLockHeld
AssertLockHeld(pool.cs)
FillableSigningProvider::ImplicitlyLearnRelatedKeyScripts
void ImplicitlyLearnRelatedKeyScripts(const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
Definition: signingprovider.cpp:85
FlatSigningProvider::GetKeyOrigin
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
Definition: signingprovider.cpp:54
WitnessV0KeyHash
Definition: standard.h:109
FillableSigningProvider::GetCScripts
virtual std::set< CScriptID > GetCScripts() const
Definition: signingprovider.cpp:169
FlatSigningProvider::scripts
std::map< CScriptID, CScript > scripts
Definition: signingprovider.h:51
signingprovider.h
FlatSigningProvider::GetTaprootSpendData
bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const override
Definition: signingprovider.cpp:62
CTxDestination
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:157
HidingSigningProvider::GetKeyOrigin
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
Definition: signingprovider.cpp:41
FlatSigningProvider::origins
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > origins
Definition: signingprovider.h:53
HidingSigningProvider::m_hide_secret
const bool m_hide_secret
Definition: signingprovider.h:36
FillableSigningProvider::GetPubKey
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
Definition: signingprovider.cpp:108
SigningProvider::GetKeyOrigin
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
Definition: signingprovider.h:27
TaprootSpendData
Definition: standard.h:223
standard.h
FillableSigningProvider::HaveCScript
virtual bool HaveCScript(const CScriptID &hash) const override
Definition: signingprovider.cpp:163
GetKeyForDestination
CKeyID GetKeyForDestination(const SigningProvider &store, const CTxDestination &dest)
Return the CKeyID of the key involved in a script (if there is a unique one).
Definition: signingprovider.cpp:191
SigningProvider::GetTaprootSpendData
virtual bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const
Definition: signingprovider.h:28
SigningProvider::GetCScript
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
Definition: signingprovider.h:22
FlatSigningProvider::pubkeys
std::map< CKeyID, CPubKey > pubkeys
Definition: signingprovider.h:52
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
ExtractDestination
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:213
CPubKey::IsCompressed
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:194
system.h
ToKeyID
CKeyID ToKeyID(const PKHash &key_hash)
Definition: standard.cpp:34
FlatSigningProvider::GetKey
bool GetKey(const CKeyID &keyid, CKey &key) const override
Definition: signingprovider.cpp:61
CPubKey
An encapsulated public key.
Definition: pubkey.h:32
HidingSigningProvider::GetTaprootSpendData
bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const override
Definition: signingprovider.cpp:47
CKey
An encapsulated private key.
Definition: key.h:27
FillableSigningProvider::GetKey
virtual bool GetKey(const CKeyID &address, CKey &keyOut) const override
Definition: signingprovider.cpp:142
LOCK
#define LOCK(cs)
Definition: sync.h:232
HidingSigningProvider::GetPubKey
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
Definition: signingprovider.cpp:30
prevector::size
size_type size() const
Definition: prevector.h:282
FillableSigningProvider::AddKeyPubKey
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Definition: signingprovider.cpp:118
HidingSigningProvider::m_hide_origin
const bool m_hide_origin
Definition: signingprovider.h:37
SigningProvider::GetKey
virtual bool GetKey(const CKeyID &address, CKey &key) const
Definition: signingprovider.h:25
FillableSigningProvider::GetCScript
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
Definition: signingprovider.cpp:179
FillableSigningProvider::AddCScript
virtual bool AddCScript(const CScript &redeemScript)
Definition: signingprovider.cpp:153
MAX_SCRIPT_ELEMENT_SIZE
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:23
HidingSigningProvider::GetCScript
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
Definition: signingprovider.cpp:25
FlatSigningProvider::GetCScript
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
Map from output key to spend data.
Definition: signingprovider.cpp:52
FlatSigningProvider::tr_spenddata
std::map< XOnlyPubKey, TaprootSpendData > tr_spenddata
Definition: signingprovider.h:55
FillableSigningProvider::HaveKey
virtual bool HaveKey(const CKeyID &address) const override
Definition: signingprovider.cpp:126
FillableSigningProvider::cs_KeyStore
RecursiveMutex cs_KeyStore
Definition: signingprovider.h:125
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
FlatSigningProvider::GetPubKey
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
Definition: signingprovider.cpp:53
keyorigin.h
HidingSigningProvider::GetKey
bool GetKey(const CKeyID &keyid, CKey &key) const override
Definition: signingprovider.cpp:35
CScriptID
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:25
KeyOriginInfo
Definition: keyorigin.h:11
FlatSigningProvider
Definition: signingprovider.h:49
CPubKey::GetID
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:160