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.
This commit is contained in:
Andrew Chow committed 2020-01-23 16:35:08 -05:00
1 parent 3afe53c403
commit 3f373659d7
7 files changed
+46 -30

No files matched your search

+2 -2
View File
@@ -119,7 +119,7 @@ public:
} }
bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) override bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) override
{ {
const SigningProvider* provider = m_wallet->GetSigningProvider(script); std::unique_ptr<SigningProvider> provider = m_wallet->GetSigningProvider(script);
if (provider) { if (provider) {
return provider->GetPubKey(address, pub_key); return provider->GetPubKey(address, pub_key);
} }
@@ -127,7 +127,7 @@ public:
} }
bool getPrivKey(const CScript& script, const CKeyID& address, CKey& key) override bool getPrivKey(const CScript& script, const CKeyID& address, CKey& key) override
{ {
const SigningProvider* provider = m_wallet->GetSigningProvider(script); std::unique_ptr<SigningProvider> provider = m_wallet->GetSigningProvider(script);
if (provider) { if (provider) {
return provider->GetKey(address, key); return provider->GetKey(address, key);
} }
+4 -4
View File
@@ -52,21 +52,21 @@ TransactionError FillPSBT(const CWallet* pwallet, PartiallySignedTransaction& ps
} }
SignatureData sigdata; SignatureData sigdata;
input.FillSignatureData(sigdata); input.FillSignatureData(sigdata);
const SigningProvider* provider = pwallet->GetSigningProvider(script, sigdata); std::unique_ptr<SigningProvider> provider = pwallet->GetSigningProvider(script, sigdata);
if (!provider) { if (!provider) {
complete = false; complete = false;
continue; continue;
} }
complete &= SignPSBTInput(HidingSigningProvider(provider, !sign, !bip32derivs), psbtx, i, sighash_type); complete &= SignPSBTInput(HidingSigningProvider(provider.get(), !sign, !bip32derivs), psbtx, i, sighash_type);
} }
// Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change // Fill in the bip32 keypaths and redeemscripts for the outputs so that hardware wallets can identify change
for (unsigned int i = 0; i < psbtx.tx->vout.size(); ++i) { for (unsigned int i = 0; i < psbtx.tx->vout.size(); ++i) {
const CTxOut& out = psbtx.tx->vout.at(i); const CTxOut& out = psbtx.tx->vout.at(i);
const SigningProvider* provider = pwallet->GetSigningProvider(out.scriptPubKey); std::unique_ptr<SigningProvider> provider = pwallet->GetSigningProvider(out.scriptPubKey);
if (provider) { if (provider) {
UpdatePSBTOutput(HidingSigningProvider(provider, true, !bip32derivs), psbtx, i); UpdatePSBTOutput(HidingSigningProvider(provider.get(), true, !bip32derivs), psbtx, i);
} }
} }
+12 -12
View File
@@ -565,7 +565,7 @@ static UniValue signmessage(const JSONRPCRequest& request)
} }
CScript script_pub_key = GetScriptForDestination(*pkhash); CScript script_pub_key = GetScriptForDestination(*pkhash);
const SigningProvider* provider = pwallet->GetSigningProvider(script_pub_key); std::unique_ptr<SigningProvider> provider = pwallet->GetSigningProvider(script_pub_key);
if (!provider) { if (!provider) {
throw JSONRPCError(RPC_WALLET_ERROR, "Private key not available"); throw JSONRPCError(RPC_WALLET_ERROR, "Private key not available");
} }
@@ -2948,7 +2948,7 @@ static UniValue listunspent(const JSONRPCRequest& request)
entry.pushKV("label", i->second.name); entry.pushKV("label", i->second.name);
} }
const SigningProvider* provider = pwallet->GetSigningProvider(scriptPubKey); std::unique_ptr<SigningProvider> provider = pwallet->GetSigningProvider(scriptPubKey);
if (provider) { if (provider) {
if (scriptPubKey.IsPayToScriptHash()) { if (scriptPubKey.IsPayToScriptHash()) {
const CScriptID& hash = CScriptID(boost::get<ScriptHash>(address)); const CScriptID& hash = CScriptID(boost::get<ScriptHash>(address));
@@ -2988,7 +2988,7 @@ static UniValue listunspent(const JSONRPCRequest& request)
entry.pushKV("spendable", out.fSpendable); entry.pushKV("spendable", out.fSpendable);
entry.pushKV("solvable", out.fSolvable); entry.pushKV("solvable", out.fSolvable);
if (out.fSolvable) { if (out.fSolvable) {
const SigningProvider* provider = pwallet->GetSigningProvider(scriptPubKey); std::unique_ptr<SigningProvider> provider = pwallet->GetSigningProvider(scriptPubKey);
if (provider) { if (provider) {
auto descriptor = InferDescriptor(scriptPubKey, *provider); auto descriptor = InferDescriptor(scriptPubKey, *provider);
entry.pushKV("desc", descriptor->ToString()); entry.pushKV("desc", descriptor->ToString());
@@ -3301,21 +3301,21 @@ UniValue signrawtransactionwithwallet(const JSONRPCRequest& request)
// Parse the prevtxs array // Parse the prevtxs array
ParsePrevouts(request.params[1], nullptr, coins); ParsePrevouts(request.params[1], nullptr, coins);
std::set<const SigningProvider*> providers; std::set<std::shared_ptr<SigningProvider>> providers;
for (const std::pair<COutPoint, Coin> coin_pair : coins) { for (const std::pair<COutPoint, Coin> coin_pair : coins) {
const SigningProvider* provider = pwallet->GetSigningProvider(coin_pair.second.out.scriptPubKey); std::unique_ptr<SigningProvider> provider = pwallet->GetSigningProvider(coin_pair.second.out.scriptPubKey);
if (provider) { if (provider) {
providers.insert(std::move(provider)); providers.insert(std::move(provider));
} }
} }
if (providers.size() == 0) { if (providers.size() == 0) {
// When there are no available providers, use DUMMY_SIGNING_PROVIDER so we can check if the tx is complete // When there are no available providers, use a dummy SigningProvider so we can check if the tx is complete
providers.insert(&DUMMY_SIGNING_PROVIDER); providers.insert(std::make_shared<SigningProvider>());
} }
UniValue result(UniValue::VOBJ); UniValue result(UniValue::VOBJ);
for (const SigningProvider* provider : providers) { for (std::shared_ptr<SigningProvider> provider : providers) {
SignTransaction(mtx, provider, coins, request.params[2], result); SignTransaction(mtx, provider.get(), coins, request.params[2], result);
} }
return result; return result;
} }
@@ -3701,12 +3701,12 @@ static UniValue DescribeWalletAddress(CWallet* pwallet, const CTxDestination& de
UniValue ret(UniValue::VOBJ); UniValue ret(UniValue::VOBJ);
UniValue detail = DescribeAddress(dest); UniValue detail = DescribeAddress(dest);
CScript script = GetScriptForDestination(dest); CScript script = GetScriptForDestination(dest);
const SigningProvider* provider = nullptr; std::unique_ptr<SigningProvider> provider = nullptr;
if (pwallet) { if (pwallet) {
provider = pwallet->GetSigningProvider(script); provider = pwallet->GetSigningProvider(script);
} }
ret.pushKVs(detail); ret.pushKVs(detail);
ret.pushKVs(boost::apply_visitor(DescribeWalletAddressVisitor(provider), dest)); ret.pushKVs(boost::apply_visitor(DescribeWalletAddressVisitor(provider.get()), dest));
return ret; return ret;
} }
@@ -3804,7 +3804,7 @@ UniValue getaddressinfo(const JSONRPCRequest& request)
CScript scriptPubKey = GetScriptForDestination(dest); CScript scriptPubKey = GetScriptForDestination(dest);
ret.pushKV("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end())); ret.pushKV("scriptPubKey", HexStr(scriptPubKey.begin(), scriptPubKey.end()));
const SigningProvider* provider = pwallet->GetSigningProvider(scriptPubKey); std::unique_ptr<SigningProvider> provider = pwallet->GetSigningProvider(scriptPubKey);
isminetype mine = pwallet->IsMine(dest); isminetype mine = pwallet->IsMine(dest);
ret.pushKV("ismine", bool(mine & ISMINE_SPENDABLE)); ret.pushKV("ismine", bool(mine & ISMINE_SPENDABLE));
+2 -2
View File
@@ -469,9 +469,9 @@ int64_t LegacyScriptPubKeyMan::GetTimeFirstKey() const
return nTimeFirstKey; return nTimeFirstKey;
} }
const SigningProvider* LegacyScriptPubKeyMan::GetSigningProvider(const CScript& script) const std::unique_ptr<SigningProvider> LegacyScriptPubKeyMan::GetSigningProvider(const CScript& script) const
{ {
return this; return MakeUnique<LegacySigningProvider>(*this);
} }
bool LegacyScriptPubKeyMan::CanProvide(const CScript& script, SignatureData& sigdata) bool LegacyScriptPubKeyMan::CanProvide(const CScript& script, SignatureData& sigdata)
+18 -2
View File
@@ -198,7 +198,7 @@ public:
virtual const CKeyMetadata* GetMetadata(const CTxDestination& dest) const { return nullptr; } virtual const CKeyMetadata* GetMetadata(const CTxDestination& dest) const { return nullptr; }
virtual const SigningProvider* GetSigningProvider(const CScript& script) const { return nullptr; } virtual std::unique_ptr<SigningProvider> GetSigningProvider(const CScript& script) const { return nullptr; }
/** Whether this ScriptPubKeyMan can provide a SigningProvider (via GetSigningProvider) that, combined with /** Whether this ScriptPubKeyMan can provide a SigningProvider (via GetSigningProvider) that, combined with
* sigdata, can produce a valid signature. * sigdata, can produce a valid signature.
@@ -341,7 +341,7 @@ public:
bool CanGetAddresses(bool internal = false) override; bool CanGetAddresses(bool internal = false) override;
const SigningProvider* GetSigningProvider(const CScript& script) const override; std::unique_ptr<SigningProvider> GetSigningProvider(const CScript& script) const override;
bool CanProvide(const CScript& script, SignatureData& sigdata) override; bool CanProvide(const CScript& script, SignatureData& sigdata) override;
@@ -442,4 +442,20 @@ public:
std::set<CKeyID> GetKeys() const override; std::set<CKeyID> GetKeys() const override;
}; };
/** Wraps a LegacyScriptPubKeyMan so that it can be returned in a new unique_ptr */
class LegacySigningProvider : public SigningProvider
{
private:
const LegacyScriptPubKeyMan& m_spk_man;
public:
LegacySigningProvider(const LegacyScriptPubKeyMan& spk_man) : m_spk_man(spk_man) {}
bool GetCScript(const CScriptID &scriptid, CScript& script) const override { return m_spk_man.GetCScript(scriptid, script); }
bool HaveCScript(const CScriptID &scriptid) const override { return m_spk_man.HaveCScript(scriptid); }
bool GetPubKey(const CKeyID &address, CPubKey& pubkey) const override { return m_spk_man.GetPubKey(address, pubkey); }
bool GetKey(const CKeyID &address, CKey& key) const override { return m_spk_man.GetKey(address, key); }
bool HaveKey(const CKeyID &address) const override { return m_spk_man.HaveKey(address); }
bool GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const override { return m_spk_man.GetKeyOrigin(keyid, info); }
};
#endif // BITCOIN_WALLET_SCRIPTPUBKEYMAN_H #endif // BITCOIN_WALLET_SCRIPTPUBKEYMAN_H
+6 -6
View File
@@ -1399,7 +1399,7 @@ bool CWallet::DummySignInput(CTxIn &tx_in, const CTxOut &txout, bool use_max_sig
const CScript& scriptPubKey = txout.scriptPubKey; const CScript& scriptPubKey = txout.scriptPubKey;
SignatureData sigdata; SignatureData sigdata;
const SigningProvider* provider = GetSigningProvider(scriptPubKey); std::unique_ptr<SigningProvider> provider = GetSigningProvider(scriptPubKey);
if (!provider) { if (!provider) {
// We don't know about this scriptpbuKey; // We don't know about this scriptpbuKey;
return false; return false;
@@ -2163,7 +2163,7 @@ void CWallet::AvailableCoins(interfaces::Chain::Lock& locked_chain, std::vector<
continue; continue;
} }
const SigningProvider* provider = GetSigningProvider(wtx.tx->vout[i].scriptPubKey); std::unique_ptr<SigningProvider> provider = GetSigningProvider(wtx.tx->vout[i].scriptPubKey);
bool solvable = provider ? IsSolvable(*provider, wtx.tx->vout[i].scriptPubKey) : false; bool solvable = provider ? IsSolvable(*provider, wtx.tx->vout[i].scriptPubKey) : false;
bool spendable = ((mine & ISMINE_SPENDABLE) != ISMINE_NO) || (((mine & ISMINE_WATCH_ONLY) != ISMINE_NO) && (coinControl && coinControl->fAllowWatchOnly && solvable)); 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)
const CAmount& amount = mi->second.tx->vout[input.prevout.n].nValue; const CAmount& amount = mi->second.tx->vout[input.prevout.n].nValue;
SignatureData sigdata; SignatureData sigdata;
const SigningProvider* provider = GetSigningProvider(scriptPubKey); std::unique_ptr<SigningProvider> provider = GetSigningProvider(scriptPubKey);
if (!provider) { if (!provider) {
// We don't know about this scriptpbuKey; // We don't know about this scriptpbuKey;
return false; return false;
@@ -2886,7 +2886,7 @@ bool CWallet::CreateTransaction(interfaces::Chain::Lock& locked_chain, const std
const CScript& scriptPubKey = coin.txout.scriptPubKey; const CScript& scriptPubKey = coin.txout.scriptPubKey;
SignatureData sigdata; SignatureData sigdata;
const SigningProvider* provider = GetSigningProvider(scriptPubKey); std::unique_ptr<SigningProvider> provider = GetSigningProvider(scriptPubKey);
if (!provider || !ProduceSignature(*provider, MutableTransactionSignatureCreator(&txNew, nIn, coin.txout.nValue, SIGHASH_ALL), scriptPubKey, sigdata)) if (!provider || !ProduceSignature(*provider, MutableTransactionSignatureCreator(&txNew, nIn, coin.txout.nValue, SIGHASH_ALL), scriptPubKey, sigdata))
{ {
strFailReason = _("Signing transaction failed").translated; strFailReason = _("Signing transaction failed").translated;
@@ -4165,13 +4165,13 @@ ScriptPubKeyMan* CWallet::GetScriptPubKeyMan(const uint256& id) const
return nullptr; return nullptr;
} }
const SigningProvider* CWallet::GetSigningProvider(const CScript& script) const std::unique_ptr<SigningProvider> CWallet::GetSigningProvider(const CScript& script) const
{ {
SignatureData sigdata; SignatureData sigdata;
return GetSigningProvider(script, sigdata); return GetSigningProvider(script, sigdata);
} }
const SigningProvider* CWallet::GetSigningProvider(const CScript& script, SignatureData& sigdata) const std::unique_ptr<SigningProvider> CWallet::GetSigningProvider(const CScript& script, SignatureData& sigdata) const
{ {
for (const auto& spk_man_pair : m_spk_managers) { for (const auto& spk_man_pair : m_spk_managers) {
if (spk_man_pair.second->CanProvide(script, sigdata)) { if (spk_man_pair.second->CanProvide(script, sigdata)) {
+2 -2
View File
@@ -1154,8 +1154,8 @@ public:
ScriptPubKeyMan* GetScriptPubKeyMan(const uint256& id) const; ScriptPubKeyMan* GetScriptPubKeyMan(const uint256& id) const;
//! Get the SigningProvider for a script //! Get the SigningProvider for a script
const SigningProvider* GetSigningProvider(const CScript& script) const; std::unique_ptr<SigningProvider> GetSigningProvider(const CScript& script) const;
const SigningProvider* GetSigningProvider(const CScript& script, SignatureData& sigdata) const; std::unique_ptr<SigningProvider> GetSigningProvider(const CScript& script, SignatureData& sigdata) const;
//! Get the LegacyScriptPubKeyMan which is used for all types, internal, and external. //! Get the LegacyScriptPubKeyMan which is used for all types, internal, and external.
LegacyScriptPubKeyMan* GetLegacyScriptPubKeyMan() const; LegacyScriptPubKeyMan* GetLegacyScriptPubKeyMan() const;