Bitcoin Core  24.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-2022 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 }
52 {
53  return m_provider->GetTaprootBuilder(output_key, builder);
54 }
55 
56 bool FlatSigningProvider::GetCScript(const CScriptID& scriptid, CScript& script) const { return LookupHelper(scripts, scriptid, script); }
57 bool FlatSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const { return LookupHelper(pubkeys, keyid, pubkey); }
59 {
60  std::pair<CPubKey, KeyOriginInfo> out;
61  bool ret = LookupHelper(origins, keyid, out);
62  if (ret) info = std::move(out.second);
63  return ret;
64 }
65 bool FlatSigningProvider::GetKey(const CKeyID& keyid, CKey& key) const { return LookupHelper(keys, keyid, key); }
67 {
68  TaprootBuilder builder;
69  if (LookupHelper(tr_trees, output_key, builder)) {
70  spenddata = builder.GetSpendData();
71  return true;
72  }
73  return false;
74 }
75 bool FlatSigningProvider::GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const
76 {
77  return LookupHelper(tr_trees, output_key, builder);
78 }
79 
81 {
82  scripts.merge(b.scripts);
83  pubkeys.merge(b.pubkeys);
84  keys.merge(b.keys);
85  origins.merge(b.origins);
86  tr_trees.merge(b.tr_trees);
87  return *this;
88 }
89 
91 {
93  CKeyID key_id = pubkey.GetID();
94  // This adds the redeemscripts necessary to detect P2WPKH and P2SH-P2WPKH
95  // outputs. Technically P2WPKH outputs don't have a redeemscript to be
96  // spent. However, our current IsMine logic requires the corresponding
97  // P2SH-P2WPKH redeemscript to be present in the wallet in order to accept
98  // payment even to P2WPKH outputs.
99  // Also note that having superfluous scripts in the keystore never hurts.
100  // They're only used to guide recursion in signing and IsMine logic - if
101  // a script is present but we can't do anything with it, it has no effect.
102  // "Implicitly" refers to fact that scripts are derived automatically from
103  // existing keys, and are present in memory, even without being explicitly
104  // loaded (e.g. from a file).
105  if (pubkey.IsCompressed()) {
107  // This does not use AddCScript, as it may be overridden.
108  CScriptID id(script);
109  mapScripts[id] = std::move(script);
110  }
111 }
112 
113 bool FillableSigningProvider::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
114 {
115  CKey key;
116  if (!GetKey(address, key)) {
117  return false;
118  }
119  vchPubKeyOut = key.GetPubKey();
120  return true;
121 }
122 
123 bool FillableSigningProvider::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
124 {
125  LOCK(cs_KeyStore);
126  mapKeys[pubkey.GetID()] = key;
128  return true;
129 }
130 
131 bool FillableSigningProvider::HaveKey(const CKeyID &address) const
132 {
133  LOCK(cs_KeyStore);
134  return mapKeys.count(address) > 0;
135 }
136 
137 std::set<CKeyID> FillableSigningProvider::GetKeys() const
138 {
139  LOCK(cs_KeyStore);
140  std::set<CKeyID> set_address;
141  for (const auto& mi : mapKeys) {
142  set_address.insert(mi.first);
143  }
144  return set_address;
145 }
146 
147 bool FillableSigningProvider::GetKey(const CKeyID &address, CKey &keyOut) const
148 {
149  LOCK(cs_KeyStore);
150  KeyMap::const_iterator mi = mapKeys.find(address);
151  if (mi != mapKeys.end()) {
152  keyOut = mi->second;
153  return true;
154  }
155  return false;
156 }
157 
159 {
160  if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
161  return error("FillableSigningProvider::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
162 
163  LOCK(cs_KeyStore);
164  mapScripts[CScriptID(redeemScript)] = redeemScript;
165  return true;
166 }
167 
169 {
170  LOCK(cs_KeyStore);
171  return mapScripts.count(hash) > 0;
172 }
173 
174 std::set<CScriptID> FillableSigningProvider::GetCScripts() const
175 {
176  LOCK(cs_KeyStore);
177  std::set<CScriptID> set_script;
178  for (const auto& mi : mapScripts) {
179  set_script.insert(mi.first);
180  }
181  return set_script;
182 }
183 
184 bool FillableSigningProvider::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
185 {
186  LOCK(cs_KeyStore);
187  ScriptMap::const_iterator mi = mapScripts.find(hash);
188  if (mi != mapScripts.end())
189  {
190  redeemScriptOut = (*mi).second;
191  return true;
192  }
193  return false;
194 }
195 
197 {
198  // Only supports destinations which map to single public keys:
199  // P2PKH, P2WPKH, P2SH-P2WPKH, P2TR
200  if (auto id = std::get_if<PKHash>(&dest)) {
201  return ToKeyID(*id);
202  }
203  if (auto witness_id = std::get_if<WitnessV0KeyHash>(&dest)) {
204  return ToKeyID(*witness_id);
205  }
206  if (auto script_hash = std::get_if<ScriptHash>(&dest)) {
207  CScript script;
208  CScriptID script_id(*script_hash);
209  CTxDestination inner_dest;
210  if (store.GetCScript(script_id, script) && ExtractDestination(script, inner_dest)) {
211  if (auto inner_witness_id = std::get_if<WitnessV0KeyHash>(&inner_dest)) {
212  return ToKeyID(*inner_witness_id);
213  }
214  }
215  }
216  if (auto output_key = std::get_if<WitnessV1Taproot>(&dest)) {
217  TaprootSpendData spenddata;
218  CPubKey pub;
219  if (store.GetTaprootSpendData(*output_key, spenddata)
220  && !spenddata.internal_key.IsNull()
221  && spenddata.merkle_root.IsNull()
222  && store.GetPubKeyByXOnly(spenddata.internal_key, pub)) {
223  return pub.GetID();
224  }
225  }
226  return CKeyID();
227 }
int ret
An encapsulated private key.
Definition: key.h:27
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:24
An encapsulated public key.
Definition: pubkey.h:34
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:198
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:164
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:27
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
virtual bool GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const override
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
virtual bool GetKey(const CKeyID &address, CKey &keyOut) const override
virtual bool AddCScript(const CScript &redeemScript)
void ImplicitlyLearnRelatedKeyScripts(const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
virtual std::set< CKeyID > GetKeys() const
virtual std::set< CScriptID > GetCScripts() const
virtual bool HaveCScript(const CScriptID &hash) const override
RecursiveMutex cs_KeyStore
virtual bool HaveKey(const CKeyID &address) const override
bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const override
bool GetKey(const CKeyID &keyid, CKey &key) const override
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
const SigningProvider * m_provider
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
bool GetTaprootBuilder(const XOnlyPubKey &output_key, TaprootBuilder &builder) const override
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
An interface to be implemented by keystores that support signing.
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
virtual bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
virtual bool GetTaprootBuilder(const XOnlyPubKey &output_key, TaprootBuilder &builder) const
virtual bool GetKey(const CKeyID &address, CKey &key) const
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
bool GetPubKeyByXOnly(const XOnlyPubKey &pubkey, CPubKey &out) const
Utility class to construct Taproot outputs from internal key and script tree.
Definition: standard.h:227
TaprootSpendData GetSpendData() const
Compute spending data (after Finalize()).
Definition: standard.cpp:477
bool IsNull() const
Test whether this is the 0 key (the result of default construction).
Definition: pubkey.h:243
bool IsNull() const
Definition: uint256.h:34
size_type size() const
Definition: prevector.h:284
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:24
const SigningProvider & DUMMY_SIGNING_PROVIDER
bool LookupHelper(const M &map, const K &key, V &value)
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 ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:237
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:334
CKeyID ToKeyID(const PKHash &key_hash)
Definition: standard.cpp:31
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:149
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
FlatSigningProvider & Merge(FlatSigningProvider &&b) LIFETIMEBOUND
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > origins
bool GetTaprootBuilder(const XOnlyPubKey &output_key, TaprootBuilder &builder) const override
bool GetKey(const CKeyID &keyid, CKey &key) const override
std::map< CKeyID, CPubKey > pubkeys
std::map< CKeyID, CKey > keys
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
std::map< CScriptID, CScript > scripts
std::map< XOnlyPubKey, TaprootBuilder > tr_trees
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
Map from output key to Taproot tree (which can then make the TaprootSpendData.
bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const override
uint256 merkle_root
The Merkle root of the script tree (0 if no scripts).
Definition: standard.h:213
XOnlyPubKey internal_key
The BIP341 internal key.
Definition: standard.h:211
#define LOCK(cs)
Definition: sync.h:258
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
AssertLockHeld(pool.cs)