fiatjaf / relay29

NIP-29 relay
MIT License
13 stars 4 forks source link
nostr

= relay29 image:https://pkg.go.dev/badge/github.com/fiatjaf/relay29.svg[link=https://pkg.go.dev/github.com/fiatjaf/relay29]

NIP-29 requires the relays to have more of an active role in making groups work with the rules, so this is a library for creating NIP-29 relays, works with https://github.com/fiatjaf/khatru[khatru] using the https://pkg.go.dev/github.com/fiatjaf/relay29/khatru29[khatru29] wrapper, https://github.com/hoytech/strfry[strfry] with link:strfry29[strfry29] and https://github.com/fiatjaf/relayer[relayer] with link:relayer29[relayer29].

CAUTION: This is probably broken so please don't trust it for anything serious and be prepared to delete your database.

[source,go]

package main

import ( "fmt" "log" "net/http" "time"

"github.com/fiatjaf/eventstore/slicestore"
"github.com/fiatjaf/khatru/policies"
"github.com/fiatjaf/relay29"
"github.com/fiatjaf/relay29/khatru29"
"github.com/nbd-wtf/go-nostr"

)

func main() { relayPrivateKey := nostr.GeneratePrivateKey()

db := &slicestore.SliceStore{} // this only keeps things in memory, use a different eventstore in production
db.Init()

relay, _ := khatru29.Init(relay29.Options{
    Domain:    "localhost:2929",
    DB:        db,
    SecretKey: relayPrivateKey,
})

// init relay
relay.Info.Name = "very ephemeral chat relay"
relay.Info.Description = "everything will be deleted as soon as I turn off my computer"

// extra policies
relay.RejectEvent = append(relay.RejectEvent,
    policies.PreventLargeTags(64),
    policies.PreventTooManyIndexableTags(6, []int{9005}, nil),
    policies.RestrictToSpecifiedKinds(
        9, 10, 11, 12,
        30023, 31922, 31923, 9802,
        9000, 9001, 9002, 9003, 9004, 9005, 9006, 9007,
        9021,
    ),
    policies.PreventTimestampsInThePast(60 * time.Second),
    policies.PreventTimestampsInTheFuture(30 * time.Second),
)

// http routes
relay.Router().HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "nothing to see here, you must use a nip-29 powered client")
})

fmt.Println("running on http://0.0.0.0:2929")
if err := http.ListenAndServe(":2929", relay); err != nil {
    log.Fatal("failed to serve")
}

}

== How to use

Basically you just call khatru29.Init() and then you get back a khatru.Relay and a relay29.State instances. The state has inside it also a map of Group objects that you can read but you should not modify manually. To modify these groups you must write moderation events with the .AddEvent() method of the Relay. This API may be improved later.

See link:examples/groups.fiatjaf.com/main.go[] for a (not very much) more complex example.

== How it works

What this library does is basically: