Compare commits

...

2 commits

18 changed files with 222 additions and 1704 deletions

5
.gitignore vendored
View file

@ -21,7 +21,8 @@
# Go workspace file # Go workspace file
go.work go.work
# binary outputs # binary outputs
well-goknown well-goknown
cmd/lnd-verifier/lnd-verifier
# dev environment vars
dev.env

View file

@ -1,5 +1,19 @@
# well-goknown # well-goknown
Server to manage and dynamically add/remove `.well-known` entries. Server to manage and dynamically add/remove `.well-known` entries.
Requires postgres 15+
Requires redis ## Pre-requisites
```
apt install podman
podman pull docker.io/library/postgres:15
podman run -e POSTGRES_PASSWORD=x -p 5432:5432 --name wg-psql docker.io/library/postgres:15
# setup postgres
PGPASSWORD=x psql -d postgres -U postgres -h 127.0.0.1 << EOF
CREATE DATABASE wgk;
CREATE USER wgk WITH ENCRYPTED PASSWORD 'x';
GRANT ALL PRIVILEGES ON DATABASE wgk TO wgk;
ALTER DATABASE wgk OWNER TO wgk;
EOF
```

View file

@ -1,95 +0,0 @@
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
}
}

View file

@ -6,29 +6,23 @@ import (
type ( type (
Config struct { Config struct {
DbUrl string
ListenAddr string ListenAddr string
LndCertB64 string
LndAddr string
LndMacaroonHex string
LogLevel string LogLevel string
MatrixIdentityServer string MatrixIdentityServer string
MatrixWellKnownAddress string
MatrixMsc3575Address string MatrixMsc3575Address string
NostrAddrFee string MatrixWellKnownAddress string
} }
) )
func GetConfig() Config { func GetConfig() Config {
return Config{ return Config{
DbUrl: getEnv("DATABASE_URL", "postgres://user:password@127.0.0.1:5432/db?sslmode=disable"),
ListenAddr: getEnv("LISTEN_ADDR", ":8090"), ListenAddr: getEnv("LISTEN_ADDR", ":8090"),
LndCertB64: getEnv("LND_CERT_B64", ""),
LndAddr: getEnv("LND_ADDR", ""),
LndMacaroonHex: getEnv("LND_MACAROON_HEX", ""),
LogLevel: getEnv("LOG_LEVEL", "INFO"), LogLevel: getEnv("LOG_LEVEL", "INFO"),
MatrixIdentityServer: getEnv("MATRIX_IDENTITY_SERVER", ""), MatrixIdentityServer: getEnv("MATRIX_IDENTITY_SERVER", ""),
MatrixWellKnownAddress: getEnv("MATRIX_WELL_KNOWN_ADDRESS", ""),
MatrixMsc3575Address: getEnv("MATRIX_MSC3575_ADDRESS", ""), MatrixMsc3575Address: getEnv("MATRIX_MSC3575_ADDRESS", ""),
NostrAddrFee: getEnv("NOSTR_ADDR_FEE", "0"), MatrixWellKnownAddress: getEnv("MATRIX_WELL_KNOWN_ADDRESS", ""),
} }
} }

23
db/db.go Normal file
View file

@ -0,0 +1,23 @@
package db
import (
"git.minhas.io/asara/gologger"
"git.minhas.io/asara/well-goknown/config"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
func NewDB() (*sqlx.DB, error) {
l := gologger.Get(config.GetConfig().LogLevel).With().Str("context", "db").Logger()
db, err := sqlx.Open("postgres", config.GetConfig().DbUrl)
if err != nil {
l.Panic().Msg(err.Error())
}
err = db.Ping()
if err != nil {
l.Panic().Msg(err.Error())
}
l.Debug().Msg("connected to database")
return db, nil
}

167
go.mod
View file

@ -3,169 +3,14 @@ module git.minhas.io/asara/well-goknown
go 1.19 go 1.19
require ( require (
github.com/lightninglabs/lndclient v0.16.0-10 git.minhas.io/asara/gologger v0.7.0
github.com/lightningnetwork/lnd v0.16.0-beta github.com/jmoiron/sqlx v1.4.0
github.com/nbd-wtf/go-nostr v0.18.3 github.com/lib/pq v1.10.9
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 ( require (
github.com/SaveTheRbtz/generic-sync-map-go v0.0.0-20220414055132-a37292614db8 // indirect github.com/mattn/go-colorable v0.1.12 // indirect
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect github.com/mattn/go-isatty v0.0.14 // indirect
github.com/aead/siphash v1.0.1 // indirect github.com/rs/zerolog v1.30.0 // 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/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
) )

1047
go.sum

File diff suppressed because it is too large Load diff

View file

@ -1,19 +0,0 @@
<!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

@ -1,12 +0,0 @@
<!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>

View file

@ -1,65 +0,0 @@
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
}

View file

@ -1,40 +0,0 @@
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
}

31
main.go
View file

@ -3,42 +3,29 @@ package main
import ( import (
"net/http" "net/http"
"git.minhas.io/asara/gologger"
"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/db"
"git.minhas.io/asara/well-goknown/matrix" "git.minhas.io/asara/well-goknown/matrix"
"git.minhas.io/asara/well-goknown/nostr" "git.minhas.io/asara/well-goknown/nostr"
"git.minhas.io/asara/well-goknown/redis"
) )
func main() { func main() {
var err error l := gologger.Get(config.GetConfig().LogLevel).With().Str("context", "main").Logger()
l := logger.Get() db, _ := db.NewDB()
// connect to redis defer db.Close()
redis.NostrRedisConn, err = redis.New("localhost:6379", "", redis.NostrKeyspace)
if err != nil { nostr.DB = db
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 // matrix endpoints
l.Debug().Msg("enabling matrix server endpoint") l.Debug().Msg("enabling matrix well-known endpoints")
http.HandleFunc("/.well-known/matrix/server", matrix.MatrixServer) http.HandleFunc("/.well-known/matrix/server", matrix.MatrixServer)
l.Debug().Msg("enabling matrix client endpoint")
http.HandleFunc("/.well-known/matrix/client", matrix.MatrixClient) http.HandleFunc("/.well-known/matrix/client", matrix.MatrixClient)
// nostr endpoints // nostr endpoints
l.Debug().Msg("enabling nostr user endpoint") l.Debug().Msg("enabling nostr well-known endpoints")
http.HandleFunc("/.well-known/nostr.json", nostr.GetNostrAddr) http.HandleFunc("/.well-known/nostr.json", nostr.GetNostrAddr)
addr := config.GetConfig().LndAddr
if addr != "" {
l.Debug().Msg("enabling nostr request endpoint")
http.HandleFunc("/request/nostr", nostr.RequestNostrAddr)
}
// start server // start server
port := config.GetConfig().ListenAddr port := config.GetConfig().ListenAddr
l.Info().Msgf("starting server on %s", port) l.Info().Msgf("starting server on %s", port)

View file

@ -5,8 +5,8 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"git.minhas.io/asara/gologger"
"git.minhas.io/asara/well-goknown/config" "git.minhas.io/asara/well-goknown/config"
"git.minhas.io/asara/well-goknown/logger"
) )
type matrixServerWellKnown struct { type matrixServerWellKnown struct {
@ -32,7 +32,7 @@ type matrixClientWellKnown struct {
} }
func MatrixServer(w http.ResponseWriter, req *http.Request) { func MatrixServer(w http.ResponseWriter, req *http.Request) {
l := logger.Get() l := gologger.Get(config.GetConfig().LogLevel).With().Str("context", "matrix-server").Logger()
// uses an environment variable for now // uses an environment variable for now
wellKnownAddr := fmt.Sprintf("%s:8448", config.GetConfig().MatrixWellKnownAddress) wellKnownAddr := fmt.Sprintf("%s:8448", config.GetConfig().MatrixWellKnownAddress)
if wellKnownAddr == "" { if wellKnownAddr == "" {
@ -50,7 +50,7 @@ func MatrixServer(w http.ResponseWriter, req *http.Request) {
} }
func MatrixClient(w http.ResponseWriter, req *http.Request) { func MatrixClient(w http.ResponseWriter, req *http.Request) {
l := logger.Get() l := gologger.Get(config.GetConfig().LogLevel).With().Str("context", "matrix-client").Logger()
m := &matrixClientWellKnown{} m := &matrixClientWellKnown{}
wellKnownAddr := config.GetConfig().MatrixWellKnownAddress wellKnownAddr := config.GetConfig().MatrixWellKnownAddress

View file

@ -0,0 +1,8 @@
BEGIN;
DROP TRIGGER IF EXISTS update_nip05s_update_ts on nip05s;
DROP FUNCTION IF EXISTS nip05s_update_ts();
DROP TABLE IF EXISTS nip05s;
DROP TRIGGER IF EXISTS update_users_update_ts on users;
DROP FUNCTION IF EXISTS users_update_ts();
DROP TABLE IF EXISTS users;
COMMIT;

View file

@ -0,0 +1,55 @@
BEGIN;
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
email TEXT UNIQUE,
password TEXT NOT NULL,
enabled BOOLEAN DEFAULT false,
admin BOOLEAN DEFAULT false,
pubkey TEXT UNIQUE NOT NULL,
relays TEXT DEFAULT NULL,
create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
update_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);
CREATE FUNCTION users_update_ts()
RETURNS TRIGGER AS $$
BEGIN
NEW.update_ts = now();
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE TRIGGER update_users_update_ts
BEFORE UPDATE
ON
users
FOR EACH ROW
EXECUTE PROCEDURE users_update_ts();
CREATE TABLE IF NOT EXISTS nip05s (
id SERIAL PRIMARY KEY,
owner_id INT NOT NULL,
name TEXT UNIQUE NOT NULL,
domain TEXT NOT NULL,
create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
update_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
CONSTRAINT fk_owner FOREIGN KEY(owner_id) REFERENCES users(id),
CONSTRAINT ck_name CHECK (name ~ '^[a-z0-9]*$')
);
CREATE FUNCTION nip05s_update_ts()
RETURNS TRIGGER AS $$
BEGIN
NEW.update_ts = now();
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE TRIGGER update_nip05s_update_ts
BEFORE UPDATE
ON
nip05s
FOR EACH ROW
EXECUTE PROCEDURE nip05s_update_ts();
COMMIT;

View file

@ -1,238 +1,94 @@
package nostr package nostr
import ( import (
"context"
b64 "encoding/base64"
"encoding/json" "encoding/json"
"errors" "net"
"fmt"
"html/template"
"net/http" "net/http"
"strings" "strings"
"git.minhas.io/asara/well-goknown/lnd" "git.minhas.io/asara/gologger"
"git.minhas.io/asara/well-goknown/logger" "git.minhas.io/asara/well-goknown/config"
"git.minhas.io/asara/well-goknown/redis" "github.com/jmoiron/sqlx"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/skip2/go-qrcode"
) )
var (
DB *sqlx.DB
)
type nostrUser struct {
Pubkey string `db:"pubkey"`
Relays *string `db:"relays,omitempty"`
}
type nostrWellKnown struct { type nostrWellKnown struct {
Names map[string]string `json:"names"` Names map[string]string `json:"names"`
Relays map[string][]string `json:"relays,omitempty"` 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()
name := strings.ToLower(r.FormValue("Name"))
redisCli := redis.NostrRedisConn.Client
// check if the user already exists
rKey := getRkey("verified", 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", 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{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) { func GetNostrAddr(w http.ResponseWriter, r *http.Request) {
ctx := context.TODO() l := gologger.Get(config.GetConfig().LogLevel).With().Str("context", "nostr").Logger()
l := logger.Get()
// get query string for username // get query string for username
r.ParseForm() r.ParseForm()
requestedName := strings.ToLower(r.FormValue("name")) name := strings.ToLower(r.FormValue("name"))
// connect to redis // normalize domain
redisCli := redis.NostrRedisConn.Client domain, _, err := net.SplitHostPort(r.Host)
// search for user@domain
search := getRkey("verified", requestedName, getHostname(r.Host))
addr, err := redisCli.Get(ctx, search).Result()
if err != nil { if err != nil {
l.Info().Msg(fmt.Sprintf("get %s: not found", search)) l.Debug().Msgf("unable to split hostname %s: %s", r.Host, err.Error())
w.WriteHeader(http.StatusNotFound) domain = r.Host
json.NewEncoder(w).Encode(&jsonErrorMessage{ return
Error: "username not registered", }
})
return // get owner id for nip05 request
} var uid int
dec, err := b64.StdEncoding.DecodeString(addr) DB.QueryRow("SELECT owner_id FROM nip05s WHERE name=$1 AND domain=$2", name, domain).Scan(&uid)
if err != nil {
l.Error().Msg(fmt.Sprintf("get %s: unable to decode key", search)) // get the pubkey and relays associated with the nip05
w.WriteHeader(http.StatusInternalServerError) user := nostrUser{}
json.NewEncoder(w).Encode(&jsonErrorMessage{ err = DB.QueryRow("SELECT pubkey, relays FROM users WHERE id=$1", uid).Scan(&user.Pubkey, &user.Relays)
Error: "internal server error", if err != nil {
}) l.Error().Msgf("unable to get user for uid %v: %s", uid, err.Error())
return
}
// get all names associated with the pubkey
names := []string{}
err = DB.Select(&names, "SELECT nip05s.name FROM nip05s JOIN users ON nip05s.owner_id = users.id WHERE nip05s.owner_id = $1", uid)
if err != nil {
l.Error().Msgf("unable to get user for uid %v: %s", uid, err.Error())
return
}
// map of names
n := make(map[string]string)
for _, name := range names {
n[name] = user.Pubkey
}
// map of relays
s := make(map[string][]string)
if user.Relays != nil {
if strings.Contains(*user.Relays, ",") {
s[user.Pubkey] = strings.Split(*user.Relays, ",")
} else {
s[user.Pubkey] = []string{*user.Relays}
}
}
ret := nostrWellKnown{
Names: n,
Relays: s,
}
j, err := json.Marshal(ret)
if err != nil {
l.Error().Msg(err.Error())
return return
} }
l.Info().Msg(fmt.Sprintf("get %s: found and returned", search))
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write(dec) w.Write(j)
} return
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)
} }

View file

@ -1,36 +0,0 @@
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
}

5
sample.env Normal file
View file

@ -0,0 +1,5 @@
export LISTEN_ADDR=":8090"
export LOG_LEVEL=0
export MATRIX_WELL_KNOWN_ADDRESS=matrix.example.com
export MATRIX_MSC3575_ADDRESS=https://matrix.example.com
export DATABASE_URL="postgres://user:password@127.0.0.1:5432/db?sslmode=disable"