Skip to content

Commit 8ba2f9b

Browse files
committed
refactor: use util::Result for GetExternalSigner()
This commit does not change behavior, except that the error message no longer contains the function name.
1 parent fd4399c commit 8ba2f9b

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

src/wallet/external_signer_scriptpubkeyman.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ bool ExternalSignerScriptPubKeyMan::SetupDescriptor(WalletBatch& batch, std::uni
4545
return true;
4646
}
4747

48-
ExternalSigner ExternalSignerScriptPubKeyMan::GetExternalSigner() {
48+
util::Result<ExternalSigner> ExternalSignerScriptPubKeyMan::GetExternalSigner() {
4949
const std::string command = gArgs.GetArg("-signer", "");
50-
if (command == "") throw std::runtime_error(std::string(__func__) + ": restart bitcoind with -signer=<cmd>");
50+
if (command == "") return util::Error{Untranslated("restart bitcoind with -signer=<cmd>")};
5151
std::vector<ExternalSigner> signers;
5252
ExternalSigner::Enumerate(command, signers, Params().GetChainTypeString());
53-
if (signers.empty()) throw std::runtime_error(std::string(__func__) + ": No external signers found");
53+
if (signers.empty()) return util::Error{Untranslated("No external signers found")};
5454
// TODO: add fingerprint argument instead of failing in case of multiple signers.
55-
if (signers.size() > 1) throw std::runtime_error(std::string(__func__) + ": More than one external signer found. Please connect only one at a time.");
55+
if (signers.size() > 1) return util::Error{Untranslated("More than one external signer found. Please connect only one at a time.")};
5656
return signers[0];
5757
}
5858

@@ -93,9 +93,12 @@ std::optional<PSBTError> ExternalSignerScriptPubKeyMan::FillPSBT(PartiallySigned
9393
}
9494
if (complete) return {};
9595

96-
std::string strFailReason;
97-
if(!GetExternalSigner().SignTransaction(psbt, strFailReason)) {
98-
tfm::format(std::cerr, "Failed to sign: %s\n", strFailReason);
96+
auto signer{GetExternalSigner()};
97+
if (!signer) throw std::runtime_error(util::ErrorString(signer).original);
98+
99+
std::string failure_reason;
100+
if(!signer->SignTransaction(psbt, failure_reason)) {
101+
tfm::format(std::cerr, "Failed to sign: %s\n", failure_reason);
99102
return PSBTError::EXTERNAL_SIGNER_FAILED;
100103
}
101104
if (finalize) FinalizePSBT(psbt); // This won't work in a multisig setup

src/wallet/external_signer_scriptpubkeyman.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <wallet/scriptpubkeyman.h>
99

1010
#include <memory>
11+
#include <util/result.h>
1112

1213
struct bilingual_str;
1314

@@ -27,7 +28,7 @@ class ExternalSignerScriptPubKeyMan : public DescriptorScriptPubKeyMan
2728
*/
2829
bool SetupDescriptor(WalletBatch& batch, std::unique_ptr<Descriptor>desc);
2930

30-
static ExternalSigner GetExternalSigner();
31+
static util::Result<ExternalSigner> GetExternalSigner();
3132

3233
/**
3334
* Display address on the device and verify that the returned value matches.

src/wallet/wallet.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2602,8 +2602,9 @@ util::Result<void> CWallet::DisplayAddress(const CTxDestination& dest)
26022602
if (signer_spk_man == nullptr) {
26032603
continue;
26042604
}
2605-
ExternalSigner signer = ExternalSignerScriptPubKeyMan::GetExternalSigner();
2606-
return signer_spk_man->DisplayAddress(dest, signer);
2605+
auto signer{ExternalSignerScriptPubKeyMan::GetExternalSigner()};
2606+
if (!signer) throw std::runtime_error(util::ErrorString(signer).original);
2607+
return signer_spk_man->DisplayAddress(dest, *signer);
26072608
}
26082609
return util::Error{_("There is no ScriptPubKeyManager for this address")};
26092610
}
@@ -3586,11 +3587,12 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
35863587
return true;
35873588
})) throw std::runtime_error("Error: cannot process db transaction for descriptors setup");
35883589
} else {
3589-
ExternalSigner signer = ExternalSignerScriptPubKeyMan::GetExternalSigner();
3590+
auto signer = ExternalSignerScriptPubKeyMan::GetExternalSigner();
3591+
if (!signer) throw std::runtime_error(util::ErrorString(signer).original);
35903592

35913593
// TODO: add account parameter
35923594
int account = 0;
3593-
UniValue signer_res = signer.GetDescriptors(account);
3595+
UniValue signer_res = signer->GetDescriptors(account);
35943596

35953597
if (!signer_res.isObject()) throw std::runtime_error(std::string(__func__) + ": Unexpected result");
35963598

test/functional/wallet_signer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def test_multiple_signers(self):
243243
self.log.debug(f"-signer={self.mock_multi_signers_path()}")
244244
self.log.info('Test multiple external signers')
245245

246-
assert_raises_rpc_error(-1, "GetExternalSigner: More than one external signer found", self.nodes[1].createwallet, wallet_name='multi_hww', disable_private_keys=True, external_signer=True)
246+
assert_raises_rpc_error(-1, "More than one external signer found", self.nodes[1].createwallet, wallet_name='multi_hww', disable_private_keys=True, external_signer=True)
247247

248248
if __name__ == '__main__':
249249
WalletSignerTest(__file__).main()

0 commit comments

Comments
 (0)