IBC#033: Bitcoin Core Initialization, Step 12: Onion Services
IBC-033-Bitcoin-Core-Initialization-Step-12-Onion-Services-kkgpnv
tuma
Bitcoin, Freedom and my personal pursuit of Happiness Open-source Writer FOSS Reporter @ npub1z6dx97fxn94j3z9c3pvgsellnywqgakmj82d7rrr3jdlgy2nqgnq5uf7n9 Contributor @ npub1hkuk45c6c6h3y0rks0z4wa0wyyud5ru0qy0rn9x4dgnjwrnfy46s5a432p Tech Editor @ npub10pensatlcfwktnvjjw2dtem38n6rvw8g6fv73h84cuacxn4c28eqyfn34f Sometimes Photographer @ npub1m85lkfcs73qgpdmm5l5zsjr0wd2khccum6vl8u39v7dvt407tcdq6uxjkj
3 min read
Hello everyone and welcome to this new episode of Inside Bitcoin Code.
We concluded the previous episode with discussing about port bindings. Today, we continue our deep dive into step 12 of the initialization of Bitcoin Core, discussing about Tor services. [Code Link]
Let’s start!
Onion Service Target
First of all, the code defines a CService object — a combination of a network address and a port number — which will store the local address Bitcoin Core should use for its Tor hidden service.
CService onion_service_target;
Then, the program tries three different options to choose which local address will receive Tor traffic, by explicitly setting an onion bind address, using the first bind address provided by the user, or setting a default generated address.
First, it checks that the onion_binds vector in not empty().
If so, it takes the address at the front() of the vector — the first one.
if (!connOptions.onion_binds.empty()) {
onion_service_target = connOptions.onion_binds.front();
}
Otherwise, it looks into the vBinds vector. In case it is not empty(), the variable onion_service_target is assigned the address value at the front() of the vector.
else if (!connOptions.vBinds.empty()) {
onion_service_target = connOptions.vBinds.front();
}
Finally, if neither of the two previous options is available, the onion_service_target variable is assigned a default address generated by the DefaultOnionServiceTarget function.
The address is the added to the onion_binds vector.
else {
onion_service_target = DefaultOnionServiceTarget(default_bind_port_onion);
connOptions.onion_binds.push_back(onion_service_target);
}
Consider supporting this newsletter by using one of my affiliate links. These are not sponsorships, just products I use everyday. Thank you!
Launching Tor Control
Once the onion_service_target is filled with the Tor address, the code checks whether the -listenonion input argument has been set to 1 by the user.
if (args.GetBoolArg("-listenonion", DEFAULT_LISTEN_ONION))
Before creating the Tor onion service, the program checks that there is not more than one address provided.
if (connOptions.onion_binds.size() > 1)
If that is the case, a warning is sent to the user, stating that only the one provided by the onion_service_target variable will be used.
InitWarning(strprintf(_(
"More than one onion bind address is provided. Using %s "
"for the automatically created Tor onion service."),
onion_service_target.ToStringAddrPort()
));
Finally, the StartTorControl() function is called.
This function launches the torcontrol thread, which instructs Tor to create a hidden service that points to the chosen onion_service_target address.
This allows external Tor users to connect to Bitcoin Core via an .onion address without knowing the node's IP.
StartTorControl(onion_service_target);
Affiliate Program
Ready to Lead the Change*? If you are looking to professionalize your journey in the Bitcoin ecosystem, the Plan B Network provides the ultimate educational framework. Whether you are aiming to build the next major startup or dive deep into the protocol's code, they have a Master track for you. Use my code* SS_Varese at checkout to receive 15% OFF on both the Business and Developers tracks!
Let’s keep in touch:
- Follow me on Nostr and X
- Check out my writings on btc++ insider edition
- Try my new app Sats Tracker, an expense tracker app for people living in the Bitcoin standard. Available on Zapstore!
Help keep these ideas circulating
This is an independent project. If you'd like to help support it, you can make a contribution. But there's no obligation at all. Reading, sharing, and coming back is already a form of support.
Read also
1
> *Ciao, benvenuti al decimo episodio de Il Gazzettino Cypherpunk, la vostra fonte settimanale di news sullo sviluppo di…

2
[
3
Un nuovo Lodging of Wayfaring Men in italiano

Want to publish here too? No sign-up, and the text stays yours. Learn how to start
Replies (0)
Loading…
