7 #ifndef BITCOIN_PUBKEY_H 8 #define BITCOIN_PUBKEY_H 39 static constexpr
unsigned int SIZE = 65;
49 "COMPRESSED_SIZE is larger than SIZE");
60 unsigned int static GetLen(
unsigned char chHeader)
62 if (chHeader == 2 || chHeader == 3)
64 if (chHeader == 4 || chHeader == 6 || chHeader == 7)
89 void Set(
const T pbegin,
const T pend)
91 int len = pend == pbegin ? 0 :
GetLen(pbegin[0]);
92 if (len && len == (pend - pbegin))
93 memcpy(
vch, (
unsigned char*)&pbegin[0], len);
106 explicit CPubKey(std::span<const uint8_t> _vch)
108 Set(_vch.begin(), _vch.end());
113 const unsigned char*
data()
const {
return vch; }
116 const unsigned char&
operator[](
unsigned int pos)
const {
return vch[pos]; }
121 return a.
vch[0] == b.
vch[0] &&
126 return a.
vch[0] < b.
vch[0] ||
131 return a.
vch[0] > b.
vch[0] ||
136 template <
typename Stream>
139 unsigned int len =
size();
141 s << std::span{
vch, len};
143 template <
typename Stream>
148 s >> std::span{
vch, len};
193 return size() > 0 && (
vch[0] == 0x02 ||
vch[0] == 0x03 ||
vch[0] == 0x04);
209 bool Verify(
const uint256& hash,
const std::vector<unsigned char>& vchSig)
const;
214 static bool CheckLowS(
const std::vector<unsigned char>& vchSig);
311 static constexpr
size_t SIZE = 64;
370 #endif // BITCOIN_PUBKEY_H
static constexpr size_t size()
EllSwiftPubKey() noexcept=default
Default constructor creates all-zero pubkey (which is valid).
friend bool operator<(const CExtPubKey &a, const CExtPubKey &b)
static constexpr unsigned int SIZE
secp256k1:
unsigned char vchFingerprint[4]
unsigned static int GetLen(unsigned char chHeader)
Compute the length of a pubkey with a given first byte.
SERIALIZE_METHODS(XOnlyPubKey, obj)
Implement serialization without length prefixes since it is a fixed length.
void DecodeWithVersion(const unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE])
bool IsNull() const
Test whether this is the 0 key (the result of default construction).
friend bool operator<(const CPubKey &a, const CPubKey &b)
unsigned char vch[SIZE]
see www.keylength.com script supports up to 75 for single byte push
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
std::array< std::byte, SIZE > m_pubkey
static constexpr size_t size()
CKeyID(const uint160 &in)
uint256 ComputeTapTweakHash(const uint256 *merkle_root) const
Compute the Taproot tweak as specified in BIP341, with *this as internal key:
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 EncodeWithVersion(unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]) const
XOnlyPubKey()=default
Construct an empty x-only pubkey.
static const XOnlyPubKey NUMS_H
Nothing Up My Sleeve point H Used as an internal key for provably disabling the key path spend see BI...
static bool CheckLowS(const std::vector< unsigned char > &vchSig)
Check whether a signature is normalized (lower-S).
const unsigned char & operator[](unsigned int pos) const
void Unserialize(Stream &s)
memcpy(result.begin(), stream.data(), stream.size())
friend bool operator==(const CPubKey &a, const CPubKey &b)
Comparator implementation.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
bool IsValidNonHybrid() const noexcept
Check if a public key is a syntactically valid compressed or uncompressed key.
const unsigned char * begin() const
constexpr unsigned char * begin()
XOnlyPubKey & operator=(const XOnlyPubKey &)=default
void Serialize(Stream &s) const
Implement serialization, as if this was a byte vector.
bool CheckTapTweak(const XOnlyPubKey &internal, const uint256 &merkle_root, bool parity) const
Verify that this is a Taproot tweaked output point, against a specified internal key, Merkle root, and parity.
constexpr XOnlyPubKey(std::span< const unsigned char > bytes)
Construct an x-only pubkey from exactly 32 bytes.
static bool ValidSize(const std::vector< unsigned char > &vch)
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
static constexpr unsigned int COMPRESSED_SIZE
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
const unsigned char * begin() const
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
const unsigned char * end() const
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
CPubKey()
Construct an invalid public key.
bool Derive(CPubKey &pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc, uint256 *bip32_tweak_out=nullptr) const
Derive BIP32 child pubkey.
CPubKey(std::span< const uint8_t > _vch)
Construct a public key from a byte vector.
An encapsulated public key.
bool Derive(CExtPubKey &out, unsigned int nChild, uint256 *bip32_tweak_out=nullptr) const
An ElligatorSwift-encoded public key.
std::vector< CPubKey > GetCPubKeys() const
Returns this XOnlyPubKey with 0x02 and 0x03 prefixes.
static constexpr size_t SIZE
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
bool friend operator==(const EllSwiftPubKey &a, const EllSwiftPubKey &b)
bool operator<(const XOnlyPubKey &other) const
static constexpr unsigned int COMPACT_SIGNATURE_SIZE
CPubKey GetEvenCorrespondingCPubKey() const
constexpr bool IsNull() const
bool IsFullyValid() const
Determine if this pubkey is fully valid.
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
XOnlyPubKey(const CPubKey &pubkey)
Construct an x-only pubkey from a normal pubkey.
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
const unsigned int BIP32_EXTKEY_SIZE
constexpr unsigned char * end()
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.
bool operator==(const XOnlyPubKey &other) const
const unsigned char & operator[](int pos) const
const std::byte * data() const
bool VerifySchnorr(const uint256 &msg, std::span< const unsigned char > sigbytes) const
Verify a Schnorr signature against this public key.
const unsigned char * data() const
const unsigned int BIP32_EXTKEY_WITH_VERSION_SIZE
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
friend bool operator>(const CPubKey &a, const CPubKey &b)
static constexpr unsigned int SIGNATURE_SIZE
const unsigned char * end() const
std::optional< std::pair< XOnlyPubKey, bool > > CreateTapTweak(const uint256 *merkle_root) const
Construct a Taproot tweaked output point with this point as internal key.
CPubKey Decode() const
Decode to normal compressed CPubKey (for debugging purposes).
std::vector< CKeyID > GetKeyIDs() const
Returns a list of CKeyIDs for the CPubKeys that could have been used to create this XOnlyPubKey...
bool Decompress()
Turn this public key into an uncompressed public key.
bool IsCompressed() const
Check whether this is a compressed public key.
const unsigned char * data() const