Changeset View
Changeset View
Standalone View
Standalone View
src/pubkey.h
Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | 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[65]; | uint8_t vch[65]; | ||||
//! 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) return 33; | if (chHeader == 2 || chHeader == 3) { | ||||
if (chHeader == 4 || chHeader == 6 || chHeader == 7) return 65; | return 33; | ||||
} | |||||
if (chHeader == 4 || chHeader == 6 || chHeader == 7) { | |||||
return 65; | |||||
} | |||||
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: | ||||
//! Construct an invalid public key. | //! Construct an invalid public key. | ||||
CPubKey() { Invalidate(); } | CPubKey() { Invalidate(); } | ||||
//! Initialize a public key using begin/end iterators to byte data. | //! Initialize a public key using begin/end iterators to byte data. | ||||
template <typename T> void Set(const T pbegin, const T pend) { | template <typename T> void Set(const T pbegin, const T pend) { | ||||
int len = pend == pbegin ? 0 : GetLen(pbegin[0]); | int len = pend == pbegin ? 0 : GetLen(pbegin[0]); | ||||
if (len && len == (pend - pbegin)) | if (len && len == (pend - pbegin)) { | ||||
memcpy(vch, (uint8_t *)&pbegin[0], len); | memcpy(vch, (uint8_t *)&pbegin[0], len); | ||||
else | } else { | ||||
Invalidate(); | Invalidate(); | ||||
} | } | ||||
} | |||||
//! Construct a public key using begin/end iterators to byte data. | //! Construct a public key using begin/end iterators to byte data. | ||||
template <typename T> CPubKey(const T pbegin, const T pend) { | template <typename T> CPubKey(const T pbegin, const T pend) { | ||||
Set(pbegin, pend); | Set(pbegin, pend); | ||||
} | } | ||||
//! Construct a public key from a byte vector. | //! Construct a public key from a byte vector. | ||||
explicit CPubKey(const std::vector<uint8_t> &_vch) { | explicit CPubKey(const std::vector<uint8_t> &_vch) { | ||||
Show All 26 Lines | public: | ||||
} | } | ||||
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 <= 65) { | if (len <= 65) { | ||||
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(); | ||||
} | } | ||||
} | } | ||||
//! Get the KeyID of this public key (hash of its serialization) | //! Get the KeyID of this public key (hash of its serialization) | ||||
CKeyID GetID() const { return CKeyID(Hash160(vch, vch + size())); } | CKeyID GetID() const { return CKeyID(Hash160(vch, vch + size())); } | ||||
//! Get the 256-bit hash of this public key. | //! Get the 256-bit hash of this public key. | ||||
▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | template <typename Stream> void Serialize(Stream &s) const { | ||||
unsigned int len = BIP32_EXTKEY_SIZE; | unsigned int len = BIP32_EXTKEY_SIZE; | ||||
::WriteCompactSize(s, len); | ::WriteCompactSize(s, len); | ||||
uint8_t code[BIP32_EXTKEY_SIZE]; | uint8_t code[BIP32_EXTKEY_SIZE]; | ||||
Encode(code); | Encode(code); | ||||
s.write((const char *)&code[0], len); | s.write((const char *)&code[0], 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); | ||||
uint8_t code[BIP32_EXTKEY_SIZE]; | if (len != BIP32_EXTKEY_SIZE) { | ||||
if (len != BIP32_EXTKEY_SIZE) | |||||
throw std::runtime_error("Invalid extended key size\n"); | throw std::runtime_error("Invalid extended key size\n"); | ||||
} | |||||
uint8_t code[BIP32_EXTKEY_SIZE]; | |||||
s.read((char *)&code[0], len); | s.read((char *)&code[0], len); | ||||
Decode(code); | Decode(code); | ||||
} | } | ||||
}; | }; | ||||
/** | /** | ||||
* Users of this module must hold an ECCVerifyHandle. The constructor and | * Users of this module must hold an ECCVerifyHandle. The constructor and | ||||
* destructor of these are not allowed to run in parallel, though. | * destructor of these are not allowed to run in parallel, though. | ||||
Show All 10 Lines |