1
0
mirror of https://github.com/bitcoinbook/bitcoinbook synced 2024-12-23 15:18:11 +00:00
bitcoinbook/code/vanity-miner.cpp

74 lines
2.2 KiB
C++
Raw Normal View History

2017-07-18 04:07:31 +00:00
#include <random>
2014-09-28 15:52:50 +00:00
#include <bitcoin/bitcoin.hpp>
// The string we are searching for
const std::string search = "1kid";
// Generate a random secret key. A random 32 bytes.
bc::ec_secret random_secret(std::default_random_engine& engine);
// Extract the Bitcoin address from an EC secret.
std::string bitcoin_address(const bc::ec_secret& secret);
// Case insensitive comparison with the search string.
bool match_found(const std::string& address);
int main()
{
2015-02-23 23:09:27 +00:00
// random_device on Linux uses "/dev/urandom"
// CAUTION: Depending on implementation this RNG may not be secure enough!
// Do not use vanity keys generated by this example in production
2014-09-28 15:52:50 +00:00
std::random_device random;
std::default_random_engine engine(random());
2015-02-23 23:09:27 +00:00
2014-09-28 15:52:50 +00:00
// Loop continuously...
while (true)
{
// Generate a random secret.
bc::ec_secret secret = random_secret(engine);
// Get the address.
std::string address = bitcoin_address(secret);
// Does it match our search string? (1kid)
if (match_found(address))
{
// Success!
std::cout << "Found vanity address! " << address << std::endl;
2017-07-18 04:07:31 +00:00
std::cout << "Secret: " << bc::encode_base16(secret) << std::endl;
2014-09-28 15:52:50 +00:00
return 0;
}
}
// Should never reach here!
return 0;
}
bc::ec_secret random_secret(std::default_random_engine& engine)
{
// Create new secret...
bc::ec_secret secret;
// Iterate through every byte setting a random value...
for (uint8_t& byte: secret)
byte = engine() & 255;
2014-09-28 15:52:50 +00:00
// Return result.
return secret;
}
std::string bitcoin_address(const bc::ec_secret& secret)
{
2017-07-18 04:07:31 +00:00
// Convert secret to payment address
bc::wallet::ec_private private_key(secret);
bc::wallet::payment_address payaddr(private_key);
2014-09-28 15:52:50 +00:00
// Return encoded form.
return payaddr.encoded();
}
bool match_found(const std::string& address)
{
auto addr_it = address.begin();
// Loop through the search string comparing it to the lower case
// character of the supplied address.
for (auto it = search.begin(); it != search.end(); ++it, ++addr_it)
if (*it != std::tolower(*addr_it))
return false;
// Reached end of search string, so address matches.
return true;
}