Back to the Blog

Lightning Node Beginnings, Part 1

Overview

In this series of blog posts, we will be going over a few different technical concepts, which are at the core of running a successful Lightning routing node. There is a lot of nuance still, as the Lightning Network is still in rapid development mode, but learning some of these core lightning primitives will help you understand the inner-workings of the Lightning Network, and help enable you to be a more successful router, more successful app developer, or chicken farmer!

Getting Started

At Voltage, we make fairly extensive usage of Python, and I think it is a pretty awesome and readable language. We will be using Python and Python libraries in order to get started programmatically using your Voltage node quickly!

  1. Obtain the credentials for the Lightning node.
  2. On the voltage node, unlock your node and navigate to the CONNECT panel
  3. Download the macaroon, and TLS Certificate
  4. Take note of your node’s API endpoint

2. Move the credentials to a known place on your computer.

# Create a folder to hold the credentials
mkdir /home/voltage-user/Documents/creds
# Move the credentials
mv /home/voltage-user/Downloads/tls.cert /home/voltage-user/Documents/creds
mv /home/voltage-user/Downloads/admin.macaroon /home/voltage-user/Documents/creds

3. Create a Python “virtual environment”, and install required dependencies. There are a few ways of creating a Python environment, some of these are “venv”, and “virtualenv”. There are pros and cons to each of them, but I think “venv” is a good choice for beginners.

# Ensure venv installed
sudo apt install python3-venv
# Create the virtual environment named "env"
python3 -m venv env
# Enter the environment
source env/bin/activate

Once you do this, all packages installed using pip will be located in this environment, and won’t interfere with system packages.

4. Install dependencies.

pip install https://github.com/sako0938/lnd-grpc-client/blob/master/dist/lndgrpc-0.2.3-py3-none-any.whl?raw=true

5. Start! We will just start up a Python shell, and type the commands needed to interact with our node.

python3

Load all of the dependencies, if there are any errors, go back and make sure you installed everything correctly.

from pathlib import Path
import json
from pprint import pprint
import os
import base64

# Pip installed Modules
from lndgrpc import LNDClient
from protobuf_to_dict import protobuf_to_dict

Remember we put our node’s credentials in safe place? Use that here.

# TODO: ** UPDATE THIS PATH WITH THE LOCATION OF YOUR CREDENTIALS! **
credential_path = Path("/home/voltage-user/Documents/creds/test-nodey/")

mac = str(credential_path.joinpath("admin.macaroon").absolute())
tls = str(credential_path.joinpath("tls.cert").absolute())

Now do some sanity checking, to make sure we are really talking to our node!

lnd = LNDClient(
    "test-nodey.t.voltageapp.io:10009",
    macaroon_filepath=mac,
    cert_filepath=tls
)

# Get info about your node
info = lnd.get_info()
print(f"Node Name Is: {info.alias}")

# Save your pubkey for later
mypk = info.identity_pubkey

# Print out getinfo as a dictionary
protobuf_to_dict(info)

6. Fund your node before you can begin making channels.

# create a new segwit address
lnd.new_address(address_type=0).address

# create new wrapped segwit address
lnd.new_address(address_type=1).address

# check the on-chain balance of your node
lnd.wallet_balance()

7. Open some channels to some peers. You first need to connect to a peer before you can make a channel to them.

# TODO: ** Update these strings with node/channel
# info: pubkey@host **
ln_at_url = "031f2669adab71548fad4432277a0d90233e3bc07ac29cfb0b3e01bd3fb26cb9fa@44.242.118.94:9735"
channel_amt_sats = 200000
fee_rate = 10

lnpk, lnhost = ln_at_url.split("@")
# Connect to a new peer before you create the channel
lnd.connect_peer(
    pub_key=lnpk,
    host=lnhost,
    permanent=True
)

# Wait to be sure connection to be finished
sleep(5)

# Create some new channels
y = lnd.open_channel(
    node_pubkey=bytes.fromhex(lnpk),
    sat_per_byte=fee_rate,
    local_funding_amount=channel_amt_sats,
    private=False
)

8. Use this command to list the pending channels. Look up the channel_point on a block explorer like https://mempool.space to see when it will confirm. It takes 3 confirmations before a channel becomes active.

# Show all of the pending channels
lnd.pending_channels()

# as a dictionary
protobuf_to_dict(lnd.pending_channels())

9. Before the next blog post, you need to spend a little from your wallet balance somehow, so you have both a local_balance and a remote_balance on your node. Try some of these out and spend some satoshis:

# Pay an invoice
lnd.send_payment(payment_request="")

Stay tuned for the next blog in the series! Connect with us on our Discord chat.

SUBSCRIBE TO OUR NEWSLETTER