2023-02-03 04:56:15 +00:00
|
|
|
package lnd
|
|
|
|
|
2023-02-05 00:11:50 +00:00
|
|
|
import (
|
2023-05-04 01:54:20 +00:00
|
|
|
"context"
|
|
|
|
b64 "encoding/base64"
|
2023-05-04 02:25:41 +00:00
|
|
|
"encoding/hex"
|
2023-05-04 01:54:20 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2023-02-05 03:39:01 +00:00
|
|
|
|
2023-05-04 01:54:20 +00:00
|
|
|
"git.minhas.io/asara/well-goknown/config"
|
|
|
|
"git.minhas.io/asara/well-goknown/logger"
|
2023-05-05 04:37:27 +00:00
|
|
|
"git.minhas.io/asara/well-goknown/redis"
|
2023-05-04 01:54:20 +00:00
|
|
|
"github.com/lightninglabs/lndclient"
|
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2023-02-05 00:11:50 +00:00
|
|
|
)
|
2023-02-03 04:56:15 +00:00
|
|
|
|
2023-05-05 04:37:27 +00:00
|
|
|
func Request(rKey string) (string, error) {
|
2023-05-04 01:54:20 +00:00
|
|
|
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")
|
2023-05-05 04:37:27 +00:00
|
|
|
return "", errors.New("internal server error")
|
2023-05-04 01:54:20 +00:00
|
|
|
}
|
2023-05-05 04:37:27 +00:00
|
|
|
|
2023-05-06 23:50:10 +00:00
|
|
|
expiryInMin := int64(5)
|
|
|
|
invoice, err := lndCli.AddInvoice(ctx, &lnrpc.Invoice{
|
2023-05-04 01:54:20 +00:00
|
|
|
Memo: fmt.Sprintf("nostr addr %s", rKey),
|
2023-05-06 23:50:10 +00:00
|
|
|
Expiry: expiryInMin * 60,
|
2023-05-04 01:54:20 +00:00
|
|
|
Value: addrFee,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
l.Fatal().Msg("unable to create lnd invoice")
|
2023-05-05 04:37:27 +00:00
|
|
|
return "", errors.New("internal server error")
|
2023-05-04 01:54:20 +00:00
|
|
|
}
|
2023-05-07 00:16:00 +00:00
|
|
|
|
|
|
|
// add a minute to the expiry time to ensure entries aren't removed from redis before they are expired
|
|
|
|
|
2023-05-06 23:50:10 +00:00
|
|
|
paymentReq := invoice.PaymentRequest
|
|
|
|
rHash := hex.EncodeToString(invoice.RHash)
|
2023-05-05 04:37:27 +00:00
|
|
|
|
|
|
|
// write lnd request to redis
|
|
|
|
redisCli := redis.LndRedisConn.Client
|
2023-05-07 02:38:50 +00:00
|
|
|
err = redisCli.Set(ctx, rKey, rHash, 0).Err()
|
2023-05-05 04:37:27 +00:00
|
|
|
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
|
2023-02-03 04:56:15 +00:00
|
|
|
}
|