0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-03 09:56:38 -05:00

wallet: refactor GetNewDestination, use BResult

This commit is contained in:
furszy 2022-05-24 21:59:54 -03:00
parent 22351725bc
commit 111ea3ab71
No known key found for this signature in database
GPG key ID: 5DD23CCC686AA623
13 changed files with 72 additions and 86 deletions

View file

@ -47,12 +47,11 @@ static void BenchUnloadWallet(std::shared_ptr<CWallet>&& wallet)
static void AddTx(CWallet& wallet)
{
bilingual_str error;
CTxDestination dest;
wallet.GetNewDestination(OutputType::BECH32, "", dest, error);
const auto& dest = wallet.GetNewDestination(OutputType::BECH32, "");
assert(dest.HasRes());
CMutableTransaction mtx;
mtx.vout.push_back({COIN, GetScriptForDestination(dest)});
mtx.vout.push_back({COIN, GetScriptForDestination(dest.GetObj())});
mtx.vin.push_back(CTxIn());
wallet.AddToWallet(MakeTransactionRef(mtx), TxStateInactive{});

View file

@ -88,7 +88,7 @@ public:
virtual std::string getWalletName() = 0;
// Get a new address.
virtual bool getNewDestination(const OutputType type, const std::string label, CTxDestination& dest) = 0;
virtual BResult<CTxDestination> getNewDestination(const OutputType type, const std::string label) = 0;
//! Get public key.
virtual bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) = 0;

View file

@ -370,23 +370,21 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
else if(type == Receive)
{
// Generate a new address to associate with given label
CTxDestination dest;
if(!walletModel->wallet().getNewDestination(address_type, strLabel, dest))
{
auto op_dest = walletModel->wallet().getNewDestination(address_type, strLabel);
if (!op_dest) {
WalletModel::UnlockContext ctx(walletModel->requestUnlock());
if(!ctx.isValid())
{
if (!ctx.isValid()) {
// Unlock wallet failed or was cancelled
editStatus = WALLET_UNLOCK_FAILURE;
return QString();
}
if(!walletModel->wallet().getNewDestination(address_type, strLabel, dest))
{
op_dest = walletModel->wallet().getNewDestination(address_type, strLabel);
if (!op_dest) {
editStatus = KEY_GENERATION_FAILURE;
return QString();
}
}
strAddress = EncodeDestination(dest);
strAddress = EncodeDestination(op_dest.GetObj());
}
else
{

View file

@ -20,11 +20,10 @@ const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqq
std::string getnewaddress(CWallet& w)
{
constexpr auto output_type = OutputType::BECH32;
CTxDestination dest;
bilingual_str error;
if (!w.GetNewDestination(output_type, "", dest, error)) assert(false);
auto op_dest = w.GetNewDestination(output_type, "");
assert(op_dest.HasRes());
return EncodeDestination(dest);
return EncodeDestination(op_dest.GetObj());
}
#endif // ENABLE_WALLET

View file

@ -146,11 +146,10 @@ public:
void abortRescan() override { m_wallet->AbortRescan(); }
bool backupWallet(const std::string& filename) override { return m_wallet->BackupWallet(filename); }
std::string getWalletName() override { return m_wallet->GetName(); }
bool getNewDestination(const OutputType type, const std::string label, CTxDestination& dest) override
BResult<CTxDestination> getNewDestination(const OutputType type, const std::string label) override
{
LOCK(m_wallet->cs_wallet);
bilingual_str error;
return m_wallet->GetNewDestination(type, label, dest, error);
return m_wallet->GetNewDestination(type, label);
}
bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) override
{

View file

@ -58,13 +58,12 @@ RPCHelpMan getnewaddress()
output_type = parsed.value();
}
CTxDestination dest;
bilingual_str error;
if (!pwallet->GetNewDestination(output_type, label, dest, error)) {
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error.original);
auto op_dest = pwallet->GetNewDestination(output_type, label);
if (!op_dest) {
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, op_dest.GetError().original);
}
return EncodeDestination(dest);
return EncodeDestination(op_dest.GetObj());
},
};
}
@ -106,12 +105,11 @@ RPCHelpMan getrawchangeaddress()
output_type = parsed.value();
}
CTxDestination dest;
bilingual_str error;
if (!pwallet->GetNewChangeDestination(output_type, dest, error)) {
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error.original);
auto op_dest = pwallet->GetNewChangeDestination(output_type);
if (!op_dest) {
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, op_dest.GetError().original);
}
return EncodeDestination(dest);
return EncodeDestination(op_dest.GetObj());
},
};
}

View file

@ -21,26 +21,22 @@ namespace wallet {
//! Value for the first BIP 32 hardened derivation. Can be used as a bit mask and as a value. See BIP 32 for more details.
const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000;
bool LegacyScriptPubKeyMan::GetNewDestination(const OutputType type, CTxDestination& dest, bilingual_str& error)
BResult<CTxDestination> LegacyScriptPubKeyMan::GetNewDestination(const OutputType type)
{
if (LEGACY_OUTPUT_TYPES.count(type) == 0) {
error = _("Error: Legacy wallets only support the \"legacy\", \"p2sh-segwit\", and \"bech32\" address types");
return false;
return _("Error: Legacy wallets only support the \"legacy\", \"p2sh-segwit\", and \"bech32\" address types");;
}
assert(type != OutputType::BECH32M);
LOCK(cs_KeyStore);
error.clear();
// Generate a new key that is added to wallet
CPubKey new_key;
if (!GetKeyFromPool(new_key, type)) {
error = _("Error: Keypool ran out, please call keypoolrefill first");
return false;
return _("Error: Keypool ran out, please call keypoolrefill first");
}
LearnRelatedScripts(new_key, type);
dest = GetDestinationForKey(new_key, type);
return true;
return GetDestinationForKey(new_key, type);
}
typedef std::vector<unsigned char> valtype;
@ -1658,12 +1654,11 @@ std::set<CKeyID> LegacyScriptPubKeyMan::GetKeys() const
return set_address;
}
bool DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type, CTxDestination& dest, bilingual_str& error)
BResult<CTxDestination> DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type)
{
// Returns true if this descriptor supports getting new addresses. Conditions where we may be unable to fetch them (e.g. locked) are caught later
if (!CanGetAddresses()) {
error = _("No addresses available");
return false;
return _("No addresses available");
}
{
LOCK(cs_desc_man);
@ -1681,15 +1676,14 @@ bool DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type, CTxDest
std::vector<CScript> scripts_temp;
if (m_wallet_descriptor.range_end <= m_max_cached_index && !TopUp(1)) {
// We can't generate anymore keys
error = _("Error: Keypool ran out, please call keypoolrefill first");
return false;
return _("Error: Keypool ran out, please call keypoolrefill first");
}
if (!m_wallet_descriptor.descriptor->ExpandFromCache(m_wallet_descriptor.next_index, m_wallet_descriptor.cache, scripts_temp, out_keys)) {
// We can't generate anymore keys
error = _("Error: Keypool ran out, please call keypoolrefill first");
return false;
return _("Error: Keypool ran out, please call keypoolrefill first");
}
CTxDestination dest;
std::optional<OutputType> out_script_type = m_wallet_descriptor.descriptor->GetOutputType();
if (out_script_type && out_script_type == type) {
ExtractDestination(scripts_temp[0], dest);
@ -1698,7 +1692,7 @@ bool DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type, CTxDest
}
m_wallet_descriptor.next_index++;
WalletBatch(m_storage.GetDatabase()).WriteDescriptor(GetID(), m_wallet_descriptor);
return true;
return dest;
}
}
@ -1769,9 +1763,14 @@ bool DescriptorScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, Walle
bool DescriptorScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error)
{
LOCK(cs_desc_man);
bool result = GetNewDestination(type, address, error);
auto op_dest = GetNewDestination(type);
index = m_wallet_descriptor.next_index - 1;
return result;
if (op_dest) {
address = op_dest.GetObj();
} else {
error = op_dest.GetError();
}
return op_dest.HasRes();
}
void DescriptorScriptPubKeyMan::ReturnDestination(int64_t index, bool internal, const CTxDestination& addr)

View file

@ -11,6 +11,7 @@
#include <script/standard.h>
#include <util/error.h>
#include <util/message.h>
#include <util/result.h>
#include <util/time.h>
#include <wallet/crypter.h>
#include <wallet/ismine.h>
@ -171,7 +172,7 @@ protected:
public:
explicit ScriptPubKeyMan(WalletStorage& storage) : m_storage(storage) {}
virtual ~ScriptPubKeyMan() {};
virtual bool GetNewDestination(const OutputType type, CTxDestination& dest, bilingual_str& error) { return false; }
virtual BResult<CTxDestination> GetNewDestination(const OutputType type) { return Untranslated("Not supported"); }
virtual isminetype IsMine(const CScript& script) const { return ISMINE_NO; }
//! Check that the given decryption key is valid for this ScriptPubKeyMan, i.e. it decrypts all of the keys handled by it.
@ -359,7 +360,7 @@ private:
public:
using ScriptPubKeyMan::ScriptPubKeyMan;
bool GetNewDestination(const OutputType type, CTxDestination& dest, bilingual_str& error) override;
BResult<CTxDestination> GetNewDestination(const OutputType type) override;
isminetype IsMine(const CScript& script) const override;
bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) override;
@ -567,7 +568,7 @@ public:
mutable RecursiveMutex cs_desc_man;
bool GetNewDestination(const OutputType type, CTxDestination& dest, bilingual_str& error) override;
BResult<CTxDestination> GetNewDestination(const OutputType type) override;
isminetype IsMine(const CScript& script) const override;
bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) override;

View file

@ -74,11 +74,9 @@ static void add_coin(std::vector<COutput>& coins, CWallet& wallet, const CAmount
tx.vout.resize(nInput + 1);
tx.vout[nInput].nValue = nValue;
if (spendable) {
CTxDestination dest;
bilingual_str error;
const bool destination_ok = wallet.GetNewDestination(OutputType::BECH32, "", dest, error);
assert(destination_ok);
tx.vout[nInput].scriptPubKey = GetScriptForDestination(dest);
auto op_dest = wallet.GetNewDestination(OutputType::BECH32, "");
assert(op_dest.HasRes());
tx.vout[nInput].scriptPubKey = GetScriptForDestination(op_dest.GetObj());
}
uint256 txid = tx.GetHash();

View file

@ -69,15 +69,14 @@ struct FuzzedWallet {
CScript GetScriptPubKey(FuzzedDataProvider& fuzzed_data_provider)
{
auto type{fuzzed_data_provider.PickValueInArray(OUTPUT_TYPES)};
CTxDestination dest;
bilingual_str error;
BResult<CTxDestination> op_dest;
if (fuzzed_data_provider.ConsumeBool()) {
assert(wallet->GetNewDestination(type, "", dest, error));
op_dest = wallet->GetNewDestination(type, "");
} else {
assert(wallet->GetNewChangeDestination(type, dest, error));
op_dest = wallet->GetNewChangeDestination(type);
}
assert(error.empty());
return GetScriptForDestination(dest);
assert(op_dest.HasRes());
return GetScriptForDestination(op_dest.GetObj());
}
};

View file

@ -610,9 +610,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_disableprivkeys, TestChain100Setup)
wallet->SetMinVersion(FEATURE_LATEST);
wallet->SetWalletFlag(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
BOOST_CHECK(!wallet->TopUpKeyPool(1000));
CTxDestination dest;
bilingual_str error;
BOOST_CHECK(!wallet->GetNewDestination(OutputType::BECH32, "", dest, error));
BOOST_CHECK(!wallet->GetNewDestination(OutputType::BECH32, ""));
}
{
const std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", m_args, CreateDummyWalletDatabase());
@ -620,9 +618,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_disableprivkeys, TestChain100Setup)
wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
wallet->SetMinVersion(FEATURE_LATEST);
wallet->SetWalletFlag(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
CTxDestination dest;
bilingual_str error;
BOOST_CHECK(!wallet->GetNewDestination(OutputType::BECH32, "", dest, error));
BOOST_CHECK(!wallet->GetNewDestination(OutputType::BECH32, ""));
}
}

View file

@ -2307,37 +2307,36 @@ bool CWallet::TopUpKeyPool(unsigned int kpSize)
return res;
}
bool CWallet::GetNewDestination(const OutputType type, const std::string label, CTxDestination& dest, bilingual_str& error)
BResult<CTxDestination> CWallet::GetNewDestination(const OutputType type, const std::string label)
{
LOCK(cs_wallet);
error.clear();
bool result = false;
auto spk_man = GetScriptPubKeyMan(type, false /* internal */);
if (spk_man) {
spk_man->TopUp();
result = spk_man->GetNewDestination(type, dest, error);
} else {
error = strprintf(_("Error: No %s addresses available."), FormatOutputType(type));
}
if (result) {
SetAddressBook(dest, label, "receive");
if (!spk_man) {
return strprintf(_("Error: No %s addresses available."), FormatOutputType(type));
}
return result;
spk_man->TopUp();
auto op_dest = spk_man->GetNewDestination(type);
if (op_dest) {
SetAddressBook(op_dest.GetObj(), label, "receive");
}
return op_dest;
}
bool CWallet::GetNewChangeDestination(const OutputType type, CTxDestination& dest, bilingual_str& error)
BResult<CTxDestination> CWallet::GetNewChangeDestination(const OutputType type)
{
LOCK(cs_wallet);
error.clear();
CTxDestination dest;
bilingual_str error;
ReserveDestination reservedest(this, type);
if (!reservedest.GetReservedDestination(dest, true, error)) {
return false;
return error;
}
reservedest.KeepDestination();
return true;
return dest;
}
std::optional<int64_t> CWallet::GetOldestKeyPoolTime() const

View file

@ -15,6 +15,7 @@
#include <psbt.h>
#include <tinyformat.h>
#include <util/message.h>
#include <util/result.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/system.h>
@ -665,8 +666,8 @@ public:
*/
void MarkDestinationsDirty(const std::set<CTxDestination>& destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
bool GetNewDestination(const OutputType type, const std::string label, CTxDestination& dest, bilingual_str& error);
bool GetNewChangeDestination(const OutputType type, CTxDestination& dest, bilingual_str& error);
BResult<CTxDestination> GetNewDestination(const OutputType type, const std::string label);
BResult<CTxDestination> GetNewChangeDestination(const OutputType type);
isminetype IsMine(const CTxDestination& dest) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
isminetype IsMine(const CScript& script) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);