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
15struct KeyOriginInfo;
16
19{
20public:
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{
59private:
60 const bool m_hide_secret;
61 const bool m_hide_origin;
63
64public:
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
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{
93protected:
94 using KeyMap = std::map<CKeyID, CKey>;
95 using ScriptMap = std::map<CScriptID, CScript>;
96
103
145
147
148public:
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
165
166#endif // BITCOIN_SCRIPT_SIGNINGPROVIDER_H
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
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
Fillable signing provider that keeps keys in an address->secret map.
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)
std::map< CScriptID, CScript > ScriptMap
KeyMap mapKeys GUARDED_BY(cs_KeyStore)
Map of key id to unencrypted private keys known by the signing provider.
ScriptMap mapScripts GUARDED_BY(cs_KeyStore)
Map of script id to scripts known by the signing provider.
void ImplicitlyLearnRelatedKeyScripts(const CPubKey &pubkey) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore)
virtual std::set< CKeyID > GetKeys() const
std::map< CKeyID, CKey > KeyMap
virtual std::set< CScriptID > GetCScripts() const
virtual bool HaveCScript(const CScriptID &hash) const override
virtual bool AddKey(const CKey &key)
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
HidingSigningProvider(const SigningProvider *provider, bool hide_secret, bool hide_origin)
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
An interface to be implemented by keystores that support signing.
virtual ~SigningProvider()
virtual bool HaveKey(const CKeyID &address) const
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
virtual bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const
virtual bool HaveCScript(const CScriptID &scriptid) const
bool GetKeyByXOnly(const XOnlyPubKey &pubkey, CKey &key) const
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
bool GetKeyOriginByXOnly(const XOnlyPubKey &pubkey, KeyOriginInfo &info) 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
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
const SigningProvider & DUMMY_SIGNING_PROVIDER
FlatSigningProvider Merge(const FlatSigningProvider &a, const FlatSigningProvider &b)
CKeyID GetKeyForDestination(const SigningProvider &store, const CTxDestination &dest)
Return the CKeyID of the key involved in a script (if there is a unique one).
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 EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49