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"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/btcec/v2"
|
"github.com/btcsuite/btcd/btcec/v2"
|
||||||
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
"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 {
|
func Generate() SecretKey {
|
||||||
var sk SecretKey
|
var sk SecretKey
|
||||||
if _, err := io.ReadFull(rand.Reader, sk[:]); err != nil {
|
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) Hex() string { return hex.EncodeToString(sk[:]) }
|
||||||
func (sk SecretKey) Public() PubKey { return GetPublicKey(sk) }
|
func (sk SecretKey) Public() PubKey { return GetPublicKey(sk) }
|
||||||
|
|
||||||
func SecretKeyFromHex(idh string) (SecretKey, error) {
|
func SecretKeyFromHex(skh string) (SecretKey, error) {
|
||||||
id := SecretKey{}
|
id := SecretKey{}
|
||||||
|
|
||||||
if len(idh) != 64 {
|
if len(skh) > 64 {
|
||||||
return id, fmt.Errorf("pubkey should be 64-char hex, got '%s'", idh)
|
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
|
return id, nil
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ type Relay struct {
|
|||||||
OnEventSaved func(ctx context.Context, event nostr.Event)
|
OnEventSaved func(ctx context.Context, event nostr.Event)
|
||||||
OnEphemeralEvent 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)
|
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]
|
QueryStored func(ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event]
|
||||||
Count func(ctx context.Context, filter nostr.Filter) (uint32, error)
|
Count func(ctx context.Context, filter nostr.Filter) (uint32, error)
|
||||||
CountHLL func(ctx context.Context, filter nostr.Filter, offset int) (uint32, *hyperloglog.HyperLogLog, 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 {
|
func (rl *Relay) handleCountRequest(ctx context.Context, ws *WebSocket, filter nostr.Filter) uint32 {
|
||||||
// check if we'll reject this filter
|
// check if we'll reject this filter
|
||||||
if nil != rl.OnCountFilter {
|
if nil != rl.OnCount {
|
||||||
if rejecting, msg := rl.OnCountFilter(ctx, filter); rejecting {
|
if rejecting, msg := rl.OnCount(ctx, filter); rejecting {
|
||||||
ws.WriteJSON(nostr.NoticeEnvelope(msg))
|
ws.WriteJSON(nostr.NoticeEnvelope(msg))
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@@ -65,8 +65,8 @@ func (rl *Relay) handleCountRequestWithHLL(
|
|||||||
offset int,
|
offset int,
|
||||||
) (uint32, *hyperloglog.HyperLogLog) {
|
) (uint32, *hyperloglog.HyperLogLog) {
|
||||||
// check if we'll reject this filter
|
// check if we'll reject this filter
|
||||||
if nil != rl.OnCountFilter {
|
if nil != rl.OnCount {
|
||||||
if rejecting, msg := rl.OnCountFilter(ctx, filter); rejecting {
|
if rejecting, msg := rl.OnCount(ctx, filter); rejecting {
|
||||||
ws.WriteJSON(nostr.NoticeEnvelope(msg))
|
ws.WriteJSON(nostr.NoticeEnvelope(msg))
|
||||||
return 0, nil
|
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.
|
// helper function for ValidateAuthEvent.
|
||||||
func parseURL(input string) (*url.URL, error) {
|
func parseURL(input string) (*url.URL, error) {
|
||||||
return url.Parse(
|
return url.Parse(
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ type RelayReadWrite struct {
|
|||||||
Write bool `json:"write"`
|
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
|
var req Request
|
||||||
|
|
||||||
plain, err := nip44.Decrypt(event.Content, s.ConversationKey)
|
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
|
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,
|
req Request,
|
||||||
resp Response,
|
resp Response,
|
||||||
eventResponse nostr.Event,
|
eventResponse nostr.Event,
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ func (r Response) String() string {
|
|||||||
|
|
||||||
type Signer interface {
|
type Signer interface {
|
||||||
GetSession(client nostr.PubKey) (Session, bool)
|
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 {
|
func IsValidBunkerURL(input string) bool {
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ func (p *StaticKeySigner) getOrCreateSession(clientPubkey nostr.PubKey) (Session
|
|||||||
return session, nil
|
return session, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *StaticKeySigner) HandleRequest(_ context.Context, event *nostr.Event) (
|
func (p *StaticKeySigner) HandleRequest(_ context.Context, event nostr.Event) (
|
||||||
req Request,
|
req Request,
|
||||||
resp Response,
|
resp Response,
|
||||||
eventResponse nostr.Event,
|
eventResponse nostr.Event,
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ func (zi *Info) ToEvent(ctx context.Context, kr nostr.Keyer, evt *nostr.Event) e
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (zi *Info) ParseEvent(evt *nostr.Event) error {
|
func (zi *Info) ParseEvent(evt nostr.Event) error {
|
||||||
zi.Mints = make([]string, 0)
|
zi.Mints = make([]string, 0)
|
||||||
for _, tag := range evt.Tags {
|
for _, tag := range evt.Tags {
|
||||||
if len(tag) < 2 {
|
if len(tag) < 2 {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ func SendNutzap(
|
|||||||
targetUserPublickey nostr.PubKey,
|
targetUserPublickey nostr.PubKey,
|
||||||
getUserReadRelays func(context.Context, nostr.PubKey, int) []string,
|
getUserReadRelays func(context.Context, nostr.PubKey, int) []string,
|
||||||
relays []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,
|
amount uint64,
|
||||||
message string,
|
message string,
|
||||||
) (chan nostr.PublishResult, error) {
|
) (chan nostr.PublishResult, error) {
|
||||||
@@ -33,7 +33,7 @@ func SendNutzap(
|
|||||||
}
|
}
|
||||||
|
|
||||||
info := Info{}
|
info := Info{}
|
||||||
if err := info.ParseEvent(&ie.Event); err != nil {
|
if err := info.ParseEvent(ie.Event); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,8 +56,8 @@ func SendNutzap(
|
|||||||
}
|
}
|
||||||
|
|
||||||
nutzap.Tags = append(nutzap.Tags, nostr.Tag{"p", targetUserPublickey.Hex()})
|
nutzap.Tags = append(nutzap.Tags, nostr.Tag{"p", targetUserPublickey.Hex()})
|
||||||
if eventId != "" {
|
if eventId != nostr.ZeroID {
|
||||||
nutzap.Tags = append(nutzap.Tags, nostr.Tag{"e", eventId})
|
nutzap.Tags = append(nutzap.Tags, nostr.Tag{"e", eventId.Hex()})
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if we have enough tokens in any of these mints
|
// check if we have enough tokens in any of these mints
|
||||||
|
|||||||
Reference in New Issue
Block a user