22
22
#include < policy/rbf.h>
23
23
#include < primitives/block.h>
24
24
#include < primitives/transaction.h>
25
+ #include < script/descriptor.h>
25
26
#include < script/script.h>
26
27
#include < shutdown.h>
27
28
#include < timedata.h>
@@ -104,67 +105,17 @@ std::string COutput::ToString() const
104
105
return strprintf (" COutput(%s, %d, %d) [%s]" , tx->GetHash ().ToString (), i, nDepth, FormatMoney (tx->tx ->vout [i].nValue ));
105
106
}
106
107
107
- /* * A class to identify which pubkeys a script and a keystore have in common. */
108
- class CAffectedKeysVisitor : public boost ::static_visitor<void > {
109
- private:
110
- const CKeyStore &keystore;
111
- std::vector<CKeyID> &vKeys;
112
-
113
- public:
114
- /* *
115
- * @param[in] keystoreIn The CKeyStore that is queried for the presence of a pubkey.
116
- * @param[out] vKeysIn A vector to which a script's pubkey identifiers are appended if they are in the keystore.
117
- */
118
- CAffectedKeysVisitor (const CKeyStore &keystoreIn, std::vector<CKeyID> &vKeysIn) : keystore(keystoreIn), vKeys(vKeysIn) {}
119
-
120
- /* *
121
- * Apply the visitor to each destination in a script, recursively to the redeemscript
122
- * in the case of p2sh destinations.
123
- * @param[in] script The CScript from which destinations are extracted.
124
- * @post Any CKeyIDs that script and keystore have in common are appended to the visitor's vKeys.
125
- */
126
- void Process (const CScript &script) {
127
- txnouttype type;
128
- std::vector<CTxDestination> vDest;
129
- int nRequired;
130
- if (ExtractDestinations (script, type, vDest, nRequired)) {
131
- for (const CTxDestination &dest : vDest)
132
- boost::apply_visitor (*this , dest);
133
- }
134
- }
135
-
136
- void operator ()(const CKeyID &keyId) {
137
- if (keystore.HaveKey (keyId))
138
- vKeys.push_back (keyId);
139
- }
140
-
141
- void operator ()(const CScriptID &scriptId) {
142
- CScript script;
143
- if (keystore.GetCScript (scriptId, script))
144
- Process (script);
145
- }
146
-
147
- void operator ()(const WitnessV0ScriptHash& scriptID)
148
- {
149
- CScriptID id;
150
- CRIPEMD160 ().Write (scriptID.begin (), 32 ).Finalize (id.begin ());
151
- CScript script;
152
- if (keystore.GetCScript (id, script)) {
153
- Process (script);
154
- }
155
- }
156
-
157
- void operator ()(const WitnessV0KeyHash& keyid)
158
- {
159
- CKeyID id (keyid);
160
- if (keystore.HaveKey (id)) {
161
- vKeys.push_back (id);
162
- }
108
+ std::vector<CKeyID> GetAffectedKeys (const CScript& spk, const SigningProvider& provider)
109
+ {
110
+ std::vector<CScript> dummy;
111
+ FlatSigningProvider out;
112
+ InferDescriptor (spk, provider)->Expand (0 , DUMMY_SIGNING_PROVIDER, dummy, out);
113
+ std::vector<CKeyID> ret;
114
+ for (const auto & entry : out.pubkeys ) {
115
+ ret.push_back (entry.first );
163
116
}
164
-
165
- template <typename X>
166
- void operator ()(const X &none) {}
167
- };
117
+ return ret;
118
+ }
168
119
169
120
const CWalletTx* CWallet::GetWalletTx (const uint256& hash) const
170
121
{
@@ -977,9 +928,7 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransactionRef& ptx, const CBlockI
977
928
// loop though all outputs
978
929
for (const CTxOut& txout: tx.vout ) {
979
930
// extract addresses and check if they match with an unused keypool key
980
- std::vector<CKeyID> vAffected;
981
- CAffectedKeysVisitor (*this , vAffected).Process (txout.scriptPubKey );
982
- for (const CKeyID &keyid : vAffected) {
931
+ for (const auto & keyid : GetAffectedKeys (txout.scriptPubKey , *this )) {
983
932
std::map<CKeyID, int64_t >::const_iterator mi = m_pool_key_to_index.find (keyid);
984
933
if (mi != m_pool_key_to_index.end ()) {
985
934
WalletLogPrintf (" %s: Detected a used keypool key, mark all keypool key up to this key as used\n " , __func__);
@@ -3693,7 +3642,6 @@ void CWallet::GetKeyBirthTimes(interfaces::Chain::Lock& locked_chain, std::map<C
3693
3642
return ;
3694
3643
3695
3644
// find first block that affects those keys, if there are any left
3696
- std::vector<CKeyID> vAffected;
3697
3645
for (const auto & entry : mapWallet) {
3698
3646
// iterate over all wallet transactions...
3699
3647
const CWalletTx &wtx = entry.second ;
@@ -3703,14 +3651,12 @@ void CWallet::GetKeyBirthTimes(interfaces::Chain::Lock& locked_chain, std::map<C
3703
3651
int nHeight = pindex->nHeight ;
3704
3652
for (const CTxOut &txout : wtx.tx ->vout ) {
3705
3653
// iterate over all their outputs
3706
- CAffectedKeysVisitor (*this , vAffected).Process (txout.scriptPubKey );
3707
- for (const CKeyID &keyid : vAffected) {
3654
+ for (const auto &keyid : GetAffectedKeys (txout.scriptPubKey , *this )) {
3708
3655
// ... and all their affected keys
3709
3656
std::map<CKeyID, CBlockIndex*>::iterator rit = mapKeyFirstBlock.find (keyid);
3710
3657
if (rit != mapKeyFirstBlock.end () && nHeight < rit->second ->nHeight )
3711
3658
rit->second = pindex;
3712
3659
}
3713
- vAffected.clear ();
3714
3660
}
3715
3661
}
3716
3662
}
0 commit comments