fixes and tweaks from nak port work.
This commit is contained in:
16
keys.go
16
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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user