Clean up requests, start working out request management

This commit is contained in:
Amarpreet Minhas 2023-05-05 00:37:27 -04:00
parent 7122e704b8
commit c58314a948
4 changed files with 39 additions and 15 deletions

5
go.mod
View file

@ -3,12 +3,12 @@ module git.minhas.io/asara/well-goknown
go 1.19 go 1.19
require ( require (
github.com/davecgh/go-spew v1.1.1
github.com/lightninglabs/lndclient v0.16.0-10 github.com/lightninglabs/lndclient v0.16.0-10
github.com/lightningnetwork/lnd v0.16.0-beta github.com/lightningnetwork/lnd v0.16.0-beta
github.com/nbd-wtf/go-nostr v0.17.1 github.com/nbd-wtf/go-nostr v0.17.3
github.com/redis/go-redis/v9 v9.0.4 github.com/redis/go-redis/v9 v9.0.4
github.com/rs/zerolog v1.15.0 github.com/rs/zerolog v1.15.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
) )
require ( require (
@ -36,6 +36,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/coreos/go-semver v0.3.0 // indirect github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/decred/dcrd/lru v1.0.0 // indirect github.com/decred/dcrd/lru v1.0.0 // indirect

6
go.sum
View file

@ -440,8 +440,8 @@ github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nbd-wtf/go-nostr v0.17.1 h1:dRyNNf1rx5vGYi9AH1A/mcKKQg8ZSiY8uRUJGObXPPI= github.com/nbd-wtf/go-nostr v0.17.3 h1:OG/85OgjandmHfnVZmr/qsnMPbkVaIaJyXHMBI3ywIE=
github.com/nbd-wtf/go-nostr v0.17.1/go.mod h1:YCDHJtaFQE76d1ZkcUsTkz3dYNP+bldo5CIQwXPPcbk= github.com/nbd-wtf/go-nostr v0.17.3/go.mod h1:YCDHJtaFQE76d1ZkcUsTkz3dYNP+bldo5CIQwXPPcbk=
github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0= github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=
github.com/nwaples/rardecode v1.1.2 h1:Cj0yZY6T1Zx1R7AhTbyGSALm44/Mmq+BAPc4B/p/d3M= github.com/nwaples/rardecode v1.1.2 h1:Cj0yZY6T1Zx1R7AhTbyGSALm44/Mmq+BAPc4B/p/d3M=
github.com/nwaples/rardecode v1.1.2/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0= github.com/nwaples/rardecode v1.1.2/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=
@ -507,6 +507,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js= github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=
github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0= github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=

View file

@ -7,15 +7,16 @@ 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"
"github.com/davecgh/go-spew/spew" "git.minhas.io/asara/well-goknown/redis"
"github.com/lightninglabs/lndclient" "github.com/lightninglabs/lndclient"
"github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc"
) )
func Request(rKey string, request []byte) (string, error) { func Request(rKey string) (string, error) {
l := logger.Get() l := logger.Get()
ctx := context.TODO() ctx := context.TODO()
// connect to lnd // connect to lnd
@ -33,8 +34,9 @@ func Request(rKey string, request []byte) (string, error) {
addrFee, err := strconv.ParseInt(config.GetConfig().NostrAddrFee, 10, 64) addrFee, err := strconv.ParseInt(config.GetConfig().NostrAddrFee, 10, 64)
if err != nil { if err != nil {
l.Fatal().Msg("nostr address fee not set properlly") l.Fatal().Msg("nostr address fee not set properlly")
return "", errors.New("nostr address fee not set properly") return "", errors.New("internal server error")
} }
info, err := lndCli.AddInvoice(ctx, &lnrpc.Invoice{ info, err := lndCli.AddInvoice(ctx, &lnrpc.Invoice{
Memo: fmt.Sprintf("nostr addr %s", rKey), Memo: fmt.Sprintf("nostr addr %s", rKey),
Expiry: 1 * 60, Expiry: 1 * 60,
@ -42,9 +44,18 @@ func Request(rKey string, request []byte) (string, error) {
}) })
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")
} }
spew.Dump(hex.EncodeToString(info.RHash)) rHash := hex.EncodeToString(info.RHash)
spew.Dump(info.PaymentRequest) paymentReq := info.PaymentRequest
spew.Dump(request)
return "x", nil // write lnd request to redis
redisCli := redis.LndRedisConn.Client
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")
}
return paymentReq, nil
} }

View file

@ -8,11 +8,13 @@ import (
"fmt" "fmt"
"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"
"git.minhas.io/asara/well-goknown/redis" "git.minhas.io/asara/well-goknown/redis"
"github.com/nbd-wtf/go-nostr/nip19" "github.com/nbd-wtf/go-nostr/nip19"
"github.com/skip2/go-qrcode"
) )
type nostrWellKnown struct { type nostrWellKnown struct {
@ -86,8 +88,7 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
enc := b64.StdEncoding.EncodeToString([]byte(jsonUser)) enc := b64.StdEncoding.EncodeToString([]byte(jsonUser))
// generate the payment request // generate the payment request
// paymentReq, err := lnd.Request(rKey, jsonUser) paymentReq, err := lnd.Request(rKey)
_, err = lnd.Request(rKey, jsonUser)
if err != nil { if err != nil {
w.WriteHeader(http.StatusServiceUnavailable) w.WriteHeader(http.StatusServiceUnavailable)
json.NewEncoder(w).Encode(&jsonErrorMessage{ json.NewEncoder(w).Encode(&jsonErrorMessage{
@ -95,11 +96,20 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
}) })
return return
} }
requestKey := getRkey("requested", r.FormValue("Name"), getHostname(r.Host))
err = redisCli.Set(ctx, requestKey, enc, 0).Err() // generate qr png
png, err := qrcode.Encode(paymentReq, qrcode.Medium, 256)
// write request to redis
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")
} }
// return qr code
w.Header().Set("Content-Type", "image/png")
w.WriteHeader(http.StatusOK)
w.Write(png)
} }
} }