mirror of
https://github.com/bitcoinbook/bitcoinbook
synced 2024-12-24 23:48:32 +00:00
Merge pull request #298 from thecodefactory/develop
Updates libbitcoin code samples to libbitcoin version3 api
This commit is contained in:
commit
cca1112e47
@ -2,23 +2,29 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// Private secret key.
|
// Private secret key string as base16
|
||||||
bc::ec_secret secret;
|
bc::ec_secret decoded;
|
||||||
bool success = bc::decode_base16(secret,
|
bc::decode_base16(decoded,
|
||||||
"038109007313a5807b2eccc082c8c3fbb988a973cacf1a7df9ce725c31b14776");
|
"038109007313a5807b2eccc082c8c3fbb988a973cacf1a7df9ce725c31b14776");
|
||||||
assert(success);
|
|
||||||
|
bc::wallet::ec_private secret(
|
||||||
|
decoded, bc::wallet::ec_private::mainnet_p2kh);
|
||||||
|
|
||||||
// Get public key.
|
// Get public key.
|
||||||
bc::ec_point public_key = bc::secret_to_public_key(secret);
|
bc::wallet::ec_public public_key(secret);
|
||||||
std::cout << "Public key: " << bc::encode_hex(public_key) << std::endl;
|
std::cout << "Public key: " << public_key.encoded() << std::endl;
|
||||||
|
|
||||||
// Create Bitcoin address.
|
// Create Bitcoin address.
|
||||||
// Normally you can use:
|
// Normally you can use:
|
||||||
// bc::payment_address payaddr;
|
// bc::wallet::payment_address payaddr =
|
||||||
// bc::set_public_key(payaddr, public_key);
|
// public_key.to_payment_address(
|
||||||
// const std::string address = payaddr.encoded();
|
// bc::wallet::ec_public::mainnet_p2kh);
|
||||||
|
// const std::string address = payaddr.encoded();
|
||||||
|
|
||||||
// Compute hash of public key for P2PKH address.
|
// Compute hash of public key for P2PKH address.
|
||||||
const bc::short_hash hash = bc::bitcoin_short_hash(public_key);
|
bc::data_chunk public_key_data;
|
||||||
|
public_key.to_data(public_key_data);
|
||||||
|
const auto hash = bc::bitcoin_short_hash(public_key_data);
|
||||||
|
|
||||||
bc::data_chunk unencoded_address;
|
bc::data_chunk unencoded_address;
|
||||||
// Reserve 25 bytes
|
// Reserve 25 bytes
|
||||||
|
@ -24,10 +24,10 @@ bc::hash_digest create_merkle(bc::hash_list& merkle)
|
|||||||
{
|
{
|
||||||
// Join both current hashes together (concatenate).
|
// Join both current hashes together (concatenate).
|
||||||
bc::data_chunk concat_data(bc::hash_size * 2);
|
bc::data_chunk concat_data(bc::hash_size * 2);
|
||||||
auto concat = bc::make_serializer(concat_data.begin());
|
auto concat = bc::serializer<
|
||||||
|
decltype(concat_data.begin())>(concat_data.begin());
|
||||||
concat.write_hash(*it);
|
concat.write_hash(*it);
|
||||||
concat.write_hash(*(it + 1));
|
concat.write_hash(*(it + 1));
|
||||||
assert(concat.iterator() == concat_data.end());
|
|
||||||
// Hash both of the hashes.
|
// Hash both of the hashes.
|
||||||
bc::hash_digest new_root = bc::bitcoin_hash(concat_data);
|
bc::hash_digest new_root = bc::bitcoin_hash(concat_data);
|
||||||
// Add this to the new list.
|
// Add this to the new list.
|
||||||
@ -39,7 +39,7 @@ bc::hash_digest create_merkle(bc::hash_list& merkle)
|
|||||||
// DEBUG output -------------------------------------
|
// DEBUG output -------------------------------------
|
||||||
std::cout << "Current merkle hash list:" << std::endl;
|
std::cout << "Current merkle hash list:" << std::endl;
|
||||||
for (const auto& hash: merkle)
|
for (const auto& hash: merkle)
|
||||||
std::cout << " " << bc::encode_hex(hash) << std::endl;
|
std::cout << " " << bc::encode_base16(hash) << std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
// --------------------------------------------------
|
// --------------------------------------------------
|
||||||
}
|
}
|
||||||
@ -56,7 +56,7 @@ int main()
|
|||||||
bc::hash_literal("0000000000000000000000000000000000000000000000000000000000000022"),
|
bc::hash_literal("0000000000000000000000000000000000000000000000000000000000000022"),
|
||||||
}};
|
}};
|
||||||
const bc::hash_digest merkle_root = create_merkle(tx_hashes);
|
const bc::hash_digest merkle_root = create_merkle(tx_hashes);
|
||||||
std::cout << "Result: " << bc::encode_hex(merkle_root) << std::endl;
|
std::cout << "Result: " << bc::encode_base16(merkle_root) << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,20 +7,19 @@
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// Create genesis block.
|
// Create genesis block.
|
||||||
const bc::block_type block = bc::genesis_block();
|
bc::chain::block block = bc::chain::block::genesis_mainnet();
|
||||||
// Genesis block contains a single coinbase transaction.
|
// Genesis block contains a single coinbase transaction.
|
||||||
assert(block.transactions.size() == 1);
|
assert(block.transactions().size() == 1);
|
||||||
// Get first transaction in block (coinbase).
|
// Get first transaction in block (coinbase).
|
||||||
const bc::transaction_type& coinbase_tx = block.transactions[0];
|
const bc::chain::transaction& coinbase_tx = block.transactions()[0];
|
||||||
// Coinbase tx has a single input.
|
// Coinbase tx has a single input.
|
||||||
assert(coinbase_tx.inputs.size() == 1);
|
assert(coinbase_tx.inputs().size() == 1);
|
||||||
const bc::transaction_input_type& coinbase_input = coinbase_tx.inputs[0];
|
const bc::chain::input& coinbase_input = coinbase_tx.inputs()[0];
|
||||||
// Convert the input script to its raw format.
|
// Convert the input script to its raw format.
|
||||||
const bc::data_chunk raw_message = save_script(coinbase_input.script);
|
const auto prefix = false;
|
||||||
// Convert this to an std::string.
|
const bc::data_chunk& raw_message = coinbase_input.script().to_data(prefix);
|
||||||
std::string message;
|
// Convert this to a std::string.
|
||||||
message.resize(raw_message.size());
|
std::string message(raw_message.begin(), raw_message.end());
|
||||||
std::copy(raw_message.begin(), raw_message.end(), message.begin());
|
|
||||||
// Display the genesis block message.
|
// Display the genesis block message.
|
||||||
std::cout << message << std::endl;
|
std::cout << message << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
#include <random>
|
||||||
#include <bitcoin/bitcoin.hpp>
|
#include <bitcoin/bitcoin.hpp>
|
||||||
|
|
||||||
// The string we are searching for
|
// The string we are searching for
|
||||||
@ -30,7 +31,7 @@ int main()
|
|||||||
{
|
{
|
||||||
// Success!
|
// Success!
|
||||||
std::cout << "Found vanity address! " << address << std::endl;
|
std::cout << "Found vanity address! " << address << std::endl;
|
||||||
std::cout << "Secret: " << bc::encode_hex(secret) << std::endl;
|
std::cout << "Secret: " << bc::encode_base16(secret) << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -51,11 +52,9 @@ bc::ec_secret random_secret(std::default_random_engine& engine)
|
|||||||
|
|
||||||
std::string bitcoin_address(const bc::ec_secret& secret)
|
std::string bitcoin_address(const bc::ec_secret& secret)
|
||||||
{
|
{
|
||||||
// Convert secret to pubkey...
|
// Convert secret to payment address
|
||||||
bc::ec_point pubkey = bc::secret_to_public_key(secret);
|
bc::wallet::ec_private private_key(secret);
|
||||||
// Finally create address.
|
bc::wallet::payment_address payaddr(private_key);
|
||||||
bc::payment_address payaddr;
|
|
||||||
bc::set_public_key(payaddr, pubkey);
|
|
||||||
// Return encoded form.
|
// Return encoded form.
|
||||||
return payaddr.encoded();
|
return payaddr.encoded();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user