Skip to content

Commit

Permalink
Add missing fields to event
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Jun 21, 2023
1 parent 839cd4e commit 7e2fb08
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 10 deletions.
2 changes: 2 additions & 0 deletions service/adapters/firestore/repository_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (r *RegistrationRepository) saveUnderRelays(registration domain.Registratio
}

func (r *RegistrationRepository) GetRelays(ctx context.Context) ([]domain.RelayAddress, error) {
// todo do it in transaction? emulator doesn't support it
iter := r.client.Collection(collectionRelays).Documents(ctx)

var result []domain.RelayAddress
Expand All @@ -122,6 +123,7 @@ func (r *RegistrationRepository) GetRelays(ctx context.Context) ([]domain.RelayA
}

func (r *RegistrationRepository) GetPublicKeys(ctx context.Context, address domain.RelayAddress) ([]domain.PublicKey, error) {
// todo do it in transaction? emulator doesn't support it
iter := r.client.Collection(collectionRelays).Doc(r.relayAddressAsKey(address)).Collection(collectionRelaysPublicKeys).Documents(ctx)

var result []domain.PublicKey
Expand Down
70 changes: 63 additions & 7 deletions service/domain/event.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package domain

import (
"bytes"
"time"

"github.com/boreq/errors"
"github.com/nbd-wtf/go-nostr"
)

type Event struct {
pubKey PublicKey
content []byte
id EventId
pubKey PublicKey
createdAt time.Time
kind EventKind
tags []EventTag
content string
sig EventSignature
}

func NewEvent(libevent nostr.Event) (Event, error) {
Expand All @@ -22,21 +27,72 @@ func NewEvent(libevent nostr.Event) (Event, error) {
return Event{}, errors.New("invalid signature")
}

id, err := NewEventId(libevent.ID)
if err != nil {
return Event{}, errors.Wrap(err, "error creating an event id")
}

pubKey, err := NewPublicKey(libevent.PubKey)
if err != nil {
return Event{}, errors.Wrap(err, "error creating a pub key")
}

createdAt := time.Unix(int64(libevent.CreatedAt), 0).UTC()

kind, err := NewEventKind(libevent.Kind)
if err != nil {
return Event{}, errors.Wrap(err, "error creating event kind")
}

var tags []EventTag
for _, libtag := range libevent.Tags {
eventTag, err := NewEventTag(libtag)
if err != nil {
return Event{}, errors.Wrap(err, "error creating a tag")
}
tags = append(tags, eventTag)
}

sig, err := NewEventSignature(libevent.Sig)
if err != nil {
return Event{}, errors.Wrap(err, "error creating a signature")
}

return Event{
pubKey: pubKey,
content: []byte(libevent.Content),
id: id,
pubKey: pubKey,
createdAt: createdAt,
kind: kind,
tags: tags,
content: libevent.Content,
sig: sig,
}, nil
}

func (e Event) Id() EventId {
return e.id
}

func (e Event) PubKey() PublicKey {
return e.pubKey
}

func (e Event) Content() []byte {
return bytes.Clone(e.content)
func (e Event) CreatedAt() time.Time {
return e.createdAt
}

func (e Event) Kind() EventKind {
return e.kind
}

func (e Event) Tags() []EventTag {
return e.tags
}

func (e Event) Content() string {
return e.content
}

func (e Event) Sig() EventSignature {
return e.sig
}
37 changes: 37 additions & 0 deletions service/domain/event_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package domain

import (
"crypto/sha256"
"encoding/hex"
"github.com/boreq/errors"
)

type EventId struct {
s string
}

func NewEventId(s string) (EventId, error) {
b, err := hex.DecodeString(s)
if err != nil {
return EventId{}, errors.Wrap(err, "error decoding hex")
}

if len(b) != sha256.Size {
return EventId{}, errors.New("invalid length")
}

s = hex.EncodeToString(b)
return EventId{s}, nil
}

func (id EventId) Hex() string {
return id.s
}

func (id EventId) Bytes() []byte {
b, err := hex.DecodeString(id.s)
if err != nil {
panic(err)
}
return b
}
18 changes: 18 additions & 0 deletions service/domain/event_kind.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package domain

import "github.com/boreq/errors"

type EventKind struct {
k int
}

func NewEventKind(k int) (EventKind, error) {
if k < 0 {
return EventKind{}, errors.New("kind must be positive")
}
return EventKind{k}, nil
}

func (k EventKind) Int() int {
return k.k
}
36 changes: 36 additions & 0 deletions service/domain/event_signature.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package domain

import (
"encoding/hex"
"github.com/boreq/errors"
)

type EventSignature struct {
s string
}

func NewEventSignature(s string) (EventSignature, error) {
b, err := hex.DecodeString(s)
if err != nil {
return EventSignature{}, errors.Wrap(err, "error decoding hex")
}

if len(b) != 64 {
return EventSignature{}, errors.New("invalid length")
}

s = hex.EncodeToString(b)
return EventSignature{s}, nil
}

func (sig EventSignature) Hex() string {
return sig.s
}

func (sig EventSignature) Bytes() []byte {
b, err := hex.DecodeString(sig.s)
if err != nil {
panic(err)
}
return b
}
9 changes: 7 additions & 2 deletions service/domain/public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package domain

import (
"encoding/hex"
"github.com/decred/dcrd/dcrec/secp256k1/v4"

"github.com/boreq/errors"
)
Expand All @@ -11,12 +12,16 @@ type PublicKey struct {
}

func NewPublicKey(s string) (PublicKey, error) {
decodeString, err := hex.DecodeString(s)
b, err := hex.DecodeString(s)
if err != nil {
return PublicKey{}, errors.Wrap(err, "error decoding hex")
}

s = hex.EncodeToString(decodeString)
if len(b) != secp256k1.PrivKeyBytesLen {
return PublicKey{}, errors.New("invalid length")
}

s = hex.EncodeToString(b)
return PublicKey{s}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion service/domain/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Registration struct {

func NewRegistrationFromEvent(event Event) (Registration, error) {
var v registrationTransport
if err := json.Unmarshal(event.Content(), &v); err != nil {
if err := json.Unmarshal([]byte(event.Content()), &v); err != nil {
return Registration{}, errors.Wrap(err, "error unmarshaling content")
}

Expand Down
15 changes: 15 additions & 0 deletions service/domain/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package domain

import "github.com/boreq/errors"

type EventTag struct {
tag []string
}

func NewEventTag(tag []string) (EventTag, error) {
if len(tag) < 2 {
return EventTag{}, errors.New("tag needs at least two fields I recon")
}

return EventTag{tag}, nil
}

0 comments on commit 7e2fb08

Please sign in to comment.