#include#include int main() { CKey key; key.MakeNewKey(true); // generate a new random key std::cout << "Private key: " << key.GetSecret().ToString() << std::endl; std::cout << "Public key: " << key.GetPubKey().GetID().ToString() << std::endl; std::cout << "Address: " << CBitcoinAddress(key.GetPubKey().GetID()).ToString() << std::endl; return 0; }
#include#include int main() { std::string wif = "KxdABFkKjfi2LxvFhdJG2E69g4V7yLUj4JjjmXZmUSMNM5mu5Li5"; CKey key; if (!key.SetString(wif, true)) { std::cerr << "Invalid key format" << std::endl; return 1; } std::cout << "Private key: " << key.GetSecret().ToString() << std::endl; std::cout << "Public key: " << key.GetPubKey().GetID().ToString() << std::endl; std::cout << "Address: " << CBitcoinAddress(key.GetPubKey().GetID()).ToString() << std::endl; return 0; }
#includeThis code generates a new random private key, signs a message using it, verifies the signature using the corresponding public key, and prints a success message if the verification succeeds. Package/library: Bitcoin Core#include #include int main() { std::string message = "hello world"; CKey key; key.MakeNewKey(true); // generate a new random key std::vector signature; if (!key.Sign(Hash(message), signature)) { std::cerr << "Failed to sign message" << std::endl; return 1; } std::cout << "Signature size: " << signature.size() << std::endl; CKeyID keyid = key.GetPubKey().GetID(); if (!keyid.IsValid()) { std::cerr << "Invalid key ID" << std::endl; return 1; } CPubKey pubkey; if (!pubkey.RecoverCompact(Hash(message), signature, keyid)) { std::cerr << "Failed to recover public key" << std::endl; return 1; } if (!pubkey.IsValid()) { std::cerr << "Invalid public key" << std::endl; return 1; } if (!pubkey.Verify(Hash(message), signature)) { std::cerr << "Invalid signature" << std::endl; return 1; } std::cout << "Success" << std::endl; return 0; }