lnurl 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/README.md +48 -12
- data/lib/lnurl.rb +19 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fea4fd9e77664afe507942dc5617bae6c49a04a606112b42a23a9f97dd66df51
|
4
|
+
data.tar.gz: b2372cedd6cb920bf267ef6334e42ad4129f5573151c481874ac14f9cef50433
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bda02e84e7604f33f4266bdf67805b8b531c9b2d6a0cd72f11cfaa6c7e813cd3b4e83f27fb53dd6023acddf1ec3c47c0216a32e5f0450ab54d0623121e9dd841
|
7
|
+
data.tar.gz: b34597fc4635bd7cdeac6a1a02e32427297a0eed8127e788b4566d81f181d255ba526b2b3c54bebf751db06f1a40f6233d172f795a32a1b345462815927ad3a3
|
data/README.md
CHANGED
@@ -1,8 +1,16 @@
|
|
1
|
-
#
|
1
|
+
# LNURL tools for Ruby
|
2
2
|
|
3
|
-
|
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
|
-
|
29
|
+
### Encoding
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
lnurl = Lnurl.new('https://lnurl.com/pay')
|
33
|
+
puts lnurl.to_bech32 # => LNURL1DP68GURN8GHJ7MRWW4EXCTNRDAKJ7URP0YVM59LW
|
34
|
+
```
|
26
35
|
|
27
|
-
|
36
|
+
### Decoding
|
28
37
|
|
29
|
-
|
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/
|
71
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bumi/lnurl-ruby.
|
36
72
|
|
37
73
|
|
38
74
|
## License
|
data/lib/lnurl.rb
CHANGED
@@ -4,14 +4,18 @@ require 'json'
|
|
4
4
|
require 'ostruct'
|
5
5
|
|
6
6
|
class Lnurl
|
7
|
-
VERSION = '0.1.
|
7
|
+
VERSION = '0.1.1'.freeze
|
8
8
|
|
9
9
|
InvoiceResponse = Class.new(OpenStruct)
|
10
10
|
LnurlResponse = Class.new(OpenStruct) do
|
11
|
-
|
12
|
-
|
11
|
+
# amount in msats
|
12
|
+
def request_invoice(args)
|
13
|
+
args.transform_keys!(&:to_s)
|
13
14
|
callback_uri = URI(callback)
|
14
|
-
|
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.
|
53
|
-
value.to_s.downcase.match?(Regexp.new("^#{HRP}"))
|
54
|
-
|
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
|