satdress-custom2/makeinvoice.go

91 lines
2.0 KiB
Go
Raw Permalink Normal View History

2021-08-13 16:21:00 +00:00
package main
import (
"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
}
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,
}
case "lnpay":
backend = makeinvoice.LNPayParams{
PublicAccessKey: params.Pak,
WalletInvoiceKey: params.Waki,
}
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
}
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),
}
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)
} else {
// make the lnurlpay description_hash
mip.Description = makeMetadata(params)
mip.UseDescriptionHash = true
}
2021-08-20 13:30:20 +00:00
2021-08-13 16:21:00 +00:00
// actually generate the invoice
bolt11, err = makeinvoice.MakeInvoice(mip)
2021-08-13 16:21:00 +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
}