-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_invoice.go
300 lines (264 loc) · 7.5 KB
/
cmd_invoice.go
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"encoding/hex"
"fmt"
"strconv"
"github.com/brsuite/broln/lnrpc"
"github.com/urfave/cli"
)
var addInvoiceCommand = cli.Command{
Name: "addinvoice",
Category: "Invoices",
Usage: "Add a new invoice.",
Description: `
Add a new invoice, expressing intent for a future payment.
Invoices without an amount can be created by not supplying any
parameters or providing an amount of 0. These invoices allow the payee
to specify the amount of broneess they wish to send.`,
ArgsUsage: "value preimage",
Flags: []cli.Flag{
cli.StringFlag{
Name: "memo",
Usage: "a description of the payment to attach along " +
"with the invoice (default=\"\")",
},
cli.StringFlag{
Name: "preimage",
Usage: "the hex-encoded preimage (32 byte) which will " +
"allow settling an incoming HTLC payable to this " +
"preimage. If not set, a random preimage will be " +
"created.",
},
cli.Int64Flag{
Name: "amt",
Usage: "the amt of broneess in this invoice",
},
cli.Int64Flag{
Name: "amt_msat",
Usage: "the amt of millibroneess in this invoice",
},
cli.StringFlag{
Name: "description_hash",
Usage: "SHA-256 hash of the description of the payment. " +
"Used if the purpose of payment cannot naturally " +
"fit within the memo. If provided this will be " +
"used instead of the description(memo) field in " +
"the encoded invoice.",
},
cli.StringFlag{
Name: "fallback_addr",
Usage: "fallback on-chain address that can be used in " +
"case the lightning payment fails",
},
cli.Int64Flag{
Name: "expiry",
Usage: "the invoice's expiry time in seconds. If not " +
"specified an expiry of 3600 seconds (1 hour) " +
"is implied.",
},
cli.BoolTFlag{
Name: "private",
Usage: "encode routing hints in the invoice with " +
"private channels in order to assist the " +
"payer in reaching you",
},
cli.BoolFlag{
Name: "amp",
Usage: "creates an AMP invoice. If true, preimage " +
"should not be set.",
},
},
Action: actionDecorator(addInvoice),
}
func addInvoice(ctx *cli.Context) error {
var (
preimage []byte
descHash []byte
amt int64
amtMsat int64
err error
)
ctxc := getContext()
client, cleanUp := getClient(ctx)
defer cleanUp()
args := ctx.Args()
amt = ctx.Int64("amt")
amtMsat = ctx.Int64("amt_msat")
if !ctx.IsSet("amt") && !ctx.IsSet("amt_msat") && args.Present() {
amt, err = strconv.ParseInt(args.First(), 10, 64)
args = args.Tail()
if err != nil {
return fmt.Errorf("unable to decode amt argument: %v", err)
}
}
switch {
case ctx.IsSet("preimage"):
preimage, err = hex.DecodeString(ctx.String("preimage"))
case args.Present():
preimage, err = hex.DecodeString(args.First())
}
if err != nil {
return fmt.Errorf("unable to parse preimage: %v", err)
}
descHash, err = hex.DecodeString(ctx.String("description_hash"))
if err != nil {
return fmt.Errorf("unable to parse description_hash: %v", err)
}
invoice := &lnrpc.Invoice{
Memo: ctx.String("memo"),
RPreimage: preimage,
Value: amt,
ValueMsat: amtMsat,
DescriptionHash: descHash,
FallbackAddr: ctx.String("fallback_addr"),
Expiry: ctx.Int64("expiry"),
Private: ctx.Bool("private"),
IsAmp: ctx.Bool("amp"),
}
resp, err := client.AddInvoice(ctxc, invoice)
if err != nil {
return err
}
printRespJSON(resp)
return nil
}
var lookupInvoiceCommand = cli.Command{
Name: "lookupinvoice",
Category: "Invoices",
Usage: "Lookup an existing invoice by its payment hash.",
ArgsUsage: "rhash",
Flags: []cli.Flag{
cli.StringFlag{
Name: "rhash",
Usage: "the 32 byte payment hash of the invoice to query for, the hash " +
"should be a hex-encoded string",
},
},
Action: actionDecorator(lookupInvoice),
}
func lookupInvoice(ctx *cli.Context) error {
ctxc := getContext()
client, cleanUp := getClient(ctx)
defer cleanUp()
var (
rHash []byte
err error
)
switch {
case ctx.IsSet("rhash"):
rHash, err = hex.DecodeString(ctx.String("rhash"))
case ctx.Args().Present():
rHash, err = hex.DecodeString(ctx.Args().First())
default:
return fmt.Errorf("rhash argument missing")
}
if err != nil {
return fmt.Errorf("unable to decode rhash argument: %v", err)
}
req := &lnrpc.PaymentHash{
RHash: rHash,
}
invoice, err := client.LookupInvoice(ctxc, req)
if err != nil {
return err
}
printRespJSON(invoice)
return nil
}
var listInvoicesCommand = cli.Command{
Name: "listinvoices",
Category: "Invoices",
Usage: "List all invoices currently stored within the database. Any " +
"active debug invoices are ignored.",
Description: `
This command enables the retrieval of all invoices currently stored
within the database. It has full support for paginationed responses,
allowing users to query for specific invoices through their add_index.
This can be done by using either the first_index_offset or
last_index_offset fields included in the response as the index_offset of
the next request. Backward pagination is enabled by default to receive
current invoices first. If you wish to paginate forwards, set the
paginate-forwards flag. If none of the parameters are specified, then
the last 100 invoices will be returned.
For example: if you have 200 invoices, "brolncli listinvoices" will return
the last 100 created. If you wish to retrieve the previous 100, the
first_offset_index of the response can be used as the index_offset of
the next listinvoices request.`,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "pending_only",
Usage: "toggles if all invoices should be returned, " +
"or only those that are currently unsettled",
},
cli.Uint64Flag{
Name: "index_offset",
Usage: "the index of an invoice that will be used as " +
"either the start or end of a query to " +
"determine which invoices should be returned " +
"in the response",
},
cli.Uint64Flag{
Name: "max_invoices",
Usage: "the max number of invoices to return",
},
cli.BoolFlag{
Name: "paginate-forwards",
Usage: "if set, invoices succeeding the " +
"index_offset will be returned",
},
},
Action: actionDecorator(listInvoices),
}
func listInvoices(ctx *cli.Context) error {
ctxc := getContext()
client, cleanUp := getClient(ctx)
defer cleanUp()
req := &lnrpc.ListInvoiceRequest{
PendingOnly: ctx.Bool("pending_only"),
IndexOffset: ctx.Uint64("index_offset"),
NumMaxInvoices: ctx.Uint64("max_invoices"),
Reversed: !ctx.Bool("paginate-forwards"),
}
invoices, err := client.ListInvoices(ctxc, req)
if err != nil {
return err
}
printRespJSON(invoices)
return nil
}
var decodePayReqCommand = cli.Command{
Name: "decodepayreq",
Category: "Invoices",
Usage: "Decode a payment request.",
Description: "Decode the passed payment request revealing the destination, payment hash and value of the payment request",
ArgsUsage: "pay_req",
Flags: []cli.Flag{
cli.StringFlag{
Name: "pay_req",
Usage: "the bech32 encoded payment request",
},
},
Action: actionDecorator(decodePayReq),
}
func decodePayReq(ctx *cli.Context) error {
ctxc := getContext()
client, cleanUp := getClient(ctx)
defer cleanUp()
var payreq string
switch {
case ctx.IsSet("pay_req"):
payreq = ctx.String("pay_req")
case ctx.Args().Present():
payreq = ctx.Args().First()
default:
return fmt.Errorf("pay_req argument missing")
}
resp, err := client.DecodePayReq(ctxc, &lnrpc.PayReqString{
PayReq: payreq,
})
if err != nil {
return err
}
printRespJSON(resp)
return nil
}