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"
|
"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"
|
||||||
|
@ -36,25 +37,21 @@ func Request(rKey string) (string, error) {
|
||||||
return "", errors.New("internal server error")
|
return "", errors.New("internal server error")
|
||||||
}
|
}
|
||||||
|
|
||||||
expiryInMin := int64(5)
|
info, err := lndCli.AddInvoice(ctx, &lnrpc.Invoice{
|
||||||
invoice, err := lndCli.AddInvoice(ctx, &lnrpc.Invoice{
|
|
||||||
Memo: fmt.Sprintf("nostr addr %s", rKey),
|
Memo: fmt.Sprintf("nostr addr %s", rKey),
|
||||||
Expiry: expiryInMin * 60,
|
Expiry: 1 * 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)
|
||||||
// add a minute to the expiry time to ensure entries aren't removed from redis before they are expired
|
paymentReq := info.PaymentRequest
|
||||||
|
|
||||||
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, rHash, 0).Err()
|
err = redisCli.Set(ctx, rKey, fmt.Sprintf("%s:%s", rHash, paymentReq), 15*time.Minute).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")
|
||||||
|
|
9
main.go
9
main.go
|
@ -22,7 +22,14 @@ 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)
|
||||||
|
|
|
@ -9,6 +9,7 @@ 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"
|
||||||
|
@ -43,7 +44,6 @@ 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,14 +108,9 @@ 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, 0).Err()
|
err = redisCli.Set(ctx, rKey, enc, 15*time.Minute).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
|
||||||
|
@ -127,20 +122,12 @@ 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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue