Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
signingprovider.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2019 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
9#include <logging.h>
10
11#include <variant>
12
14
15template <typename M, typename K, typename V>
16bool LookupHelper(const M &map, const K &key, V &value) {
17 auto it = map.find(key);
18 if (it != map.end()) {
19 value = it->second;
20 return true;
21 }
22 return false;
23}
24
26 CScript &script) const {
27 return m_provider->GetCScript(scriptid, script);
28}
29
31 CPubKey &pubkey) const {
32 return m_provider->GetPubKey(keyid, pubkey);
33}
34
36 if (m_hide_secret) {
37 return false;
38 }
39 return m_provider->GetKey(keyid, key);
40}
41
43 KeyOriginInfo &info) const {
44 if (m_hide_origin) {
45 return false;
46 }
47 return m_provider->GetKeyOrigin(keyid, info);
48}
49
51 CScript &script) const {
52 return LookupHelper(scripts, scriptid, script);
53}
55 CPubKey &pubkey) const {
56 return LookupHelper(pubkeys, keyid, pubkey);
57}
59 KeyOriginInfo &info) const {
60 std::pair<CPubKey, KeyOriginInfo> out;
61 bool ret = LookupHelper(origins, keyid, out);
62 if (ret) {
63 info = std::move(out.second);
64 }
65 return ret;
66}
67bool FlatSigningProvider::GetKey(const CKeyID &keyid, CKey &key) const {
68 return LookupHelper(keys, keyid, key);
69}
70
72 const FlatSigningProvider &b) {
74 ret.scripts = a.scripts;
75 ret.scripts.insert(b.scripts.begin(), b.scripts.end());
76 ret.pubkeys = a.pubkeys;
77 ret.pubkeys.insert(b.pubkeys.begin(), b.pubkeys.end());
78 ret.keys = a.keys;
79 ret.keys.insert(b.keys.begin(), b.keys.end());
80 ret.origins = a.origins;
81 ret.origins.insert(b.origins.begin(), b.origins.end());
82 return ret;
83}
84
86 CPubKey &vchPubKeyOut) const {
87 CKey key;
88 if (!GetKey(address, key)) {
89 return false;
90 }
91 vchPubKeyOut = key.GetPubKey();
92 return true;
93}
94
96 const CPubKey &pubkey) {
98 mapKeys[pubkey.GetID()] = key;
99 return true;
100}
101
102bool FillableSigningProvider::HaveKey(const CKeyID &address) const {
104 return mapKeys.count(address) > 0;
105}
106
107std::set<CKeyID> FillableSigningProvider::GetKeys() const {
109 std::set<CKeyID> set_address;
110 for (const auto &mi : mapKeys) {
111 set_address.insert(mi.first);
112 }
113 return set_address;
114}
115
117 CKey &keyOut) const {
119 KeyMap::const_iterator mi = mapKeys.find(address);
120 if (mi != mapKeys.end()) {
121 keyOut = mi->second;
122 return true;
123 }
124 return false;
125}
126
129 return error(
130 "FillableSigningProvider::AddCScript(): redeemScripts > %i bytes "
131 "are invalid",
133 }
134
137 return true;
138}
139
142 return mapScripts.count(hash) > 0;
143}
144
145std::set<CScriptID> FillableSigningProvider::GetCScripts() const {
147 std::set<CScriptID> set_script;
148 for (const auto &mi : mapScripts) {
149 set_script.insert(mi.first);
150 }
151 return set_script;
152}
153
155 CScript &redeemScriptOut) const {
157 ScriptMap::const_iterator mi = mapScripts.find(hash);
158 if (mi != mapScripts.end()) {
159 redeemScriptOut = (*mi).second;
160 return true;
161 }
162 return false;
163}
164
166 const CTxDestination &dest) {
167 // Only supports destinations which map to single public keys, i.e. P2PKH.
168 if (auto id = std::get_if<PKHash>(&dest)) {
169 return ToKeyID(*id);
170 }
171 return CKeyID();
172}
An encapsulated secp256k1 private key.
Definition key.h:28
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition key.cpp:210
A reference to a CKey: the Hash160 of its serialized public key.
Definition pubkey.h:22
An encapsulated public key.
Definition pubkey.h:31
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition pubkey.h:137
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition standard.h:24
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)
virtual std::set< CKeyID > GetKeys() const
virtual std::set< CScriptID > GetCScripts() const
virtual bool HaveCScript(const CScriptID &hash) const override
virtual bool HaveKey(const CKeyID &address) 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 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
bool error(const char *fmt, const Args &...args)
Definition logging.h:226
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
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).
CKeyID ToKeyID(const PKHash &key_hash)
Definition standard.cpp:25
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition standard.h:85
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< 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
#define LOCK(cs)
Definition sync.h:306