nip46: add a random element to id generation.

This commit is contained in:
fiatjaf
2024-02-11 11:07:01 -03:00
parent 64cc356404
commit f3d9f02598

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"math/rand"
"net/url" "net/url"
"strconv" "strconv"
"sync/atomic" "sync/atomic"
@@ -22,6 +23,7 @@ type BunkerClient struct {
relays []string relays []string
sharedSecret []byte sharedSecret []byte
listeners *xsync.MapOf[string, chan Response] listeners *xsync.MapOf[string, chan Response]
idPrefix string
// memoized // memoized
getPublicKeyResponse string getPublicKeyResponse string
@@ -69,6 +71,7 @@ func ConnectBunker(
relays: relays, relays: relays,
sharedSecret: shared, sharedSecret: shared,
listeners: xsync.NewMapOf[string, chan Response](), listeners: xsync.NewMapOf[string, chan Response](),
idPrefix: "gn-" + strconv.Itoa(rand.Intn(65536)),
} }
go func() { go func() {
@@ -131,7 +134,7 @@ func (bunker *BunkerClient) SignEvent(ctx context.Context, evt *nostr.Event) err
} }
func (bunker *BunkerClient) RPC(ctx context.Context, method string, params []string) (string, error) { func (bunker *BunkerClient) RPC(ctx context.Context, method string, params []string) (string, error) {
id := strconv.FormatUint(bunker.serial.Add(1), 10) id := bunker.idPrefix + "-" + strconv.FormatUint(bunker.serial.Add(1), 10)
req, err := json.Marshal(Request{ req, err := json.Marshal(Request{
ID: id, ID: id,
Method: method, Method: method,