lighstorm 0.0.15 → 0.0.16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +7 -7
- data/adapters/bitcoin/address.rb +19 -0
- data/adapters/bitcoin/transaction.rb +34 -0
- data/adapters/lightning/connections/channel_node/fee.rb +28 -0
- data/adapters/lightning/connections/channel_node/policy.rb +63 -0
- data/adapters/lightning/connections/channel_node.rb +77 -0
- data/adapters/lightning/connections/payment_channel.rb +30 -0
- data/adapters/lightning/edges/channel.rb +98 -0
- data/adapters/lightning/edges/forward.rb +42 -0
- data/adapters/lightning/edges/payment/purpose.rb +45 -0
- data/adapters/lightning/edges/payment.rb +89 -0
- data/adapters/lightning/invoice.rb +186 -0
- data/adapters/lightning/nodes/node.rb +113 -0
- data/controllers/action.rb +1 -1
- data/controllers/bitcoin/address/actions/create.rb +91 -0
- data/controllers/bitcoin/address/actions/pay.rb +119 -0
- data/controllers/bitcoin/address.rb +24 -0
- data/controllers/bitcoin/transaction/all.rb +65 -0
- data/controllers/bitcoin/transaction.rb +25 -0
- data/controllers/concerns/impersonatable.rb +5 -3
- data/controllers/connection.rb +1 -1
- data/controllers/lightning/channel/actions/apply_gossip.rb +201 -0
- data/controllers/lightning/channel/actions/update_fee.rb +94 -0
- data/controllers/lightning/channel/all.rb +85 -0
- data/controllers/lightning/channel/find_by_id.rb +155 -0
- data/controllers/lightning/channel/mine.rb +116 -0
- data/controllers/lightning/channel.rb +43 -0
- data/controllers/lightning/forward/all.rb +250 -0
- data/controllers/lightning/forward/group_by_channel.rb +91 -0
- data/controllers/lightning/forward.rb +36 -0
- data/controllers/lightning/invoice/actions/create.rb +90 -0
- data/controllers/lightning/invoice/actions/pay.rb +97 -0
- data/controllers/lightning/invoice/actions/pay_through_route.rb +73 -0
- data/controllers/lightning/invoice/all.rb +82 -0
- data/controllers/lightning/invoice/decode.rb +50 -0
- data/controllers/lightning/invoice/find_by_code.rb +76 -0
- data/controllers/lightning/invoice/find_by_secret_hash.rb +56 -0
- data/controllers/lightning/invoice.rb +64 -0
- data/controllers/lightning/node/actions/apply_gossip.rb +114 -0
- data/controllers/lightning/node/actions/pay.rb +117 -0
- data/controllers/lightning/node/all.rb +69 -0
- data/controllers/lightning/node/find_by_public_key.rb +55 -0
- data/controllers/lightning/node/myself.rb +36 -0
- data/controllers/lightning/node.rb +35 -0
- data/controllers/lightning/payment/actions/pay.rb +110 -0
- data/controllers/lightning/payment/all.rb +439 -0
- data/controllers/lightning/payment.rb +37 -0
- data/controllers/lightning/secret/valid_proof.rb +60 -0
- data/controllers/lightning/secret.rb +16 -0
- data/controllers/wallet/activity/all.rb +196 -0
- data/controllers/wallet/activity.rb +28 -0
- data/controllers/wallet/balance.rb +3 -3
- data/controllers/wallet.rb +1 -1
- data/docs/README.md +229 -148
- data/docs/_coverpage.md +1 -1
- data/docs/index.html +1 -1
- data/models/bitcoin/address.rb +49 -0
- data/models/bitcoin/transaction.rb +41 -0
- data/models/concerns/protectable.rb +1 -1
- data/models/errors.rb +7 -8
- data/models/lightning/connections/channel_node/accounting.rb +27 -0
- data/models/lightning/connections/channel_node/fee.rb +82 -0
- data/models/lightning/connections/channel_node/htlc/blocks/delta.rb +41 -0
- data/models/lightning/connections/channel_node/htlc.rb +88 -0
- data/models/lightning/connections/channel_node/policy.rb +41 -0
- data/models/lightning/connections/channel_node.rb +86 -0
- data/models/lightning/connections/forward_channel.rb +31 -0
- data/models/lightning/connections/payment_channel.rb +58 -0
- data/models/lightning/edges/channel/accounting.rb +87 -0
- data/models/lightning/edges/channel/hop.rb +67 -0
- data/models/lightning/edges/channel.rb +197 -0
- data/models/lightning/edges/forward.rb +51 -0
- data/models/lightning/edges/groups/channel_forwards/analysis.rb +71 -0
- data/models/lightning/edges/groups/channel_forwards.rb +39 -0
- data/models/lightning/edges/payment.rb +104 -0
- data/models/lightning/invoice.rb +115 -0
- data/models/lightning/nodes/node/lightning.rb +33 -0
- data/models/lightning/nodes/node/platform.rb +50 -0
- data/models/lightning/nodes/node.rb +145 -0
- data/models/lightning/rate.rb +37 -0
- data/models/lightning/secret.rb +57 -0
- data/models/satoshis.rb +1 -1
- data/models/wallet/activity.rb +54 -0
- data/models/wallet/balance.rb +42 -0
- data/ports/dsl/lighstorm.rb +29 -16
- data/static/cache.rb +2 -0
- data/static/spec.rb +1 -1
- metadata +74 -68
- data/adapters/connections/channel_node/fee.rb +0 -26
- data/adapters/connections/channel_node/policy.rb +0 -61
- data/adapters/connections/channel_node.rb +0 -75
- data/adapters/connections/payment_channel.rb +0 -28
- data/adapters/edges/channel.rb +0 -96
- data/adapters/edges/forward.rb +0 -40
- data/adapters/edges/payment/purpose.rb +0 -43
- data/adapters/edges/payment.rb +0 -87
- data/adapters/invoice.rb +0 -184
- data/adapters/nodes/node.rb +0 -111
- data/adapters/transaction.rb +0 -23
- data/controllers/activity/all.rb +0 -194
- data/controllers/activity.rb +0 -26
- data/controllers/channel/actions/apply_gossip.rb +0 -193
- data/controllers/channel/actions/update_fee.rb +0 -91
- data/controllers/channel/all.rb +0 -83
- data/controllers/channel/find_by_id.rb +0 -153
- data/controllers/channel/mine.rb +0 -114
- data/controllers/channel.rb +0 -41
- data/controllers/forward/all.rb +0 -248
- data/controllers/forward/group_by_channel.rb +0 -89
- data/controllers/forward.rb +0 -34
- data/controllers/invoice/actions/create.rb +0 -88
- data/controllers/invoice/actions/pay.rb +0 -95
- data/controllers/invoice/actions/pay_through_route.rb +0 -71
- data/controllers/invoice/all.rb +0 -80
- data/controllers/invoice/decode.rb +0 -44
- data/controllers/invoice/find_by_code.rb +0 -70
- data/controllers/invoice/find_by_secret_hash.rb +0 -54
- data/controllers/invoice.rb +0 -62
- data/controllers/node/actions/apply_gossip.rb +0 -112
- data/controllers/node/actions/pay.rb +0 -115
- data/controllers/node/all.rb +0 -67
- data/controllers/node/find_by_public_key.rb +0 -53
- data/controllers/node/myself.rb +0 -34
- data/controllers/node.rb +0 -33
- data/controllers/payment/actions/pay.rb +0 -108
- data/controllers/payment/all.rb +0 -437
- data/controllers/payment.rb +0 -35
- data/controllers/secret/valid_proof.rb +0 -58
- data/controllers/transaction/all.rb +0 -61
- data/controllers/transaction.rb +0 -25
- data/models/activity.rb +0 -52
- data/models/connections/channel_node/accounting.rb +0 -25
- data/models/connections/channel_node/fee.rb +0 -80
- data/models/connections/channel_node/htlc/blocks/delta.rb +0 -39
- data/models/connections/channel_node/htlc.rb +0 -86
- data/models/connections/channel_node/policy.rb +0 -39
- data/models/connections/channel_node.rb +0 -84
- data/models/connections/forward_channel.rb +0 -29
- data/models/connections/payment_channel.rb +0 -56
- data/models/edges/channel/accounting.rb +0 -85
- data/models/edges/channel/hop.rb +0 -65
- data/models/edges/channel.rb +0 -195
- data/models/edges/forward.rb +0 -49
- data/models/edges/groups/channel_forwards/analysis.rb +0 -69
- data/models/edges/groups/channel_forwards.rb +0 -37
- data/models/edges/payment.rb +0 -102
- data/models/invoice.rb +0 -113
- data/models/nodes/node/lightning.rb +0 -31
- data/models/nodes/node/platform.rb +0 -48
- data/models/nodes/node.rb +0 -143
- data/models/rate.rb +0 -35
- data/models/secret.rb +0 -55
- data/models/transaction.rb +0 -39
- data/models/wallet.rb +0 -40
@@ -1,108 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'securerandom'
|
4
|
-
require 'digest'
|
5
|
-
|
6
|
-
require_relative '../../../ports/grpc'
|
7
|
-
require_relative '../../../models/errors'
|
8
|
-
require_relative '../../../models/edges/payment'
|
9
|
-
require_relative '../../../adapters/edges/payment'
|
10
|
-
require_relative '../../node/myself'
|
11
|
-
require_relative '../../invoice/decode'
|
12
|
-
|
13
|
-
module Lighstorm
|
14
|
-
module Controllers
|
15
|
-
module Payment
|
16
|
-
module Pay
|
17
|
-
def self.call(components, grpc_request)
|
18
|
-
result = []
|
19
|
-
components[:grpc].send(grpc_request[:service]).send(
|
20
|
-
grpc_request[:method], grpc_request[:params]
|
21
|
-
) do |response|
|
22
|
-
result << response.to_h
|
23
|
-
end
|
24
|
-
{ response: result, exception: nil }
|
25
|
-
rescue StandardError => e
|
26
|
-
{ exception: e }
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.dispatch(components, grpc_request, &vcr)
|
30
|
-
if vcr.nil?
|
31
|
-
call(components, grpc_request)
|
32
|
-
else
|
33
|
-
vcr.call(-> { call(components, grpc_request) }, :dispatch)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.fetch_all(components, code)
|
38
|
-
{
|
39
|
-
invoice_decode: code.nil? ? nil : Invoice::Decode.data(components, code),
|
40
|
-
node_myself: Node::Myself.data(components)
|
41
|
-
}
|
42
|
-
end
|
43
|
-
|
44
|
-
def self.fetch(components, code = nil, &vcr)
|
45
|
-
raw = vcr.nil? ? fetch_all(components, code) : vcr.call(-> { fetch_all(components, code) })
|
46
|
-
end
|
47
|
-
|
48
|
-
def self.adapt(grpc_data, fetch_data)
|
49
|
-
Adapter::Payment.send_payment_v2(
|
50
|
-
grpc_data[:response].last,
|
51
|
-
fetch_data[:node_myself],
|
52
|
-
fetch_data[:invoice_decode]
|
53
|
-
)
|
54
|
-
end
|
55
|
-
|
56
|
-
def self.model(data, components)
|
57
|
-
Models::Payment.new(data, components)
|
58
|
-
end
|
59
|
-
|
60
|
-
def self.raise_error_if_exists!(request, response)
|
61
|
-
return if response[:exception].nil?
|
62
|
-
|
63
|
-
if response[:exception].is_a?(GRPC::AlreadyExists)
|
64
|
-
raise AlreadyPaidError.new(
|
65
|
-
'The invoice is already paid.',
|
66
|
-
request: request, grpc: response[:exception]
|
67
|
-
)
|
68
|
-
end
|
69
|
-
|
70
|
-
if response[:exception].message =~ /amount must not be specified when paying a non-zero/
|
71
|
-
raise AmountForNonZeroError.new(
|
72
|
-
'Millisatoshis must not be specified when paying a non-zero amount invoice.',
|
73
|
-
request: request, grpc: response[:exception]
|
74
|
-
)
|
75
|
-
end
|
76
|
-
|
77
|
-
if response[:exception].message =~ /amount must be specified when paying a zero amount/
|
78
|
-
raise MissingMillisatoshisError.new(
|
79
|
-
'Millisatoshis must be specified when paying a zero amount invoice.',
|
80
|
-
request: request, grpc: response[:exception]
|
81
|
-
)
|
82
|
-
end
|
83
|
-
|
84
|
-
raise PaymentError.new(
|
85
|
-
response[:exception].message,
|
86
|
-
request: request, grpc: response[:exception]
|
87
|
-
)
|
88
|
-
end
|
89
|
-
|
90
|
-
def self.raise_failure_if_exists!(model, request, response)
|
91
|
-
return unless model.state == 'failed'
|
92
|
-
|
93
|
-
if response[:response].last[:failure_reason] == :FAILURE_REASON_NO_ROUTE
|
94
|
-
raise NoRouteFoundError.new(
|
95
|
-
response[:response].last[:failure_reason],
|
96
|
-
request: request, response: response[:response], result: model
|
97
|
-
)
|
98
|
-
else
|
99
|
-
raise PaymentError.new(
|
100
|
-
response[:response].last[:failure_reason],
|
101
|
-
request: request, response: response[:response], result: model
|
102
|
-
)
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
data/controllers/payment/all.rb
DELETED
@@ -1,437 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../../ports/grpc'
|
4
|
-
require_relative '../../adapters/nodes/node'
|
5
|
-
require_relative '../../adapters/edges/payment'
|
6
|
-
require_relative '../../adapters/invoice'
|
7
|
-
require_relative '../../adapters/edges/payment/purpose'
|
8
|
-
|
9
|
-
require_relative '../../models/edges/payment'
|
10
|
-
|
11
|
-
module Lighstorm
|
12
|
-
module Controllers
|
13
|
-
module Payment
|
14
|
-
module All
|
15
|
-
def self.fetch(components, purpose: nil, invoice_code: nil, secret_hash: nil, limit: nil, fetch: {})
|
16
|
-
at = Time.now
|
17
|
-
|
18
|
-
grpc = components[:grpc].session
|
19
|
-
|
20
|
-
get_info = grpc.lightning.get_info.to_h
|
21
|
-
|
22
|
-
last_offset = 0
|
23
|
-
|
24
|
-
payments = []
|
25
|
-
|
26
|
-
loop do
|
27
|
-
response = grpc.lightning.list_payments(
|
28
|
-
index_offset: last_offset
|
29
|
-
)
|
30
|
-
|
31
|
-
response.payments.each do |payment|
|
32
|
-
payment = payment.to_h
|
33
|
-
|
34
|
-
next unless invoice_code.nil? || payment[:payment_request] == invoice_code
|
35
|
-
|
36
|
-
next unless secret_hash.nil? || payment[:payment_hash] == secret_hash
|
37
|
-
|
38
|
-
payment_purpose = Adapter::Purpose.list_payments(payment, get_info)
|
39
|
-
|
40
|
-
case purpose
|
41
|
-
when 'self-payment', 'self'
|
42
|
-
payments << payment if payment_purpose == 'self-payment'
|
43
|
-
when 'peer-to-peer', 'p2p'
|
44
|
-
payments << payment if payment_purpose == 'peer-to-peer'
|
45
|
-
when '!peer-to-peer', '!p2p'
|
46
|
-
payments << payment unless payment_purpose == 'peer-to-peer'
|
47
|
-
when 'rebalance'
|
48
|
-
payments << payment if payment_purpose == 'rebalance'
|
49
|
-
when '!rebalance'
|
50
|
-
payments << payment unless payment_purpose == 'rebalance'
|
51
|
-
when '!payment'
|
52
|
-
payments << payment unless payment_purpose == 'payment'
|
53
|
-
when 'payment'
|
54
|
-
payments << payment if payment_purpose == 'payment'
|
55
|
-
else
|
56
|
-
payments << payment
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
# TODO: How to optimize this?
|
61
|
-
# break if !limit.nil? && payments.size >= limit
|
62
|
-
|
63
|
-
break if last_offset == response.last_index_offset || last_offset > response.last_index_offset
|
64
|
-
|
65
|
-
last_offset = response.last_index_offset
|
66
|
-
end
|
67
|
-
|
68
|
-
payments = payments.sort_by { |raw_payment| -raw_payment[:creation_time_ns] }
|
69
|
-
|
70
|
-
payments = payments[0..limit - 1] unless limit.nil?
|
71
|
-
|
72
|
-
data = {
|
73
|
-
at: at,
|
74
|
-
get_info: get_info,
|
75
|
-
fee_report: grpc.lightning.fee_report.to_h,
|
76
|
-
list_payments: payments,
|
77
|
-
list_channels: {},
|
78
|
-
get_chan_info: {},
|
79
|
-
get_node_info: {},
|
80
|
-
lookup_invoice: {},
|
81
|
-
decode_pay_req: {}
|
82
|
-
}
|
83
|
-
|
84
|
-
payments.each do |payment|
|
85
|
-
unless fetch[:decode_pay_req] == false || payment[:payment_request] == '' || data[:decode_pay_req][payment[:payment_request]]
|
86
|
-
data[:decode_pay_req][payment[:payment_request]] = grpc.lightning.decode_pay_req(
|
87
|
-
pay_req: payment[:payment_request]
|
88
|
-
).to_h
|
89
|
-
end
|
90
|
-
|
91
|
-
unless fetch[:lookup_invoice] == false || data[:lookup_invoice][payment[:payment_hash]]
|
92
|
-
begin
|
93
|
-
data[:lookup_invoice][payment[:payment_hash]] = grpc.lightning.lookup_invoice(
|
94
|
-
r_hash_str: payment[:payment_hash]
|
95
|
-
).to_h
|
96
|
-
rescue StandardError => e
|
97
|
-
data[:lookup_invoice][payment[:payment_hash]] = { _error: e }
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
payment[:htlcs].each do |htlc|
|
102
|
-
htlc[:route][:hops].each do |hop|
|
103
|
-
unless fetch[:get_chan_info] == false || data[:get_chan_info][hop[:chan_id]]
|
104
|
-
begin
|
105
|
-
data[:get_chan_info][hop[:chan_id]] = grpc.lightning.get_chan_info(
|
106
|
-
chan_id: hop[:chan_id]
|
107
|
-
).to_h
|
108
|
-
rescue GRPC::Unknown => e
|
109
|
-
data[:get_chan_info][hop[:chan_id]] = { _error: e }
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
next if fetch[:get_node_info] == false || data[:get_node_info][hop[:pub_key]]
|
114
|
-
|
115
|
-
begin
|
116
|
-
data[:get_node_info][hop[:pub_key]] = grpc.lightning.get_node_info(
|
117
|
-
pub_key: hop[:pub_key]
|
118
|
-
).to_h
|
119
|
-
rescue StandardError => e
|
120
|
-
data[:get_node_info][hop[:pub_key]] = { _error: e }
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
if fetch[:lookup_invoice] != false
|
127
|
-
data[:lookup_invoice].each_value do |invoice|
|
128
|
-
if fetch[:decode_pay_req] == false || invoice[:_error] || invoice[:payment_request] == '' || data[:decode_pay_req][invoice[:payment_request]]
|
129
|
-
next
|
130
|
-
end
|
131
|
-
|
132
|
-
data[:decode_pay_req][invoice[:payment_request]] = grpc.lightning.decode_pay_req(
|
133
|
-
pay_req: invoice[:payment_request]
|
134
|
-
).to_h
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
list_channels_done = {}
|
139
|
-
|
140
|
-
data[:get_chan_info].each_value do |channel|
|
141
|
-
next if channel[:_error]
|
142
|
-
|
143
|
-
partners = [channel[:node1_pub], channel[:node2_pub]]
|
144
|
-
|
145
|
-
is_mine = partners.include?(data[:get_info][:identity_pubkey])
|
146
|
-
|
147
|
-
if is_mine
|
148
|
-
partner = partners.find { |p| p != data[:get_info][:identity_pubkey] }
|
149
|
-
|
150
|
-
unless list_channels_done[partner]
|
151
|
-
grpc.lightning.list_channels(
|
152
|
-
peer: [partner].pack('H*')
|
153
|
-
).channels.map(&:to_h).each do |list_channels|
|
154
|
-
data[:list_channels][list_channels[:chan_id]] = list_channels
|
155
|
-
end
|
156
|
-
|
157
|
-
list_channels_done[partner] = true
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
unless fetch[:get_node_info] == false || data[:get_node_info][channel[:node1_pub]]
|
162
|
-
begin
|
163
|
-
data[:get_node_info][channel[:node1_pub]] = grpc.lightning.get_node_info(
|
164
|
-
pub_key: channel[:node1_pub]
|
165
|
-
).to_h
|
166
|
-
rescue StandardError => e
|
167
|
-
data[:get_node_info][channel[:node1_pub]] = { _error: e }
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
next if fetch[:get_node_info] == false || data[:get_node_info][channel[:node2_pub]]
|
172
|
-
|
173
|
-
begin
|
174
|
-
data[:get_node_info][channel[:node2_pub]] = grpc.lightning.get_node_info(
|
175
|
-
pub_key: channel[:node2_pub]
|
176
|
-
).to_h
|
177
|
-
rescue StandardError => e
|
178
|
-
data[:get_node_info][channel[:node2_pub]] = { _error: e }
|
179
|
-
end
|
180
|
-
end
|
181
|
-
|
182
|
-
data[:list_channels].each_value do |channel|
|
183
|
-
next if fetch[:get_node_info] == false || data[:get_node_info][channel[:remote_pubkey]]
|
184
|
-
|
185
|
-
begin
|
186
|
-
data[:get_node_info][channel[:remote_pubkey]] = grpc.lightning.get_node_info(
|
187
|
-
pub_key: channel[:remote_pubkey]
|
188
|
-
).to_h
|
189
|
-
rescue StandardError => e
|
190
|
-
data[:get_node_info][channel[:remote_pubkey]] = { _error: e }
|
191
|
-
end
|
192
|
-
end
|
193
|
-
|
194
|
-
data[:@meta] = { calls: grpc.calls }
|
195
|
-
|
196
|
-
data
|
197
|
-
end
|
198
|
-
|
199
|
-
def self.adapt(raw)
|
200
|
-
adapted = {
|
201
|
-
get_info: Lighstorm::Adapter::Node.get_info(raw[:get_info]),
|
202
|
-
list_payments: raw[:list_payments].map do |raw_payment|
|
203
|
-
Lighstorm::Adapter::Payment.list_payments(raw_payment, raw[:get_info])
|
204
|
-
end,
|
205
|
-
fee_report: raw[:fee_report][:channel_fees].map do |raw_fee|
|
206
|
-
Lighstorm::Adapter::Fee.fee_report(raw_fee.to_h)
|
207
|
-
end,
|
208
|
-
lookup_invoice: {},
|
209
|
-
list_channels: {},
|
210
|
-
get_chan_info: {},
|
211
|
-
get_node_info: {},
|
212
|
-
decode_pay_req: {}
|
213
|
-
}
|
214
|
-
|
215
|
-
raw[:decode_pay_req].each_key do |key|
|
216
|
-
next if raw[:decode_pay_req][key][:_error]
|
217
|
-
|
218
|
-
adapted[:decode_pay_req][key] = Lighstorm::Adapter::Invoice.decode_pay_req(
|
219
|
-
raw[:decode_pay_req][key]
|
220
|
-
)
|
221
|
-
end
|
222
|
-
|
223
|
-
raw[:lookup_invoice].each_key do |key|
|
224
|
-
next if raw[:lookup_invoice][key][:_error]
|
225
|
-
|
226
|
-
adapted[:lookup_invoice][key] = Lighstorm::Adapter::Invoice.lookup_invoice(
|
227
|
-
raw[:lookup_invoice][key], raw[:at]
|
228
|
-
)
|
229
|
-
end
|
230
|
-
|
231
|
-
raw[:get_chan_info].each_key do |key|
|
232
|
-
next if raw[:get_chan_info][key][:_error]
|
233
|
-
|
234
|
-
adapted[:get_chan_info][key] = Lighstorm::Adapter::Channel.get_chan_info(
|
235
|
-
raw[:get_chan_info][key]
|
236
|
-
)
|
237
|
-
end
|
238
|
-
|
239
|
-
raw[:list_channels].each_key do |key|
|
240
|
-
adapted[:list_channels][key] = Lighstorm::Adapter::Channel.list_channels(
|
241
|
-
raw[:list_channels][key], raw[:at]
|
242
|
-
)
|
243
|
-
end
|
244
|
-
|
245
|
-
raw[:get_node_info].each_key do |key|
|
246
|
-
next if raw[:get_node_info][key][:_error]
|
247
|
-
|
248
|
-
adapted[:get_node_info][key] = Lighstorm::Adapter::Node.get_node_info(
|
249
|
-
raw[:get_node_info][key]
|
250
|
-
)
|
251
|
-
end
|
252
|
-
|
253
|
-
adapted
|
254
|
-
end
|
255
|
-
|
256
|
-
def self.transform_channel(data, adapted)
|
257
|
-
if adapted[:get_chan_info][data[:id].to_i]
|
258
|
-
target = data[:target]
|
259
|
-
data = adapted[:get_chan_info][data[:id].to_i]
|
260
|
-
data[:target] = target
|
261
|
-
data[:known] = true
|
262
|
-
else
|
263
|
-
data[:_key] = Digest::SHA256.hexdigest(data[:id])
|
264
|
-
end
|
265
|
-
|
266
|
-
data[:mine] = true if data[:partners].size == 1
|
267
|
-
|
268
|
-
[0, 1].each do |i|
|
269
|
-
next unless data[:partners] && data[:partners][i]
|
270
|
-
|
271
|
-
if data[:partners][i][:node][:public_key] == adapted[:get_info][:public_key]
|
272
|
-
data[:partners][i][:node] = adapted[:get_info]
|
273
|
-
end
|
274
|
-
|
275
|
-
data[:partners].each do |partner|
|
276
|
-
if data[:partners][i][:node][:public_key] == partner[:node][:public_key]
|
277
|
-
data[:partners][i][:policy] = partner[:policy]
|
278
|
-
end
|
279
|
-
end
|
280
|
-
|
281
|
-
if data[:partners][i][:node][:public_key] == adapted[:get_info][:public_key]
|
282
|
-
data[:partners][i][:node][:platform] = adapted[:get_info][:platform]
|
283
|
-
data[:partners][i][:node][:myself] = true
|
284
|
-
data[:mine] = true
|
285
|
-
adapted[:fee_report].each do |channel|
|
286
|
-
next unless data[:id] == channel[:id]
|
287
|
-
|
288
|
-
if data[:partners][i][:policy].nil?
|
289
|
-
data[:partners][i][:policy] = channel[:partner][:policy]
|
290
|
-
else
|
291
|
-
data[:partners][i][:policy][:fee] = channel[:partner][:policy][:fee]
|
292
|
-
end
|
293
|
-
break
|
294
|
-
end
|
295
|
-
else
|
296
|
-
if adapted[:get_node_info][data[:partners][i][:node][:public_key]]
|
297
|
-
data[:partners][i][:node] = adapted[:get_node_info][data[:partners][i][:node][:public_key]]
|
298
|
-
end
|
299
|
-
data[:partners][i][:node][:platform] = {
|
300
|
-
blockchain: adapted[:get_info][:platform][:blockchain],
|
301
|
-
network: adapted[:get_info][:platform][:network]
|
302
|
-
}
|
303
|
-
|
304
|
-
data[:partners][i][:node][:myself] = false
|
305
|
-
end
|
306
|
-
end
|
307
|
-
|
308
|
-
channel = adapted[:list_channels][data[:id].to_i]
|
309
|
-
|
310
|
-
return data unless channel
|
311
|
-
|
312
|
-
channel.each_key do |key|
|
313
|
-
next if data.key?(key)
|
314
|
-
|
315
|
-
data[key] = channel[key]
|
316
|
-
end
|
317
|
-
|
318
|
-
data[:accounting] = channel[:accounting]
|
319
|
-
|
320
|
-
channel[:partners].each do |partner|
|
321
|
-
data[:partners].each_index do |i|
|
322
|
-
partner.each_key do |key|
|
323
|
-
next if data[:partners][i].key?(key)
|
324
|
-
|
325
|
-
data[:partners][i][key] = partner[key]
|
326
|
-
end
|
327
|
-
end
|
328
|
-
end
|
329
|
-
|
330
|
-
data
|
331
|
-
end
|
332
|
-
|
333
|
-
def self.transform(list_payments, adapted)
|
334
|
-
if adapted[:lookup_invoice][list_payments[:secret][:hash]] &&
|
335
|
-
!adapted[:lookup_invoice][list_payments[:secret][:hash]][:_error]
|
336
|
-
|
337
|
-
if list_payments[:invoice]
|
338
|
-
lookup = adapted[:lookup_invoice][list_payments[:secret][:hash]]
|
339
|
-
|
340
|
-
list_payments[:invoice][:description] = lookup[:description]
|
341
|
-
|
342
|
-
lookup.each_key do |key|
|
343
|
-
if lookup[key].is_a?(Hash)
|
344
|
-
unless list_payments[:invoice].key?(key) && !list_payments[:invoice][key].nil?
|
345
|
-
list_payments[:invoice][key] = lookup[:key]
|
346
|
-
end
|
347
|
-
|
348
|
-
next
|
349
|
-
end
|
350
|
-
|
351
|
-
unless list_payments[:invoice].key?(key) && !list_payments[:invoice][key].nil? &&
|
352
|
-
(!list_payments[:invoice][key].is_a?(String) || !list_payments[:invoice][key].empty?)
|
353
|
-
list_payments[:invoice][key] = lookup[key]
|
354
|
-
end
|
355
|
-
end
|
356
|
-
else
|
357
|
-
list_payments[:invoice] = adapted[:lookup_invoice][list_payments[:secret][:hash]]
|
358
|
-
end
|
359
|
-
end
|
360
|
-
|
361
|
-
list_payments[:hops].each do |hop|
|
362
|
-
hop[:channel] = transform_channel(hop[:channel], adapted)
|
363
|
-
end
|
364
|
-
|
365
|
-
if adapted[:decode_pay_req][list_payments[:invoice][:code]]
|
366
|
-
decoded = adapted[:decode_pay_req][list_payments[:invoice][:code]]
|
367
|
-
invoice = list_payments[:invoice]
|
368
|
-
|
369
|
-
decoded.each_key do |key|
|
370
|
-
if !decoded[key].is_a?(Hash)
|
371
|
-
invoice[key] = decoded[key]
|
372
|
-
elsif decoded[key].is_a?(Hash)
|
373
|
-
invoice[key] = {} unless invoice.key?(key)
|
374
|
-
|
375
|
-
next if key == :secret
|
376
|
-
|
377
|
-
decoded[key].each_key do |sub_key|
|
378
|
-
next if decoded[key][sub_key].nil? ||
|
379
|
-
(decoded[key][sub_key].is_a?(String) && decoded[key][sub_key].empty?)
|
380
|
-
|
381
|
-
invoice[key][sub_key] = decoded[key][sub_key]
|
382
|
-
end
|
383
|
-
end
|
384
|
-
end
|
385
|
-
end
|
386
|
-
|
387
|
-
if list_payments[:invoice][:code]
|
388
|
-
if list_payments[:invoice][:payable] == 'once'
|
389
|
-
list_payments[:through] = 'non-amp'
|
390
|
-
elsif list_payments[:invoice][:payable] == 'indefinitely'
|
391
|
-
list_payments[:through] = 'amp'
|
392
|
-
end
|
393
|
-
end
|
394
|
-
|
395
|
-
list_payments
|
396
|
-
end
|
397
|
-
|
398
|
-
def self.data(
|
399
|
-
components,
|
400
|
-
purpose: nil, invoice_code: nil, secret_hash: nil, limit: nil,
|
401
|
-
fetch: {}, &vcr
|
402
|
-
)
|
403
|
-
raw = if vcr.nil?
|
404
|
-
self.fetch(
|
405
|
-
components,
|
406
|
-
purpose: purpose, invoice_code: invoice_code, secret_hash: secret_hash,
|
407
|
-
limit: limit, fetch: fetch
|
408
|
-
)
|
409
|
-
else
|
410
|
-
vcr.call(lambda {
|
411
|
-
self.fetch(
|
412
|
-
components,
|
413
|
-
purpose: purpose, invoice_code: invoice_code, secret_hash: secret_hash,
|
414
|
-
limit: limit, fetch: fetch
|
415
|
-
)
|
416
|
-
})
|
417
|
-
end
|
418
|
-
|
419
|
-
adapted = adapt(raw)
|
420
|
-
|
421
|
-
{
|
422
|
-
data: adapted[:list_payments].map do |data|
|
423
|
-
transform(data, adapted)
|
424
|
-
end,
|
425
|
-
meta: raw[:@meta]
|
426
|
-
}
|
427
|
-
end
|
428
|
-
|
429
|
-
def self.model(data, components)
|
430
|
-
data[:data].map do |node_data|
|
431
|
-
Lighstorm::Models::Payment.new(node_data, components)
|
432
|
-
end
|
433
|
-
end
|
434
|
-
end
|
435
|
-
end
|
436
|
-
end
|
437
|
-
end
|
data/controllers/payment.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative './concerns/impersonatable'
|
4
|
-
|
5
|
-
require_relative './payment/all'
|
6
|
-
|
7
|
-
module Lighstorm
|
8
|
-
module Controllers
|
9
|
-
module Payment
|
10
|
-
extend Impersonatable
|
11
|
-
|
12
|
-
class DSL < Impersonatable::DSL
|
13
|
-
def all(purpose: nil, limit: nil, fetch: {})
|
14
|
-
All.model(All.data(components, purpose: purpose, limit: limit, fetch: fetch), components)
|
15
|
-
end
|
16
|
-
|
17
|
-
def first(purpose: nil, fetch: {})
|
18
|
-
All.model(All.data(components, purpose: purpose, fetch: fetch), components).first
|
19
|
-
end
|
20
|
-
|
21
|
-
def last(purpose: nil, fetch: {})
|
22
|
-
All.model(All.data(components, purpose: purpose, fetch: fetch), components).last
|
23
|
-
end
|
24
|
-
|
25
|
-
def find_by_secret_hash(secret_hash, &vcr)
|
26
|
-
All.model(All.data(components, secret_hash: secret_hash, &vcr), components).first
|
27
|
-
end
|
28
|
-
|
29
|
-
def find_by_invoice_code(invoice_code, &vcr)
|
30
|
-
All.model(All.data(components, invoice_code: invoice_code, &vcr), components).first
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,58 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../../ports/grpc'
|
4
|
-
require_relative '../../adapters/invoice'
|
5
|
-
require_relative '../../models/invoice'
|
6
|
-
|
7
|
-
module Lighstorm
|
8
|
-
module Controllers
|
9
|
-
module Secret
|
10
|
-
module ValidProof
|
11
|
-
def self.fetch(components, invoice_main_secret_hash)
|
12
|
-
{ response: {
|
13
|
-
at: Time.now,
|
14
|
-
lookup_invoice: components[:grpc].lightning.lookup_invoice(r_hash_str: invoice_main_secret_hash).to_h
|
15
|
-
}, exception: nil }
|
16
|
-
rescue StandardError => e
|
17
|
-
{ exception: e }
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.adapt(raw)
|
21
|
-
{
|
22
|
-
lookup_invoice: Lighstorm::Adapter::Invoice.lookup_invoice(
|
23
|
-
raw[:lookup_invoice],
|
24
|
-
raw[:at]
|
25
|
-
)
|
26
|
-
}
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.transform(adapted, proof)
|
30
|
-
return true if adapted[:lookup_invoice][:secret][:preimage] == proof
|
31
|
-
|
32
|
-
return false if adapted[:lookup_invoice][:payments].nil? ||
|
33
|
-
adapted[:lookup_invoice][:payments].empty?
|
34
|
-
|
35
|
-
!adapted[:lookup_invoice][:payments].find do |payment|
|
36
|
-
next if payment[:secret].nil?
|
37
|
-
|
38
|
-
payment[:secret][:preimage] == proof
|
39
|
-
end.nil?
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.data(components, invoice_main_secret_hash, proof, &vcr)
|
43
|
-
raise 'Invalid proof' if proof.size != 64
|
44
|
-
|
45
|
-
raw = if vcr.nil?
|
46
|
-
fetch(components, invoice_main_secret_hash)
|
47
|
-
else
|
48
|
-
vcr.call(-> { fetch(components, invoice_main_secret_hash) })
|
49
|
-
end
|
50
|
-
|
51
|
-
adapted = adapt(raw[:response])
|
52
|
-
|
53
|
-
transform(adapted, proof)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
@@ -1,61 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative '../../ports/grpc'
|
4
|
-
require_relative '../../adapters/transaction'
|
5
|
-
require_relative '../../models/transaction'
|
6
|
-
|
7
|
-
module Lighstorm
|
8
|
-
module Controllers
|
9
|
-
module Transaction
|
10
|
-
module All
|
11
|
-
def self.fetch(components, limit: nil)
|
12
|
-
at = Time.now
|
13
|
-
|
14
|
-
transactions = []
|
15
|
-
|
16
|
-
response = components[:grpc].lightning.get_transactions
|
17
|
-
|
18
|
-
response.transactions.each do |transaction|
|
19
|
-
transactions << transaction.to_h
|
20
|
-
end
|
21
|
-
|
22
|
-
transactions = transactions.sort_by { |raw_transaction| -raw_transaction[:time_stamp] }
|
23
|
-
|
24
|
-
transactions = transactions[0..limit - 1] unless limit.nil?
|
25
|
-
|
26
|
-
{ at: at, get_transactions: transactions }
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.adapt(raw)
|
30
|
-
{
|
31
|
-
get_transactions: raw[:get_transactions].map do |raw_transaction|
|
32
|
-
Lighstorm::Adapter::Transaction.get_transactions(raw_transaction)
|
33
|
-
end
|
34
|
-
}
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.transform(adapted)
|
38
|
-
adapted[:get_transactions]
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.data(components, limit: nil, &vcr)
|
42
|
-
raw = if vcr.nil?
|
43
|
-
fetch(components, limit: limit)
|
44
|
-
else
|
45
|
-
vcr.call(-> { fetch(components, limit: limit) })
|
46
|
-
end
|
47
|
-
|
48
|
-
adapted = adapt(raw)
|
49
|
-
|
50
|
-
transform(adapted)
|
51
|
-
end
|
52
|
-
|
53
|
-
def self.model(data)
|
54
|
-
data.map do |transaction_data|
|
55
|
-
Lighstorm::Models::Transaction.new(transaction_data)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|