github.com/status-im/status-go@v1.1.0/common/utils.go (about)

     1  package common
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	"errors"
     6  	"reflect"
     7  	"regexp"
     8  	"strings"
     9  
    10  	"github.com/status-im/status-go/eth-node/crypto"
    11  	"github.com/status-im/status-go/protocol/identity/alias"
    12  	"github.com/status-im/status-go/protocol/protobuf"
    13  )
    14  
    15  var ErrInvalidDisplayNameRegExp = errors.New("only letters, numbers, underscores and hyphens allowed")
    16  var ErrInvalidDisplayNameEthSuffix = errors.New(`usernames ending with "eth" are not allowed`)
    17  var ErrInvalidDisplayNameNotAllowed = errors.New("name is not allowed")
    18  
    19  var DISPLAY_NAME_EXT = []string{"_eth", ".eth", "-eth"}
    20  
    21  func RecoverKey(m *protobuf.ApplicationMetadataMessage) (*ecdsa.PublicKey, error) {
    22  	if m.Signature == nil {
    23  		return nil, nil
    24  	}
    25  
    26  	recoveredKey, err := crypto.SigToPub(
    27  		crypto.Keccak256(m.Payload),
    28  		m.Signature,
    29  	)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	return recoveredKey, nil
    35  }
    36  
    37  func ValidateDisplayName(displayName *string) error {
    38  	name := strings.TrimSpace(*displayName)
    39  	*displayName = name
    40  
    41  	if name == "" {
    42  		return nil
    43  	}
    44  
    45  	// ^[\\w-\\s]{5,24}$ to allow spaces
    46  	if match, _ := regexp.MatchString("^[\\w-\\s]{5,24}$", name); !match {
    47  		return ErrInvalidDisplayNameRegExp
    48  	}
    49  
    50  	// .eth should not happen due to the regexp above, but let's keep it here in case the regexp is changed in the future
    51  	for _, ext := range DISPLAY_NAME_EXT {
    52  		if strings.HasSuffix(*displayName, ext) {
    53  			return ErrInvalidDisplayNameEthSuffix
    54  		}
    55  	}
    56  
    57  	if alias.IsAlias(name) {
    58  		return ErrInvalidDisplayNameNotAllowed
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  // implementation referenced from https://github.com/embarklabs/embark/blob/master/packages/plugins/ens/src/index.js
    65  func IsENSName(displayName string) bool {
    66  	if len(displayName) == 0 {
    67  		return false
    68  	}
    69  
    70  	if strings.HasSuffix(displayName, ".eth") {
    71  		return true
    72  	}
    73  
    74  	return false
    75  }
    76  
    77  func IsNil(i interface{}) bool {
    78  	if i == nil {
    79  		return true
    80  	}
    81  	switch reflect.TypeOf(i).Kind() {
    82  	case reflect.Ptr, reflect.Interface:
    83  		return reflect.ValueOf(i).IsNil()
    84  	}
    85  	return false
    86  }