From c5c63b8e4f3fbdb6b5a423a39d6e318fecab991f Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Thu, 25 Jul 2019 11:58:14 -0400 Subject: [PATCH] Implement operator< for KeyOriginInfo and CExtPubKey --- src/pubkey.h | 15 +++++++++++++++ src/script/keyorigin.h | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/pubkey.h b/src/pubkey.h index f37e24bc47186..204e96f49e5f3 100644 --- a/src/pubkey.h +++ b/src/pubkey.h @@ -130,6 +130,11 @@ class CPubKey return a.vch[0] < b.vch[0] || (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0); } + friend bool operator>(const CPubKey& a, const CPubKey& b) + { + return a.vch[0] > b.vch[0] || + (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) > 0); + } //! Implement serialization, as if this was a byte vector. template @@ -305,6 +310,16 @@ struct CExtPubKey { return !(a == b); } + friend bool operator<(const CExtPubKey &a, const CExtPubKey &b) + { + if (a.pubkey < b.pubkey) { + return true; + } else if (a.pubkey > b.pubkey) { + return false; + } + 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; diff --git a/src/script/keyorigin.h b/src/script/keyorigin.h index 210395d1775f3..c779872be2c4d 100644 --- a/src/script/keyorigin.h +++ b/src/script/keyorigin.h @@ -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; + } + SERIALIZE_METHODS(KeyOriginInfo, obj) { READWRITE(obj.fingerprint, obj.path); } void clear()