-
Notifications
You must be signed in to change notification settings - Fork 246
/
helpers.go
53 lines (43 loc) · 1.19 KB
/
helpers.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
package walletconnect
import (
"encoding/json"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
)
// Returns namspace name, chainID and error
func parseCaip2ChainID(str string) (string, uint64, error) {
caip2 := strings.Split(str, ":")
if len(caip2) != 2 {
return "", 0, errors.New("CAIP-2 string is not valid")
}
chainIDStr := caip2[1]
chainID, err := strconv.ParseUint(chainIDStr, 10, 64)
if err != nil {
return "", 0, fmt.Errorf("CAIP-2 second value not valid Chain ID: %w", err)
}
return caip2[0], chainID, nil
}
// JSONProxyType provides a generic way of changing the JSON value before unmarshalling it into the target.
// transform function is called before unmarshalling.
type JSONProxyType struct {
target interface{}
transform func([]byte) ([]byte, error)
}
func (b *JSONProxyType) UnmarshalJSON(input []byte) error {
if b.transform == nil {
return errors.New("transform function is not set")
}
output, err := b.transform(input)
if err != nil {
return err
}
return json.Unmarshal(output, b.target)
}
func isValidNamespaceName(namespaceName string) bool {
pattern := "^[a-z0-9-]{3,8}$"
regex := regexp.MustCompile(pattern)
return regex.MatchString(namespaceName)
}