Search Apps Documentation Source Content File Folder Download Copy Actions Download

validation.gno

1.03 Kb · 51 lines
 1package admin
 2
 3import (
 4	"net/url"
 5	"strings"
 6)
 7
 8func assertValidAddress(addr address) {
 9	if !addr.IsValid() {
10		panic("invalid address")
11	}
12}
13
14func assertPositiveFaucetTarget(amount int64) {
15	if amount <= 0 {
16		panic("faucet target amount must be positive")
17	}
18}
19
20func assertPositiveUgnotAmount(amount int64) {
21	if amount <= 0 {
22		panic("amount must be greater than 0")
23	}
24}
25
26func normalizeExplorerURL(rawURL string) string {
27	if rawURL == "" {
28		panic("explorer URL is required")
29	}
30	if strings.TrimSpace(rawURL) != rawURL || strings.ContainsAny(rawURL, " \t\r\n") {
31		panic("invalid explorer URL")
32	}
33	if strings.ContainsAny(rawURL, "[]()") {
34		panic("invalid explorer URL")
35	}
36
37	parsed, err := url.Parse(rawURL)
38	if err != nil || parsed.Host == "" {
39		panic("invalid explorer URL")
40	}
41	if parsed.Scheme != "https" {
42		panic("explorer URL must use https")
43	}
44
45	normalizedURL := rawURL
46	for len(normalizedURL) > len("https://") && strings.HasSuffix(normalizedURL, "/") {
47		normalizedURL = normalizedURL[:len(normalizedURL)-1]
48	}
49
50	return normalizedURL
51}