libbitcoin/libbitcoin-explorer

/usr/local/include/bitcoin/explorer/command.hpp:60:1: error: ‘SYSCONFDIR’ was not declared in this scope

netsamir opened this issue · 15 comments

Hi,

Trying to compile a code (see later) I get the following error message:


$ g++ -o vanity-miner vanity-miner.cpp $(pkg-config --cflags --libs libbitcoin-explorer)
In file included from /usr/local/include/bitcoin/system.hpp:47,
                 from /usr/local/include/bitcoin/client.hpp:17,
                 from /usr/local/include/bitcoin/explorer.hpp:17,
                 from vanity-miner.cpp:2:
/usr/local/include/bitcoin/explorer/command.hpp: In function ‘boost::filesystem::path libbitcoin::explorer::config_default_path()’:
/usr/local/include/bitcoin/explorer/command.hpp:60:1: error: ‘SYSCONFDIR’ was not declared in this scope
   60 | BC_DECLARE_CONFIG_DEFAULT_PATH("libbitcoin" / BX_PROGRAM_NAME ".cfg")
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here is the code:


$ cat vanity-miner.cpp                                                                                                                                                                                                                                                  
#include <random>
#include <bitcoin/explorer.hpp>
                                                                    
// The string we are searching for
const std::string search = "1oana";

// 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()           
{                 
    // 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
    std::random_device random;
    std::default_random_engine engine(random());
                                                                    
    // 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;
            std::cout << "Secret: " << bc::encode_base16(secret) << std::endl;
            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;
    // Return result.
    return secret;
}

std::string bitcoin_address(const bc::ec_secret& secret)
{
    // Convert secret to payment address
    bc::wallet::ec_private private_key(secret);
    bc::wallet::payment_address payaddr(private_key);
    // 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;
            return false;
    // Reached end of search string, so address matches.
    return true;
}


I would assume this pertains to your boost dependency. How did you build libbitcoin? Also, you aren't using any bx code. You can build this directly against libbitcoin.

#include <bitcoin/system.hpp>

BC_USE_LIBBITCOIN_MAIN

int bc::system::main(int argc, char* argv[])
{
    const std::string search = "1oana";
    data_chunk entropy(ec_secret_size);

    while (true)
    {
        pseudo_random_fill(entropy);
        wallet::ec_private private_key(entropy);

        // Guard against entropy -> invalid secret (rare but possible).
        if (!private_key)
            continue;

        const auto address = private_key.to_payment_address().encoded();

        if (to_lower(address.substr(0, search.length())) == search)
        {
            cout << "Found vanity address! " << address << std::endl;
            cout << "Secret (WIF encoded): " << private_key.encoded()
                << std::endl;

            return 0;
        }

        cout << address << address << std::endl;
    }

    return 0;
}

The above is equivalent code, and both it and yours compile and run with only the libbitcoin dependency.

I would assume this pertains to your boost dependency. How did you build libbitcoin? Also, you aren't using any bx code. You can build this directly against libbitcoin.

I have build the last version with the following command:

sudo ./install.sh --build-boost --build-zmq --with-icu --disable-shared

#include <bitcoin/system.hpp>

BC_USE_LIBBITCOIN_MAIN

int bc::system::main(int argc, char* argv[])
{
    const std::string search = "1oana";
    data_chunk entropy(ec_secret_size);

    while (true)
    {
        pseudo_random_fill(entropy);
        wallet::ec_private private_key(entropy);

        // Guard against entropy -> invalid secret (rare but possible).
        if (!private_key)
            continue;

        const auto address = private_key.to_payment_address().encoded();

        if (to_lower(address.substr(0, search.length())) == search)
        {
            cout << "Found vanity address! " << address << std::endl;
            cout << "Secret (WIF encoded): " << private_key.encoded()
                << std::endl;

            return 0;
        }

        cout << address << address << std::endl;
    }

    return 0;
}

Trying to compile your code I get the following trace:


$ g++ -o vanity vanity.cpp $(pkg-config --cflags --libs libbitcoin-explorer)                                                             <<<                                                                                                                                    
vanity.cpp:5:9: error: ‘bc::system’ has not been declared                                                                               
    5 | int bc::system::main(int argc, char* argv[])                                                                                                                                                                                                                            
      |         ^~~~~~                                                                                                                  
vanity.cpp:5:5: error: redefinition of ‘int main(int, char**)’                                                                          
    5 | int bc::system::main(int argc, char* argv[])                                                                                    
      |     ^~                                                                                                                          
In file included from /usr/local/include/bitcoin/system/unicode/file_lock.hpp:28,                                                                                                                                                                                               
                 from /usr/local/include/bitcoin/system.hpp:129,                                                                        
                 from vanity.cpp:1:                                 
vanity.cpp:3:1: note: ‘int main(int, char**)’ previously defined here                                                                   
    3 | BC_USE_LIBBITCOIN_MAIN                                                                                                          
      | ^~~~~~~~~~~~~~~~~~~~~~                                                                                                                                                                                                                                                  
vanity.cpp: In function ‘int main(int, char**)’:                                                                                        
vanity.cpp:8:5: error: ‘data_chunk’ was not declared in this scope; did you mean ‘libbitcoin::data_chunk’?                                                                                                                                                                      
    8 |     data_chunk entropy(ec_secret_size);                                                                                                                                                                                                                                 
      |     ^~~~~~~~~~                                              
      |     libbitcoin::data_chunk                                                                                                      
In file included from /usr/local/include/bitcoin/system/math/hash.hpp:29,                                                                                                                                                                                                       
                 from /usr/local/include/bitcoin/system/formats/base_16.hpp:24,                                                                                                                                                                                                 
                 from /usr/local/include/bitcoin/system/config/checkpoint.hpp:27,                                                                                                                                                                                               
                 from /usr/local/include/bitcoin/system/constants.hpp:25,                                                                                                                                                                                                       
                 from /usr/local/include/bitcoin/system.hpp:19,                                                                         
                 from vanity.cpp:1:                                 
/usr/local/include/bitcoin/system/utility/data.hpp:48:30: note: ‘libbitcoin::data_chunk’ declared here                                                                                                                                                                          
   48 | typedef std::vector<uint8_t> data_chunk;                                                                                                                                                                                                                                
      |                              ^~~~~~~~~~                                                                                         
vanity.cpp:12:28: error: ‘entropy’ was not declared in this scope; did you mean ‘getentropy’?                                                                                                                                                                                   
   12 |         pseudo_random_fill(entropy);                                                                                                                                                                                                                                    
      |                            ^~~~~~~                                                                                                                                                                                                                                      
      |                            getentropy                                                                                           
vanity.cpp:12:9: error: ‘pseudo_random_fill’ was not declared in this scope; did you mean ‘libbitcoin::pseudo_random_fill’?                                                                                                                                                     
   12 |         pseudo_random_fill(entropy);                                                                                            
      |         ^~~~~~~~~~~~~~~~~~                                                                                                      
      |         libbitcoin::pseudo_random_fill                                                                                          
In file included from /usr/local/include/bitcoin/system.hpp:164,                                                                                                                                                                                                                
                 from vanity.cpp:1:                                                                                                     
/usr/local/include/bitcoin/system/utility/pseudo_random.hpp:105:13: note: ‘libbitcoin::pseudo_random_fill’ declared here                                                                                                                                                        
  105 | BC_API void pseudo_random_fill(data_chunk& out);                                                                                
      |             ^~~~~~~~~~~~~~~~~~                                                                                                  
vanity.cpp:13:9: error: ‘wallet’ has not been declared                                                                                  
   13 |         wallet::ec_private private_key(entropy);                                                                                
      |         ^~~~~~                                                                                                                  
vanity.cpp:16:14: error: ‘private_key’ was not declared in this scope                                                                   
   16 |         if (!private_key)                                                                                                       
      |              ^~~~~~~~~~~                                                                                                                                                                                                                                                
vanity.cpp:19:30: error: ‘private_key’ was not declared in this scope                                                                                                                                                                                                           
   19 |         const auto address = private_key.to_payment_address().encoded();                                                                                                                                                                                                
      |                              ^~~~~~~~~~~                                                                                        
vanity.cpp:21:13: error: ‘to_lower’ was not declared in this scope; did you mean ‘libbitcoin::to_lower’?                                                                                                                                                                        
   21 |         if (to_lower(address.substr(0, search.length())) == search)                                                                                                                                                                                                     
      |             ^~~~~~~~                                        
      |             libbitcoin::to_lower                                                                                                                                                                                                                                        
In file included from /usr/local/include/bitcoin/system/unicode/file_lock.hpp:28,                                                                                                                                                                                               
                 from /usr/local/include/bitcoin/system.hpp:129,                                                                        
                 from vanity.cpp:1:                                                                                                     
/usr/local/include/bitcoin/system/unicode/unicode.hpp:123:20: note: ‘libbitcoin::to_lower’ declared here                                                                                                                                                                        
  123 | BC_API std::string to_lower(const std::string& value);                                                                          
      |                    ^~~~~~~~                                 
vanity.cpp:23:13: error: ‘cout’ was not declared in this scope                                                                          
   23 |             cout << "Found vanity address! " << address << std::endl;                                                                                                                                                                                                   
      |             ^~~~                                            
vanity.cpp:23:13: note: suggested alternatives:                                                                                         
In file included from /usr/local/include/bitcoin/system/config/checkpoint.hpp:23,                                                                                                                                                                                               
                 from /usr/local/include/bitcoin/system/constants.hpp:25,                                                                                                                                                                                                       
                 from /usr/local/include/bitcoin/system.hpp:19,                                                                         
                 from vanity.cpp:1:                                 
   61 |   extern ostream cout;  /// Linked to standard output                                                                           
      |                  ^~~~                                       
In file included from /usr/local/include/bitcoin/system/unicode/file_lock.hpp:28,                                                                                                                                                                                               
                 from /usr/local/include/bitcoin/system.hpp:129,                                                                        
                 from vanity.cpp:1:                                 
vanity.cpp:3:1: note:   ‘libbitcoin::cout’                                                                                              
    3 | BC_USE_LIBBITCOIN_MAIN                                      
      | ^~~~~~~~~~~~~~~~~~~~~~                                      
vanity.cpp:30:9: error: ‘cout’ was not declared in this scope                                                                           
   30 |         cout << address << address << std::endl;                                                                                
      |         ^~~~                                                
vanity.cpp:30:9: note: suggested alternatives:                                                                                          
In file included from /usr/local/include/bitcoin/system/config/checkpoint.hpp:23,                                                                                                                                                                                               
                 from /usr/local/include/bitcoin/system/constants.hpp:25,                                                                                                                                                                                                       
                 from /usr/local/include/bitcoin/system.hpp:19,                                                                         
                 from vanity.cpp:1:                                 
/usr/include/c++/9/iostream:61:18: note:   ‘std::cout’                                                                                  
   61 |   extern ostream cout;  /// Linked to standard output                                                                           
      |                  ^~~~                                       
In file included from /usr/local/include/bitcoin/system/unicode/file_lock.hpp:28,                                                                                                                                                                                               
                 from /usr/local/include/bitcoin/system.hpp:129,                                                                        
                 from vanity.cpp:1:                                 
vanity.cpp:3:1: note:   ‘libbitcoin::cout’                                                                                              
    3 | BC_USE_LIBBITCOIN_MAIN                                      
      | ^~~~~~~~~~~~~~~~~~~~~~                                      

I used v4 (master), just remove system:: namespace refs.

Just some background information. I am studying bitcoin and I am testing the code provided by https://github.com/bitcoinbook/bitcoinbook.

The coded provided by the author is out of date. Eventually, I would like to make a pull request with the correct code. Hence my question. Honestly, I am not a CPP developer, I am just trying to follow the book. Thanks in advance.

I used v4 (master), just remove system:: namespace refs.

yes that compile.

Yes, the book is dated. It’s a couple versions back. But it’s all pretty close.

The code you have provided compile and seems to do what the author intended to do. Have you got any idea why his coded failed ? I am asking because I will try to propose the code you provided. Thanks

It didn’t need to use bx, much simpler to use bc. But otherwise it’s just your build environment that produced the earlier error. Nothing wrong with the code, except being way too complex.

Ok thanks have a nice day. Appreciate. I will create a pull request with your code. Is there something I can comment to make clear that you assist ?

Many thanks for your quick reply

I previously edited the book. Happy to have someone else keep it up. No need for credit.

New pull request for bitcoinbook bitcoinbook/bitcoinbook#908