well-goknown/lnd/lnd.go

62 lines
1.7 KiB
Go

package lnd
import (
"context"
b64 "encoding/base64"
"encoding/hex"
"errors"
"fmt"
"strconv"
"time"
"git.minhas.io/asara/well-goknown/config"
"git.minhas.io/asara/well-goknown/logger"
"git.minhas.io/asara/well-goknown/redis"
"github.com/lightninglabs/lndclient"
"github.com/lightningnetwork/lnd/lnrpc"
)
func Request(rKey string) (string, error) {
l := logger.Get()
ctx := context.TODO()
// connect to lnd
// TODO: move to its own function and don't connect on every request
macaroon := config.GetConfig().LndMacaroonHex
addr := config.GetConfig().LndAddr
dec, _ := b64.StdEncoding.DecodeString(config.GetConfig().LndCertB64)
lndCert := string(dec)
lndCli, err := lndclient.NewBasicClient(addr, "", "", "bitcoind", lndclient.MacaroonData(macaroon), lndclient.TLSData(lndCert))
if err != nil {
l.Fatal().Msg("unable to connect to lnd")
}
// add invoice
addrFee, err := strconv.ParseInt(config.GetConfig().NostrAddrFee, 10, 64)
if err != nil {
l.Fatal().Msg("nostr address fee not set properlly")
return "", errors.New("internal server error")
}
info, err := lndCli.AddInvoice(ctx, &lnrpc.Invoice{
Memo: fmt.Sprintf("nostr addr %s", rKey),
Expiry: 1 * 60,
Value: addrFee,
})
if err != nil {
l.Fatal().Msg("unable to create lnd invoice")
return "", errors.New("internal server error")
}
rHash := hex.EncodeToString(info.RHash)
paymentReq := info.PaymentRequest
// write lnd request to redis
redisCli := redis.LndRedisConn.Client
err = redisCli.Set(ctx, rKey, fmt.Sprintf("%s:%s", rHash, paymentReq), 15*time.Minute).Err()
if err != nil {
l.Error().Msg("unable to connect to redis when writing lnd request")
return "", errors.New("unable to make the request, please try again")
}
return paymentReq, nil
}