lightning_network 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c59dcb13b12d5c6eae7342d92cdc586fadbd0e8
4
+ data.tar.gz: f9c5f2df1f42937a009f27c54acc248e97ab1c3e
5
+ SHA512:
6
+ metadata.gz: 354a03a176026727c366163152763e25d27d5712f4c69db0afa27cb4e31f12c0df5cde7dee92be767a27c6c6171829935b4ce815d97e1b9d053ffa387e076f6e
7
+ data.tar.gz: '08032511e41921d357214ce02ed7903a0b74008b57061cf8969da36dac3257286da85029db2c81d45b90717c04f4033387ca1c89566d04349c9a5d0587621fea'
@@ -0,0 +1,9 @@
1
+
2
+ ENV['GRPC_SSL_CIPHER_SUITES'] = "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256"
3
+
4
+ require 'rpc_pb'
5
+ require 'rpc_services_pb'
6
+ require 'grpc'
7
+ require 'macaroon_interceptor'
8
+ require 'lnd'
9
+ require 'node'
data/lib/lnd.rb ADDED
@@ -0,0 +1,49 @@
1
+
2
+
3
+ module Lnd
4
+ private
5
+
6
+ def protect_call(failed_value, &block)
7
+ begin
8
+ block.call
9
+ rescue => e
10
+ failed_value
11
+ end
12
+ end
13
+
14
+ def certificate
15
+ # TODO: READ CERTIFICATE FROM ENV
16
+ File.read(File.expand_path(ENV['lnd_cert']))
17
+ end
18
+
19
+ def credentials
20
+ GRPC::Core::ChannelCredentials.new(certificate)
21
+ end
22
+
23
+ def macaroon_binary
24
+ # TODO: READ MACA FROM ENV
25
+ File.read(File.expand_path(ENV['lnd_macaroon']))
26
+ end
27
+
28
+ def macaroon
29
+ macaroon_binary.each_byte.map { |b| b.to_s(16).rjust(2,'0') }.join
30
+ end
31
+
32
+ def stub
33
+ # TODO: READ THE NODE URL FROM ENV
34
+ Lnrpc::Lightning::Stub.new(ENV['lnd_host'],
35
+ credentials,
36
+ interceptors: [MacaroonInterceptor.new(macaroon)])
37
+ end
38
+
39
+
40
+ def bin_to_hex(s)
41
+ ap s
42
+ s.each_byte.map { |b| b.to_s(16) }.join
43
+ end
44
+
45
+
46
+
47
+ end
48
+
49
+
@@ -0,0 +1,15 @@
1
+ class MacaroonInterceptor < GRPC::ClientInterceptor
2
+ attr_reader :macaroon
3
+
4
+ def initialize(macaroon)
5
+ @macaroon = macaroon
6
+ super
7
+ end
8
+
9
+ def request_response(request:, call:, method:, metadata:)
10
+ metadata['macaroon'] = macaroon
11
+ yield
12
+ end
13
+ end
14
+
15
+
data/lib/node.rb ADDED
@@ -0,0 +1,202 @@
1
+ class Node
2
+
3
+
4
+ class Payments
5
+ include Lnd
6
+
7
+ def list_invoices
8
+ response = stub.list_invoices(Lnrpc::ListInvoiceRequest.new())
9
+ response.invoices
10
+ end
11
+
12
+ def list_payments
13
+ response = stub.list_payments(Lnrpc::ListPaymentRequest.new())
14
+ response.payments
15
+ end
16
+
17
+
18
+
19
+ def decode_pay_req(pay_req)
20
+ stub.decode_pay_req(Lnrpc::PayReqString.new(pay_req: pay_req))
21
+ end
22
+
23
+ def lookup_invoice(payment_hash)
24
+ stub.lookup_invoice(Lnrpc::PaymentHash.new(payment_hash))
25
+ end
26
+
27
+
28
+
29
+ def add_invoice(value, memo="")
30
+ stub.add_invoice(Lnrpc::Invoice.new(value: value, memo: memo))
31
+ end
32
+
33
+ def send_payment(payment_request)
34
+ r = stub.send_payment_sync(Lnrpc::SendRequest.new(payment_request: payment_request))
35
+ end
36
+ end
37
+
38
+ class Channels
39
+
40
+ include Lnd
41
+
42
+
43
+ def open_channel(addr, amt)
44
+
45
+ r = addr.split("@")
46
+ pubkey = r[0]
47
+ host = r[1]
48
+
49
+
50
+ hex = bin_to_hex(pubkey)
51
+
52
+ request = Lnrpc::OpenChannelRequest.new(node_pubkey_string: pubkey,
53
+ local_funding_amount: amt,
54
+ push_sat: (amt / 100))
55
+
56
+ r = stub.open_channel_sync(request)
57
+ end
58
+
59
+ def close_channel(channel_point)
60
+
61
+ cp = Lnrpc::ChannelPoint.new(funding_txid_str: channel_point)
62
+
63
+ stub.close_channel(Lnrpc::CloseChannelRequest.new(channel_point: cp,
64
+ force: true,
65
+ target_conf: 6,
66
+ sat_per_byte: 150000))
67
+ end
68
+
69
+
70
+ def close_channels
71
+ Node::Payments.new.list_channels["channels"].map do |r|
72
+ puts "Closing #{r["channel_point"]}"
73
+ Node::Payments.new.close_channel( r["channel_point"] )
74
+ end
75
+ end
76
+
77
+ def channel_balance
78
+ stub.channel_balance(Lnrpc::ChannelBalanceRequest.new())
79
+ end
80
+
81
+ def pending_channels
82
+ stub.pending_channels(Lnrpc::PendingChannelsRequest.new())
83
+ end
84
+
85
+ def list_channels
86
+ stub.list_channels(Lnrpc::ListChannelsRequest.new())
87
+ end
88
+
89
+ def get_chain_info(chain_id)
90
+ # TODO: ERROR OUT
91
+ stub.get_chain_info(Lnrpc::ChainInfoRequest.new(chain_id: chain_id))
92
+ end
93
+
94
+ def get_network_info
95
+ stub.get_network_info(Lnrpc::FeeReportRequest.new())
96
+ end
97
+
98
+ def fee_report
99
+ stub.fee_report(Lnrpc::FeeReportRequest.new())
100
+ end
101
+
102
+ def update_chan_policy
103
+ # TODO: Raise won't do
104
+ raise "won't do :) "
105
+ end
106
+ end
107
+
108
+
109
+ include Lnd
110
+
111
+ class OnChain
112
+ include Lnd
113
+ def list_chain_txns
114
+ stub.get_transactions(Lnrpc::GetTransactionsRequest.new())
115
+ end
116
+
117
+ def send_coins(addr, amt)
118
+ stub.send_coins(Lnrpc::SendCoinsRequest.new(addr: addr, amount: amt))
119
+ end
120
+
121
+ def send_many(array_addrs)
122
+ stub.send_many(Lnrpc::SendManyRequest.new(AddrToAmount: array_addrs, target_conf: 6, sat_per_byte: 200))
123
+ end
124
+
125
+ end
126
+
127
+ class Peers
128
+ include Lnd
129
+
130
+ def connect(addr)
131
+ # TODO: Create utility function to separate pub_key and host from full adress
132
+ r = addr.split("@")
133
+ pubkey = r[0]
134
+ host = r[1]
135
+
136
+ address = Lnrpc::LightningAddress.new(pubkey: pubkey, host: host)
137
+
138
+ stub.connect_peer(Lnrpc::ConnectPeerRequest.new(addr: address, perm: false))
139
+ end
140
+
141
+ def disconnect(pub_key)
142
+ stub.disconnect_peer(Lnrpc::DisconnectPeerRequest.new(pub_key: pub_key))
143
+ end
144
+
145
+ def list_peers
146
+ stub.list_peers(Lnrpc::ListPeersRequest.new())
147
+ end
148
+
149
+ def describe_graph
150
+ stub.describe_graph(Lnrpc::ChannelGraphRequest.new())
151
+ end
152
+
153
+ def get_node_info(pub_key)
154
+ stub.get_node_info(Lnrpc::NodeInfoRequest.new(pub_key: pub_key))
155
+ end
156
+ end
157
+
158
+ class Wallet
159
+ include Lnd
160
+
161
+ def new_address
162
+ stub.new_witness_address(Lnrpc::NewWitnessAddressRequest.new())
163
+ end
164
+
165
+ def wallet_balance
166
+ stub.wallet_balance(Lnrpc::WalletBalanceRequest.new())
167
+ end
168
+
169
+ # TODO: Double check this
170
+ def sign_message(msg)
171
+ stub.sign_message(Lnrpc::SignMessageRequest.new(msg: msg))
172
+ end
173
+
174
+ def verify_message(msg, signature)
175
+ stub.verify_message(Lnrpc::VerifyMessageRequest.new(msg: msg, signature: signature))
176
+ end
177
+
178
+ end
179
+
180
+ def get_info
181
+ stub.get_info(Lnrpc::GetInfoRequest.new())
182
+ end
183
+
184
+ def add_invoice(amount)
185
+ response = stub.add_invoice(Lnrpc::Invoice.new(value: amount))
186
+ end
187
+
188
+ #
189
+ #
190
+ # We are not using the following methods.
191
+ #
192
+ #
193
+
194
+ private
195
+
196
+ def balance
197
+ response = stub.wallet_balance(Lnrpc::WalletBalanceRequest.new())
198
+ response.total_balance
199
+ end
200
+
201
+ end
202
+