This repository has been archived by the owner on Oct 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
encode.go
303 lines (264 loc) · 5.96 KB
/
encode.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package util
import (
"encoding/hex"
"fmt"
"math/big"
"path/filepath"
"sort"
"strconv"
"strings"
gmath "github.com/ethereum/go-ethereum/common/math"
"github.com/huandu/xstrings"
"github.com/shopspring/decimal"
"github.com/ttacon/libphonenumber"
"github.com/tuvistavie/securerandom"
)
func NumberOrString(ns interface{}) (interface{}, error) {
switch t := ns.(type) {
case uint64:
return t, nil
case string:
r, err := strconv.ParseUint(t, 10, 64)
if err != nil {
return t, nil
}
return r, nil
default:
return "", fmt.Errorf("Parse %v failed", t)
}
}
func RandStr(length int) string {
str, _ := securerandom.Hex(length)
return str
}
func SnakeString(s string) string {
return strings.ToLower(xstrings.ToSnakeCase(s))
}
func CamelString(s string) string {
return xstrings.ToCamelCase(s)
}
func Padding(str string) string {
str = strings.TrimPrefix(str, "0x")
return xstrings.RightJustify(str, 64, "0")
}
func PaddingF(str string) string {
str = strings.TrimPrefix(str, "0x")
return xstrings.RightJustify(str, 64, "f")
}
func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func IntInSlice(a int, list []int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func ToWei(v int64) *big.Int {
return big.NewInt(v).Mul(big.NewInt(v), big.NewInt(int64(10)).Exp(big.NewInt(int64(10)), big.NewInt(int64(18)), nil))
}
func EthToWei(v decimal.Decimal) *big.Int {
decimalStr := v.Mul(decimal.NewFromFloat(1e18)).String()
wei, _ := new(big.Int).SetString(decimalStr, 10)
return wei
}
func BigToDecimal(v *big.Int) decimal.Decimal {
return decimal.NewFromBigInt(v, 0).Div(decimal.NewFromFloat(1e18))
}
func AddHex(s string, chain ...string) string {
prefix := "0x"
addr := s
if len(chain) > 0 {
switch chain[0] {
case "Tron":
prefix = "41"
case "Eth":
prefix = "0x"
case "PChain":
prefix = "0p"
}
}
if strings.HasPrefix(addr, prefix) {
return addr
}
return strings.ToLower(prefix + addr)
}
func U256(v string) *big.Int {
v = strings.TrimPrefix(v, "0x")
bn := new(big.Int)
bn.SetString(v, 16)
return gmath.U256(bn)
}
func DecodeInputU256(i *big.Int) string {
if i.Sign() == -1 {
pow := BigIntLog(new(big.Int).Abs(i), big.NewInt(16))
newHex := big.NewInt(16).Exp(big.NewInt(16), pow, nil)
newHex = newHex.Add(newHex, i)
return PaddingF(fmt.Sprintf("%x", newHex))
} else {
return fmt.Sprintf("%064s", fmt.Sprintf("%x", i))
}
}
func IntToString(i int) string {
return strconv.Itoa(i)
}
func StringToInt(s string) int {
if i, err := strconv.Atoi(s); err != nil {
return 0
} else {
return i
}
}
func StringToInt64(s string) int64 {
if i, err := strconv.ParseInt(s, 10, 64); err != nil {
return 0
} else {
return i
}
}
func IntToHex(i interface{}) string {
return fmt.Sprintf("0x%x", i)
}
func BigIntLog(i *big.Int, n *big.Int) *big.Int {
c := big.NewInt(1)
for {
quotient := new(big.Int).Quo(i, n)
if quotient.Sign() != 1 { // quot >= n
break
} else {
c = c.Add(c, big.NewInt(1))
i = quotient
}
}
return c
}
func DealNegU256(s string) *big.Int {
index := strings.IndexFunc(s, isSlash)
fis := new(big.Int)
exp := 0
if index != -1 {
fis = U256(s[index:])
exp = len(s) - index
}
pos := new(big.Int).Exp(big.NewInt(16), big.NewInt(int64(exp)), nil)
return new(big.Int).Sub(fis, pos)
}
func EncodeU256(s string) *big.Int {
if s[0] == 'f' {
return DealNegU256(s)
} else {
return U256(s)
}
}
func ResourceDecode(r string) []int {
var resource []int
resourceEnum := [5]int{0, 1, 2, 3, 4}
for _, index := range resourceEnum {
b := EncodeU256(r)
fs := new(big.Int).Lsh(big.NewInt(65535), uint(16*index))
ss := new(big.Int).And(b, fs)
ts := new(big.Int).Rsh(ss, uint(16*index))
resource = append(resource, int(ts.Int64()))
}
return resource
}
func isSlash(r rune) bool {
return r != 'f'
}
func GetFileType(uploadFilePath string) string {
fileType := strings.TrimPrefix(filepath.Ext(uploadFilePath), ".")
return fileType
}
func AppendIfMissing(slice []string, i string) []string {
for _, ele := range slice {
if ele == i {
return slice
}
}
return append(slice, i)
}
// mobile like +86 151 8888 9998
func BlurryMobile(mobile string) string {
mobileSlice := strings.Split(mobile, " ")
mobile = strings.Join(mobileSlice[1:], "")
return mobile[0:3] + "*****" + mobile[len(mobile)-3:]
}
func AnalysisMobile(mobile, region string) string {
if mobile == "" {
return ""
}
if region == "" {
region = "CN"
}
num, err := libphonenumber.Parse(mobile, region)
if err != nil {
return mobile
}
return libphonenumber.Format(num, libphonenumber.INTERNATIONAL)
}
func TrimHex(s string) string {
return strings.TrimPrefix(s, "0x")
}
func TrimTronHex(s string) string {
return strings.TrimPrefix(s, "41")
}
type Pair struct {
Key string
Value int
}
type PairList []Pair
func (p PairList) Len() int { return len(p) }
func (p PairList) Less(i, j int) bool { return p[i].Value < p[j].Value }
func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func RankByWordCount(wordFrequencies map[string]int) PairList {
pl := make(PairList, len(wordFrequencies))
i := 0
for k, v := range wordFrequencies {
pl[i] = Pair{k, v}
i++
}
sort.Sort(sort.Reverse(pl))
return pl
}
func StringsIntersection(a []string, b []string) []string {
var refresh []string
for _, v := range a {
if StringInSlice(v, b) {
refresh = append(refresh, v)
}
}
return refresh
}
func StringsExclude(a []string, b []string) []string {
var refresh []string
for _, v := range a {
if !StringInSlice(v, b) {
refresh = append(refresh, v)
}
}
return refresh
}
func BoolToString(b bool) string {
if b {
return "true"
}
return "false"
}
func HexToBytes(s string) []byte {
s = strings.TrimPrefix(s, "0x")
c := make([]byte, hex.DecodedLen(len(s)))
_, _ = hex.Decode(c, []byte(s))
return c
}
func BytesToHex(b []byte) string {
c := make([]byte, hex.EncodedLen(len(b)))
hex.Encode(c, b)
return string(c)
}