From 4aee45c86f080cb29c952391838835edbc274742 Mon Sep 17 00:00:00 2001 From: Asara Date: Mon, 15 May 2023 20:37:40 -0400 Subject: [PATCH] finish nostr flow --- cmd/lnd-verifier/main.go | 23 +++++++++++++++-------- go.mod | 12 +++++++++--- go.sum | 20 ++++++++++++++++---- lnd/lnd.go | 11 ++++++----- nostr/nostr.go | 1 - 5 files changed, 46 insertions(+), 21 deletions(-) diff --git a/cmd/lnd-verifier/main.go b/cmd/lnd-verifier/main.go index d7a40e4..b102d7d 100644 --- a/cmd/lnd-verifier/main.go +++ b/cmd/lnd-verifier/main.go @@ -5,11 +5,11 @@ import ( b64 "encoding/base64" "encoding/hex" "fmt" + "strings" "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" @@ -47,10 +47,11 @@ func main() { l.Fatal().Msg("unable to connect to lnd") } - // get requested keys + // set up redis connections lndRedisCli := redis.LndRedisConn.Client - //nostrRedisCli := redis.NostrRedisConn.Client + nostrRedisCli := redis.NostrRedisConn.Client + // get requested keys requestedKeys := lndRedisCli.Keys(ctx, "requested:*") for _, key := range requestedKeys.Val() { value, err := lndRedisCli.Get(ctx, key).Result() @@ -68,10 +69,12 @@ func main() { if err != nil { switch status.Code(err) { case codes.NotFound: - l.Error().Msg(fmt.Sprintf("%s: expired", key)) + l.Info().Msg(fmt.Sprintf("%s: payment request expired", key)) + lndRedisCli.Del(ctx, "requested:%s", key) + nostrRedisCli.Del(ctx, "requested:%s", key) continue default: - l.Error().Msg(fmt.Sprintf("%s: unknown error", key)) + l.Error().Msg(fmt.Sprintf("%s: unknown error during invoice lookup", key)) continue } } @@ -79,10 +82,14 @@ func main() { if invoice.Settled { // move requested to verified on nostr redis keyspace // delete current key + verified := fmt.Sprintf("verified:%s", strings.Split(key, ":")[1]) + nostrRedisCli.Rename(ctx, key, verified) + lndRedisCli.Del(ctx, "requested:%s", key) + l.Info().Msg(fmt.Sprintf("%s: invoice paid, nip-05 verified", key)) continue } - spew.Dump(invoice) - spew.Dump(key) - spew.Dump(value) + + l.Info().Msg(fmt.Sprintf("%s: invoice still unpaid", key)) + continue } } diff --git a/go.mod b/go.mod index 858c08b..04d099a 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,11 @@ go 1.19 require ( github.com/lightninglabs/lndclient v0.16.0-10 github.com/lightningnetwork/lnd v0.16.0-beta - github.com/nbd-wtf/go-nostr v0.17.3 + github.com/nbd-wtf/go-nostr v0.18.3 github.com/redis/go-redis/v9 v9.0.4 github.com/rs/zerolog v1.15.0 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e + google.golang.org/grpc v1.41.0 ) require ( @@ -45,6 +46,9 @@ require ( github.com/dustin/go-humanize v1.0.0 // indirect github.com/fergusstrange/embedded-postgres v1.10.0 // indirect github.com/go-errors/errors v1.0.1 // indirect + github.com/gobwas/httphead v0.1.0 // indirect + github.com/gobwas/pool v0.2.1 // indirect + github.com/gobwas/ws v1.2.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang-jwt/jwt/v4 v4.4.2 // indirect github.com/golang/protobuf v1.5.2 // indirect @@ -109,6 +113,9 @@ require ( github.com/stretchr/objx v0.5.0 // indirect github.com/stretchr/testify v1.8.1 // indirect github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect + github.com/tidwall/gjson v1.14.4 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 // indirect github.com/ulikunitz/xz v0.5.10 // indirect github.com/xdg-go/stringprep v1.0.4 // indirect @@ -137,13 +144,12 @@ require ( golang.org/x/mod v0.6.0 // indirect golang.org/x/net v0.4.0 // indirect golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect - golang.org/x/sys v0.3.0 // indirect + golang.org/x/sys v0.6.0 // indirect golang.org/x/term v0.3.0 // indirect golang.org/x/text v0.5.0 // indirect golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect golang.org/x/tools v0.2.0 // indirect google.golang.org/genproto v0.0.0-20210617175327-b9e0b3197ced // indirect - google.golang.org/grpc v1.41.0 // indirect google.golang.org/protobuf v1.27.1 // indirect gopkg.in/errgo.v1 v1.0.1 // indirect gopkg.in/macaroon-bakery.v2 v2.0.1 // indirect diff --git a/go.sum b/go.sum index 8230a46..43fd4e4 100644 --- a/go.sum +++ b/go.sum @@ -187,6 +187,12 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9 github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU= +github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM= +github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= +github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= +github.com/gobwas/ws v1.2.0 h1:u0p9s3xLYpZCA1z5JgCkMeB34CKCMMQbM+G8Ii7YD0I= +github.com/gobwas/ws v1.2.0/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/KY= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0= @@ -440,8 +446,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/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/nbd-wtf/go-nostr v0.17.3 h1:OG/85OgjandmHfnVZmr/qsnMPbkVaIaJyXHMBI3ywIE= -github.com/nbd-wtf/go-nostr v0.17.3/go.mod h1:YCDHJtaFQE76d1ZkcUsTkz3dYNP+bldo5CIQwXPPcbk= +github.com/nbd-wtf/go-nostr v0.18.3 h1:ofMYxlFAptyoErlOGOCUk7zGHQNJ8/ZkIXXOsveFZ+c= +github.com/nbd-wtf/go-nostr v0.18.3/go.mod h1:GPJOOK8US38kz+bfb9nWe873Xu0e6bXlThejOs1LTkc= 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/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0= @@ -532,6 +538,12 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc= +github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= +github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 h1:uruHq4dN7GR16kFc5fp3d1RIYzJW5onx8Ybykw2YQFA= github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8= @@ -776,8 +788,8 @@ golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= -golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= diff --git a/lnd/lnd.go b/lnd/lnd.go index 369f53e..18f24fe 100644 --- a/lnd/lnd.go +++ b/lnd/lnd.go @@ -26,14 +26,15 @@ func Request(rKey string) (string, error) { 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") + l.Error().Msg("unable to connect to lnd") + return "", errors.New("internal server error, please try again later") } // add invoice addrFee, err := strconv.ParseInt(config.GetConfig().NostrAddrFee, 10, 64) if err != nil { - l.Fatal().Msg("nostr address fee not set properlly") - return "", errors.New("internal server error") + l.Error().Msg("nostr address fee not set properlly") + return "", errors.New("internal server error, please try again later") } expiryInMin := int64(5) @@ -43,8 +44,8 @@ func Request(rKey string) (string, error) { Value: addrFee, }) if err != nil { - l.Fatal().Msg("unable to create lnd invoice") - return "", errors.New("internal server error") + l.Error().Msg("unable to create lnd invoice") + return "", errors.New("internal server error, please try again later") } // add a minute to the expiry time to ensure entries aren't removed from redis before they are expired diff --git a/nostr/nostr.go b/nostr/nostr.go index 59a4a2a..7f691e1 100644 --- a/nostr/nostr.go +++ b/nostr/nostr.go @@ -43,7 +43,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()