32typedef std::vector<unsigned char, secure_allocator<unsigned char> >
CPrivKey;
38using ECDHSecret = std::array<std::byte, ECDH_SECRET_SIZE>;
49 static constexpr unsigned int SIZE{279};
57 "COMPRESSED_SIZE is larger than SIZE");
61 using KeyType = std::array<unsigned char, 32>;
70 bool static Check(
const unsigned char* vch);
111 template <
typename T>
112 void Set(
const T pbegin,
const T pend,
bool fCompressedIn)
114 if (
size_t(pend - pbegin) != std::tuple_size_v<KeyType>) {
118 memcpy(
keydata->data(), (
unsigned char*)&pbegin[0],
keydata->size());
127 const std::byte*
data()
const {
return keydata ?
reinterpret_cast<const std::byte*
>(
keydata->data()) :
nullptr; }
156 bool Sign(
const uint256& hash, std::vector<unsigned char>& vchSig,
bool grind =
true, uint32_t test_case = 0)
const;
215 bool initiating)
const;
253 CExtKey(
const CExtPubKey& xpub,
const CKey& key_in) : nDepth(xpub.nDepth), fingerprint(xpub.fingerprint), nChild(xpub.nChild), chaincode(xpub.chaincode), key(key_in) {}
261 template <
typename Stream>
265 s << nDepth << fingerprint << Using<BigEndianFormatter<4>>(nChild) << chaincode << uint8_t{0} << std::span{key.
data(), key.
size()};
267 template <
typename Stream>
271 std::vector<unsigned char, secure_allocator<unsigned char>> ser_key(32);
272 s >> nDepth >> fingerprint >> Using<BigEndianFormatter<4>>(nChild) >> chaincode >> key_prefix >> std::span{ser_key};
273 key.
Set(ser_key.begin(), ser_key.end(),
true);
274 if ((nDepth == 0 && (nChild != 0 || fingerprint !=
KeyFingerprint{})) || key_prefix != 0) key =
CKey();
276 [[nodiscard]]
bool Derive(
CExtKey&
out,
unsigned int nChild)
const;
278 void SetSeed(std::span<const std::byte> seed);
285std::optional<std::pair<CExtKey, KeyOriginInfo>>
DeriveExtKey(
const CExtKey& ext_key,
const std::vector<uint32_t>& path);
306 if (
this != &other) {
307 if (other.m_keypair) {
328 using KeyType = std::array<unsigned char, 96>;
An encapsulated private key.
KeyPair ComputeKeyPair(const uint256 *merkle_root) const
Compute a KeyPair.
static constexpr unsigned int COMPRESSED_SIZE
bool SignSchnorr(const uint256 &hash, std::span< unsigned char > sig, const uint256 *merkle_root, const uint256 &aux) const
Create a BIP-340 Schnorr signature, for the xonly-pubkey corresponding to *this, optionally tweaked b...
friend bool operator==(const CKey &a, const CKey &b)
unsigned int size() const
Simple read-only vector-like interface.
bool IsValid() const
Check whether this private key is valid.
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized signature.
const std::byte * begin() const
ECDHSecret ComputeBIP324ECDHSecret(const EllSwiftPubKey &their_ellswift, const EllSwiftPubKey &our_ellswift, bool initiating) const
Compute a BIP324-style ECDH shared secret.
static constexpr unsigned int SIZE
secp256k1:
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
EllSwiftPubKey EllSwiftCreate(std::span< const std::byte > entropy) const
Create an ellswift-encoded public key for this key, with specified entropy.
CPubKey GetPubKey() const
Compute the public key from a private key.
const std::byte * end() const
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
static bool Check(const unsigned char *vch)
Check whether the 32-byte array pointed to by vch is valid keydata.
std::array< unsigned char, 32 > KeyType
see www.keylength.com script supports up to 75 for single byte push
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
secure_unique_ptr< KeyType > keydata
The actual byte data. nullptr for invalid keys.
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Create a compact signature (65 bytes), which allows reconstructing the used public key.
const std::byte * data() const
KeyFingerprint fingerprint() const
An encapsulated public key.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
static constexpr size_t OUTPUT_SIZE
RAII class initializing and deinitializing global state for elliptic curve support.
KeyPair() noexcept=default
bool SignSchnorr(const uint256 &hash, std::span< unsigned char > sig, const uint256 &aux) const
std::array< unsigned char, 96 > KeyType
bool IsValid() const
Check whether this keypair is valid.
secure_unique_ptr< KeyType > m_keypair
KeyPair(const KeyPair &other)
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
CPrivKey is a serialized private key, with all parameters included (SIZE bytes)
secp256k1_context * GetSecp256k1SignContext()
Access the secp256k1 context used for signing and MuSig2 nonce generation.
constexpr size_t ECDH_SECRET_SIZE
Size of ECDH shared secrets.
std::optional< std::pair< CExtKey, KeyOriginInfo > > DeriveExtKey(const CExtKey &ext_key, const std::vector< uint32_t > &path)
Get extended key and origin info for a given path.
std::array< std::byte, ECDH_SECRET_SIZE > ECDHSecret
bool ECC_InitSanityCheck()
Check that required EC support is available at runtime.
CKey GenerateRandomKey(bool compressed=true) noexcept
std::array< unsigned char, 4 > KeyFingerprint
std::unique_ptr< T, SecureUniqueDeleter< T > > secure_unique_ptr
unsigned char * UCharCast(char *c)
CExtKey(const CExtPubKey &xpub, const CKey &key_in)
KeyFingerprint id_key_fingerprint() const
void Serialize(Stream &s) const
BIP32 serialization without the version bytes (BIP32_EXTKEY_SIZE bytes)
KeyFingerprint fingerprint
friend bool operator==(const CExtKey &a, const CExtKey &b)
void Unserialize(Stream &s)
An ElligatorSwift-encoded public key.