libbitcoin / libbitcoin-network

Bitcoin P2P Network Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

transaction subscribe

virus-cyber opened this issue · comments

hello, share the working code for subscribe transactions to display the bitcoin address

doesn't anyone know ?

The question if far too vague. There is detailed Libbitcoin server documentation on its wiki. This is not a support site. Please see the slack channel or mailing list.

please help me build a code to listen to all transactions and translate them into a bitcon address, I still have little hope in this

hello what am I doing wrong trying to subscribe to transactions and get bitcoin addresses

#include <bitcoin/network.hpp>

using namespace bc;
int main(int argc, char* argv[])
{
// Create P2P object.
// -------------------------------------------------------------------------

// Single Outbound Channel.
bc::network::settings settings(bc::config::settings::mainnet);
settings.seeds.clear();
settings.seeds.push_back({ "seed.bitcoin.wiz.biz",8333 });
settings.seeds.push_back({ "dnsseed.bitcoin.dashjr.org",8333 });
settings.seeds.push_back({ "seed.btc.petertodd.org",8333 });
settings.inbound_connections = 0; // default setting = 0.
settings.outbound_connections = 2;  // default setting = 8.
settings.host_pool_capacity = 100; // default setting = 0.
settings.threads = 3; // default setting = 0.
settings.services = bc::message::version::service::none; // default none.

// Create P2P object.
bc::network::p2p my_p2p(settings);

// Promise which signals completion to main loop (below).
std::promise<bc::code> promise;

// Handlers
// -------------------------------------------------------------------------

const auto send_handler = [](const bc::code& ec)
{
	std::cout << std::string("[My App] Get headers message sent: ")
		.append(ec.message()) << std::endl;
};

const auto block_subscription_handler = [](const bc::code& ec,
	bc::message::transaction::const_ptr message)
{
	if (ec)
		return true;

    auto coinbase_hash = message->hash();

	std::cout << wallet::payment_address(coinbase_hash) << std::endl;

};

const auto connect_handler = [&block_subscription_handler, &send_handler](
	const bc::code& ec, bc::network::channel::ptr channel)
{
	// Resubscribe if connect error occurs.
	if (ec)
		return true;

	// Get negotiated version.
	std::cout << std::string("[My App] New channel with version: ")
		.append(std::to_string(channel->peer_version()->value()))
		<< std::endl;

	// Depending on version, get_blocks or get_headers.
	if (channel->peer_version()->value() >=
		bc::message::version::level::headers)
	{
		// Subscribe to block message.
		channel->subscribe<bc::message::transaction>(
			block_subscription_handler);
	}

	else
	{
		// We reject peer that do not support headers first sync.
		channel->stop(bc::error::channel_stopped);
	}

	// Resubscribe
	return true;
};

const auto run_handler = [&my_p2p](const bc::code& ec)
{
	// Omitted.
};

const auto start_handler = [&my_p2p, &promise, &connect_handler,
	&run_handler](const bc::code& ec)
{
	if (ec && ec != bc::error::peer_throttling)
	{
		promise.set_value(ec);
	}

	// Network run.
	my_p2p.subscribe_connection(connect_handler);
	my_p2p.run(run_handler);
};

// Start P2P object.
// -------------------------------------------------------------------------

my_p2p.start(start_handler);

std::cout << promise.get_future().get().message() << std::endl;

return 0;

}

pancake already ran out of strength to do please help how to sign for new transactions

it is possible to receive new transactions in general

Yes, however this is libbitcoin-network, which does not provide transaction or block protocols. This is because network is not associated with a store/blockchain - that is the role of libbitcoin-node. To do this in libbitcoin-network you would need to either create and attach an additional protocol or subscribe to channel creations and then subscribe to the transaction message on each created channel.