Bitcoin — Setup your own ⚡lightning ⚡ node on mainnet!

Ismail Akkila
Coinmonks

--

I have been following bitcoin and cryptocurrencies in general for the past 2 years. I think I have seen all the documentaries around the topic! An excellent resource for the technically inclined is “Mastering Bitcoin” by Andreas Antonopolous. You should check out his talks on youtube.

I wish I got involved in bitcoin’s early days in running my own nodes or mining equipment. But now, with the upcoming lightning network and the chance to be part of bitcoin’s second layer scaling solution, I knew I had to run my own lightning node and just get involved. This post will describe my experience.

Get Best Software Deals Directly In Your Inbox

Disclaimer: The lightning network is still not production ready and not recommended for high value transactions. Current implementations can contain bugs which may lead to a loss of funds. Please proceed after conducting your own diligence.

Getting Started

I had my own Ubuntu 16.04 server which had enough storage to sync up with the bitcoin blockchain. You will need bitcoind and lightningd also. Here are the steps:

  1. Install bitcoind:
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:bitcoin/bitcoin
$ sudo apt-get update
$ sudo apt-get install -y bitcoind
$ bitcoind --daemon --datadir=<optional/path/to/bitcoind>

You can specify an optional data directory where the blockchain data will be downloaded. In my setup, I used my external mounted storage as my data directory due to the space requirements (169 GB and increasing by ~1 MB every 10 mins).

2. Allow the node to fully sync with the blockchain. Monitor the progress:

$ bitcoin-cli --datadir=<optional/path/to/bitcoin> getinfo{
"deprecation-warning": "WARNING: getinfo is deprecated and will be fully removed in 0.16. Projects should transition to using getblockchaininfo, getnetworkinfo, and getwalletinfo before upgrading to 0.16",
"version": 150100,
"protocolversion": 70015,
"walletversion": 139900,
"balance": 0.00000000,
"blocks": 507134,
"timeoffset": -1,
"connections": 42,
"proxy": "",
"difficulty": 2603077300218.593,
"testnet": false,
"keypoololdest": 1516978369,
"keypoolsize": 2000,
"paytxfee": 0.00000000,
"relayfee": 0.00001000,
"errors": ""
}

Look out for the number of blocks. You will need to be synced up to the current block height. You can check the current height here.

3. Install lightningd:

$ git clone https://github.com/ElementsProject/lightning.git
$ cd lightning
$ make
$ screen -S lightningd
$ lightningd --alias=<your_alias> --rgb=<your_hex_color_value> --network=bitcoin --lightning-dir=<optional/path/to/lightningd> --bitcoin-datadir <optional/path/to/bitcoind> --log-level=debug
[CTRL A, then D] to detach the screen session. You can reattach with: screen -r lightningd

You can specify an optional data directory for lightning also. You will also need to specify your bitcoind data directory.

Initial Setup of your Lightning Node

4. Get a lightning deposit address so that you send funds to it (I put in 0.003BTC). Think of this address as your hot wallet:

$ lightning-cli newaddr
{ "address" : "39b4uZiuuDfCdymLs3mapAPagEWp2u9mVr" }

You can check your balance/funds:

$ lightning-cli --lightning-dir=<optional/path/to/lightning> listfunds
{ "outputs" :
[
{ "txid" : "a7f2741a0950019c020caad26f0c3600d2f20e8f85344bf23b0153c7973489a6", "output" : 0, "value" : 49117 },
{ "txid" : "42da2d234675e08ea21ab64ec63638529bdef29d560cf53d54b78771376a0654", "output" : 0, "value" : 221965 }
]
}

Setup your Lightning Payment Channels

5. Connect to another lightning node to create a payment channel (I connected to SLEEPYARK which looks very well connected):

$ lightning-cli --lightning-dir=<optional/path/to/lightning> connect 02f6725f9c1c40333b67faea92fd211c183050f28df32cac3f9d69685fe9665432 104.198.32.198 9735

Check out the present deployment of the lightning network. At this time, we are up to 417 nodes on mainnet!

You can verify the channel status with your peers:

$ lightning-cli --lightning-dir=<optional/path/to/lightning> listpeers{ "peers" :
[
{ "state" : "GOSSIPING", "id" : "02f6725f9c1c40333b67faea92fd211c183050f28df32cac3f9d69685fe9665432", "netaddr" :
[ "104.198.32.198:9735" ], "connected" : true, "owner" : "lightning_gossipd" }
]
}

6. Now, you will need to fund this payment channel. You will need to specify that peer id and the satoshi amount (In my case, 230000 satoshis/ 0.0023 BTC):

$ lightning-cli --lightning-dir=<optional/path/to/lightning> fundchannel 02f6725f9c1c40333b67faea92fd211c183050f28df32cac3f9d69685fe9665432 230000{ "tx" : "0200000000010254066a377187b7543df50c569df2de9b523836c64eb61aa28ee07546232dda420000000000ffffffffa6893497c753013bf24b34858f0ef2d200360c6fd2aa0c029c0150091a74f2a70000000000ffffffff025b120000000000001600149befef0cd1e1e281d9efa23aa275d98d64fdb9ae7082030000000000220020f77d81d394fdce27f7f4419464723cd1b1af927600d57362024d75bbe278288902483045022100e0ffe33682c8cc2b9fcd087ac5616ea2034e370f846f212c879c162fd769aca002205e768ed3bdf6f81ffdc9ebe2f8f2fc37e967dc28296ba0136379e58cb621f4b8012103be9c9ff74a27576357def749de3d8bdf71678a6f3299613832fb6a4069cdced102473044022006ba24e444dbc229c05892ce834560a795a5966688f2470dc79652c9e63b1830022064cb139d7355ed8ed1ae1d381b8db7bd3df86b07a4655323c032fed2ecbe5378012103c422f654d078785baddf803d586d29dd188deaf41801fe4e73b3f3f8e9666c5a00000000", "txid" : "066363dcd11c9f5268fee778aed72f290bef3bfa2a32d77611fbd4a3ec7f0ef3" }

Keep note of the txid value. This is your funding transaction hash that you can lookup on the blockchain. Wait for at least 6 confirmations before moving ahead. In the meantime, your peer status should look like this:

$ lightning-cli --lightning-dir=<optional/path/to/lightning> listpeers{ "peers" :
{ "id" : "02f6725f9c1c40333b67faea92fd211c183050f28df32cac3f9d69685fe9665432", "connected" : true, "netaddr" :
[ "104.198.32.198:9735" ], "channels" :
[
{ "state" : "CHANNELD_AWAITING_LOCKIN", "owner" : "lightning_channeld", "funding_txid" : "066363dcd11c9f5268fee778aed72f290bef3bfa2a32d77611fbd4a3ec7f0ef3", "msatoshi_to_us" : 230000000, "msatoshi_total" : 230000000, "dust_limit_satoshis" : 546, "max_htlc_value_in_flight_msat" : 18446744073709551615, "channel_reserve_satoshis" : 0, "htlc_minimum_msat" : 0, "to_self_delay" : 144, "max_accepted_htlcs" : 483 }
]
}
]
}

After 6 confirmations, look for the CHANNELD_NORMAL state:

$ lightning-cli --lightning-dir=<optional/path/to/lightning> listpeers{ "peers" :
[
{ "id" : "02f6725f9c1c40333b67faea92fd211c183050f28df32cac3f9d69685fe9665432", "connected" : true, "netaddr" :
[ "104.198.32.198:9735" ], "channels" :
[
{ "state" : "CHANNELD_NORMAL", "owner" : "lightning_channeld", "short_channel_id" : "507047:2309:1", "funding_txid" : "066363dcd11c9f5268fee778aed72f290bef3bfa2a32d77611fbd4a3ec7f0ef3", "msatoshi_to_us" : 91217997, "msatoshi_total" : 230000000, "dust_limit_satoshis" : 546, "max_htlc_value_in_flight_msat" : 18446744073709551615, "channel_reserve_satoshis" : 0, "htlc_minimum_msat" : 0, "to_self_delay" : 144, "max_accepted_htlcs" : 483
}
]
}
]
}

At this point, you will see that your lightning wallet balance has changed to reflect the deduction made for the funding the payment channel:

$ lightning-cli --lightning-dir=<optional/path/to/lightning> listfunds{ "outputs" :
[
{ "txid" : "066363dcd11c9f5268fee778aed72f290bef3bfa2a32d77611fbd4a3ec7f0ef3", "output" : 0, "value" : 4699 }
]
}

Be reckless and make your lightning payment!!

7. I have purchased some stickers on the blockstream store. Here is what the invoice looks like:

You can decode this BOLT11 payment encoded invoice. You will see that all the required payment details are included:

$ lightning-cli --lightning-dir=<optional/path/to/lightning> decodepay lnbc1387820030p1pd8xfrrpp57xyr5w3f6cn40xduw5qwk03v3s7x5dtuhpre4xrt7xk24m6rpt2sdy2gfkx7cmtwd68yetpd5azqvfj9cunvgz42dzzqen0wgsyymr0vd4hxarjv4sk6gznv96x2mrvd96x2gznw35kx6m9wgs8sgpjypskuepqf35kw6r5de5kueeq2d6xjcmtv4ezq7pqxgcqpga5zs3g6r08rkfe4zah33emc7krwcz99v9kvzymv7xnnxeg7eflkp54qj4jsc9mf946h300an57cd0ht5rsmw9kspxsume7pfjjjvvqcp42w7mq{ "currency" : "bc", "timestamp" : 1517495395, "created_at" : 1517495395, "expiry" : 3600, "payee" : "02f6725f9c1c40333b67faea92fd211c183050f28df32cac3f9d69685fe9665432", "msatoshi" : 138782003, "description" : "Blockstream: 12.96 USD for Blockstream Satellite Sticker x 2 and Lightning Sticker x 2", "min_final_cltv_expiry" : 8, "payment_hash" : "f1883a3a29d6275799bc7500eb3e2c8c3c6a357cb8479a986bf1acaaef430ad5", "signature" : "3045022100ed0508a34379c764e6a2ede31cef1eb0dd8114ac2d98226d9e34e66ca3d94fec02201a5412aca182ed25aeaf17bfb3a7b0d7dd741c36e2da013439bcf82994a4c603" }

For the curious, you can see how this payment will be routed within the lightning network. It looks like the blockstream node is 9 hops away:

$ lightning-cli --lightning-dir=<optional/path/to/lightning> getroute 02f6725f9c1c40333b67faea92fd211c183050f28df32cac3f9d69685fe9665432 138782003 0
{ "route" :
[
{ "id" : "02f6725f9c1c40333b67faea92fd211c183050f28df32cac3f9d69685fe9665432", "channel" : "507047:2309:1", "msatoshi" : 138782003, "delay" : 9 } ]
}

Finally, you can pay (Its quite fast so try not to blink!):

$ lightning-cli --lightning-dir=<optional/path/to/lightning> pay lnbc1387820030p1pd8xfrrpp57xyr5w3f6cn40xduw5qwk03v3s7x5dtuhpre4xrt7xk24m6rpt2sdy2gfkx7cmtwd68yetpd5azqvfj9cunvgz42dzzqen0wgsyymr0vd4hxarjv4sk6gznv96x2mrvd96x2gznw35kx6m9wgs8sgpjypskuepqf35kw6r5de5kueeq2d6xjcmtv4ezq7pqxgcqpga5zs3g6r08rkfe4zah33emc7krwcz99v9kvzymv7xnnxeg7eflkp54qj4jsc9mf946h300an57cd0ht5rsmw9kspxsume7pfjjjvvqcp42w7mq{ "preimage" : "1af7e9d5f8942bc7cffb0512441679a7a0f30436706f13621528932c908d8b39" }

Here is my lightning payment experience:

Hope someone finds this useful! Thanks to all the developers involved in making this happen. I personally cant wait for this to go mainstream.

--

--

Ismail Akkila
Coinmonks

I live and breathe technology. Curious about programming, bitcoin and cybersecurity.