class Lnurl

Constants

HRP
InvoiceResponse
LnurlResponse
VERSION

Attributes

uri[R]

Public Class Methods

convert_bits(data, from, to, padding=true) click to toggle source

FROM: github.com/azuchi/bech32rb/blob/master/lib/bech32/segwit_addr.rb

# File lib/lnurl.rb, line 76
def self.convert_bits(data, from, to, padding=true)
  acc = 0
  bits = 0
  ret = []
  maxv = (1 << to) - 1
  max_acc = (1 << (from + to - 1)) - 1
  data.each do |v|
    return nil if v < 0 || (v >> from) != 0
    acc = ((acc << from) | v) & max_acc
    bits += from
    while bits >= to
      bits -= to
      ret << ((acc >> bits) & maxv)
    end
  end
  if padding
    ret << ((acc << (to - bits)) & maxv) unless bits == 0
  elsif bits >= from || ((acc << (to - bits)) & maxv) != 0
    return nil
  end
  ret
end
decode(lnurl) click to toggle source
# File lib/lnurl.rb, line 64
def self.decode(lnurl)
  Lnurl.new(decode_raw(lnurl))
end
decode_raw(lnurl) click to toggle source
# File lib/lnurl.rb, line 68
def self.decode_raw(lnurl)
  lnurl = lnurl.gsub(/^lightning:/, '')
  hrp, data = Bech32.decode(lnurl, lnurl.length)
  # raise 'no lnurl' if hrp != HRP
  convert_bits(data, 5, 8, false).pack('C*').force_encoding('utf-8')
end
new(uri) click to toggle source
# File lib/lnurl.rb, line 28
def initialize(uri)
  @uri = URI(uri)
end
valid?(value) click to toggle source
# File lib/lnurl.rb, line 56
def self.valid?(value)
  return false unless value.to_s.downcase.match?(Regexp.new("^#{HRP}", 'i')) # false if the HRP does not match
  decoded = decode_raw(value) rescue false # rescue any decoding errors
  return false unless decoded # false if it could not get decoded

  return decoded.match?(URI.regexp) # check if the URI is valid
end

Public Instance Methods

data() click to toggle source
# File lib/lnurl.rb, line 37
def data
  self.class.convert_bits(uri.to_s.codepoints, 8, 5, true)
end
encode()
Alias for: to_bech32
payment_request(amount:) click to toggle source
# File lib/lnurl.rb, line 52
def payment_request(amount:)
  request_invoice(amount: amount).pr
end
request_invoice(amount:) click to toggle source
# File lib/lnurl.rb, line 48
def request_invoice(amount:)
  response.request_invoice(amount: amount)
end
response() click to toggle source
# File lib/lnurl.rb, line 41
def response
  @response ||= begin
                  body = Net::HTTP.get(uri) # TODO: redirects?
                  LnurlResponse.new JSON.parse(body)
                end
end
to_bech32() click to toggle source
# File lib/lnurl.rb, line 32
def to_bech32
  Bech32.encode(HRP, data).upcase
end
Also aliased as: encode