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