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.0"

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Lightning.



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

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"))
  macaroon_binary ||= ::File.read(::File.expand_path(@options[:macaroon_path] || "~/.lnd/data/chain/bitcoin/testnet/admin.macaroon"))
  @options[:macaroon] = macaroon_binary.each_byte.map { |b| b.to_s(16).rjust(2,'0') }.join
  @lnd_client = Lnrpc::Lightning::Stub.new(@options[:address], GRPC::Core::ChannelCredentials.new(@options[:credentials]))
end

Instance Method Details

#call(env) ⇒ Object



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

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



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

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



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

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)


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

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



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

def price
  @price
end

#used?(preimage_hash_hex) ⇒ Boolean

Returns:

  • (Boolean)


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

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