Compare commits
No commits in common. "557a20ac93be39b77e6683921f7c56df9c928b65" and "e043ca1c694f972dc1400edf8c4ac22f0d7aaea6" have entirely different histories.
557a20ac93
...
e043ca1c69
4 changed files with 18 additions and 115 deletions
|
@ -1,88 +0,0 @@
|
|||
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)
|
||||
}
|
||||
}
|
15
lnd/lnd.go
15
lnd/lnd.go
|
@ -7,6 +7,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.minhas.io/asara/well-goknown/config"
|
||||
"git.minhas.io/asara/well-goknown/logger"
|
||||
|
@ -36,25 +37,21 @@ func Request(rKey string) (string, error) {
|
|||
return "", errors.New("internal server error")
|
||||
}
|
||||
|
||||
expiryInMin := int64(5)
|
||||
invoice, err := lndCli.AddInvoice(ctx, &lnrpc.Invoice{
|
||||
info, err := lndCli.AddInvoice(ctx, &lnrpc.Invoice{
|
||||
Memo: fmt.Sprintf("nostr addr %s", rKey),
|
||||
Expiry: expiryInMin * 60,
|
||||
Expiry: 1 * 60,
|
||||
Value: addrFee,
|
||||
})
|
||||
if err != nil {
|
||||
l.Fatal().Msg("unable to create lnd invoice")
|
||||
return "", errors.New("internal server error")
|
||||
}
|
||||
|
||||
// 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)
|
||||
rHash := hex.EncodeToString(info.RHash)
|
||||
paymentReq := info.PaymentRequest
|
||||
|
||||
// write lnd request to redis
|
||||
redisCli := redis.LndRedisConn.Client
|
||||
err = redisCli.Set(ctx, rKey, rHash, 0).Err()
|
||||
err = redisCli.Set(ctx, rKey, fmt.Sprintf("%s:%s", rHash, paymentReq), 15*time.Minute).Err()
|
||||
if err != nil {
|
||||
l.Error().Msg("unable to connect to redis when writing lnd request")
|
||||
return "", errors.New("unable to make the request, please try again")
|
||||
|
|
9
main.go
9
main.go
|
@ -22,7 +22,14 @@ func main() {
|
|||
if err != nil {
|
||||
l.Fatal().Msg("unable to connect to redis")
|
||||
}
|
||||
|
||||
/*
|
||||
nostrTest := nostr.NostrRequest{
|
||||
Name: "asara",
|
||||
Key: "npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6",
|
||||
Hostname: "devvul.com",
|
||||
}
|
||||
nostr.AddNostrAddr(nostrTest)
|
||||
*/
|
||||
// matrix endpoints
|
||||
l.Debug().Msg("enabling matrix server endpoint")
|
||||
http.HandleFunc("/.well-known/matrix/server", matrix.MatrixServer)
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"html/template"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.minhas.io/asara/well-goknown/lnd"
|
||||
"git.minhas.io/asara/well-goknown/logger"
|
||||
|
@ -43,7 +44,6 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
|
|||
l := logger.Get()
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
w.WriteHeader(http.StatusOK)
|
||||
http.ServeFile(w, r, "html/nostr_form.html")
|
||||
case "POST":
|
||||
r.ParseForm()
|
||||
|
@ -108,14 +108,9 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
|
|||
pngB64 := b64.StdEncoding.EncodeToString(png)
|
||||
|
||||
// write request to redis
|
||||
err = redisCli.Set(ctx, rKey, enc, 0).Err()
|
||||
err = redisCli.Set(ctx, rKey, enc, 15*time.Minute).Err()
|
||||
if err != nil {
|
||||
l.Error().Msg("unable to connect to redis")
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
json.NewEncoder(w).Encode(&jsonErrorMessage{
|
||||
Error: "service unavailable",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// return qr code
|
||||
|
@ -127,20 +122,12 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
|
|||
tmplt, err := template.ParseFiles("html/nostr_qr_response.html")
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
l.Error().Msg("unable to connect to render template")
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
json.NewEncoder(w).Encode(&jsonErrorMessage{
|
||||
Error: "service unavailable",
|
||||
})
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue