mirror of
https://github.com/bitcoinbook/bitcoinbook
synced 2025-07-26 16:38:07 +00:00
Upgrade libbitcoin 1.0 code samples to libbitcoin 2.0
This commit is contained in:
parent
665f0b69ba
commit
f7804da5db
@ -3,8 +3,10 @@
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// Private secret key.
|
// Private secret key.
|
||||||
bc::ec_secret secret = bc::decode_hash(
|
bc::ec_secret secret;
|
||||||
|
bool success = bc::decode_hash(secret,
|
||||||
"038109007313a5807b2eccc082c8c3fbb988a973cacf1a7df9ce725c31b14776");
|
"038109007313a5807b2eccc082c8c3fbb988a973cacf1a7df9ce725c31b14776");
|
||||||
|
assert(success);
|
||||||
// Get public key.
|
// Get public key.
|
||||||
bc::ec_point public_key = bc::secret_to_public_key(secret);
|
bc::ec_point public_key = bc::secret_to_public_key(secret);
|
||||||
std::cout << "Public key: " << bc::encode_hex(public_key) << std::endl;
|
std::cout << "Public key: " << bc::encode_hex(public_key) << std::endl;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include <bitcoin/bitcoin.hpp>
|
#include <bitcoin/bitcoin.hpp>
|
||||||
|
|
||||||
bc::hash_digest create_merkle(bc::hash_digest_list& merkle)
|
bc::hash_digest create_merkle(bc::hash_list& merkle)
|
||||||
{
|
{
|
||||||
// Stop if hash list is empty.
|
// Stop if hash list is empty.
|
||||||
if (merkle.empty())
|
if (merkle.empty())
|
||||||
@ -18,7 +18,7 @@ bc::hash_digest create_merkle(bc::hash_digest_list& merkle)
|
|||||||
assert(merkle.size() % 2 == 0);
|
assert(merkle.size() % 2 == 0);
|
||||||
|
|
||||||
// New hash list.
|
// New hash list.
|
||||||
bc::hash_digest_list new_merkle;
|
bc::hash_list new_merkle;
|
||||||
// Loop through hashes 2 at a time.
|
// Loop through hashes 2 at a time.
|
||||||
for (auto it = merkle.begin(); it != merkle.end(); it += 2)
|
for (auto it = merkle.begin(); it != merkle.end(); it += 2)
|
||||||
{
|
{
|
||||||
@ -47,13 +47,21 @@ bc::hash_digest create_merkle(bc::hash_digest_list& merkle)
|
|||||||
return merkle[0];
|
return merkle[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert hex string to hash bytes.
|
||||||
|
bc::hash_digest get_hash(const std::string& value)
|
||||||
|
{
|
||||||
|
bc::hash_digest result;
|
||||||
|
bool success = bc::decode_hash(result, value);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// Replace these hashes with ones from a block to reproduce the same merkle root.
|
// Replace these hashes with ones from a block to reproduce the same merkle root.
|
||||||
bc::hash_digest_list tx_hashes{{
|
bc::hash_list tx_hashes{{
|
||||||
bc::decode_hash("0000000000000000000000000000000000000000000000000000000000000000"),
|
get_hash("0000000000000000000000000000000000000000000000000000000000000000"),
|
||||||
bc::decode_hash("0000000000000000000000000000000000000000000000000000000000000011"),
|
get_hash("0000000000000000000000000000000000000000000000000000000000000011"),
|
||||||
bc::decode_hash("0000000000000000000000000000000000000000000000000000000000000022"),
|
get_hash("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_hex(merkle_root) << std::endl;
|
||||||
|
Loading…
Reference in New Issue
Block a user