2017-05-16 12:09:52 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2017-09-04 12:56:58 +00:00
|
|
|
"context"
|
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"
|
2017-05-25 13:14:52 +00:00
|
|
|
"sync"
|
|
|
|
|
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"
|
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-03-20 18:35:28 +00:00
|
|
|
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
|
|
|
"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"
|
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"
|
2018-09-24 18:07:34 +00:00
|
|
|
"github.com/status-im/status-go/services/shhext/chat"
|
2018-09-27 13:07:32 +00:00
|
|
|
"github.com/status-im/status-go/services/shhext/chat/crypto"
|
2018-11-06 06:26:12 +00:00
|
|
|
"github.com/status-im/status-go/services/typeddata"
|
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
|
|
|
|
fcmServerKey = "AAAAxwa-r08:APA91bFtMIToDVKGAmVCm76iEXtA4dn9MPvLdYKIZqAlNpLJbd12EgdBI9DSDSXKdqvIAgLodepmRhGVaWvhxnXJzVpE6MoIRuKedDV3kfHSVBhWFqsyoLTwXY4xeufL9Sdzb581U-lx"
|
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-03-22 12:31:12 +00:00
|
|
|
)
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// StatusBackend implements Status.im service
|
|
|
|
type StatusBackend struct {
|
2018-08-16 11:37:53 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
statusNode *node.StatusNode
|
|
|
|
personalAPI *personal.PublicAPI
|
|
|
|
rpcFilters *rpcfilters.Service
|
|
|
|
accountManager *account.Manager
|
|
|
|
transactor *transactions.Transactor
|
|
|
|
newNotification fcm.NotificationConstructor
|
|
|
|
connectionState connectionState
|
|
|
|
appState appState
|
|
|
|
log log.Logger
|
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()
|
|
|
|
accountManager := account.NewManager(statusNode)
|
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-08-15 10:27:12 +00:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-05-04 10:53:41 +00:00
|
|
|
services := []gethnode.ServiceConstructor{}
|
|
|
|
services = appendIf(config.UpstreamConfig.Enabled, services, b.rpcFiltersService())
|
|
|
|
|
|
|
|
if err = b.statusNode.Start(config, services...); 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
|
|
|
|
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-02-14 16:32:36 +00:00
|
|
|
func (b *StatusBackend) CallRPC(inputJSON string) string {
|
2018-04-05 09:45:26 +00:00
|
|
|
client := b.statusNode.RPCClient()
|
2017-09-14 20:14:31 +00:00
|
|
|
return client.CallRaw(inputJSON)
|
2017-05-28 13:57:30 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 08:01:37 +00:00
|
|
|
// CallPrivateRPC executes public and private RPC requests on node's in-proc RPC server.
|
|
|
|
func (b *StatusBackend) CallPrivateRPC(inputJSON string) string {
|
|
|
|
client := b.statusNode.RPCPrivateClient()
|
|
|
|
return client.CallRaw(inputJSON)
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
verifiedAccount, err := b.getVerifiedAccount(password)
|
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err = b.transactor.SendTransaction(sendArgs, verifiedAccount)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go b.rpcFilters.TriggerTransactionSentToUpstreamEvent(hash)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
verifiedAccount, err := b.getVerifiedAccount(rpcParams.Password)
|
|
|
|
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.
|
|
|
|
func (b *StatusBackend) SignTypedData(typed typeddata.TypedData, password string) (hexutil.Bytes, error) {
|
|
|
|
account, err := b.getVerifiedAccount(password)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-03-26 15:53:10 +00:00
|
|
|
func (b *StatusBackend) getVerifiedAccount(password string) (*account.SelectedExtKey, error) {
|
|
|
|
selectedAccount, err := b.accountManager.SelectedAccount()
|
|
|
|
if err != nil {
|
|
|
|
b.log.Error("failed to get a selected account", "err", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-16 12:36:09 +00:00
|
|
|
config := b.StatusNode().Config()
|
2018-03-26 15:53:10 +00:00
|
|
|
_, err = b.accountManager.VerifyAccountPassword(config.KeyStoreDir, selectedAccount.Address.String(), password)
|
|
|
|
if err != nil {
|
|
|
|
b.log.Error("failed to verify account", "account", selectedAccount.Address.String(), "error", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return selectedAccount, nil
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
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()
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-04-20 15:39:53 +00:00
|
|
|
b.AccountManager().Logout()
|
|
|
|
|
|
|
|
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 {
|
2018-03-22 12:31:12 +00:00
|
|
|
selectedAccount, err := b.AccountManager().SelectedAccount()
|
|
|
|
if selectedAccount == nil || err == account.ErrNoAccountSelected {
|
|
|
|
return nil
|
|
|
|
}
|
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.SelectKeyPair(selectedAccount.AccountKey.PrivateKey); err != nil {
|
|
|
|
return ErrWhisperIdentityInjectionFailure
|
|
|
|
}
|
|
|
|
default:
|
2018-03-22 12:31:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SelectAccount selects current account, by verifying that address has corresponding account which can be decrypted
|
|
|
|
// using provided password. Once verification is done, decrypted key is injected into Whisper (as a single identity,
|
|
|
|
// all previous identities are removed).
|
|
|
|
func (b *StatusBackend) SelectAccount(address, password string) error {
|
2018-05-26 07:37:13 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2018-03-22 12:31:12 +00:00
|
|
|
err := b.accountManager.SelectAccount(address, password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
acc, err := b.accountManager.SelectedAccount()
|
|
|
|
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:
|
|
|
|
if err := whisperService.SelectKeyPair(acc.AccountKey.PrivateKey); err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := st.InitProtocol(address, password); err != nil {
|
2018-09-27 13:07:32 +00:00
|
|
|
return err
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 12:31:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-04-23 13:34:49 +00:00
|
|
|
|
|
|
|
// NotifyUsers sends push notifications to users.
|
2018-11-20 18:02:54 +00:00
|
|
|
func (b *StatusBackend) NotifyUsers(dataPayloadJSON string, tokens ...string) error {
|
|
|
|
log.Debug("sending push notification")
|
|
|
|
|
|
|
|
err := b.newNotification().Send(dataPayloadJSON, tokens...)
|
2018-04-23 13:34:49 +00:00
|
|
|
if err != nil {
|
2018-11-20 18:02:54 +00:00
|
|
|
b.log.Error("NotifyUsers failed", "dataPayloadJSON", dataPayloadJSON, "error", err)
|
2018-04-23 13:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// CreateContactCode create or return the latest contact code
|
|
|
|
func (b *StatusBackend) CreateContactCode() (string, error) {
|
|
|
|
selectedAccount, err := b.AccountManager().SelectedAccount()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
st, err := b.statusNode.ShhExtService()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
bundle, err := st.GetBundle(selectedAccount.AccountKey.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bundle.ToBase64()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessContactCode process and adds the someone else's bundle
|
|
|
|
func (b *StatusBackend) ProcessContactCode(contactCode string) error {
|
|
|
|
selectedAccount, err := b.AccountManager().SelectedAccount()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
st, err := b.statusNode.ShhExtService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bundle, err := chat.FromBase64(contactCode)
|
|
|
|
if err != nil {
|
|
|
|
b.log.Error("error decoding base64", "err", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-16 10:31:05 +00:00
|
|
|
if _, err := st.ProcessPublicBundle(selectedAccount.AccountKey.PrivateKey, bundle); err != nil {
|
2018-09-24 18:07:34 +00:00
|
|
|
b.log.Error("error adding bundle", "err", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExtractIdentityFromContactCode extract the identity of the user generating the contact code
|
|
|
|
func (b *StatusBackend) ExtractIdentityFromContactCode(contactCode string) (string, error) {
|
|
|
|
bundle, err := chat.FromBase64(contactCode)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return chat.ExtractIdentity(bundle)
|
|
|
|
}
|
2018-09-27 13:07:32 +00:00
|
|
|
|
2018-10-04 16:53:48 +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-27 13:07:32 +00:00
|
|
|
// SignGroupMembership signs a piece of data containing membership information
|
|
|
|
func (b *StatusBackend) SignGroupMembership(content string) (string, error) {
|
|
|
|
selectedAccount, err := b.AccountManager().SelectedAccount()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return crypto.Sign(content, selectedAccount.AccountKey.PrivateKey)
|
|
|
|
}
|
2018-11-06 08:05:32 +00:00
|
|
|
|
|
|
|
// EnableInstallation enables an installation for multi-device sync.
|
|
|
|
func (b *StatusBackend) EnableInstallation(installationID string) error {
|
|
|
|
selectedAccount, err := b.AccountManager().SelectedAccount()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
st, err := b.statusNode.ShhExtService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := st.EnableInstallation(&selectedAccount.AccountKey.PrivateKey.PublicKey, 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 {
|
|
|
|
selectedAccount, err := b.AccountManager().SelectedAccount()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
st, err := b.statusNode.ShhExtService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := st.DisableInstallation(&selectedAccount.AccountKey.PrivateKey.PublicKey, 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
|
|
|
}
|