status-go/api/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

64 lines
1.4 KiB
Go

package api
import (
"bytes"
"encoding/hex"
"strconv"
"strings"
"github.com/status-im/status-go/common"
"github.com/status-im/status-go/eth-node/crypto"
)
// RunAsync runs the specified function asynchronously.
func RunAsync(f func() error) <-chan error {
resp := make(chan error, 1)
go func() {
defer common.LogOnPanic()
err := f()
resp <- err
close(resp)
}()
return resp
}
// HashMessage calculates the hash of a message to be safely signed by the keycard
// The hash is calulcated as
//
// keccak256("\x19Ethereum Signed Message:\n"${message length}${message}).
//
// This gives context to the signed message and prevents signing of transactions.
func HashMessage(message string) ([]byte, error) {
buf := bytes.NewBufferString("\x19Ethereum Signed Message:\n")
if value, ok := decodeHexStrict(message); ok {
if _, err := buf.WriteString(strconv.Itoa(len(value))); err != nil {
return nil, err
}
if _, err := buf.Write(value); err != nil {
return nil, err
}
} else {
if _, err := buf.WriteString(strconv.Itoa(len(message))); err != nil {
return nil, err
}
if _, err := buf.WriteString(message); err != nil {
return nil, err
}
}
return crypto.Keccak256(buf.Bytes()), nil
}
func decodeHexStrict(s string) ([]byte, bool) {
if !strings.HasPrefix(s, "0x") {
return nil, false
}
value, err := hex.DecodeString(s[2:])
if err != nil {
return nil, false
}
return value, true
}