Use bilingual_str for address fetching functions

For GetNewDestination, GetNewChangeDestination, and
GetReservedDestination, use bilingual_str for
errors
This commit is contained in:
Andrew Chow 2021-06-23 16:54:13 -04:00
parent 9571c69b51
commit 171366e89b
10 changed files with 39 additions and 36 deletions

View File

@ -8,6 +8,7 @@
#include <outputtype.h> #include <outputtype.h>
#include <script/standard.h> #include <script/standard.h>
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
#include <util/translation.h>
#include <wallet/wallet.h> #include <wallet/wallet.h>
#endif #endif
@ -18,7 +19,7 @@ std::string getnewaddress(CWallet& w)
{ {
constexpr auto output_type = OutputType::BECH32; constexpr auto output_type = OutputType::BECH32;
CTxDestination dest; CTxDestination dest;
std::string error; bilingual_str error;
if (!w.GetNewDestination(output_type, "", dest, error)) assert(false); if (!w.GetNewDestination(output_type, "", dest, error)) assert(false);
return EncodeDestination(dest); return EncodeDestination(dest);

View File

@ -16,6 +16,7 @@
#include <uint256.h> #include <uint256.h>
#include <util/check.h> #include <util/check.h>
#include <util/system.h> #include <util/system.h>
#include <util/translation.h>
#include <util/ui_change_type.h> #include <util/ui_change_type.h>
#include <wallet/context.h> #include <wallet/context.h>
#include <wallet/feebumper.h> #include <wallet/feebumper.h>
@ -130,7 +131,7 @@ public:
bool getNewDestination(const OutputType type, const std::string label, CTxDestination& dest) override bool getNewDestination(const OutputType type, const std::string label, CTxDestination& dest) override
{ {
LOCK(m_wallet->cs_wallet); LOCK(m_wallet->cs_wallet);
std::string error; bilingual_str error;
return m_wallet->GetNewDestination(type, label, dest, error); return m_wallet->GetNewDestination(type, label, dest, error);
} }
bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) override bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) override

View File

@ -275,9 +275,9 @@ static RPCHelpMan getnewaddress()
} }
CTxDestination dest; CTxDestination dest;
std::string error; bilingual_str error;
if (!pwallet->GetNewDestination(output_type, label, dest, error)) { if (!pwallet->GetNewDestination(output_type, label, dest, error)) {
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error); throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error.original);
} }
return EncodeDestination(dest); return EncodeDestination(dest);
@ -322,9 +322,9 @@ static RPCHelpMan getrawchangeaddress()
} }
CTxDestination dest; CTxDestination dest;
std::string error; bilingual_str error;
if (!pwallet->GetNewChangeDestination(output_type, dest, error)) { if (!pwallet->GetNewChangeDestination(output_type, dest, error)) {
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error); throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error.original);
} }
return EncodeDestination(dest); return EncodeDestination(dest);
}, },

View File

@ -20,10 +20,10 @@
//! 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. //! 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; const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000;
bool LegacyScriptPubKeyMan::GetNewDestination(const OutputType type, CTxDestination& dest, std::string& error) bool LegacyScriptPubKeyMan::GetNewDestination(const OutputType type, CTxDestination& dest, bilingual_str& error)
{ {
if (LEGACY_OUTPUT_TYPES.count(type) == 0) { if (LEGACY_OUTPUT_TYPES.count(type) == 0) {
error = _("Error: Legacy wallets only support the \"legacy\", \"p2sh-segwit\", and \"bech32\" address types").translated; error = _("Error: Legacy wallets only support the \"legacy\", \"p2sh-segwit\", and \"bech32\" address types");
return false; return false;
} }
assert(type != OutputType::BECH32M); assert(type != OutputType::BECH32M);
@ -34,7 +34,7 @@ bool LegacyScriptPubKeyMan::GetNewDestination(const OutputType type, CTxDestinat
// Generate a new key that is added to wallet // Generate a new key that is added to wallet
CPubKey new_key; CPubKey new_key;
if (!GetKeyFromPool(new_key, type)) { if (!GetKeyFromPool(new_key, type)) {
error = _("Error: Keypool ran out, please call keypoolrefill first").translated; error = _("Error: Keypool ran out, please call keypoolrefill first");
return false; return false;
} }
LearnRelatedScripts(new_key, type); LearnRelatedScripts(new_key, type);
@ -295,22 +295,22 @@ bool LegacyScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, WalletBat
return true; return true;
} }
bool LegacyScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, std::string& error) bool LegacyScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error)
{ {
if (LEGACY_OUTPUT_TYPES.count(type) == 0) { if (LEGACY_OUTPUT_TYPES.count(type) == 0) {
error = _("Error: Legacy wallets only support the \"legacy\", \"p2sh-segwit\", and \"bech32\" address types").translated; error = _("Error: Legacy wallets only support the \"legacy\", \"p2sh-segwit\", and \"bech32\" address types");
return false; return false;
} }
assert(type != OutputType::BECH32M); assert(type != OutputType::BECH32M);
LOCK(cs_KeyStore); LOCK(cs_KeyStore);
if (!CanGetAddresses(internal)) { if (!CanGetAddresses(internal)) {
error = _("Error: Keypool ran out, please call keypoolrefill first").translated; error = _("Error: Keypool ran out, please call keypoolrefill first");
return false; return false;
} }
if (!ReserveKeyFromKeyPool(index, keypool, internal)) { if (!ReserveKeyFromKeyPool(index, keypool, internal)) {
error = _("Error: Keypool ran out, please call keypoolrefill first").translated; error = _("Error: Keypool ran out, please call keypoolrefill first");
return false; return false;
} }
address = GetDestinationForKey(keypool.vchPubKey, type); address = GetDestinationForKey(keypool.vchPubKey, type);
@ -1613,11 +1613,11 @@ std::set<CKeyID> LegacyScriptPubKeyMan::GetKeys() const
return set_address; return set_address;
} }
bool DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type, CTxDestination& dest, std::string& error) bool DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type, CTxDestination& dest, bilingual_str& error)
{ {
// Returns true if this descriptor supports getting new addresses. Conditions where we may be unable to fetch them (e.g. locked) are caught later // 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()) { if (!CanGetAddresses()) {
error = "No addresses available"; error = _("No addresses available");
return false; return false;
} }
{ {
@ -1636,12 +1636,12 @@ bool DescriptorScriptPubKeyMan::GetNewDestination(const OutputType type, CTxDest
std::vector<CScript> scripts_temp; std::vector<CScript> scripts_temp;
if (m_wallet_descriptor.range_end <= m_max_cached_index && !TopUp(1)) { if (m_wallet_descriptor.range_end <= m_max_cached_index && !TopUp(1)) {
// We can't generate anymore keys // We can't generate anymore keys
error = "Error: Keypool ran out, please call keypoolrefill first"; error = _("Error: Keypool ran out, please call keypoolrefill first");
return false; return false;
} }
if (!m_wallet_descriptor.descriptor->ExpandFromCache(m_wallet_descriptor.next_index, m_wallet_descriptor.cache, scripts_temp, out_keys)) { 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 // We can't generate anymore keys
error = "Error: Keypool ran out, please call keypoolrefill first"; error = _("Error: Keypool ran out, please call keypoolrefill first");
return false; return false;
} }
@ -1721,7 +1721,7 @@ bool DescriptorScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, Walle
return true; return true;
} }
bool DescriptorScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, std::string& error) bool DescriptorScriptPubKeyMan::GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error)
{ {
LOCK(cs_desc_man); LOCK(cs_desc_man);
bool result = GetNewDestination(type, address, error); bool result = GetNewDestination(type, address, error);

View File

@ -174,14 +174,14 @@ protected:
public: public:
explicit ScriptPubKeyMan(WalletStorage& storage) : m_storage(storage) {} explicit ScriptPubKeyMan(WalletStorage& storage) : m_storage(storage) {}
virtual ~ScriptPubKeyMan() {}; virtual ~ScriptPubKeyMan() {};
virtual bool GetNewDestination(const OutputType type, CTxDestination& dest, std::string& error) { return false; } virtual bool GetNewDestination(const OutputType type, CTxDestination& dest, bilingual_str& error) { return false; }
virtual isminetype IsMine(const CScript& script) const { return ISMINE_NO; } 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. //! Check that the given decryption key is valid for this ScriptPubKeyMan, i.e. it decrypts all of the keys handled by it.
virtual bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) { return false; } virtual bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) { return false; }
virtual bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) { return false; } virtual bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) { return false; }
virtual bool GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, std::string& error) { return false; } virtual bool GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error) { return false; }
virtual void KeepDestination(int64_t index, const OutputType& type) {} virtual void KeepDestination(int64_t index, const OutputType& type) {}
virtual void ReturnDestination(int64_t index, bool internal, const CTxDestination& addr) {} virtual void ReturnDestination(int64_t index, bool internal, const CTxDestination& addr) {}
@ -355,13 +355,13 @@ private:
public: public:
using ScriptPubKeyMan::ScriptPubKeyMan; using ScriptPubKeyMan::ScriptPubKeyMan;
bool GetNewDestination(const OutputType type, CTxDestination& dest, std::string& error) override; bool GetNewDestination(const OutputType type, CTxDestination& dest, bilingual_str& error) override;
isminetype IsMine(const CScript& script) const override; isminetype IsMine(const CScript& script) const override;
bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) override; bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) override;
bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) override; bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) override;
bool GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, std::string& error) override; bool GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error) override;
void KeepDestination(int64_t index, const OutputType& type) override; void KeepDestination(int64_t index, const OutputType& type) override;
void ReturnDestination(int64_t index, bool internal, const CTxDestination&) override; void ReturnDestination(int64_t index, bool internal, const CTxDestination&) override;
@ -559,13 +559,13 @@ public:
mutable RecursiveMutex cs_desc_man; mutable RecursiveMutex cs_desc_man;
bool GetNewDestination(const OutputType type, CTxDestination& dest, std::string& error) override; bool GetNewDestination(const OutputType type, CTxDestination& dest, bilingual_str& error) override;
isminetype IsMine(const CScript& script) const override; isminetype IsMine(const CScript& script) const override;
bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) override; bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) override;
bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) override; bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) override;
bool GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, std::string& error) override; bool GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool, bilingual_str& error) override;
void ReturnDestination(int64_t index, bool internal, const CTxDestination& addr) override; void ReturnDestination(int64_t index, bool internal, const CTxDestination& addr) override;
// Tops up the descriptor cache and m_map_script_pub_keys. The cache is stored in the wallet file // Tops up the descriptor cache and m_map_script_pub_keys. The cache is stored in the wallet file

View File

@ -618,9 +618,9 @@ bool CWallet::CreateTransactionInternal(
// Reserve a new key pair from key pool. If it fails, provide a dummy // Reserve a new key pair from key pool. If it fails, provide a dummy
// destination in case we don't need change. // destination in case we don't need change.
CTxDestination dest; CTxDestination dest;
std::string dest_err; bilingual_str dest_err;
if (!reservedest.GetReservedDestination(dest, true, dest_err)) { if (!reservedest.GetReservedDestination(dest, true, dest_err)) {
error = strprintf(_("Transaction needs a change address, but we can't generate it. %s"), dest_err); error = _("Transaction needs a change address, but we can't generate it.") + Untranslated(" ") + dest_err;
} }
scriptChange = GetScriptForDestination(dest); scriptChange = GetScriptForDestination(dest);
// A valid destination implies a change script (and // A valid destination implies a change script (and

View File

@ -7,6 +7,7 @@
#include <primitives/transaction.h> #include <primitives/transaction.h>
#include <random.h> #include <random.h>
#include <test/util/setup_common.h> #include <test/util/setup_common.h>
#include <util/translation.h>
#include <wallet/coincontrol.h> #include <wallet/coincontrol.h>
#include <wallet/coinselection.h> #include <wallet/coinselection.h>
#include <wallet/test/wallet_test_fixture.h> #include <wallet/test/wallet_test_fixture.h>
@ -66,7 +67,7 @@ static void add_coin(CWallet& wallet, const CAmount& nValue, int nAge = 6*24, bo
tx.vout[nInput].nValue = nValue; tx.vout[nInput].nValue = nValue;
if (spendable) { if (spendable) {
CTxDestination dest; CTxDestination dest;
std::string error; bilingual_str error;
const bool destination_ok = wallet.GetNewDestination(OutputType::BECH32, "", dest, error); const bool destination_ok = wallet.GetNewDestination(OutputType::BECH32, "", dest, error);
assert(destination_ok); assert(destination_ok);
tx.vout[nInput].scriptPubKey = GetScriptForDestination(dest); tx.vout[nInput].scriptPubKey = GetScriptForDestination(dest);

View File

@ -601,7 +601,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_disableprivkeys, TestChain100Setup)
wallet->SetWalletFlag(WALLET_FLAG_DISABLE_PRIVATE_KEYS); wallet->SetWalletFlag(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
BOOST_CHECK(!wallet->TopUpKeyPool(1000)); BOOST_CHECK(!wallet->TopUpKeyPool(1000));
CTxDestination dest; CTxDestination dest;
std::string error; bilingual_str error;
BOOST_CHECK(!wallet->GetNewDestination(OutputType::BECH32, "", dest, error)); BOOST_CHECK(!wallet->GetNewDestination(OutputType::BECH32, "", dest, error));
} }

View File

@ -2128,7 +2128,7 @@ bool CWallet::TopUpKeyPool(unsigned int kpSize)
return res; return res;
} }
bool CWallet::GetNewDestination(const OutputType type, const std::string label, CTxDestination& dest, std::string& error) bool CWallet::GetNewDestination(const OutputType type, const std::string label, CTxDestination& dest, bilingual_str& error)
{ {
LOCK(cs_wallet); LOCK(cs_wallet);
error.clear(); error.clear();
@ -2138,7 +2138,7 @@ bool CWallet::GetNewDestination(const OutputType type, const std::string label,
spk_man->TopUp(); spk_man->TopUp();
result = spk_man->GetNewDestination(type, dest, error); result = spk_man->GetNewDestination(type, dest, error);
} else { } else {
error = strprintf(_("Error: No %s addresses available."), FormatOutputType(type)).translated; error = strprintf(_("Error: No %s addresses available."), FormatOutputType(type));
} }
if (result) { if (result) {
SetAddressBook(dest, label, "receive"); SetAddressBook(dest, label, "receive");
@ -2147,7 +2147,7 @@ bool CWallet::GetNewDestination(const OutputType type, const std::string label,
return result; return result;
} }
bool CWallet::GetNewChangeDestination(const OutputType type, CTxDestination& dest, std::string& error) bool CWallet::GetNewChangeDestination(const OutputType type, CTxDestination& dest, bilingual_str& error)
{ {
LOCK(cs_wallet); LOCK(cs_wallet);
error.clear(); error.clear();
@ -2200,11 +2200,11 @@ std::set<CTxDestination> CWallet::GetLabelAddresses(const std::string& label) co
return result; return result;
} }
bool ReserveDestination::GetReservedDestination(CTxDestination& dest, bool internal, std::string& error) bool ReserveDestination::GetReservedDestination(CTxDestination& dest, bool internal, bilingual_str& error)
{ {
m_spk_man = pwallet->GetScriptPubKeyMan(type, internal); m_spk_man = pwallet->GetScriptPubKeyMan(type, internal);
if (!m_spk_man) { if (!m_spk_man) {
error = strprintf(_("Error: No %s addresses available."), FormatOutputType(type)).translated; error = strprintf(_("Error: No %s addresses available."), FormatOutputType(type));
return false; return false;
} }

View File

@ -183,7 +183,7 @@ public:
} }
//! Reserve an address //! Reserve an address
bool GetReservedDestination(CTxDestination& pubkey, bool internal, std::string& error); bool GetReservedDestination(CTxDestination& pubkey, bool internal, bilingual_str& error);
//! Return reserved address //! Return reserved address
void ReturnDestination(); void ReturnDestination();
//! Keep the address. Do not return it's key to the keypool when this object goes out of scope //! Keep the address. Do not return it's key to the keypool when this object goes out of scope
@ -665,8 +665,8 @@ public:
*/ */
void MarkDestinationsDirty(const std::set<CTxDestination>& destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); void MarkDestinationsDirty(const std::set<CTxDestination>& destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
bool GetNewDestination(const OutputType type, const std::string label, CTxDestination& dest, std::string& error); bool GetNewDestination(const OutputType type, const std::string label, CTxDestination& dest, bilingual_str& error);
bool GetNewChangeDestination(const OutputType type, CTxDestination& dest, std::string& error); bool GetNewChangeDestination(const OutputType type, CTxDestination& dest, bilingual_str& error);
isminetype IsMine(const CTxDestination& dest) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); isminetype IsMine(const CTxDestination& dest) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
isminetype IsMine(const CScript& script) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); isminetype IsMine(const CScript& script) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);