7 #ifndef BITCOIN_PUBKEY_H 8 #define BITCOIN_PUBKEY_H 14 #include <boost/range/adaptor/sliced.hpp> 36 static constexpr
unsigned int SIZE = 65;
37 static constexpr
unsigned int COMPRESSED_SIZE = 33;
38 static constexpr
unsigned int SCHNORR_SIZE = 64;
39 static constexpr
unsigned int SIGNATURE_SIZE = 72;
40 static constexpr
unsigned int COMPACT_SIGNATURE_SIZE = 65;
45 static_assert(SIZE >= COMPRESSED_SIZE,
46 "COMPRESSED_SIZE is larger than SIZE");
56 static unsigned int GetLen(uint8_t chHeader) {
57 if (chHeader == 2 || chHeader == 3) {
58 return COMPRESSED_SIZE;
60 if (chHeader == 4 || chHeader == 6 || chHeader == 7) {
70 bool static ValidSize(
const std::vector<uint8_t> &vch) {
71 return vch.size() > 0 && GetLen(vch[0]) == vch.size();
78 template <
typename T>
void Set(
const T pbegin,
const T pend) {
79 int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
80 if (len && len == (pend - pbegin)) {
81 memcpy(vch, (uint8_t *)&pbegin[0], len);
88 template <
typename T>
CPubKey(
const T pbegin,
const T pend) {
93 explicit CPubKey(
const std::vector<uint8_t> &_vch) {
94 Set(_vch.begin(), _vch.end());
98 unsigned int size()
const {
return GetLen(vch[0]); }
99 const uint8_t *
data()
const {
return vch; }
100 const uint8_t *
begin()
const {
return vch; }
101 const uint8_t *
end()
const {
return vch +
size(); }
102 const uint8_t &
operator[](
unsigned int pos)
const {
return vch[pos]; }
112 return a.
vch[0] < b.
vch[0] ||
117 template <
typename Stream>
void Serialize(Stream &s)
const {
118 unsigned int len =
size();
120 s.write((
char *)vch, len);
125 s.read((
char *)vch, len);
151 bool IsFullyValid()
const;
160 bool VerifyECDSA(
const uint256 &hash,
161 const std::vector<uint8_t> &vchSig)
const;
167 bool VerifySchnorr(
const uint256 &hash,
168 const std::array<uint8_t, SCHNORR_SIZE> &
sig)
const;
169 bool VerifySchnorr(
const uint256 &hash,
170 const std::vector<uint8_t> &vchSig)
const;
176 CheckLowS(
const boost::sliced_range<
const std::vector<uint8_t>> &vchSig);
177 static bool CheckLowS(
const std::vector<uint8_t> &vchSig) {
178 return CheckLowS(vchSig | boost::adaptors::sliced(0, vchSig.size()));
182 bool RecoverCompact(
const uint256 &hash,
183 const std::vector<uint8_t> &vchSig);
195 uint8_t vchFingerprint[4];
203 sizeof(vchFingerprint)) == 0 &&
213 void Decode(
const uint8_t code[BIP32_EXTKEY_SIZE]);
214 bool Derive(
CExtPubKey &out,
unsigned int nChild)
const;
231 #endif // BITCOIN_PUBKEY_H static bool CheckLowS(const std::vector< uint8_t > &vchSig)
std::array< uint8_t, 64 > sig
const uint8_t * data() const
friend bool operator<(const CPubKey &a, const CPubKey &b)
static bool ValidSize(const std::vector< uint8_t > &vch)
uint64_t ReadCompactSize(Stream &is)
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
static unsigned int GetLen(uint8_t chHeader)
Compute the length of a pubkey with a given first byte.
CKeyID(const uint160 &in)
void Invalidate()
Set this key data to be invalid.
void Set(const T pbegin, const T pend)
Initialize a public key using begin/end iterators to byte data.
uint256 GetHash() const
Get the 256-bit hash of this public key.
void Unserialize(Stream &s)
uint160 Hash160(const T1 pbegin, const T1 pend)
Compute the 160-bit hash an object.
friend bool operator==(const CPubKey &a, const CPubKey &b)
Comparator implementation.
const uint8_t * begin() const
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
void Serialize(Stream &s) const
Implement serialization, as if this was a byte vector.
Users of this module must hold an ECCVerifyHandle.
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
friend bool operator!=(const CExtPubKey &a, const CExtPubKey &b)
CPubKey()
Construct an invalid public key.
An encapsulated public key.
std::pair< std::string, data > Decode(const std::string &str, const std::string &default_prefix)
Decode a cashaddr string.
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
unsigned int size() const
uint8_t vchFingerprint[4]
const unsigned int BIP32_EXTKEY_SIZE
std::string Encode(const std::string &prefix, const data &payload)
Encode a cashaddr string.
CPubKey(const T pbegin, const T pend)
Construct a public key using begin/end iterators to byte data.
A reference to a CKey: the Hash160 of its serialized public key.
CPubKey(const std::vector< uint8_t > &_vch)
Construct a public key from a byte vector.
uint8_t vch[SIZE]
see www.keylength.com script supports up to 75 for single byte push
const uint8_t * end() const
const uint8_t & operator[](unsigned int pos) const
friend bool operator!=(const CPubKey &a, const CPubKey &b)
bool IsCompressed() const
Check whether this is a compressed public key.