Skip to content

Commit

Permalink
Implement operator< for KeyOriginInfo and CExtPubKey
Browse files Browse the repository at this point in the history
Github-Pull: bitcoin#16463
Rebased-From: 391e26f
  • Loading branch information
achow101 authored and luke-jr committed Jun 12, 2020
1 parent 8250efa commit 3e8b22b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/pubkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ struct CExtPubKey {
a.pubkey == b.pubkey;
}

friend bool operator<(const CExtPubKey &a, const CExtPubKey &b)
{
if (a.pubkey < b.pubkey) {
return true;
}
return a.chaincode < b.chaincode;
}

void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
void EncodeWithVersion(unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]) const;
Expand Down
19 changes: 19 additions & 0 deletions src/script/keyorigin.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ struct KeyOriginInfo
return std::equal(std::begin(a.fingerprint), std::end(a.fingerprint), std::begin(b.fingerprint)) && a.path == b.path;
}

friend bool operator<(const KeyOriginInfo& a, const KeyOriginInfo& b)
{
// Compare the fingerprints lexicographically
int fpr_cmp = memcmp(a.fingerprint, b.fingerprint, 4);
if (fpr_cmp < 0) {
return true;
} else if (fpr_cmp > 0) {
return false;
}
// Compare the sizes of the paths, shorter is "less than"
if (a.path.size() < b.path.size()) {
return true;
} else if (a.path.size() > b.path.size()) {
return false;
}
// Paths same length, compare them lexicographically
return a.path < b.path;
}

ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
Expand Down

0 comments on commit 3e8b22b

Please sign in to comment.