Skip to content

Commit

Permalink
migrated parsing to new code
Browse files Browse the repository at this point in the history
tests pass but algia isn't getting anything
  • Loading branch information
mleku committed Jan 9, 2024
1 parent 6126f4c commit f6567a6
Show file tree
Hide file tree
Showing 23 changed files with 131 additions and 296 deletions.
9 changes: 0 additions & 9 deletions cmd/replicatrd/replicatr/alias.go

This file was deleted.

2 changes: 2 additions & 0 deletions cmd/replicatrd/replicatr/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Listener struct {
cancel context.C
}

type ListenerMap = *xsync.MapOf[string, *Listener]

var listeners = xsync.NewTypedMapOf[*WebSocket,
ListenerMap](pointerHasher[WebSocket])

Expand Down
5 changes: 5 additions & 0 deletions pkg/byter/byter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package byter

type I interface {
Bytes() []byte
}
5 changes: 5 additions & 0 deletions pkg/labeler/labeler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package labeler

type I interface{
Label() string
}
26 changes: 4 additions & 22 deletions pkg/nostr/envelopes/OK/okenvelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

log2 "github.com/Hubmakerlabs/replicatr/pkg/log"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/eventid"
"github.com/Hubmakerlabs/replicatr/pkg/wire/array"
Expand Down Expand Up @@ -39,11 +39,6 @@ type Envelope struct {
Reason string
}

func (env *Envelope) UnmarshalJSON(bytes []byte) error {
// TODO implement me
panic("implement me")
}

func NewOKEnvelope(eventID eventid.EventID, ok bool, reason string) (o *Envelope,
e error) {
var ei eventid.EventID
Expand All @@ -58,31 +53,18 @@ func NewOKEnvelope(eventID eventid.EventID, ok bool, reason string) (o *Envelope
return
}

// Label returns the label enum/type of the envelope. The relevant bytes could
// be retrieved using nip1.List[T]
func (env *Envelope) Label() (l string) { return labels.OK }

// ToArray converts an Envelope to a form that has a JSON formatted String
// and Bytes function (array.T). To get the encoded form, invoke either of these
// methods on the returned value.
func (env *Envelope) ToArray() (a array.T) {
// log.D.F("'%s' %v '%s' ", env.EventID, env.OK, env.Reason)
return array.T{labels.OK, env.EventID, env.OK, env.Reason}
}

func (env *Envelope) String() (s string) {
return env.ToArray().String()
}
func (env *Envelope) String() (s string) { return env.ToArray().String() }

func (env *Envelope) Bytes() (s []byte) {
return env.ToArray().Bytes()
}
func (env *Envelope) Bytes() (s []byte) { return env.ToArray().Bytes() }

// MarshalJSON returns the JSON encoded form of the envelope.
func (env *Envelope) MarshalJSON() (bytes []byte, e error) {
// log.D.F("ok envelope marshal")
return env.ToArray().Bytes(), nil
}
func (env *Envelope) MarshalJSON() ([]byte, error) { return env.Bytes(), nil }

const (
Btrue = "true"
Expand Down
44 changes: 10 additions & 34 deletions pkg/nostr/envelopes/auth/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package auth
import (
"fmt"

"github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper"
l "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels"
"github.com/Hubmakerlabs/replicatr/pkg/wire/array"
"github.com/Hubmakerlabs/replicatr/pkg/wire/text"
)
Expand All @@ -19,39 +19,15 @@ func NewChallenge(c string) (a *Challenge) {
return &Challenge{Challenge: c}
}

func (a *Challenge) Label() string { return labels.AUTH }
func (a *Challenge) String() (s string) { return a.ToArray().String() }
func (a *Challenge) Bytes() (s []byte) { return a.ToArray().Bytes() }
func (a *Challenge) ToArray() array.T {
return array.T{labels.AUTH,
a.Challenge}
}
func (a *Challenge) MarshalJSON() (bytes []byte, e error) {
return a.ToArray().Bytes(), nil
}
func (a *Challenge) Label() string { return l.AUTH }

func (a *Challenge) UnmarshalJSON(b []byte) (e error) {
// if a == nil {
// return fmt.Errorf("cannot unmarshal to nil pointer")
// }
// var l labels.T
// var buf *text.Buffer
// if l, buf, e = sentinel.Identify(b); log.Fail(e) {
// return
// }
// if l != labels.LAuth {
// e = fmt.Errorf("expected '%s' envelope, got '%s'",
// labels.AUTH, labels.List[l])
// log.D.Ln(e)
// return
// }
// var c enveloper.I
// if c, e = sentinel.Read(buf, l); log.Fail(e) {
// return
// }
// *a = *c.(*Challenge)
return
}
func (a *Challenge) String() string { return a.ToArray().String() }

func (a *Challenge) Bytes() []byte { return a.ToArray().Bytes() }

func (a *Challenge) ToArray() array.T { return array.T{l.AUTH, a.Challenge} }

func (a *Challenge) MarshalJSON() ([]byte, error) { return a.Bytes(), nil }

func (a *Challenge) Unmarshal(buf *text.Buffer) (e error) {
log.D.Ln("ok envelope unmarshal", string(buf.Buf))
Expand Down
52 changes: 12 additions & 40 deletions pkg/nostr/envelopes/auth/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"

log2 "github.com/Hubmakerlabs/replicatr/pkg/log"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper"
l "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/event"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/kind"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/tags"
Expand All @@ -22,57 +22,29 @@ type Response struct {

var _ enveloper.I = &Response{}

// New creates an Response response from an Challenge.
// NewResponse creates an Response response from an Challenge.
//
// The caller must sign the embedded event before sending it back to
// authenticate.
func New(ac *Challenge, rl string) (ae *Response) {
func NewResponse(ac *Challenge, rl string) (ae *Response) {
ae = &Response{
&event.T{
Kind: kind.ClientAuthentication,
Tags: tags.T{
{"relay", rl},
{"challenge", ac.Challenge},
},
Tags: tags.T{{"relay", rl}, {"challenge", ac.Challenge}},
},
}
return
}

func (a *Response) Label() string { return labels.AUTH }
func (a *Response) ToArray() array.T {
return array.T{labels.AUTH,
a.T.ToObject()}
}
func (a *Response) String() (s string) { return a.ToArray().String() }
func (a *Response) Bytes() (s []byte) { return a.ToArray().Bytes() }
func (a *Response) Label() string { return l.AUTH }

func (a *Response) MarshalJSON() (b []byte, e error) {
return a.ToArray().Bytes(), nil
}
func (a *Response) ToArray() array.T { return array.T{l.AUTH, a.T.ToObject()} }

func (a *Response) UnmarshalJSON(b []byte) (e error) {
// if a == nil {
// return fmt.Errorf("cannot unmarshal to nil pointer")
// }
// var l labels.T
// var buf *text.Buffer
// if l, buf, e = sentinel.Identify(b); log.Fail(e) {
// return
// }
// if l != labels.LAuth {
// e = fmt.Errorf("expected '%s' envelope, got '%s'",
// labels.AUTH, labels.List[l])
// log.D.Ln(e)
// return
// }
// var c enveloper.I
// if c, e = sentinel.Read(buf, l); log.Fail(e) {
// return
// }
// *a = *c.(*Response)
return
}
func (a *Response) String() string { return a.ToArray().String() }

func (a *Response) Bytes() []byte { return a.ToArray().Bytes() }

func (a *Response) MarshalJSON() ([]byte, error) { return a.Bytes(), nil }

func (a *Response) Unmarshal(buf *text.Buffer) (e error) {
if a == nil {
Expand Down
35 changes: 9 additions & 26 deletions pkg/nostr/envelopes/closed/closedenvelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"

log2 "github.com/Hubmakerlabs/replicatr/pkg/log"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper"
l "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/subscriptionid"
"github.com/Hubmakerlabs/replicatr/pkg/wire/array"
"github.com/Hubmakerlabs/replicatr/pkg/wire/text"
Expand All @@ -21,36 +21,19 @@ type Envelope struct {

var _ enveloper.I = &Envelope{}

func NewClosedEnvelope(s subscriptionid.T, reason string) (ce *Envelope) {
ce = &Envelope{T: s, Reason: reason}
return
func New(s subscriptionid.T, reason string) *Envelope {
return &Envelope{T: s, Reason: reason}
}

// Label returns the label enum/type of the envelope. The relevant bytes could
// be retrieved using nip1.Labels[Label]
func (E *Envelope) Label() (l string) { return labels.CLOSED }
func (E *Envelope) ToArray() array.T { return array.T{l.CLOSED, E.T, E.Reason} }

func (E *Envelope) ToArray() (a array.T) {
return array.T{labels.CLOSED, E.T, E.Reason}
}
func (E *Envelope) Label() string { return l.CLOSED }

func (E *Envelope) String() (s string) {
return E.ToArray().String()
}
func (E *Envelope) String() (s string) { return E.ToArray().String() }

func (E *Envelope) Bytes() (s []byte) {
return E.ToArray().Bytes()
}
func (E *Envelope) Bytes() (s []byte) { return E.ToArray().Bytes() }

// MarshalJSON returns the JSON encoded form of the envelope.
func (E *Envelope) MarshalJSON() (bytes []byte, e error) {
return E.Bytes(), nil
}

func (E *Envelope) UnmarshalJSON(bytes []byte) error {
// TODO implement me
panic("implement me")
}
func (E *Envelope) MarshalJSON() ([]byte, error) { return E.Bytes(), nil }

// Unmarshal the envelope.
func (E *Envelope) Unmarshal(buf *text.Buffer) (e error) {
Expand Down
28 changes: 9 additions & 19 deletions pkg/nostr/envelopes/closer/closeenvelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"

log2 "github.com/Hubmakerlabs/replicatr/pkg/log"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/enveloper"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/enveloper"
l "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/subscriptionid"
"github.com/Hubmakerlabs/replicatr/pkg/wire/array"
"github.com/Hubmakerlabs/replicatr/pkg/wire/text"
Expand All @@ -20,27 +20,17 @@ type Envelope struct {

var _ enveloper.I = &Envelope{}

func NewCloseEnvelope(s subscriptionid.T) (ce *Envelope) {
return &Envelope{T: s}
}
func New(s subscriptionid.T) (ce *Envelope) { return &Envelope{T: s} }

func (E *Envelope) Label() (l string) { return labels.CLOSE }
func (E *Envelope) String() (s string) { return E.ToArray().String() }
func (E *Envelope) Bytes() (s []byte) { return E.ToArray().Bytes() }
func (E *Envelope) Label() string { return l.CLOSE }

func (E *Envelope) ToArray() (a array.T) {
return array.T{labels.CLOSE, E.T}
}
func (E *Envelope) ToArray() array.T { return array.T{l.CLOSE, E.T} }

// MarshalJSON returns the JSON encoded form of the envelope.
func (E *Envelope) MarshalJSON() (bytes []byte, e error) {
return E.ToArray().Bytes(), nil
}
func (E *Envelope) String() string { return E.ToArray().String() }

func (E *Envelope) UnmarshalJSON(bytes []byte) error {
// TODO implement me
panic("implement me")
}
func (E *Envelope) Bytes() []byte { return E.ToArray().Bytes() }

func (E *Envelope) MarshalJSON() ([]byte, error) { return E.Bytes(), nil }

// Unmarshal the envelope.
func (E *Envelope) Unmarshal(buf *text.Buffer) (e error) {
Expand Down
21 changes: 7 additions & 14 deletions pkg/nostr/envelopes/countrequest/countrequestenvelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"

log2 "github.com/Hubmakerlabs/replicatr/pkg/log"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels"
l "github.com/Hubmakerlabs/replicatr/pkg/nostr/envelopes/labels"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/filter"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/filters"
"github.com/Hubmakerlabs/replicatr/pkg/nostr/subscriptionid"
Expand All @@ -20,24 +20,17 @@ type Envelope struct {
filters.T
}

func (C *Envelope) Label() string { return labels.COUNT }
func (C *Envelope) String() (s string) { return C.ToArray().String() }
func (C *Envelope) Bytes() (s []byte) { return C.ToArray().Bytes() }
func (C *Envelope) Label() string { return l.COUNT }

func (C *Envelope) ToArray() array.T {
return array.T{labels.COUNT, C.SubscriptionID, C.T}
return array.T{l.COUNT, C.SubscriptionID, C.T}
}

// MarshalJSON returns the JSON encoded form of the envelope.
func (C *Envelope) MarshalJSON() (bytes []byte, e error) {
// log.D.F("count request envelope marshal")
return C.ToArray().Bytes(), nil
}
func (C *Envelope) String() string { return C.ToArray().String() }

func (C *Envelope) UnmarshalJSON(bytes []byte) error {
// TODO implement me
panic("implement me")
}
func (C *Envelope) Bytes() []byte { return C.ToArray().Bytes() }

func (C *Envelope) MarshalJSON() ([]byte, error) { return C.Bytes(), nil }

func (C *Envelope) Unmarshal(buf *text.Buffer) (e error) {
log.D.Ln("ok envelope unmarshal", string(buf.Buf))
Expand Down
Loading

0 comments on commit f6567a6

Please sign in to comment.