mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-02 19:19:29 +02:00
fix: removing redundancy
This commit is contained in:
parent
ee8f58360f
commit
58d56abe05
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
3
keys.go
3
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 ""
|
||||
}
|
||||
|
||||
|
@ -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]
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
15
pool.go
15
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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user