Compare commits

...

4 commits

4 changed files with 115 additions and 18 deletions

88
cmd/lnd-verifier/main.go Normal file
View file

@ -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)
}
}

View file

@ -7,7 +7,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"strconv" "strconv"
"time"
"git.minhas.io/asara/well-goknown/config" "git.minhas.io/asara/well-goknown/config"
"git.minhas.io/asara/well-goknown/logger" "git.minhas.io/asara/well-goknown/logger"
@ -37,21 +36,25 @@ func Request(rKey string) (string, error) {
return "", errors.New("internal server error") return "", errors.New("internal server error")
} }
info, err := lndCli.AddInvoice(ctx, &lnrpc.Invoice{ expiryInMin := int64(5)
invoice, err := lndCli.AddInvoice(ctx, &lnrpc.Invoice{
Memo: fmt.Sprintf("nostr addr %s", rKey), Memo: fmt.Sprintf("nostr addr %s", rKey),
Expiry: 1 * 60, Expiry: expiryInMin * 60,
Value: addrFee, Value: addrFee,
}) })
if err != nil { if err != nil {
l.Fatal().Msg("unable to create lnd invoice") l.Fatal().Msg("unable to create lnd invoice")
return "", errors.New("internal server error") return "", errors.New("internal server error")
} }
rHash := hex.EncodeToString(info.RHash)
paymentReq := info.PaymentRequest // add a minute to the expiry time to ensure entries aren't removed from redis before they are expired
paymentReq := invoice.PaymentRequest
rHash := hex.EncodeToString(invoice.RHash)
// write lnd request to redis // write lnd request to redis
redisCli := redis.LndRedisConn.Client redisCli := redis.LndRedisConn.Client
err = redisCli.Set(ctx, rKey, fmt.Sprintf("%s:%s", rHash, paymentReq), 15*time.Minute).Err() err = redisCli.Set(ctx, rKey, rHash, 0).Err()
if err != nil { if err != nil {
l.Error().Msg("unable to connect to redis when writing lnd request") l.Error().Msg("unable to connect to redis when writing lnd request")
return "", errors.New("unable to make the request, please try again") return "", errors.New("unable to make the request, please try again")

View file

@ -22,14 +22,7 @@ func main() {
if err != nil { if err != nil {
l.Fatal().Msg("unable to connect to redis") l.Fatal().Msg("unable to connect to redis")
} }
/*
nostrTest := nostr.NostrRequest{
Name: "asara",
Key: "npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6",
Hostname: "devvul.com",
}
nostr.AddNostrAddr(nostrTest)
*/
// matrix endpoints // matrix endpoints
l.Debug().Msg("enabling matrix server endpoint") l.Debug().Msg("enabling matrix server endpoint")
http.HandleFunc("/.well-known/matrix/server", matrix.MatrixServer) http.HandleFunc("/.well-known/matrix/server", matrix.MatrixServer)

View file

@ -9,7 +9,6 @@ import (
"html/template" "html/template"
"net/http" "net/http"
"strings" "strings"
"time"
"git.minhas.io/asara/well-goknown/lnd" "git.minhas.io/asara/well-goknown/lnd"
"git.minhas.io/asara/well-goknown/logger" "git.minhas.io/asara/well-goknown/logger"
@ -44,6 +43,7 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
l := logger.Get() l := logger.Get()
switch r.Method { switch r.Method {
case "GET": case "GET":
w.WriteHeader(http.StatusOK)
http.ServeFile(w, r, "html/nostr_form.html") http.ServeFile(w, r, "html/nostr_form.html")
case "POST": case "POST":
r.ParseForm() r.ParseForm()
@ -108,9 +108,14 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
pngB64 := b64.StdEncoding.EncodeToString(png) pngB64 := b64.StdEncoding.EncodeToString(png)
// write request to redis // write request to redis
err = redisCli.Set(ctx, rKey, enc, 15*time.Minute).Err() err = redisCli.Set(ctx, rKey, enc, 0).Err()
if err != nil { if err != nil {
l.Error().Msg("unable to connect to redis") l.Error().Msg("unable to connect to redis")
w.WriteHeader(http.StatusServiceUnavailable)
json.NewEncoder(w).Encode(&jsonErrorMessage{
Error: "service unavailable",
})
return
} }
// return qr code // return qr code
@ -122,12 +127,20 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
tmplt, err := template.ParseFiles("html/nostr_qr_response.html") tmplt, err := template.ParseFiles("html/nostr_qr_response.html")
if err != nil { if err != nil {
l.Error().Msg("unable to parse template") l.Error().Msg("unable to parse template")
w.WriteHeader(http.StatusServiceUnavailable)
json.NewEncoder(w).Encode(&jsonErrorMessage{
Error: "service unavailable",
})
return
} }
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
err = tmplt.Execute(w, response) err = tmplt.Execute(w, response)
if err != nil { if err != nil {
l.Error().Msg("unable to connect to render template") l.Error().Msg("unable to connect to render template")
w.WriteHeader(http.StatusServiceUnavailable)
json.NewEncoder(w).Encode(&jsonErrorMessage{
Error: "service unavailable",
})
return
} }
return return
} }