Libbitcoin: Working with Altcoins

Libbitcoin is a modular framework making it extremely adaptable to the needs of the developer. The libbitcoin maintainers go to great lengths to design a system that avoids hardcoding values and parameters into the framework. The wallet namespace classes that deal with various key types do not have hardcoded prefix and version values, allowing the developer(us) to easily use these classes for currencies other than bitcoin.

By simply passing the necessary version values to the constructor functions we can generate keys for various altcoins. First, I’ve defined a function to generate a WIF private key and a payment address, displaying both to the user. This function takes the version prefix argument as a short.

The libbitcoin ec_private constructor takes a 2 byte version argument with most significant byte being the WIF version code and the least significant byte being the address prefix. So we simply instantiate the ec_private object with the altcoins specific prefix and some entropy. We can now use these altcoin keys with libbitcoin functions as we normally would.(working with keys in libbitcoin).

void showKeys(short altcoin)
{
 data_chunk seedChunk(16);
 pseudo_random_fill(seedChunk);

 ec_secret secret = sha256_hash(seedChunk);

 wallet::ec_private privateKey(secret, altcoin);
 std::cout << "\nWIF: " << privateKey.encoded() << std::endl;
 std::cout << "Payment Address: " << privateKey.to_payment_address().encoded() << std::endl; 
}

Now that we have a function that generates altcoin keys, I’ve declared three variables as the version codes for three Altcoins. Note that these value are the hexadecimal WIF code followed by the hexadecimal address prefix. So for Litecoin, the WIF code if 0xB0 and the version prefix is 0x30 which should result in an address that begins with “L”. Simply call the key generator function and pass the altcoin value to the function.


int main()
{
 short LTC = 0xB030; //L 0xB0, 0x30
 short DOGE = 0x9E1E; //D 0x9E, 0x1E
 short DASH = 0xCC4C; //X 0xCC, 0x4C

 showKeys(LTC);
 showKeys(DOGE);
 showKeys(DASH);

}

This script can be compiled as usual, with libbitcoins library flags.

 $ g++ -std=c++11 -o altcoin AltcoinKeys.cpp $(pkg-config --cflags libbitcoin --libs libbitcoin)
$ ./altcoin

The output should look like this. Be sure to check the first letter matches the prefix for the altcoin.

Output

As usual the full code for these blog post can be found on github. To learn more about using libbitcoin with altcoins check here.

Leave a Reply

Your email address will not be published.