forked from nbd-wtf/satdress
-
Notifications
You must be signed in to change notification settings - Fork 14
/
makeinvoice.go
435 lines (361 loc) · 10.4 KB
/
makeinvoice.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package main
import (
"bytes"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/fiatjaf/eclair-go"
lightning "github.com/fiatjaf/lightningd-gjson-rpc"
lnsocket "github.com/jb55/lnsocket/go"
"github.com/lnpay/lnpay-go"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
var (
TorProxyURL = "socks5://127.0.0.1:9050"
Client = &http.Client{
Timeout: 10 * time.Second,
}
)
type LNParams struct {
Backend BackendParams
Msatoshi int64
Description string
// setting this to true will cause .Description to be hashed and used as
// the description_hash (h) field on the bolt11 invoice
UseDescriptionHash bool
Label string // only used for c-lightning
}
type CommandoParams struct {
Rune string
Host string
NodeId string
}
func (l CommandoParams) getCert() string { return "" }
func (l CommandoParams) isTor() bool {
return strings.Contains(l.Host, ".onion")
}
type SparkoParams struct {
Cert string
Host string
Key string
}
func (l SparkoParams) getCert() string { return l.Cert }
func (l SparkoParams) isTor() bool {
return strings.Contains(l.Host, ".onion")
}
type LNDParams struct {
Cert string
Host string
Macaroon string
}
func (l LNDParams) getCert() string { return l.Cert }
func (l LNDParams) isTor() bool {
return strings.Contains(l.Host, ".onion")
}
type LNBitsParams struct {
Cert string
Host string
Key string
}
func (l LNBitsParams) getCert() string { return l.Cert }
func (l LNBitsParams) isTor() bool {
return strings.Contains(l.Host, ".onion")
}
type LNPayParams struct {
PublicAccessKey string
WalletInvoiceKey string
}
func (l LNPayParams) getCert() string { return "" }
func (l LNPayParams) isTor() bool { return false }
type EclairParams struct {
Host string
Password string
Cert string
}
func (l EclairParams) getCert() string { return l.Cert }
func (l EclairParams) isTor() bool {
return strings.Contains(l.Host, ".onion")
}
type StrikeParams struct {
Key string
Username string
Currency string
}
func (l StrikeParams) getCert() string { return "" }
func (l StrikeParams) isTor() bool { return false }
type BackendParams interface {
getCert() string
isTor() bool
}
func MakeInvoice(params LNParams) (bolt11 string, err error) {
defer func(prevTransport http.RoundTripper) {
Client.Transport = prevTransport
}(Client.Transport)
specialTransport := &http.Transport{}
// use a cert or skip TLS verification?
if params.Backend.getCert() != "" {
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(params.Backend.getCert()))
specialTransport.TLSClientConfig = &tls.Config{RootCAs: caCertPool}
} else {
specialTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
// use a tor proxy?
if params.Backend.isTor() {
torURL, _ := url.Parse(TorProxyURL)
specialTransport.Proxy = http.ProxyURL(torURL)
}
Client.Transport = specialTransport
// description hash?
var hexh, b64h string
if params.UseDescriptionHash {
descriptionHash := sha256.Sum256([]byte(params.Description))
hexh = hex.EncodeToString(descriptionHash[:])
b64h = base64.StdEncoding.EncodeToString(descriptionHash[:])
}
switch backend := params.Backend.(type) {
case SparkoParams:
spark := &lightning.Client{
SparkURL: backend.Host,
SparkToken: backend.Key,
CallTimeout: time.Second * 3,
}
var method, desc string
if params.UseDescriptionHash {
method = "invoicewithdescriptionhash"
desc = hexh
} else {
method = "invoice"
desc = params.Description
}
label := params.Label
if label == "" {
label = makeRandomLabel()
}
inv, err := spark.Call(method, params.Msatoshi, label, desc)
if err != nil {
return "", fmt.Errorf(method+" call failed: %w", err)
}
return inv.Get("bolt11").String(), nil
case LNDParams:
body, _ := sjson.Set("{}", "value_msat", params.Msatoshi)
if params.UseDescriptionHash {
body, _ = sjson.Set(body, "description_hash", b64h)
} else {
body, _ = sjson.Set(body, "memo", params.Description)
}
if s.LNDprivateOnly {
body, _ = sjson.Set(body, "private", true)
}
req, err := http.NewRequest("POST",
backend.Host+"/v1/invoices",
bytes.NewBufferString(body),
)
if err != nil {
return "", err
}
// macaroon must be hex, so if it is on base64 we adjust that
if b, err := base64.StdEncoding.DecodeString(backend.Macaroon); err == nil {
backend.Macaroon = hex.EncodeToString(b)
}
req.Header.Set("Grpc-Metadata-macaroon", backend.Macaroon)
resp, err := Client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
body, _ := io.ReadAll(resp.Body)
text := string(body)
if len(text) > 300 {
text = text[:300]
}
return "", fmt.Errorf("call to lnd failed (%d): %s", resp.StatusCode, text)
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return gjson.ParseBytes(b).Get("payment_request").String(), nil
case LNBitsParams:
body, _ := sjson.Set("{}", "amount", params.Msatoshi/1000)
body, _ = sjson.Set(body, "out", false)
if params.UseDescriptionHash {
body, _ = sjson.Set(body, "unhashed_description", hex.EncodeToString([]byte(params.Description)))
} else {
if params.Description == "" {
body, _ = sjson.Set(body, "memo", "created by makeinvoice")
} else {
body, _ = sjson.Set(body, "memo", params.Description)
}
if s.LNDprivateOnly {
body, _ = sjson.Set(body, "private", true)
}
}
req, err := http.NewRequest("POST",
backend.Host+"/api/v1/payments",
bytes.NewBufferString(body),
)
if err != nil {
return "", err
}
req.Header.Set("X-Api-Key", backend.Key)
req.Header.Set("Content-Type", "application/json")
resp, err := Client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
body, _ := io.ReadAll(resp.Body)
text := string(body)
if len(text) > 300 {
text = text[:300]
}
return "", fmt.Errorf("call to lnbits failed (%d): %s", resp.StatusCode, text)
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return gjson.ParseBytes(b).Get("payment_request").String(), nil
case LNPayParams:
client := lnpay.NewClient(backend.PublicAccessKey)
wallet := client.Wallet(backend.WalletInvoiceKey)
lntx, err := wallet.Invoice(lnpay.InvoiceParams{
NumSatoshis: params.Msatoshi / 1000,
Memo: params.Description,
DescriptionHash: hexh,
})
if err != nil {
return "", fmt.Errorf("error creating invoice on lnpay: %w", err)
}
return lntx.PaymentRequest, nil
case EclairParams:
client := eclair.Client{Host: backend.Host, Password: backend.Password}
eclairParams := eclair.Params{"amountMsat": params.Msatoshi}
if params.UseDescriptionHash {
eclairParams["descriptionHash"] = hexh
} else {
eclairParams["description"] = params.Description
}
inv, err := client.Call("createinvoice", eclairParams)
if err != nil {
return "", fmt.Errorf("error creating invoice on eclair: %w", err)
}
return inv.Get("serialized").String(), nil
case StrikeParams:
payload := struct {
Description string `json:"description"`
Amount struct {
Currency string `json:"currency"`
Amount string `json:"amount"`
} `json:"amount"`
}{}
payload.Description = "created by makeinvoice"
if params.Description != "" {
payload.Description = params.Description
}
// TODO: BTC currency does not seem to be supported at the moment Currently the currency needs to be the user's base currency (USD for the US, USDT for El Sal and Argentina). However, we're going to enable BTC invoices in the coming weeks.
payload.Amount.Currency = backend.Currency
payload.Amount.Amount = fmt.Sprintf("%.8f",
float32(params.Msatoshi)/100000000000)
jpayload := &bytes.Buffer{}
json.NewEncoder(jpayload).Encode(payload)
client := &http.Client{}
req, err := http.NewRequest("POST",
"https://api.strike.me/v1/invoices/handle/"+backend.Username, jpayload)
if err != nil {
return "", err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", "Bearer "+backend.Key)
res, err := client.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
invoiceId := gjson.ParseBytes(body).Get("invoiceId").String()
// got strike invoice - get actual LN invoice now. sigh.
req, err = http.NewRequest("POST",
"https://api.strike.me/v1/invoices/"+invoiceId+"/quote", jpayload)
if err != nil {
return "", err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", "Bearer "+backend.Key)
res, err = client.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
body, err = io.ReadAll(res.Body)
if err != nil {
return "", err
}
lnInvoice := gjson.ParseBytes(body).Get("lnInvoice").String()
return lnInvoice, nil
case CommandoParams:
ln := lnsocket.LNSocket{}
ln.GenKey()
err := ln.ConnectAndInit(backend.Host, backend.NodeId)
if err != nil {
return "", err
}
defer ln.Disconnect()
label := params.Label
if label == "" {
label = makeRandomLabel()
}
invoiceParams := map[string]interface{}{
"amount_msat": params.Msatoshi,
"label": label,
"description": params.Description,
}
if params.UseDescriptionHash {
invoiceParams["deschashonly"] = true
}
jparams, _ := json.Marshal(invoiceParams)
body, err := ln.Rpc(backend.Rune, "invoice", string(jparams))
if err != nil {
return "", err
}
resErr := gjson.Get(body, "error")
if resErr.Type != gjson.Null {
if resErr.Type == gjson.JSON {
return "", errors.New(resErr.Get("message").String())
} else if resErr.Type == gjson.String {
return "", errors.New(resErr.String())
}
return "", fmt.Errorf("unknown commando error: '%v'", resErr)
}
invoice := gjson.Get(body, "result.bolt11")
if invoice.Type != gjson.String {
return "", fmt.Errorf("no bolt11 result found in invoice response, got %v", body)
}
return invoice.String(), nil
}
return "", errors.New("missing backend params")
}
func makeRandomLabel() string {
return "makeinvoice/" + strconv.FormatInt(time.Now().Unix(), 16)
}