implement nip-11, relay information document.

This commit is contained in:
fiatjaf 2022-07-11 16:00:21 -03:00
parent 809f8030db
commit 8cc12a6bd9
3 changed files with 35 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import (
"time"
"github.com/fiatjaf/go-nostr"
"github.com/fiatjaf/go-nostr/nip11"
"github.com/gorilla/websocket"
)
@ -193,3 +194,25 @@ func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request) {
}()
}
}
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
info := nip11.RelayInformationDocument{
Name: relay.Name(),
Description: "relay powered by the relayer framework",
PubKey: "~",
Contact: "~",
SupportedNIPs: []int{9, 15, 16},
Software: "https://github.com/fiatjaf/relayer",
Version: "~",
}
if ifmer, ok := relay.(Informationer); ok {
info = ifmer.GetNIP11InformationDocument()
}
json.NewEncoder(w).Encode(info)
}
}

View File

@ -2,6 +2,7 @@ package relayer
import (
"github.com/fiatjaf/go-nostr"
"github.com/fiatjaf/go-nostr/nip11"
)
var Log = log
@ -16,3 +17,7 @@ type Relay interface {
type Injector interface {
InjectEvents() chan nostr.Event
}
type Informationer interface {
GetNIP11InformationDocument() nip11.RelayInformationDocument
}

View File

@ -16,8 +16,10 @@ type Settings struct {
Port string `envconfig:"PORT" default:"7447"`
}
var s Settings
var log = zerolog.New(os.Stderr).Output(zerolog.ConsoleWriter{Out: os.Stderr})
var (
s Settings
log = zerolog.New(os.Stderr).Output(zerolog.ConsoleWriter{Out: os.Stderr})
)
var Router = mux.NewRouter()
@ -33,6 +35,9 @@ func Start(relay Relay) {
// catch the websocket call before anything else
Router.Path("/").Headers("Upgrade", "websocket").HandlerFunc(handleWebsocket(relay))
// nip-11, relay information
Router.Path("/").Headers("Accept", "application/nostr+json").HandlerFunc(handleNIP11(relay))
// allow implementations to do initialization stuff
if err := relay.Init(); err != nil {
Log.Fatal().Err(err).Msg("failed to start")