lnurl 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/README.md +48 -12
  4. data/lib/lnurl.rb +19 -9
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6e23197258071b756215d4f300ea391232f225a85eca0690088b116f9a3a6510
4
- data.tar.gz: ce09219efdb19a01f87ebf9e909a668e1c21129ad4faa00257d165c1b4f1b007
3
+ metadata.gz: fea4fd9e77664afe507942dc5617bae6c49a04a606112b42a23a9f97dd66df51
4
+ data.tar.gz: b2372cedd6cb920bf267ef6334e42ad4129f5573151c481874ac14f9cef50433
5
5
  SHA512:
6
- metadata.gz: 9c7ce4fcb15cd1bf2e5f240cfd5c25469eee0d8b6f644d0ed037e587a8de3cf72a17eb45245a29e53347ad5e543cc8e0d501444a5e1cb1c4b1a043ac9222ba56
7
- data.tar.gz: dc3a3fa4da4b54c3aaa5cd9ed89fc75ef51ef09c91e91a34d865e8fcb4093255ff7fe822dba2d13bf74faa3e65ff776d2079dc4208d78fb0231cb84e2510633a
6
+ metadata.gz: bda02e84e7604f33f4266bdf67805b8b531c9b2d6a0cd72f11cfaa6c7e813cd3b4e83f27fb53dd6023acddf1ec3c47c0216a32e5f0450ab54d0623121e9dd841
7
+ data.tar.gz: b34597fc4635bd7cdeac6a1a02e32427297a0eed8127e788b4566d81f181d255ba526b2b3c54bebf751db06f1a40f6233d172f795a32a1b345462815927ad3a3
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
+ *.gem
2
+
1
3
  /.bundle/
2
4
  /.yardoc
3
5
  /_yardoc/
data/README.md CHANGED
@@ -1,8 +1,16 @@
1
- # Lnurl
1
+ # LNURL tools for Ruby
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/lnurl`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ LNURL is a protocol for interaction between Lightning wallets and third-party services.
4
+
5
+ This gem provides helpers to work with LNURLs from Ruby.
6
+
7
+
8
+ ## Links:
9
+
10
+ * [LNURL: Lightning Network UX protocol RFC](https://github.com/btcontract/lnurl-rfc)
11
+ * [Awesome LNURL - a curated list with things related to LNURL](https://github.com/fiatjaf/awesome-lnurl)
12
+ * [LNURL pay flow](https://xn--57h.bigsun.xyz/lnurl-pay-flow.txt)
4
13
 
5
- TODO: Delete this and the text above, and describe your gem
6
14
 
7
15
  ## Installation
8
16
 
@@ -12,27 +20,55 @@ Add this line to your application's Gemfile:
12
20
  gem 'lnurl'
13
21
  ```
14
22
 
15
- And then execute:
16
-
17
- $ bundle install
18
-
19
23
  Or install it yourself as:
20
24
 
21
25
  $ gem install lnurl
22
26
 
23
27
  ## Usage
24
28
 
25
- TODO: Write usage instructions here
29
+ ### Encoding
30
+
31
+ ```ruby
32
+ lnurl = Lnurl.new('https://lnurl.com/pay')
33
+ puts lnurl.to_bech32 # => LNURL1DP68GURN8GHJ7MRWW4EXCTNRDAKJ7URP0YVM59LW
34
+ ```
26
35
 
27
- ## Development
36
+ ### Decoding
28
37
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+ ```ruby
39
+ Lnurl.valid?('nolnurl') #=> false
40
+
41
+ lnurl = Lnurl.decode('LNURL1DP68GURN8GHJ7MRWW4EXCTNRDAKJ7URP0YVM59LW')
42
+ lnurl.uri # => #<URI::HTTPS https://lnurl.com/pay>
43
+ ```
44
+
45
+ ### LNURL responses
46
+
47
+ ```ruby
48
+ lnurl = Lnurl.decode('LNURL1DP68GURN8GHJ7MRWW4EXCTNRDAKJ7URP0YVM59LW')
49
+ response = lnurl.response # => #<Lnurl::LnurlResponse status="OK" ...
50
+ response.status # => OK / ERROR
51
+ response.callback # => https://...
52
+ response.tag # => payRequest
53
+ response.maxSendable # => 100000000
54
+ response.minSendable # => 1000
55
+ response.metadata # => [...]
56
+
57
+ invoice = response.request_invoice(amount: 100000) # (amount in msats) #<Lnurl::InvoiceResponse status="OK"
58
+ # or:
59
+ invoice = lnurl.request_invoice(amount: 100000) # (amount in msats)
60
+
61
+ invoice.status # => OK / ERROR
62
+ invoice.pr # => lntb20u1p0tdr7mpp...
63
+ invoice.successAction # => {...}
64
+ invoice.routes # => [...]
65
+
66
+ ```
30
67
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
68
 
33
69
  ## Contributing
34
70
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lnurl.
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bumi/lnurl-ruby.
36
72
 
37
73
 
38
74
  ## License
@@ -4,14 +4,18 @@ require 'json'
4
4
  require 'ostruct'
5
5
 
6
6
  class Lnurl
7
- VERSION = '0.1.0'.freeze
7
+ VERSION = '0.1.1'.freeze
8
8
 
9
9
  InvoiceResponse = Class.new(OpenStruct)
10
10
  LnurlResponse = Class.new(OpenStruct) do
11
- def request_invoice(amount: nil, amt_msat: nil)
12
- msats = amount ? amount * 1000 : amt_msat
11
+ # amount in msats
12
+ def request_invoice(args)
13
+ args.transform_keys!(&:to_s)
13
14
  callback_uri = URI(callback)
14
- callback_uri.query = "amount=#{msats}&" + callback_uri.query.to_s
15
+ if callback_uri.query
16
+ args = Hash[URI.decode_www_form(callback_uri.query)].merge(args) # reverse merge
17
+ end
18
+ callback_uri.query = URI.encode_www_form(args)
15
19
  body = Net::HTTP.get(callback_uri)
16
20
  InvoiceResponse.new JSON.parse(body)
17
21
  end
@@ -49,17 +53,23 @@ class Lnurl
49
53
  request_invoice(amount: amount).pr
50
54
  end
51
55
 
52
- def self.lnurl?(value)
53
- value.to_s.downcase.match?(Regexp.new("^#{HRP}")) &&
54
- decode(value) rescue false
56
+ def self.valid?(value)
57
+ return false unless value.to_s.downcase.match?(Regexp.new("^#{HRP}", 'i')) # false if the HRP does not match
58
+ decoded = decode_raw(value) rescue false # rescue any decoding errors
59
+ return false unless decoded # false if it could not get decoded
60
+
61
+ return decoded.match?(URI.regexp) # check if the URI is valid
55
62
  end
56
63
 
57
64
  def self.decode(lnurl)
65
+ Lnurl.new(decode_raw(lnurl))
66
+ end
67
+
68
+ def self.decode_raw(lnurl)
58
69
  lnurl = lnurl.gsub(/^lightning:/, '')
59
70
  hrp, data = Bech32.decode(lnurl, lnurl.length)
60
71
  # raise 'no lnurl' if hrp != HRP
61
-
62
- Lnurl.new(convert_bits(data, 5, 8, false).pack('C*').force_encoding('utf-8'))
72
+ convert_bits(data, 5, 8, false).pack('C*').force_encoding('utf-8')
63
73
  end
64
74
 
65
75
  # FROM: https://github.com/azuchi/bech32rb/blob/master/lib/bech32/segwit_addr.rb
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lnurl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bumann