lnurl 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6e23197258071b756215d4f300ea391232f225a85eca0690088b116f9a3a6510
4
+ data.tar.gz: ce09219efdb19a01f87ebf9e909a668e1c21129ad4faa00257d165c1b4f1b007
5
+ SHA512:
6
+ metadata.gz: 9c7ce4fcb15cd1bf2e5f240cfd5c25469eee0d8b6f644d0ed037e587a8de3cf72a17eb45245a29e53347ad5e543cc8e0d501444a5e1cb1c4b1a043ac9222ba56
7
+ data.tar.gz: dc3a3fa4da4b54c3aaa5cd9ed89fc75ef51ef09c91e91a34d865e8fcb4093255ff7fe822dba2d13bf74faa3e65ff776d2079dc4208d78fb0231cb84e2510633a
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in lnurl.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Michael Bumann
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # Lnurl
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.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'lnurl'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install lnurl
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
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.
30
+
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
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lnurl.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lnurl"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,88 @@
1
+ require 'bech32'
2
+ require 'net/http'
3
+ require 'json'
4
+ require 'ostruct'
5
+
6
+ class Lnurl
7
+ VERSION = '0.1.0'.freeze
8
+
9
+ InvoiceResponse = Class.new(OpenStruct)
10
+ LnurlResponse = Class.new(OpenStruct) do
11
+ def request_invoice(amount: nil, amt_msat: nil)
12
+ msats = amount ? amount * 1000 : amt_msat
13
+ callback_uri = URI(callback)
14
+ callback_uri.query = "amount=#{msats}&" + callback_uri.query.to_s
15
+ body = Net::HTTP.get(callback_uri)
16
+ InvoiceResponse.new JSON.parse(body)
17
+ end
18
+ end
19
+
20
+ HRP = 'lnurl'.freeze
21
+
22
+ attr_reader :uri
23
+
24
+ def initialize(uri)
25
+ @uri = URI(uri)
26
+ end
27
+
28
+ def to_bech32
29
+ Bech32.encode(HRP, data).upcase
30
+ end
31
+ alias encode to_bech32
32
+
33
+ def data
34
+ self.class.convert_bits(uri.to_s.codepoints, 8, 5, true)
35
+ end
36
+
37
+ def response
38
+ @response ||= begin
39
+ body = Net::HTTP.get(uri) # TODO: redirects?
40
+ LnurlResponse.new JSON.parse(body)
41
+ end
42
+ end
43
+
44
+ def request_invoice(amount:)
45
+ response.request_invoice(amount: amount)
46
+ end
47
+
48
+ def payment_request(amount:)
49
+ request_invoice(amount: amount).pr
50
+ end
51
+
52
+ def self.lnurl?(value)
53
+ value.to_s.downcase.match?(Regexp.new("^#{HRP}")) &&
54
+ decode(value) rescue false
55
+ end
56
+
57
+ def self.decode(lnurl)
58
+ lnurl = lnurl.gsub(/^lightning:/, '')
59
+ hrp, data = Bech32.decode(lnurl, lnurl.length)
60
+ # raise 'no lnurl' if hrp != HRP
61
+
62
+ Lnurl.new(convert_bits(data, 5, 8, false).pack('C*').force_encoding('utf-8'))
63
+ end
64
+
65
+ # FROM: https://github.com/azuchi/bech32rb/blob/master/lib/bech32/segwit_addr.rb
66
+ def self.convert_bits(data, from, to, padding=true)
67
+ acc = 0
68
+ bits = 0
69
+ ret = []
70
+ maxv = (1 << to) - 1
71
+ max_acc = (1 << (from + to - 1)) - 1
72
+ data.each do |v|
73
+ return nil if v < 0 || (v >> from) != 0
74
+ acc = ((acc << from) | v) & max_acc
75
+ bits += from
76
+ while bits >= to
77
+ bits -= to
78
+ ret << ((acc >> bits) & maxv)
79
+ end
80
+ end
81
+ if padding
82
+ ret << ((acc << (to - bits)) & maxv) unless bits == 0
83
+ elsif bits >= from || ((acc << (to - bits)) & maxv) != 0
84
+ return nil
85
+ end
86
+ ret
87
+ end
88
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'lib/lnurl'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "lnurl"
5
+ spec.version = Lnurl::VERSION
6
+ spec.authors = ["Michael Bumann"]
7
+ spec.email = ["hello@michaelbumann.com"]
8
+
9
+ spec.summary = %q{LNURL implementation for ruby}
10
+ spec.description = %q{A collection of tools to work with LNURLs - the protocol for interaction between Lightning wallets and third-party services.}
11
+ spec.homepage = "https://github.com/bumi/lnurl-ruby"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/bumi/lnurl-ruby"
17
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
18
+
19
+ spec.metadata['funding'] = 'lightning:02ad33d99d0bb3bf3bb8ec8e089cbefa8fd7de23a13cfa59aec9af9730816be76f'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_runtime_dependency 'bech32'
31
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lnurl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Bumann
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bech32
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: A collection of tools to work with LNURLs - the protocol for interaction
28
+ between Lightning wallets and third-party services.
29
+ email:
30
+ - hello@michaelbumann.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - ".rspec"
37
+ - ".travis.yml"
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - bin/console
43
+ - bin/setup
44
+ - lib/lnurl.rb
45
+ - lnurl.gemspec
46
+ homepage: https://github.com/bumi/lnurl-ruby
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ homepage_uri: https://github.com/bumi/lnurl-ruby
51
+ source_code_uri: https://github.com/bumi/lnurl-ruby
52
+ funding: lightning:02ad33d99d0bb3bf3bb8ec8e089cbefa8fd7de23a13cfa59aec9af9730816be76f
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.3.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.0.3
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: LNURL implementation for ruby
72
+ test_files: []