2017-05-16 12:09:52 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2017-09-04 12:56:58 +00:00
|
|
|
"context"
|
|
|
|
|
2017-09-15 15:57:34 +00:00
|
|
|
"github.com/NaySoftware/go-fcm"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/status-im/status-go/geth/common"
|
2017-09-15 15:57:34 +00:00
|
|
|
"github.com/status-im/status-go/geth/log"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
|
|
|
)
|
|
|
|
|
2017-09-15 15:57:34 +00:00
|
|
|
const (
|
|
|
|
serverKey = "AAAAxwa-r08:APA91bFtMIToDVKGAmVCm76iEXtA4dn9MPvLdYKIZqAlNpLJbd12EgdBI9DSDSXKdqvIAgLodepmRhGVaWvhxnXJzVpE6MoIRuKedDV3kfHSVBhWFqsyoLTwXY4xeufL9Sdzb581U-lx"
|
|
|
|
)
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// StatusAPI provides API to access Status related functionality.
|
|
|
|
type StatusAPI struct {
|
|
|
|
b *StatusBackend
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStatusAPI create a new StatusAPI instance
|
|
|
|
func NewStatusAPI() *StatusAPI {
|
|
|
|
return &StatusAPI{
|
|
|
|
b: NewStatusBackend(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeManager returns reference to node manager
|
|
|
|
func (api *StatusAPI) NodeManager() common.NodeManager {
|
|
|
|
return api.b.NodeManager()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountManager returns reference to account manager
|
|
|
|
func (api *StatusAPI) AccountManager() common.AccountManager {
|
|
|
|
return api.b.AccountManager()
|
|
|
|
}
|
|
|
|
|
|
|
|
// JailManager returns reference to jail
|
|
|
|
func (api *StatusAPI) JailManager() common.JailManager {
|
|
|
|
return api.b.JailManager()
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// TxQueueManager returns reference to account manager
|
|
|
|
func (api *StatusAPI) TxQueueManager() common.TxQueueManager {
|
|
|
|
return api.b.TxQueueManager()
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// StartNode start Status node, fails if node is already started
|
|
|
|
func (api *StatusAPI) StartNode(config *params.NodeConfig) error {
|
|
|
|
nodeStarted, err := api.b.StartNode(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-25 13:14:52 +00:00
|
|
|
<-nodeStarted
|
2017-05-16 12:09:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
// StartNodeAsync start Status node, fails if node is already started
|
|
|
|
// Returns immediately w/o waiting for node to start (see node.ready)
|
|
|
|
func (api *StatusAPI) StartNodeAsync(config *params.NodeConfig) (<-chan struct{}, error) {
|
|
|
|
return api.b.StartNode(config)
|
|
|
|
}
|
2017-05-25 12:34:13 +00:00
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
// StopNode stop Status node. Stopped node cannot be resumed.
|
|
|
|
func (api *StatusAPI) StopNode() error {
|
|
|
|
nodeStopped, err := api.b.StopNode()
|
2017-05-16 12:09:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-25 13:14:52 +00:00
|
|
|
<-nodeStopped
|
2017-05-16 12:09:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
// StopNodeAsync stop Status node. Stopped node cannot be resumed.
|
|
|
|
// Returns immediately, w/o waiting for node to stop (see node.stopped)
|
|
|
|
func (api *StatusAPI) StopNodeAsync() (<-chan struct{}, error) {
|
2017-05-16 12:09:52 +00:00
|
|
|
return api.b.StopNode()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestartNode restart running Status node, fails if node is not running
|
|
|
|
func (api *StatusAPI) RestartNode() error {
|
|
|
|
nodeStarted, err := api.b.RestartNode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
<-nodeStarted // do not return up until backend is ready
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
// RestartNodeAsync restart running Status node, in async manner
|
|
|
|
func (api *StatusAPI) RestartNodeAsync() (<-chan struct{}, error) {
|
|
|
|
return api.b.RestartNode()
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// ResetChainData remove chain data from data directory.
|
|
|
|
// Node is stopped, and new node is started, with clean data directory.
|
|
|
|
func (api *StatusAPI) ResetChainData() error {
|
|
|
|
nodeStarted, err := api.b.ResetChainData()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
<-nodeStarted // do not return up until backend is ready
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
// ResetChainDataAsync remove chain data from data directory, in async manner
|
|
|
|
func (api *StatusAPI) ResetChainDataAsync() (<-chan struct{}, error) {
|
|
|
|
return api.b.ResetChainData()
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-28 13:57:30 +00:00
|
|
|
// CallRPC executes RPC request on node's in-proc RPC server
|
|
|
|
func (api *StatusAPI) CallRPC(inputJSON string) string {
|
|
|
|
return api.b.CallRPC(inputJSON)
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// CreateAccount creates an internal geth account
|
|
|
|
// BIP44-compatible keys are generated: CKD#1 is stored as account key, CKD#2 stored as sub-account root
|
|
|
|
// Public key of CKD#1 is returned, with CKD#2 securely encoded into account key file (to be used for
|
|
|
|
// sub-account derivations)
|
|
|
|
func (api *StatusAPI) CreateAccount(password string) (address, pubKey, mnemonic string, err error) {
|
2017-05-25 13:14:52 +00:00
|
|
|
return api.b.AccountManager().CreateAccount(password)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateChildAccount creates sub-account for an account identified by parent address.
|
|
|
|
// CKD#2 is used as root for master accounts (when parentAddress is "").
|
|
|
|
// Otherwise (when parentAddress != ""), child is derived directly from parent.
|
|
|
|
func (api *StatusAPI) CreateChildAccount(parentAddress, password string) (address, pubKey string, err error) {
|
2017-05-25 13:14:52 +00:00
|
|
|
return api.b.AccountManager().CreateChildAccount(parentAddress, password)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RecoverAccount re-creates master key using given details.
|
|
|
|
// Once master key is re-generated, it is inserted into keystore (if not already there).
|
|
|
|
func (api *StatusAPI) RecoverAccount(password, mnemonic string) (address, pubKey string, err error) {
|
2017-05-25 13:14:52 +00:00
|
|
|
return api.b.AccountManager().RecoverAccount(password, mnemonic)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// VerifyAccountPassword tries to decrypt a given account key file, with a provided password.
|
|
|
|
// If no error is returned, then account is considered verified.
|
|
|
|
func (api *StatusAPI) VerifyAccountPassword(keyStoreDir, address, password string) (*keystore.Key, error) {
|
2017-05-25 13:14:52 +00:00
|
|
|
return api.b.AccountManager().VerifyAccountPassword(keyStoreDir, address, password)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (api *StatusAPI) SelectAccount(address, password string) error {
|
2017-10-16 21:07:42 +00:00
|
|
|
// FIXME(oleg-raev): This method doesn't make stop, it rather resets its cells to an initial state
|
|
|
|
// and should be properly renamed, for example: ResetCells
|
|
|
|
api.b.jailManager.Stop()
|
2017-05-25 13:14:52 +00:00
|
|
|
return api.b.AccountManager().SelectAccount(address, password)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Logout clears whisper identities
|
|
|
|
func (api *StatusAPI) Logout() error {
|
2017-10-16 21:07:42 +00:00
|
|
|
api.b.jailManager.Stop()
|
2017-05-25 13:14:52 +00:00
|
|
|
return api.b.AccountManager().Logout()
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 12:56:58 +00:00
|
|
|
// SendTransaction creates a new transaction and waits until it's complete.
|
|
|
|
func (api *StatusAPI) SendTransaction(ctx context.Context, args common.SendTxArgs) (gethcommon.Hash, error) {
|
|
|
|
return api.b.SendTransaction(ctx, args)
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// CompleteTransaction instructs backend to complete sending of a given transaction
|
2017-09-04 12:56:58 +00:00
|
|
|
func (api *StatusAPI) CompleteTransaction(id common.QueuedTxID, password string) (gethcommon.Hash, error) {
|
|
|
|
return api.b.txQueueManager.CompleteTransaction(id, password)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CompleteTransactions instructs backend to complete sending of multiple transactions
|
2017-09-04 12:56:58 +00:00
|
|
|
func (api *StatusAPI) CompleteTransactions(ids []common.QueuedTxID, password string) map[common.QueuedTxID]common.RawCompleteTransactionResult {
|
|
|
|
return api.b.txQueueManager.CompleteTransactions(ids, password)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DiscardTransaction discards a given transaction from transaction queue
|
2017-09-04 12:56:58 +00:00
|
|
|
func (api *StatusAPI) DiscardTransaction(id common.QueuedTxID) error {
|
|
|
|
return api.b.txQueueManager.DiscardTransaction(id)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DiscardTransactions discards given multiple transactions from transaction queue
|
2017-09-04 12:56:58 +00:00
|
|
|
func (api *StatusAPI) DiscardTransactions(ids []common.QueuedTxID) map[common.QueuedTxID]common.RawDiscardTransactionResult {
|
|
|
|
return api.b.txQueueManager.DiscardTransactions(ids)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-28 13:57:30 +00:00
|
|
|
// JailParse creates a new jail cell context, with the given chatID as identifier.
|
2017-05-16 12:09:52 +00:00
|
|
|
// New context executes provided JavaScript code, right after the initialization.
|
|
|
|
func (api *StatusAPI) JailParse(chatID string, js string) string {
|
|
|
|
return api.b.jailManager.Parse(chatID, js)
|
|
|
|
}
|
|
|
|
|
2017-05-28 13:57:30 +00:00
|
|
|
// JailCall executes given JavaScript function w/i a jail cell context identified by the chatID.
|
2017-09-02 17:04:23 +00:00
|
|
|
func (api *StatusAPI) JailCall(chatID, this, args string) string {
|
|
|
|
return api.b.jailManager.Call(chatID, this, args)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-28 13:57:30 +00:00
|
|
|
// JailBaseJS allows to setup initial JavaScript to be loaded on each jail.Parse()
|
2017-05-16 12:09:52 +00:00
|
|
|
func (api *StatusAPI) JailBaseJS(js string) {
|
|
|
|
api.b.jailManager.BaseJS(js)
|
|
|
|
}
|
2017-09-15 15:57:34 +00:00
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
// Notify sends a push notification to the device with the given token.
|
2017-09-15 15:57:34 +00:00
|
|
|
// TODO(oskarth): API package this stuff
|
|
|
|
func (api *StatusAPI) Notify(token string) string {
|
|
|
|
log.Debug("Notify", "token", token)
|
|
|
|
|
|
|
|
var NP fcm.NotificationPayload
|
|
|
|
NP.Title = "Status - new message"
|
|
|
|
NP.Body = "ping"
|
|
|
|
|
|
|
|
// TODO(oskarth): Experiment with this
|
|
|
|
data := map[string]string{
|
|
|
|
"msg": "Hello World1",
|
|
|
|
"sum": "Happy Day",
|
|
|
|
}
|
|
|
|
|
|
|
|
ids := []string{
|
|
|
|
token,
|
|
|
|
}
|
|
|
|
|
|
|
|
c := fcm.NewFcmClient(serverKey)
|
|
|
|
c.NewFcmRegIdsMsg(ids, data)
|
|
|
|
c.SetNotificationPayload(&NP)
|
|
|
|
|
|
|
|
_, err := c.Send()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Notify failed:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return token
|
|
|
|
}
|