mirror of
https://github.com/status-im/status-go.git
synced 2026-08-31 00:51:12 +00:00
`common` was a grab-bag with no domain: the issue's own preamble names it as the kind of package that must not exist. Every symbol moves to the package that owns it, and the directory is deleted. common/dbsetup -> internal/db/dbsetup common/devices.go -> internal/platform common/pausable*.go -> internal/pausable LogOnPanic -> internal/panics TruncateWithDot(N) -> internal/logutils RecoverKey, ValidateDisplayName, display-name errors -> protocol/common IpfsGatewayURL -> internal/ipfs.GatewayURL Archives/TorrentTorrentsRelativePath, MainnetEthereumNetworkURL -> params StatusService -> pkg/backend/node ErrBigIntSetFromString -> services/wallet IsNil, Ptr -> inlined at their call sites IsENSName -> deleted, it had no callers Notes: - LogOnPanic gets its own package rather than living in logutils. It reports to Sentry, and logutils is imported by nearly everything: put the guard in logutils and the Sentry SDK lands in every dependency graph in the tree (213 -> 250 packages). internal/panics imports logutils and sentry, which is the direction root `common` had. - TruncateWithDot is log redaction, not string formatting: every one of its 121 call sites is inside a log or error message, so it belongs next to the logger. - Moving RecoverKey and ValidateDisplayName into protocol/common removes the common -> protocol layering inversion; all their callers were already inside protocol/. - Makefile lint-panics target follows LogOnPanic to its new path. refs #7067
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package backend
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/status-im/status-go/internal/crypto"
|
|
"github.com/status-im/status-go/internal/panics"
|
|
)
|
|
|
|
// RunAsync runs the specified function asynchronously.
|
|
func RunAsync(f func() error) <-chan error {
|
|
resp := make(chan error, 1)
|
|
go func() {
|
|
defer panics.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
|
|
}
|