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>
7 #include <script/signingprovider.h>
8 #include <script/standard.h>
9 
10 #include <util/system.h>
11 
12 const SigningProvider& DUMMY_SIGNING_PROVIDER = SigningProvider();
13 
14 template<typename M, typename K, typename V>
LookupHelper(const M & map,const K & key,V & value)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 
GetCScript(const CScriptID & scriptid,CScript & script) const25 bool HidingSigningProvider::GetCScript(const CScriptID& scriptid, CScript& script) const
26 {
27     return m_provider->GetCScript(scriptid, script);
28 }
29 
GetPubKey(const CKeyID & keyid,CPubKey & pubkey) const30 bool HidingSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const
31 {
32     return m_provider->GetPubKey(keyid, pubkey);
33 }
34 
GetKey(const CKeyID & keyid,CKey & key) const35 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 
GetKeyOrigin(const CKeyID & keyid,KeyOriginInfo & info) const41 bool HidingSigningProvider::GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const
42 {
43     if (m_hide_origin) return false;
44     return m_provider->GetKeyOrigin(keyid, info);
45 }
46 
GetTaprootSpendData(const XOnlyPubKey & output_key,TaprootSpendData & spenddata) const47 bool HidingSigningProvider::GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const
48 {
49     return m_provider->GetTaprootSpendData(output_key, spenddata);
50 }
51 
GetCScript(const CScriptID & scriptid,CScript & script) const52 bool FlatSigningProvider::GetCScript(const CScriptID& scriptid, CScript& script) const { return LookupHelper(scripts, scriptid, script); }
GetPubKey(const CKeyID & keyid,CPubKey & pubkey) const53 bool FlatSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const { return LookupHelper(pubkeys, keyid, pubkey); }
GetKeyOrigin(const CKeyID & keyid,KeyOriginInfo & info) const54 bool FlatSigningProvider::GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const
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 }
GetKey(const CKeyID & keyid,CKey & key) const61 bool FlatSigningProvider::GetKey(const CKeyID& keyid, CKey& key) const { return LookupHelper(keys, keyid, key); }
GetTaprootSpendData(const XOnlyPubKey & output_key,TaprootSpendData & spenddata) const62 bool FlatSigningProvider::GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const
63 {
64     return LookupHelper(tr_spenddata, output_key, spenddata);
65 }
66 
Merge(const FlatSigningProvider & a,const FlatSigningProvider & b)67 FlatSigningProvider Merge(const FlatSigningProvider& a, const FlatSigningProvider& b)
68 {
69     FlatSigningProvider ret;
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 
ImplicitlyLearnRelatedKeyScripts(const CPubKey & pubkey)85 void FillableSigningProvider::ImplicitlyLearnRelatedKeyScripts(const CPubKey& pubkey)
86 {
87     AssertLockHeld(cs_KeyStore);
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()) {
101         CScript script = GetScriptForDestination(WitnessV0KeyHash(key_id));
102         // This does not use AddCScript, as it may be overridden.
103         CScriptID id(script);
104         mapScripts[id] = std::move(script);
105     }
106 }
107 
GetPubKey(const CKeyID & address,CPubKey & vchPubKeyOut) const108 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 
AddKeyPubKey(const CKey & key,const CPubKey & pubkey)118 bool FillableSigningProvider::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
119 {
120     LOCK(cs_KeyStore);
121     mapKeys[pubkey.GetID()] = key;
122     ImplicitlyLearnRelatedKeyScripts(pubkey);
123     return true;
124 }
125 
HaveKey(const CKeyID & address) const126 bool FillableSigningProvider::HaveKey(const CKeyID &address) const
127 {
128     LOCK(cs_KeyStore);
129     return mapKeys.count(address) > 0;
130 }
131 
GetKeys() const132 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 
GetKey(const CKeyID & address,CKey & keyOut) const142 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 
AddCScript(const CScript & redeemScript)153 bool FillableSigningProvider::AddCScript(const CScript& redeemScript)
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 
HaveCScript(const CScriptID & hash) const163 bool FillableSigningProvider::HaveCScript(const CScriptID& hash) const
164 {
165     LOCK(cs_KeyStore);
166     return mapScripts.count(hash) > 0;
167 }
168 
GetCScripts() const169 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 
GetCScript(const CScriptID & hash,CScript & redeemScriptOut) const179 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 
GetKeyForDestination(const SigningProvider & store,const CTxDestination & dest)191 CKeyID GetKeyForDestination(const SigningProvider& store, const CTxDestination& dest)
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 }
213