Skip to content

Commit 41837d6

Browse files
Add literal functions
1 parent 84c198a commit 41837d6

File tree

2 files changed

+123
-4
lines changed

2 files changed

+123
-4
lines changed

tools/literal.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,23 @@ func IsAddressLazy(address string) bool {
3131
return len(address) == 36 && (strings.HasPrefix(address, "KT") || strings.HasPrefix(address, "tz"))
3232
}
3333

34+
var (
35+
addressRegex = regexp.MustCompile("(tz|KT)[0-9A-Za-z]{34}")
36+
operationHashRegex = regexp.MustCompile("(o)[0-9A-Za-z]{50}")
37+
bigMapKeyHashRegex = regexp.MustCompile("(expr)[0-9A-Za-z]{50}")
38+
)
39+
3440
// IsAddress -
3541
func IsAddress(str string) bool {
36-
regexString := "(tz|KT)[0-9A-Za-z]{34}"
37-
re := regexp.MustCompile(regexString)
38-
return re.MatchString(str)
42+
return addressRegex.MatchString(str)
43+
}
44+
45+
// IsOperationHash -
46+
func IsOperationHash(str string) bool {
47+
return operationHashRegex.MatchString(str)
48+
}
49+
50+
// IsBigMapKeyHash -
51+
func IsBigMapKeyHash(str string) bool {
52+
return bigMapKeyHashRegex.MatchString(str)
3953
}

tools/literal_test.go

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package tools
22

3-
import "testing"
3+
import (
4+
"testing"
5+
)
46

57
func TestIsContract(t *testing.T) {
68
tests := []struct {
@@ -20,6 +22,10 @@ func TestIsContract(t *testing.T) {
2022
name: "KT1Ap287P1NzsnToSJdA4aqSNjPomRaHBZSr",
2123
address: "KT1Ap287P1NzsnToSJdA4aqSNjPomRaHBZSr",
2224
want: true,
25+
}, {
26+
name: "expru2dKqDfZG8hu4wNGkiyunvq2hdSKuVYtcKta7BWP6Q18oNxKjS",
27+
address: "expru2dKqDfZG8hu4wNGkiyunvq2hdSKuVYtcKta7BWP6Q18oNxKjS",
28+
want: false,
2329
},
2430
}
2531
for _, tt := range tests {
@@ -30,3 +36,102 @@ func TestIsContract(t *testing.T) {
3036
})
3137
}
3238
}
39+
40+
func TestIsBigMapKeyHash(t *testing.T) {
41+
tests := []struct {
42+
name string
43+
str string
44+
want bool
45+
}{
46+
{
47+
name: "KT1Ap287P1NzsnToSJdA4aqSNjPomRaHBZSr",
48+
str: "KT1Ap287P1NzsnToSJdA4aqSNjPomRaHBZSr",
49+
want: false,
50+
}, {
51+
name: "exprtqoNj2hRg8PsPMaXLcy3dXjMM3B7nHKrRNqpfjbYpMbULbRj8k",
52+
str: "exprtqoNj2hRg8PsPMaXLcy3dXjMM3B7nHKrRNqpfjbYpMbULbRj8k",
53+
want: true,
54+
}, {
55+
name: "expru2dKqDfZG8hu4wNGkiyunvq2hdSKuVYtcKta7BWP6Q18oNxKjS",
56+
str: "expru2dKqDfZG8hu4wNGkiyunvq2hdSKuVYtcKta7BWP6Q18oNxKjS",
57+
want: true,
58+
},
59+
}
60+
for _, tt := range tests {
61+
t.Run(tt.name, func(t *testing.T) {
62+
if got := IsBigMapKeyHash(tt.str); got != tt.want {
63+
t.Errorf("IsBigMapKeyHash() = %v, want %v", got, tt.want)
64+
}
65+
})
66+
}
67+
}
68+
69+
func TestIsOperationHash(t *testing.T) {
70+
tests := []struct {
71+
name string
72+
str string
73+
want bool
74+
}{
75+
{
76+
name: "KT1Ap287P1NzsnToSJdA4aqSNjPomRaHBZSr",
77+
str: "KT1Ap287P1NzsnToSJdA4aqSNjPomRaHBZSr",
78+
want: false,
79+
}, {
80+
name: "exprtqoNj2hRg8PsPMaXLcy3dXjMM3B7nHKrRNqpfjbYpMbULbRj8k",
81+
str: "exprtqoNj2hRg8PsPMaXLcy3dXjMM3B7nHKrRNqpfjbYpMbULbRj8k",
82+
want: false,
83+
}, {
84+
name: "opDqhqYmqgmXTxcEcDXbJMWBThZkaQCovwV8BC3gwthEWYdPCWD",
85+
str: "opDqhqYmqgmXTxcEcDXbJMWBThZkaQCovwV8BC3gwthEWYdPCWD",
86+
want: true,
87+
}, {
88+
name: "opRRiHEQacoet5rq7jgcd33K66bkj5qCdThxGnCQwyZtdFjZ8ph",
89+
str: "opRRiHEQacoet5rq7jgcd33K66bkj5qCdThxGnCQwyZtdFjZ8ph",
90+
want: true,
91+
},
92+
}
93+
for _, tt := range tests {
94+
t.Run(tt.name, func(t *testing.T) {
95+
if got := IsOperationHash(tt.str); got != tt.want {
96+
t.Errorf("IsOperationHash() = %v, want %v", got, tt.want)
97+
}
98+
})
99+
}
100+
}
101+
102+
func TestIsAddress(t *testing.T) {
103+
tests := []struct {
104+
name string
105+
str string
106+
want bool
107+
}{
108+
{
109+
name: "KT1Ap287P1NzsnToSJdA4aqSNjPomRaHBZSr",
110+
str: "KT1Ap287P1NzsnToSJdA4aqSNjPomRaHBZSr",
111+
want: true,
112+
}, {
113+
name: "exprtqoNj2hRg8PsPMaXLcy3dXjMM3B7nHKrRNqpfjbYpMbULbRj8k",
114+
str: "exprtqoNj2hRg8PsPMaXLcy3dXjMM3B7nHKrRNqpfjbYpMbULbRj8k",
115+
want: false,
116+
}, {
117+
name: "opDqhqYmqgmXTxcEcDXbJMWBThZkaQCovwV8BC3gwthEWYdPCWD",
118+
str: "opDqhqYmqgmXTxcEcDXbJMWBThZkaQCovwV8BC3gwthEWYdPCWD",
119+
want: false,
120+
}, {
121+
name: "opRRiHEQacoet5rq7jgcd33K66bkj5qCdThxGnCQwyZtdFjZ8ph",
122+
str: "opRRiHEQacoet5rq7jgcd33K66bkj5qCdThxGnCQwyZtdFjZ8ph",
123+
want: false,
124+
}, {
125+
name: "tz1PUnJ3m435ZK4RTqhTEiSYF22YAUx5rEU1",
126+
str: "tz1PUnJ3m435ZK4RTqhTEiSYF22YAUx5rEU1",
127+
want: true,
128+
},
129+
}
130+
for _, tt := range tests {
131+
t.Run(tt.name, func(t *testing.T) {
132+
if got := IsAddress(tt.str); got != tt.want {
133+
t.Errorf("IsAddress() = %v, want %v", got, tt.want)
134+
}
135+
})
136+
}
137+
}

0 commit comments

Comments
 (0)