-
Notifications
You must be signed in to change notification settings - Fork 55
/
makeinvoice.go
91 lines (78 loc) · 2 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
package main
import (
"fmt"
"strconv"
"time"
"github.com/fiatjaf/makeinvoice"
"github.com/tidwall/sjson"
)
func makeMetadata(params *Params) string {
metadata, _ := sjson.Set("[]", "0.0", "text/identifier")
metadata, _ = sjson.Set(metadata, "0.1", params.Name+"@"+params.Domain)
metadata, _ = sjson.Set(metadata, "1.0", "text/plain")
metadata, _ = sjson.Set(metadata, "1.1", "Satoshis to "+params.Name+"@"+params.Domain+".")
// TODO support image, custom description
return metadata
}
func makeInvoice(
params *Params,
msat int,
pin *string,
) (bolt11 string, err error) {
// prepare params
var backend makeinvoice.BackendParams
switch params.Kind {
case "sparko":
backend = makeinvoice.SparkoParams{
Host: params.Host,
Key: params.Key,
}
case "lnd":
backend = makeinvoice.LNDParams{
Host: params.Host,
Macaroon: params.Key,
Private: true,
}
case "lnbits":
backend = makeinvoice.LNBitsParams{
Host: params.Host,
Key: params.Key,
}
case "lnpay":
backend = makeinvoice.LNPayParams{
PublicAccessKey: params.Pak,
WalletInvoiceKey: params.Waki,
}
case "eclair":
backend = makeinvoice.EclairParams{
Host: params.Host,
Password: "",
}
case "commando":
backend = makeinvoice.CommandoParams{
Host: params.Host,
NodeId: params.NodeId,
Rune: params.Rune,
}
}
mip := makeinvoice.Params{
Msatoshi: int64(msat),
Backend: backend,
Label: params.Domain + "/" + strconv.FormatInt(time.Now().Unix(), 16),
}
if pin != nil {
// use this as the description
mip.Description = fmt.Sprintf("%s's PIN for '%s@%s' lightning address: %s", params.Domain, params.Name, params.Domain, *pin)
} else {
// make the lnurlpay description_hash
mip.Description = makeMetadata(params)
mip.UseDescriptionHash = true
}
// actually generate the invoice
bolt11, err = makeinvoice.MakeInvoice(mip)
log.Debug().Int("msatoshi", msat).
Interface("backend", backend).
Str("bolt11", bolt11).Err(err).
Msg("invoice generation")
return bolt11, err
}