implement auth, closed and close envelopes

This commit is contained in:
2025-08-30 13:21:06 +01:00
parent 1ba2bb0a9b
commit acee5e3a90
23 changed files with 1186 additions and 44 deletions

View File

@@ -0,0 +1,100 @@
// Package closedenvelope defines the nostr message type CLOSED which is sent
// from a relay to indicate the relay-side termination of a subscription or the
// demand for authentication associated with a subscription.
package closedenvelope
import (
"io"
"lol.mleku.dev/chk"
"next.orly.dev/pkg/encoders/envelopes"
"next.orly.dev/pkg/encoders/text"
"next.orly.dev/pkg/interfaces/codec"
)
// L is the label associated with this type of codec.Envelope.
const L = "CLOSED"
// T is a CLOSED envelope, which is a signal that a subscription has been
// stopped on the relay side for some reason. Primarily this is for auth and can
// be for other things like rate limiting.
type T struct {
Subscription []byte
Reason []byte
}
var _ codec.Envelope = (*T)(nil)
// New creates an empty new T.
func New() *T {
return new(T)
}
// NewFrom creates a new closedenvelope.T populated with subscription ID and Reason.
func NewFrom(id, msg []byte) *T {
return &T{
Subscription: id, Reason: msg,
}
}
// Label returns the label of a closedenvelope.T.
func (en *T) Label() string { return L }
// ReasonString returns the Reason in the form of a string.
func (en *T) ReasonString() string { return string(en.Reason) }
// Write the closedenvelope.T to a provided io.Writer.
func (en *T) Write(w io.Writer) (err error) {
var b []byte
b = en.Marshal(b)
_, err = w.Write(b)
return
}
// Marshal a closedenvelope.T envelope in minified JSON, appending to a provided
// destination slice. Note that this ensures correct string escaping on the
// Reason field.
func (en *T) Marshal(dst []byte) (b []byte) {
b = dst
b = envelopes.Marshal(
b, L,
func(bst []byte) (o []byte) {
o = bst
o = append(o, '"')
o = append(o, en.Subscription...)
o = append(o, '"')
o = append(o, ',')
o = append(o, '"')
o = text.NostrEscape(o, en.Reason)
o = append(o, '"')
return
},
)
return
}
// Unmarshal a closedenvelope.T from minified JSON, returning the remainder after the end
// of the envelope. Note that this ensures the Reason string is correctly
// unescaped by NIP-01 escaping rules.
func (en *T) Unmarshal(b []byte) (r []byte, err error) {
r = b
if en.Subscription, r, err = text.UnmarshalQuoted(r); chk.E(err) {
return
}
if en.Reason, r, err = text.UnmarshalQuoted(r); chk.E(err) {
return
}
if r, err = envelopes.SkipToTheEnd(r); chk.E(err) {
return
}
return
}
// Parse reads a closedenvelope.T from minified JSON into a newly allocated closedenvelope.T.
func Parse(b []byte) (t *T, rem []byte, err error) {
t = New()
if rem, err = t.Unmarshal(b); chk.E(err) {
return
}
return
}

View File

@@ -0,0 +1,85 @@
package closedenvelope
import (
"fmt"
"math"
"testing"
"lol.mleku.dev/chk"
"next.orly.dev/pkg/encoders/envelopes"
"next.orly.dev/pkg/utils"
"next.orly.dev/pkg/utils/bufpool"
"lukechampine.com/frand"
)
var messages = [][]byte{
[]byte(""),
[]byte("pow: difficulty 25>=24"),
[]byte("duplicate: already have this event"),
[]byte("blocked: you are banned from posting here"),
[]byte("blocked: please register your pubkey at https://my-expensive-realy.example.com"),
[]byte("rate-limited: slow down there chief"),
[]byte("invalid: event creation date is too far off from the current time"),
[]byte("pow: difficulty 26 is less than 30"),
[]byte("error: could not connect to the database"),
}
func RandomMessage() []byte {
return messages[frand.Intn(len(messages)-1)]
}
func TestMarshalUnmarshal(t *testing.T) {
var err error
for _ = range 1000 {
rb, rb1, rb2 := bufpool.Get(), bufpool.Get(), bufpool.Get()
s := []byte(fmt.Sprintf("sub:%d", frand.Intn(math.MaxInt64)))
req := NewFrom(s, RandomMessage())
rb = req.Marshal(rb)
rb1 = append(rb1, rb...)
var rem []byte
var l string
if l, rb, err = envelopes.Identify(rb); chk.E(err) {
t.Fatal(err)
}
if l != L {
t.Fatalf("invalid sentinel %s, expect %s", l, L)
}
req2 := New()
if rem, err = req2.Unmarshal(rb); chk.E(err) {
t.Fatal(err)
}
// log.I.Ln(req2.ID)
if len(rem) > 0 {
t.Fatalf(
"unmarshal failed, remainder\n%d %s",
len(rem), rem,
)
}
rb2 = req2.Marshal(rb2)
if !utils.FastEqual(rb1, rb2) {
if len(rb1) != len(rb2) {
t.Fatalf(
"unmarshal failed, different lengths\n%d %s\n%d %s\n",
len(rb1), rb1, len(rb2), rb2,
)
}
for i := range rb1 {
if rb1[i] != rb2[i] {
t.Fatalf(
"unmarshal failed, difference at position %d\n%d %s\n%s\n%d %s\n%s\n",
i, len(rb1), rb1[:i], rb1[i:], len(rb2), rb2[:i],
rb2[i:],
)
}
}
t.Fatalf(
"unmarshal failed\n%d %s\n%d %s\n",
len(rb1), rb1, len(rb2), rb2,
)
}
bufpool.Put(rb1)
bufpool.Put(rb2)
bufpool.Put(rb)
}
}