Merge pull request #138 from javiervargas/master

Create 15_3_Receiving_Bitcoind_Notifications_with_C.md
This commit is contained in:
Shannon Appelcline 2020-07-02 08:10:51 -10:00 committed by GitHub
commit 5cb5fc03fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 405 additions and 3 deletions

View File

@ -4,6 +4,6 @@ _This is currently a placeholder; Writing it in Task #15 on the current [TODO li
This section will talk about using the Tor services that are now available courtesy of Bitcoin Standup._) This section will talk about using the Tor services that are now available courtesy of Bitcoin Standup._)
* 12.1: Verifying Your Tor Setup * [12.1: Verifying Your Tor Setup](12_1_Verifying_Your_Tor_Setup.md)
* 12.2: Changing Your Bitcoin Hidden Services * [12.2: Changing Your Bitcoin Hidden Services](12_2_Changing_Your_Bitcoin_Hidden_Services.md)
* 12.3: Adding SSH Hiddne Services * [12.3: Adding SSH Hidden Services](12_3_Adding_SSH_Hidden_Services.md)

View File

@ -0,0 +1,98 @@
# Chapter 12: Verifying Your Tor Setup
In this chapter we will verify tor installation and setup.
```
~$ sudo -u debian-tor tor --verify-config
```
If tor is installed correctly you should see an output like this:
```
Jun 26 21:52:09.230 [notice] Tor 0.4.3.5 running on Linux with Libevent 2.0.21-stable, OpenSSL 1.0.2n, Zlib 1.2.11, Liblzma 5.2.2, and Libzstd N/A.
Jun 26 21:52:09.230 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning
Jun 26 21:52:09.230 [notice] Read configuration file "/etc/tor/torrc".
Configuration was valid
~$
```
## Verify bitcoind Tor setup
You should see something like this in your debug log file to verify your ID onion address, which confirms you're using an onion hidden service that will bypass firewalls and NAT's and allows you connect to your node remotely using your ID and port.
```
$ grep -e "tor: " debug.log
```
Output
```
2020-06-25T18:16:44Z tor: Thread interrupt
2020-06-25T19:11:12Z tor: Got service ID [YOUR_ONION_ID], advertising service your_onion_id.onion:8333
```
Using bitcoin-cli you should use getnetworkinfo to get your onion id like this:
```
"localaddresses": [
{
"address": "your_onion_id.onion",
"port": 8333,
"score": 4
}
],
```
```
$ bitcoin-cli getnetworkinfo
```
Output
```
{
"version": 200000,
"subversion": "/Satoshi:0.20.0/",
"protocolversion": 70015,
"localservices": "0000000000000409",
"localservicesnames": [
"NETWORK",
"WITNESS",
"NETWORK_LIMITED"
],
"localrelay": true,
"timeoffset": 0,
"networkactive": true,
"connections": 5,
"networks": [
{
"name": "ipv4",
"limited": false,
"reachable": true,
"proxy": "",
"proxy_randomize_credentials": false
},
{
"name": "ipv6",
"limited": false,
"reachable": true,
"proxy": "",
"proxy_randomize_credentials": false
},
{
"name": "onion",
"limited": false,
"reachable": true,
"proxy": "127.0.0.1:9050",
"proxy_randomize_credentials": true
}
],
"relayfee": 0.00001000,
"incrementalfee": 0.00001000,
"localaddresses": [
{
"address": "your_onion_id.onion",
"port": 8333,
"score": 4
}
],
"warnings": ""
}
```

View File

@ -0,0 +1,30 @@
# Chapter 12.2: Changing Your Bitcoin Hidden Services
In this chapter we will show you how to create or change your local Bitcoin Hidden Service. To archieve this we need to add current user to tor or debian-tor group to guarantee that Bitcoin daemon can sets up an automatic hidden service on the first startup if it's correclty configured. Bitcoind will create a file called onion_private_key in the data directory. Further check if file control.authcookie exists like this:
```
~$ ll /run/tor/control.authcookie
-rw-r----- 1 debian-tor debian-tor 32 jun 26 09:44 /run/tor/control.authcookie
```
To avoid see a message like this in debug log file you need to add your current user to debian-tor group like this:
debug.log
```
2020-05-15T16:49:20Z tor: Authentication cookie /run/tor/control.authcookie could not be opened (check permissions)
```
```
~$ sudo usermod -a -G debian-tor [CHANGE_MY_USER]
```
If you're running a older version Tor like 0.2.7 add this lines to /etc/tor/torrc file:
```
HiddenServiceDir /var/lib/tor/bitcoin-service/
HiddenServicePort 8333 127.0.0.1:8333
HiddenServicePort 18333 127.0.0.1:18333
```
If you're running Tor version 3 bitcoind will configurate hidden services automatically to listen on.

View File

@ -0,0 +1,50 @@
# Chapter 12.3: Adding SSH Hidden Services
In this chapter we will show you how to add a ssh hidden service to login remotelly using Tor.
## Create SSH Hidden Services
To create new service you need to add some lines in your torrc file.
This should be under /etc/tor/torrc
Add this lines:
```
HiddenServiceDir /var/lib/tor/hidden-service-ssh/
HiddenServicePort 22 127.0.0.1:22
HiddenServiceAuthorizeClient stealth hidden-service-ssh
```
* HiddenServiceDir: indicates tor that you have a hidden service directory with the necessary configuration on path.
* HiddenServicePort: indicates tor port to be used, in SSH case is 22, if you want use other port you can change here.
* HiddenServiceAuthorizeClient: As it's name indicates authorize a client to connect to the hidden service.
After add lines to tor file you need to restart tor service
```
sudo /etc/init.d/tor restart
```
After restart you should have three new files like this:
```
total 24
drwx--S--- 3 debian-tor debian-tor 4096 jul 1 18:39 ./
drwx--S--- 5 debian-tor debian-tor 4096 jul 1 18:39 ../
drwx--S--- 2 debian-tor debian-tor 4096 jul 1 18:39 authorized_clients/
-rw------- 1 debian-tor debian-tor 63 jul 1 18:39 hostname
-rw------- 1 debian-tor debian-tor 64 jul 1 18:39 hs_ed25519_public_key
-rw------- 1 debian-tor debian-tor 96 jul 1 18:39 hs_ed25519_secret_key
```
The file hostname contains your id onion.
Use this address to connect to your ssh hidden service like this:
```
torify ssh <your-username>@your_new_onion_id.onion
```

View File

@ -0,0 +1,224 @@
# 15.3 Receiving Bitcoind Notifications with C
> **NOTE:** This is a draft in progress, so that I can get some feedback from early reviewers. It is not yet ready for learning.
[§15.3](15_3_Receiving_Bitcoind_Notifications_with_C.md) In this chapter we will show how to create a simple listener for bitcoin blockchain to receive notifications using ZMQ-based (http://zeromq.org/) notification interface, which is a highly scalable networking library written in C that acts like a concurrency framework. Bitcoin allows connection points where clients can subscribe to get notified about blockchain events like raw transactions or raw blocks.
## Install ZMQ
To create a blockchain listener in C you need to install ZMQ following this steps.
1. Install ZMQ
2. Create C program.
3. Compile C program.
4. Configure bitcoind to allow ZMQ notifications
5. Execute listener.
### 1. Install ZMQ
```
sudo apt-get install libzmq3-dev
```
Output
```
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
libzmq3-dev
0 upgraded, 1 newly installed, 0 to remove and 18 not upgraded.
Need to get 400 kB of archives.
After this operation, 2.227 kB of additional disk space will be used.
Get:1 http://es.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 libzmq3-dev amd64 4.2.5-1ubuntu0.2 [400 kB]
Fetched 400 kB in 0s (1.114 kB/s)
Selecting previously unselected package libzmq3-dev:amd64.
(Reading database ... 313982 files and directories currently installed.)
Preparing to unpack .../libzmq3-dev_4.2.5-1ubuntu0.2_amd64.deb ...
Unpacking libzmq3-dev:amd64 (4.2.5-1ubuntu0.2) ...
Setting up libzmq3-dev:amd64 (4.2.5-1ubuntu0.2) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
```
Then
```
sudo apt-get install libczmq-dev
```
Output:
```
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libczmq4
The following NEW packages will be installed:
libczmq-dev libczmq4
0 upgraded, 2 newly installed, 0 to remove and 18 not upgraded.
Need to get 496 kB of archives.
After this operation, 2.148 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://es.archive.ubuntu.com/ubuntu bionic/universe amd64 libczmq4 amd64 4.1.0-2 [145 kB]
Get:2 http://es.archive.ubuntu.com/ubuntu bionic/universe amd64 libczmq-dev amd64 4.1.0-2 [351 kB]
Fetched 496 kB in 0s (1.287 kB/s)
Selecting previously unselected package libczmq4:amd64.
(Reading database ... 314055 files and directories currently installed.)
Preparing to unpack .../libczmq4_4.1.0-2_amd64.deb ...
Unpacking libczmq4:amd64 (4.1.0-2) ...
Selecting previously unselected package libczmq-dev:amd64.
Preparing to unpack .../libczmq-dev_4.1.0-2_amd64.deb ...
Unpacking libczmq-dev:amd64 (4.1.0-2) ...
Setting up libczmq4:amd64 (4.1.0-2) ...
Setting up libczmq-dev:amd64 (4.1.0-2) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
```
### 2. Create C Program
Now we've installed ZMQ and we can compile our C program using it's notifications. This C program it's a simple client that subscribes to a connection point served by bitcoind and ZMQ interface and reads incoming messages.
The program use czmq.h library and receives two parameters as follows, first param is the point exposed by bitcoind that should be tcp connection point depending second param that could be one of these:
zmqpubrawblock
zmqpubrawtx
zmqpubhashtx
zmqpubhashblock
``` c
#include <czmq.h>
int main(int argc, char ** argv) {
char *zmqserver;
char *topic;
if (argc < 3) {
printf("\nUSAGE:\nchainlistener <tcp://localhost:port> <topic>\n\n");
return 0;
} else {
zmqserver = argv[1];
topic = argv[2];
}
zsock_t *socket = zsock_new_sub(zmqserver, topic);
assert(socket);
while(1) {
zmsg_t *msg;
int rc = zsock_recv(socket, "m", &msg);
assert(rc == 0);
char *header = zmsg_popstr(msg);
zframe_t *zdata = zmsg_pop(msg);
unsigned int *no = (unsigned int*)zmsg_popstr(msg);
char *data = zframe_strhex(zdata);
int len = zframe_size(zdata);
printf("Size: %d\n", len);
printf("Data: %s", data);
printf("\nNo: %d\n", *no);
free(header);
free(data);
free(no);
free(zdata);
zmsg_destroy(&msg);
sleep(1);
}
zsock_destroy(&socket);
return 0;
}
```
### 3. Compile C program
To compile this C program you have to do it with clang or gcc compiler and test zmq and czmq libraries.
```
gcc -o chainlistener chainlistener.c -I/usr/local/include -L/usr/local/lib -lzmq -lczmq
```
### 4. Configure ZMQ on bitcoind
#### ZMQ
ZeroMQ is a high-performance asynchronous messaging library that provides a message queue. ZeroMQ supports common messaging patterns (pub/sub, request/reply, client/server and others) over a variety of transports (TCP, in-process, inter-process, multicast, WebSocket and more), making inter-process messaging as simple as inter-thread messaging. As the purpose of this chapter is to show how to receive bitcoind notifications in the C language, ZMQ will be used for it.
Currently, the ZeroMQ facility only needs to have the ZeroMQ endpoint specified. ZeroMQ publish sockets prepend each data item with an arbitrary topic
prefix that allows subscriber clients to request only those items with a matching prefix.
Topics.
```
zmqpubrawblock=tcp://127.0.0.1:28332
zmqpubrawtx=tcp://127.0.0.1:28333
zmqpubhashtx=tcp://127.0.0.1:28334
zmqpubhashblock=tcp://127.0.0.1:28335
```
In this example we'll use raw tx that is the topic that notifies about new transactions in raw format.
Add this lines to bitcoin.conf file and restart daemon.
```
zmqpubrawblock=tcp://127.0.0.1:28332
zmqpubrawtx=tcp://127.0.0.1:28333
```
Then test it's working using this command
```
$ bitcoin-cli --testnet getzmqnotifications
```
Output:
```
[
{
"type": "pubrawblock",
"address": "tcp://127.0.0.1:28332",
"hwm": 1000
},
{
"type": "pubrawtx",
"address": "tcp://127.0.0.1:28333",
"hwm": 1000
}
]
```
### 5. Execute listener:
When you execute chainlistener and passes these params you'll get an output like this:
```
$ ./chainlistener tcp://127.0.0.1:28333 rawtx
Size: 250
Data: 02000000000101F5BD2032E5A9E6650D4E411AD272E391F26AFC3C9102B7C0C7444F8F74AE86010000000017160014AE9D51ADEEE8F46ED2017F41CD631D210F2ED9C5FEFFFFFF0203A732000000000017A9147231060F1CDF34B522E9DB650F44EDC6C0714E4C8710270000000000001976A914262437B129CF8592AB2EDC59C07D19C57729F72888AC02483045022100AE316D5F21657E3525271DE39EB285D8A0E89A20AB6413824E88CE47DCD0EFE702202F61E10C2A8F4A7125D5EB63AEF883D8E3584A0ECED0D349283AABB6CA5E066D0121035A77FE575A9005E3D3FF0682E189E753E82FA8BFF0A20F8C45F06DC6EBE3421079111B00
No: 67
Size: 249
Data: 0200000000010165C986992F7DAD22BBCE3FCF0BF546EDBC3C599618B04CFA22D9E64EF0CE4C030000000017160014B58E0A5CD68B249F1C407E9AAE9CD0332AAA3067FEFFFFFF02637932000000000017A914CCC47261489036CB6B9AA610857793FF5752E5378710270000000000001976A914262437B129CF8592AB2EDC59C07D19C57729F72888AC0247304402206CCC3F3B4BE01D4E532A01C2DC6BC3B53E4FFB6B494C8B87DD603EFC648A159902201653841E8B16A814DC375129189BB7CF01CFF7D269E91178645B6A97F5C7F4F10121030E20F3D2F172281B8DC747F007DF24B352248AC09E48CA64016942A8F01D317079111B00
No: 68
Size: 250
Data: 02000000000101E889CFC1FFE127BA49F6C1011388606A194109AE1EDAAB9BEE215E123C14A7920000000017160014577B0B3C2BF91B33B5BD70AE9E8BD8144F4B87E7FEFFFFFF02C34B32000000000017A914A9F1440402B46235822639C4FD2F78A31E8D269E8710270000000000001976A914262437B129CF8592AB2EDC59C07D19C57729F72888AC02483045022100B46318F53E1DCE63E7109DB4FA54AF40AADFC2FEB0E08263756BC3B7A6A744CB02200851982AF87DBABDC3DFC3362016ECE96AECFF50E24D9DCF264AE8966A5646FE0121039C90FCB46AEA1530E5667F8FF15CB36169D2AD81247472F236E3A3022F39917079111B00
No: 69
Size: 250
Data: 0200000000010137527957C9AD6CFF0C9A74597E6EFCD7E1EBD53E942AB2FA34A831046CA11488000000001716001429BFF05B3CD79E9CCEFDB5AE82139F72EB3E9DB0FEFFFFFF0210270000000000001976A914262437B129CF8592AB2EDC59C07D19C57729F72888AC231E32000000000017A9146C8D5FE29BFDDABCED0D6F4D8E82DCBFD9D34A8B8702483045022100F259846BAE29EB2C7A4AD711A3BC6109DE69AE91E35B14CA2742157894DD9760022021464E09C00ABA486AEAA0C49FEE12D2850DC03F57F04A1A9E2CC4D0F4F1459C012102899F24A9D60132F4DD1A5BA6DCD1E4E4B6C728927BA482C2C4E511679F60CA5779111B00
No: 70
.......
```
The first param it's bitcoind connection point where ZMQ will notifies all about transactions in raw format. It corresponds to this line in bitcoin.conf file zmqpubrawtx=tcp://127.0.0.1:28333
The second param is the topic selected rawtx explained above.
### For More Information
In this repository you can browse find more details about ZMQ notifications and others kind of messages. (https://github.com/Actinium-project/ChainTools/blob/master/docs/chainlistener.md)
### Summary Receiving Bitcoind Notifications with C.md
By using ZMQ framework, you can easily receive notifications by subscribing to a connection point exposed by bitcoind changing configuration file.