Changeset View
Changeset View
Standalone View
Standalone View
src/pubkey.h
Show All 27 Lines | |||||
typedef uint256 ChainCode; | typedef uint256 ChainCode; | ||||
/** An encapsulated public key. */ | /** An encapsulated public key. */ | ||||
class CPubKey { | class CPubKey { | ||||
public: | public: | ||||
/** | /** | ||||
* secp256k1: | * secp256k1: | ||||
*/ | */ | ||||
static constexpr unsigned int PUBLIC_KEY_SIZE = 65; | static constexpr unsigned int SIZE = 65; | ||||
static constexpr unsigned int COMPRESSED_PUBLIC_KEY_SIZE = 33; | static constexpr unsigned int COMPRESSED_SIZE = 33; | ||||
static constexpr unsigned int SIGNATURE_SIZE = 72; | static constexpr unsigned int SIGNATURE_SIZE = 72; | ||||
static constexpr unsigned int COMPACT_SIGNATURE_SIZE = 65; | static constexpr unsigned int COMPACT_SIGNATURE_SIZE = 65; | ||||
/** | /** | ||||
* see www.keylength.com | * see www.keylength.com | ||||
* script supports up to 75 for single byte push | * script supports up to 75 for single byte push | ||||
*/ | */ | ||||
static_assert(PUBLIC_KEY_SIZE >= COMPRESSED_PUBLIC_KEY_SIZE, | static_assert(SIZE >= COMPRESSED_SIZE, | ||||
"COMPRESSED_PUBLIC_KEY_SIZE is larger than PUBLIC_KEY_SIZE"); | "COMPRESSED_SIZE is larger than SIZE"); | ||||
private: | private: | ||||
/** | /** | ||||
* Just store the serialized data. | * Just store the serialized data. | ||||
* Its length can very cheaply be computed from the first byte. | * Its length can very cheaply be computed from the first byte. | ||||
*/ | */ | ||||
uint8_t vch[PUBLIC_KEY_SIZE]; | uint8_t vch[SIZE]; | ||||
//! Compute the length of a pubkey with a given first byte. | //! Compute the length of a pubkey with a given first byte. | ||||
static unsigned int GetLen(uint8_t chHeader) { | static unsigned int GetLen(uint8_t chHeader) { | ||||
if (chHeader == 2 || chHeader == 3) { | if (chHeader == 2 || chHeader == 3) { | ||||
return COMPRESSED_PUBLIC_KEY_SIZE; | return COMPRESSED_SIZE; | ||||
} | } | ||||
if (chHeader == 4 || chHeader == 6 || chHeader == 7) { | if (chHeader == 4 || chHeader == 6 || chHeader == 7) { | ||||
return PUBLIC_KEY_SIZE; | return SIZE; | ||||
} | } | ||||
return 0; | return 0; | ||||
} | } | ||||
//! Set this key data to be invalid | //! Set this key data to be invalid | ||||
void Invalidate() { vch[0] = 0xFF; } | void Invalidate() { vch[0] = 0xFF; } | ||||
public: | public: | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | public: | ||||
//! Implement serialization, as if this was a byte vector. | //! Implement serialization, as if this was a byte vector. | ||||
template <typename Stream> void Serialize(Stream &s) const { | template <typename Stream> void Serialize(Stream &s) const { | ||||
unsigned int len = size(); | unsigned int len = size(); | ||||
::WriteCompactSize(s, len); | ::WriteCompactSize(s, len); | ||||
s.write((char *)vch, len); | s.write((char *)vch, len); | ||||
} | } | ||||
template <typename Stream> void Unserialize(Stream &s) { | template <typename Stream> void Unserialize(Stream &s) { | ||||
unsigned int len = ::ReadCompactSize(s); | unsigned int len = ::ReadCompactSize(s); | ||||
if (len <= PUBLIC_KEY_SIZE) { | if (len <= SIZE) { | ||||
s.read((char *)vch, len); | s.read((char *)vch, len); | ||||
} else { | } else { | ||||
// invalid pubkey, skip available data | // invalid pubkey, skip available data | ||||
char dummy; | char dummy; | ||||
while (len--) { | while (len--) { | ||||
s.read(&dummy, 1); | s.read(&dummy, 1); | ||||
} | } | ||||
Invalidate(); | Invalidate(); | ||||
Show All 13 Lines | public: | ||||
*/ | */ | ||||
bool IsValid() const { return size() > 0; } | bool IsValid() const { return size() > 0; } | ||||
//! fully validate whether this is a valid public key (more expensive than | //! fully validate whether this is a valid public key (more expensive than | ||||
//! IsValid()) | //! IsValid()) | ||||
bool IsFullyValid() const; | bool IsFullyValid() const; | ||||
//! Check whether this is a compressed public key. | //! Check whether this is a compressed public key. | ||||
bool IsCompressed() const { return size() == COMPRESSED_PUBLIC_KEY_SIZE; } | bool IsCompressed() const { return size() == COMPRESSED_SIZE; } | ||||
/** | /** | ||||
* Verify a DER-serialized ECDSA signature (~72 bytes). | * Verify a DER-serialized ECDSA signature (~72 bytes). | ||||
* If this public key is not fully valid, the return value will be false. | * If this public key is not fully valid, the return value will be false. | ||||
*/ | */ | ||||
bool VerifyECDSA(const uint256 &hash, | bool VerifyECDSA(const uint256 &hash, | ||||
const std::vector<uint8_t> &vchSig) const; | const std::vector<uint8_t> &vchSig) const; | ||||
▲ Show 20 Lines • Show All 63 Lines • Show Last 20 Lines |