github.com/status-im/status-go@v1.1.0/services/wallet/walletconnect/helpers.go (about)

     1  package walletconnect
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"regexp"
     8  	"strconv"
     9  	"strings"
    10  )
    11  
    12  // Returns namspace name, chainID and error
    13  func parseCaip2ChainID(str string) (string, uint64, error) {
    14  	caip2 := strings.Split(str, ":")
    15  	if len(caip2) != 2 {
    16  		return "", 0, errors.New("CAIP-2 string is not valid")
    17  	}
    18  
    19  	chainIDStr := caip2[1]
    20  	chainID, err := strconv.ParseUint(chainIDStr, 10, 64)
    21  	if err != nil {
    22  		return "", 0, fmt.Errorf("CAIP-2 second value not valid Chain ID: %w", err)
    23  	}
    24  	return caip2[0], chainID, nil
    25  }
    26  
    27  // JSONProxyType provides a generic way of changing the JSON value before unmarshalling it into the target.
    28  // transform function is called before unmarshalling.
    29  type JSONProxyType struct {
    30  	target    interface{}
    31  	transform func([]byte) ([]byte, error)
    32  }
    33  
    34  func (b *JSONProxyType) UnmarshalJSON(input []byte) error {
    35  	if b.transform == nil {
    36  		return errors.New("transform function is not set")
    37  	}
    38  
    39  	output, err := b.transform(input)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	return json.Unmarshal(output, b.target)
    45  }
    46  
    47  func isValidNamespaceName(namespaceName string) bool {
    48  	pattern := "^[a-z0-9-]{3,8}$"
    49  
    50  	regex := regexp.MustCompile(pattern)
    51  
    52  	return regex.MatchString(namespaceName)
    53  }