mirror of
https://github.com/status-im/status-go.git
synced 2025-02-12 23:06:51 +00:00
author shashankshampi <shashank.sanket1995@gmail.com> 1729780155 +0530 committer shashankshampi <shashank.sanket1995@gmail.com> 1730274350 +0530 test: Code Migration from status-cli-tests fix_: functional tests (#5979) * fix_: generate on test-functional * chore(test)_: fix functional test assertion --------- Co-authored-by: Siddarth Kumar <siddarthkay@gmail.com> feat(accounts)_: cherry-pick Persist acceptance of Terms of Use & Privacy policy (#5766) (#5977) * feat(accounts)_: Persist acceptance of Terms of Use & Privacy policy (#5766) The original GH issue https://github.com/status-im/status-mobile/issues/21113 came from a request from the Legal team. We must show to Status v1 users the new terms (Terms of Use & Privacy Policy) right after they upgrade to Status v2 from the stores. The solution we use is to create a flag in the accounts table, named hasAcceptedTerms. The flag will be set to true on the first account ever created in v2 and we provide a native call in mobile/status.go#AcceptTerms, which allows the client to persist the user's choice in case they are upgrading (from v1 -> v2, or from a v2 older than this PR). This solution is not the best because we should store the setting in a separate table, not in the accounts table. Related Mobile PR https://github.com/status-im/status-mobile/pull/21124 * fix(test)_: Compare addresses using uppercased strings --------- Co-authored-by: Icaro Motta <icaro.ldm@gmail.com> test_: restore account (#5960) feat_: `LogOnPanic` linter (#5969) * feat_: LogOnPanic linter * fix_: add missing defer LogOnPanic * chore_: make vendor * fix_: tests, address pr comments * fix_: address pr comments fix(ci)_: remove workspace and tmp dir This ensures we do not encounter weird errors like: ``` + ln -s /home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907 /home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907@tmp/go/src/github.com/status-im/status-go ln: failed to create symbolic link '/home/jenkins/workspace/go_prs_linux_x86_64_main_PR-5907@tmp/go/src/github.com/status-im/status-go': File exists script returned exit code 1 ``` Signed-off-by: Jakub Sokołowski <jakub@status.im> chore_: enable windows and macos CI build (#5840) - Added support for Windows and macOS in CI pipelines - Added missing dependencies for Windows and x86-64-darwin - Resolved macOS SDK version compatibility for darwin-x86_64 The `mkShell` override was necessary to ensure compatibility with the newer macOS SDK (version 11.0) for x86_64. The default SDK (10.12) was causing build failures because of the missing libs and frameworks. OverrideSDK creates a mapping from the default SDK in all package categories to the requested SDK (11.0). fix(contacts)_: fix trust status not being saved to cache when changed (#5965) Fixes https://github.com/status-im/status-desktop/issues/16392 cleanup added logger and cleanup review comments changes fix_: functional tests (#5979) * fix_: generate on test-functional * chore(test)_: fix functional test assertion --------- Co-authored-by: Siddarth Kumar <siddarthkay@gmail.com> feat(accounts)_: cherry-pick Persist acceptance of Terms of Use & Privacy policy (#5766) (#5977) * feat(accounts)_: Persist acceptance of Terms of Use & Privacy policy (#5766) The original GH issue https://github.com/status-im/status-mobile/issues/21113 came from a request from the Legal team. We must show to Status v1 users the new terms (Terms of Use & Privacy Policy) right after they upgrade to Status v2 from the stores. The solution we use is to create a flag in the accounts table, named hasAcceptedTerms. The flag will be set to true on the first account ever created in v2 and we provide a native call in mobile/status.go#AcceptTerms, which allows the client to persist the user's choice in case they are upgrading (from v1 -> v2, or from a v2 older than this PR). This solution is not the best because we should store the setting in a separate table, not in the accounts table. Related Mobile PR https://github.com/status-im/status-mobile/pull/21124 * fix(test)_: Compare addresses using uppercased strings --------- Co-authored-by: Icaro Motta <icaro.ldm@gmail.com> test_: restore account (#5960) feat_: `LogOnPanic` linter (#5969) * feat_: LogOnPanic linter * fix_: add missing defer LogOnPanic * chore_: make vendor * fix_: tests, address pr comments * fix_: address pr comments chore_: enable windows and macos CI build (#5840) - Added support for Windows and macOS in CI pipelines - Added missing dependencies for Windows and x86-64-darwin - Resolved macOS SDK version compatibility for darwin-x86_64 The `mkShell` override was necessary to ensure compatibility with the newer macOS SDK (version 11.0) for x86_64. The default SDK (10.12) was causing build failures because of the missing libs and frameworks. OverrideSDK creates a mapping from the default SDK in all package categories to the requested SDK (11.0). fix(contacts)_: fix trust status not being saved to cache when changed (#5965) Fixes https://github.com/status-im/status-desktop/issues/16392 test_: remove port bind chore(wallet)_: move route execution code to separate module chore_: replace geth logger with zap logger (#5962) closes: #6002 feat(telemetry)_: add metrics for message reliability (#5899) * feat(telemetry)_: track message reliability Add metrics for dial errors, missed messages, missed relevant messages, and confirmed delivery. * fix_: handle error from json marshal chore_: use zap logger as request logger iterates: status-im/status-desktop#16536 test_: unique project per run test_: use docker compose v2, more concrete project name fix(codecov)_: ignore folders without tests Otherwise Codecov reports incorrect numbers when making changes. https://docs.codecov.com/docs/ignoring-paths Signed-off-by: Jakub Sokołowski <jakub@status.im> test_: verify schema of signals during init; fix schema verification warnings (#5947) fix_: update defaultGorushURL (#6011) fix(tests)_: use non-standard port to avoid conflicts We have observed `nimbus-eth2` build failures reporting this port: ```json { "lvl": "NTC", "ts": "2024-10-28 13:51:32.308+00:00", "msg": "REST HTTP server could not be started", "topics": "beacnde", "address": "127.0.0.1:5432", "reason": "(98) Address already in use" } ``` https://ci.status.im/job/nimbus-eth2/job/platforms/job/linux/job/x86_64/job/main/job/PR-6683/3/ Signed-off-by: Jakub Sokołowski <jakub@status.im> fix_: create request logger ad-hoc in tests Fixes `TestCall` failing when run concurrently. chore_: configure codecov (#6005) * chore_: configure codecov * fix_: after_n_builds
411 lines
11 KiB
Go
411 lines
11 KiB
Go
package web3provider
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
signercore "github.com/ethereum/go-ethereum/signer/core/apitypes"
|
|
"github.com/status-im/status-go/account"
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
"github.com/status-im/status-go/logutils"
|
|
"github.com/status-im/status-go/services/typeddata"
|
|
"github.com/status-im/status-go/transactions"
|
|
)
|
|
|
|
const Web3SendAsyncReadOnly = "web3-send-async-read-only"
|
|
const RequestAPI = "api-request"
|
|
|
|
const Web3SendAsyncCallback = "web3-send-async-callback"
|
|
const ResponseAPI = "api-response"
|
|
const Web3ResponseError = "web3-response-error"
|
|
|
|
const PermissionWeb3 = "web3"
|
|
const PermissionContactCode = "contact-code"
|
|
const PermissionUnknown = "unknown"
|
|
|
|
const ethCoinbase = "eth_coinbase"
|
|
|
|
var ErrorInvalidAPIRequest = errors.New("invalid API request")
|
|
var ErrorUnknownPermission = errors.New("unknown permission")
|
|
|
|
var authMethods = []string{
|
|
"eth_accounts",
|
|
"eth_coinbase",
|
|
"eth_sendTransaction",
|
|
"eth_sign",
|
|
"keycard_signTypedData",
|
|
"eth_signTypedData",
|
|
"eth_signTypedData_v3",
|
|
"personal_sign",
|
|
}
|
|
|
|
var signMethods = []string{
|
|
"eth_sign",
|
|
"personal_sign",
|
|
"eth_signTypedData",
|
|
"eth_signTypedData_v3",
|
|
"eth_signTypedData_v4",
|
|
}
|
|
|
|
var accMethods = []string{
|
|
"eth_accounts",
|
|
"eth_coinbase",
|
|
}
|
|
|
|
func NewAPI(s *Service) *API {
|
|
return &API{
|
|
s: s,
|
|
}
|
|
}
|
|
|
|
// API is class with methods available over RPC.
|
|
type API struct {
|
|
s *Service
|
|
}
|
|
|
|
type ETHPayload struct {
|
|
ID interface{} `json:"id,omitempty"`
|
|
JSONRPC string `json:"jsonrpc"`
|
|
From string `json:"from"`
|
|
Method string `json:"method"`
|
|
Params []interface{} `json:"params"`
|
|
Password string `json:"password,omitempty"`
|
|
ChainID uint64 `json:"chainId,omitempty"`
|
|
}
|
|
|
|
type JSONRPCResponse struct {
|
|
ID interface{} `json:"id,omitempty"`
|
|
JSONRPC string `json:"jsonrpc"`
|
|
Result interface{} `json:"result"`
|
|
}
|
|
type Web3SendAsyncReadOnlyRequest struct {
|
|
Title string `json:"title,omitempty"`
|
|
MessageID interface{} `json:"messageId"`
|
|
Payload ETHPayload `json:"payload"`
|
|
Hostname string `json:"hostname"`
|
|
Address string `json:"address,omitempty"`
|
|
}
|
|
|
|
type Web3SendAsyncReadOnlyError struct {
|
|
Code uint `json:"code"`
|
|
Message string `json:"message,omitempty"`
|
|
}
|
|
|
|
type Web3SendAsyncReadOnlyResponse struct {
|
|
ProviderResponse
|
|
|
|
MessageID interface{} `json:"messageId"`
|
|
Error interface{} `json:"error,omitempty"`
|
|
Result interface{} `json:"result,omitempty"`
|
|
}
|
|
|
|
type APIRequest struct {
|
|
MessageID interface{} `json:"messageId,omitempty"`
|
|
Address string `json:"address,omitempty"`
|
|
Hostname string `json:"hostname"`
|
|
Permission string `json:"permission"`
|
|
}
|
|
|
|
type APIResponse struct {
|
|
ProviderResponse
|
|
|
|
MessageID interface{} `json:"messageId,omitempty"`
|
|
Permission string `json:"permission"`
|
|
Data interface{} `json:"data,omitempty"`
|
|
IsAllowed bool `json:"isAllowed"`
|
|
}
|
|
|
|
type ProviderResponse struct {
|
|
ResponseType string `json:"type"`
|
|
}
|
|
|
|
func (api *API) ProcessRequest(requestType string, payload json.RawMessage) (interface{}, error) {
|
|
switch requestType {
|
|
case RequestAPI:
|
|
var request APIRequest
|
|
if err := json.Unmarshal([]byte(payload), &request); err != nil {
|
|
return nil, err
|
|
}
|
|
return api.ProcessAPIRequest(request)
|
|
case Web3SendAsyncReadOnly:
|
|
var request Web3SendAsyncReadOnlyRequest
|
|
if err := json.Unmarshal(payload, &request); err != nil {
|
|
return nil, err
|
|
}
|
|
return api.ProcessWeb3ReadOnlyRequest(request)
|
|
default:
|
|
return nil, errors.New("invalid request type")
|
|
}
|
|
}
|
|
|
|
func contains(item string, elems []string) bool {
|
|
for _, x := range elems {
|
|
if x == item {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// web3Call returns a response from a read-only eth RPC method
|
|
func (api *API) web3Call(request Web3SendAsyncReadOnlyRequest) (*Web3SendAsyncReadOnlyResponse, error) {
|
|
var rpcResult interface{}
|
|
var errMsg interface{}
|
|
|
|
if request.Payload.Method == "personal_ecRecover" {
|
|
data, err := hexutil.Decode(request.Payload.Params[0].(string))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
sig, err := hexutil.Decode(request.Payload.Params[1].(string))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
addr, err := api.EcRecover(data, sig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rpcResult = JSONRPCResponse{
|
|
JSONRPC: "2.0",
|
|
ID: request.Payload.ID,
|
|
Result: addr.String(),
|
|
}
|
|
} else {
|
|
ethPayload, err := json.Marshal(request.Payload)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
response := api.s.rpcClient.CallRaw(string(ethPayload))
|
|
if response == "" {
|
|
errMsg = Web3ResponseError
|
|
}
|
|
rpcResult = json.RawMessage(response)
|
|
}
|
|
|
|
return &Web3SendAsyncReadOnlyResponse{
|
|
ProviderResponse: ProviderResponse{
|
|
ResponseType: Web3SendAsyncCallback,
|
|
},
|
|
MessageID: request.MessageID,
|
|
Error: errMsg,
|
|
Result: rpcResult,
|
|
}, nil
|
|
}
|
|
|
|
func (api *API) web3NoPermission(request Web3SendAsyncReadOnlyRequest) (*Web3SendAsyncReadOnlyResponse, error) {
|
|
return &Web3SendAsyncReadOnlyResponse{
|
|
ProviderResponse: ProviderResponse{
|
|
ResponseType: Web3SendAsyncCallback,
|
|
},
|
|
MessageID: request.MessageID,
|
|
Error: Web3SendAsyncReadOnlyError{
|
|
Code: 4100,
|
|
Message: "The requested method and/or account has not been authorized by the user.",
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (api *API) web3AccResponse(request Web3SendAsyncReadOnlyRequest) (*Web3SendAsyncReadOnlyResponse, error) {
|
|
dappsAddress, err := api.s.accountsDB.GetDappsAddress()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var result interface{}
|
|
if request.Payload.Method == ethCoinbase {
|
|
result = dappsAddress
|
|
} else {
|
|
result = []types.Address{dappsAddress}
|
|
}
|
|
|
|
return &Web3SendAsyncReadOnlyResponse{
|
|
ProviderResponse: ProviderResponse{
|
|
ResponseType: Web3SendAsyncCallback,
|
|
},
|
|
MessageID: request.MessageID,
|
|
Result: JSONRPCResponse{
|
|
JSONRPC: "2.0",
|
|
ID: request.Payload.ID,
|
|
Result: result,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (api *API) getVerifiedWalletAccount(address, password string) (*account.SelectedExtKey, error) {
|
|
exists, err := api.s.accountsDB.AddressExists(types.HexToAddress(address))
|
|
if err != nil {
|
|
logutils.ZapLogger().Error("failed to query db for a given address",
|
|
zap.String("address", address),
|
|
zap.Error(err),
|
|
)
|
|
return nil, err
|
|
}
|
|
|
|
if !exists {
|
|
logutils.ZapLogger().Error("failed to get a selected account", zap.Error(transactions.ErrInvalidTxSender))
|
|
return nil, transactions.ErrAccountDoesntExist
|
|
}
|
|
|
|
key, err := api.s.accountsManager.VerifyAccountPassword(api.s.config.KeyStoreDir, address, password)
|
|
if err != nil {
|
|
logutils.ZapLogger().Error("failed to verify account", zap.String("account", address), zap.Error(err))
|
|
return nil, err
|
|
}
|
|
|
|
return &account.SelectedExtKey{
|
|
Address: key.Address,
|
|
AccountKey: key,
|
|
}, nil
|
|
}
|
|
|
|
func (api *API) web3SignatureResponse(request Web3SendAsyncReadOnlyRequest) (*Web3SendAsyncReadOnlyResponse, error) {
|
|
var err error
|
|
var signature types.HexBytes
|
|
if request.Payload.Method == "eth_signTypedData" || request.Payload.Method == "eth_signTypedData_v3" {
|
|
raw := json.RawMessage(request.Payload.Params[1].(string))
|
|
var data typeddata.TypedData
|
|
err = json.Unmarshal(raw, &data)
|
|
if err == nil {
|
|
signature, err = api.signTypedData(data, request.Payload.From, request.Payload.Password)
|
|
}
|
|
} else if request.Payload.Method == "eth_signTypedData_v4" {
|
|
signature, err = api.signTypedDataV4(request.Payload.Params[1].(signercore.TypedData), request.Payload.From, request.Payload.Password)
|
|
} else {
|
|
signature, err = api.signMessage(request.Payload.Params[0], request.Payload.From, request.Payload.Password)
|
|
}
|
|
|
|
if err != nil {
|
|
logutils.ZapLogger().Error("could not sign message", zap.Error(err))
|
|
return &Web3SendAsyncReadOnlyResponse{
|
|
ProviderResponse: ProviderResponse{
|
|
ResponseType: Web3SendAsyncCallback,
|
|
},
|
|
MessageID: request.MessageID,
|
|
Error: Web3SendAsyncReadOnlyError{
|
|
Code: 4100,
|
|
Message: err.Error(),
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
return &Web3SendAsyncReadOnlyResponse{
|
|
ProviderResponse: ProviderResponse{
|
|
ResponseType: Web3SendAsyncCallback,
|
|
},
|
|
MessageID: request.MessageID,
|
|
Result: JSONRPCResponse{
|
|
JSONRPC: "2.0",
|
|
ID: request.Payload.ID,
|
|
Result: signature,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func (api *API) ProcessWeb3ReadOnlyRequest(request Web3SendAsyncReadOnlyRequest) (*Web3SendAsyncReadOnlyResponse, error) {
|
|
hasPermission, err := api.s.permissionsDB.HasPermission(request.Hostname, request.Address, PermissionWeb3)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if contains(request.Payload.Method, authMethods) && !hasPermission {
|
|
return api.web3NoPermission(request)
|
|
}
|
|
|
|
if contains(request.Payload.Method, accMethods) {
|
|
return api.web3AccResponse(request)
|
|
} else if contains(request.Payload.Method, signMethods) {
|
|
return api.web3SignatureResponse(request)
|
|
} else if request.Payload.Method == "eth_sendTransaction" {
|
|
jsonString, err := json.Marshal(request.Payload.Params[0])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var trxArgs transactions.SendTxArgs
|
|
if err := json.Unmarshal(jsonString, &trxArgs); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
hash, err := api.sendTransaction(request.Payload.ChainID, trxArgs, request.Payload.Password, Web3SendAsyncReadOnly)
|
|
if err != nil {
|
|
logutils.ZapLogger().Error("could not send transaction message", zap.Error(err))
|
|
return &Web3SendAsyncReadOnlyResponse{
|
|
ProviderResponse: ProviderResponse{
|
|
ResponseType: Web3SendAsyncCallback,
|
|
},
|
|
MessageID: request.MessageID,
|
|
Error: Web3ResponseError,
|
|
}, nil
|
|
}
|
|
|
|
return &Web3SendAsyncReadOnlyResponse{
|
|
ProviderResponse: ProviderResponse{
|
|
ResponseType: Web3SendAsyncCallback,
|
|
},
|
|
MessageID: request.MessageID,
|
|
Result: JSONRPCResponse{
|
|
JSONRPC: "2.0",
|
|
ID: request.Payload.ID,
|
|
Result: hash,
|
|
},
|
|
}, nil
|
|
} else {
|
|
return api.web3Call(request)
|
|
}
|
|
}
|
|
|
|
func (api *API) ProcessAPIRequest(request APIRequest) (*APIResponse, error) {
|
|
if request.Permission == "" {
|
|
return nil, ErrorInvalidAPIRequest
|
|
}
|
|
hasPermission, err := api.s.permissionsDB.HasPermission(request.Hostname, request.Address, request.Permission)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !hasPermission {
|
|
// Not allowed
|
|
return &APIResponse{
|
|
ProviderResponse: ProviderResponse{
|
|
ResponseType: ResponseAPI,
|
|
},
|
|
Permission: request.Permission,
|
|
MessageID: request.MessageID,
|
|
IsAllowed: false,
|
|
}, nil
|
|
}
|
|
var data interface{}
|
|
switch request.Permission {
|
|
case PermissionWeb3:
|
|
dappsAddress, err := api.s.accountsDB.GetDappsAddress()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
response := make([]interface{}, 1)
|
|
response[0] = dappsAddress
|
|
data = response
|
|
case PermissionContactCode:
|
|
pubKey, err := api.s.accountsDB.GetPublicKey()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
data = pubKey
|
|
default:
|
|
return nil, ErrorUnknownPermission
|
|
}
|
|
return &APIResponse{
|
|
ProviderResponse: ProviderResponse{
|
|
ResponseType: ResponseAPI,
|
|
},
|
|
Permission: request.Permission,
|
|
MessageID: request.MessageID,
|
|
Data: data,
|
|
IsAllowed: true,
|
|
}, nil
|
|
}
|