lighstorm 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/README.md +7 -7
  4. data/adapters/bitcoin/address.rb +19 -0
  5. data/adapters/bitcoin/transaction.rb +34 -0
  6. data/adapters/lightning/connections/channel_node/fee.rb +28 -0
  7. data/adapters/lightning/connections/channel_node/policy.rb +63 -0
  8. data/adapters/lightning/connections/channel_node.rb +77 -0
  9. data/adapters/lightning/connections/payment_channel.rb +30 -0
  10. data/adapters/lightning/edges/channel.rb +98 -0
  11. data/adapters/lightning/edges/forward.rb +42 -0
  12. data/adapters/lightning/edges/payment/purpose.rb +45 -0
  13. data/adapters/lightning/edges/payment.rb +89 -0
  14. data/adapters/lightning/invoice.rb +186 -0
  15. data/adapters/lightning/nodes/node.rb +113 -0
  16. data/controllers/action.rb +1 -1
  17. data/controllers/bitcoin/address/actions/create.rb +91 -0
  18. data/controllers/bitcoin/address/actions/pay.rb +119 -0
  19. data/controllers/bitcoin/address.rb +24 -0
  20. data/controllers/bitcoin/transaction/all.rb +65 -0
  21. data/controllers/bitcoin/transaction.rb +25 -0
  22. data/controllers/concerns/impersonatable.rb +5 -3
  23. data/controllers/connection.rb +1 -1
  24. data/controllers/lightning/channel/actions/apply_gossip.rb +201 -0
  25. data/controllers/lightning/channel/actions/update_fee.rb +94 -0
  26. data/controllers/lightning/channel/all.rb +85 -0
  27. data/controllers/lightning/channel/find_by_id.rb +155 -0
  28. data/controllers/lightning/channel/mine.rb +116 -0
  29. data/controllers/lightning/channel.rb +43 -0
  30. data/controllers/lightning/forward/all.rb +250 -0
  31. data/controllers/lightning/forward/group_by_channel.rb +91 -0
  32. data/controllers/lightning/forward.rb +36 -0
  33. data/controllers/lightning/invoice/actions/create.rb +90 -0
  34. data/controllers/lightning/invoice/actions/pay.rb +97 -0
  35. data/controllers/lightning/invoice/actions/pay_through_route.rb +73 -0
  36. data/controllers/lightning/invoice/all.rb +82 -0
  37. data/controllers/lightning/invoice/decode.rb +50 -0
  38. data/controllers/lightning/invoice/find_by_code.rb +76 -0
  39. data/controllers/lightning/invoice/find_by_secret_hash.rb +56 -0
  40. data/controllers/lightning/invoice.rb +64 -0
  41. data/controllers/lightning/node/actions/apply_gossip.rb +114 -0
  42. data/controllers/lightning/node/actions/pay.rb +117 -0
  43. data/controllers/lightning/node/all.rb +69 -0
  44. data/controllers/lightning/node/find_by_public_key.rb +55 -0
  45. data/controllers/lightning/node/myself.rb +36 -0
  46. data/controllers/lightning/node.rb +35 -0
  47. data/controllers/lightning/payment/actions/pay.rb +110 -0
  48. data/controllers/lightning/payment/all.rb +439 -0
  49. data/controllers/lightning/payment.rb +37 -0
  50. data/controllers/lightning/secret/valid_proof.rb +60 -0
  51. data/controllers/lightning/secret.rb +16 -0
  52. data/controllers/wallet/activity/all.rb +196 -0
  53. data/controllers/wallet/activity.rb +28 -0
  54. data/controllers/wallet/balance.rb +3 -3
  55. data/controllers/wallet.rb +1 -1
  56. data/docs/README.md +229 -148
  57. data/docs/_coverpage.md +1 -1
  58. data/docs/index.html +1 -1
  59. data/models/bitcoin/address.rb +49 -0
  60. data/models/bitcoin/transaction.rb +41 -0
  61. data/models/concerns/protectable.rb +1 -1
  62. data/models/errors.rb +7 -8
  63. data/models/lightning/connections/channel_node/accounting.rb +27 -0
  64. data/models/lightning/connections/channel_node/fee.rb +82 -0
  65. data/models/lightning/connections/channel_node/htlc/blocks/delta.rb +41 -0
  66. data/models/lightning/connections/channel_node/htlc.rb +88 -0
  67. data/models/lightning/connections/channel_node/policy.rb +41 -0
  68. data/models/lightning/connections/channel_node.rb +86 -0
  69. data/models/lightning/connections/forward_channel.rb +31 -0
  70. data/models/lightning/connections/payment_channel.rb +58 -0
  71. data/models/lightning/edges/channel/accounting.rb +87 -0
  72. data/models/lightning/edges/channel/hop.rb +67 -0
  73. data/models/lightning/edges/channel.rb +197 -0
  74. data/models/lightning/edges/forward.rb +51 -0
  75. data/models/lightning/edges/groups/channel_forwards/analysis.rb +71 -0
  76. data/models/lightning/edges/groups/channel_forwards.rb +39 -0
  77. data/models/lightning/edges/payment.rb +104 -0
  78. data/models/lightning/invoice.rb +115 -0
  79. data/models/lightning/nodes/node/lightning.rb +33 -0
  80. data/models/lightning/nodes/node/platform.rb +50 -0
  81. data/models/lightning/nodes/node.rb +145 -0
  82. data/models/lightning/rate.rb +37 -0
  83. data/models/lightning/secret.rb +57 -0
  84. data/models/satoshis.rb +1 -1
  85. data/models/wallet/activity.rb +54 -0
  86. data/models/wallet/balance.rb +42 -0
  87. data/ports/dsl/lighstorm.rb +29 -16
  88. data/static/cache.rb +2 -0
  89. data/static/spec.rb +1 -1
  90. metadata +74 -68
  91. data/adapters/connections/channel_node/fee.rb +0 -26
  92. data/adapters/connections/channel_node/policy.rb +0 -61
  93. data/adapters/connections/channel_node.rb +0 -75
  94. data/adapters/connections/payment_channel.rb +0 -28
  95. data/adapters/edges/channel.rb +0 -96
  96. data/adapters/edges/forward.rb +0 -40
  97. data/adapters/edges/payment/purpose.rb +0 -43
  98. data/adapters/edges/payment.rb +0 -87
  99. data/adapters/invoice.rb +0 -184
  100. data/adapters/nodes/node.rb +0 -111
  101. data/adapters/transaction.rb +0 -23
  102. data/controllers/activity/all.rb +0 -194
  103. data/controllers/activity.rb +0 -26
  104. data/controllers/channel/actions/apply_gossip.rb +0 -193
  105. data/controllers/channel/actions/update_fee.rb +0 -91
  106. data/controllers/channel/all.rb +0 -83
  107. data/controllers/channel/find_by_id.rb +0 -153
  108. data/controllers/channel/mine.rb +0 -114
  109. data/controllers/channel.rb +0 -41
  110. data/controllers/forward/all.rb +0 -248
  111. data/controllers/forward/group_by_channel.rb +0 -89
  112. data/controllers/forward.rb +0 -34
  113. data/controllers/invoice/actions/create.rb +0 -88
  114. data/controllers/invoice/actions/pay.rb +0 -95
  115. data/controllers/invoice/actions/pay_through_route.rb +0 -71
  116. data/controllers/invoice/all.rb +0 -80
  117. data/controllers/invoice/decode.rb +0 -44
  118. data/controllers/invoice/find_by_code.rb +0 -70
  119. data/controllers/invoice/find_by_secret_hash.rb +0 -54
  120. data/controllers/invoice.rb +0 -62
  121. data/controllers/node/actions/apply_gossip.rb +0 -112
  122. data/controllers/node/actions/pay.rb +0 -115
  123. data/controllers/node/all.rb +0 -67
  124. data/controllers/node/find_by_public_key.rb +0 -53
  125. data/controllers/node/myself.rb +0 -34
  126. data/controllers/node.rb +0 -33
  127. data/controllers/payment/actions/pay.rb +0 -108
  128. data/controllers/payment/all.rb +0 -437
  129. data/controllers/payment.rb +0 -35
  130. data/controllers/secret/valid_proof.rb +0 -58
  131. data/controllers/transaction/all.rb +0 -61
  132. data/controllers/transaction.rb +0 -25
  133. data/models/activity.rb +0 -52
  134. data/models/connections/channel_node/accounting.rb +0 -25
  135. data/models/connections/channel_node/fee.rb +0 -80
  136. data/models/connections/channel_node/htlc/blocks/delta.rb +0 -39
  137. data/models/connections/channel_node/htlc.rb +0 -86
  138. data/models/connections/channel_node/policy.rb +0 -39
  139. data/models/connections/channel_node.rb +0 -84
  140. data/models/connections/forward_channel.rb +0 -29
  141. data/models/connections/payment_channel.rb +0 -56
  142. data/models/edges/channel/accounting.rb +0 -85
  143. data/models/edges/channel/hop.rb +0 -65
  144. data/models/edges/channel.rb +0 -195
  145. data/models/edges/forward.rb +0 -49
  146. data/models/edges/groups/channel_forwards/analysis.rb +0 -69
  147. data/models/edges/groups/channel_forwards.rb +0 -37
  148. data/models/edges/payment.rb +0 -102
  149. data/models/invoice.rb +0 -113
  150. data/models/nodes/node/lightning.rb +0 -31
  151. data/models/nodes/node/platform.rb +0 -48
  152. data/models/nodes/node.rb +0 -143
  153. data/models/rate.rb +0 -35
  154. data/models/secret.rb +0 -55
  155. data/models/transaction.rb +0 -39
  156. data/models/wallet.rb +0 -40
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative './concerns/impersonatable'
4
-
5
- require_relative './transaction/all'
6
-
7
- module Lighstorm
8
- module Controllers
9
- module Transaction
10
- extend Impersonatable
11
-
12
- class DSL < Impersonatable::DSL
13
- def all(direction: nil, how: nil, layer: nil, limit: nil)
14
- All.model(All.data(
15
- components,
16
- direction: direction,
17
- how: how,
18
- layer: layer,
19
- limit: limit
20
- ))
21
- end
22
- end
23
- end
24
- end
25
- end
data/models/activity.rb DELETED
@@ -1,52 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'invoice'
4
- require_relative 'transaction'
5
-
6
- module Lighstorm
7
- module Models
8
- class Activity
9
- attr_reader :direction, :at, :message, :layer, :how, :_key
10
-
11
- def initialize(data)
12
- @data = data
13
-
14
- @_key = @data[:_key]
15
- @at = @data[:at]
16
- @direction = @data[:direction]
17
- @layer = @data[:layer]
18
- @how = @data[:how]
19
- @message = @data[:message]
20
- end
21
-
22
- def amount
23
- @amount ||= Satoshis.new(millisatoshis: @data[:amount][:millisatoshis])
24
- end
25
-
26
- def invoice
27
- @invoice ||= @data[:data][:invoice].nil? ? nil : Invoice.new(@data[:data][:invoice], nil)
28
- end
29
-
30
- def transaction
31
- @transaction ||= @data[:data][:transaction].nil? ? nil : Transaction.new(@data[:data][:transaction])
32
- end
33
-
34
- def to_h
35
- output = {
36
- _key: _key,
37
- at: at,
38
- direction: direction,
39
- layer: layer,
40
- how: how,
41
- amount: amount.to_h,
42
- message: message
43
- }
44
-
45
- output[:invoice] = invoice.to_h unless invoice.nil?
46
- output[:transaction] = transaction.to_h unless transaction.nil?
47
-
48
- output
49
- end
50
- end
51
- end
52
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../../satoshis'
4
-
5
- module Lighstorm
6
- module Models
7
- class ChannelNodeAccounting
8
- def initialize(data)
9
- @data = data
10
- end
11
-
12
- def balance
13
- @balance ||= Satoshis.new(millisatoshis: @data[:balance][:millisatoshis])
14
- end
15
-
16
- def to_h
17
- { balance: balance.to_h }
18
- end
19
-
20
- def dump
21
- Marshal.load(Marshal.dump(@data))
22
- end
23
- end
24
- end
25
- end
@@ -1,80 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'securerandom'
4
-
5
- require_relative '../../satoshis'
6
- require_relative '../../rate'
7
- require_relative '../../concerns/protectable'
8
-
9
- require_relative '../../../controllers/channel/actions/update_fee'
10
-
11
- module Lighstorm
12
- module Models
13
- class Fee
14
- include Protectable
15
-
16
- def initialize(policy, components, data)
17
- @policy = policy
18
- @components = components
19
- @data = data
20
- end
21
-
22
- def base
23
- return nil unless @data[:base]
24
-
25
- @base ||= Satoshis.new(millisatoshis: @data[:base][:millisatoshis])
26
- end
27
-
28
- def rate
29
- return nil unless @data[:rate]
30
-
31
- @rate ||= Rate.new(parts_per_million: @data[:rate][:parts_per_million])
32
- end
33
-
34
- def to_h
35
- result = {}
36
-
37
- result[:base] = base.to_h if @data[:base]
38
- result[:rate] = rate.to_h if @data[:rate]
39
-
40
- return nil if result.empty?
41
-
42
- result
43
- end
44
-
45
- def dump
46
- Marshal.load(Marshal.dump(@data))
47
- end
48
-
49
- def update(params, preview: false, &vcr)
50
- raise MissingComponentsError if @components.nil?
51
-
52
- Controllers::Channel::UpdateFee.perform(
53
- @components,
54
- @policy, @policy.transaction, params,
55
- preview: preview, &vcr
56
- )
57
- end
58
-
59
- def base=(value)
60
- protect!(value)
61
-
62
- @base = value[:value]
63
-
64
- @data[:base] = { millisatoshis: @base.millisatoshis }
65
-
66
- base
67
- end
68
-
69
- def rate=(value)
70
- protect!(value)
71
-
72
- @rate = value[:value]
73
-
74
- @data[:rate] = { parts_per_million: @rate.parts_per_million }
75
-
76
- rate
77
- end
78
- end
79
- end
80
- end
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../../../../concerns/protectable'
4
-
5
- module Lighstorm
6
- module Models
7
- class BlocksDelta
8
- include Protectable
9
-
10
- def initialize(data)
11
- @data = data
12
- end
13
-
14
- def minimum
15
- @minimum ||= @data[:minimum]
16
- end
17
-
18
- def to_h
19
- {
20
- minimum: minimum
21
- }
22
- end
23
-
24
- def dump
25
- Marshal.load(Marshal.dump(@data))
26
- end
27
-
28
- def minimum=(value)
29
- protect!(value)
30
-
31
- @minimum = value[:value]
32
-
33
- @data[:minimum] = @minimum
34
-
35
- minimum
36
- end
37
- end
38
- end
39
- end
@@ -1,86 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../../satoshis'
4
- require_relative '../../rate'
5
- require_relative '../../concerns/protectable'
6
- require_relative 'htlc/blocks/delta'
7
-
8
- module Lighstorm
9
- module Models
10
- class HTLC
11
- include Protectable
12
-
13
- def initialize(data)
14
- @data = data
15
- end
16
-
17
- def minimum
18
- @minimum ||= if @data[:minimum]
19
- Satoshis.new(
20
- millisatoshis: @data[:minimum][:millisatoshis]
21
- )
22
- end
23
- end
24
-
25
- def maximum
26
- @maximum ||= if @data[:maximum]
27
- Satoshis.new(
28
- millisatoshis: @data[:maximum][:millisatoshis]
29
- )
30
- end
31
- end
32
-
33
- def blocks
34
- @blocks ||= Struct.new(:data) do
35
- def delta
36
- @delta ||= BlocksDelta.new(data[:delta] || {})
37
- end
38
-
39
- def dump
40
- { delta: delta.dump }
41
- end
42
-
43
- def to_h
44
- { delta: delta.to_h }
45
- end
46
- end.new(@data[:blocks] || {})
47
- end
48
-
49
- def to_h
50
- {
51
- minimum: minimum.to_h,
52
- maximum: maximum.to_h,
53
- blocks: blocks.to_h
54
- }
55
- end
56
-
57
- def dump
58
- result = Marshal.load(Marshal.dump(@data))
59
-
60
- result = result.merge({ blocks: blocks.dump }) if @data[:blocks]
61
-
62
- result
63
- end
64
-
65
- def minimum=(value)
66
- protect!(value)
67
-
68
- @minimum = value[:value]
69
-
70
- @data[:minimum] = { millisatoshis: @minimum.millisatoshis }
71
-
72
- minimum
73
- end
74
-
75
- def maximum=(value)
76
- protect!(value)
77
-
78
- @maximum = value[:value]
79
-
80
- @data[:maximum] = { millisatoshis: @maximum.millisatoshis }
81
-
82
- maximum
83
- end
84
- end
85
- end
86
- end
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'fee'
4
- require_relative 'htlc'
5
-
6
- module Lighstorm
7
- module Models
8
- class Policy
9
- attr_reader :transaction
10
-
11
- def initialize(data, components, transaction)
12
- @data = data
13
- @components = components
14
- @transaction = transaction
15
- end
16
-
17
- def fee
18
- @fee ||= Fee.new(self, @components, @data ? @data[:fee] : {})
19
- end
20
-
21
- def htlc
22
- @htlc ||= HTLC.new(@data ? @data[:htlc] : {})
23
- end
24
-
25
- def to_h
26
- { fee: fee.to_h, htlc: htlc.to_h }
27
- end
28
-
29
- def dump
30
- result = Marshal.load(Marshal.dump(@data))
31
-
32
- result = result.merge({ fee: fee.dump }) if @data && @data[:fee]
33
- result = result.merge({ htlc: htlc.dump }) if @data && @data[:htlc]
34
-
35
- result
36
- end
37
- end
38
- end
39
- end
@@ -1,84 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'channel_node/policy'
4
- require_relative 'channel_node/accounting'
5
-
6
- require_relative '../nodes/node'
7
- require_relative '../errors'
8
-
9
- module Lighstorm
10
- module Models
11
- class ChannelNode
12
- include Protectable
13
-
14
- attr_reader :state
15
-
16
- def initialize(data, components, is_mine, transaction)
17
- @data = data
18
- @components = components
19
- @state = data[:state]
20
- @initiator = data[:initiator]
21
- @is_mine = is_mine
22
- @transaction = transaction
23
- end
24
-
25
- def active?
26
- state == 'active'
27
- end
28
-
29
- def initiator?
30
- @initiator
31
- end
32
-
33
- def node
34
- @node ||= Node.new(@data[:node], @components)
35
- end
36
-
37
- def policy
38
- @policy ||= Policy.new(@data[:policy], @components, @transaction)
39
- end
40
-
41
- def accounting
42
- raise Errors::NotYourChannelError unless @is_mine
43
-
44
- @accounting ||= @data[:accounting] ? ChannelNodeAccounting.new(@data[:accounting]) : nil
45
- end
46
-
47
- def to_h
48
- restult = {
49
- state: state,
50
- initiator: @initiator,
51
- node: node.to_h
52
- }
53
-
54
- restult[:accounting] = accounting.to_h if @is_mine
55
- restult[:policy] = policy.to_h if @data[:policy]
56
-
57
- restult
58
- end
59
-
60
- def dump
61
- result = Marshal.load(Marshal.dump(@data)).merge(
62
- {
63
- node: node.dump,
64
- policy: policy.dump
65
- }
66
- )
67
-
68
- result[:accounting] = accounting.dump if @is_mine
69
- result.delete(:policy) if result[:policy].nil?
70
-
71
- result
72
- end
73
-
74
- def state=(value)
75
- protect!(value)
76
-
77
- @state = value[:value]
78
- @data[:state] = @state
79
-
80
- state
81
- end
82
- end
83
- end
84
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../edges/channel'
4
-
5
- module Lighstorm
6
- module Models
7
- class ForwardChannel
8
- def initialize(data, components)
9
- @data = data
10
- @components = components
11
- end
12
-
13
- def amount
14
- @amount ||= Satoshis.new(millisatoshis: @data[:amount][:millisatoshis])
15
- end
16
-
17
- def channel
18
- @channel ||= Channel.new(@data[:channel], @components)
19
- end
20
-
21
- def to_h
22
- {
23
- amount: amount.to_h,
24
- channel: channel.to_h
25
- }
26
- end
27
- end
28
- end
29
- end
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../edges/channel/hop'
4
- require_relative '../nodes/node'
5
-
6
- module Lighstorm
7
- module Models
8
- class PaymentChannel
9
- attr_reader :hop
10
-
11
- def initialize(data, payment)
12
- @data = data
13
-
14
- @hop = data[:hop]
15
- @payment = payment
16
- end
17
-
18
- def first?
19
- @hop == 1
20
- end
21
-
22
- def last?
23
- @data[:is_last] == true
24
- end
25
-
26
- def amount
27
- @amount ||= @data[:amount] ? Satoshis.new(millisatoshis: @data[:amount][:millisatoshis]) : nil
28
- end
29
-
30
- def fee
31
- @fee ||= @data[:fee] ? Satoshis.new(millisatoshis: @data[:fee][:millisatoshis]) : nil
32
- end
33
-
34
- def channel
35
- @channel ||= HopChannel.new(@data, @payment)
36
- end
37
-
38
- def to_h
39
- result = {
40
- hop: hop,
41
- channel: channel.to_h
42
- }
43
-
44
- result[:amount] = amount.to_h if amount
45
- if fee
46
- result[:fee] = {
47
- millisatoshis: fee.millisatoshis,
48
- parts_per_million: fee.parts_per_million(amount.millisatoshis)
49
- }
50
- end
51
-
52
- result
53
- end
54
- end
55
- end
56
- end
@@ -1,85 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../../satoshis'
4
- require_relative '../../errors'
5
- require_relative '../../concerns/protectable'
6
-
7
- module Lighstorm
8
- module Models
9
- class ChannelAccounting
10
- include Protectable
11
-
12
- def initialize(data, is_mine)
13
- @data = data
14
- @is_mine = is_mine
15
- end
16
-
17
- def capacity
18
- @capacity ||= if @data[:capacity]
19
- Satoshis.new(
20
- millisatoshis: @data[:capacity][:millisatoshis]
21
- )
22
- end
23
- end
24
-
25
- def sent
26
- raise Errors::NotYourChannelError unless @is_mine
27
-
28
- @sent ||= if @data[:sent]
29
- Satoshis.new(
30
- millisatoshis: @data[:sent][:millisatoshis]
31
- )
32
- end
33
- end
34
-
35
- def received
36
- raise Errors::NotYourChannelError unless @is_mine
37
-
38
- @received ||= if @data[:received]
39
- Satoshis.new(
40
- millisatoshis: @data[:received][:millisatoshis]
41
- )
42
- end
43
- end
44
-
45
- def unsettled
46
- raise Errors::NotYourChannelError unless @is_mine
47
-
48
- @unsettled ||= if @data[:unsettled]
49
- Satoshis.new(
50
- millisatoshis: @data[:unsettled][:millisatoshis]
51
- )
52
- end
53
- end
54
-
55
- def to_h
56
- if @is_mine
57
- {
58
- capacity: capacity.to_h,
59
- sent: sent.to_h,
60
- received: received.to_h,
61
- unsettled: unsettled.to_h
62
- }
63
- else
64
- {
65
- capacity: capacity.to_h
66
- }
67
- end
68
- end
69
-
70
- def dump
71
- Marshal.load(Marshal.dump(@data))
72
- end
73
-
74
- def capacity=(value)
75
- protect!(value)
76
-
77
- @capacity = value[:value]
78
-
79
- @data[:capacity][:millisatoshis] = @capacity.millisatoshis
80
-
81
- capacity
82
- end
83
- end
84
- end
85
- end
@@ -1,65 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../channel'
4
-
5
- module Lighstorm
6
- module Models
7
- class HopChannel < Channel
8
- def initialize(data, payment)
9
- @hop_data = data
10
- @payment = payment
11
- super(data[:channel], nil)
12
- end
13
-
14
- def target
15
- partners.find do |partner|
16
- partner.node.public_key == @hop_data[:channel][:target][:public_key]
17
- end.node
18
- end
19
-
20
- def exit
21
- @exit ||= if include_myself? && @hop_data[:hop] == 1
22
- partners.reverse.find do |partner|
23
- !partner.node.myself?
24
- end.node
25
- elsif @hop_data[:hop] == 1
26
- target
27
- end
28
- end
29
-
30
- def entry
31
- return nil if @hop_data[:is_last] && @hop_data[:hop] == 1
32
-
33
- @entry ||= if include_myself? && @hop_data[:is_last]
34
- if partners.size > 1
35
- partners.reverse.find do |partner|
36
- !partner.node.myself?
37
- end.node
38
- else
39
- @payment.hops[@payment.hops.size - 2].channel.target
40
- end
41
- end
42
- end
43
-
44
- def include_myself?
45
- !partners.find { |partner| partner.node.myself? }.nil?
46
- end
47
-
48
- def to_h
49
- if !known? && !partners.size.positive?
50
- { _key: _key, id: id }
51
- else
52
- target_hash = target.to_h
53
- target_hash.delete(:platform)
54
-
55
- result = { _key: _key, id: id, target: target_hash }
56
-
57
- result[:entry] = entry.to_h if entry
58
- result[:exit] = self.exit.to_h if self.exit
59
-
60
- result
61
- end
62
- end
63
- end
64
- end
65
- end