/ln-markdown-to-pdf

An example for machine to machine payments using the bitcoin lightning network

Primary LanguageRuby

Markdown to html/pdf

This is a demo for machine to machine bitcoin lightning payments

This app exposes HTTP endpoints to convert markdown files to HTML or PDF after the client paid a lightning invoice.

This is a simple Sinatra.rb app that uses the rack-lightning middleware to handle lightning invoices.

Video Demo

I've made a video giving an overview of the setup and showing how it works: https://vimeo.com/302338717

How does it work?

  1. If no proof of payment is provided the rack lightning middleware creates a Lightning invoice and returns a 402 Payment Required HTTP status code with a application/vnd.lightning.bolt11 content type header and a Lightning invoice as a body.
  2. The client pays the invoice and does a second request providing the proof of payment / the preimage of the Lightning payment in a X-Preimage header.
  3. Now the lightning middleware checks the if the invoice was paid and proceeds to the sinatra app doing the convertion.

Have a look at the server code:

  • config.ru - where the magic happens: loading the lightning middleware
  • converter.rb - the actual sinatra app (no lightning related code in here; all handled by the middleware)

Usage details about the rack lightning middleware can be found here

Requirements

Server and client are using connecting (gRPC) to the Lightning Network Daemon(lnd) to create and pay invoices.. A running node with funded channels is required. Details about lnd can be found on their github page

API endpoints

The application is deployed on https://lightning-2pdf.herokuapp.com and connected to the lightning node on 79.137.71.183

The following endpoints are available:

POST /convert/pdf

converts markdown in the request body to PDF and returns the PDF file content

POST /convert/html

converts the markdown in the request body to HTML and returns the html file content

Client

requirements: you need a running lnd node with funded, open channels. Check the lnd github page for information about lnd.

The API client uses the faraday HTTP library and the faraday_ln_paywall middleware to automatically pay the lightning invoice for every request.

Have a look at the code in client.rb and have a look at the faraday lightning middleware

Usage:

$ ruby client.rb [path to markdown file] [format]
$
$ # example:
$ ruby client.rb ./README.md html

The client is using default lnd settings and loads ~/.lnd/data/chain/bitcoin/testnet/admin.macaroon and ~/.lnd/tls.cert You can configure that in the faraday middleware. (documentation)

Questions?

Please let me know if you have any questions and I'd love to hear feedback and your ideas on this: hello@michaelbumann.com
more contact details on my website: michaelbumann.com

What is the Lightning Network?

The Lightning Network allows to send real near-instant microtransactions with extremely low fees. It is a second layer on top of the Bitcoin network (and other crypto currencies). Thanks to this properties it can be easily used for automated machine to machine payments.

License

The gem is available as open source under the terms of the MIT License.