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")
}
/*
// 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")
}
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
}
}