2018-10-27 18:05:01 +00:00
# Faraday middleware for lightning paywalls
2018-10-27 12:37:12 +02:00
2018-10-27 20:01:58 +02:00
This is a [Faraday ](https://github.com/lostisland/faraday#readme ) middleware that handles payment requests by the server
and sends Bitcoin [lightning payments ](https://lightning.network/ ).
2018-10-27 12:37:12 +02:00
2018-10-27 20:01:58 +02:00
## How does it work?
This Faraday middleware checks if the server responds with a `402 Payment Required` HTTP status code and
2018-10-27 18:04:35 +00:00
a lightning invoice ([BOLT11 ](https://github.com/lightningnetwork/lightning-rfc/blob/master/11-payment-encoding.md )).
2018-10-27 20:01:58 +02:00
If so it pays the invoice through the connected [lnd node ](https://github.com/lightningnetwork/lnd/ ) and performs
a second request with the proof of payment.
2018-10-27 18:04:21 +00:00
### How does the server side look like?
2018-10-27 20:01:58 +02:00
2018-10-27 18:04:21 +00:00
Have a look at [@philippgille's related server implementation in Go ](https://github.com/philippgille/ln-paywall ).
2018-10-27 20:01:58 +02:00
## Requirements
The middleware uses the gRPC service provided by the [Lightning Network Daemon(lnd) ](https://github.com/lightningnetwork/lnd/ ).
A running node with funded channels is required. Details about lnd can be found on their [github page ](https://github.com/lightningnetwork/lnd/ )
2018-10-27 12:37:12 +02:00
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'faraday_ln_paywall'
```
2018-10-27 20:01:58 +02:00
## Usage
Simply use the `FaradayLnPaywall::Middleware` in your Faraday connection:
2018-10-27 12:37:12 +02:00
2018-10-27 20:01:58 +02:00
```ruby
conn = Faraday.new(:url => 'https://api.lightning.ws') do |faraday|
faraday.use FaradayLnPaywall::Middleware, { max_amount: 100 }
faraday.adapter Faraday.default_adapter
end
puts conn.get("/translate?text=Danke&to=en").body
2018-10-27 12:37:12 +02:00
2018-10-27 20:01:58 +02:00
```
2018-10-27 12:37:12 +02:00
2018-10-27 20:01:58 +02:00
## Configuration
2018-10-27 12:37:12 +02:00
2018-10-27 20:01:58 +02:00
The middleware accepts the following configuration options:
2018-10-27 12:37:12 +02:00
2018-10-27 18:03:11 +00:00
* `max_amount` : the maximum amount of an invoice that will automatically be paid. Raises a `FaradayLnPaywall::PaymentError` if the server request a higher amount
* `address` : the address of the lnd gRPC service( default: `localhost:10009` )
2018-10-28 23:10:36 +01:00
* `credentials_path` : path to the tls.cert (default: `~/.lnd/tls.cert` )
* `macaroon_path` : path to the macaroon path (default: `~/.lnd/data/chain/bitcoin/testnet/admin.macaroon` )
2018-10-27 12:37:12 +02:00
2018-10-27 20:01:58 +02:00
## What is the Lightning Network?
2018-10-27 12:37:12 +02:00
2018-10-27 20:01:58 +02:00
The [Lightning Network ](https://en.wikipedia.org/wiki/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 used to monetize APIs.
2018-10-27 12:37:12 +02:00
## Contributing
2018-10-27 18:04:21 +00:00
Bug reports and pull requests are welcome on GitHub at https://github.com/bumi/faraday_ln_paywall.
2018-10-27 12:37:12 +02:00
## License
The gem is available as open source under the terms of the [MIT License ](http://opensource.org/licenses/MIT ).