-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.go
201 lines (168 loc) · 6.26 KB
/
utilities.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package paymail
import (
"fmt"
"net/url"
"regexp"
"strings"
"time"
)
var (
emailRegExp = regexp.MustCompile(`[^a-zA-Z0-9-_.@+]`)
pathNameRegExp = regexp.MustCompile(`[^a-zA-Z0-9-_]`)
portRegExp = regexp.MustCompile(`:\d*$`)
)
// SanitisedPaymail contains elements of a sanitized paymail address.
// All elements are lowercase.
type SanitisedPaymail struct {
Alias, Domain, Address string
}
// ValidateAndSanitisePaymail will take a paymail address or handle,
// convert to a paymail address if it's a handle,
// validate that address, then sanitize it if it is valid.
// If the address or handle isn't valid, an error will be returned.
func ValidateAndSanitisePaymail(paymail string, isBeta bool) (*SanitisedPaymail, error) {
h := ConvertHandle(paymail, isBeta)
if err := ValidatePaymail(h); err != nil {
return nil, err
}
a, d, ad := SanitizePaymail(h)
return &SanitisedPaymail{
Alias: a,
Domain: d,
Address: ad,
}, nil
}
// SanitizePaymail will take an input and return the sanitized version ([email protected])
//
// Alias is the first part of the address (alias @)
// Domain is the lowercase sanitized version (domain.tld)
// Address is the full sanitized paymail address ([email protected])
func SanitizePaymail(paymailAddress string) (alias, domain, address string) {
// Sanitize the paymail address
address = SanitizeEmail(paymailAddress)
// Split the email parts (alias @ domain)
parts := strings.Split(address, "@")
// Sanitize the domain name (force to lowercase, remove www.)
if len(parts) > 1 {
domain, _ = SanitizeDomain(parts[1])
}
// Set the alias (lowercase, no spaces)
alias = strings.TrimSpace(strings.ToLower(parts[0]))
// Paymail address does not meet the basic requirement of an email address
// Since we don't return an error, we will return an empty result
if len(alias) == 0 || len(domain) == 0 {
address = ""
}
return
}
// ValidatePaymail will do a basic validation on the paymail format (email address format)
//
// This will not check to see if the paymail address is active via the provider
func ValidatePaymail(paymailAddress string) error {
isValid := IsValidEmail(paymailAddress)
// Validate the format for the paymail address (paymail addresses follow conventional email requirements)
if !isValid {
return fmt.Errorf("paymail address failed format validation: %s", paymailAddress)
}
return nil
}
// ValidateDomain will do a basic validation on the domain format
//
// This will not check to see if the domain is an active paymail provider
// This will not check DNS records to make sure the domain is active
func ValidateDomain(domain string) error {
// Check for a real domain (require at least one period)
if !strings.Contains(domain, ".") {
return fmt.Errorf("domain name is invalid: %s", domain)
} else if !IsValidHost(domain) { // Basic DNS check (not a REAL domain name check)
return fmt.Errorf("domain name failed host check: %s", domain)
}
return nil
}
// ConvertHandle will convert a $handle or 1handle to a paymail address
//
// For HandCash: $handle = [email protected] or [email protected]
// For RelayX: 1handle = [email protected]
func ConvertHandle(handle string, isBeta bool) string {
if strings.HasPrefix(handle, "$") {
if isBeta {
return strings.ToLower(strings.Replace(handle, "$", "", -1)) + "@beta.handcash.io"
}
return strings.ToLower(strings.Replace(handle, "$", "", -1)) + "@handcash.io"
} else if strings.HasPrefix(handle, "1") && len(handle) < 25 && !strings.Contains(handle, "@") {
return strings.ToLower(strings.Replace(handle, "1", "", -1)) + "@relayx.io"
}
return handle
}
// ValidateTimestamp will test if the timestamp is valid
//
// This is used to validate the "dt" parameter in resolve_address.go
// Allowing 3 minutes before/after for
func ValidateTimestamp(timestamp string) error {
// Parse the time using the RFC3339 layout
dt, err := time.Parse(time.RFC3339, timestamp)
if err != nil {
return err
}
// Timestamp cannot be more than 2 minutes in the past
// Specs: http://bsvalias.org/04-02-sender-validation.html
if dt.Before(time.Now().UTC().Add(-2 * time.Minute)) {
return fmt.Errorf("timestamp: %s is in the past", timestamp)
} else if dt.After(time.Now().UTC().Add(2 * time.Minute)) {
return fmt.Errorf("timestamp: %s is in the future", timestamp)
}
return nil
}
// SanitizeDomain will take an input and return the sanitized version (domain.tld)
//
// This will not check to see if the domain is an active paymail provider
// Example: SanitizeDomain(" www.Google.com ")
// Result: google.com
func SanitizeDomain(original string) (string, error) {
// Try to see if we have a host
if len(original) == 0 {
return original, nil
}
if !strings.HasPrefix(original, "http") {
// The http part is temporary, we just need it for url.Parse to work
original = "http://" + strings.TrimSpace(original)
}
u, err := url.Parse(original)
if err != nil {
return original, err
}
u.Host = strings.ToLower(u.Host) // Generally all domains should be uniform and lowercase
u.Host = strings.TrimPrefix(u.Host, "www.")
u.Host = removePort(u.Host)
return u.Host, nil
}
func removePort(host string) string {
return portRegExp.ReplaceAllString(host, "")
}
// SanitizeEmail will take an input and return the sanitized version
//
// This will sanitize the email address (force to lowercase, remove spaces, etc.)
// Example: SanitizeEmail(" John.Doe@Gmail ", false)
// Result: johndoe@gmail
func SanitizeEmail(original string) string {
original = strings.ToLower(original)
original = strings.Replace(original, "mailto:", "", -1)
original = strings.TrimSpace(original)
return emailRegExp.ReplaceAllString(original, "")
}
// SanitizePathName returns a formatted path compliant name.
//
// View examples: sanitize_test.go
func SanitizePathName(original string) string {
return pathNameRegExp.ReplaceAllString(original, "")
}
// replaceAliasDomain will replace the alias and domain with the correct values
func replaceAliasDomain(urlString, alias, domain string) string {
urlString = strings.ReplaceAll(urlString, "{alias}", alias)
urlString = strings.ReplaceAll(urlString, "{domain.tld}", domain)
return urlString
}
// replacePubKey will replace the PubKey with the correct values
func replacePubKey(urlString, pubKey string) string {
return strings.ReplaceAll(urlString, "{pubkey}", pubKey)
}