blockstream_satellite 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8d1135b391797f323b91a479490743a7a96a0894a69871c286031532c6274cff
4
+ data.tar.gz: 9cd0355f530d00d3c1aa4ff8d1591f9e87b0952d1e05aa09b29019fea6283ac9
5
+ SHA512:
6
+ metadata.gz: c2ebbd8285cd42e435476db3973fe50ff79e9d28599a058fbdc9378485ed3b6f06c8183ae51b3e892e761a54178c5a8441240ba0c7ccf03998d2e5f2026e8292
7
+ data.tar.gz: 49e0bbf5ab6fd068f8c712d36408e1bc57c162be08b22dcfc9e4b362952ed47de2fa838bdaf608af2ea1e2e1691a77507c1bb13f0d6028c8f9e0cde96b6e2712
@@ -0,0 +1,13 @@
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
12
+
13
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in blockstream_satellite.gemspec
6
+ gemspec
@@ -0,0 +1,64 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ blockstream_satellite (0.1.0)
5
+ activesupport (>= 5.0.1)
6
+ faraday (> 0.8)
7
+ faraday_middleware (> 0.12)
8
+ lnrpc (>= 0.1.0)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ activesupport (5.2.2)
14
+ concurrent-ruby (~> 1.0, >= 1.0.2)
15
+ i18n (>= 0.7, < 2)
16
+ minitest (~> 5.1)
17
+ tzinfo (~> 1.1)
18
+ concurrent-ruby (1.1.4)
19
+ diff-lcs (1.3)
20
+ faraday (0.15.4)
21
+ multipart-post (>= 1.2, < 3)
22
+ faraday_middleware (0.13.1)
23
+ faraday (>= 0.7.4, < 1.0)
24
+ google-protobuf (3.6.1)
25
+ googleapis-common-protos-types (1.0.3)
26
+ google-protobuf (~> 3.0)
27
+ grpc (1.18.0)
28
+ google-protobuf (~> 3.1)
29
+ googleapis-common-protos-types (~> 1.0.0)
30
+ i18n (1.5.3)
31
+ concurrent-ruby (~> 1.0)
32
+ lnrpc (0.1.0)
33
+ grpc (>= 1.16.0)
34
+ minitest (5.11.3)
35
+ multipart-post (2.0.0)
36
+ rake (10.5.0)
37
+ rspec (3.8.0)
38
+ rspec-core (~> 3.8.0)
39
+ rspec-expectations (~> 3.8.0)
40
+ rspec-mocks (~> 3.8.0)
41
+ rspec-core (3.8.0)
42
+ rspec-support (~> 3.8.0)
43
+ rspec-expectations (3.8.2)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.8.0)
46
+ rspec-mocks (3.8.0)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.8.0)
49
+ rspec-support (3.8.0)
50
+ thread_safe (0.3.6)
51
+ tzinfo (1.2.5)
52
+ thread_safe (~> 0.1)
53
+
54
+ PLATFORMS
55
+ ruby
56
+
57
+ DEPENDENCIES
58
+ blockstream_satellite!
59
+ bundler (~> 1.17)
60
+ rake (~> 10.0)
61
+ rspec (~> 3.0)
62
+
63
+ BUNDLED WITH
64
+ 1.17.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 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,96 @@
1
+ # BlockstreamSatellite
2
+
3
+ Ruby gem to interact with the [Blockstream Satellite](https://blockstream.com/satellite/) API.
4
+
5
+ To learn more about the Blockstream Satellite check their [website](https://blockstream.com/satellite/) and [API documentation](https://blockstream.com/satellite-api/).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'blockstream_satellite'
13
+ ```
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install blockstream_satellite
18
+
19
+
20
+ ## Usage
21
+
22
+ ### Quick example
23
+
24
+ ```ruby
25
+ order = BlockstreamSatellite::Order.create(path: '/path/to/file')
26
+ puts order.status # pending
27
+ order.pay # sends the lightning payment using the configured lnd client; request is synchronous
28
+
29
+ puts order.status # transmitting
30
+ order.refresh
31
+ puts order.status # sent
32
+ ```
33
+
34
+ ### Configuration
35
+
36
+ In order to be able to pay for the lightninig invoice you need to connect to you lightning node.
37
+ (By default the testnet cert and macaroon files are loaded from `~/.lnd` and `localhost:10009` is used)
38
+
39
+ ```ruby
40
+ lnd_client = Lnrpc::Client.new({
41
+ credentials_path: '/path/to/tls.cert',
42
+ macaroon_path: '/path/to/admin.macaroon',
43
+ address: 'localhost:10009'
44
+ })
45
+ BlockstreamSatellite.lnd_client = lnd_client
46
+ ```
47
+
48
+ ### Creating an order
49
+
50
+ `BlockstreamSatellite::Order.create` accepts the following parameters:
51
+
52
+ * `path`: path to the file to transmit
53
+ * `data`: data to transmit; can be used as an alternative `path`
54
+ * `bid`: bid for the order. defaults to file size * 51
55
+
56
+ ```ruby
57
+ order = BlockstreamSatellite::Order.create(path: '/path/to/file')
58
+ ```
59
+
60
+ ### Loading an order by UUID and auth_token
61
+
62
+ ```ruby
63
+ order = BlockstreamSatellite::Order.get(uuid: 'uuid', auth_token: 'auth_token')
64
+ ```
65
+
66
+ ### Bump the bid for an order
67
+
68
+ ```ruby
69
+ order = BlockstreamSatellite::Order.get(uuid: 'uuid', auth_token: 'auth_token')
70
+ order.bump(1000)
71
+ ```
72
+
73
+ ### Pay for an order
74
+
75
+ ```ruby
76
+ order = BlockstreamSatellite::Order.get(uuid: 'uuid', auth_token: 'auth_token')
77
+ # or
78
+ order = BlockstreamSatellite::Order.create(path: '/path/to/file')
79
+
80
+ order.pay
81
+ puts order.status
82
+ ```
83
+
84
+
85
+ ## Status
86
+
87
+ Currently I consider this gem experimental and it is missing tests current.
88
+
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bumi/blockstream_satellite.
93
+
94
+ ## License
95
+
96
+ 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 "blockstream_satellite"
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,44 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "blockstream_satellite/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "blockstream_satellite"
8
+ spec.version = BlockstreamSatellite::VERSION
9
+ spec.authors = ["Michael Bumann"]
10
+ spec.email = ["hello@michaelbumann.com"]
11
+
12
+ spec.summary = %q{API client for the Blockstream satellite}
13
+ spec.description = %q{API clent to interact with the Blockstream satellite}
14
+ spec.homepage = "http://github.com/bumi/blockstream_satellite"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = spec.homepage
22
+ spec.metadata["changelog_uri"] = "http://github.com/bumi/blockstream_satellite/"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_development_dependency "bundler", "~> 1.17"
38
+ spec.add_development_dependency "rake", "~> 10.0"
39
+ spec.add_development_dependency "rspec", "~> 3.0"
40
+ spec.add_dependency "faraday", "> 0.8"
41
+ spec.add_dependency "faraday_middleware", ">0.12"
42
+ spec.add_dependency "lnrpc", ">= 0.1.0"
43
+ spec.add_dependency "activesupport", ">= 5.0.1"
44
+ end
@@ -0,0 +1,45 @@
1
+ require "blockstream_satellite/version"
2
+
3
+ require 'lnrpc'
4
+
5
+ module BlockstreamSatellite
6
+ class Error < StandardError; end
7
+ autoload :Order, 'blockstream_satellite/order'
8
+ autoload :Client, 'blockstream_satellite/client'
9
+ autoload :Configuration, 'blockstream_satellite/configuration'
10
+ autoload :Response, 'blockstream_satellite/response'
11
+
12
+ API_HOST = 'https://satellite.blockstream.com'
13
+
14
+ def self.lnd_client=(value)
15
+ @lnd_client = value
16
+ end
17
+
18
+ def self.lnd_client
19
+ @lnd_client ||= Lnrpc::Client.new({})
20
+ end
21
+
22
+ def http_client=(value)
23
+ @http_client = value
24
+ end
25
+
26
+ def self.http_client
27
+ @http_client ||= Faraday.new(url: API_HOST) do |faraday|
28
+ faraday.request :multipart
29
+ faraday.request :url_encoded
30
+ faraday.response :json, :content_type => /\bjson$/
31
+
32
+ faraday.adapter Faraday.default_adapter
33
+ end
34
+ end
35
+
36
+ def self.client
37
+ @client ||= Client.new(lnd_client: self.lnd_client, http_client: self.http_client)
38
+ end
39
+
40
+ def self.info
41
+ self.client.get('/info')
42
+ end
43
+
44
+ end
45
+ BSat = BlockstreamSatellite
@@ -0,0 +1,40 @@
1
+ require "faraday"
2
+ require 'faraday_middleware'
3
+
4
+ module BlockstreamSatellite
5
+ class Client
6
+ attr_accessor :http_client, :lnd_client
7
+
8
+ def initialize(options)
9
+ self.lnd_client = options[:lnd_client]
10
+ self.http_client = options[:http_client]
11
+ end
12
+
13
+ def get(path, params = nil, &block)
14
+ request(:get, path, params, &block)
15
+ end
16
+
17
+ def post(path, body = nil, &block)
18
+ puts body.inspect
19
+ request(:post, path, nil, body, &block)
20
+ end
21
+
22
+ def pay(payreq)
23
+ self.lnd_client.send_payment_sync(Lnrpc::SendRequest.new(payment_request: payreq))
24
+ end
25
+
26
+ def request(http_method, path, params=nil, body=nil)
27
+ response = http_client.send(http_method) do |req|
28
+ req.url "/api/#{path}"
29
+ req.params = params if params
30
+ req.body = body if body
31
+ yield req if block_given?
32
+ end
33
+ Response.new(response)
34
+ rescue Faraday::ClientError => error
35
+ return Response.new(error)
36
+ end
37
+
38
+ end
39
+ end
40
+
@@ -0,0 +1,78 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require "active_support/core_ext/hash/indifferent_access"
4
+ require 'tempfile'
5
+ require 'securerandom'
6
+
7
+ module BlockstreamSatellite
8
+ class Order
9
+
10
+ attr_accessor :attributes
11
+
12
+ def initialize(attributes)
13
+ self.attributes = attributes.with_indifferent_access
14
+ end
15
+
16
+ # defining accessors
17
+ [:auth_token, :uuid, :lightning_invoice, :sha256_message_digest, :message_digest, :status, :bid, :unpaid_bid].each do |attr|
18
+ define_method(attr) do
19
+ self.attributes[attr]
20
+ end
21
+ end
22
+
23
+ def payreq
24
+ lightning_invoice['payreq']
25
+ end
26
+
27
+ def self.create(options)
28
+ if data = options.delete(:data)
29
+ file = Tempfile.new(SecureRandom.hex(5))
30
+ file.write(data)
31
+ file.rewind
32
+ options[:file] = file
33
+ elsif path = options.delete(:path)
34
+ options[:file] = File.open(path)
35
+ end
36
+ options[:bid] ||= options[:file].size * 51 # default price
37
+ options[:file] = UploadIO.new(options[:file], 'text/plain')
38
+ response = BlockstreamSatellite.client.post('order', options)
39
+ if response.success?
40
+ Order.new(response.body)
41
+ else
42
+ response
43
+ end
44
+ end
45
+
46
+ def self.get(order)
47
+ Order.new(order).tap do |o|
48
+ o.refresh
49
+ end
50
+ end
51
+
52
+ def refresh
53
+ response = BlockstreamSatellite.client.get("order/#{self.uuid}") do |req|
54
+ req.headers['X-Auth-Token'] = self.auth_token
55
+ end
56
+ if response.success?
57
+ self.attributes.merge!(response.body)
58
+ end
59
+ end
60
+
61
+ def pay
62
+ BlockstreamSatellite.client.pay(self.payreq)
63
+ self.refresh
64
+ end
65
+
66
+ def bump(bid_increase)
67
+ response = BlockstreamSatellite.client.post("order/#{self.uuid}/bump", bid_increase: bid_increase) do |req|
68
+ req.headers['X-Auth-Token'] = self.auth_token
69
+ end
70
+ if response.success?
71
+ self.attributes['lightning_invoice'] = response['lightning_invoice']
72
+ else
73
+ response
74
+ end
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,23 @@
1
+ require "faraday"
2
+ module BlockstreamSatellite
3
+ class Response
4
+ attr_accessor :response, :body, :error
5
+
6
+ def initialize(response)
7
+ if response.is_a?(Faraday::ClientError)
8
+ @error = response
9
+ else
10
+ @response = response
11
+ @body = response.body
12
+ end
13
+ end
14
+
15
+ def success?
16
+ self.error.nil? && !self.response.nil? && self.response.success?
17
+ end
18
+
19
+ def [](key)
20
+ self.body[key.to_s]
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module BlockstreamSatellite
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blockstream_satellite
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: 2019-02-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.8'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.8'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday_middleware
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.12'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: lnrpc
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 0.1.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 0.1.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: activesupport
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 5.0.1
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 5.0.1
111
+ description: API clent to interact with the Blockstream satellite
112
+ email:
113
+ - hello@michaelbumann.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - Gemfile
121
+ - Gemfile.lock
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - blockstream_satellite.gemspec
128
+ - lib/blockstream_satellite.rb
129
+ - lib/blockstream_satellite/client.rb
130
+ - lib/blockstream_satellite/order.rb
131
+ - lib/blockstream_satellite/response.rb
132
+ - lib/blockstream_satellite/version.rb
133
+ homepage: http://github.com/bumi/blockstream_satellite
134
+ licenses:
135
+ - MIT
136
+ metadata:
137
+ homepage_uri: http://github.com/bumi/blockstream_satellite
138
+ source_code_uri: http://github.com/bumi/blockstream_satellite
139
+ changelog_uri: http://github.com/bumi/blockstream_satellite/
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.7.6
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: API client for the Blockstream satellite
160
+ test_files: []