37 const CKey key = [&] {
39 k.Set(buffer.begin(), buffer.end(),
true);
55 assert(!(invalid_key == key));
62 CKey uncompressed_key;
63 uncompressed_key.
Set(buffer.begin(), buffer.end(),
false);
64 assert(!(uncompressed_key == key));
78 CKey negated_key = key;
81 assert(!(negated_key == key));
84 assert(negated_key == key);
92 const bool ok = key.
Derive(child_key, child_chaincode, 0, random_uint256);
95 assert(!(child_key == key));
96 assert(child_chaincode != random_uint256);
120 assert(pubkey_deserialized == pubkey);
133 assert(!tx_multisig_script.IsPayToScriptHash());
134 assert(!tx_multisig_script.IsPayToWitnessScriptHash());
135 assert(!tx_multisig_script.IsPushOnly());
136 assert(!tx_multisig_script.IsUnspendable());
137 assert(tx_multisig_script.HasValidOps());
138 assert(tx_multisig_script.size() == 37);
143 assert(fillable_signing_provider.GetKeys().size() == 0);
144 assert(!fillable_signing_provider.HaveKey(pubkey.
GetID()));
146 const bool ok_add_key = fillable_signing_provider.AddKey(key);
148 assert(fillable_signing_provider.HaveKey(pubkey.
GetID()));
153 const bool ok_add_key_pubkey = fillable_signing_provider_pub.
AddKeyPubKey(key, pubkey);
154 assert(ok_add_key_pubkey);
158 const bool is_standard_tx_pubkey =
IsStandard(tx_pubkey_script, std::nullopt, which_type_tx_pubkey);
159 assert(is_standard_tx_pubkey);
163 const bool is_standard_tx_multisig =
IsStandard(tx_multisig_script, std::nullopt, which_type_tx_multisig);
164 assert(is_standard_tx_multisig);
167 std::vector<std::vector<unsigned char>> v_solutions_ret_tx_pubkey;
168 const TxoutType outtype_tx_pubkey =
Solver(tx_pubkey_script, v_solutions_ret_tx_pubkey);
170 assert(v_solutions_ret_tx_pubkey.size() == 1);
171 assert(v_solutions_ret_tx_pubkey[0].size() == 33);
173 std::vector<std::vector<unsigned char>> v_solutions_ret_tx_multisig;
174 const TxoutType outtype_tx_multisig =
Solver(tx_multisig_script, v_solutions_ret_tx_multisig);
176 assert(v_solutions_ret_tx_multisig.size() == 3);
177 assert(v_solutions_ret_tx_multisig[0].size() == 1);
178 assert(v_solutions_ret_tx_multisig[1].size() == 33);
179 assert(v_solutions_ret_tx_multisig[2].size() == 1);
188 assert(script_for_destination.size() == 25);
193 const CPubKey pubkey_from_address_string =
AddrToPubKey(fillable_signing_provider, destination_address);
194 assert(pubkey_from_address_string == pubkey);
202 const bool ok_get_pubkey = fillable_signing_provider.GetPubKey(key_id, pubkey_out);
206 const bool ok_get_key = fillable_signing_provider.GetKey(key_id, key_out);
208 assert(fillable_signing_provider.GetKeys().size() == 1);
209 assert(fillable_signing_provider.HaveKey(key_id));
212 const bool ok_get_key_origin = fillable_signing_provider.GetKeyOrigin(key_id, key_origin_info);
213 assert(!ok_get_key_origin);
217 const std::vector<unsigned char> vch_pubkey{pubkey.
begin(), pubkey.
end()};
221 const CPubKey pubkey_ctor_1{vch_pubkey};
222 assert(pubkey == pubkey_ctor_1);
224 const CPubKey pubkey_ctor_2{vch_pubkey.
begin(), vch_pubkey.end()};
225 assert(pubkey == pubkey_ctor_2);
228 pubkey_set.
Set(vch_pubkey.begin(), vch_pubkey.end());
229 assert(pubkey == pubkey_set);
233 const CPubKey invalid_pubkey{};
234 assert(!invalid_pubkey.IsValid());
235 assert(!invalid_pubkey.IsFullyValid());
236 assert(!(pubkey == invalid_pubkey));
237 assert(pubkey != invalid_pubkey);
238 assert(pubkey < invalid_pubkey);
243 unsigned int sum = 0;
244 for (
size_t i = 0; i < pubkey.
size(); ++i) {
251 CPubKey decompressed_pubkey = pubkey;
254 const bool ok = decompressed_pubkey.
Decompress();
261 std::vector<unsigned char> vch_sig;
262 const bool ok = key.
Sign(random_uint256, vch_sig,
false);
267 const std::vector<unsigned char> vch_invalid_sig{vch_sig.begin(), vch_sig.begin() + vch_sig.size() - 1};
268 assert(!pubkey.
Verify(random_uint256, vch_invalid_sig));
273 std::vector<unsigned char> vch_compact_sig;
274 const bool ok_sign_compact = key.
SignCompact(random_uint256, vch_compact_sig);
278 const bool ok_recover_compact = recover_pubkey.
RecoverCompact(random_uint256, vch_compact_sig);
279 assert(ok_recover_compact);
280 assert(recover_pubkey == pubkey);
286 const bool ok = pubkey.
Derive(child_pubkey, child_chaincode, 0, random_uint256);
288 assert(child_pubkey != pubkey);
293 assert(child_chaincode != random_uint256);
299 for (
const bool skip_check : {
true,
false}) {
301 const bool ok = loaded_key.
Load(priv_key, pubkey, skip_check);
303 assert(key == loaded_key);
void ECC_Start()
Initialize the elliptic curve support.
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
bool Negate()
Negate private key.
static const std::string REGTEST
bool IsPayToScriptHash() const
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
CPubKey GetPubKey() const
Compute the public key from a private key.
void Set(const T pbegin, const T pend)
Initialize a public key using begin/end iterators to byte data.
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
uint256 GetHash() const
Get the 256-bit hash of this public key.
CPubKey HexToPubKey(const std::string &hex_in)
static bool CheckLowS(const std::vector< unsigned char > &vchSig)
Check whether a signature is normalized (lower-S).
CKeyID GetKeyForDestination(const SigningProvider &store, const CTxDestination &dest)
Return the CKeyID of the key involved in a script (if there is a unique one).
bool Derive(CPubKey &pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child pubkey.
void Unserialize(Stream &s)
bool HasValidOps() const
Check if the script contains valid OP_CODES.
Double ended buffer combining vector and stream-like interfaces.
bool IsStandard(const CScript &scriptPubKey, const std::optional< unsigned > &max_datacarrier_bytes, TxoutType &whichType)
const unsigned char * begin() const
bool IsSegWitOutput(const SigningProvider &provider, const CScript &script)
Check whether a scriptPubKey is known to be segwit.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
const unsigned char * begin() const
std::vector< CTxDestination > GetAllDestinationsForKey(const CPubKey &key)
Get all destinations (potentially) supported by the wallet for the given key.
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack...
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Create a compact signature (65 bytes), which allows reconstructing the used public key...
void Serialize(Stream &s) const
Implement serialization, as if this was a byte vector.
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
CPrivKey is a serialized private key, with all parameters included (SIZE bytes)
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized signature.
static bool ValidSize(const std::vector< unsigned char > &vch)
FUZZ_TARGET_INIT(key, initialize_key)
Users of this module must hold an ECCVerifyHandle.
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
const unsigned char * end() const
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
An encapsulated public key.
Fillable signing provider that keeps keys in an address->secret map.
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
unsigned int size() const
Simple read-only vector-like interface.
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Serialized script, used inside transaction inputs and outputs.
const unsigned char * end() const
A reference to a CKey: the Hash160 of its serialized public key.
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
CTxDestination GetDestinationForKey(const CPubKey &key, OutputType type)
Get a destination of the requested type (if possible) to the specified key.
std::string EncodeDestination(const CTxDestination &dest)
virtual bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
const unsigned char * data() const
An encapsulated private key.
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
CKey DecodeSecret(const std::string &str)
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
std::string EncodeSecret(const CKey &key)
CPubKey AddrToPubKey(const FillableSigningProvider &keystore, const std::string &addr_in)
bool Decompress()
Turn this public key into an uncompressed public key.
bool IsPayToWitnessScriptHash() const
bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
bool IsValid() const
Check whether this private key is valid.
bool IsCompressed() const
Check whether this is a compressed public key.
virtual bool HaveKey(const CKeyID &address) const override