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
14template<typename M, typename K, typename V>
15bool 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
25bool HidingSigningProvider::GetCScript(const CScriptID& scriptid, CScript& script) const
26{
27 return m_provider->GetCScript(scriptid, script);
28}
29
30bool HidingSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const
31{
32 return m_provider->GetPubKey(keyid, pubkey);
33}
34
35bool 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
52bool FlatSigningProvider::GetCScript(const CScriptID& scriptid, CScript& script) const { return LookupHelper(scripts, scriptid, script); }
53bool 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}
61bool 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());
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
108bool 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
119{
121 mapKeys[pubkey.GetID()] = key;
123 return true;
124}
125
126bool FillableSigningProvider::HaveKey(const CKeyID &address) const
127{
129 return mapKeys.count(address) > 0;
130}
131
132std::set<CKeyID> FillableSigningProvider::GetKeys() const
133{
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
142bool FillableSigningProvider::GetKey(const CKeyID &address, CKey &keyOut) const
143{
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
159 mapScripts[CScriptID(redeemScript)] = redeemScript;
160 return true;
161}
162
164{
166 return mapScripts.count(hash) > 0;
167}
168
169std::set<CScriptID> FillableSigningProvider::GetCScripts() const
170{
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
179bool FillableSigningProvider::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
180{
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}
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:23
An encapsulated public key.
Definition: pubkey.h:33
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:194
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:160
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:26
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 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 GetKey(const CKeyID &address, CKey &key) const
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
size_type size() const
Definition: prevector.h:282
static NodeId id
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:24
const SigningProvider & DUMMY_SIGNING_PROVIDER
FlatSigningProvider Merge(const FlatSigningProvider &a, const FlatSigningProvider &b)
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:213
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:310
CKeyID ToKeyID(const PKHash &key_hash)
Definition: standard.cpp:34
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:157
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > origins
bool GetKey(const CKeyID &keyid, CKey &key) const override
std::map< XOnlyPubKey, TaprootSpendData > tr_spenddata
std::map< CKeyID, CPubKey > pubkeys
std::map< CKeyID, CKey > keys
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
std::map< CScriptID, CScript > scripts
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
Map from output key to spend data.
bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const override
#define LOCK(cs)
Definition: sync.h:226
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
AssertLockHeld(pool.cs)