Merge pull request 'Add nostr nip-05 registration with lnd invoices' (#1) from nostr_lnd into main

Reviewed-on: https://git.minhas.io/Asara/well-goknown/pulls/1
This commit is contained in:
Amarpreet Minhas 2023-05-30 00:10:36 +00:00
commit 6768713682
14 changed files with 1872 additions and 1 deletions

4
.gitignore vendored
View file

@ -21,3 +21,7 @@
# Go workspace file
go.work
# binary outputs
well-goknown
cmd/lnd-verifier/lnd-verifier

View file

@ -1,3 +1,5 @@
# well-goknown
Server to manage and dynamically add/remove `.well-known` entries.
Server to manage and dynamically add/remove `.well-known` entries.
Requires redis

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

@ -0,0 +1,95 @@
package main
import (
"context"
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/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 keyspace for easy bootstrap/general laziness
nostrTest := nostr.NostrRequest{
Name: "asara",
Key: "npub19hhggqd5zpmmddv9dvu2qq0ne5pn7f884v4dmku3llp4s44xaqzsl0vms7",
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")
}
// set up redis connections
lndRedisCli := redis.LndRedisConn.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()
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.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 during invoice lookup", key))
continue
}
}
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
}
l.Info().Msg(fmt.Sprintf("%s: invoice still unpaid", key))
continue
}
}

38
config/config.go Normal file
View file

@ -0,0 +1,38 @@
package config
import (
"os"
)
type (
Config struct {
ListenAddr string
LndCertB64 string
LndAddr string
LndMacaroonHex string
LogLevel string
MatrixIdentityServer string
MatrixWellKnownAddress string
NostrAddrFee string
}
)
func GetConfig() Config {
return Config{
ListenAddr: getEnv("LISTEN_ADDR", ":8090"),
LndCertB64: getEnv("LND_CERT_B64", ""),
LndAddr: getEnv("LND_ADDR", ""),
LndMacaroonHex: getEnv("LND_MACAROON_HEX", ""),
LogLevel: getEnv("LOG_LEVEL", "INFO"),
MatrixIdentityServer: getEnv("MATRIX_IDENTITY_SERVER", ""),
MatrixWellKnownAddress: getEnv("MATRIX_WELL_KNOWN_ADDRESS", ""),
NostrAddrFee: getEnv("NOSTR_ADDR_FEE", "0"),
}
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}

171
go.mod Normal file
View file

@ -0,0 +1,171 @@
module git.minhas.io/asara/well-goknown
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.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 (
github.com/SaveTheRbtz/generic-sync-map-go v0.0.0-20220414055132-a37292614db8 // indirect
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
github.com/aead/siphash v1.0.1 // indirect
github.com/andybalholm/brotli v1.0.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd v0.23.5-0.20230125025938-be056b0a0b2f // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
github.com/btcsuite/btcd/btcutil/psbt v1.1.5 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f // indirect
github.com/btcsuite/btcwallet v0.16.7 // indirect
github.com/btcsuite/btcwallet/wallet/txauthor v1.3.2 // indirect
github.com/btcsuite/btcwallet/wallet/txrules v1.2.0 // indirect
github.com/btcsuite/btcwallet/wallet/txsizes v1.2.3 // indirect
github.com/btcsuite/btcwallet/walletdb v1.4.0 // indirect
github.com/btcsuite/btcwallet/wtxmgr v1.5.0 // indirect
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd // indirect
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 // indirect
github.com/btcsuite/winsvc v1.0.0 // indirect
github.com/cenkalti/backoff/v4 v4.1.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/coreos/go-semver v0.3.0 // 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/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/decred/dcrd/lru v1.0.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dsnet/compress v0.0.1 // indirect
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
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.5.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.10.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.1.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.8.1 // indirect
github.com/jackc/pgx/v4 v4.13.0 // indirect
github.com/jessevdk/go-flags v1.4.0 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/jrick/logrotate v1.0.0 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/juju/loggo v0.0.0-20210728185423-eebad3a902c4 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kkdai/bstream v1.0.0 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/klauspost/pgzip v1.2.5 // indirect
github.com/lib/pq v1.10.3 // indirect
github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf // indirect
github.com/lightninglabs/neutrino v0.15.0 // indirect
github.com/lightninglabs/neutrino/cache v1.1.1 // indirect
github.com/lightningnetwork/lightning-onion v1.2.1-0.20221202012345-ca23184850a1 // indirect
github.com/lightningnetwork/lnd/clock v1.1.0 // indirect
github.com/lightningnetwork/lnd/healthcheck v1.2.2 // indirect
github.com/lightningnetwork/lnd/kvdb v1.4.1 // indirect
github.com/lightningnetwork/lnd/queue v1.1.0 // indirect
github.com/lightningnetwork/lnd/ticker v1.1.0 // indirect
github.com/lightningnetwork/lnd/tlv v1.1.0 // indirect
github.com/lightningnetwork/lnd/tor v1.1.0 // indirect
github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mholt/archiver/v3 v3.5.0 // indirect
github.com/miekg/dns v1.1.43 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/nwaples/rardecode v1.1.2 // indirect
github.com/pierrec/lz4/v4 v4.1.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/rogpeppe/fastuuid v1.2.0 // indirect
github.com/sirupsen/logrus v1.7.0 // indirect
github.com/soheilhy/cmux v0.1.5 // indirect
github.com/spf13/pflag v1.0.5 // indirect
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
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
go.etcd.io/etcd/api/v3 v3.5.7 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.7 // indirect
go.etcd.io/etcd/client/v2 v2.305.7 // indirect
go.etcd.io/etcd/client/v3 v3.5.7 // indirect
go.etcd.io/etcd/pkg/v3 v3.5.7 // indirect
go.etcd.io/etcd/raft/v3 v3.5.7 // indirect
go.etcd.io/etcd/server/v3 v3.5.7 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.25.0 // indirect
go.opentelemetry.io/otel v1.0.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.0.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.0.1 // indirect
go.opentelemetry.io/otel/sdk v1.0.1 // indirect
go.opentelemetry.io/otel/trace v1.0.1 // indirect
go.opentelemetry.io/proto/otlp v0.9.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/exp v0.0.0-20221111094246-ab4555d3164f // indirect
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.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/protobuf v1.27.1 // indirect
gopkg.in/errgo.v1 v1.0.1 // indirect
gopkg.in/macaroon-bakery.v2 v2.0.1 // indirect
gopkg.in/macaroon.v2 v2.1.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.40.0 // indirect
modernc.org/ccgo/v3 v3.16.13 // indirect
modernc.org/libc v1.22.2 // indirect
modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.4.0 // indirect
modernc.org/opt v0.1.3 // indirect
modernc.org/sqlite v1.20.3 // indirect
modernc.org/strutil v1.1.3 // indirect
modernc.org/token v1.0.1 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)

1029
go.sum Normal file

File diff suppressed because it is too large Load diff

19
html/nostr_form.html Normal file
View file

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<div>
<form method="POST" action="/request/nostr">
<label>Username</label><br>
<input name="Name" type="text" value="" /><br>
<label>Key in npub format</label><br>
<input name="Key" type="text" value="" /><br>
<label>Relays as comma seperated list (optional)</label><br>
<input name="Relays" type="text" value="" /><br>
<input type="submit" value="submit" />
</form>
</div>
</body>
</html>

View file

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<div>
<p>Nostr NIP-05 {{ .RequestKey }}</p><br>
<img src="data:image/png;base64,{{.QRCode}}"/><br>
</div>
</body>
</html>

65
lnd/lnd.go Normal file
View file

@ -0,0 +1,65 @@
package lnd
import (
"context"
b64 "encoding/base64"
"encoding/hex"
"errors"
"fmt"
"strconv"
"git.minhas.io/asara/well-goknown/config"
"git.minhas.io/asara/well-goknown/logger"
"git.minhas.io/asara/well-goknown/redis"
"github.com/lightninglabs/lndclient"
"github.com/lightningnetwork/lnd/lnrpc"
)
func Request(rKey string) (string, error) {
l := logger.Get()
ctx := context.TODO()
// connect to lnd
// TODO: move to its own function and don't connect on every request
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.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.Error().Msg("nostr address fee not set properlly")
return "", errors.New("internal server error, please try again later")
}
expiryInMin := int64(5)
invoice, err := lndCli.AddInvoice(ctx, &lnrpc.Invoice{
Memo: fmt.Sprintf("nostr addr %s", rKey),
Expiry: expiryInMin * 60,
Value: addrFee,
})
if err != nil {
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
paymentReq := invoice.PaymentRequest
rHash := hex.EncodeToString(invoice.RHash)
// write lnd request to redis
redisCli := redis.LndRedisConn.Client
err = redisCli.Set(ctx, rKey, rHash, 0).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
}

40
logger/logger.go Normal file
View file

@ -0,0 +1,40 @@
package logger
import (
"io"
"os"
"strconv"
"sync"
"time"
"git.minhas.io/asara/well-goknown/config"
"github.com/rs/zerolog"
)
var once sync.Once
var log zerolog.Logger
func Get() zerolog.Logger {
once.Do(func() {
zerolog.TimeFieldFormat = time.RFC3339Nano
logLevel, err := strconv.Atoi(config.GetConfig().LogLevel)
if err != nil {
logLevel = int(zerolog.InfoLevel) // default to INFO
}
var output io.Writer = zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: time.RFC3339,
}
log = zerolog.New(output).
Level(zerolog.Level(logLevel)).
With().
Timestamp().
Logger()
})
return log
}

42
main.go Normal file
View file

@ -0,0 +1,42 @@
package main
import (
"net/http"
"git.minhas.io/asara/well-goknown/config"
"git.minhas.io/asara/well-goknown/logger"
"git.minhas.io/asara/well-goknown/matrix"
"git.minhas.io/asara/well-goknown/nostr"
"git.minhas.io/asara/well-goknown/redis"
)
func main() {
var err error
l := logger.Get()
// connect to redis
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")
}
// matrix endpoints
l.Debug().Msg("enabling matrix server endpoint")
http.HandleFunc("/.well-known/matrix/server", matrix.MatrixServer)
l.Debug().Msg("enabling matrix client endpoint")
http.HandleFunc("/.well-known/matrix/client", matrix.MatrixClient)
// nostr endpoints
l.Debug().Msg("enabling nostr user endpoint")
http.HandleFunc("/.well-known/nostr.json", nostr.GetNostrAddr)
l.Debug().Msg("enabling nostr request endpoint")
http.HandleFunc("/request/nostr", nostr.RequestNostrAddr)
// start server
port := config.GetConfig().ListenAddr
l.Info().Msgf("starting server on %s", port)
http.ListenAndServe(port, nil)
}

81
matrix/matrix.go Normal file
View file

@ -0,0 +1,81 @@
package matrix
import (
"encoding/json"
"fmt"
"net/http"
"git.minhas.io/asara/well-goknown/config"
"git.minhas.io/asara/well-goknown/logger"
)
type matrixServerWellKnown struct {
WellKnownAddress string `json:"m.server"`
}
type matrixBaseUrl struct {
BaseUrl string `json:"base_url"`
}
type matrixIdentityServer struct {
BaseUrl string `json:"base_url,omitempty"`
}
type matrixClientWellKnown struct {
HomeServer *matrixBaseUrl `json:"m.homeserver"`
IdentityServer *matrixIdentityServer `json:"m.identity_server,omitempty"`
}
func MatrixServer(w http.ResponseWriter, req *http.Request) {
l := logger.Get()
// uses an environment variable for now
wellKnownAddr := config.GetConfig().MatrixWellKnownAddress
if wellKnownAddr == "" {
w.WriteHeader(http.StatusNotFound)
l.Debug().Str("path", "matrix/server").Msg("matrix well known address unset")
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
wellKnown := &matrixServerWellKnown{
WellKnownAddress: wellKnownAddr,
}
json.NewEncoder(w).Encode(wellKnown)
}
func MatrixClient(w http.ResponseWriter, req *http.Request) {
l := logger.Get()
// uses an environment variable for now
wellKnownAddr := config.GetConfig().MatrixWellKnownAddress
if wellKnownAddr == "" {
w.WriteHeader(http.StatusNotFound)
l.Debug().Str("path", "matrix/client").Msg("matrix well known address unset")
return
}
identityServer := config.GetConfig().MatrixIdentityServer
if identityServer == "" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
baseUrl := &matrixClientWellKnown{
HomeServer: &matrixBaseUrl{
BaseUrl: fmt.Sprintf("https://%s", wellKnownAddr),
},
}
json.NewEncoder(w).Encode(baseUrl)
} else {
identityServer = fmt.Sprintf("https://%s", identityServer)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
baseUrl := &matrixClientWellKnown{
HomeServer: &matrixBaseUrl{
BaseUrl: fmt.Sprintf("https://%s", wellKnownAddr),
},
IdentityServer: &matrixIdentityServer{
BaseUrl: identityServer,
},
}
json.NewEncoder(w).Encode(baseUrl)
}
}

237
nostr/nostr.go Normal file
View file

@ -0,0 +1,237 @@
package nostr
import (
"context"
b64 "encoding/base64"
"encoding/json"
"errors"
"fmt"
"html/template"
"net/http"
"strings"
"git.minhas.io/asara/well-goknown/lnd"
"git.minhas.io/asara/well-goknown/logger"
"git.minhas.io/asara/well-goknown/redis"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/skip2/go-qrcode"
)
type nostrWellKnown struct {
Names map[string]string `json:"names"`
Relays map[string][]string `json:"relays,omitempty"`
}
type jsonErrorMessage struct {
Error string `json:"error"`
}
type NostrRequest struct {
Name string `json:"name"`
Key string `json:"key"`
Hostname string
Relays []string `json:"relays,omitempty"`
}
type NostrResponse struct {
RequestKey string `json:"request_key"`
QRCode string `json:"QRCode"`
}
func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
ctx := context.TODO()
l := logger.Get()
switch r.Method {
case "GET":
http.ServeFile(w, r, "html/nostr_form.html")
case "POST":
r.ParseForm()
redisCli := redis.NostrRedisConn.Client
// check if the user already exists
rKey := getRkey("verified", r.FormValue("Name"), getHostname(r.Host))
exists := redisCli.Exists(ctx, rKey)
if exists.Val() == 1 {
w.WriteHeader(http.StatusConflict)
json.NewEncoder(w).Encode(&jsonErrorMessage{
Error: "username already registered",
})
return
}
rKey = getRkey("requested", r.FormValue("Name"), getHostname(r.Host))
exists = redisCli.Exists(ctx, rKey)
if exists.Val() == 1 {
w.WriteHeader(http.StatusConflict)
json.NewEncoder(w).Encode(&jsonErrorMessage{
Error: "username already requested, try again in a few minutes",
})
return
}
// get the hexkey
hexKey, err := convertNpubToHex(r.FormValue("Key"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(&jsonErrorMessage{
Error: err.Error(),
})
return
}
// create the struct
relays := make(map[string][]string)
names := map[string]string{r.FormValue("Name"): hexKey}
user := nostrWellKnown{}
if r.FormValue("Relays") != "" {
relays = map[string][]string{
hexKey: strings.Split(r.FormValue("Relays"), ","),
}
user = nostrWellKnown{Names: names, Relays: relays}
} else {
user = nostrWellKnown{Names: names}
}
jsonUser, _ := json.Marshal(user)
enc := b64.StdEncoding.EncodeToString([]byte(jsonUser))
// generate the payment request
paymentReq, err := lnd.Request(rKey)
if err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
json.NewEncoder(w).Encode(&jsonErrorMessage{
Error: "service unavailable",
})
return
}
// generate qr png
png, err := qrcode.Encode(paymentReq, qrcode.Medium, 256)
pngB64 := b64.StdEncoding.EncodeToString(png)
// write request to redis
err = redisCli.Set(ctx, rKey, enc, 0).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
response := NostrResponse{
RequestKey: rKey,
QRCode: pngB64,
}
tmplt, err := template.ParseFiles("html/nostr_qr_response.html")
if err != nil {
l.Error().Msg("unable to parse qr response template")
w.WriteHeader(http.StatusServiceUnavailable)
json.NewEncoder(w).Encode(&jsonErrorMessage{
Error: "service unavailable",
})
return
}
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
}
}
func GetNostrAddr(w http.ResponseWriter, r *http.Request) {
ctx := context.TODO()
l := logger.Get()
// get query string for username
r.ParseForm()
requestedName := r.FormValue("name")
// connect to redis
redisCli := redis.NostrRedisConn.Client
// search for user@domain
search := getRkey("verified", requestedName, getHostname(r.Host))
addr, err := redisCli.Get(ctx, search).Result()
if err != nil {
l.Info().Msg(fmt.Sprintf("get %s: not found", search))
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(&jsonErrorMessage{
Error: "username not registered",
})
return
}
dec, err := b64.StdEncoding.DecodeString(addr)
if err != nil {
l.Error().Msg(fmt.Sprintf("get %s: unable to decode key", search))
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(&jsonErrorMessage{
Error: "internal server error",
})
return
}
l.Info().Msg(fmt.Sprintf("get %s: found and returned", search))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(dec)
}
func AddNostrAddr(n NostrRequest) {
ctx := context.TODO()
l := logger.Get()
// transform request to well known struct
relays := make(map[string][]string)
user := nostrWellKnown{}
hexKey, err := convertNpubToHex(n.Key)
if err != nil {
l.Error().Msg("unable to convert npub to hex")
}
names := map[string]string{n.Name: hexKey}
if n.Relays != nil {
relays = map[string][]string{hexKey: n.Relays}
user = nostrWellKnown{Names: names, Relays: relays}
} else {
user = nostrWellKnown{Names: names}
}
jsonUser, _ := json.Marshal(user)
redisCli := redis.NostrRedisConn.Client
nameKey := getRkey("verified", n.Name, n.Hostname)
enc := b64.StdEncoding.EncodeToString([]byte(jsonUser))
err = redisCli.Set(ctx, nameKey, enc, 0).Err()
if err != nil {
l.Error().Msg("unable to connect to redis")
}
}
func getHostname(hostname string) string {
// remove port for local testing
return hostname
}
func convertNpubToHex(npub string) (string, error) {
if !strings.HasPrefix(npub, "npub1") {
return "", errors.New("key does not start with npub1 prefix")
}
_, hex, err := nip19.Decode(npub)
if err != nil {
return "", errors.New("unable to decode npub key")
}
return hex.(string), nil
}
func getRkey(prefix string, user string, hostname string) string {
if strings.Contains(hostname, ":") {
hostname = strings.Split(hostname, ":")[0]
}
return fmt.Sprintf("%s:%s@%s", prefix, user, hostname)
}

36
redis/redis.go Normal file
View file

@ -0,0 +1,36 @@
package redis
import (
"context"
"github.com/redis/go-redis/v9"
)
const (
NostrKeyspace = iota
LndKeyspace = iota
)
type Redis struct {
Client *redis.Client
}
var (
NostrRedisConn *Redis
LndRedisConn *Redis
)
func New(address string, password string, database int) (*Redis, error) {
client := redis.NewClient(&redis.Options{
Addr: address,
Password: password,
DB: database,
})
ctx := context.TODO()
if err := client.Ping(ctx).Err(); err != nil {
return nil, err
}
return &Redis{
Client: client,
}, nil
}