From 557a20ac93be39b77e6683921f7c56df9c928b65 Mon Sep 17 00:00:00 2001 From: Asara Date: Sat, 6 May 2023 22:39:04 -0400 Subject: [PATCH] Beginnings of the sync binary --- cmd/lnd-verifier/main.go | 88 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 cmd/lnd-verifier/main.go diff --git a/cmd/lnd-verifier/main.go b/cmd/lnd-verifier/main.go new file mode 100644 index 0000000..d7a40e4 --- /dev/null +++ b/cmd/lnd-verifier/main.go @@ -0,0 +1,88 @@ +package main + +import ( + "context" + b64 "encoding/base64" + "encoding/hex" + "fmt" + + "git.minhas.io/asara/well-goknown/config" + "git.minhas.io/asara/well-goknown/logger" + "git.minhas.io/asara/well-goknown/redis" + "github.com/davecgh/go-spew/spew" + "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") + } + + // get requested keys + lndRedisCli := redis.LndRedisConn.Client + //nostrRedisCli := redis.NostrRedisConn.Client + + 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.Error().Msg(fmt.Sprintf("%s: expired", key)) + continue + default: + l.Error().Msg(fmt.Sprintf("%s: unknown error", key)) + continue + } + } + + if invoice.Settled { + // move requested to verified on nostr redis keyspace + // delete current key + continue + } + spew.Dump(invoice) + spew.Dump(key) + spew.Dump(value) + } +}