2017-05-16 12:09:52 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2017-09-04 12:56:58 +00:00
|
|
|
"context"
|
|
|
|
|
2017-10-23 15:46:51 +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"
|
2018-03-20 18:35:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
|
|
|
"github.com/status-im/status-go/jail"
|
|
|
|
"github.com/status-im/status-go/node"
|
|
|
|
"github.com/status-im/status-go/params"
|
2018-04-09 08:18:22 +00:00
|
|
|
"github.com/status-im/status-go/sign"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
2017-05-16 12:09:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// StatusAPI provides API to access Status related functionality.
|
|
|
|
type StatusAPI struct {
|
2018-03-20 18:35:28 +00:00
|
|
|
b *StatusBackend
|
|
|
|
log log.Logger
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-12-02 18:51:55 +00:00
|
|
|
// NewStatusAPI creates a new StatusAPI instance
|
2017-05-16 12:09:52 +00:00
|
|
|
func NewStatusAPI() *StatusAPI {
|
2017-12-02 18:51:55 +00:00
|
|
|
return NewStatusAPIWithBackend(NewStatusBackend())
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStatusAPIWithBackend creates a new StatusAPI instance using
|
|
|
|
// the passed backend.
|
|
|
|
func NewStatusAPIWithBackend(b *StatusBackend) *StatusAPI {
|
2017-05-16 12:09:52 +00:00
|
|
|
return &StatusAPI{
|
2018-03-20 18:35:28 +00:00
|
|
|
b: b,
|
2018-06-08 11:29:50 +00:00
|
|
|
log: log.New("package", "status-go/api.StatusAPI"),
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
// StatusNode returns reference to StatusNode.
|
|
|
|
func (api *StatusAPI) StatusNode() *node.StatusNode {
|
|
|
|
return api.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 (api *StatusAPI) AccountManager() *account.Manager {
|
2017-05-16 12:09:52 +00:00
|
|
|
return api.b.AccountManager()
|
|
|
|
}
|
|
|
|
|
|
|
|
// JailManager returns reference to jail
|
2018-03-01 16:48:30 +00:00
|
|
|
func (api *StatusAPI) JailManager() jail.Manager {
|
2017-05-16 12:09:52 +00:00
|
|
|
return api.b.JailManager()
|
|
|
|
}
|
|
|
|
|
2018-04-09 08:18:22 +00:00
|
|
|
// Transactor returns reference to a status transactor
|
|
|
|
func (api *StatusAPI) Transactor() *transactions.Transactor {
|
|
|
|
return api.b.Transactor()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PendingSignRequests returns reference to a list of current sign requests
|
|
|
|
func (api *StatusAPI) PendingSignRequests() *sign.PendingRequests {
|
|
|
|
return api.b.PendingSignRequests()
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-02-09 13:37:56 +00:00
|
|
|
return api.b.StartNode(config)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2018-02-09 13:37:56 +00:00
|
|
|
func (api *StatusAPI) StartNodeAsync(config *params.NodeConfig) <-chan error {
|
|
|
|
return runAsync(func() error { return api.StartNode(config) })
|
2017-05-25 13:14:52 +00:00
|
|
|
}
|
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 {
|
2018-02-09 13:37:56 +00:00
|
|
|
return api.b.StopNode()
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2018-02-09 13:37:56 +00:00
|
|
|
func (api *StatusAPI) StopNodeAsync() <-chan error {
|
|
|
|
return runAsync(api.StopNode)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RestartNode restart running Status node, fails if node is not running
|
|
|
|
func (api *StatusAPI) RestartNode() error {
|
2018-02-09 13:37:56 +00:00
|
|
|
return api.b.RestartNode()
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
// RestartNodeAsync restart running Status node, in async manner
|
2018-02-09 13:37:56 +00:00
|
|
|
func (api *StatusAPI) RestartNodeAsync() <-chan error {
|
|
|
|
return runAsync(api.RestartNode)
|
2017-05-25 13:14:52 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-02-09 13:37:56 +00:00
|
|
|
return api.b.ResetChainData()
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
// ResetChainDataAsync remove chain data from data directory, in async manner
|
2018-02-09 13:37:56 +00:00
|
|
|
func (api *StatusAPI) ResetChainDataAsync() <-chan error {
|
|
|
|
return runAsync(api.ResetChainData)
|
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.
|
2017-05-28 13:57:30 +00:00
|
|
|
func (api *StatusAPI) CallRPC(inputJSON string) string {
|
|
|
|
return api.b.CallRPC(inputJSON)
|
|
|
|
}
|
|
|
|
|
2018-04-16 08:01:37 +00:00
|
|
|
// CallPrivateRPC executes public and private RPC requests on node's in-proc RPC server.
|
|
|
|
func (api *StatusAPI) CallPrivateRPC(inputJSON string) string {
|
|
|
|
return api.b.CallPrivateRPC(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 {
|
2018-03-22 12:31:12 +00:00
|
|
|
return api.b.SelectAccount(address, password)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Logout clears whisper identities
|
|
|
|
func (api *StatusAPI) Logout() error {
|
2018-03-22 12:31:12 +00:00
|
|
|
return api.b.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.
|
2018-04-04 17:39:38 +00:00
|
|
|
func (api *StatusAPI) SendTransaction(ctx context.Context, args transactions.SendTxArgs) (gethcommon.Hash, error) {
|
2017-09-04 12:56:58 +00:00
|
|
|
return api.b.SendTransaction(ctx, args)
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
// ApproveSignRequest instructs backend to complete sending of a given transaction
|
2018-06-06 07:35:32 +00:00
|
|
|
func (api *StatusAPI) ApproveSignRequest(id, password string) sign.Result {
|
2018-04-10 10:02:54 +00:00
|
|
|
return api.b.ApproveSignRequest(id, password)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 07:35:32 +00:00
|
|
|
// ApproveSignRequestWithArgs instructs backend to complete sending of a given transaction
|
|
|
|
// gas and gasPrice will be overrided with the given values before signing the
|
|
|
|
// transaction.
|
|
|
|
func (api *StatusAPI) ApproveSignRequestWithArgs(id, password string, gas, gasPrice int64) sign.Result {
|
|
|
|
return api.b.ApproveSignRequestWithArgs(id, password, gas, gasPrice)
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
// ApproveSignRequests instructs backend to complete sending of multiple transactions
|
|
|
|
func (api *StatusAPI) ApproveSignRequests(ids []string, password string) map[string]sign.Result {
|
|
|
|
return api.b.ApproveSignRequests(ids, password)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
// DiscardSignRequest discards a given transaction from transaction queue
|
|
|
|
func (api *StatusAPI) DiscardSignRequest(id string) error {
|
|
|
|
return api.b.DiscardSignRequest(id)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
// DiscardSignRequests discards given multiple transactions from transaction queue
|
|
|
|
func (api *StatusAPI) DiscardSignRequests(ids []string) map[string]error {
|
|
|
|
return api.b.DiscardSignRequests(ids)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-23 12:37:59 +00:00
|
|
|
// JailParse creates a new jail cell context, with the given chatID as identifier.
|
|
|
|
// New context executes provided JavaScript code, right after the initialization.
|
|
|
|
// DEPRECATED in favour of CreateAndInitCell.
|
|
|
|
func (api *StatusAPI) JailParse(chatID string, js string) string {
|
|
|
|
return api.b.jailManager.Parse(chatID, js)
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// CreateAndInitCell 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.
|
2017-11-07 17:36:42 +00:00
|
|
|
func (api *StatusAPI) CreateAndInitCell(chatID, js string) string {
|
|
|
|
return api.b.jailManager.CreateAndInitCell(chatID, js)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
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-11-07 17:36:42 +00:00
|
|
|
// JailExecute allows to run arbitrary JS code within a jail cell.
|
|
|
|
func (api *StatusAPI) JailExecute(chatID, code string) string {
|
|
|
|
return api.b.jailManager.Execute(chatID, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetJailBaseJS allows to setup initial JavaScript to be loaded on each jail.CreateAndInitCell().
|
|
|
|
func (api *StatusAPI) SetJailBaseJS(js string) {
|
|
|
|
api.b.jailManager.SetBaseJS(js)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2017-09-15 15:57:34 +00:00
|
|
|
|
2017-10-23 15:46:51 +00:00
|
|
|
// NotifyUsers send notifications to users.
|
|
|
|
func (api *StatusAPI) NotifyUsers(message string, payload fcm.NotificationPayload, tokens ...string) error {
|
2018-04-23 13:34:49 +00:00
|
|
|
return api.b.NotifyUsers(message, payload, tokens...)
|
2017-09-15 15:57:34 +00:00
|
|
|
}
|
2018-02-09 20:28:16 +00:00
|
|
|
|
|
|
|
// ConnectionChange handles network state changes logic.
|
|
|
|
func (api *StatusAPI) ConnectionChange(typ string, expensive bool) {
|
2018-04-23 13:34:49 +00:00
|
|
|
api.b.ConnectionChange(typ, expensive)
|
2018-02-09 20:28:16 +00:00
|
|
|
}
|
2018-03-14 15:46:21 +00:00
|
|
|
|
|
|
|
// AppStateChange handles app state changes (background/foreground).
|
|
|
|
// state values: see https://facebook.github.io/react-native/docs/appstate.html
|
|
|
|
func (api *StatusAPI) AppStateChange(state string) {
|
2018-04-23 13:34:49 +00:00
|
|
|
api.b.AppStateChange(state)
|
2018-03-14 15:46:21 +00:00
|
|
|
}
|