My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Let us Ride the lightning!

Shashank Karmakar's photo
Shashank Karmakar
·Aug 1, 2021·

5 min read

rtl.jpeg

Why do we need lightning network?

On an average it takes 10 minutes to make sure a transaction gets on the main blockchain, plus transaction fees on-chain is too high, it might even treat your transaction as dust, paying for coffee and everyday stuff won't be feasible given such constraints, hence it makes bitcoin network non-scalable, here enters the lightning network,which allows to make off-chain micro transactions with low transaction fees, lightning network is layer 2.

What is c-lightning?

c-lightning is c implementation of lightning network, there are other implementations too in RUST , GO , SCALA

What is c-lightning REST?

Let's say you want to make a payment portal in lightning network for a average user to help him pay for groceries, you can't expect him to operate via terminal, we need something more robust, with a good GUI, but making such an app can be tricky given that you have to talk to sockets in c-lightning, here enters c-lightning REST, which provides you with REST api inorder for the app to communicate to c-lightning.

What is RTL ?

To keep it simple, it is GooglePay for bitcoin network, will help you pay for your coffee in the near future :)

Let us setup the environment now!

Step1: Setup bitcoin node

  • Get the latest version of bitcoin core for bitcoincore.org

    wget https://bitcoincore.org/bin/bitcoin-core-0.21.1/bitcoin-0.21.1-x86_64-linux-gnu.tar.gz
    
  • Get the corresponding signature file

    wget https://bitcoincore.org/bin/bitcoin-core-0.21.1/SHA256SUMS.asc
    
  • Verify that the hash of the tar.gz file matches the one in SHA256SUMS.asc
    sha256sum --ignore-missing --check SHA256SUMS.asc
    
  • Lookup the fingerprint of Bitcoin Core's release key, It should be 01EA5486DE18A882D4C2684590C8019E36C2E964, But make sure that the fingerprint is authentic by, checking with multiple sources.
    gpg --keyserver hkp://keyserver.ubuntu.com --recv-keys 01EA5486DE18A882D4C2684590C8019E36C2E964
    
  • When you're sure you have the correct key, verify the signature in the signature file

    gpg --verify SHA256SUMS.asc
    

    Look for "Good signature from..." and verify that the fingerprint matches what you expect

  • Unpack Bitcoin Core

    tar -zxvf bitcoin-0.21.1-x86_64-linux-gnu.tar.gz
    
  • navigate to the bitcoin core bin
    cd bitcoin-0.21.1/bin
    
  • Start Bitcoin Core in the background (-daemon) and run on a test network, called testnet

    ./bitcoind -testnet -daemon
    
  • finally some fun part, have a look at data from the blockchain

    ./bitcoin-cli -tesnet getblockchaininfo
    

    Look for "initialblockdownload". True means it's sill syncing

  • Stop Bitcoin Core

    ./bitcoin-cli -testnet stop
    
  • Make it always run testnet in home folder make .bitcoin directory make conf file
    mkdir .bitcoin
    cd .bitcoin
    sudo nano ./bitcoin.conf
    
    the content inside the config file will be
    testnet=1
    txindex=1
    server=1
    daemon=1
    rpcuser=<rpcuser>
    rpcpassword=<password>
    zmqpubrawblock=tcp://127.0.0.1:28332
    zmqpubrawtx=tcp://127.0.0.1:28333
    
  • Since we have made daemon=1 and testnet=1 we don't need those flags anymore while running our node, just running ./bitcoind in the bin file will do the job, but we still have to lead ourselves to the bin folder, let's setup bitcoin as a path variable, go to home and run nano .profile (the name of this file can change with different distros of linux) and at the end of this file add export PATH="$PATH:<path to you bitcoind>"
  • Run source .profile and restart your pc, voila you have your path variable enabled now, you now check your blockchain info in home, no need to direct to the bin folder.
    bitcoin-cli getblockchaininfo
    
  • Let's setup bitcoin node as service in linux, this would ensure better control of this function, go to /etc/systemd/system and create a service file named bitcoind.service, an example of the service I wrote is given below, you make changes in accordance to you needs
[Unit]
Description=Bitcoin daemon
After=network.target

[Service]
ExecStartPre=/bin/sh -c 'sleep 30'
ExecStart=/home/shashank/bitcoin/bitcoin-0.21.1/bin/bitcoind -daemon -conf=/home/shashank/.bitcoin/bitcoin.conf -pid=/home/shashank/.bitcoin/bitcoind.pid
PIDFile=/home/shashank/.bitcoin/bitcoind.pid
User=shashank
Group=shashank
Type=forking
KillMode=process
Restart=always
TimeoutSec=120
RestartSec=30

[Install]
WantedBy=multi-user.target
  • Go to $HOME and verify your service code.
systemctl start bitcoind.service

systemctl status bitcoind.service

systemctl stop bitcoind.service

Step 2: Setup lightning node

  • Get all the dependencies

    sudo apt-get update
    sudo apt-get install -y \
    autoconf automake build-essential git libtool libgmp-dev \
    libsqlite3-dev python python3 net-tools zlib1g-dev
    
  • Clone lightning repo

    git clone https://github.com/ElementsProject/lightning.git
    cd lightning
    
  • For development or running tests, get additional dependencies:
    sudo apt-get install -y valgrind python3-pip libpq-dev
    sudo pip3 install -r requirements.txt
    
    if incase sudo pip3 install -r requirements.txt throws some error, try sudo pip3 install -r requirements.lock instead and then sudo pip3 install -r requirements.txt
  • Build the lightning
    ./configure
    make
    sudo make install
    
  • Configure your lightning node, in $HOME create a .lightning folder and create a config file inside that directory
    sudo nano ~/.lightning/config
    
    and the content inside the config file will be
    #local address
    bind-addr=0.0.0.0:9430
    #external address
    announce-addr=<your IP>:9430
    alias=<your alias>
    network=testnet
    rgb=003000
    
  • Forward route your router and add the port 9430 to make sure you are visible the testnet on the internet

  • Run your lightning node

    lightningd/lightningd
    

Step 3: Setup c-lightning REST node

  • Clone the repo
    git clone https://github.com/saubyk/c-lightning-REST
    cd c-lightning-REST
    npm install
    
  • For running the server, rename the file sample-cl-rest-config.json to cl-rest-config.json. Following parameters can be configured in the config file.
    {
      "PORT": 3001,
      "DOCPORT": 4001,
      "PROTOCOL": "https",
      "EXECMODE": "production",
      "RPCCOMMANDS": ["*"]
    }
    
  • Run it as an API-server via node cl-rest.js

Step4: Setup RTL

  • Clone repo and npm install
    git clone https://github.com/Ride-The-Lightning/RTL.git
    cd RTL
    npm install --only=prod
    
  • Configure the RTL, copy the file Sample-RTL-Config.json from ./RTL/docs to ./RTL and rename it to RTL-Config.json, locate the complete path of the readable macroon file, it will in the c-lightning REST directory, it is used by RTL to authenticate to c-lightning REST.
  • modify RTL-Config.json as
{
  "multiPass": <your password, whatever you wish>,
  "port": "3000",
  "defaultNodeIndex": 1,
  "SSO": {
    "rtlSSO": 0,
    "rtlCookiePath": "",
    "logoutRedirectLink": ""
  },
  "nodes": [
    {
      "index": 1,
      "lnNode": "node 1",
      "lnImplementation": "CLT",
      "Authentication": {
        "macaroonPath": "<Complete path of the folder containing admin.macaroon for the node # 1>",
        "configPath": "<Optional:Path of the .conf if present locally or empty>"
      },
      "Settings": {
        "userPersona": "OPERATOR",
        "themeMode": "DAY",
        "themeColor": "PURPLE",
        "enableLogging": true,
        "fiatConversion": false,
        "lnServerUrl": "https://localhost:3001"
      }
    }
  ]
}
  • Refer here if you have multiple nodes
  • Now finally start the server with node rtl
  • voila! thee can buyeth thy coffee via bitcoin!

Screenshot from 2021-08-01 19-27-21.png

Screenshot from 2021-08-01 19-27-28.png

If you are new to lightning network and bitcoin, then fret not, I will write more blogs explaining the intricacies of these amazing technologies.