2024-07-18 15:30:10 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2024-09-19 18:24:06 +00:00
|
|
|
"context"
|
2024-07-18 15:30:10 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/signal"
|
|
|
|
"github.com/status-im/status-go/transactions"
|
|
|
|
)
|
|
|
|
|
2024-09-19 18:24:06 +00:00
|
|
|
const (
|
|
|
|
Method_EthAccounts = "eth_accounts"
|
|
|
|
Method_EthRequestAccounts = "eth_requestAccounts"
|
|
|
|
Method_EthChainId = "eth_chainId"
|
|
|
|
Method_PersonalSign = "personal_sign"
|
2024-11-19 12:12:08 +00:00
|
|
|
Method_SignTypedDataV4 = "eth_signTypedData_v4"
|
2024-09-19 18:24:06 +00:00
|
|
|
Method_EthSendTransaction = "eth_sendTransaction"
|
|
|
|
Method_RequestPermissions = "wallet_requestPermissions"
|
|
|
|
Method_RevokePermissions = "wallet_revokePermissions"
|
|
|
|
Method_SwitchEthereumChain = "wallet_switchEthereumChain"
|
|
|
|
)
|
|
|
|
|
2024-07-18 15:30:10 +00:00
|
|
|
// errors
|
|
|
|
var (
|
|
|
|
ErrRequestMissingDAppData = errors.New("request missing dApp data")
|
|
|
|
ErrDAppIsNotPermittedByUser = errors.New("dApp is not permitted by user")
|
|
|
|
ErrEmptyRPCParams = errors.New("empty rpc params")
|
|
|
|
)
|
|
|
|
|
|
|
|
type RPCRequest struct {
|
|
|
|
JSONRPC string `json:"jsonrpc"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
Method string `json:"method"`
|
|
|
|
Params []interface{} `json:"params"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
IconURL string `json:"iconUrl"`
|
2024-09-19 18:24:06 +00:00
|
|
|
ChainID uint64 `json:"chainId"`
|
2024-07-18 15:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RPCCommand interface {
|
2024-09-19 18:24:06 +00:00
|
|
|
Execute(ctx context.Context, request RPCRequest) (interface{}, error)
|
2024-07-18 15:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RequestAccountsAcceptedArgs struct {
|
|
|
|
RequestID string `json:"requestId"`
|
|
|
|
Account types.Address `json:"account"`
|
|
|
|
ChainID uint64 `json:"chainId"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type SendTransactionAcceptedArgs struct {
|
|
|
|
RequestID string `json:"requestId"`
|
|
|
|
Hash types.Hash `json:"hash"`
|
|
|
|
}
|
|
|
|
|
2024-11-19 12:12:08 +00:00
|
|
|
type SignAcceptedArgs struct {
|
2024-08-13 09:11:24 +00:00
|
|
|
RequestID string `json:"requestId"`
|
|
|
|
Signature string `json:"signature"`
|
|
|
|
}
|
|
|
|
|
2024-07-18 15:30:10 +00:00
|
|
|
type RejectedArgs struct {
|
|
|
|
RequestID string `json:"requestId"`
|
|
|
|
}
|
|
|
|
|
2024-11-05 14:00:28 +00:00
|
|
|
type RecallDAppPermissionsArgs struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
2024-07-18 15:30:10 +00:00
|
|
|
type ClientSideHandlerInterface interface {
|
|
|
|
RequestShareAccountForDApp(dApp signal.ConnectorDApp) (types.Address, uint64, error)
|
|
|
|
RequestAccountsAccepted(args RequestAccountsAcceptedArgs) error
|
|
|
|
RequestAccountsRejected(args RejectedArgs) error
|
2024-11-05 14:00:28 +00:00
|
|
|
RecallDAppPermissions(args RecallDAppPermissionsArgs) error
|
2024-08-13 09:11:24 +00:00
|
|
|
|
|
|
|
RequestSendTransaction(dApp signal.ConnectorDApp, chainID uint64, txArgs *transactions.SendTxArgs) (types.Hash, error)
|
2024-07-18 15:30:10 +00:00
|
|
|
SendTransactionAccepted(args SendTransactionAcceptedArgs) error
|
|
|
|
SendTransactionRejected(args RejectedArgs) error
|
2024-08-13 09:11:24 +00:00
|
|
|
|
2024-11-19 12:12:08 +00:00
|
|
|
RequestSign(dApp signal.ConnectorDApp, challenge, address string, method string) (string, error)
|
|
|
|
SignAccepted(args SignAcceptedArgs) error
|
|
|
|
SignRejected(args RejectedArgs) error
|
2024-07-18 15:30:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type NetworkManagerInterface interface {
|
|
|
|
GetActiveNetworks() ([]*params.Network, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type RPCClientInterface interface {
|
|
|
|
CallRaw(body string) string
|
|
|
|
}
|
|
|
|
|
|
|
|
func RPCRequestFromJSON(inputJSON string) (RPCRequest, error) {
|
|
|
|
var request RPCRequest
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(inputJSON), &request)
|
|
|
|
if err != nil {
|
|
|
|
return RPCRequest{}, fmt.Errorf("error unmarshalling JSON: %v", err)
|
|
|
|
}
|
|
|
|
return request, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RPCRequest) Validate() error {
|
|
|
|
if r.URL == "" || r.Name == "" {
|
|
|
|
return ErrRequestMissingDAppData
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|