lighstorm 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5255fb12e5463a29335c433ca7ccc20cdc6154a686f25c1272d4bf608e71764b
4
- data.tar.gz: 7865931e5f6ee408a810481f1f809edcdf3d5fabfcd31e76bf3e3cad1dfabe64
3
+ metadata.gz: 4c585645f0ccf6b55a8b0840d5351900f701002a4616f351016f1b8d9f097ed4
4
+ data.tar.gz: fca5794bab41231c60400aab4ce82021bb649df52df63acf801ef4931bf5c53c
5
5
  SHA512:
6
- metadata.gz: 7c7793a2361ffe29f9e97b1be00500274054f58acea841bb025fac94ebac969117c09f69cc644219825e06883df93a400cebce0db1f72f1b72e75ab3296e37b6
7
- data.tar.gz: 81ce8ce6fcc5d564ae5b0a51c99080d0f301ecbf9a5fb6da45bd8f633a4d35eea6adb1b77f1f0a2ecebb9bf4c8e11c0185aca35f688f21f2118ff73a0f5ac1ab
6
+ metadata.gz: 0717fc86e023d1591872b3c81c58338c713409030f3bc2351988ee62abb163b262df95e1357537c7530d3f3bd615208af99f68c92a0a53b6f6b784401d5c8e15
7
+ data.tar.gz: ff9bba186921987b3f301f95f82f1d3b7041feb4db92d8ca2a43c663915ea12e6d609615419805bc26cde7c2ba658702b713a603dd33c3f024de6b16794eca7e
data/Gemfile CHANGED
@@ -6,5 +6,5 @@ gemspec
6
6
 
7
7
  group :test, :development do
8
8
  gem 'pry-byebug', '~> 3.10', '>= 3.10.1'
9
- gem 'rubocop', '~> 1.44', '>= 1.44.1'
9
+ gem 'rubocop', '~> 1.45', '>= 1.45.1'
10
10
  end
data/Gemfile.lock CHANGED
@@ -1,9 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lighstorm (0.0.2)
4
+ lighstorm (0.0.3)
5
5
  dotenv (~> 2.8, >= 2.8.1)
6
- lnd-client (~> 0.0.4)
6
+ lnd-client (~> 0.0.5)
7
7
  zache (~> 0.12.0)
8
8
 
9
9
  GEM
@@ -20,8 +20,8 @@ GEM
20
20
  google-protobuf (~> 3.21)
21
21
  googleapis-common-protos-types (~> 1.0)
22
22
  json (2.6.3)
23
- lnd-client (0.0.4)
24
- grpc (~> 1.51)
23
+ lnd-client (0.0.5)
24
+ grpc (~> 1.52)
25
25
  method_source (1.0.0)
26
26
  parallel (1.22.1)
27
27
  parser (3.2.1.0)
@@ -45,8 +45,8 @@ GEM
45
45
  rubocop-ast (>= 1.24.1, < 2.0)
46
46
  ruby-progressbar (~> 1.7)
47
47
  unicode-display_width (>= 2.4.0, < 3.0)
48
- rubocop-ast (1.24.1)
49
- parser (>= 3.1.1.0)
48
+ rubocop-ast (1.26.0)
49
+ parser (>= 3.2.1.0)
50
50
  ruby-progressbar (1.11.0)
51
51
  unicode-display_width (2.4.2)
52
52
  zache (0.12.0)
@@ -57,7 +57,7 @@ PLATFORMS
57
57
  DEPENDENCIES
58
58
  lighstorm!
59
59
  pry-byebug (~> 3.10, >= 3.10.1)
60
- rubocop (~> 1.44, >= 1.44.1)
60
+ rubocop (~> 1.45, >= 1.45.1)
61
61
 
62
62
  BUNDLED WITH
63
63
  2.4.4
data/README.md CHANGED
@@ -7,7 +7,7 @@ API for interacting with a [Lightning Node](https://lightning.network).
7
7
  ![Lighstorm text written stylized with an illustration of a Graph connecting two Nodes.](https://raw.githubusercontent.com/icebaker/assets/main/lighstorm/lighstorm.png)
8
8
 
9
9
  ```ruby
10
- Lighstorm::Channel.first.myself.node.alias
10
+ Lighstorm::Channel.mine.first.myself.node.alias
11
11
  ```
12
12
 
13
13
  ## Index
@@ -42,7 +42,7 @@ Although it tries to stay close to [Lightning's terminologies](https://docs.ligh
42
42
  Add to your `Gemfile`:
43
43
 
44
44
  ```ruby
45
- gem 'lighstorm', '~> 0.0.2'
45
+ gem 'lighstorm', '~> 0.0.3'
46
46
  ```
47
47
 
48
48
  Run `bundle install`.
@@ -78,7 +78,8 @@ Lighstorm.config!(
78
78
  ```ruby
79
79
  Lighstorm::Node
80
80
 
81
- Lighstorm::Node.myself
81
+ Lighstorm::Node.myself # Your Node.
82
+ Lighstorm::Node.all # All 18k+ Nodes on the Network.
82
83
  Lighstorm::Node.find_by_public_key(
83
84
  '02d3c80335a8ccb2ed364c06875f32240f36f7edb37d80f8dbe321b4c364b6e997'
84
85
  )
@@ -98,13 +99,14 @@ node.platform.lightning.implementation
98
99
  node.platform.lightning.version
99
100
 
100
101
  Lighstorm::Channel
101
- Lighstorm::Channel.all
102
- Lighstorm::Channel.first
103
- Lighstorm::Channel.last
104
- Lighstorm::Channel.find_by_id(850099509773795329)
102
+ Lighstorm::Channel.mine # Your Node's Channels.
103
+ Lighstorm::Channel.all # All 80k+ Channels on the Network.
104
+ Lighstorm::Channel.find_by_id('850099509773795329')
105
105
 
106
106
  channel.to_h
107
107
 
108
+ channel.mine?
109
+
108
110
  channel.id
109
111
  channel.opened_at
110
112
  channel.up_at
@@ -116,12 +118,30 @@ channel.accounting.sent.milisatoshis
116
118
  channel.accounting.received.milisatoshis
117
119
  channel.accounting.unsettled.milisatoshis
118
120
 
121
+ # Channels that don't belong to you:
122
+ channel.partners
123
+
124
+ channel.partners[0]
125
+ channel.partners[0].node.alias
126
+
127
+ channel.partners[1]
128
+ channel.partners[1].node.alias
129
+
130
+ # Channels that belong to you:
131
+ channel.myself
132
+ channel.myself.node.alias
133
+
134
+ channel.partner
135
+ channel.partner.node.alias
136
+
119
137
  channel.partner.accounting.balance.milisatoshis
120
138
  channel.partner.node.alias
121
139
  channel.partner.node.public_key
122
140
  channel.partner.node.color
123
141
  channel.partner.policy.fee.base.milisatoshis
124
142
  channel.partner.policy.fee.rate.parts_per_million
143
+ channel.partner.policy.htlc.minimum.milisatoshis
144
+ channel.partner.policy.htlc.maximum.milisatoshis
125
145
 
126
146
  channel.myself.accounting.balance.milisatoshis
127
147
  channel.myself.node.alias
@@ -129,15 +149,25 @@ channel.myself.node.public_key
129
149
  channel.myself.node.color
130
150
  channel.myself.policy.fee.base.milisatoshis
131
151
  channel.myself.policy.fee.rate.parts_per_million
152
+ channel.myself.policy.htlc.minimum.milisatoshis
153
+ channel.myself.policy.htlc.maximum.milisatoshis
132
154
 
133
155
  channel.myself.policy.fee.update(
134
156
  { rate: { parts_per_million: 25 } }, preview: true
135
157
  )
136
158
 
159
+ channel.myself.policy.fee.update(
160
+ { base: { milisatoshis: 1 } }
161
+ )
162
+
137
163
  channel.myself.policy.fee.update(
138
164
  { rate: { parts_per_million: 25 } }
139
165
  )
140
166
 
167
+ channel.myself.policy.fee.update(
168
+ { base: { milisatoshis: 1 }, rate: { parts_per_million: 25 } }
169
+ )
170
+
141
171
  Lighstorm::Forward
142
172
  Lighstorm::Forward.all
143
173
  Lighstorm::Forward.first
@@ -274,7 +304,7 @@ satoshis.parts_per_million(reference_in_milisatoshis)
274
304
  ```ruby
275
305
  require 'lighstorm'
276
306
 
277
- puts Lighstorm.version # => 0.0.2
307
+ puts Lighstorm.version # => 0.0.3
278
308
 
279
309
  Lighstorm::Satoshis.new(
280
310
  milisatoshis: 75621650
@@ -287,7 +317,7 @@ Lighstorm::Node.myself.to_h #> { ... }
287
317
 
288
318
  Lighstorm::Node.myself.channels.count # => 5
289
319
 
290
- Lighstorm::Channel.all.first.partner.node.alias
320
+ Lighstorm::Channel.mine.first.partner.node.alias
291
321
 
292
322
  forward = Lighstorm::Forward.all(limit: 10).first
293
323
 
@@ -321,7 +351,7 @@ So, we are going to think in terms of _Edges_, _Nodes_, and _Connections_:
321
351
  #### Channel
322
352
 
323
353
  ```ruby
324
- channel = Lighstorm::Channel.first
354
+ channel = Lighstorm::Channel.mine.first
325
355
 
326
356
  channel.id
327
357
 
@@ -396,7 +426,7 @@ gem 'lighstorm', path: '/home/user/lighstorm'
396
426
  # demo.rb
397
427
  require 'lighstorm'
398
428
 
399
- puts Lighstorm.version # => 0.0.2
429
+ puts Lighstorm.version # => 0.0.3
400
430
  ```
401
431
 
402
432
  ```sh
@@ -411,5 +441,5 @@ gem build lighstorm.gemspec
411
441
 
412
442
  gem signin
413
443
 
414
- gem push lighstorm-0.0.2.gem
444
+ gem push lighstorm-0.0.3.gem
415
445
  ```
data/lighstorm.gemspec CHANGED
@@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
30
30
  spec.require_paths = ['ports/dsl']
31
31
 
32
32
  spec.add_dependency 'dotenv', '~> 2.8', '>= 2.8.1'
33
- spec.add_dependency 'lnd-client', '~> 0.0.4'
33
+ spec.add_dependency 'lnd-client', '~> 0.0.5'
34
34
  spec.add_dependency 'zache', '~> 0.12.0'
35
35
 
36
36
  spec.metadata['rubygems_mfa_required'] = 'true'
@@ -11,6 +11,8 @@ module Lighstorm
11
11
  end
12
12
 
13
13
  def balance
14
+ return nil unless @channel.data[:list_channels]
15
+
14
16
  @balance ||= if @node.myself?
15
17
  Satoshis.new(milisatoshis: (
16
18
  @channel.data[:list_channels][:channels].first.local_balance.to_f * 1000.0
@@ -13,7 +13,7 @@ module Lighstorm
13
13
  def initialize(policy, channel, node)
14
14
  @channel = channel
15
15
  @policy = policy
16
- if node.myself?
16
+ if channel.data[:fee_report] && node.myself?
17
17
  @base = Satoshis.new(
18
18
  milisatoshis: channel.data[:fee_report][:channel_fees].first.base_fee_msat
19
19
  )
@@ -21,10 +21,10 @@ module Lighstorm
21
21
  @rate = Rate.new(
22
22
  parts_per_million: channel.data[:fee_report][:channel_fees].first.fee_per_mil
23
23
  )
24
- else
25
- @base = Satoshis.new(milisatoshis: policy.data.fee_base_msat)
24
+ elsif policy.data
25
+ @base = policy.data.fee_base_msat ? Satoshis.new(milisatoshis: policy.data.fee_base_msat) : nil
26
26
 
27
- @rate = Rate.new(parts_per_million: policy.data.fee_rate_milli_msat)
27
+ @rate = policy.data.fee_rate_milli_msat ? Rate.new(parts_per_million: policy.data.fee_rate_milli_msat) : nil
28
28
  end
29
29
  end
30
30
 
@@ -59,13 +59,39 @@ module Lighstorm
59
59
  }
60
60
  }
61
61
 
62
- grpc_request[:params][:fee_rate_ppm] = params[:rate][:parts_per_million]
62
+ if params[:rate] && params[:rate][:parts_per_million]
63
+ if (params[:rate][:parts_per_million]).negative?
64
+ raise "fee rate can't be negative [#{params[:rate][:parts_per_million]}]"
65
+ end
66
+
67
+ grpc_request[:params][:fee_rate_ppm] = params[:rate][:parts_per_million]
68
+ end
69
+
70
+ if params[:base] && params[:base][:milisatoshis]
71
+ if (params[:base][:milisatoshis]).negative?
72
+ raise "fee base can't be negative [#{params[:base][:milisatoshis]}]"
73
+ end
74
+
75
+ grpc_request[:params][:base_fee_msat] = params[:base][:milisatoshis]
76
+ end
63
77
 
64
78
  return grpc_request if preview
65
79
 
66
- LND.instance.middleware("lightning.#{grpc_request[:method]}") do
80
+ response = LND.instance.middleware("lightning.#{grpc_request[:method]}") do
67
81
  LND.instance.client.lightning.send(grpc_request[:method], grpc_request[:params])
68
82
  end
83
+
84
+ if response.failed_updates.empty?
85
+ @base = Satoshis.new(
86
+ milisatoshis: grpc_request[:params][:base_fee_msat]
87
+ )
88
+
89
+ @rate = Rate.new(
90
+ parts_per_million: grpc_request[:params][:fee_rate_ppm]
91
+ )
92
+ end
93
+
94
+ response
69
95
  end
70
96
 
71
97
  def to_h
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../satoshis'
4
+ require_relative '../../rate'
5
+
6
+ require_relative '../../../components/lnd'
7
+
8
+ module Lighstorm
9
+ module Models
10
+ class HTLC
11
+ attr_reader :minimum, :maximum
12
+
13
+ def initialize(policy, channel, _node)
14
+ @channel = channel
15
+ @policy = policy
16
+
17
+ return unless policy.data
18
+
19
+ @minimum = Satoshis.new(milisatoshis: policy.data.min_htlc)
20
+
21
+ @maximum = Satoshis.new(milisatoshis: policy.data.max_htlc_msat)
22
+ end
23
+
24
+ def to_h
25
+ {
26
+ minimum: @minimum.to_h,
27
+ maximum: @maximum.to_h
28
+ }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'fee'
4
+ require_relative 'htlc'
4
5
 
5
6
  module Lighstorm
6
7
  module Models
@@ -11,10 +12,16 @@ module Lighstorm
11
12
  end
12
13
 
13
14
  def data
14
- @data ||= if @channel.data[:get_chan_info].node1_pub == @node.public_key
15
- @channel.data[:get_chan_info].node1_policy
15
+ return @data if @data
16
+
17
+ return if !@channel.data[:get_chan_info] && !@channel.data[:describe_graph]
18
+
19
+ key = @channel.data[:get_chan_info] ? :get_chan_info : :describe_graph
20
+
21
+ @data ||= if @channel.data[key].node1_pub == @node.public_key
22
+ @channel.data[key].node1_policy
16
23
  else
17
- @channel.data[:get_chan_info].node2_policy
24
+ @channel.data[key].node2_policy
18
25
  end
19
26
  end
20
27
 
@@ -22,8 +29,12 @@ module Lighstorm
22
29
  @fee ||= Fee.new(self, @channel, @node)
23
30
  end
24
31
 
32
+ def htlc
33
+ @htlc ||= HTLC.new(self, @channel, @node)
34
+ end
35
+
25
36
  def to_h
26
- { fee: fee.to_h }
37
+ { fee: fee.to_h, htlc: htlc.to_h }
27
38
  end
28
39
  end
29
40
  end
@@ -29,12 +29,19 @@ module Lighstorm
29
29
  end
30
30
 
31
31
  def to_h
32
- {
33
- accounting: accounting.to_h,
34
- node: @node.to_h,
35
- policy: policy.to_h
36
- # constraints: constraints.to_h
37
- }
32
+ if @channel.data[:get_chan_info]
33
+ {
34
+ accounting: accounting.to_h,
35
+ node: @node.to_h,
36
+ policy: policy.to_h
37
+ # constraints: constraints.to_h
38
+ }
39
+ else
40
+ {
41
+ node: @node.to_h,
42
+ policy: policy.to_h
43
+ }
44
+ end
38
45
  end
39
46
  end
40
47
  end
@@ -10,34 +10,50 @@ module Lighstorm
10
10
  end
11
11
 
12
12
  def capacity
13
- @capacity ||= Satoshis.new(milisatoshis: @channel.data[:get_chan_info].capacity * 1000)
13
+ if @channel.data[:get_chan_info]
14
+ @capacity ||= Satoshis.new(milisatoshis: @channel.data[:get_chan_info].capacity * 1000)
15
+ elsif @channel.data[:describe_graph]
16
+ @capacity ||= Satoshis.new(milisatoshis: @channel.data[:describe_graph].capacity * 1000)
17
+ end
14
18
  end
15
19
 
16
20
  def sent
21
+ return nil unless @channel.data[:list_channels]
22
+
17
23
  @sent ||= Satoshis.new(milisatoshis: (
18
24
  @channel.data[:list_channels][:channels].first.total_satoshis_sent.to_f * 1000.0
19
25
  ))
20
26
  end
21
27
 
22
28
  def received
29
+ return nil unless @channel.data[:list_channels]
30
+
23
31
  @received ||= Satoshis.new(milisatoshis: (
24
32
  @channel.data[:list_channels][:channels].first.total_satoshis_received.to_f * 1000.0
25
33
  ))
26
34
  end
27
35
 
28
36
  def unsettled
37
+ return nil unless @channel.data[:list_channels]
38
+
29
39
  @unsettled ||= Satoshis.new(milisatoshis: (
30
40
  @channel.data[:list_channels][:channels].first.unsettled_balance.to_f * 1000.0
31
41
  ))
32
42
  end
33
43
 
34
44
  def to_h
35
- {
36
- capacity: capacity.to_h,
37
- sent: sent.to_h,
38
- received: received.to_h,
39
- unsettled: unsettled.to_h
40
- }
45
+ if @channel.data[:get_chan_info]
46
+ {
47
+ capacity: capacity.to_h,
48
+ sent: sent.to_h,
49
+ received: received.to_h,
50
+ unsettled: unsettled.to_h
51
+ }
52
+ else
53
+ {
54
+ capacity: capacity.to_h
55
+ }
56
+ end
41
57
  end
42
58
  end
43
59
  end
@@ -20,6 +20,16 @@ module Lighstorm
20
20
  attr_reader :data
21
21
 
22
22
  def self.all
23
+ response = LND.instance.middleware('lightning.describe_graph') do
24
+ LND.instance.client.lightning.describe_graph
25
+ end
26
+
27
+ response.edges.map do |raw_channel|
28
+ Channel.new({ describe_graph: raw_channel })
29
+ end
30
+ end
31
+
32
+ def self.mine
23
33
  response = Cache.for('lightning.list_channels') do
24
34
  LND.instance.middleware('lightning.list_channels') do
25
35
  LND.instance.client.lightning.list_channels
@@ -31,14 +41,6 @@ module Lighstorm
31
41
  end
32
42
  end
33
43
 
34
- def self.first
35
- all.first
36
- end
37
-
38
- def self.last
39
- all.last
40
- end
41
-
42
44
  def self.find_by_id(id)
43
45
  Channel.new({ id: id })
44
46
  end
@@ -49,18 +51,23 @@ module Lighstorm
49
51
  end
50
52
 
51
53
  def initialize(params)
52
- begin
53
- response = Cache.for('lightning.get_chan_info', params: { chan_id: params[:id].to_i }) do
54
- LND.instance.middleware('lightning.get_chan_info') do
55
- LND.instance.client.lightning.get_chan_info(chan_id: params[:id].to_i)
54
+ if params[:id]
55
+ begin
56
+ response = Cache.for('lightning.get_chan_info', params: { chan_id: params[:id].to_i }) do
57
+ LND.instance.middleware('lightning.get_chan_info') do
58
+ LND.instance.client.lightning.get_chan_info(chan_id: params[:id].to_i)
59
+ end
56
60
  end
57
- end
58
61
 
59
- @data = { get_chan_info: response }
60
- @id = @data[:get_chan_info].channel_id
61
- rescue StandardError => e
62
- @data = { get_chan_info: nil, error: e }
63
- @id = params[:id]
62
+ @data = { get_chan_info: response }
63
+ @id = @data[:get_chan_info].channel_id
64
+ rescue StandardError => e
65
+ @data = { get_chan_info: nil, error: e }
66
+ @id = params[:id]
67
+ end
68
+ elsif params[:describe_graph]
69
+ @data = { describe_graph: params[:describe_graph] }
70
+ @id = @data[:describe_graph].channel_id
64
71
  end
65
72
 
66
73
  fetch_from_fee_report!
@@ -69,6 +76,10 @@ module Lighstorm
69
76
  calculate_times_after_list_channels!
70
77
  end
71
78
 
79
+ def error?
80
+ !@data[:error].nil?
81
+ end
82
+
72
83
  def error
73
84
  @data[:error]
74
85
  end
@@ -78,6 +89,8 @@ module Lighstorm
78
89
  end
79
90
 
80
91
  def exposure
92
+ return 'public' if @data[:describe_graph]
93
+
81
94
  return unless @data[:list_channels]
82
95
 
83
96
  @data[:list_channels][:channels].first.private ? 'private' : 'public'
@@ -100,24 +113,70 @@ module Lighstorm
100
113
  end
101
114
 
102
115
  def accounting
103
- return nil unless @data[:get_chan_info]
104
-
105
116
  @accounting ||= ChannelAccounting.new(self)
106
117
  end
107
118
 
119
+ def partners(fetch: false)
120
+ @partners ||= if mine?
121
+ [myself, partner]
122
+ elsif @data[:describe_graph]
123
+ [
124
+ ChannelNode.new(
125
+ self,
126
+ Node.new({ public_key: @data[:describe_graph].node1_pub }, fetch: fetch)
127
+ ),
128
+ ChannelNode.new(
129
+ self,
130
+ Node.new({ public_key: @data[:describe_graph].node2_pub }, fetch: fetch)
131
+ )
132
+ ]
133
+ elsif @data[:get_chan_info]
134
+ [
135
+ ChannelNode.new(
136
+ self,
137
+ Node.new({ public_key: @data[:get_chan_info].node1_pub }, fetch: fetch)
138
+ ),
139
+ ChannelNode.new(
140
+ self,
141
+ Node.new({ public_key: @data[:get_chan_info].node2_pub }, fetch: fetch)
142
+ )
143
+ ]
144
+ else
145
+ raise 'missing data'
146
+ end
147
+ end
148
+
149
+ def mine?
150
+ my_node = Node.myself.public_key
151
+
152
+ if @data[:get_chan_info]
153
+ (
154
+ @data[:get_chan_info].node1_pub == my_node || @data[:get_chan_info].node2_pub == my_node
155
+ )
156
+ elsif @data[:describe_graph]
157
+ (
158
+ @data[:describe_graph].node1_pub == my_node || @data[:describe_graph].node2_pub == my_node
159
+ )
160
+ else
161
+ false
162
+ end
163
+ end
164
+
108
165
  def myself
109
- return nil unless @data[:get_chan_info]
166
+ raise 'not your channel' unless mine?
110
167
 
111
168
  @myself ||= ChannelNode.new(self, Node.myself)
112
169
  end
113
170
 
114
171
  def partner
115
- return nil unless @data[:get_chan_info]
172
+ raise 'not your channel' unless mine?
173
+
174
+ key = @data[:get_chan_info] ? :get_chan_info : :describe_graph
116
175
 
117
- public_key = if @data[:get_chan_info].node1_pub == myself.node.public_key
118
- @data[:get_chan_info].node2_pub
176
+ public_key = if @data[key].node1_pub == myself.node.public_key
177
+ @data[key].node2_pub
119
178
  else
120
- @data[:get_chan_info].node1_pub
179
+ @data[key].node1_pub
121
180
  end
122
181
 
123
182
  @partner ||= ChannelNode.new(self, Node.find_by_public_key(public_key))
@@ -126,21 +185,32 @@ module Lighstorm
126
185
  def raw
127
186
  {
128
187
  get_chan_info: @data[:get_chan_info].to_h,
129
- list_channels: { channels: @data[:list_channels][:channels].map(&:to_h) }
188
+ describe_graph: @data[:describe_graph].to_h,
189
+ list_channels: {
190
+ channels: @data[:list_channels] ? @data[:list_channels][:channels].map(&:to_h) : nil
191
+ }
130
192
  }
131
193
  end
132
194
 
133
195
  def to_h
134
- {
135
- id: id,
136
- opened_at: opened_at,
137
- up_at: up_at,
138
- active: active,
139
- exposure: exposure,
140
- accounting: accounting.to_h,
141
- partner: partner.to_h,
142
- myself: myself.to_h
143
- }
196
+ if @data[:get_chan_info]
197
+ {
198
+ id: id,
199
+ opened_at: opened_at,
200
+ up_at: up_at,
201
+ active: active,
202
+ exposure: exposure,
203
+ accounting: accounting.to_h,
204
+ partner: partner.to_h,
205
+ myself: myself.to_h
206
+ }
207
+ else
208
+ {
209
+ id: id,
210
+ accounting: accounting.to_h,
211
+ partners: partners.map(&:to_h)
212
+ }
213
+ end
144
214
  end
145
215
 
146
216
  private
data/models/nodes/node.rb CHANGED
@@ -27,6 +27,18 @@ module Lighstorm
27
27
  Node.new({ public_key: public_key }, myself: myself)
28
28
  end
29
29
 
30
+ def self.all
31
+ response = LND.instance.middleware('lightning.describe_graph') do
32
+ LND.instance.client.lightning.describe_graph
33
+ end
34
+
35
+ myself_public_key = myself.public_key
36
+
37
+ response.nodes.map do |raw_node|
38
+ Node.new({ describe_graph: raw_node }, myself: raw_node.pub_key == myself_public_key)
39
+ end
40
+ end
41
+
30
42
  def myself?
31
43
  @myself
32
44
  end
@@ -36,52 +48,98 @@ module Lighstorm
36
48
  end
37
49
 
38
50
  def channels
39
- raise 'cannot list channels from a node that is not yours' unless myself?
40
-
41
- Channel.all
51
+ if myself?
52
+ Channel.mine
53
+ else
54
+ Channel.all
55
+ end
42
56
  end
43
57
 
44
58
  def raw
45
59
  {
46
- get_node_info: @data[:get_node_info].to_h
60
+ get_node_info: @data[:get_node_info].to_h,
61
+ describe_graph: @data[:describe_graph].to_h
47
62
  }
48
63
  end
49
64
 
50
65
  def to_h
51
- {
52
- alias: @alias,
53
- public_key: @public_key,
54
- color: @color,
55
- platform: platform.to_h
56
- }
66
+ if (@data[:get_node_info] || @data[:describe_graph]) && myself?
67
+ {
68
+ alias: @alias,
69
+ public_key: @public_key,
70
+ color: @color,
71
+ platform: platform.to_h
72
+ }
73
+ elsif @data[:get_node_info] || @data[:describe_graph]
74
+ {
75
+ alias: @alias,
76
+ public_key: @public_key,
77
+ color: @color
78
+ }
79
+ else
80
+ {
81
+ public_key: @public_key
82
+ }
83
+ end
57
84
  end
58
85
 
59
- private
86
+ def myself
87
+ return @myself unless @myself.nil?
60
88
 
61
- def initialize(params, myself: false)
62
- response = Cache.for('lightning.get_node_info', params: { pub_key: params[:public_key] }) do
63
- LND.instance.middleware('lightning.get_node_info') do
64
- LND.instance.client.lightning.get_node_info(pub_key: params[:public_key])
89
+ response_get_info = Cache.for('lightning.get_info') do
90
+ LND.instance.middleware('lightning.get_info') do
91
+ LND.instance.client.lightning.get_info
65
92
  end
66
93
  end
67
94
 
68
- unless myself
69
- response_get_info = Cache.for('lightning.get_info') do
70
- LND.instance.middleware('lightning.get_info') do
71
- LND.instance.client.lightning.get_info
95
+ @myself = public_key == response_get_info.identity_pubkey
96
+ end
97
+
98
+ def error?
99
+ !@data[:error].nil?
100
+ end
101
+
102
+ def error
103
+ @data[:error]
104
+ end
105
+
106
+ def initialize(params, myself: false, fetch: true)
107
+ if params[:public_key] && fetch
108
+ begin
109
+ response = Cache.for('lightning.get_node_info', params: { pub_key: params[:public_key] }) do
110
+ LND.instance.middleware('lightning.get_node_info') do
111
+ LND.instance.client.lightning.get_node_info(pub_key: params[:public_key])
112
+ end
72
113
  end
114
+
115
+ @data = { get_node_info: response }
116
+ @raw_node = response.node
117
+ rescue StandardError => e
118
+ @data = { get_node_info: nil, error: e }
119
+ @public_key = params[:public_key]
73
120
  end
121
+ elsif params[:describe_graph]
122
+ @data = { describe_graph: params[:describe_graph] }
74
123
 
75
- myself = true if params[:public_key] == response_get_info.identity_pubkey
124
+ @raw_node = params[:describe_graph]
125
+ else
126
+ @data = {}
76
127
  end
77
128
 
78
- @data = { get_node_info: response }
129
+ @myself = myself
130
+
131
+ if params[:public_key] && !fetch
132
+ @public_key = params[:public_key]
133
+ return
134
+ end
79
135
 
80
136
  @myself = myself
81
137
 
82
- @alias = @data[:get_node_info].node.alias
83
- @public_key = @data[:get_node_info].node.pub_key
84
- @color = @data[:get_node_info].node.color
138
+ return unless @raw_node
139
+
140
+ @alias = @raw_node.alias
141
+ @public_key = @raw_node.pub_key
142
+ @color = @raw_node.color
85
143
  end
86
144
  end
87
145
  end
@@ -4,6 +4,8 @@ require 'dotenv/load'
4
4
 
5
5
  require_relative '../../static/spec'
6
6
 
7
+ require_relative '../../models/satoshis'
8
+
7
9
  require_relative '../../models/nodes/node'
8
10
 
9
11
  require_relative '../../models/edges/channel'
data/static/spec.rb CHANGED
@@ -4,7 +4,7 @@ module Lighstorm
4
4
  module Static
5
5
  SPEC = {
6
6
  name: 'lighstorm',
7
- version: '0.0.2',
7
+ version: '0.0.3',
8
8
  author: 'icebaker',
9
9
  summary: 'API for interacting with a Lightning Node.',
10
10
  description: 'Lighstorm is an opinionated abstraction layer on top of the lnd-client for interacting with a Lightning Node.',
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lighstorm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - icebaker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-09 00:00:00.000000000 Z
11
+ date: 2023-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dotenv
@@ -36,14 +36,14 @@ dependencies:
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: 0.0.4
39
+ version: 0.0.5
40
40
  type: :runtime
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: 0.0.4
46
+ version: 0.0.5
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: zache
49
49
  requirement: !ruby/object:Gem::Requirement
@@ -78,6 +78,7 @@ files:
78
78
  - models/connections/channel_node/accounting.rb
79
79
  - models/connections/channel_node/constraints.rb
80
80
  - models/connections/channel_node/fee.rb
81
+ - models/connections/channel_node/htlc.rb
81
82
  - models/connections/channel_node/policy.rb
82
83
  - models/connections/forward_channel.rb
83
84
  - models/connections/payment_channel.rb