diff --git a/connection.go b/connection.go index b61a7cc..a7a0bc0 100644 --- a/connection.go +++ b/connection.go @@ -85,11 +85,7 @@ func NewConnection(ctx context.Context, url string, requestHeader http.Header) ( }) } - writer := wsutil.NewWriter( - conn, - state, - ws.OpText, - ) + writer := wsutil.NewWriter(conn, state, ws.OpText) writer.SetExtensions(&msgState) return &Connection{ @@ -121,8 +117,7 @@ func (c *Connection) WriteMessage(data []byte) error { return fmt.Errorf("failed to write message: %w", err) } - err := c.flateWriter.Close() - if err != nil { + if err := c.flateWriter.Close(); err != nil { return fmt.Errorf("failed to close flate writer: %w", err) } } else { @@ -131,8 +126,7 @@ func (c *Connection) WriteMessage(data []byte) error { } } - err := c.writer.Flush() - if err != nil { + if err := c.writer.Flush(); err != nil { return fmt.Errorf("failed to flush writer: %w", err) } diff --git a/envelopes_test.go b/envelopes_test.go index 03502a8..4c73bd5 100644 --- a/envelopes_test.go +++ b/envelopes_test.go @@ -13,8 +13,7 @@ func TestEventEnvelopeEncodingAndDecoding(t *testing.T) { for _, raw := range eventEnvelopes { var env EventEnvelope - err := json.Unmarshal([]byte(raw), &env) - if err != nil { + if err := json.Unmarshal([]byte(raw), &env); err != nil { t.Errorf("failed to parse event envelope json: %v", err) } @@ -46,8 +45,7 @@ func TestNoticeEnvelopeEncodingAndDecoding(t *testing.T) { t.Error("failed to decode NOTICE") } - res, _ := json.Marshal(env) - if string(res) != src { + if res, _ := json.Marshal(env); string(res) != src { t.Errorf("failed to encode NOTICE: expected '%s', got '%s'", src, string(res)) } } @@ -60,8 +58,7 @@ func TestEoseEnvelopeEncodingAndDecoding(t *testing.T) { t.Error("failed to decode EOSE") } - res, _ := json.Marshal(env) - if string(res) != src { + if res, _ := json.Marshal(env); string(res) != src { t.Errorf("failed to encode EOSE: expected '%s', got '%s'", src, string(res)) } } @@ -74,8 +71,7 @@ func TestOKEnvelopeEncodingAndDecoding(t *testing.T) { for _, raw := range okEnvelopes { var env OKEnvelope - err := json.Unmarshal([]byte(raw), &env) - if err != nil { + if err := json.Unmarshal([]byte(raw), &env); err != nil { t.Errorf("failed to parse ok envelope json: %v", err) } @@ -99,8 +95,7 @@ func TestAuthEnvelopeEncodingAndDecoding(t *testing.T) { for _, raw := range authEnvelopes { var env AuthEnvelope - err := json.Unmarshal([]byte(raw), &env) - if err != nil { + if err := json.Unmarshal([]byte(raw), &env); err != nil { t.Errorf("failed to parse auth envelope json: %v", err) } diff --git a/event_test.go b/event_test.go index f5fbf9c..13676ef 100644 --- a/event_test.go +++ b/event_test.go @@ -15,8 +15,7 @@ func TestEventParsingAndVerifying(t *testing.T) { for _, raw := range rawEvents { var ev Event - err := json.Unmarshal([]byte(raw), &ev) - if err != nil { + if err := json.Unmarshal([]byte(raw), &ev); err != nil { t.Errorf("failed to parse event json: %v", err) } diff --git a/filter_test.go b/filter_test.go index 5559d98..6be09dd 100644 --- a/filter_test.go +++ b/filter_test.go @@ -10,8 +10,7 @@ import ( func TestFilterUnmarshal(t *testing.T) { raw := `{"ids": ["abc"],"#e":["zzz"],"#something":["nothing","bab"],"since":1644254609,"search":"test"}` var f Filter - err := json.Unmarshal([]byte(raw), &f) - if err != nil { + if err := json.Unmarshal([]byte(raw), &f); err != nil { t.Errorf("failed to parse filter json: %v", err) } diff --git a/keys.go b/keys.go index e31431a..b0e8c97 100644 --- a/keys.go +++ b/keys.go @@ -16,8 +16,7 @@ func GeneratePrivateKey() string { one := new(big.Int).SetInt64(1) b := make([]byte, params.BitSize/8+8) - _, err := io.ReadFull(rand.Reader, b) - if err != nil { + if _, err := io.ReadFull(rand.Reader, b); err != nil { return "" } diff --git a/metadata.go b/metadata.go index 80fe925..922f993 100644 --- a/metadata.go +++ b/metadata.go @@ -22,8 +22,7 @@ func ParseMetadata(event Event) (*ProfileMetadata, error) { } var meta ProfileMetadata - err := json.Unmarshal([]byte(event.Content), &meta) - if err != nil { + if err := json.Unmarshal([]byte(event.Content), &meta); err != nil { cont := event.Content if len(cont) > 100 { cont = cont[0:99] diff --git a/nip04/nip04.go b/nip04/nip04.go index c79ea57..99e642d 100644 --- a/nip04/nip04.go +++ b/nip04/nip04.go @@ -43,8 +43,7 @@ func Encrypt(message string, key []byte) (string, error) { // block size is 16 bytes iv := make([]byte, 16) // can probably use a less expensive lib since IV has to only be unique; not perfectly random; math/rand? - _, err := rand.Read(iv) - if err != nil { + if _, err := rand.Read(iv); err != nil { return "", fmt.Errorf("error creating initization vector: %w", err) } diff --git a/nip06/nip06.go b/nip06/nip06.go index 1226ca7..455c771 100644 --- a/nip06/nip06.go +++ b/nip06/nip06.go @@ -42,8 +42,7 @@ func PrivateKeyFromSeed(seed []byte) (string, error) { next := key for _, idx := range derivationPath { var err error - next, err = next.NewChildKey(idx) - if err != nil { + if next, err = next.NewChildKey(idx); err != nil { return "", err } } diff --git a/pool.go b/pool.go index 729fdbe..b26c868 100644 --- a/pool.go +++ b/pool.go @@ -43,8 +43,7 @@ func (pool *SimplePool) EnsureRelay(url string) (*Relay, error) { // we use this ctx here so when the pool dies everything dies ctx, cancel := context.WithTimeout(pool.Context, time.Second*15) defer cancel() - relay, err = RelayConnect(ctx, nm) - if err != nil { + if relay, err = RelayConnect(ctx, nm); err != nil { return nil, fmt.Errorf("failed to connect: %w", err) } @@ -55,11 +54,7 @@ func (pool *SimplePool) EnsureRelay(url string) (*Relay, error) { // SubMany opens a subscription with the given filters to multiple relays // the subscriptions only end when the context is canceled -func (pool *SimplePool) SubMany( - ctx context.Context, - urls []string, - filters Filters, -) chan *Event { +func (pool *SimplePool) SubMany(ctx context.Context, urls []string, filters Filters) chan *Event { uniqueEvents := make(chan *Event) seenAlready := xsync.NewMapOf[bool]() @@ -96,11 +91,7 @@ func (pool *SimplePool) SubMany( } // SubManyEose is like SubMany, but it stops subscriptions and closes the channel when gets a EOSE -func (pool *SimplePool) SubManyEose( - ctx context.Context, - urls []string, - filters Filters, -) chan *Event { +func (pool *SimplePool) SubManyEose(ctx context.Context, urls []string, filters Filters) chan *Event { ctx, cancel := context.WithCancel(ctx) uniqueEvents := make(chan *Event) diff --git a/subscription.go b/subscription.go index 564569e..7c78d19 100644 --- a/subscription.go +++ b/subscription.go @@ -54,7 +54,7 @@ func (sub *Subscription) Unsub() { sub.conn.WriteMessage(closeb) sub.Relay.Subscriptions.Delete(id) - if sub.stopped == false && sub.Events != nil { + if !sub.stopped && sub.Events != nil { close(sub.Events) } sub.stopped = true @@ -74,8 +74,7 @@ func (sub *Subscription) Fire() error { reqb, _ := ReqEnvelope{id, sub.Filters}.MarshalJSON() debugLog("{%s} sending %v", sub.Relay.URL, reqb) - err := sub.conn.WriteMessage(reqb) - if err != nil { + if err := sub.conn.WriteMessage(reqb); err != nil { sub.cancel() return fmt.Errorf("failed to write: %w", err) }