2017-05-16 12:09:52 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2017-09-04 12:56:58 +00:00
|
|
|
"context"
|
2019-07-25 05:35:09 +00:00
|
|
|
"database/sql"
|
2018-03-22 12:31:12 +00:00
|
|
|
"errors"
|
2018-02-09 13:37:56 +00:00
|
|
|
"fmt"
|
2018-11-06 06:26:12 +00:00
|
|
|
"math/big"
|
2019-08-20 15:38:40 +00:00
|
|
|
"path/filepath"
|
2017-05-25 13:14:52 +00:00
|
|
|
"sync"
|
2019-03-29 10:20:24 +00:00
|
|
|
"time"
|
2017-05-25 13:14:52 +00:00
|
|
|
|
2019-03-28 14:56:21 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-05-16 12:09:52 +00:00
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
2018-08-16 11:37:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2019-01-24 15:44:46 +00:00
|
|
|
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
2019-03-29 10:20:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
2019-08-28 07:49:03 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2018-03-20 18:35:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-05-04 10:53:41 +00:00
|
|
|
gethnode "github.com/ethereum/go-ethereum/node"
|
2018-11-21 10:22:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
2019-07-25 05:35:09 +00:00
|
|
|
"github.com/status-im/status-go/appdatabase"
|
2019-07-17 22:25:42 +00:00
|
|
|
"github.com/status-im/status-go/crypto"
|
2019-08-20 15:38:40 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2019-03-29 10:20:24 +00:00
|
|
|
"github.com/status-im/status-go/mailserver/registry"
|
2019-08-20 15:38:40 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts"
|
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/node"
|
|
|
|
"github.com/status-im/status-go/notifications/push/fcm"
|
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
2019-08-20 15:38:40 +00:00
|
|
|
accountssvc "github.com/status-im/status-go/services/accounts"
|
2019-07-25 05:35:09 +00:00
|
|
|
"github.com/status-im/status-go/services/browsers"
|
2019-09-04 06:25:33 +00:00
|
|
|
"github.com/status-im/status-go/services/mailservers"
|
2019-07-25 05:35:09 +00:00
|
|
|
"github.com/status-im/status-go/services/permissions"
|
2018-04-10 10:02:54 +00:00
|
|
|
"github.com/status-im/status-go/services/personal"
|
2018-05-04 10:53:41 +00:00
|
|
|
"github.com/status-im/status-go/services/rpcfilters"
|
2019-05-07 07:05:38 +00:00
|
|
|
"github.com/status-im/status-go/services/subscriptions"
|
2018-11-06 06:26:12 +00:00
|
|
|
"github.com/status-im/status-go/services/typeddata"
|
2019-07-25 05:35:09 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet"
|
2018-05-03 07:35:58 +00:00
|
|
|
"github.com/status-im/status-go/signal"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
2017-10-10 15:30:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
//todo(jeka): should be removed
|
2019-03-29 10:20:24 +00:00
|
|
|
fcmServerKey = "AAAAxwa-r08:APA91bFtMIToDVKGAmVCm76iEXtA4dn9MPvLdYKIZqAlNpLJbd12EgdBI9DSDSXKdqvIAgLodepmRhGVaWvhxnXJzVpE6MoIRuKedDV3kfHSVBhWFqsyoLTwXY4xeufL9Sdzb581U-lx"
|
|
|
|
contractQueryTimeout = 1000 * time.Millisecond
|
2017-05-16 12:09:52 +00:00
|
|
|
)
|
|
|
|
|
2018-03-22 12:31:12 +00:00
|
|
|
var (
|
|
|
|
// ErrWhisperClearIdentitiesFailure clearing whisper identities has failed.
|
|
|
|
ErrWhisperClearIdentitiesFailure = errors.New("failed to clear whisper identities")
|
|
|
|
// ErrWhisperIdentityInjectionFailure injecting whisper identities has failed.
|
|
|
|
ErrWhisperIdentityInjectionFailure = errors.New("failed to inject identity into Whisper")
|
2018-08-16 11:37:53 +00:00
|
|
|
// ErrUnsupportedRPCMethod is for methods not supported by the RPC interface
|
|
|
|
ErrUnsupportedRPCMethod = errors.New("method is unsupported by RPC interface")
|
2018-12-20 08:31:17 +00:00
|
|
|
// ErrRPCClientUnavailable is returned if an RPC client can't be retrieved.
|
|
|
|
// This is a normal situation when a node is stopped.
|
|
|
|
ErrRPCClientUnavailable = errors.New("JSON-RPC client is unavailable")
|
2018-03-22 12:31:12 +00:00
|
|
|
)
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// StatusBackend implements Status.im service
|
|
|
|
type StatusBackend struct {
|
2019-08-20 15:38:40 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
// rootDataDir is the same for all networks.
|
|
|
|
rootDataDir string
|
2019-07-25 05:35:09 +00:00
|
|
|
appDB *sql.DB
|
2018-08-16 11:37:53 +00:00
|
|
|
statusNode *node.StatusNode
|
|
|
|
personalAPI *personal.PublicAPI
|
|
|
|
rpcFilters *rpcfilters.Service
|
2019-08-20 15:38:40 +00:00
|
|
|
multiaccountsDB *multiaccounts.Database
|
2018-08-16 11:37:53 +00:00
|
|
|
accountManager *account.Manager
|
|
|
|
transactor *transactions.Transactor
|
|
|
|
newNotification fcm.NotificationConstructor
|
|
|
|
connectionState connectionState
|
|
|
|
appState appState
|
|
|
|
log log.Logger
|
2019-05-07 07:05:38 +00:00
|
|
|
allowAllRPC bool // used only for tests, disables api method restrictions
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewStatusBackend create a new NewStatusBackend instance
|
|
|
|
func NewStatusBackend() *StatusBackend {
|
2018-11-28 11:05:18 +00:00
|
|
|
defer log.Info("Status backend initialized", "version", params.Version, "commit", params.GitCommit)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
statusNode := node.New()
|
2019-08-20 15:38:40 +00:00
|
|
|
accountManager := account.NewManager()
|
2018-08-16 11:37:53 +00:00
|
|
|
transactor := transactions.NewTransactor()
|
|
|
|
personalAPI := personal.NewAPI()
|
2017-10-18 20:56:39 +00:00
|
|
|
notificationManager := fcm.NewNotification(fcmServerKey)
|
2018-07-27 15:54:40 +00:00
|
|
|
rpcFilters := rpcfilters.New(statusNode)
|
2017-05-16 12:09:52 +00:00
|
|
|
return &StatusBackend{
|
2018-08-16 11:37:53 +00:00
|
|
|
statusNode: statusNode,
|
|
|
|
accountManager: accountManager,
|
|
|
|
transactor: transactor,
|
|
|
|
personalAPI: personalAPI,
|
|
|
|
rpcFilters: rpcFilters,
|
|
|
|
newNotification: notificationManager,
|
|
|
|
log: log.New("package", "status-go/api.StatusBackend"),
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
// StatusNode returns reference to node manager
|
|
|
|
func (b *StatusBackend) StatusNode() *node.StatusNode {
|
|
|
|
return b.statusNode
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AccountManager returns reference to account manager
|
2018-03-22 12:31:12 +00:00
|
|
|
func (b *StatusBackend) AccountManager() *account.Manager {
|
2018-02-14 16:32:36 +00:00
|
|
|
return b.accountManager
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
// Transactor returns reference to a status transactor
|
|
|
|
func (b *StatusBackend) Transactor() *transactions.Transactor {
|
|
|
|
return b.transactor
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// IsNodeRunning confirm that node is running
|
2018-02-14 16:32:36 +00:00
|
|
|
func (b *StatusBackend) IsNodeRunning() bool {
|
2018-04-05 09:45:26 +00:00
|
|
|
return b.statusNode.IsRunning()
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartNode start Status node, fails if node is already started
|
2018-02-14 16:32:36 +00:00
|
|
|
func (b *StatusBackend) StartNode(config *params.NodeConfig) error {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
2018-04-20 15:39:53 +00:00
|
|
|
|
|
|
|
if err := b.startNode(config); err != nil {
|
2018-05-03 07:35:58 +00:00
|
|
|
signal.SendNodeCrashed(err)
|
2018-04-20 15:39:53 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-02-09 13:37:56 +00:00
|
|
|
}
|
2017-05-25 13:14:52 +00:00
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func (b *StatusBackend) UpdateRootDataDir(datadir string) {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
b.rootDataDir = datadir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *StatusBackend) OpenAccounts() error {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
if b.multiaccountsDB != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
db, err := multiaccounts.InitializeDB(filepath.Join(b.rootDataDir, "accounts.sql"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.multiaccountsDB = db
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *StatusBackend) GetAccounts() ([]multiaccounts.Account, error) {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
if b.multiaccountsDB == nil {
|
|
|
|
return nil, errors.New("accoutns db wasn't initialized")
|
|
|
|
}
|
|
|
|
return b.multiaccountsDB.GetAccounts()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *StatusBackend) SaveAccount(account multiaccounts.Account) error {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
if b.multiaccountsDB == nil {
|
|
|
|
return errors.New("accoutns db wasn't initialized")
|
|
|
|
}
|
|
|
|
return b.multiaccountsDB.SaveAccount(account)
|
|
|
|
}
|
|
|
|
|
2019-07-25 05:35:09 +00:00
|
|
|
func (b *StatusBackend) ensureAppDBOpened(account multiaccounts.Account, password string) (err error) {
|
2019-08-20 15:38:40 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
2019-07-25 05:35:09 +00:00
|
|
|
if b.appDB != nil {
|
2019-08-20 15:38:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(b.rootDataDir) == 0 {
|
|
|
|
return errors.New("root datadir wasn't provided")
|
|
|
|
}
|
2019-07-25 05:35:09 +00:00
|
|
|
path := filepath.Join(b.rootDataDir, fmt.Sprintf("app-%x.sql", account.Address))
|
|
|
|
b.appDB, err = appdatabase.InitializeDB(path, password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 19:03:15 +00:00
|
|
|
func (b *StatusBackend) SaveAccountAndStartNodeWithKey(acc multiaccounts.Account, conf *params.NodeConfig, password string, keyHex string) error {
|
|
|
|
err := b.SaveAccount(acc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = b.ensureAppDBOpened(acc, password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = b.saveNodeConfig(conf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.StartNodeWithKey(acc, password, keyHex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartNodeWithKey instead of loading addresses from database this method derives address from key
|
|
|
|
// and uses it in application.
|
2019-09-24 06:32:32 +00:00
|
|
|
func (b *StatusBackend) startNodeWithKey(acc multiaccounts.Account, password string, keyHex string) error {
|
2019-09-02 19:03:15 +00:00
|
|
|
err := b.ensureAppDBOpened(acc, password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conf, err := b.loadNodeConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := logutils.OverrideRootLogWithConfig(conf, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
chatKey, err := ethcrypto.HexToECDSA(keyHex)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = b.StartNode(conf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.accountManager.SetChatAccount(chatKey)
|
|
|
|
chatAcc, err := b.accountManager.SelectedChatAccount()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.accountManager.SetAccountAddresses(chatAcc.Address)
|
2019-09-24 06:32:32 +00:00
|
|
|
err = b.injectAccountIntoServices()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = b.multiaccountsDB.UpdateAccountTimestamp(acc.Address, time.Now().Unix())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *StatusBackend) StartNodeWithKey(acc multiaccounts.Account, password string, keyHex string) error {
|
|
|
|
err := b.startNodeWithKey(acc, password, keyHex)
|
|
|
|
if err != nil {
|
|
|
|
// Stop node for clean up
|
|
|
|
_ = b.StopNode()
|
|
|
|
}
|
|
|
|
signal.SendLoggedIn(err)
|
|
|
|
return err
|
2019-09-02 19:03:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 13:00:14 +00:00
|
|
|
func (b *StatusBackend) startNodeWithAccount(acc multiaccounts.Account, password string) error {
|
2019-07-25 05:35:09 +00:00
|
|
|
err := b.ensureAppDBOpened(acc, password)
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conf, err := b.loadNodeConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := logutils.OverrideRootLogWithConfig(conf, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-25 05:35:09 +00:00
|
|
|
accountsDB := accounts.NewDB(b.appDB)
|
|
|
|
chatAddr, err := accountsDB.GetChatAddress()
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-25 05:35:09 +00:00
|
|
|
walletAddr, err := accountsDB.GetWalletAddress()
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-25 05:35:09 +00:00
|
|
|
watchAddrs, err := accountsDB.GetAddresses()
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
login := account.LoginParams{
|
|
|
|
Password: password,
|
|
|
|
ChatAddress: chatAddr,
|
|
|
|
WatchAddresses: watchAddrs,
|
|
|
|
MainAccount: walletAddr,
|
|
|
|
}
|
|
|
|
err = b.StartNode(conf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = b.SelectAccount(login)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = b.multiaccountsDB.UpdateAccountTimestamp(acc.Address, time.Now().Unix())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 13:00:14 +00:00
|
|
|
func (b *StatusBackend) StartNodeWithAccount(acc multiaccounts.Account, password string) error {
|
|
|
|
err := b.startNodeWithAccount(acc, password)
|
|
|
|
if err != nil {
|
|
|
|
// Stop node for clean up
|
|
|
|
_ = b.StopNode()
|
|
|
|
}
|
2019-09-24 06:32:32 +00:00
|
|
|
signal.SendLoggedIn(err)
|
2019-09-03 13:00:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
// StartNodeWithAccountAndConfig is used after account and config was generated.
|
|
|
|
// In current setup account name and config is generated on the client side. Once/if it will be generated on
|
|
|
|
// status-go side this flow can be simplified.
|
|
|
|
func (b *StatusBackend) StartNodeWithAccountAndConfig(account multiaccounts.Account, password string, conf *params.NodeConfig, subaccs []accounts.Account) error {
|
|
|
|
err := b.SaveAccount(account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-25 05:35:09 +00:00
|
|
|
err = b.ensureAppDBOpened(account, password)
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = b.saveNodeConfig(conf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-25 05:35:09 +00:00
|
|
|
err = accounts.NewDB(b.appDB).SaveAccounts(subaccs)
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.StartNodeWithAccount(account, password)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *StatusBackend) saveNodeConfig(config *params.NodeConfig) error {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
2019-07-25 05:35:09 +00:00
|
|
|
return accounts.NewDB(b.appDB).SaveConfig(accounts.NodeConfigTag, config)
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *StatusBackend) loadNodeConfig() (*params.NodeConfig, error) {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
conf := params.NodeConfig{}
|
2019-07-25 05:35:09 +00:00
|
|
|
err := accounts.NewDB(b.appDB).GetConfig(accounts.NodeConfigTag, &conf)
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-28 11:41:01 +00:00
|
|
|
// NodeConfig.Version should be taken from params.Version
|
|
|
|
// which is set at the compile time.
|
|
|
|
// What's cached is usually outdated so we overwrite it here.
|
|
|
|
conf.Version = params.Version
|
2019-08-20 15:38:40 +00:00
|
|
|
return &conf, nil
|
|
|
|
}
|
|
|
|
|
2018-05-04 10:53:41 +00:00
|
|
|
func (b *StatusBackend) rpcFiltersService() gethnode.ServiceConstructor {
|
|
|
|
return func(*gethnode.ServiceContext) (gethnode.Service, error) {
|
|
|
|
return rpcfilters.New(b.statusNode), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 07:05:38 +00:00
|
|
|
func (b *StatusBackend) subscriptionService() gethnode.ServiceConstructor {
|
|
|
|
return func(*gethnode.ServiceContext) (gethnode.Service, error) {
|
|
|
|
return subscriptions.New(b.statusNode), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 07:49:03 +00:00
|
|
|
func (b *StatusBackend) accountsService(accountsFeed *event.Feed) gethnode.ServiceConstructor {
|
2019-08-20 15:38:40 +00:00
|
|
|
return func(*gethnode.ServiceContext) (gethnode.Service, error) {
|
2019-08-28 07:49:03 +00:00
|
|
|
return accountssvc.NewService(accounts.NewDB(b.appDB), b.multiaccountsDB, b.accountManager, accountsFeed), nil
|
2019-07-25 05:35:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *StatusBackend) browsersService() gethnode.ServiceConstructor {
|
|
|
|
return func(*gethnode.ServiceContext) (gethnode.Service, error) {
|
|
|
|
return browsers.NewService(browsers.NewDB(b.appDB)), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *StatusBackend) permissionsService() gethnode.ServiceConstructor {
|
|
|
|
return func(*gethnode.ServiceContext) (gethnode.Service, error) {
|
|
|
|
return permissions.NewService(permissions.NewDB(b.appDB)), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 06:25:33 +00:00
|
|
|
func (b *StatusBackend) mailserversService() gethnode.ServiceConstructor {
|
|
|
|
return func(*gethnode.ServiceContext) (gethnode.Service, error) {
|
|
|
|
return mailservers.NewService(mailservers.NewDB(b.appDB)), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 07:49:03 +00:00
|
|
|
func (b *StatusBackend) walletService(network uint64, accountsFeed *event.Feed) gethnode.ServiceConstructor {
|
2019-07-25 05:35:09 +00:00
|
|
|
return func(*gethnode.ServiceContext) (gethnode.Service, error) {
|
2019-08-28 07:49:03 +00:00
|
|
|
return wallet.NewService(wallet.NewDB(b.appDB, network), accountsFeed), nil
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-14 16:32:36 +00:00
|
|
|
func (b *StatusBackend) startNode(config *params.NodeConfig) (err error) {
|
2018-02-09 13:37:56 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("node crashed on start: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2018-04-12 16:17:10 +00:00
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
// Start by validating configuration
|
|
|
|
if err := config.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-28 07:49:03 +00:00
|
|
|
accountsFeed := &event.Feed{}
|
2018-05-04 10:53:41 +00:00
|
|
|
services := []gethnode.ServiceConstructor{}
|
|
|
|
services = appendIf(config.UpstreamConfig.Enabled, services, b.rpcFiltersService())
|
2019-05-07 07:05:38 +00:00
|
|
|
services = append(services, b.subscriptionService())
|
2019-08-28 07:49:03 +00:00
|
|
|
services = appendIf(b.appDB != nil && b.multiaccountsDB != nil, services, b.accountsService(accountsFeed))
|
2019-07-25 05:35:09 +00:00
|
|
|
services = appendIf(config.BrowsersConfig.Enabled, services, b.browsersService())
|
|
|
|
services = appendIf(config.PermissionsConfig.Enabled, services, b.permissionsService())
|
2019-09-04 06:25:33 +00:00
|
|
|
services = appendIf(config.MailserversConfig.Enabled, services, b.mailserversService())
|
2019-08-28 07:49:03 +00:00
|
|
|
services = appendIf(config.WalletConfig.Enabled, services, b.walletService(config.NetworkID, accountsFeed))
|
2018-05-04 10:53:41 +00:00
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
manager := b.accountManager.GetManager()
|
|
|
|
if manager == nil {
|
|
|
|
return errors.New("ethereum accounts.Manager is nil")
|
|
|
|
}
|
2019-01-02 18:57:36 +00:00
|
|
|
if err = b.statusNode.StartWithOptions(config, node.StartOptions{
|
|
|
|
Services: services,
|
|
|
|
// The peers discovery protocols are started manually after
|
|
|
|
// `node.ready` signal is sent.
|
|
|
|
// It was discussed in https://github.com/status-im/status-go/pull/1333.
|
2019-08-20 15:38:40 +00:00
|
|
|
StartDiscovery: false,
|
|
|
|
AccountsManager: manager,
|
2019-01-02 18:57:36 +00:00
|
|
|
}); err != nil {
|
2018-04-20 15:39:53 +00:00
|
|
|
return
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2018-05-03 07:35:58 +00:00
|
|
|
signal.SendNodeStarted()
|
2018-04-09 08:18:22 +00:00
|
|
|
|
|
|
|
b.transactor.SetNetworkID(config.NetworkID)
|
2018-04-10 10:02:54 +00:00
|
|
|
b.transactor.SetRPC(b.statusNode.RPCClient(), rpc.DefaultCallTimeout)
|
2018-04-18 11:04:02 +00:00
|
|
|
b.personalAPI.SetRPC(b.statusNode.RPCPrivateClient(), rpc.DefaultCallTimeout)
|
2018-04-20 15:39:53 +00:00
|
|
|
|
|
|
|
if err = b.registerHandlers(); err != nil {
|
2018-03-20 18:35:28 +00:00
|
|
|
b.log.Error("Handler registration failed", "err", err)
|
2018-04-20 15:39:53 +00:00
|
|
|
return
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2018-04-20 15:39:53 +00:00
|
|
|
b.log.Info("Handlers registered")
|
|
|
|
|
2018-06-27 08:11:45 +00:00
|
|
|
if err = b.reSelectAccount(); err != nil {
|
2018-03-20 18:35:28 +00:00
|
|
|
b.log.Error("Reselect account failed", "err", err)
|
2018-04-20 15:39:53 +00:00
|
|
|
return
|
2017-10-20 09:06:22 +00:00
|
|
|
}
|
2018-03-20 18:35:28 +00:00
|
|
|
b.log.Info("Account reselected")
|
2018-04-20 15:39:53 +00:00
|
|
|
|
2018-05-03 10:36:56 +00:00
|
|
|
if st, err := b.statusNode.StatusService(); err == nil {
|
|
|
|
st.SetAccountManager(b.AccountManager())
|
|
|
|
}
|
|
|
|
|
2018-07-16 07:40:40 +00:00
|
|
|
if st, err := b.statusNode.PeerService(); err == nil {
|
|
|
|
st.SetDiscoverer(b.StatusNode())
|
|
|
|
}
|
|
|
|
|
2018-05-03 07:35:58 +00:00
|
|
|
signal.SendNodeReady()
|
2018-04-20 15:39:53 +00:00
|
|
|
|
2019-01-02 18:57:36 +00:00
|
|
|
if err := b.statusNode.StartDiscovery(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-09 13:37:56 +00:00
|
|
|
return nil
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StopNode stop Status node. Stopped node cannot be resumed.
|
2018-02-14 16:32:36 +00:00
|
|
|
func (b *StatusBackend) StopNode() error {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
return b.stopNode()
|
2018-02-09 13:37:56 +00:00
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2018-02-14 16:32:36 +00:00
|
|
|
func (b *StatusBackend) stopNode() error {
|
|
|
|
if !b.IsNodeRunning() {
|
2018-02-09 13:37:56 +00:00
|
|
|
return node.ErrNoRunningNode
|
2017-05-25 13:14:52 +00:00
|
|
|
}
|
2018-05-03 07:35:58 +00:00
|
|
|
defer signal.SendNodeStopped()
|
2018-04-05 09:45:26 +00:00
|
|
|
return b.statusNode.Stop()
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
// RestartNode restart running Status node, fails if node is not running
|
2018-02-14 16:32:36 +00:00
|
|
|
func (b *StatusBackend) RestartNode() error {
|
2018-04-20 15:39:53 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2018-02-14 16:32:36 +00:00
|
|
|
if !b.IsNodeRunning() {
|
2018-02-09 13:37:56 +00:00
|
|
|
return node.ErrNoRunningNode
|
2017-05-25 13:14:52 +00:00
|
|
|
}
|
2018-04-20 15:39:53 +00:00
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
newcfg := *(b.statusNode.Config())
|
2018-02-14 16:32:36 +00:00
|
|
|
if err := b.stopNode(); err != nil {
|
2018-02-09 13:37:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-02-14 16:32:36 +00:00
|
|
|
return b.startNode(&newcfg)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
// ResetChainData remove chain data from data directory.
|
|
|
|
// Node is stopped, and new node is started, with clean data directory.
|
2018-02-14 16:32:36 +00:00
|
|
|
func (b *StatusBackend) ResetChainData() error {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
2018-04-16 12:36:09 +00:00
|
|
|
newcfg := *(b.statusNode.Config())
|
2018-02-14 16:32:36 +00:00
|
|
|
if err := b.stopNode(); err != nil {
|
2018-02-09 13:37:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-02-14 16:32:36 +00:00
|
|
|
// config is cleaned when node is stopped
|
2018-04-05 09:45:26 +00:00
|
|
|
if err := b.statusNode.ResetChainData(&newcfg); err != nil {
|
2018-02-09 13:37:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-05-03 07:35:58 +00:00
|
|
|
signal.SendChainDataRemoved()
|
2018-02-14 16:32:36 +00:00
|
|
|
return b.startNode(&newcfg)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 08:01:37 +00:00
|
|
|
// CallRPC executes public RPC requests on node's in-proc RPC server.
|
2018-12-20 08:31:17 +00:00
|
|
|
func (b *StatusBackend) CallRPC(inputJSON string) (string, error) {
|
2018-04-05 09:45:26 +00:00
|
|
|
client := b.statusNode.RPCClient()
|
2018-12-20 08:31:17 +00:00
|
|
|
if client == nil {
|
|
|
|
return "", ErrRPCClientUnavailable
|
|
|
|
}
|
|
|
|
return client.CallRaw(inputJSON), nil
|
2017-05-28 13:57:30 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 17:49:01 +00:00
|
|
|
// GetNodesFromContract returns a list of nodes from the contract
|
2019-03-29 10:20:24 +00:00
|
|
|
func (b *StatusBackend) GetNodesFromContract(rpcEndpoint string, contractAddress string) ([]string, error) {
|
|
|
|
var response []string
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), contractQueryTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ethclient, err := ethclient.DialContext(ctx, rpcEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
|
|
|
contract, err := registry.NewNodes(gethcommon.HexToAddress(contractAddress), ethclient)
|
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
2019-03-29 17:49:01 +00:00
|
|
|
nodeCount, err := contract.NodeCount(nil)
|
2019-03-29 10:20:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
2019-03-29 17:49:01 +00:00
|
|
|
one := big.NewInt(1)
|
|
|
|
for i := big.NewInt(0); i.Cmp(nodeCount) < 0; i.Add(i, one) {
|
|
|
|
node, err := contract.Nodes(nil, i)
|
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
response = append(response, node)
|
|
|
|
}
|
|
|
|
|
2019-03-29 10:20:24 +00:00
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2018-04-16 08:01:37 +00:00
|
|
|
// CallPrivateRPC executes public and private RPC requests on node's in-proc RPC server.
|
2018-12-20 08:31:17 +00:00
|
|
|
func (b *StatusBackend) CallPrivateRPC(inputJSON string) (string, error) {
|
2018-04-16 08:01:37 +00:00
|
|
|
client := b.statusNode.RPCPrivateClient()
|
2018-12-20 08:31:17 +00:00
|
|
|
if client == nil {
|
|
|
|
return "", ErrRPCClientUnavailable
|
|
|
|
}
|
|
|
|
return client.CallRaw(inputJSON), nil
|
2018-04-16 08:01:37 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// SendTransaction creates a new transaction and waits until it's complete.
|
2018-08-16 11:37:53 +00:00
|
|
|
func (b *StatusBackend) SendTransaction(sendArgs transactions.SendTxArgs, password string) (hash gethcommon.Hash, err error) {
|
2019-07-26 14:45:10 +00:00
|
|
|
verifiedAccount, err := b.getVerifiedWalletAccount(sendArgs.From.String(), password)
|
2018-08-16 11:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err = b.transactor.SendTransaction(sendArgs, verifiedAccount)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go b.rpcFilters.TriggerTransactionSentToUpstreamEvent(hash)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-15 11:31:20 +00:00
|
|
|
func (b *StatusBackend) SendTransactionWithSignature(sendArgs transactions.SendTxArgs, sig []byte) (hash gethcommon.Hash, err error) {
|
|
|
|
hash, err = b.transactor.SendTransactionWithSignature(sendArgs, sig)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go b.rpcFilters.TriggerTransactionSentToUpstreamEvent(hash)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-21 09:53:39 +00:00
|
|
|
// HashTransaction validate the transaction and returns new sendArgs and the transaction hash.
|
|
|
|
func (b *StatusBackend) HashTransaction(sendArgs transactions.SendTxArgs) (transactions.SendTxArgs, gethcommon.Hash, error) {
|
|
|
|
return b.transactor.HashTransaction(sendArgs)
|
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
// SignMessage checks the pwd vs the selected account and passes on the signParams
|
|
|
|
// to personalAPI for message signature
|
|
|
|
func (b *StatusBackend) SignMessage(rpcParams personal.SignParams) (hexutil.Bytes, error) {
|
2019-07-26 14:45:10 +00:00
|
|
|
verifiedAccount, err := b.getVerifiedWalletAccount(rpcParams.Address, rpcParams.Password)
|
2018-08-16 11:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return hexutil.Bytes{}, err
|
2018-07-27 15:54:40 +00:00
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
return b.personalAPI.Sign(rpcParams, verifiedAccount)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recover calls the personalAPI to return address associated with the private
|
|
|
|
// key that was used to calculate the signature in the message
|
|
|
|
func (b *StatusBackend) Recover(rpcParams personal.RecoverParams) (gethcommon.Address, error) {
|
|
|
|
return b.personalAPI.Recover(rpcParams)
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 06:26:12 +00:00
|
|
|
// SignTypedData accepts data and password. Gets verified account and signs typed data.
|
2019-07-26 14:45:10 +00:00
|
|
|
func (b *StatusBackend) SignTypedData(typed typeddata.TypedData, address string, password string) (hexutil.Bytes, error) {
|
|
|
|
account, err := b.getVerifiedWalletAccount(address, password)
|
2018-11-06 06:26:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return hexutil.Bytes{}, err
|
|
|
|
}
|
|
|
|
chain := new(big.Int).SetUint64(b.StatusNode().Config().NetworkID)
|
|
|
|
sig, err := typeddata.Sign(typed, account.AccountKey.PrivateKey, chain)
|
|
|
|
if err != nil {
|
|
|
|
return hexutil.Bytes{}, err
|
|
|
|
}
|
|
|
|
return hexutil.Bytes(sig), err
|
|
|
|
}
|
|
|
|
|
2019-03-28 14:56:21 +00:00
|
|
|
// HashTypedData generates the hash of TypedData.
|
|
|
|
func (b *StatusBackend) HashTypedData(typed typeddata.TypedData) (common.Hash, error) {
|
|
|
|
chain := new(big.Int).SetUint64(b.StatusNode().Config().NetworkID)
|
|
|
|
hash, err := typeddata.ValidateAndHash(typed, chain)
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:45:10 +00:00
|
|
|
func (b *StatusBackend) getVerifiedWalletAccount(address, password string) (*account.SelectedExtKey, error) {
|
|
|
|
config := b.StatusNode().Config()
|
|
|
|
|
2019-08-29 08:06:22 +00:00
|
|
|
db := accounts.NewDB(b.appDB)
|
|
|
|
exists, err := db.AddressExists(common.HexToAddress(address))
|
2018-03-26 15:53:10 +00:00
|
|
|
if err != nil {
|
2019-08-29 08:06:22 +00:00
|
|
|
b.log.Error("failed to query db for a given address", "address", address, "error", err)
|
2018-03-26 15:53:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-26 14:45:10 +00:00
|
|
|
|
2019-08-29 08:06:22 +00:00
|
|
|
if !exists {
|
2019-07-26 14:45:10 +00:00
|
|
|
b.log.Error("failed to get a selected account", "err", transactions.ErrInvalidTxSender)
|
2019-08-29 08:06:22 +00:00
|
|
|
return nil, transactions.ErrAccountDoesntExist
|
2019-07-26 14:45:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
key, err := b.accountManager.VerifyAccountPassword(config.KeyStoreDir, address, password)
|
2018-03-26 15:53:10 +00:00
|
|
|
if err != nil {
|
2019-07-26 14:45:10 +00:00
|
|
|
b.log.Error("failed to verify account", "account", address, "error", err)
|
2018-03-26 15:53:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-26 14:45:10 +00:00
|
|
|
|
|
|
|
return &account.SelectedExtKey{
|
|
|
|
Address: key.Address,
|
|
|
|
AccountKey: key,
|
|
|
|
}, nil
|
2018-03-26 15:53:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// registerHandlers attaches Status callback handlers to running node
|
2018-02-14 16:32:36 +00:00
|
|
|
func (b *StatusBackend) registerHandlers() error {
|
2018-08-07 14:21:43 +00:00
|
|
|
var clients []*rpc.Client
|
|
|
|
|
|
|
|
if c := b.StatusNode().RPCClient(); c != nil {
|
|
|
|
clients = append(clients, c)
|
|
|
|
} else {
|
2018-04-16 12:36:09 +00:00
|
|
|
return errors.New("RPC client unavailable")
|
2017-10-26 13:25:38 +00:00
|
|
|
}
|
2018-04-09 08:18:22 +00:00
|
|
|
|
2018-08-07 14:21:43 +00:00
|
|
|
if c := b.StatusNode().RPCPrivateClient(); c != nil {
|
|
|
|
clients = append(clients, c)
|
|
|
|
} else {
|
|
|
|
return errors.New("RPC private client unavailable")
|
|
|
|
}
|
2018-04-09 08:18:22 +00:00
|
|
|
|
2018-08-07 14:21:43 +00:00
|
|
|
for _, client := range clients {
|
|
|
|
client.RegisterHandler(
|
|
|
|
params.AccountsMethodName,
|
|
|
|
func(context.Context, ...interface{}) (interface{}, error) {
|
|
|
|
return b.AccountManager().Accounts()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2019-05-07 07:05:38 +00:00
|
|
|
if b.allowAllRPC {
|
|
|
|
// this should only happen in unit-tests, this variable is not available outside this package
|
|
|
|
continue
|
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
client.RegisterHandler(params.SendTransactionMethodName, unsupportedMethodHandler)
|
|
|
|
client.RegisterHandler(params.PersonalSignMethodName, unsupportedMethodHandler)
|
|
|
|
client.RegisterHandler(params.PersonalRecoverMethodName, unsupportedMethodHandler)
|
2018-08-07 14:21:43 +00:00
|
|
|
}
|
2018-04-10 10:02:54 +00:00
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-02-09 20:28:16 +00:00
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
func unsupportedMethodHandler(ctx context.Context, rpcParams ...interface{}) (interface{}, error) {
|
|
|
|
return nil, ErrUnsupportedRPCMethod
|
|
|
|
}
|
|
|
|
|
2018-02-09 20:28:16 +00:00
|
|
|
// ConnectionChange handles network state changes logic.
|
2018-04-23 13:34:49 +00:00
|
|
|
func (b *StatusBackend) ConnectionChange(typ string, expensive bool) {
|
2018-04-20 15:39:53 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2018-06-27 08:11:45 +00:00
|
|
|
state := connectionState{
|
|
|
|
Type: newConnectionType(typ),
|
2018-04-23 13:34:49 +00:00
|
|
|
Expensive: expensive,
|
|
|
|
}
|
2018-06-27 08:11:45 +00:00
|
|
|
if typ == none {
|
2018-04-23 13:34:49 +00:00
|
|
|
state.Offline = true
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:35:28 +00:00
|
|
|
b.log.Info("Network state change", "old", b.connectionState, "new", state)
|
2018-04-23 13:34:49 +00:00
|
|
|
|
2018-02-16 13:58:13 +00:00
|
|
|
b.connectionState = state
|
2018-02-09 20:28:16 +00:00
|
|
|
|
|
|
|
// logic of handling state changes here
|
|
|
|
// restart node? force peers reconnect? etc
|
|
|
|
}
|
2018-03-14 15:46:21 +00:00
|
|
|
|
|
|
|
// AppStateChange handles app state changes (background/foreground).
|
2018-04-23 13:34:49 +00:00
|
|
|
// state values: see https://facebook.github.io/react-native/docs/appstate.html
|
|
|
|
func (b *StatusBackend) AppStateChange(state string) {
|
2018-06-27 08:11:45 +00:00
|
|
|
s, err := parseAppState(state)
|
2018-04-23 13:34:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("AppStateChange failed, ignoring", "error", err)
|
|
|
|
return // and do nothing
|
|
|
|
}
|
|
|
|
|
2018-06-27 08:11:45 +00:00
|
|
|
b.log.Info("App State changed", "new-state", s)
|
|
|
|
b.appState = s
|
2018-03-14 15:46:21 +00:00
|
|
|
|
|
|
|
// TODO: put node in low-power mode if the app is in background (or inactive)
|
|
|
|
// and normal mode if the app is in foreground.
|
|
|
|
}
|
2018-03-22 12:31:12 +00:00
|
|
|
|
|
|
|
// Logout clears whisper identities.
|
|
|
|
func (b *StatusBackend) Logout() error {
|
2018-05-26 07:37:13 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
err := b.cleanupServices()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-25 05:35:09 +00:00
|
|
|
err = b.closeAppDB()
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.AccountManager().Logout()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanupServices stops parts of services that doesn't managed by a node and removes injected data from services.
|
|
|
|
func (b *StatusBackend) cleanupServices() error {
|
2018-04-05 09:45:26 +00:00
|
|
|
whisperService, err := b.statusNode.WhisperService()
|
2018-04-20 15:39:53 +00:00
|
|
|
switch err {
|
|
|
|
case node.ErrServiceUnknown: // Whisper was never registered
|
|
|
|
case nil:
|
|
|
|
if err := whisperService.DeleteKeyPairs(); err != nil {
|
|
|
|
return fmt.Errorf("%s: %v", ErrWhisperClearIdentitiesFailure, err)
|
|
|
|
}
|
|
|
|
default:
|
2018-03-22 12:31:12 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-06-14 10:16:30 +00:00
|
|
|
if b.statusNode.Config().WalletConfig.Enabled {
|
|
|
|
wallet, err := b.statusNode.WalletService()
|
|
|
|
switch err {
|
|
|
|
case node.ErrServiceUnknown:
|
|
|
|
case nil:
|
|
|
|
err = wallet.StopReactor()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-08-20 15:38:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-07-17 05:28:37 +00:00
|
|
|
|
2019-07-25 05:35:09 +00:00
|
|
|
func (b *StatusBackend) closeAppDB() error {
|
|
|
|
if b.appDB != nil {
|
|
|
|
err := b.appDB.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.appDB = nil
|
|
|
|
return nil
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
2018-04-20 15:39:53 +00:00
|
|
|
return nil
|
2018-03-22 12:31:12 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 08:11:45 +00:00
|
|
|
// reSelectAccount selects previously selected account, often, after node restart.
|
|
|
|
func (b *StatusBackend) reSelectAccount() error {
|
2019-06-26 22:28:16 +00:00
|
|
|
b.AccountManager().RemoveOnboarding()
|
|
|
|
|
2019-01-09 08:47:06 +00:00
|
|
|
selectedChatAccount, err := b.AccountManager().SelectedChatAccount()
|
|
|
|
if selectedChatAccount == nil || err == account.ErrNoAccountSelected {
|
2018-03-22 12:31:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-01-09 08:47:06 +00:00
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
whisperService, err := b.statusNode.WhisperService()
|
2018-04-20 15:39:53 +00:00
|
|
|
switch err {
|
|
|
|
case node.ErrServiceUnknown: // Whisper was never registered
|
|
|
|
case nil:
|
2019-01-09 08:47:06 +00:00
|
|
|
if err := whisperService.SelectKeyPair(selectedChatAccount.AccountKey.PrivateKey); err != nil {
|
2018-04-20 15:39:53 +00:00
|
|
|
return ErrWhisperIdentityInjectionFailure
|
|
|
|
}
|
|
|
|
default:
|
2018-03-22 12:31:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-18 09:01:14 +00:00
|
|
|
// SelectAccount selects current wallet and chat accounts, by verifying that each address has corresponding account which can be decrypted
|
|
|
|
// using provided password. Once verification is done, the decrypted chat key is injected into Whisper (as a single identity,
|
2018-03-22 12:31:12 +00:00
|
|
|
// all previous identities are removed).
|
2019-07-26 14:45:10 +00:00
|
|
|
func (b *StatusBackend) SelectAccount(loginParams account.LoginParams) error {
|
2018-05-26 07:37:13 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2019-06-26 22:28:16 +00:00
|
|
|
b.AccountManager().RemoveOnboarding()
|
|
|
|
|
2019-07-26 14:45:10 +00:00
|
|
|
err := b.accountManager.SelectAccount(loginParams)
|
2018-03-22 12:31:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-18 09:01:14 +00:00
|
|
|
|
2019-09-02 19:03:15 +00:00
|
|
|
return b.injectAccountIntoServices()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *StatusBackend) injectAccountIntoServices() error {
|
2019-01-09 08:47:06 +00:00
|
|
|
chatAccount, err := b.accountManager.SelectedChatAccount()
|
2018-03-22 12:31:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
whisperService, err := b.statusNode.WhisperService()
|
2018-04-20 15:39:53 +00:00
|
|
|
switch err {
|
|
|
|
case node.ErrServiceUnknown: // Whisper was never registered
|
|
|
|
case nil:
|
2019-01-09 08:47:06 +00:00
|
|
|
if err := whisperService.SelectKeyPair(chatAccount.AccountKey.PrivateKey); err != nil {
|
2018-04-20 15:39:53 +00:00
|
|
|
return ErrWhisperIdentityInjectionFailure
|
|
|
|
}
|
|
|
|
default:
|
2018-03-22 12:31:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-24 18:07:34 +00:00
|
|
|
if whisperService != nil {
|
|
|
|
st, err := b.statusNode.ShhExtService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-27 12:04:15 +00:00
|
|
|
if err := st.InitProtocol(b.appDB); err != nil {
|
2018-09-27 13:07:32 +00:00
|
|
|
return err
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-02 19:03:15 +00:00
|
|
|
return b.startWallet()
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
2018-09-24 18:07:34 +00:00
|
|
|
|
2019-09-02 19:03:15 +00:00
|
|
|
func (b *StatusBackend) startWallet() error {
|
2019-06-14 10:16:30 +00:00
|
|
|
if !b.statusNode.Config().WalletConfig.Enabled {
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-26 14:45:10 +00:00
|
|
|
|
2019-06-14 10:16:30 +00:00
|
|
|
wallet, err := b.statusNode.WalletService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-26 14:45:10 +00:00
|
|
|
|
|
|
|
watchAddresses := b.accountManager.WatchAddresses()
|
|
|
|
mainAccountAddress, err := b.accountManager.MainAccountAddress()
|
2019-06-14 10:16:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-26 14:45:10 +00:00
|
|
|
|
|
|
|
allAddresses := make([]common.Address, len(watchAddresses)+1)
|
|
|
|
allAddresses[0] = mainAccountAddress
|
|
|
|
copy(allAddresses[1:], watchAddresses)
|
2019-07-25 05:35:09 +00:00
|
|
|
return wallet.StartReactor(
|
2019-06-14 10:16:30 +00:00
|
|
|
b.statusNode.RPCClient().Ethclient(),
|
2019-07-26 14:45:10 +00:00
|
|
|
allAddresses,
|
2019-06-14 10:16:30 +00:00
|
|
|
new(big.Int).SetUint64(b.statusNode.Config().NetworkID))
|
2018-03-22 12:31:12 +00:00
|
|
|
}
|
2018-04-23 13:34:49 +00:00
|
|
|
|
2019-01-18 13:03:32 +00:00
|
|
|
// SendDataNotification sends data push notifications to users.
|
|
|
|
// dataPayloadJSON is a JSON string that looks like this:
|
|
|
|
// {
|
2019-06-14 10:16:30 +00:00
|
|
|
// "data": {
|
|
|
|
// "msg-v2": {
|
|
|
|
// "from": "0x2cea3bd5", // hash of sender (first 10 characters/4 bytes of sha3 hash)
|
|
|
|
// "to": "0xb1f89744", // hash of recipient (first 10 characters/4 bytes of sha3 hash)
|
|
|
|
// "id": "0x872653ad", // message ID hash (first 10 characters/4 bytes of sha3 hash)
|
|
|
|
// }
|
|
|
|
// }
|
2019-01-18 13:03:32 +00:00
|
|
|
// }
|
|
|
|
func (b *StatusBackend) SendDataNotification(dataPayloadJSON string, tokens ...string) error {
|
|
|
|
log.Debug("sending data push notification")
|
|
|
|
|
|
|
|
err := b.newNotification().Send(dataPayloadJSON, tokens...)
|
2018-04-23 13:34:49 +00:00
|
|
|
if err != nil {
|
2019-01-18 13:03:32 +00:00
|
|
|
b.log.Error("SendDataNotification failed", "dataPayloadJSON", dataPayloadJSON, "error", err)
|
2018-04-23 13:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2018-05-04 10:53:41 +00:00
|
|
|
|
2019-01-24 15:44:46 +00:00
|
|
|
// InjectChatAccount selects the current chat account using chatKeyHex and injects the key into whisper.
|
|
|
|
func (b *StatusBackend) InjectChatAccount(chatKeyHex, encryptionKeyHex string) error {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
|
|
|
b.accountManager.Logout()
|
|
|
|
|
|
|
|
chatKey, err := ethcrypto.HexToECDSA(chatKeyHex)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.accountManager.SetChatAccount(chatKey)
|
|
|
|
chatAccount, err := b.accountManager.SelectedChatAccount()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
whisperService, err := b.statusNode.WhisperService()
|
|
|
|
switch err {
|
|
|
|
case node.ErrServiceUnknown: // Whisper was never registered
|
|
|
|
case nil:
|
|
|
|
if err := whisperService.SelectKeyPair(chatAccount.AccountKey.PrivateKey); err != nil {
|
|
|
|
return ErrWhisperIdentityInjectionFailure
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if whisperService != nil {
|
|
|
|
st, err := b.statusNode.ShhExtService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-27 12:04:15 +00:00
|
|
|
if err := st.InitProtocol(b.appDB); err != nil {
|
2019-01-24 15:44:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-04 10:53:41 +00:00
|
|
|
func appendIf(condition bool, services []gethnode.ServiceConstructor, service gethnode.ServiceConstructor) []gethnode.ServiceConstructor {
|
|
|
|
if !condition {
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
return append(services, service)
|
|
|
|
}
|
2018-09-24 18:07:34 +00:00
|
|
|
|
2019-05-23 08:47:20 +00:00
|
|
|
// ExtractGroupMembershipSignatures extract signatures from tuples of content/signature
|
|
|
|
func (b *StatusBackend) ExtractGroupMembershipSignatures(signaturePairs [][2]string) ([]string, error) {
|
|
|
|
return crypto.ExtractSignatures(signaturePairs)
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 08:47:20 +00:00
|
|
|
// SignGroupMembership signs a piece of data containing membership information
|
|
|
|
func (b *StatusBackend) SignGroupMembership(content string) (string, error) {
|
|
|
|
selectedChatAccount, err := b.AccountManager().SelectedChatAccount()
|
2019-02-12 11:07:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-05-23 08:47:20 +00:00
|
|
|
return crypto.Sign(content, selectedChatAccount.AccountKey.PrivateKey)
|
2019-02-12 11:07:13 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 13:15:13 +00:00
|
|
|
// EnableInstallation enables an installation for multi-device sync.
|
|
|
|
func (b *StatusBackend) EnableInstallation(installationID string) error {
|
|
|
|
st, err := b.statusNode.ShhExtService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := st.EnableInstallation(installationID); err != nil {
|
|
|
|
b.log.Error("error enabling installation", "err", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableInstallation disables an installation for multi-device sync.
|
|
|
|
func (b *StatusBackend) DisableInstallation(installationID string) error {
|
|
|
|
st, err := b.statusNode.ShhExtService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := st.DisableInstallation(installationID); err != nil {
|
|
|
|
b.log.Error("error disabling installation", "err", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 10:22:30 +00:00
|
|
|
// UpdateMailservers on ShhExtService.
|
|
|
|
func (b *StatusBackend) UpdateMailservers(enodes []string) error {
|
|
|
|
st, err := b.statusNode.ShhExtService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nodes := make([]*enode.Node, len(enodes))
|
|
|
|
for i, rawurl := range enodes {
|
|
|
|
node, err := enode.ParseV4(rawurl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nodes[i] = node
|
|
|
|
}
|
2018-12-12 09:39:00 +00:00
|
|
|
return st.UpdateMailservers(nodes)
|
2018-11-21 10:22:30 +00:00
|
|
|
}
|
2019-04-18 13:52:08 +00:00
|
|
|
|
|
|
|
// SignHash exposes vanilla ECDSA signing for signing a message for Swarm
|
|
|
|
func (b *StatusBackend) SignHash(hexEncodedHash string) (string, error) {
|
|
|
|
hash, err := hexutil.Decode(hexEncodedHash)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("SignHash: could not unmarshal the input: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chatAccount, err := b.AccountManager().SelectedChatAccount()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("SignHash: could not select account: %v", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
signature, err := ethcrypto.Sign(hash, chatAccount.AccountKey.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("SignHash: could not sign the hash: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hexEncodedSignature := hexutil.Encode(signature)
|
|
|
|
return hexEncodedSignature, nil
|
|
|
|
}
|