libbitcoin / libbitcoin-system

Bitcoin Cross-Platform C++ Development Toolkit

Home Page:https://libbitcoin.info/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to run this code

Z-fly opened this issue · comments

https://chat.openai.com/share/c2e4bb89-648c-4d7b-b825-bf70f2ddbd6e

I asked GPT about this, but the code returned by GPT cannot be compiled. Can someone help me?

#include <iostream>
#include <string>
#include <stdexcept>
#include <bitcoin/bitcoin.hpp>

class Bip44Wallet {
public:
    Bip44Wallet(const std::string& mnemonic, int account_index = 0) {
        if (!bc::wallet::mnemonic_validate(mnemonic))
            throw std::invalid_argument("Invalid mnemonic");

        auto seed = bc::to_chunk(bc::wallet::decode_mnemonic(mnemonic));
        master_key = bc::wallet::hd_private(seed, bc::wallet::hd_private::testnet);
        account_key = master_key.derive_private(44 + bc::wallet::hd_first_hardened_key)
                                       .derive_private(bc::wallet::hd_private::testnet)
                                       .derive_private(account_index + bc::wallet::hd_first_hardened_key);
    }

    std::string get_address(int change, int address_index) {
        auto change_key = account_key.derive_private(change);
        auto address_key = change_key.derive_private(address_index);
        auto payment_address = address_key.to_payment_address();

        return payment_address.encoded();
    }

private:
    bc::wallet::hd_private master_key;
    bc::wallet::hd_private account_key;
};

int main() {
    std::string mnemonic = "interest magic gravity flock border prevent unable ten express notice shaft list";
    int bip_type = 44;
    int account = 1;
    int chain = 0;
    int address_index = 1;

    try {
        Bip44Wallet wallet(mnemonic, account);
        std::string address = wallet.get_address(chain, address_index);
        std::cout << "Generated Address: " << address << std::endl;
        std::cout << "Derivation Path: m/" << bip_type << "'/0'/" << account << "'/" << chain << "/" << address_index << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }

    return 0;
}

/root/bit/main.cpp: In constructor ‘Bip44Wallet::Bip44Wallet(const string&, int)’:
/root/bit/main.cpp:9:26: error: ‘mnemonic_validate’ is not a member of ‘libbitcoin::wallet’
9 | if (!bc::wallet::mnemonic_validate(mnemonic))
| ^~~~~~~~~~~~~~~~~
/root/bit/main.cpp:12:62: error: invalid initialization of reference of type ‘const word_list&’ {aka ‘const std::vector<std::__cxx11::basic_string >&’} from expression of type ‘const string’ {aka ‘const std::__cxx11::basic_string’}
12 | auto seed = bc::to_chunk(bc::wallet::decode_mnemonic(mnemonic));
| ^~~~~~~~
In file included from /root/bit/include/bitcoin/system.hpp:195,
from /root/bit/main.cpp:4:
/root/bit/include/bitcoin/system/wallet/mnemonic.hpp:76:51: note: in passing argument 1 of ‘libbitcoin::long_hash libbitcoin::wallet::decode_mnemonic(const word_list&)’
76 | BC_API long_hash decode_mnemonic(const word_list& mnemonic);
| ~~~~~~~~~~~~~~~~~^~~~~~~~
/root/bit/main.cpp: In member function ‘std::string Bip44Wallet::get_address(int, int)’:
/root/bit/main.cpp:22:44: error: ‘class libbitcoin::wallet::hd_private’ has no member named ‘to_payment_address’
22 | auto payment_address = address_key.to_payment_address();
| ^~~~~~~~~~~~~~~~~~
ninja: build stopped: subcommand failed.

It seems ChatGPT didn’t specify which version of Libbitcoin to compile with. Did you use /master or /version3?

It appears that the sample was defined using /version3 (the release version). /master/libbitcoin-system/ is production quality, maybe just ask Chat to use that.

I solved the problem.

#include <iostream>
#include <bitcoin/bitcoin.hpp>

std::string generate_bitcoin_address(const std::string& mnemonic, int bip_type, uint32_t account_level, uint32_t chain_type, uint32_t address_index) {
    auto mnemonic_words = libbitcoin::split(mnemonic, " ", true);
    auto seed = libbitcoin::wallet::decode_mnemonic(mnemonic_words);
    libbitcoin::data_chunk seed_chunk(std::begin(seed), std::end(seed));
    libbitcoin::wallet::hd_private m_key(seed_chunk, libbitcoin::wallet::hd_private::mainnet);
    libbitcoin::wallet::hd_private derived_key = m_key
        .derive_private(libbitcoin::wallet::hd_first_hardened_key + bip_type)
        .derive_private(libbitcoin::wallet::hd_first_hardened_key)
        .derive_private(account_level + libbitcoin::wallet::hd_first_hardened_key)
        .derive_private(chain_type)
        .derive_private(address_index);
    auto pub_key = derived_key.to_public();
    auto public_key_hash = libbitcoin::bitcoin_short_hash(pub_key.point());
    libbitcoin::wallet::payment_address pay_address(public_key_hash, libbitcoin::wallet::payment_address::mainnet_p2kh);

    return pay_address.encoded();
}

int main() {
    try {
        std::string mnemonic = "interest magic gravity flock border prevent unable ten express notice shaft list";
        int bip_type = 44;
        uint32_t account_level = 1, chain_type = 0, address_index = 0;

        std::string address = generate_bitcoin_address(mnemonic, bip_type, account_level, chain_type, address_index);
        std::cout << "Address: " << address << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }

    return 0;
}