Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

validate.gno

0.92 Kb · 39 lines
 1package common
 2
 3import (
 4	"errors"
 5
 6	"gno.land/p/nt/ufmt/v0"
 7)
 8
 9// Token keys ("<realm path>.<SYMBOL>") are embedded verbatim in pool paths
10// ("<key0>:<key1>:<fee>") and router routes ("<pool>*POOL*<pool>,<route>"),
11// so the key charset is an allowlist rather than a blocklist of separators:
12// package-path characters plus '.' plus the GRC20 symbol charset.
13func isTokenKeyChar(c rune) bool {
14	switch {
15	case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', c >= '0' && c <= '9':
16		return true
17	case c == '_', c == '-', c == '.', c == '/':
18		return true
19	}
20
21	return false
22}
23
24func validateTokenKey(tokenKey string) error {
25	if tokenKey == "" {
26		return errors.New(newErrorWithDetail(errInvalidTokenKey, "empty token key"))
27	}
28
29	for _, ch := range tokenKey {
30		if !isTokenKeyChar(ch) {
31			return errors.New(newErrorWithDetail(
32				errInvalidTokenKey,
33				ufmt.Sprintf("token key(%s) contains %q", tokenKey, string(ch)),
34			))
35		}
36	}
37
38	return nil
39}