Bitcoin Core  22.99.0
P2P Digital Currency
signingprovider.h
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 #ifndef BITCOIN_SCRIPT_SIGNINGPROVIDER_H
7 #define BITCOIN_SCRIPT_SIGNINGPROVIDER_H
8 
9 #include <key.h>
10 #include <pubkey.h>
11 #include <script/script.h>
12 #include <script/standard.h>
13 #include <sync.h>
14 
15 struct KeyOriginInfo;
16 
19 {
20 public:
21  virtual ~SigningProvider() {}
22  virtual bool GetCScript(const CScriptID &scriptid, CScript& script) const { return false; }
23  virtual bool HaveCScript(const CScriptID &scriptid) const { return false; }
24  virtual bool GetPubKey(const CKeyID &address, CPubKey& pubkey) const { return false; }
25  virtual bool GetKey(const CKeyID &address, CKey& key) const { return false; }
26  virtual bool HaveKey(const CKeyID &address) const { return false; }
27  virtual bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const { return false; }
28  virtual bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const { return false; }
29 
30  bool GetKeyByXOnly(const XOnlyPubKey& pubkey, CKey& key) const
31  {
32  for (const auto& id : pubkey.GetKeyIDs()) {
33  if (GetKey(id, key)) return true;
34  }
35  return false;
36  }
37 
38  bool GetPubKeyByXOnly(const XOnlyPubKey& pubkey, CPubKey& out) const
39  {
40  for (const auto& id : pubkey.GetKeyIDs()) {
41  if (GetPubKey(id, out)) return true;
42  }
43  return false;
44  }
45 
46  bool GetKeyOriginByXOnly(const XOnlyPubKey& pubkey, KeyOriginInfo& info) const
47  {
48  for (const auto& id : pubkey.GetKeyIDs()) {
49  if (GetKeyOrigin(id, info)) return true;
50  }
51  return false;
52  }
53 };
54 
56 
58 {
59 private:
60  const bool m_hide_secret;
61  const bool m_hide_origin;
63 
64 public:
65  HidingSigningProvider(const SigningProvider* provider, bool hide_secret, bool hide_origin) : m_hide_secret(hide_secret), m_hide_origin(hide_origin), m_provider(provider) {}
66  bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
67  bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
68  bool GetKey(const CKeyID& keyid, CKey& key) const override;
69  bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
70  bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const override;
71 };
72 
73 struct FlatSigningProvider final : public SigningProvider
74 {
75  std::map<CScriptID, CScript> scripts;
76  std::map<CKeyID, CPubKey> pubkeys;
77  std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> origins;
78  std::map<CKeyID, CKey> keys;
79  std::map<XOnlyPubKey, TaprootSpendData> tr_spenddata;
81  bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
82  bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
83  bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override;
84  bool GetKey(const CKeyID& keyid, CKey& key) const override;
85  bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const override;
86 };
87 
89 
92 {
93 protected:
94  using KeyMap = std::map<CKeyID, CKey>;
95  using ScriptMap = std::map<CScriptID, CScript>;
96 
102  KeyMap mapKeys GUARDED_BY(cs_KeyStore);
103 
144  ScriptMap mapScripts GUARDED_BY(cs_KeyStore);
145 
147 
148 public:
150 
151  virtual bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
152  virtual bool AddKey(const CKey &key) { return AddKeyPubKey(key, key.GetPubKey()); }
153  virtual bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
154  virtual bool HaveKey(const CKeyID &address) const override;
155  virtual std::set<CKeyID> GetKeys() const;
156  virtual bool GetKey(const CKeyID &address, CKey &keyOut) const override;
157  virtual bool AddCScript(const CScript& redeemScript);
158  virtual bool HaveCScript(const CScriptID &hash) const override;
159  virtual std::set<CScriptID> GetCScripts() const;
160  virtual bool GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const override;
161 };
162 
164 CKeyID GetKeyForDestination(const SigningProvider& store, const CTxDestination& dest);
165 
166 #endif // BITCOIN_SCRIPT_SIGNINGPROVIDER_H
SigningProvider::GetKeyByXOnly
bool GetKeyByXOnly(const XOnlyPubKey &pubkey, CKey &key) const
Definition: signingprovider.h:30
FillableSigningProvider::KeyMap
std::map< CKeyID, CKey > KeyMap
Definition: signingprovider.h:94
FillableSigningProvider::ScriptMap
std::map< CScriptID, CScript > ScriptMap
Definition: signingprovider.h:95
SigningProvider::~SigningProvider
virtual ~SigningProvider()
Definition: signingprovider.h:21
FillableSigningProvider::GetKeys
virtual std::set< CKeyID > GetKeys() const
Definition: signingprovider.cpp:132
HidingSigningProvider::m_provider
const SigningProvider * m_provider
Definition: signingprovider.h:62
SigningProvider::HaveCScript
virtual bool HaveCScript(const CScriptID &scriptid) const
Definition: signingprovider.h:23
sync.h
FillableSigningProvider
Fillable signing provider that keeps keys in an address->secret map.
Definition: signingprovider.h:91
FlatSigningProvider::keys
std::map< CKeyID, CKey > keys
Definition: signingprovider.h:78
SigningProvider
An interface to be implemented by keystores that support signing.
Definition: signingprovider.h:18
HidingSigningProvider::HidingSigningProvider
HidingSigningProvider(const SigningProvider *provider, bool hide_secret, bool hide_origin)
Definition: signingprovider.h:65
SigningProvider::GetKeyOriginByXOnly
bool GetKeyOriginByXOnly(const XOnlyPubKey &pubkey, KeyOriginInfo &info) const
Definition: signingprovider.h:46
XOnlyPubKey
Definition: pubkey.h:220
AnnotatedMixin< std::recursive_mutex >
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
pubkey.h
SigningProvider::GetPubKey
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
Definition: signingprovider.h:24
FillableSigningProvider::AddKey
virtual bool AddKey(const CKey &key)
Definition: signingprovider.h:152
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
FillableSigningProvider::GetCScripts
virtual std::set< CScriptID > GetCScripts() const
Definition: signingprovider.cpp:169
FlatSigningProvider::scripts
std::map< CScriptID, CScript > scripts
Definition: signingprovider.h:75
SigningProvider::HaveKey
virtual bool HaveKey(const CKeyID &address) const
Definition: signingprovider.h:26
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:77
HidingSigningProvider::m_hide_secret
const bool m_hide_secret
Definition: signingprovider.h:60
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
Merge
FlatSigningProvider Merge(const FlatSigningProvider &a, const FlatSigningProvider &b)
Definition: signingprovider.cpp:67
DUMMY_SIGNING_PROVIDER
const SigningProvider & DUMMY_SIGNING_PROVIDER
Definition: signingprovider.cpp:12
standard.h
FillableSigningProvider::HaveCScript
virtual bool HaveCScript(const CScriptID &hash) const override
Definition: signingprovider.cpp:163
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:76
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:405
script.h
key.h
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:26
FillableSigningProvider::GetKey
virtual bool GetKey(const CKeyID &address, CKey &keyOut) const override
Definition: signingprovider.cpp:142
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
HidingSigningProvider::GetPubKey
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
Definition: signingprovider.cpp:30
FillableSigningProvider::AddKeyPubKey
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
Definition: signingprovider.cpp:118
XOnlyPubKey::GetKeyIDs
std::vector< CKeyID > GetKeyIDs() const
Returns a list of CKeyIDs for the CPubKeys that could have been used to create this XOnlyPubKey.
Definition: pubkey.cpp:183
HidingSigningProvider::m_hide_origin
const bool m_hide_origin
Definition: signingprovider.h:61
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
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
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:79
FillableSigningProvider::GUARDED_BY
KeyMap mapKeys GUARDED_BY(cs_KeyStore)
Map of key id to unencrypted private keys known by the signing provider.
FillableSigningProvider::HaveKey
virtual bool HaveKey(const CKeyID &address) const override
Definition: signingprovider.cpp:126
FillableSigningProvider::cs_KeyStore
RecursiveMutex cs_KeyStore
Definition: signingprovider.h:149
HidingSigningProvider
Definition: signingprovider.h:57
FlatSigningProvider::GetPubKey
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
Definition: signingprovider.cpp:53
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
SigningProvider::GetPubKeyByXOnly
bool GetPubKeyByXOnly(const XOnlyPubKey &pubkey, CPubKey &out) const
Definition: signingprovider.h:38
KeyOriginInfo
Definition: keyorigin.h:11
FlatSigningProvider
Definition: signingprovider.h:73