status-go/common/utils.go
frank 38308d48f2
feat_: log on panic (#5849)
* feat_: log error and stacktrace when panic in goroutine

* test_: add test TestSafeGo

* chore_: rename logAndCall to call

* chore_: rename SafeGo to Go

* chore_: make lint-fix

* chore_: use t.Cleanup

* chore_: Revert "chore_: use t.Cleanup"

This reverts commit 4eb420d179cc0e208e84c13cb941e6b3d1ed9819.

* chore_: Revert "chore_: make lint-fix"

This reverts commit fcc995f157e671a4229b47419c3a0e4004b5fdab.

* chore_: Revert "chore_: rename SafeGo to Go"

This reverts commit a6d73d6df583f313032d79aac62f66328039cb55.

* chore_: Revert "chore_: rename logAndCall to call"

This reverts commit 8fbe993bedb9fbba67349a44f151e2dd5e3bc4cc.

* chore_: Revert "test_: add test TestSafeGo"

This reverts commit a1fa91839f3960398980c6bf456e6462ec944819.

* chore_: Revert "feat_: log error and stacktrace when panic in goroutine"

This reverts commit f612dd828fa2ce410d0e806fe773ecbe3e86a68a.

* feat_: log error and stacktrace when panic in goroutine

* chore_: make lint-fix

* chore_: rename logAndCall to call

* chore_: renaming LogOnPanic

* chore_: update rest goroutine function calls

* chore_: make lint-fix
2024-09-27 06:37:32 +08:00

96 lines
2.1 KiB
Go

package common
import (
"crypto/ecdsa"
"errors"
"reflect"
"regexp"
"runtime/debug"
"strings"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/protocol/identity/alias"
"github.com/status-im/status-go/protocol/protobuf"
)
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")
var DISPLAY_NAME_EXT = []string{"_eth", ".eth", "-eth"}
func RecoverKey(m *protobuf.ApplicationMetadataMessage) (*ecdsa.PublicKey, error) {
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
}
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
for _, ext := range DISPLAY_NAME_EXT {
if strings.HasSuffix(*displayName, ext) {
return ErrInvalidDisplayNameEthSuffix
}
}
if alias.IsAlias(name) {
return ErrInvalidDisplayNameNotAllowed
}
return nil
}
// 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
}
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
}
func LogOnPanic() {
if err := recover(); err != nil {
log.Error("panic in goroutine", "error", err, "stacktrace", string(debug.Stack()))
panic(err)
}
}