diff --git a/keys.go b/keys.go index 9bc1c16..6d7ad0e 100644 --- a/keys.go +++ b/keys.go @@ -5,12 +5,15 @@ import ( "encoding/hex" "fmt" "io" + "strings" "unsafe" "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcec/v2/schnorr" ) +var KeyOne = SecretKey{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} + func Generate() SecretKey { var sk SecretKey if _, err := io.ReadFull(rand.Reader, sk[:]); err != nil { @@ -25,14 +28,17 @@ func (sk SecretKey) String() string { return "sk::" + sk.Hex() } func (sk SecretKey) Hex() string { return hex.EncodeToString(sk[:]) } func (sk SecretKey) Public() PubKey { return GetPublicKey(sk) } -func SecretKeyFromHex(idh string) (SecretKey, error) { +func SecretKeyFromHex(skh string) (SecretKey, error) { id := SecretKey{} - if len(idh) != 64 { - return id, fmt.Errorf("pubkey should be 64-char hex, got '%s'", idh) + if len(skh) > 64 { + skh = strings.Repeat("0", 64-len(skh)) + skh + } else if len(skh) > 64 { + return id, fmt.Errorf("pubkey should be at most 64-char hex, got '%s'", skh) } - if _, err := hex.Decode(id[:], unsafe.Slice(unsafe.StringData(idh), 64)); err != nil { - return id, fmt.Errorf("'%s' is not valid hex: %w", idh, err) + + if _, err := hex.Decode(id[:], unsafe.Slice(unsafe.StringData(skh), 64)); err != nil { + return id, fmt.Errorf("'%s' is not valid hex: %w", skh, err) } return id, nil diff --git a/khatru/relay.go b/khatru/relay.go index d6d2616..6476269 100644 --- a/khatru/relay.go +++ b/khatru/relay.go @@ -65,7 +65,7 @@ type Relay struct { OnEventSaved func(ctx context.Context, event nostr.Event) OnEphemeralEvent func(ctx context.Context, event nostr.Event) OnRequest func(ctx context.Context, filter nostr.Filter) (reject bool, msg string) - OnCountFilter func(ctx context.Context, filter nostr.Filter) (reject bool, msg string) + OnCount func(ctx context.Context, filter nostr.Filter) (reject bool, msg string) QueryStored func(ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event] Count func(ctx context.Context, filter nostr.Filter) (uint32, error) CountHLL func(ctx context.Context, filter nostr.Filter, offset int) (uint32, *hyperloglog.HyperLogLog, error) diff --git a/khatru/responding.go b/khatru/responding.go index 2549769..c33dff6 100644 --- a/khatru/responding.go +++ b/khatru/responding.go @@ -39,8 +39,8 @@ func (rl *Relay) handleRequest(ctx context.Context, id string, eose *sync.WaitGr func (rl *Relay) handleCountRequest(ctx context.Context, ws *WebSocket, filter nostr.Filter) uint32 { // check if we'll reject this filter - if nil != rl.OnCountFilter { - if rejecting, msg := rl.OnCountFilter(ctx, filter); rejecting { + if nil != rl.OnCount { + if rejecting, msg := rl.OnCount(ctx, filter); rejecting { ws.WriteJSON(nostr.NoticeEnvelope(msg)) return 0 } @@ -65,8 +65,8 @@ func (rl *Relay) handleCountRequestWithHLL( offset int, ) (uint32, *hyperloglog.HyperLogLog) { // check if we'll reject this filter - if nil != rl.OnCountFilter { - if rejecting, msg := rl.OnCountFilter(ctx, filter); rejecting { + if nil != rl.OnCount { + if rejecting, msg := rl.OnCount(ctx, filter); rejecting { ws.WriteJSON(nostr.NoticeEnvelope(msg)) return 0, nil } diff --git a/nip42/nip42.go b/nip42/nip42.go index 54c5364..50d6737 100644 --- a/nip42/nip42.go +++ b/nip42/nip42.go @@ -23,6 +23,15 @@ func CreateUnsignedAuthEvent(challenge string, pubkey nostr.PubKey, relayURL str } } +func GetRelayURLFromAuthEvent(event nostr.Event) string { + for _, tag := range event.Tags { + if len(tag) >= 2 && tag[0] == "relay" { + return tag[1] + } + } + return "" +} + // helper function for ValidateAuthEvent. func parseURL(input string) (*url.URL, error) { return url.Parse( diff --git a/nip46/bunker_session.go b/nip46/bunker_session.go index e55aca4..0089813 100644 --- a/nip46/bunker_session.go +++ b/nip46/bunker_session.go @@ -17,7 +17,7 @@ type RelayReadWrite struct { Write bool `json:"write"` } -func (s Session) ParseRequest(event *nostr.Event) (Request, error) { +func (s Session) ParseRequest(event nostr.Event) (Request, error) { var req Request plain, err := nip44.Decrypt(event.Content, s.ConversationKey) diff --git a/nip46/dynamic-signer.go b/nip46/dynamic-signer.go index 63db51d..cd26572 100644 --- a/nip46/dynamic-signer.go +++ b/nip46/dynamic-signer.go @@ -71,7 +71,7 @@ func (p *DynamicSigner) setSession(clientPubkey nostr.PubKey, session Session) { p.sessions[clientPubkey] = session } -func (p *DynamicSigner) HandleRequest(ctx context.Context, event *nostr.Event) ( +func (p *DynamicSigner) HandleRequest(ctx context.Context, event nostr.Event) ( req Request, resp Response, eventResponse nostr.Event, diff --git a/nip46/nip46.go b/nip46/nip46.go index 56b9ca7..9a6e205 100644 --- a/nip46/nip46.go +++ b/nip46/nip46.go @@ -35,7 +35,7 @@ func (r Response) String() string { type Signer interface { GetSession(client nostr.PubKey) (Session, bool) - HandleRequest(context.Context, *nostr.Event) (req Request, resp Response, eventResponse nostr.Event, err error) + HandleRequest(context.Context, nostr.Event) (req Request, resp Response, eventResponse nostr.Event, err error) } func IsValidBunkerURL(input string) bool { diff --git a/nip46/static-key-signer.go b/nip46/static-key-signer.go index 4fefbd5..265cac9 100644 --- a/nip46/static-key-signer.go +++ b/nip46/static-key-signer.go @@ -60,7 +60,7 @@ func (p *StaticKeySigner) getOrCreateSession(clientPubkey nostr.PubKey) (Session return session, nil } -func (p *StaticKeySigner) HandleRequest(_ context.Context, event *nostr.Event) ( +func (p *StaticKeySigner) HandleRequest(_ context.Context, event nostr.Event) ( req Request, resp Response, eventResponse nostr.Event, diff --git a/nip61/info.go b/nip61/info.go index a001df1..5553a39 100644 --- a/nip61/info.go +++ b/nip61/info.go @@ -36,7 +36,7 @@ func (zi *Info) ToEvent(ctx context.Context, kr nostr.Keyer, evt *nostr.Event) e return nil } -func (zi *Info) ParseEvent(evt *nostr.Event) error { +func (zi *Info) ParseEvent(evt nostr.Event) error { zi.Mints = make([]string, 0) for _, tag := range evt.Tags { if len(tag) < 2 { diff --git a/nip61/nip61.go b/nip61/nip61.go index a285c69..c8d41a5 100644 --- a/nip61/nip61.go +++ b/nip61/nip61.go @@ -23,7 +23,7 @@ func SendNutzap( targetUserPublickey nostr.PubKey, getUserReadRelays func(context.Context, nostr.PubKey, int) []string, relays []string, - eventId string, // can be "" if not targeting a specific event + eventId nostr.ID, // can be "" if not targeting a specific event amount uint64, message string, ) (chan nostr.PublishResult, error) { @@ -33,7 +33,7 @@ func SendNutzap( } info := Info{} - if err := info.ParseEvent(&ie.Event); err != nil { + if err := info.ParseEvent(ie.Event); err != nil { return nil, err } @@ -56,8 +56,8 @@ func SendNutzap( } nutzap.Tags = append(nutzap.Tags, nostr.Tag{"p", targetUserPublickey.Hex()}) - if eventId != "" { - nutzap.Tags = append(nutzap.Tags, nostr.Tag{"e", eventId}) + if eventId != nostr.ZeroID { + nutzap.Tags = append(nutzap.Tags, nostr.Tag{"e", eventId.Hex()}) } // check if we have enough tokens in any of these mints