forked from JackalLabs/canine-chain
/
utils.go
92 lines (71 loc) · 1.56 KB
/
utils.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
package keeper
import (
"strings"
"github.com/MonikaCat/canine-chain/v2/x/rns/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
func GetTLD(name string) (string, error) {
for _, tld := range types.SupportedTLDs {
tldSize := len(tld)
if tldSize+1 >= len(name) {
return "", types.ErrNoTLD
}
checkingName := name[len(name)-tldSize:]
if checkingName == tld {
return tld, nil
}
}
return "", types.ErrNoTLD
}
func GetSubdomain(name string) (string, string, bool) {
if !strings.Contains(name, ".") {
return "", name, false
}
s := strings.Split(name, ".")
return s[0], s[1], true
}
func RemoveTLD(name string, tld string) (string, error) {
tldSize := len(tld)
if tldSize+1 >= len(name) {
return "", types.ErrNoTLD
}
checkingName := name[:len(name)-tldSize-1]
return checkingName, nil
}
func GetCostOfName(name string, tld string) (int64, error) {
baseCost := GetCost(tld)
var cost int64
chars := len(name)
switch chars {
case 0:
return 0, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Must be 1 or more characters.")
case 1:
cost = baseCost * 32
case 2:
cost = baseCost * 16
case 3:
cost = baseCost * 8
case 4:
cost = baseCost * 4
case 5:
cost = baseCost * 2
default:
cost = baseCost
}
return cost, nil
}
func GetNameAndTLD(full string) (string, string, error) {
tld, err := GetTLD(full)
if err != nil {
return "", "", err
}
name, err := RemoveTLD(full, tld)
if err != nil {
return "", "", err
}
return name, tld, nil
}
func GetCost(tld string) int64 {
cost := types.TLDCost[tld]
return cost
}