Class: Rack::Lightning

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/lightning.rb,
lib/rack/lightning/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Lightning

Returns a new instance of Lightning.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rack/lightning.rb', line 8

def initialize(app, options={})
  @app = app
  @invoice_storage = {} # TODO: don't store this in memory!
  @options = options
  @price = @options[:price] || 100
  @options[:address] ||= 'localhost:10009'
  @options[:timeout] ||= 5
  @options[:credentials] ||= ::File.read(::File.expand_path(@options[:credentials_path] || "~/.lnd/tls.cert"))
  @options[:macaroon] ||= begin
    macaroon_binary = ::File.read(::File.expand_path(@options[:macaroon_path] || "~/.lnd/data/chain/bitcoin/testnet/admin.macaroon"))
    macaroon_binary.each_byte.map { |b| b.to_s(16).rjust(2,'0') }.join
  end
  @lnd_client = Lnrpc::Lightning::Stub.new(@options[:address], GRPC::Core::ChannelCredentials.new(@options[:credentials]))
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/rack/lightning.rb', line 23

def call(env)
  if self.paid?(env)
    @app.call(env)
  else
    invoice = self.generate_invoice(env)
    [402, { 'Content-Type' => 'application/vnd.lightning.bolt11' }, [invoice.payment_request]]
  end
end

#generate_invoice(env) ⇒ Object



70
71
72
73
# File 'lib/rack/lightning.rb', line 70

def generate_invoice(env)
  invoice_request = Lnrpc::Invoice.new(memo: "API request #{env['REQUEST_URI']}", value: self.price)
  @lnd_client.add_invoice(invoice_request, { metadata: { macaroon: @options[:macaroon] }})
end

#hash_preimage(preimage) ⇒ Object



64
65
66
67
68
# File 'lib/rack/lightning.rb', line 64

def hash_preimage(preimage)
  hexdecoded = preimage.pach('H*')
  hash = Digest::SHA256.digest(hexdecoded)
  hash.each_byte.map { |b| b.to_s(16).rjust(2, '0') }.join
end

#paid?(env) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rack/lightning.rb', line 36

def paid?(env)
  preimage = env['HTTP_X_PREIMAGE']
  return false unless preimage

  hexdecoded = [preimage].pack('H*')
  preimage_hash = Digest::SHA256.digest(hexdecoded)
  preimage_hash_hex = preimage_hash.each_byte.map { |b| b.to_s(16).rjust(2, '0') }.join

  return false if used?(preimage_hash_hex)

  invoice = Lnrpc::PaymentHash.new(r_hash_str: preimage_hash_hex, r_hash: preimage_hash)
  begin
    response = @lnd_client.lookup_invoice(invoice, { metadata: { macaroon: @options[:macaroon] }})
    if response.settled
      @invoice_storage[preimage_hash_hex] = true
      return true
    else
      return false
    end
  rescue Exception => e
    return false
  end
end

#priceObject



32
33
34
# File 'lib/rack/lightning.rb', line 32

def price
  @price
end

#used?(preimage_hash_hex) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/rack/lightning.rb', line 60

def used?(preimage_hash_hex)
  !@invoice_storage[preimage_hash_hex].nil?
end