well-goknown/cmd/lnd-verifier/main.go
2023-05-15 20:37:40 -04:00

96 lines
2.7 KiB
Go

package main
import (
"context"
b64 "encoding/base64"
"encoding/hex"
"fmt"
"strings"
"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"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func main() {
var err error
ctx := context.TODO()
l := logger.Get()
redis.NostrRedisConn, err = redis.New("localhost:6379", "", redis.NostrKeyspace)
if err != nil {
l.Fatal().Msg("unable to connect to redis")
}
redis.LndRedisConn, err = redis.New("localhost:6379", "", redis.LndKeyspace)
if err != nil {
l.Fatal().Msg("unable to connect to redis")
}
/*
// Test to add requests directly to nostr for easy bootstrap/general laziness
nostrTest := nostr.NostrRequest{
Name: "asara",
Key: "npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6",
Hostname: "devvul.com",
}
nostr.AddNostrAddr(nostrTest)
*/
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")
}
// set up redis connections
lndRedisCli := redis.LndRedisConn.Client
nostrRedisCli := redis.NostrRedisConn.Client
// get requested keys
requestedKeys := lndRedisCli.Keys(ctx, "requested:*")
for _, key := range requestedKeys.Val() {
value, err := lndRedisCli.Get(ctx, key).Result()
if err != nil {
l.Error().Msg(fmt.Sprintf("%s: unable to get key", key))
}
rHash, err := hex.DecodeString(value)
if err != nil {
l.Error().Msg(fmt.Sprintf("%s: unable to decode hash for key", key))
}
invoice, err := lndCli.LookupInvoice(ctx, &lnrpc.PaymentHash{
RHash: rHash,
})
if err != nil {
switch status.Code(err) {
case codes.NotFound:
l.Info().Msg(fmt.Sprintf("%s: payment request expired", key))
lndRedisCli.Del(ctx, "requested:%s", key)
nostrRedisCli.Del(ctx, "requested:%s", key)
continue
default:
l.Error().Msg(fmt.Sprintf("%s: unknown error during invoice lookup", key))
continue
}
}
if invoice.Settled {
// move requested to verified on nostr redis keyspace
// delete current key
verified := fmt.Sprintf("verified:%s", strings.Split(key, ":")[1])
nostrRedisCli.Rename(ctx, key, verified)
lndRedisCli.Del(ctx, "requested:%s", key)
l.Info().Msg(fmt.Sprintf("%s: invoice paid, nip-05 verified", key))
continue
}
l.Info().Msg(fmt.Sprintf("%s: invoice still unpaid", key))
continue
}
}