{"id":222,"date":"2021-01-31T11:28:40","date_gmt":"2021-01-31T19:28:40","guid":{"rendered":"http:\/\/junsun.net\/wordpress\/?p=222"},"modified":"2021-02-01T09:48:09","modified_gmt":"2021-02-01T17:48:09","slug":"setup-bitcoin-electrum-lightning-network-on-raspberry-pi-4","status":"publish","type":"post","link":"https:\/\/junsun.net\/wordpress\/2021\/01\/setup-bitcoin-electrum-lightning-network-on-raspberry-pi-4\/","title":{"rendered":"Setup Bitcoin\/ElectrumX\/Lightning Network on Raspberry Pi 4"},"content":{"rendered":"\n<p>In <a href=\"http:\/\/junsun.net\/wordpress\/2021\/01\/set-up-ubuntu-xfce4-on-headless-raspberry-pi-4-via-vnc\/\">the previous post<\/a> I described the process of setting up a headless Raspberry Pi 4 (8GB) with remote VNC desktop access.  It includes 1GB SDD drive, which is needed to run bitcoin full node with full archive of blockchain transactions.<\/p>\n\n\n\n<p>In this post I will describe the process I used to set up a powerful multi-purpose Bitcoin node for the cryptocurrency community, that includes the following elements. <\/p>\n\n\n\n<ul><li>Bitcoin core &#8211; the default and standard bitcoin full node software that started the whole thing<\/li><li>ElectrumX server &#8211; de-centralized cryptocurrency wallet server.  Wallet software needs it to make payments\/transfers etc.<\/li><li>Lighnting Network Daemon (LND) &#8211; Level 2 network built on top of bitcoin to facilitate fast and cheap small amount payments.  <\/li><\/ul>\n\n\n\n<h2>Install and Run Bitcoin Core<\/h2>\n\n\n\n<ul><li>Install Tor network for more privacy, <code>sudo apt install tor<\/code><\/li><li><a href=\"https:\/\/bitcoin.org\/en\/download\">Download the pre-built Bitcoin Core binary<\/a> for ARM Linux 64bit, untar it, and copy over to \/user\/local.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>wget https:\/\/bitcoin.org\/bin\/bitcoin-core-0.21.0\/bitcoin-0.21.0-aarch64-linux-gnu.tar.gz\ntar xzf bitcoin-0.21.0-aarch64-linux-gnu.tar.gz\nsudo cp -a bitcoin-0.21.0\/* \/usr\/local\/<\/code><\/pre>\n\n\n\n<ul><li>Run <code>bitcoin-qt -txindex<\/code> and start populating the blockchain data. It will take 2+ days and will consume &gt;350GB in disk space!<ul><li>DON&#8217;T SELECT &#8220;prune&#8221; MODE! Otherwise Electrum server won&#8217;t work.<\/li><\/ul><\/li><\/ul>\n\n\n\n<ul><li>Configure bitcoin for hardening and interactions with ElectrumX\/LND<ul><li>Create ~\/.bitcoin\/bitcoin.conf file.  See sample setup below.<\/li><li>In this setup, we don&#8217;t listen to incoming peers over IPv4\/IPv6 for enhanced privacy.  New transactions will be broadcasted to Tor network only.<\/li><li>I also disabled wallet feature for better privacy again.<\/li><li>zmq (ZeroMQ) is needed for LND connection.<\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># index all transactions; LND\/ElectrumX needs it\ntxindex=1\n\n# bitcoin-qt accept RPC or not; needed for EPS\/ElectrumX\nserver=1\n\n# EPS needs wallet feature; electrumx does not\ndisablewallet=1\n\n# don't broadcast transactions from our own wallet; will submit through Tor\n# only useful when disablewallet==0\nwalletbroadcast=0\n\n# # Maximum number of inbound+outbound connections. default 125\nmaxconnections=32\n\n# enable electrumx server and LND\nrpcuser=YOUR_RPC_USER\nrpcpassword=YOUR_RPC_PASSWORD\n\n# Listening for peers, enabled by default except when 'connect' is being used\n# we don't export bitcoin ports or accept any incoming connections,\n# because we would expose our own transactions to peers associated with our IP\n# We only connect outgoing through tor\nlisten=0\n\n# outgoing traffic use onion\/tor only (default any)\nproxy=127.0.0.1:9050\nonlynet=onion\n\n# debug logging - values: 0, 1, net, ....\ndebug=0\n\n# needed by LND; I don't understand\nzmqpubrawblock=tcp:\/\/127.0.0.1:28332\nzmqpubrawtx=tcp:\/\/127.0.0.1:28333<\/code><\/pre>\n\n\n\n<h2>Install <a href=\"https:\/\/github.com\/spesmilo\/electrumx\">ElectrumX Server<\/a><\/h2>\n\n\n\n<ul><li>Install dependencies<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install -y python3-pip libleveldb-dev\nsudo pip3 install aiohttp aiorpc\nsudo pip3 install pylru\nsudo pip3 install plyvel<\/code><\/pre>\n\n\n\n<ul><li>Clone the the repo and install into \/usr\/local.  <ul><li>Note there are 2 primary electrumx repos on github.com.  Make sure choose &#8220;spesmilo&#8221; one.   The other one, albeit older, stopped supporting bitcoin due to recent segwit address introduction.<\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone https:\/\/github.com\/spesmilo\/electrumx.git\ncd ~\/electrumx\nsudo python3 setup.py install<\/code><\/pre>\n\n\n\n<ul><li>Add user to ssl-cert group to access the private SSL key<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo usermod -a -G ssl-cert $USER<\/code><\/pre>\n\n\n\n<ul><li>If bitcoin core has finished initial data sync, you can start ElectrumX server with the following script.  Make sure you forward port 50001 and 50002 to the Raspberry Pi 4.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# REQUIRED env variables\nexport DB_DIRECTORY=$HOME\/.electrumx\/db\nexport COIN=Bitcoin\nDAEMON_URL=\"http:\/\/YOUR_RPC_USER:YOUR_RPC_PASSWORD@localhost:8332\"\n\n# Optional env variables\nexport PUBLIC_IP=$(curl -s http:\/\/whatismyip.akamai.com\/)\nexport SERVICES=rpc:\/\/localhost:8000,tcp:\/\/:50001,ssl:\/\/:50002\nexport REPORT_SERVICES=ssl:\/\/$PUBLIC_IP:50002\nexport SSL_CERTFILE=\/etc\/ssl\/certs\/ssl-cert-snakeoil.pem\nexport SSL_KEYFILE=\/etc\/ssl\/private\/ssl-cert-snakeoil.key\n\n# replace with your wallet address!\nexport DONATION_ADDRESS=\"bc1q52we5s8qyddhvrfscjn4qyn5nvq8hf92k9rt2w\"\n\n# log all sent transactions\nelectrumx_server<\/code><\/pre>\n\n\n\n<p>Install LND<\/p>\n\n\n\n<p>Lightning Network in some sense is more complex than bitcoin.  You are strongly suggested to review some basic concepts before setting up the software.   There are multiple LN server implementations.  <a href=\"https:\/\/github.com\/lightningnetwork\/lnd\">LND is a popular one<\/a>.  <\/p>\n\n\n\n<ul><li>Install the pre-built binary image for ARM64<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>wget https:\/\/github.com\/lightningnetwork\/lnd\/releases\/download\/v0.12.0-beta.rc6\/lnd-linux-arm64-v0.12.0-beta.rc6.tar.gz\ntar xzf lnd-linux-arm64-v0.12.0-beta.rc6.tar.gz\nsudo cp lnd-linux-arm64-v0.12.0-beta.rc6\/* \/usr\/local\/bin<\/code><\/pre>\n\n\n\n<ul><li>Set up config file<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir ~\/.lnd\nwget -O ~\/.lnd\/lnd.conf https:\/\/raw.githubusercontent.com\/lightningnetwork\/lnd\/master\/sample-lnd.conf<\/code><\/pre>\n\n\n\n<ul><li>Below is a sample setup based on my own node<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>listen=0.0.0.0:9735\nnolisten=false\nrpclisten=0.0.0.0:10009\nexternalip=xxx.xxx.xxx.xxx\nalias=YourFavoriateNameForThisNode\n&#91;Bitcoin]\nbitcoin.active=true\nbitcoin.mainnet=true\nbitcoin.node=bitcoind\nbitcoin.basefee=1\nbitcoin.feerate=1\n&#91;Bitcoind]\nbitcoind.dir=~\/.bitcoin\nbitcoind.rpcuser=YOUR_RPC_USER\nbitcoind.rpcpass=YOUR_RPC_PASSWORD\nbitcoind.zmqpubrawblock=tcp:\/\/127.0.0.1:28332\nbitcoind.zmqpubrawtx=tcp:\/\/127.0.0.1:28333\nbitcoind.estimatemode=ECONOMICAL<\/code><\/pre>\n\n\n\n<ul><li>Run LND with &#8220;lnd&#8221; command<ul><li>First time run, you will need to create a wallet,<code> lncli create<\/code><\/li><li>For later running, you will need to unlock wallet, <code>lncli unlock<\/code><ul><li>If you like to automate LND startup, you will likely need <code>lncli unlock --stdin<\/code> option to pass in the wallet password via a script.<\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<ul><li>(Optional) if you like to set up a public LND node so that others can reach you, enable port forward on port 9735.  You need to be a public node to participate in LN routing.<\/li><\/ul>\n\n\n\n<h2>(Optional) Install RTL<\/h2>\n\n\n\n<p><a href=\"https:\/\/github.com\/Ride-The-Lightning\/RTL\">RTL stands for &#8220;Ride The Lightning&#8221;<\/a>, which provides a web interface to visually interact with LND.  Otherwise you can just the commandline interface, which can be a little boring (and hard)<\/p>\n\n\n\n<ul><li>Install package and dependencies<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone https:\/\/github.com\/Ride-The-Lightning\/RTL.git\ncd RTL\/\nsudo apt install nodejs\nsudo apt install npm\nnpm install --only=prod\ncp sample-RTL-Config.json RTL-Config.json\nvi RTL-Config.json\nnode rtl.js <\/code><\/pre>\n\n\n\n<ul><li>In order to run &#8220;node rtl.js&#8221; successfully, you need to configure RTL-config.json properly.  Below is the sample setup derived from my own node.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"multiPass\": \"YOUR_RTL_PASSWORD\",\n  \"port\": \"3000\",\n  \"defaultNodeIndex\": 1,\n  \"SSO\": {\n    \"rtlSSO\": 0,\n    \"rtlCookiePath\": \"\",\n    \"logoutRedirectLink\": \"\"\n  },\n  \"nodes\": &#91;\n    {\n      \"index\": 1,\n      \"lnNode\": \"Node 1\",\n      \"lnImplementation\": \"LND\",\n      \"Authentication\": {\n        \"macaroonPath\": \"\/home\/ubuntu\/.lnd\/data\/chain\/bitcoin\/mainnet\/\",\n        \"configPath\": \"\/home\/ubuntu\/.lnd\/lnd.conf\",\n        \"swapMacaroonPath\": \"\"\n      },\n      \"Settings\": {\n        \"userPersona\": \"MERCHANT\",\n        \"themeMode\": \"DAY\",\n        \"themeColor\": \"PURPLE\",\n        \"channelBackupPath\": \"\/home\/ubuntu\/.lnd\/backup\",\n        \"enableLogging\": false,\n        \"lnServerUrl\": \"https:\/\/localhost:8080\",\n        \"swapServerUrl\": \"https:\/\/localhost:8081\",\n        \"fiatConversion\": false\n      }\n    }\n  ],\n}<\/code><\/pre>\n\n\n\n<ul><li>Once RTL starts running, you can open a browser and point at Raspberry Pi 4 IP address at port 3000.  You see stats and perform actions with LND.<\/li><\/ul>\n\n\n\n<h2>How You May Benefit<\/h2>\n\n\n\n<ul><li>Use your own ElectrumX server for increased trust and privacy.<ul><li>In this guide, we disable bitcoin core listening from public IP addresses.  All new transactions are broadcasted from Tor.  That increases privacy and anonymity. <\/li><\/ul><\/li><li>You set up donation wallet address in ElectrumX.  It seems people do donate, although not that common.<\/li><li>Once you set up LND properly, you can collect routing fees.<\/li><\/ul>\n\n\n\n<h2>Show Your Support<\/h2>\n\n\n\n<p>This post is only complete if I can show you how I set up the donation and LN connection. \ud83d\ude42  And your reading is only complete when you perform one of the following actions. \ud83d\ude1b<\/p>\n\n\n\n<p>For donation, Bitcoin wallet address is &#8220;bc1qyc48kmpweyx2kpqhq8n6r0ckr2fqsfvz2qxpx4&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img src=\"https:\/\/github.com\/monkey-jsun\/eroas\/raw\/main\/screenshots\/bitcoin-donation-lnd.png?raw=true\" alt=\"\"\/><\/figure>\n\n\n\n<p>For Lightning Network, my node public key is &#8220;0362a4372375bd24c5b8f8bb2ea85ae2ccf783808477e68cfb06121694b34d1927&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img src=\"https:\/\/github.com\/monkey-jsun\/eroas\/raw\/main\/screenshots\/ln-connect-pub-key.png?raw=true\" alt=\"\"\/><\/figure>\n\n\n\n<h2>Appendix A &#8211; Reference Links on Lightning Network<\/h2>\n\n\n\n<ul><li>Good over all guide, <a href=\"https:\/\/stadicus.github.io\/RaspiBolt\/\">https:\/\/stadicus.github.io\/RaspiBolt\/<\/a><\/li><li>RTL github, <a href=\"https:\/\/github.com\/Ride-The-Lightning\/RTL\">https:\/\/github.com\/Ride-The-Lightning\/RTL<\/a><\/li><li>Raspiblitz, populary but code seems pretty crappy, <a href=\"https:\/\/github.com\/rootzoll\/raspiblitz\">https:\/\/github.com\/rootzoll\/raspiblitz<\/a><\/li><li>Good intro and TODO on  lightning network, <a href=\"https:\/\/decrypt.co\/resources\/bitcoin-lightning-network\">https:\/\/decrypt.co\/resources\/bitcoin-lightning-network<\/a><\/li><li>LND github page, <a href=\"https:\/\/github.com\/lightningnetwork\/lnd\">https:\/\/github.com\/lightningnetwork\/lnd<\/a><\/li><li>A good source of LN <a href=\"https:\/\/lightning.engineering\/blog\">https:\/\/lightning.engineering\/blog<\/a><\/li><li>LND setup ; <a href=\"https:\/\/blog.lightning.engineering\/posts\/2019\/08\/15\/routing-quide-1.html\">https:\/\/blog.lightning.engineering\/posts\/2019\/08\/15\/routing-quide-1.html<\/a><\/li><li>LND operation guide :  <ul><li>https:\/\/medium.com\/@suheb__\/how-to-ride-the-lightning-447af999dcd2<\/li><\/ul><\/li><\/ul>\n\n\n\n<h2>Appendix B &#8211; How to create inbound liquidity for lightning network<\/h2>\n\n\n\n<ul><li>Free ones<ul><li><a href=\"https:\/\/lightningto.me\/\">https:\/\/lightningto.me\/<\/a><\/li><li><a href=\"https:\/\/lnbig.com\/#\/open-channel\">https:\/\/lnbig.com\/#\/open-channel<\/a><\/li><\/ul><\/li><li>Paid ones<ul><li><a href=\"https:\/\/yalls.org\/about\/\">https:\/\/yalls.org\/about\/<\/a><\/li><li><a href=\"https:\/\/www.bitrefill.com\/thor-lightning-network-channels\/?hl=en\">https:\/\/www.bitrefill.com\/thor-lightning-network-channels\/?hl=en<\/a><\/li><li><a href=\"https:\/\/lightningpowerusers.com\/home\/\">https:\/\/lightningpowerusers.com\/home\/<\/a><\/li><\/ul><\/li><li>Tips<ul><li><a href=\"https:\/\/gist.github.com\/bretton\/53bc511b6fdafef31951199dd25bbf88\">https:\/\/gist.github.com\/bretton\/53bc511b6fdafef31951199dd25bbf88<\/a><\/li><\/ul><\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post I described the process of setting up a headless Raspberry Pi 4 (8GB) with remote VNC desktop access. It includes 1GB SDD drive, which is needed to run bitcoin full node with full archive of blockchain transactions. In this post I will describe the process I used to set up a &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/junsun.net\/wordpress\/2021\/01\/setup-bitcoin-electrum-lightning-network-on-raspberry-pi-4\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Setup Bitcoin\/ElectrumX\/Lightning Network on Raspberry Pi 4&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[3],"tags":[48,58,56,57,55,59,60],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/posts\/222"}],"collection":[{"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/comments?post=222"}],"version-history":[{"count":13,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/posts\/222\/revisions"}],"predecessor-version":[{"id":241,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/posts\/222\/revisions\/241"}],"wp:attachment":[{"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/media?parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/categories?post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/tags?post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}