libbitcoin/libbitcoin — pull request #156 by swansontec (2014-12-08 04:44:19+00:00)
Title: Base16 array

This branch provides a decode_base16 template that works with all fixed-width hash data. So, a user can do this now:

short_hash address;
decode_base16(address, "...");

hash_digest sha256_hash;
decode_base16(sha256_hash, "...");

long_hash hd_stage;
decode_base16(hd_stage, "...");

hash_digest transaction_hash;
decode_hash(transaction_hash, "..."); // Same C++ type, different encoding!

ec_secret privkey;
decode_base16(privkey, "..."); // This is convenient too now

As we talked about on IRC, the C++ types don't distinguish between the special Bitcoin hash type and regular hashes that just happen to be 256 bits long. Until we fix this, the user just has to know which encoding to use with which data.

Once we do get strong typing, we can just provide a universal `decode` method that works with anything via overloads, and contains the knowledge of which low-level encoding goes with which high-level type:

bool decode(data160& out, const std::string& in) { return decode_base16(out, in); }
bool decode(data256& out, const std::string& in) { return decode_base16(out, in); }
bool decode(data512& out, const std::string& in) { return decode_base16(out, in); }
bool decode(btc256& out, const std::string& in) { return decode_hash(out, in); }
bool decode(ec_secret& out, const std::string& in) { return decode_base16(out, in); }
bool decode(message_signature& out, const std::string& in) { return decode_base64(out, in); }
...

We can do the same thing with a universal `encode` method.

This keeps the issue of low-level encoding (base16, base58, base64...) separate from the high-level canonical encoding of data types (bitcoin hashes use this, message_signatures use that, ec_signatures use something else).