-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathhelpers.go
More file actions
93 lines (79 loc) · 2.09 KB
/
Copy pathhelpers.go
File metadata and controls
93 lines (79 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"context"
"fmt"
"regexp"
"strings"
"time"
"fiatjaf.com/nostr"
"fiatjaf.com/nostr/eventstore/mmm"
"fiatjaf.com/nostr/nip05"
"fiatjaf.com/nostr/nip19"
)
var justLetters = regexp.MustCompile(`^\w+$`)
func parsePubKey(value string) (nostr.PubKey, error) {
// try nip05 first
if nip05.IsValidIdentifier(value) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
pp, err := nip05.QueryIdentifier(ctx, value)
cancel()
if err == nil {
return pp.PublicKey, nil
}
// if nip05 fails, fall through to try as pubkey
}
pk, err := nostr.PubKeyFromHex(value)
if err == nil {
return pk, nil
}
if prefix, decoded, err := nip19.Decode(value); err == nil {
switch prefix {
case "npub":
if pk, ok := decoded.(nostr.PubKey); ok {
return pk, nil
}
case "nprofile":
if profile, ok := decoded.(nostr.ProfilePointer); ok {
return profile.PublicKey, nil
}
}
}
return nostr.PubKey{}, fmt.Errorf("invalid pubkey (\"%s\"): expected hex, npub, or nprofile", value)
}
func checkPinnedID(str string, store *mmm.IndexingLayer) nostr.ID {
id, err := nostr.IDFromHex(str)
if err != nil {
prefix, data, err := nip19.Decode(str)
if err != nil {
return nostr.ZeroID
}
if prefix == "nevent" {
id = data.(nostr.EventPointer).ID
} else {
return nostr.ZeroID
}
}
for range store.QueryEvents(nostr.Filter{IDs: []nostr.ID{id}}, 1) {
// the event exists, so we're ok
return id
}
return nostr.ZeroID
}
func normalizeDomainInput(domain string) (string, error) {
// trim protocol prefixes
domain = strings.TrimPrefix(domain, "http://")
domain = strings.TrimPrefix(domain, "https://")
domain = strings.TrimPrefix(domain, "ws://")
domain = strings.TrimPrefix(domain, "wss://")
// trim trailing slashes and spaces again
domain = strings.TrimRight(domain, "/")
domain = strings.TrimSpace(domain)
if domain == "" {
return "", nil
}
// validate domain only contains letters, dots
if !domainRegex.MatchString(strings.Split(domain, ":")[0]) {
return "", fmt.Errorf("'%s' is an invalid domain", domain)
}
return domain, nil
}