Installing Electrs ontop of Dojo

A guide on how to utilize your Dojo backend with Electrs Private Server to connect hardware devices (i.e Trezor, Cold Card, Ledger) to your own Full Dojo node completely over Tor.

Prep Dojo

In order to connect to External Apps via RPC (like Electrs, Explorer, and LND) you'll need to modify the default settings of your Dojo.

You must be on Dojo v1.2 to not produce errors.

$ cd dojo/docker/my-dojo
$ sudo ./dojo.sh stop
$ cd conf
$ sudo nano docker-bitcoind.conf

#Change Line 64: BITCOIND_RPC_EXTERNAL=on
#IF YOU ARE USING A DIFFERENT MACHINE OTHER THAN PI (which you should be)
#You must change line 73: to the IP of your Pi

Ctrl+X, y, Enter
$ cd ..
$ sudo ./dojo.sh start

You Dojo is now ready to connect to external apps.

Getting Started

Electrs is a unique build of ElectrumX server as it is written in Rust language and much more efficient on your resources. So first let's install Rust:

$ sudo curl -sf -L https://static.rust-lang.org/rustup.sh | sh

If you'd like to few this script and verify its source. Go to https://doc.rust-lang.org/1.0.0/book/installing-rust.html

Now, lets set up the environment so we can properly install:

$ source $HOME/.cargo/env
#NOTE: this step is extremely important

Next we need to get clang in order for rust to build the rust-rocksdb

$ sudo apt update
$ sudo apt install -y clang cmake  # for building 'rust-rocksdb'

Now we have the required tools to build electrs. Let's pull the code:

$ sudo git clone https://github.com/romanz/electrs
$ cd /home/***USER***/electrs
$ sudo cargo build --release

Now this is going to take some time. If you get an error at the end, re-enter the environment comment from above ($ source $HOME/.cargo/env) and run again.

Now in a new window we can start prepping the database

$ sudo mkdir /mnt/hdd/electrs/db 

Note: This recommended to be on your external device your dojo is set up on. You can expect the full index to be around 40-50GB.

Once the install of Electrs is complete, before we run it. Let's go ahead and create our config.toml file.

$ nano /home/**USER**/.electrs/config.toml
#Paste the following in the file:
verbose = 4
timestamp = true
jsonrpc_import = true
db_dir = "/mnt/hdd/electrs/db"
cookie = "$DOJO_RPC_USER:$DOJO_RPC_PASS"

#save and exit with Ctrl+X, y, Enter

Lock down the port 50001 via UFW (even tho we will be connecting thru Tor, best to lock it down)

$ sudo ufw allow from 192.168.X.X/24 to any port 50001 comment 'Electrs access restricted to local LAN only' 

Start Electrs

$ cd /home/**user**/electrs
$ cargo run --release -- -vvv --timestamp  --index-batch-size=100 --db-dir /mnt/hdd/electrs/db --electrum-rpc-addr="0.0.0.0:50001" --daemon-rpc-addr="<YOUR.DOJO.EXTERNAL.APP.IP:28256"

Important to note that you must enable external apps in your Dojo bitcoin.conf file. Unless you are utilizing your Dojo's RPC via Local LAN, ie from Pi to Laptop, you can keep the IP address as 127.0.0.1 . However, if you are using other apps on your Local LAN, you'll need to substitute the above IP with the IP of your RPi4.

If you get errors, enter Ctrl+C and enter the following:

$ sudo /home/**user**/.cargo/bin/cargo run --release -- --index-batch-size=100 --electrum-rpc-addr="0.0.0.0:50001"

Now that is going to take awhile to sync. So lets setup tor as a service outside the docker. (Guide credit: https://github.com/s2l1/Headless-Samourai-Dojo/blob/master/README.md#8-tor)

$ sudo nano /etc/apt/sources.list
#add the following:
deb https://deb.torproject.org/torproject.org stretch main
deb-src https://deb.torproject.org/torproject.org stretch main
#Save and exit with: Ctrl+X, y, Enter

In order to verify the integrity of the Tor files, download and add the signing keys of the torproject using the network certificate management service (dirmngr).

$ sudo apt install dirmngr apt-transport-https
$ curl https://deb.torproject.org/torproject.org/A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89.asc | gpg --import
$ gpg --export A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89 | apt-key add -

Create a new user for Tor

$ sudo useradd -m toruser
##create password and remember it
$ sudo usermod -aG sudo toruser
$ su toruser

The latest version of Tor can now be installed. While not required, tor-arm provides a dashboard that you might find useful.

$ sudo apt update
$ sudo apt install tor tor-arm

edit the torrc

$ sudo nano /etc/tor/torrc
# uncomment:
ControlPort 9051
CookieAuthentication 1

# add:
CookieAuthFileGroupReadable 1
# also add:

# Hidden Service for Electrum Server
HiddenServiceDir /var/lib/tor/hidden_service/
HiddenServiceVersion 3
HiddenServicePort 50001 127.0.0.1:50001

#Save and exit: Ctrl+X, y, Enter

$ sudo systemctl restart tor

Check the onion address of your new hidden service

$ sudo cat /var/lib/tor/hidden_service/hostname
<your-onion-address>.onion

## At this point you can switch back to your main account/electrs account if you created a new user for it
$ su **username**

Install Electrum on Host device by following the steps at: https://electrum.org/#download

Now that Electrum is downloaded on your host device we are going to set it up to connect thru Tor (so if you don't have tor or tor browser set up do that now) Note: If you use tor browser the Port is 9051, tor service is 9050.

Once Electrum is downloaded it is important to make sure you are in the right env and PATH so the commands work properly:

$ PATH=$PATH:~/.local/bin
$ touch ~/.profile
$ export PATH
$ ~/.profile

Start electrum with the Tor Browser open (proxy on port 9150)

$ electrum --oneserver --server Tor_address.onion:50001:t --proxy socks5:127.0.0.1:9150

With Tor installed and running (proxy on port 9050):

$ electrum --oneserver --server <your-onion-address>.onion:50001:t --proxy socks5:127.0.0.1:9050

"If you try to connect to Electrs before the index is complete, it will not appear to be connecting. You MUST wait for index to be complete before Electrs will allow for Electrum to connect. This took me about 8ish hours on my Pi4."

Credits

Heavily utilized the guides provided by:

openoms @ https://github.com/openoms/bitcoin-tutorials/tree/master/electrs

Romanz @ https://github.com/romanz/electrs

And bit shout out to the Dojo Telegram who helped along the way. Join the group at https://t.me/samourai_dojo

DM me on Twitter: @BTCxZelko or Telegram @BTCxZelko if you have any questions.

Last updated