Skip to content

Commit 3f37365

Browse files
committed
Refactor: Replace SigningProvider pointers with unique_ptrs
Needed for future ScriptPubKeyMans which may need to create SigningProviders dynamically and thus a normal pointer is not enough This commit does not change behavior.
1 parent 3afe53c commit 3f37365

File tree

7 files changed

+46
-30
lines changed

7 files changed

+46
-30
lines changed

src/interfaces/wallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ class WalletImpl : public Wallet
119119
}
120120
bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) override
121121
{
122-
const SigningProvider* provider = m_wallet->GetSigningProvider(script);
122+
std::unique_ptr<SigningProvider> provider = m_wallet->GetSigningProvider(script);
123123
if (provider) {
124124
return provider->GetPubKey(address, pub_key);
125125
}
126126
return false;
127127
}
128128
bool getPrivKey(const CScript& script, const CKeyID& address, CKey& key) override
129129
{
130-
const SigningProvider* provider = m_wallet->GetSigningProvider(script);
130+
std::unique_ptr<SigningProvider> provider = m_wallet->GetSigningProvider(script);
131131
if (provider) {
132132
return provider->GetKey(address, key);
133133
}

src/wallet/psbtwallet.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,21 @@ TransactionError FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& ps
5252
}
5353
SignatureData sigdata;
5454
input.FillSignatureData(sigdata);
55-
const SigningProvider* provider = pwallet->GetSigningProvider(script, sigdata);
55+
std::unique_ptr<SigningProvider> provider = pwallet->GetSigningProvider(script, sigdata);
5656
if (!provider) {
5757
complete = false;
5858
continue;
5959
}
6060

61-
complete &= SignPSBTInput(HidingSigningProvider(provider, !sign, !bip32derivs), psbtx, i, sighash_type);
61+
complete &= SignPSBTInput(HidingSigningProvider(provider.get(), !sign, !bip32derivs), psbtx, i, sighash_type);
6262
}
6363

6464
// Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
6565
for (unsigned int i = 0; i < psbtx.tx->vout.size(); ++i) {
6666
const CTxOut& out = psbtx.tx->vout.at(i);
67-
const SigningProvider* provider = pwallet->GetSigningProvider(out.scriptPubKey);
67+
std::unique_ptr<SigningProvider> provider = pwallet->GetSigningProvider(out.scriptPubKey);
6868
if (provider) {
69-
UpdatePSBTOutput(HidingSigningProvider(provider, true, !bip32derivs), psbtx, i);
69+
UpdatePSBTOutput(HidingSigningProvider(provider.get(), true, !bip32derivs), psbtx, i);
7070
}
7171
}
7272

src/wallet/rpcwallet.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ static UniValue signmessage(const JSONRPCRequest& request)
565565
}
566566

567567
CScript script_pub_key = GetScriptForDestination(*pkhash);
568-
const SigningProvider* provider = pwallet->GetSigningProvider(script_pub_key);
568+
std::unique_ptr<SigningProvider> provider = pwallet->GetSigningProvider(script_pub_key);
569569
if (!provider) {
570570
throw JSONRPCError(RPC_WALLET_ERROR, "Private key not available");
571571
}
@@ -2948,7 +2948,7 @@ static UniValue listunspent(const JSONRPCRequest& request)
29482948
entry.pushKV("label", i->second.name);
29492949
}
29502950

2951-
const SigningProvider* provider = pwallet->GetSigningProvider(scriptPubKey);
2951+
std::unique_ptr<SigningProvider> provider = pwallet->GetSigningProvider(scriptPubKey);
29522952
if (provider) {
29532953
if (scriptPubKey.IsPayToScriptHash()) {
29542954
const CScriptID& hash = CScriptID(boost::get<ScriptHash>(address));
@@ -2988,7 +2988,7 @@ static UniValue listunspent(const JSONRPCRequest& request)
29882988
entry.pushKV("spendable", out.fSpendable);
29892989
entry.pushKV("solvable", out.fSolvable);
29902990
if (out.fSolvable) {
2991-
const SigningProvider* provider = pwallet->GetSigningProvider(scriptPubKey);
2991+
std::unique_ptr<SigningProvider> provider = pwallet->GetSigningProvider(scriptPubKey);
29922992
if (provider) {
29932993
auto descriptor = InferDescriptor(scriptPubKey, *provider);
29942994
entry.pushKV("desc", descriptor->ToString());
@@ -3301,21 +3301,21 @@ UniValue signrawtransactionwithwallet(const JSONRPCRequest& request)
33013301
// Parse the prevtxs array
33023302
ParsePrevouts(request.params[1], nullptr, coins);
33033303

3304-
std::set<const SigningProvider*> providers;
3304+
std::set<std::shared_ptr<SigningProvider>> providers;
33053305
for (const std::pair<COutPoint, Coin> coin_pair : coins) {
3306-
const SigningProvider* provider = pwallet->GetSigningProvider(coin_pair.second.out.scriptPubKey);
3306+
std::unique_ptr<SigningProvider> provider = pwallet->GetSigningProvider(coin_pair.second.out.scriptPubKey);
33073307
if (provider) {
33083308
providers.insert(std::move(provider));
33093309
}
33103310
}
33113311
if (providers.size() == 0) {
3312-
// When there are no available providers, use DUMMY_SIGNING_PROVIDER so we can check if the tx is complete
3313-
providers.insert(&DUMMY_SIGNING_PROVIDER);
3312+
// When there are no available providers, use a dummy SigningProvider so we can check if the tx is complete
3313+
providers.insert(std::make_shared<SigningProvider>());
33143314
}
33153315

33163316
UniValue result(UniValue::VOBJ);
3317-
for (const SigningProvider* provider : providers) {
3318-
SignTransaction(mtx, provider, coins, request.params[2], result);
3317+
for (std::shared_ptr<SigningProvider> provider : providers) {
3318+
SignTransaction(mtx, provider.get(), coins, request.params[2], result);
33193319
}
33203320
return result;
33213321
}
@@ -3701,12 +3701,12 @@ static UniValue DescribeWalletAddress(CWallet* pwallet, const CTxDestination& de
37013701
UniValue ret(UniValue::VOBJ);
37023702
UniValue detail = DescribeAddress(dest);
37033703
CScript script = GetScriptForDestination(dest);
3704-
const SigningProvider* provider = nullptr;
3704+
std::unique_ptr<SigningProvider> provider = nullptr;
37053705
if (pwallet) {
37063706
provider = pwallet->GetSigningProvider(script);
37073707
}
37083708
ret.pushKVs(detail);
3709-
ret.pushKVs(boost::apply_visitor(DescribeWalletAddressVisitor(provider), dest));
3709+
ret.pushKVs(boost::apply_visitor(DescribeWalletAddressVisitor(provider.get()), dest));
37103710
return ret;
37113711
}
37123712

@@ -3804,7 +3804,7 @@ UniValue getaddressinfo(const JSONRPCRequest& request)
38043804
CScript scriptPubKey = GetScriptForDestination(dest);
38053805
ret.pushKV("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end()));
38063806

3807-
const SigningProvider* provider = pwallet->GetSigningProvider(scriptPubKey);
3807+
std::unique_ptr<SigningProvider> provider = pwallet->GetSigningProvider(scriptPubKey);
38083808

38093809
isminetype mine = pwallet->IsMine(dest);
38103810
ret.pushKV("ismine", bool(mine & ISMINE_SPENDABLE));

src/wallet/scriptpubkeyman.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,9 @@ int64_t LegacyScriptPubKeyMan::GetTimeFirstKey() const
469469
return nTimeFirstKey;
470470
}
471471

472-
const SigningProvider* LegacyScriptPubKeyMan::GetSigningProvider(const CScript& script) const
472+
std::unique_ptr<SigningProvider> LegacyScriptPubKeyMan::GetSigningProvider(const CScript& script) const
473473
{
474-
return this;
474+
return MakeUnique<LegacySigningProvider>(*this);
475475
}
476476

477477
bool LegacyScriptPubKeyMan::CanProvide(const CScript& script, SignatureData& sigdata)

src/wallet/scriptpubkeyman.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class ScriptPubKeyMan
198198

199199
virtual const CKeyMetadata* GetMetadata(const CTxDestination& dest) const { return nullptr; }
200200

201-
virtual const SigningProvider* GetSigningProvider(const CScript& script) const { return nullptr; }
201+
virtual std::unique_ptr<SigningProvider> GetSigningProvider(const CScript& script) const { return nullptr; }
202202

203203
/** Whether this ScriptPubKeyMan can provide a SigningProvider (via GetSigningProvider) that, combined with
204204
* sigdata, can produce a valid signature.
@@ -341,7 +341,7 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
341341

342342
bool CanGetAddresses(bool internal = false) override;
343343

344-
const SigningProvider* GetSigningProvider(const CScript& script) const override;
344+
std::unique_ptr<SigningProvider> GetSigningProvider(const CScript& script) const override;
345345

346346
bool CanProvide(const CScript& script, SignatureData& sigdata) override;
347347

@@ -442,4 +442,20 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
442442
std::set<CKeyID> GetKeys() const override;
443443
};
444444

445+
/** Wraps a LegacyScriptPubKeyMan so that it can be returned in a new unique_ptr */
446+
class LegacySigningProvider : public SigningProvider
447+
{
448+
private:
449+
const LegacyScriptPubKeyMan& m_spk_man;
450+
public:
451+
LegacySigningProvider(const LegacyScriptPubKeyMan& spk_man) : m_spk_man(spk_man) {}
452+
453+
bool GetCScript(const CScriptID &scriptid, CScript& script) const override { return m_spk_man.GetCScript(scriptid, script); }
454+
bool HaveCScript(const CScriptID &scriptid) const override { return m_spk_man.HaveCScript(scriptid); }
455+
bool GetPubKey(const CKeyID &address, CPubKey& pubkey) const override { return m_spk_man.GetPubKey(address, pubkey); }
456+
bool GetKey(const CKeyID &address, CKey& key) const override { return m_spk_man.GetKey(address, key); }
457+
bool HaveKey(const CKeyID &address) const override { return m_spk_man.HaveKey(address); }
458+
bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override { return m_spk_man.GetKeyOrigin(keyid, info); }
459+
};
460+
445461
#endif // BITCOIN_WALLET_SCRIPTPUBKEYMAN_H

src/wallet/wallet.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,7 @@ bool CWallet::DummySignInput(CTxIn &tx_in, const CTxOut &txout, bool use_max_sig
13991399
const CScript& scriptPubKey = txout.scriptPubKey;
14001400
SignatureData sigdata;
14011401

1402-
const SigningProvider* provider = GetSigningProvider(scriptPubKey);
1402+
std::unique_ptr<SigningProvider> provider = GetSigningProvider(scriptPubKey);
14031403
if (!provider) {
14041404
// We don't know about this scriptpbuKey;
14051405
return false;
@@ -2163,7 +2163,7 @@ void CWallet::AvailableCoins(interfaces::Chain::Lock& locked_chain, std::vector<
21632163
continue;
21642164
}
21652165

2166-
const SigningProvider* provider = GetSigningProvider(wtx.tx->vout[i].scriptPubKey);
2166+
std::unique_ptr<SigningProvider> provider = GetSigningProvider(wtx.tx->vout[i].scriptPubKey);
21672167

21682168
bool solvable = provider ? IsSolvable(*provider, wtx.tx->vout[i].scriptPubKey) : false;
21692169
bool spendable = ((mine & ISMINE_SPENDABLE) != ISMINE_NO) || (((mine & ISMINE_WATCH_ONLY) != ISMINE_NO) && (coinControl && coinControl->fAllowWatchOnly && solvable));
@@ -2417,7 +2417,7 @@ bool CWallet::SignTransaction(CMutableTransaction& tx)
24172417
const CAmount& amount = mi->second.tx->vout[input.prevout.n].nValue;
24182418
SignatureData sigdata;
24192419

2420-
const SigningProvider* provider = GetSigningProvider(scriptPubKey);
2420+
std::unique_ptr<SigningProvider> provider = GetSigningProvider(scriptPubKey);
24212421
if (!provider) {
24222422
// We don't know about this scriptpbuKey;
24232423
return false;
@@ -2886,7 +2886,7 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
28862886
const CScript& scriptPubKey = coin.txout.scriptPubKey;
28872887
SignatureData sigdata;
28882888

2889-
const SigningProvider* provider = GetSigningProvider(scriptPubKey);
2889+
std::unique_ptr<SigningProvider> provider = GetSigningProvider(scriptPubKey);
28902890
if (!provider || !ProduceSignature(*provider, MutableTransactionSignatureCreator(&txNew, nIn, coin.txout.nValue, SIGHASH_ALL), scriptPubKey, sigdata))
28912891
{
28922892
strFailReason = _("Signing transaction failed").translated;
@@ -4165,13 +4165,13 @@ ScriptPubKeyMan* CWallet::GetScriptPubKeyMan(const uint256& id) const
41654165
return nullptr;
41664166
}
41674167

4168-
const SigningProvider* CWallet::GetSigningProvider(const CScript& script) const
4168+
std::unique_ptr<SigningProvider> CWallet::GetSigningProvider(const CScript& script) const
41694169
{
41704170
SignatureData sigdata;
41714171
return GetSigningProvider(script, sigdata);
41724172
}
41734173

4174-
const SigningProvider* CWallet::GetSigningProvider(const CScript& script, SignatureData& sigdata) const
4174+
std::unique_ptr<SigningProvider> CWallet::GetSigningProvider(const CScript& script, SignatureData& sigdata) const
41754175
{
41764176
for (const auto& spk_man_pair : m_spk_managers) {
41774177
if (spk_man_pair.second->CanProvide(script, sigdata)) {

src/wallet/wallet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,8 +1154,8 @@ class CWallet final : public WalletStorage, private interfaces::Chain::Notificat
11541154
ScriptPubKeyMan* GetScriptPubKeyMan(const uint256& id) const;
11551155

11561156
//! Get the SigningProvider for a script
1157-
const SigningProvider* GetSigningProvider(const CScript& script) const;
1158-
const SigningProvider* GetSigningProvider(const CScript& script, SignatureData& sigdata) const;
1157+
std::unique_ptr<SigningProvider> GetSigningProvider(const CScript& script) const;
1158+
std::unique_ptr<SigningProvider> GetSigningProvider(const CScript& script, SignatureData& sigdata) const;
11591159

11601160
//! Get the LegacyScriptPubKeyMan which is used for all types, internal, and external.
11611161
LegacyScriptPubKeyMan* GetLegacyScriptPubKeyMan() const;

0 commit comments

Comments
 (0)