well-goknown/cmd/lnd-verifier/main.go

96 lines
2.7 KiB
Go
Raw Normal View History

2023-05-07 02:39:04 +00:00
package main
import (
"context"
b64 "encoding/base64"
"encoding/hex"
"fmt"
2023-05-16 00:37:40 +00:00
"strings"
2023-05-07 02:39:04 +00:00
"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")
}
/*
2023-05-16 00:43:58 +00:00
// Test to add requests directly to nostr keyspace for easy bootstrap/general laziness
2023-05-07 02:39:04 +00:00
nostrTest := nostr.NostrRequest{
Name: "asara",
2023-05-16 00:43:58 +00:00
Key: "npub19hhggqd5zpmmddv9dvu2qq0ne5pn7f884v4dmku3llp4s44xaqzsl0vms7",
2023-05-07 02:39:04 +00:00
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")
}
2023-05-16 00:37:40 +00:00
// set up redis connections
2023-05-07 02:39:04 +00:00
lndRedisCli := redis.LndRedisConn.Client
2023-05-16 00:37:40 +00:00
nostrRedisCli := redis.NostrRedisConn.Client
2023-05-07 02:39:04 +00:00
2023-05-16 00:37:40 +00:00
// get requested keys
2023-05-07 02:39:04 +00:00
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:
2023-05-16 00:37:40 +00:00
l.Info().Msg(fmt.Sprintf("%s: payment request expired", key))
lndRedisCli.Del(ctx, "requested:%s", key)
nostrRedisCli.Del(ctx, "requested:%s", key)
2023-05-07 02:39:04 +00:00
continue
default:
2023-05-16 00:37:40 +00:00
l.Error().Msg(fmt.Sprintf("%s: unknown error during invoice lookup", key))
2023-05-07 02:39:04 +00:00
continue
}
}
if invoice.Settled {
// move requested to verified on nostr redis keyspace
// delete current key
2023-05-16 00:37:40 +00:00
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))
2023-05-07 02:39:04 +00:00
continue
}
2023-05-16 00:37:40 +00:00
l.Info().Msg(fmt.Sprintf("%s: invoice still unpaid", key))
continue
2023-05-07 02:39:04 +00:00
}
}