Lightning Network

bitpilot provides Lightning (L2) support through two main classes:

Install


pip install bitpilot[lightning]   # LightningWallet + L402Client
pip install bitpilot[l402]        # L402Client only (no gRPC)

LightningWallet

Connect to LND


from bitpilot.lightning import LightningWallet

ln = LightningWallet.from_lnd(
    macaroon_path="~/.lnd/data/chain/bitcoin/testnet/admin.macaroon",
    tls_cert_path="~/.lnd/tls.cert",
    host="localhost:10009",
)

Connect to CLN


ln = LightningWallet.from_cln(socket_path="/path/to/lightning-rpc")

Both constructors validate credentials and verify connectivity at init time. Invalid config raises LightningConfigError; auth failures raise LightningAuthError.

Operations


invoice = await ln.create_invoice(amount_sats=1000, memo="test")
payment = await ln.pay_invoice(bolt11="lnbc...")
balance = await ln.get_balance()
channels = await ln.list_channels()

Models

ModelKey Fields
LightningInvoicepayment_hash, bolt11, amount_sats, status, preimage
LightningPaymentpayment_hash, bolt11, amount_sats, fee_sats, status
LightningBalancelocal_balance_sats, remote_balance_sats, total_channels
LightningChannelchannel_id, capacity_sats, local_balance_sats, status

L402Client

Auto-pays HTTP 402 challenges and retries the request with proof-of-payment headers.


from bitpilot.lightning import LightningWallet, L402Client

ln = LightningWallet.from_lnd(...)

async with L402Client(ln) as client:
    response = await client.get("https://api.example.com/data")
    response.raise_for_status()
    print(response.json())

Parameters

ParameterDefaultDescription
max_fee_sats100Maximum fee allowed per L402 payment
auto_pay_methods{"GET", "HEAD", "OPTIONS"}HTTP methods that auto-pay
clientNoneCustom httpx.AsyncClient (optional)

Challenge Extraction

The client parses L402 challenges from:

  1. WWW-Authenticate header (standard)
  2. x-l402 header
  3. JSON response body

Exceptions

Lightning Exceptions