如何在不使用 HD 密钥的情况下从 ec_private 获取 Segwit 比特币地址。我的代码如下,我很难使用 libbitcoin 文档。
#include <bitcoin/system.hpp>
using namespace bc;
using namespace std;
int main()
{
data_chunk seed(16);
pseudo_random_fill(seed);
ec_secret secretKey = bitcoin_hash(seed);
wallet::ec_private wif(secretKey, 0x8000, true);
string legacyAddress = wif.to_payment_address().encoded();
// How to get the segwit address?
...
return 0;
}
使用以下功能。
string get_segwit_address(ec_private privateKey)
{
ec_compressed publicKey = privateKey.to_public().point();
short_hash KeyHash = bitcoin_short_hash(publicKey);
script P2WPKH = script({operation(opcode(0)), operation(to_chunk(KeyHash))});
//Create P2SH script
short_hash WitnessProgramHash = bitcoin_short_hash(P2WPKH.to_data(0));
script P2SH_P2WPKH = script::to_pay_script_hash_pattern(WitnessProgramHash);
return payment_address(P2WPKH, payment_address::mainnet_p2sh).encoded();
}