mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-19 05:45:05 +01:00
wallet: return and display signer error
Both RPC and GUI now render a useful error message instead of (silently) failing. Replace bool with util::Result<void> to clarify that this either succeeds or returns an error message.
This commit is contained in:
parent
dc55531087
commit
4357158c47
@ -127,7 +127,7 @@ public:
|
|||||||
virtual bool setAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& value) = 0;
|
virtual bool setAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& value) = 0;
|
||||||
|
|
||||||
//! Display address on external signer
|
//! Display address on external signer
|
||||||
virtual bool displayAddress(const CTxDestination& dest) = 0;
|
virtual util::Result<void> displayAddress(const CTxDestination& dest) = 0;
|
||||||
|
|
||||||
//! Lock coin.
|
//! Lock coin.
|
||||||
virtual bool lockCoin(const COutPoint& output, const bool write_to_db) = 0;
|
virtual bool lockCoin(const COutPoint& output, const bool write_to_db) = 0;
|
||||||
|
@ -569,16 +569,17 @@ bool WalletModel::bumpFee(uint256 hash, uint256& new_hash)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WalletModel::displayAddress(std::string sAddress) const
|
void WalletModel::displayAddress(std::string sAddress) const
|
||||||
{
|
{
|
||||||
CTxDestination dest = DecodeDestination(sAddress);
|
CTxDestination dest = DecodeDestination(sAddress);
|
||||||
bool res = false;
|
|
||||||
try {
|
try {
|
||||||
res = m_wallet->displayAddress(dest);
|
util::Result<void> result = m_wallet->displayAddress(dest);
|
||||||
|
if (!result) {
|
||||||
|
QMessageBox::warning(nullptr, tr("Signer error"), QString::fromStdString(util::ErrorString(result).translated));
|
||||||
|
}
|
||||||
} catch (const std::runtime_error& e) {
|
} catch (const std::runtime_error& e) {
|
||||||
QMessageBox::critical(nullptr, tr("Can't display address"), e.what());
|
QMessageBox::critical(nullptr, tr("Can't display address"), e.what());
|
||||||
}
|
}
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WalletModel::isWalletEnabled()
|
bool WalletModel::isWalletEnabled()
|
||||||
|
@ -130,7 +130,7 @@ public:
|
|||||||
UnlockContext requestUnlock();
|
UnlockContext requestUnlock();
|
||||||
|
|
||||||
bool bumpFee(uint256 hash, uint256& new_hash);
|
bool bumpFee(uint256 hash, uint256& new_hash);
|
||||||
bool displayAddress(std::string sAddress) const;
|
void displayAddress(std::string sAddress) const;
|
||||||
|
|
||||||
static bool isWalletEnabled();
|
static bool isWalletEnabled();
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <univalue.h>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -52,7 +53,7 @@ ExternalSigner ExternalSignerScriptPubKeyMan::GetExternalSigner() {
|
|||||||
return signers[0];
|
return signers[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExternalSignerScriptPubKeyMan::DisplayAddress(const CTxDestination& dest, const ExternalSigner &signer) const
|
util::Result<void> ExternalSignerScriptPubKeyMan::DisplayAddress(const CTxDestination& dest, const ExternalSigner &signer) const
|
||||||
{
|
{
|
||||||
// TODO: avoid the need to infer a descriptor from inside a descriptor wallet
|
// TODO: avoid the need to infer a descriptor from inside a descriptor wallet
|
||||||
const CScript& scriptPubKey = GetScriptForDestination(dest);
|
const CScript& scriptPubKey = GetScriptForDestination(dest);
|
||||||
@ -61,10 +62,17 @@ bool ExternalSignerScriptPubKeyMan::DisplayAddress(const CTxDestination& dest, c
|
|||||||
|
|
||||||
const UniValue& result = signer.DisplayAddress(descriptor->ToString());
|
const UniValue& result = signer.DisplayAddress(descriptor->ToString());
|
||||||
|
|
||||||
const UniValue& ret_address = result.find_value("address");
|
const UniValue& error = result.find_value("error");
|
||||||
if (!ret_address.isStr()) return false;
|
if (error.isStr()) return util::Error{strprintf(_("Signer returned error: %s"), error.getValStr())};
|
||||||
|
|
||||||
return ret_address.getValStr() == EncodeDestination(dest);
|
const UniValue& ret_address = result.find_value("address");
|
||||||
|
if (!ret_address.isStr()) return util::Error{_("Signer did not echo address")};
|
||||||
|
|
||||||
|
if (ret_address.getValStr() != EncodeDestination(dest)) {
|
||||||
|
return util::Error{strprintf(_("Signer echoed unexpected address %s"), ret_address.getValStr())};
|
||||||
|
}
|
||||||
|
|
||||||
|
return util::Result<void>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// If sign is true, transaction must previously have been filled
|
// If sign is true, transaction must previously have been filled
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
struct bilingual_str;
|
||||||
|
|
||||||
namespace wallet {
|
namespace wallet {
|
||||||
class ExternalSignerScriptPubKeyMan : public DescriptorScriptPubKeyMan
|
class ExternalSignerScriptPubKeyMan : public DescriptorScriptPubKeyMan
|
||||||
{
|
{
|
||||||
@ -27,7 +29,11 @@ class ExternalSignerScriptPubKeyMan : public DescriptorScriptPubKeyMan
|
|||||||
|
|
||||||
static ExternalSigner GetExternalSigner();
|
static ExternalSigner GetExternalSigner();
|
||||||
|
|
||||||
bool DisplayAddress(const CTxDestination& dest, const ExternalSigner &signer) const;
|
/**
|
||||||
|
* Display address on the device and verify that the returned value matches.
|
||||||
|
* @returns nothing or an error message
|
||||||
|
*/
|
||||||
|
util::Result<void> DisplayAddress(const CTxDestination& dest, const ExternalSigner& signer) const;
|
||||||
|
|
||||||
TransactionError FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, int sighash_type = 1 /* SIGHASH_ALL */, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr, bool finalize = true) const override;
|
TransactionError FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, int sighash_type = 1 /* SIGHASH_ALL */, bool sign = true, bool bip32derivs = false, int* n_signed = nullptr, bool finalize = true) const override;
|
||||||
};
|
};
|
||||||
|
@ -247,7 +247,7 @@ public:
|
|||||||
return value.empty() ? m_wallet->EraseAddressReceiveRequest(batch, dest, id)
|
return value.empty() ? m_wallet->EraseAddressReceiveRequest(batch, dest, id)
|
||||||
: m_wallet->SetAddressReceiveRequest(batch, dest, id, value);
|
: m_wallet->SetAddressReceiveRequest(batch, dest, id, value);
|
||||||
}
|
}
|
||||||
bool displayAddress(const CTxDestination& dest) override
|
util::Result<void> displayAddress(const CTxDestination& dest) override
|
||||||
{
|
{
|
||||||
LOCK(m_wallet->cs_wallet);
|
LOCK(m_wallet->cs_wallet);
|
||||||
return m_wallet->DisplayAddress(dest);
|
return m_wallet->DisplayAddress(dest);
|
||||||
|
@ -789,9 +789,8 @@ RPCHelpMan walletdisplayaddress()
|
|||||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
|
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!pwallet->DisplayAddress(dest)) {
|
util::Result<void> res = pwallet->DisplayAddress(dest);
|
||||||
throw JSONRPCError(RPC_MISC_ERROR, "Failed to display address");
|
if (!res) throw JSONRPCError(RPC_MISC_ERROR, util::ErrorString(res).original);
|
||||||
}
|
|
||||||
|
|
||||||
UniValue result(UniValue::VOBJ);
|
UniValue result(UniValue::VOBJ);
|
||||||
result.pushKV("address", request.params[0].get_str());
|
result.pushKV("address", request.params[0].get_str());
|
||||||
|
@ -27,7 +27,6 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
enum class OutputType;
|
enum class OutputType;
|
||||||
struct bilingual_str;
|
|
||||||
|
|
||||||
namespace wallet {
|
namespace wallet {
|
||||||
struct MigrationData;
|
struct MigrationData;
|
||||||
|
@ -2667,7 +2667,7 @@ void ReserveDestination::ReturnDestination()
|
|||||||
address = CNoDestination();
|
address = CNoDestination();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CWallet::DisplayAddress(const CTxDestination& dest)
|
util::Result<void> CWallet::DisplayAddress(const CTxDestination& dest)
|
||||||
{
|
{
|
||||||
CScript scriptPubKey = GetScriptForDestination(dest);
|
CScript scriptPubKey = GetScriptForDestination(dest);
|
||||||
for (const auto& spk_man : GetScriptPubKeyMans(scriptPubKey)) {
|
for (const auto& spk_man : GetScriptPubKeyMans(scriptPubKey)) {
|
||||||
@ -2678,7 +2678,7 @@ bool CWallet::DisplayAddress(const CTxDestination& dest)
|
|||||||
ExternalSigner signer = ExternalSignerScriptPubKeyMan::GetExternalSigner();
|
ExternalSigner signer = ExternalSignerScriptPubKeyMan::GetExternalSigner();
|
||||||
return signer_spk_man->DisplayAddress(dest, signer);
|
return signer_spk_man->DisplayAddress(dest, signer);
|
||||||
}
|
}
|
||||||
return false;
|
return util::Error{_("There is no ScriptPubKeyManager for this address")};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CWallet::LockCoin(const COutPoint& output, WalletBatch* batch)
|
bool CWallet::LockCoin(const COutPoint& output, WalletBatch* batch)
|
||||||
|
@ -537,11 +537,8 @@ public:
|
|||||||
bool IsSpentKey(const CScript& scriptPubKey) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
bool IsSpentKey(const CScript& scriptPubKey) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||||
void SetSpentKeyState(WalletBatch& batch, const uint256& hash, unsigned int n, bool used, std::set<CTxDestination>& tx_destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
void SetSpentKeyState(WalletBatch& batch, const uint256& hash, unsigned int n, bool used, std::set<CTxDestination>& tx_destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||||
|
|
||||||
/** Display address on an external signer.
|
/** Display address on an external signer. */
|
||||||
* Returns false if the signer does not respond with a matching address.
|
util::Result<void> DisplayAddress(const CTxDestination& dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||||
* Returns false if external signer support is not compiled.
|
|
||||||
*/
|
|
||||||
bool DisplayAddress(const CTxDestination& dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
|
||||||
|
|
||||||
bool IsLockedCoin(const COutPoint& output) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
bool IsLockedCoin(const COutPoint& output) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||||
bool LockCoin(const COutPoint& output, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
bool LockCoin(const COutPoint& output, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||||
|
@ -49,6 +49,7 @@ def displayaddress(args):
|
|||||||
"sh(wpkh([00000001/49h/1h/0h/0/0]02c97dc3f4420402e01a113984311bf4a1b8de376cac0bdcfaf1b3ac81f13433c7))#kz9y5w82": "2N2gQKzjUe47gM8p1JZxaAkTcoHPXV6YyVp",
|
"sh(wpkh([00000001/49h/1h/0h/0/0]02c97dc3f4420402e01a113984311bf4a1b8de376cac0bdcfaf1b3ac81f13433c7))#kz9y5w82": "2N2gQKzjUe47gM8p1JZxaAkTcoHPXV6YyVp",
|
||||||
"pkh([00000001/44h/1h/0h/0/0]02c97dc3f4420402e01a113984311bf4a1b8de376cac0bdcfaf1b3ac81f13433c7)#q3pqd8wh": "n1LKejAadN6hg2FrBXoU1KrwX4uK16mco9",
|
"pkh([00000001/44h/1h/0h/0/0]02c97dc3f4420402e01a113984311bf4a1b8de376cac0bdcfaf1b3ac81f13433c7)#q3pqd8wh": "n1LKejAadN6hg2FrBXoU1KrwX4uK16mco9",
|
||||||
"tr([00000001/86h/1h/0h/0/0]c97dc3f4420402e01a113984311bf4a1b8de376cac0bdcfaf1b3ac81f13433c7)#puqqa90m": "tb1phw4cgpt6cd30kz9k4wkpwm872cdvhss29jga2xpmftelhqll62mscq0k4g",
|
"tr([00000001/86h/1h/0h/0/0]c97dc3f4420402e01a113984311bf4a1b8de376cac0bdcfaf1b3ac81f13433c7)#puqqa90m": "tb1phw4cgpt6cd30kz9k4wkpwm872cdvhss29jga2xpmftelhqll62mscq0k4g",
|
||||||
|
"wpkh([00000001/84h/1h/0h/0/1]03a20a46308be0b8ded6dff0a22b10b4245c587ccf23f3b4a303885be3a524f172)#aqpjv5xr": "wrong_address",
|
||||||
}
|
}
|
||||||
if args.desc not in expected_desc:
|
if args.desc not in expected_desc:
|
||||||
return sys.stdout.write(json.dumps({"error": "Unexpected descriptor", "desc": args.desc}))
|
return sys.stdout.write(json.dumps({"error": "Unexpected descriptor", "desc": args.desc}))
|
||||||
|
@ -141,6 +141,13 @@ class WalletSignerTest(BitcoinTestFramework):
|
|||||||
)
|
)
|
||||||
self.clear_mock_result(self.nodes[1])
|
self.clear_mock_result(self.nodes[1])
|
||||||
|
|
||||||
|
# Returned address MUST match:
|
||||||
|
address_fail = hww.getnewaddress(address_type="bech32")
|
||||||
|
assert_equal(address_fail, "bcrt1ql7zg7ukh3dwr25ex2zn9jse926f27xy2jz58tm")
|
||||||
|
assert_raises_rpc_error(-1, 'Signer echoed unexpected address wrong_address',
|
||||||
|
hww.walletdisplayaddress, address_fail
|
||||||
|
)
|
||||||
|
|
||||||
self.log.info('Prepare mock PSBT')
|
self.log.info('Prepare mock PSBT')
|
||||||
self.nodes[0].sendtoaddress(address4, 1)
|
self.nodes[0].sendtoaddress(address4, 1)
|
||||||
self.generate(self.nodes[0], 1)
|
self.generate(self.nodes[0], 1)
|
||||||
|
Loading…
Reference in New Issue
Block a user