2021-08-13 16:21:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-08-28 12:50:17 +00:00
|
|
|
"fmt"
|
2021-08-13 16:21:00 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fiatjaf/makeinvoice"
|
|
|
|
"github.com/tidwall/sjson"
|
|
|
|
)
|
|
|
|
|
2021-08-20 12:32:45 +00:00
|
|
|
func makeMetadata(params *Params) string {
|
2021-08-13 16:21:00 +00:00
|
|
|
metadata, _ := sjson.Set("[]", "0.0", "text/identifier")
|
2022-01-18 23:50:47 +00:00
|
|
|
metadata, _ = sjson.Set(metadata, "0.1", params.Name+"@"+params.Domain)
|
2021-08-13 16:21:00 +00:00
|
|
|
|
|
|
|
metadata, _ = sjson.Set(metadata, "1.0", "text/plain")
|
2022-01-18 23:50:47 +00:00
|
|
|
metadata, _ = sjson.Set(metadata, "1.1", "Satoshis to "+params.Name+"@"+params.Domain+".")
|
2021-08-13 16:21:00 +00:00
|
|
|
|
|
|
|
// TODO support image, custom description
|
|
|
|
|
|
|
|
return metadata
|
|
|
|
}
|
|
|
|
|
2021-08-28 12:50:17 +00:00
|
|
|
func makeInvoice(
|
|
|
|
params *Params,
|
|
|
|
msat int,
|
|
|
|
pin *string,
|
|
|
|
) (bolt11 string, err error) {
|
2021-08-13 16:21:00 +00:00
|
|
|
// 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,
|
|
|
|
}
|
|
|
|
case "lnbits":
|
|
|
|
backend = makeinvoice.LNBitsParams{
|
|
|
|
Host: params.Host,
|
|
|
|
Key: params.Key,
|
|
|
|
}
|
2021-08-14 21:55:05 +00:00
|
|
|
case "lnpay":
|
|
|
|
backend = makeinvoice.LNPayParams{
|
|
|
|
PublicAccessKey: params.Pak,
|
|
|
|
WalletInvoiceKey: params.Waki,
|
|
|
|
}
|
2021-09-14 00:27:04 +00:00
|
|
|
case "eclair":
|
|
|
|
backend = makeinvoice.EclairParams{
|
|
|
|
Host: params.Host,
|
|
|
|
Password: "",
|
|
|
|
}
|
2022-03-16 00:16:02 +00:00
|
|
|
case "commando":
|
|
|
|
backend = makeinvoice.CommandoParams{
|
|
|
|
Host: params.Host,
|
|
|
|
NodeId: params.NodeId,
|
|
|
|
Rune: params.Rune,
|
|
|
|
}
|
2021-08-13 16:21:00 +00:00
|
|
|
}
|
|
|
|
|
2021-08-28 12:50:17 +00:00
|
|
|
mip := makeinvoice.Params{
|
|
|
|
Msatoshi: int64(msat),
|
|
|
|
Backend: backend,
|
|
|
|
|
2022-01-20 13:36:49 +00:00
|
|
|
Label: params.Domain + "/" + strconv.FormatInt(time.Now().Unix(), 16),
|
2021-08-28 12:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if pin != nil {
|
|
|
|
// use this as the description
|
2022-01-20 13:36:49 +00:00
|
|
|
mip.Description = fmt.Sprintf("%s's PIN for '%s@%s' lightning address: %s", params.Domain, params.Name, params.Domain, *pin)
|
2021-08-28 12:50:17 +00:00
|
|
|
} else {
|
|
|
|
// make the lnurlpay description_hash
|
2022-07-20 18:05:14 +00:00
|
|
|
mip.Description = makeMetadata(params)
|
|
|
|
mip.UseDescriptionHash = true
|
2021-08-28 12:50:17 +00:00
|
|
|
}
|
2021-08-20 13:30:20 +00:00
|
|
|
|
2021-08-13 16:21:00 +00:00
|
|
|
// actually generate the invoice
|
2021-08-28 12:50:17 +00:00
|
|
|
bolt11, err = makeinvoice.MakeInvoice(mip)
|
2021-08-13 16:21:00 +00:00
|
|
|
|
2021-08-28 12:50:17 +00:00
|
|
|
log.Debug().Int("msatoshi", msat).
|
|
|
|
Interface("backend", backend).
|
|
|
|
Str("bolt11", bolt11).Err(err).
|
|
|
|
Msg("invoice generation")
|
|
|
|
|
|
|
|
return bolt11, err
|
2021-08-13 16:21:00 +00:00
|
|
|
}
|