Skip to content

Commit 91c7d66

Browse files
committed
Merge bitcoin/bitcoin#22789: external_signer: improve fingerprint matching logic (stop on first match)
d047ed7 external_signer: improve fingerprint matching logic (stop on first match) (Sebastian Falbesoner) Pull request description: The fingerprint matching logic in `ExternalSigner::SignTransaction` currently always iterates all inputs of a PSBT, even after a match has already been found. I guess the reason for that is not that it was not thought of, but rather the fact that breaking out of a nested loop is simply not possible (at least not without adding ugly constructs like gotos or extra state variables). This PR fixes this by using `std::any_of` from C++'s standard library, see http://www.cplusplus.com/reference/algorithm/any_of/ ACKs for top commit: lsilva01: Code Review ACK bitcoin/bitcoin@d047ed7 Sjors: utACK d047ed7 Zero-1729: crACK d047ed7 mjdietzx: Code review ACK d047ed7 hebasto: ACK d047ed7, I have reviewed the code and it looks OK, I agree it can be merged. Tree-SHA512: 447e7c0c6a5b5549a2c09d52e55ba4146302c1a06e4d96de11f6945d09f98c89129cba221202dff7e0718e01a83dd173b9f19b1f02b6be228978f3f6e35d8096
2 parents a685da5 + d047ed7 commit 91c7d66

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/external_signer.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <util/system.h>
1010
#include <external_signer.h>
1111

12+
#include <algorithm>
1213
#include <stdexcept>
1314
#include <string>
1415
#include <vector>
@@ -75,15 +76,14 @@ bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::str
7576
ssTx << psbtx;
7677

7778
// Check if signer fingerprint matches any input master key fingerprint
78-
bool match = false;
79-
for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
80-
const PSBTInput& input = psbtx.inputs[i];
79+
auto matches_signer_fingerprint = [&](const PSBTInput& input) {
8180
for (const auto& entry : input.hd_keypaths) {
82-
if (m_fingerprint == strprintf("%08x", ReadBE32(entry.second.fingerprint))) match = true;
81+
if (m_fingerprint == strprintf("%08x", ReadBE32(entry.second.fingerprint))) return true;
8382
}
84-
}
83+
return false;
84+
};
8585

86-
if (!match) {
86+
if (!std::any_of(psbtx.inputs.begin(), psbtx.inputs.end(), matches_signer_fingerprint)) {
8787
error = "Signer fingerprint " + m_fingerprint + " does not match any of the inputs:\n" + EncodeBase64(ssTx.str());
8888
return false;
8989
}

0 commit comments

Comments
 (0)