diff options
| author | geekwisdom <[email protected]> | 2021-05-20 07:24:54 -0300 |
|---|---|---|
| committer | Ross Nicoll <[email protected]> | 2021-07-25 19:32:23 +0100 |
| commit | c7a1b7c90cd6981321924991d7bd05f77a3cc7db (patch) | |
| tree | 4154255d908aea27914c24f6b0e2a67abca7f21e /src/pubkey.h | |
| parent | Merge pull request #1941 from elybin/fixing-indonesian-translation (diff) | |
| download | discoin-c7a1b7c90cd6981321924991d7bd05f77a3cc7db.tar.xz discoin-c7a1b7c90cd6981321924991d7bd05f77a3cc7db.zip | |
Trivial: Fix Magic Numbers in key and pubkey - fixes #1968
Diffstat (limited to 'src/pubkey.h')
| -rw-r--r-- | src/pubkey.h | 56 |
1 files changed, 33 insertions, 23 deletions
diff --git a/src/pubkey.h b/src/pubkey.h index 490e8072a..d78374d98 100644 --- a/src/pubkey.h +++ b/src/pubkey.h @@ -38,32 +38,16 @@ typedef uint256 ChainCode; /** An encapsulated public key. */ class CPubKey { -private: - - /** - * Just store the serialized data. - * Its length can very cheaply be computed from the first byte. - */ - unsigned char vch[65]; +public: + //! Construct an invalid public key. - //! Compute the length of a pubkey with a given first byte. - unsigned int static GetLen(unsigned char chHeader) - { - if (chHeader == 2 || chHeader == 3) - return 33; - if (chHeader == 4 || chHeader == 6 || chHeader == 7) - return 65; - return 0; - } + static constexpr unsigned int SIZE = 65; + static constexpr unsigned int COMPRESSED_SIZE = 33; - //! Set this key data to be invalid - void Invalidate() - { - vch[0] = 0xFF; + bool static ValidSize(const std::vector<unsigned char> &vch) { + return vch.size() > 0 && GetLen(vch[0]) == vch.size(); } -public: - //! Construct an invalid public key. CPubKey() { Invalidate(); @@ -127,7 +111,7 @@ public: void Unserialize(Stream& s) { unsigned int len = ::ReadCompactSize(s); - if (len <= 65) { + if (len <= SIZE) { s.read((char*)vch, len); } else { // invalid pubkey, skip available data @@ -191,6 +175,32 @@ public: //! Derive BIP32 child pubkey. bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const; + +private: + + /** + * Just store the serialized data. + * Its length can very cheaply be computed from the first byte. + */ + unsigned char vch[SIZE]; + + //! Compute the length of a pubkey with a given first byte. + unsigned int static GetLen(unsigned char chHeader) + { + if (chHeader == 2 || chHeader == 3) + return CPubKey::COMPRESSED_SIZE; + if (chHeader == 4 || chHeader == 6 || chHeader == 7) + return CPubKey::SIZE; + return 0; + } + + //! Set this key data to be invalid + void Invalidate() + { + vch[0] = 0xFF; + } + + }; struct CExtPubKey { |