Skip to content

Commit c5c63b8

Browse files
committed
Implement operator< for KeyOriginInfo and CExtPubKey
1 parent d3dbb16 commit c5c63b8

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/pubkey.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ class CPubKey
130130
return a.vch[0] < b.vch[0] ||
131131
(a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
132132
}
133+
friend bool operator>(const CPubKey& a, const CPubKey& b)
134+
{
135+
return a.vch[0] > b.vch[0] ||
136+
(a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) > 0);
137+
}
133138

134139
//! Implement serialization, as if this was a byte vector.
135140
template <typename Stream>
@@ -305,6 +310,16 @@ struct CExtPubKey {
305310
return !(a == b);
306311
}
307312

313+
friend bool operator<(const CExtPubKey &a, const CExtPubKey &b)
314+
{
315+
if (a.pubkey < b.pubkey) {
316+
return true;
317+
} else if (a.pubkey > b.pubkey) {
318+
return false;
319+
}
320+
return a.chaincode < b.chaincode;
321+
}
322+
308323
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
309324
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
310325
void EncodeWithVersion(unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]) const;

src/script/keyorigin.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ struct KeyOriginInfo
1818
return std::equal(std::begin(a.fingerprint), std::end(a.fingerprint), std::begin(b.fingerprint)) && a.path == b.path;
1919
}
2020

21+
friend bool operator<(const KeyOriginInfo& a, const KeyOriginInfo& b)
22+
{
23+
// Compare the fingerprints lexicographically
24+
int fpr_cmp = memcmp(a.fingerprint, b.fingerprint, 4);
25+
if (fpr_cmp < 0) {
26+
return true;
27+
} else if (fpr_cmp > 0) {
28+
return false;
29+
}
30+
// Compare the sizes of the paths, shorter is "less than"
31+
if (a.path.size() < b.path.size()) {
32+
return true;
33+
} else if (a.path.size() > b.path.size()) {
34+
return false;
35+
}
36+
// Paths same length, compare them lexicographically
37+
return a.path < b.path;
38+
}
39+
2140
SERIALIZE_METHODS(KeyOriginInfo, obj) { READWRITE(obj.fingerprint, obj.path); }
2241

2342
void clear()

0 commit comments

Comments
 (0)