-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlnd-grpc-test
executable file
·244 lines (145 loc) · 8.61 KB
/
lnd-grpc-test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python3
###############################################################################
###############################################################################
# Copyright (c) 2024, Andy Schroder
# See the file README.md for licensing information.
###############################################################################
###############################################################################
################################################################
# before importing modules (except for argparse), parse the command line
################################################################
from argparse import ArgumentParser,RawTextHelpFormatter
from time import sleep
parser = ArgumentParser(description="lnd-grpc-test: test python lndgrpc function calls that are used by Distributed Charge.\n\nThis script can be thought of as a dumbed down version of lncli. However, instead of using the `tls.cert` and `admin.macaroon` files in the ~/.lnd/ directory, it uses an lndconnect: URI string found in the ~/.dc/Config.yaml file. This script can test that your lndconnect: string is valid, that the python lnd grpc functions are installed and working properly, and that your node are able to be connected to. It can also be used to test if a function call works with the macaroon that you are using and/or the Lightning Terminal `Accounts` system, which has more limited functionality.",formatter_class=RawTextHelpFormatter)
parser.add_argument(
"Action",
choices=['get_info','lookup_invoice','add_invoice','add_hold_invoice','AddAndWatchInvoice','cancel_invoice','settle_invoice','decode_payment_request','send_payment','send_payment_v2','track_payment_v2','track_payments','subscribe_invoices','subscribe_single_invoice','get_best_block','get_block_hash','get_block','channel_balance'],
help='Action to take:\n'+
'\t\t`get_info` gets information about your lnd node.\n'+
'\t\t`lookup_invoice` checks the status of an invoice.\n'+
'\t\t`add_invoice` adds an invoice.\n'+
'\t\t`add_hold_invoice` adds a hold invoice.\n'+
'\t\t`AddAndWatchInvoice` adds an invoice and then watches the status of it.\n'+
'\t\t`cancel_invoice` cancels a hold invoice.\n'+
'\t\t`settle_invoice` settles a hold invoice.\n'+
'\t\t`decode_payment_request` decodes an invoice.\n'+
'\t\t`send_payment` pays an invoice.\n'+
'\t\t`send_payment_v2` pays an invoice and then tracks the status of the outbound payment.\n'+
'\t\t`track_payment_v2` tracks an outbound payment.\n'+
'\t\t`track_payments` tracks all outbound payments.\n'+
'\t\t`subscribe_invoices` tracks all invoices.\n'+
'\t\t`subscribe_single_invoice` tracks a single invoice.\n'+
'\t\t`get_best_block` gets the latest block.\n'+
'\t\t`get_block_hash` gets a block hash at a specific block height.\n'+
'\t\t`get_block` gets an entire block based on a block hash.\n'+
'\t\t`channel_balance` gets the channel balance.\n'+
''
)
parser.add_argument('-c','--config-path', help="override path to `Config.yaml` (default `~/.dc/` or defined by the `DC_DATADIR` environmental variable)", type=str,default=None)
parser.add_argument('-f','--config-file-name', help="override the name of `Config.yaml`", type=str,default=None)
parser.add_argument('-p','--party', choices=['Buyer', 'Seller'],default='Buyer',help="party to use in `Config.yaml` to select the `LNDhost` value from (default: %(default)s).")
parser.add_argument('--LNDhost', help="override the `LNDhost` value that was read from the config file (value passed must be enclosed in quotes so that your shell does not misinterpret it)", type=str, default=None)
parser.add_argument('--amount', default=1,type=int,help="`add_invoice`, `add_hold_invoice`, and `AddAndWatchInvoice` amount (default: %(default)s) [sat].")
parser.add_argument('--r_hash',type=str,help="`lookup_invoice`, `cancel_invoice`, `subscribe_single_invoice`, and `track_payment_v2` r_hash (hex)")
parser.add_argument('--preimage',type=str,help="`settle_invoice`, preimage (hex)")
parser.add_argument('--invoice',type=str,help="`send_payment`, `send_payment_v2`, and `decode_payment_request` invoice")
parser.add_argument('--fee_limit_sat', default=5,type=int,help="`send_payment_v2` fee limit (default: %(default)s) [sat].")
parser.add_argument('--block_height',type=int,help="`get_block_hash` block_height")
parser.add_argument('--block_hash',type=str,help="`get_block` block_hash (hex)")
arguments=parser.parse_args()
################################################################
# import dc and then override default values
################################################################
import dc
dc.mode='lnd-grpc-test-'+arguments.party
if arguments.config_file_name is not None:
dc.TheConfigFile=arguments.config_file_name
if arguments.config_path is not None:
dc.TheDataFolder=arguments.config_path
if arguments.LNDhost is not None:
dc.LNDhost=arguments.LNDhost
################################################################
# now import the rest of the modules
################################################################
from dc.common import lnd
################################################################
# act based on the command line arguments
################################################################
print()
if arguments.Action == 'get_info':
print(lnd.get_info())
elif arguments.Action == 'channel_balance':
print(lnd.channel_balance())
elif arguments.Action == 'get_block':
print(lnd.get_block(bytes.fromhex(arguments.block_hash)))
elif arguments.Action == 'get_best_block':
Block=lnd.get_best_block()
print(Block)
print('hex versions:')
print('block_hash: '+Block.block_hash.hex())
elif arguments.Action == 'get_block_hash':
Block=lnd.get_block_hash(arguments.block_height)
print(Block)
print('hex versions:')
print('block_hash: '+Block.block_hash.hex())
elif arguments.Action == 'add_invoice':
TheInvoice=lnd.add_invoice(arguments.amount)
print(TheInvoice)
print('hex versions:')
print('r_hash: '+TheInvoice.r_hash.hex())
print('payment_addr: '+TheInvoice.payment_addr.hex())
elif arguments.Action == 'add_hold_invoice':
TheInvoice, r_hash, preimage = lnd.add_hold_invoice(value=arguments.amount)
print(TheInvoice)
print('hex versions:')
print('r_hash: '+r_hash.hex())
print('r_preimage: '+preimage.hex())
elif arguments.Action == 'AddAndWatchInvoice':
TheInvoiceThread=lnd.AddAndWatchInvoice(value=arguments.amount)
while True:
if TheInvoiceThread.state==1 or TheInvoiceThread.state==2: # 'SETTLED' or 'CANCELED'
break
else:
sleep(0.1)
elif arguments.Action == 'cancel_invoice':
TheResponse=lnd.cancel_invoice(bytes.fromhex(arguments.r_hash))
print(TheResponse)
elif arguments.Action == 'settle_invoice':
TheResponse=lnd.settle_invoice(bytes.fromhex(arguments.preimage))
print(TheResponse)
elif arguments.Action == 'lookup_invoice':
TheInvoice=lnd.lookup_invoice(bytes.fromhex(arguments.r_hash))
print(TheInvoice)
print('hex versions:')
print('r_preimage: '+TheInvoice.r_preimage.hex())
print('r_hash: '+TheInvoice.r_hash.hex())
print('payment_addr: '+TheInvoice.payment_addr.hex())
elif arguments.Action == 'decode_payment_request':
TheInvoice=lnd.decode_payment_request(arguments.invoice)
print(TheInvoice)
elif arguments.Action == 'send_payment':
TheInvoice=lnd.send_payment(arguments.invoice)
print(TheInvoice)
print('payment_preimage: '+TheInvoice.payment_preimage.hex())
print('payment_hash: '+TheInvoice.payment_hash.hex())
elif arguments.Action == 'send_payment_v2':
for TheResponse in lnd.send_payment_v2(payment_request=arguments.invoice, fee_limit_sat=arguments.fee_limit_sat, timeout_seconds=25, allow_self_payment=True):
print('\n\n====================================================================================')
print(TheResponse)
elif arguments.Action == 'track_payment_v2':
for TheResponse in lnd.track_payment_v2(arguments.r_hash):
print('\n\n====================================================================================')
print(TheResponse)
elif arguments.Action == 'track_payments':
for TheResponse in lnd.track_payments():
print('\n\n====================================================================================')
print(TheResponse)
elif arguments.Action == 'subscribe_invoices':
for Invoice in lnd.subscribe_invoices():
print('\n\n====================================================================================')
print(Invoice)
elif arguments.Action == 'subscribe_single_invoice':
for Invoice in lnd.subscribe_single_invoice(bytes.fromhex(arguments.r_hash)):
print('\n\n====================================================================================')
print(Invoice)
print()