2023-11-15 15:58:15 +00:00
|
|
|
package common
|
2019-07-26 07:17:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2024-03-28 15:57:59 +00:00
|
|
|
"errors"
|
2024-07-01 18:52:57 +00:00
|
|
|
"reflect"
|
2024-03-28 15:57:59 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2024-11-25 12:13:47 +00:00
|
|
|
"github.com/status-im/status-go/internal/sentry"
|
2024-10-28 20:54:17 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2024-03-28 15:57:59 +00:00
|
|
|
"github.com/status-im/status-go/protocol/identity/alias"
|
2023-11-15 15:58:15 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2019-07-26 07:17:29 +00:00
|
|
|
)
|
|
|
|
|
2024-03-28 15:57:59 +00:00
|
|
|
var ErrInvalidDisplayNameRegExp = errors.New("only letters, numbers, underscores and hyphens allowed")
|
|
|
|
var ErrInvalidDisplayNameEthSuffix = errors.New(`usernames ending with "eth" are not allowed`)
|
|
|
|
var ErrInvalidDisplayNameNotAllowed = errors.New("name is not allowed")
|
|
|
|
|
2024-05-15 19:37:04 +00:00
|
|
|
var DISPLAY_NAME_EXT = []string{"_eth", ".eth", "-eth"}
|
|
|
|
|
2023-11-15 15:58:15 +00:00
|
|
|
func RecoverKey(m *protobuf.ApplicationMetadataMessage) (*ecdsa.PublicKey, error) {
|
2019-07-26 07:17:29 +00:00
|
|
|
if m.Signature == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
recoveredKey, err := crypto.SigToPub(
|
|
|
|
crypto.Keccak256(m.Payload),
|
|
|
|
m.Signature,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return recoveredKey, nil
|
|
|
|
}
|
2024-03-28 15:57:59 +00:00
|
|
|
|
|
|
|
func ValidateDisplayName(displayName *string) error {
|
|
|
|
name := strings.TrimSpace(*displayName)
|
|
|
|
*displayName = name
|
|
|
|
|
|
|
|
if name == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ^[\\w-\\s]{5,24}$ to allow spaces
|
|
|
|
if match, _ := regexp.MatchString("^[\\w-\\s]{5,24}$", name); !match {
|
|
|
|
return ErrInvalidDisplayNameRegExp
|
|
|
|
}
|
|
|
|
|
|
|
|
// .eth should not happen due to the regexp above, but let's keep it here in case the regexp is changed in the future
|
2024-05-15 19:37:04 +00:00
|
|
|
for _, ext := range DISPLAY_NAME_EXT {
|
|
|
|
if strings.HasSuffix(*displayName, ext) {
|
|
|
|
return ErrInvalidDisplayNameEthSuffix
|
|
|
|
}
|
2024-03-28 15:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if alias.IsAlias(name) {
|
|
|
|
return ErrInvalidDisplayNameNotAllowed
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-15 19:37:04 +00:00
|
|
|
|
|
|
|
// implementation referenced from https://github.com/embarklabs/embark/blob/master/packages/plugins/ens/src/index.js
|
|
|
|
func IsENSName(displayName string) bool {
|
|
|
|
if len(displayName) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(displayName, ".eth") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2024-07-01 18:52:57 +00:00
|
|
|
|
|
|
|
func IsNil(i interface{}) bool {
|
|
|
|
if i == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
switch reflect.TypeOf(i).Kind() {
|
|
|
|
case reflect.Ptr, reflect.Interface:
|
|
|
|
return reflect.ValueOf(i).IsNil()
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2024-09-26 22:37:32 +00:00
|
|
|
|
|
|
|
func LogOnPanic() {
|
2024-11-25 12:13:47 +00:00
|
|
|
err := recover()
|
|
|
|
if err == nil {
|
|
|
|
return
|
2024-09-26 22:37:32 +00:00
|
|
|
}
|
2024-11-25 12:13:47 +00:00
|
|
|
|
|
|
|
logutils.ZapLogger().Error("panic in goroutine",
|
|
|
|
zap.Any("error", err),
|
|
|
|
zap.Stack("stacktrace"))
|
|
|
|
|
|
|
|
sentry.RecoverError(err)
|
|
|
|
|
|
|
|
panic(err)
|
2024-09-26 22:37:32 +00:00
|
|
|
}
|