2024-01-09 16:55:00 -03:00
|
|
|
package nip46
|
|
|
|
|
|
|
|
import (
|
2024-10-14 16:26:01 -03:00
|
|
|
"context"
|
2024-12-17 11:05:43 -03:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2024-01-09 16:55:00 -03:00
|
|
|
|
2024-12-03 00:49:27 -03:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2024-01-09 16:55:00 -03:00
|
|
|
"github.com/nbd-wtf/go-nostr"
|
|
|
|
)
|
|
|
|
|
2024-12-17 11:05:43 -03:00
|
|
|
var json = jsoniter.ConfigFastest
|
2024-03-01 15:54:28 -03:00
|
|
|
|
2024-01-09 16:55:00 -03:00
|
|
|
type Request struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Method string `json:"method"`
|
|
|
|
Params []string `json:"params"`
|
|
|
|
}
|
|
|
|
|
2024-10-14 16:42:54 -03:00
|
|
|
func (r Request) String() string {
|
|
|
|
j, _ := json.Marshal(r)
|
|
|
|
return string(j)
|
|
|
|
}
|
|
|
|
|
2024-01-09 16:55:00 -03:00
|
|
|
type Response struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
Result string `json:"result,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-10-14 16:42:54 -03:00
|
|
|
func (r Response) String() string {
|
|
|
|
j, _ := json.Marshal(r)
|
|
|
|
return string(j)
|
|
|
|
}
|
|
|
|
|
2024-01-09 16:55:00 -03:00
|
|
|
type Signer interface {
|
|
|
|
GetSession(clientPubkey string) (Session, bool)
|
2024-10-14 16:26:01 -03:00
|
|
|
HandleRequest(context.Context, *nostr.Event) (req Request, resp Response, eventResponse nostr.Event, err error)
|
2024-01-09 16:55:00 -03:00
|
|
|
}
|
|
|
|
|
2024-03-01 15:54:28 -03:00
|
|
|
func IsValidBunkerURL(input string) bool {
|
2024-12-17 11:05:43 -03:00
|
|
|
p, err := url.Parse(input)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if p.Scheme != "bunker" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !nostr.IsValidPublicKey(p.Host) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !strings.Contains(p.RawQuery, "relay=") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2024-03-01 15:54:28 -03:00
|
|
|
}
|