2016-06-15 13:54:07 +00:00
|
|
|
package main
|
|
|
|
|
2016-06-15 19:50:35 +00:00
|
|
|
import "C"
|
2016-06-15 13:54:07 +00:00
|
|
|
import (
|
2016-06-22 18:56:27 +00:00
|
|
|
"encoding/json"
|
2016-06-15 13:54:07 +00:00
|
|
|
"fmt"
|
2016-07-27 11:47:41 +00:00
|
|
|
"os"
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-10-23 15:46:51 +00:00
|
|
|
"github.com/NaySoftware/go-fcm"
|
2018-03-20 18:35:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2017-03-15 21:03:01 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
2018-04-22 16:50:34 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2018-02-02 12:23:47 +00:00
|
|
|
"github.com/status-im/status-go/profiling"
|
2017-10-23 15:46:51 +00:00
|
|
|
"gopkg.in/go-playground/validator.v9"
|
2016-06-15 13:54:07 +00:00
|
|
|
)
|
|
|
|
|
2018-03-20 18:35:28 +00:00
|
|
|
// All general log messages in this package should be routed through this logger.
|
|
|
|
var logger = log.New("package", "status-go/lib")
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//GenerateConfig for status node
|
2017-05-16 12:09:52 +00:00
|
|
|
//export GenerateConfig
|
2018-04-26 17:59:57 +00:00
|
|
|
func GenerateConfig(datadir *C.char, networkID C.int) *C.char {
|
|
|
|
config, err := params.NewNodeConfig(C.GoString(datadir), "", uint64(networkID))
|
2017-05-16 12:09:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2017-10-18 20:03:05 +00:00
|
|
|
outBytes, err := json.Marshal(config)
|
2017-05-16 12:09:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.CString(string(outBytes))
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//StartNode - start Status node
|
2017-05-16 12:09:52 +00:00
|
|
|
//export StartNode
|
|
|
|
func StartNode(configJSON *C.char) *C.char {
|
|
|
|
config, err := params.LoadNodeConfig(C.GoString(configJSON))
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2018-04-26 16:28:42 +00:00
|
|
|
if err := logutils.OverrideRootLog(config.LogEnabled, config.LogLevel, config.LogFile, false); err != nil {
|
2018-04-22 16:50:34 +00:00
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2018-02-09 13:37:56 +00:00
|
|
|
statusAPI.StartNodeAsync(config)
|
2018-04-18 14:13:43 +00:00
|
|
|
|
2018-02-09 13:37:56 +00:00
|
|
|
return makeJSONResponse(nil)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//StopNode - stop status node
|
2017-05-16 12:09:52 +00:00
|
|
|
//export StopNode
|
|
|
|
func StopNode() *C.char {
|
2018-02-09 13:37:56 +00:00
|
|
|
statusAPI.StopNodeAsync()
|
|
|
|
return makeJSONResponse(nil)
|
2017-05-25 13:14:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//ValidateNodeConfig validates config for status node
|
2017-08-10 15:31:29 +00:00
|
|
|
//export ValidateNodeConfig
|
|
|
|
func ValidateNodeConfig(configJSON *C.char) *C.char {
|
2018-03-29 09:20:55 +00:00
|
|
|
var resp APIDetailedResponse
|
2017-08-10 15:31:29 +00:00
|
|
|
|
|
|
|
_, err := params.LoadNodeConfig(C.GoString(configJSON))
|
|
|
|
|
2018-03-29 09:20:55 +00:00
|
|
|
// Convert errors to APIDetailedResponse
|
2017-08-10 15:31:29 +00:00
|
|
|
switch err := err.(type) {
|
|
|
|
case validator.ValidationErrors:
|
2018-03-29 09:20:55 +00:00
|
|
|
resp = APIDetailedResponse{
|
2017-08-10 15:31:29 +00:00
|
|
|
Message: "validation: validation failed",
|
2018-03-29 09:20:55 +00:00
|
|
|
FieldErrors: make([]APIFieldError, len(err)),
|
2017-08-10 15:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, ve := range err {
|
2018-03-29 09:20:55 +00:00
|
|
|
resp.FieldErrors[i] = APIFieldError{
|
2017-08-10 15:31:29 +00:00
|
|
|
Parameter: ve.Namespace(),
|
2018-03-29 09:20:55 +00:00
|
|
|
Errors: []APIError{
|
2017-08-10 15:31:29 +00:00
|
|
|
{
|
|
|
|
Message: fmt.Sprintf("field validation failed on the '%s' tag", ve.Tag()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case error:
|
2018-03-29 09:20:55 +00:00
|
|
|
resp = APIDetailedResponse{
|
2017-08-10 15:31:29 +00:00
|
|
|
Message: fmt.Sprintf("validation: %s", err.Error()),
|
|
|
|
}
|
|
|
|
case nil:
|
2018-03-29 09:20:55 +00:00
|
|
|
resp = APIDetailedResponse{
|
2017-08-10 15:31:29 +00:00
|
|
|
Status: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
respJSON, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.CString(string(respJSON))
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//ResetChainData remove chain data from data directory
|
2017-05-25 13:14:52 +00:00
|
|
|
//export ResetChainData
|
|
|
|
func ResetChainData() *C.char {
|
2018-02-09 13:37:56 +00:00
|
|
|
statusAPI.ResetChainDataAsync()
|
|
|
|
return makeJSONResponse(nil)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 08:01:37 +00:00
|
|
|
//CallRPC calls public APIs via RPC
|
2017-05-28 13:57:30 +00:00
|
|
|
//export CallRPC
|
|
|
|
func CallRPC(inputJSON *C.char) *C.char {
|
|
|
|
outputJSON := statusAPI.CallRPC(C.GoString(inputJSON))
|
|
|
|
return C.CString(outputJSON)
|
|
|
|
}
|
|
|
|
|
2018-04-16 08:01:37 +00:00
|
|
|
//CallPrivateRPC calls both public and private APIs via RPC
|
|
|
|
//export CallPrivateRPC
|
|
|
|
func CallPrivateRPC(inputJSON *C.char) *C.char {
|
|
|
|
outputJSON := statusAPI.CallPrivateRPC(C.GoString(inputJSON))
|
|
|
|
return C.CString(outputJSON)
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//CreateAccount is equivalent to creating an account from the command line,
|
|
|
|
// just modified to handle the function arg passing
|
2016-06-29 11:32:04 +00:00
|
|
|
//export CreateAccount
|
2016-07-01 13:23:39 +00:00
|
|
|
func CreateAccount(password *C.char) *C.char {
|
2017-05-16 12:09:52 +00:00
|
|
|
address, pubKey, mnemonic, err := statusAPI.CreateAccount(C.GoString(password))
|
2016-06-30 13:23:07 +00:00
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
errString := ""
|
2016-06-30 13:23:07 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
errString = err.Error()
|
|
|
|
}
|
|
|
|
|
2018-03-29 09:20:55 +00:00
|
|
|
out := AccountInfo{
|
2016-08-18 00:15:58 +00:00
|
|
|
Address: address,
|
|
|
|
PubKey: pubKey,
|
|
|
|
Mnemonic: mnemonic,
|
|
|
|
Error: errString,
|
|
|
|
}
|
2017-10-18 20:03:05 +00:00
|
|
|
outBytes, _ := json.Marshal(out)
|
2016-08-18 00:15:58 +00:00
|
|
|
return C.CString(string(outBytes))
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//CreateChildAccount creates sub-account
|
2016-08-23 21:32:04 +00:00
|
|
|
//export CreateChildAccount
|
|
|
|
func CreateChildAccount(parentAddress, password *C.char) *C.char {
|
2017-05-16 12:09:52 +00:00
|
|
|
address, pubKey, err := statusAPI.CreateChildAccount(C.GoString(parentAddress), C.GoString(password))
|
2016-08-23 21:32:04 +00:00
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
errString := ""
|
2016-08-23 21:32:04 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
errString = err.Error()
|
|
|
|
}
|
|
|
|
|
2018-03-29 09:20:55 +00:00
|
|
|
out := AccountInfo{
|
2016-08-23 21:32:04 +00:00
|
|
|
Address: address,
|
|
|
|
PubKey: pubKey,
|
|
|
|
Error: errString,
|
|
|
|
}
|
2017-10-18 20:03:05 +00:00
|
|
|
outBytes, _ := json.Marshal(out)
|
2016-08-23 21:32:04 +00:00
|
|
|
return C.CString(string(outBytes))
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//RecoverAccount re-creates master key using given details
|
2016-08-24 15:10:42 +00:00
|
|
|
//export RecoverAccount
|
|
|
|
func RecoverAccount(password, mnemonic *C.char) *C.char {
|
2017-05-16 12:09:52 +00:00
|
|
|
address, pubKey, err := statusAPI.RecoverAccount(C.GoString(password), C.GoString(mnemonic))
|
2016-08-18 00:15:58 +00:00
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
errString := ""
|
2016-08-18 00:15:58 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
errString = err.Error()
|
|
|
|
}
|
|
|
|
|
2018-03-29 09:20:55 +00:00
|
|
|
out := AccountInfo{
|
2016-08-18 00:15:58 +00:00
|
|
|
Address: address,
|
|
|
|
PubKey: pubKey,
|
|
|
|
Mnemonic: C.GoString(mnemonic),
|
|
|
|
Error: errString,
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
2017-10-18 20:03:05 +00:00
|
|
|
outBytes, _ := json.Marshal(out)
|
2016-06-30 13:23:07 +00:00
|
|
|
return C.CString(string(outBytes))
|
2016-06-15 13:54:07 +00:00
|
|
|
}
|
2016-06-20 15:21:45 +00:00
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//VerifyAccountPassword verifies account password
|
2017-05-06 21:53:18 +00:00
|
|
|
//export VerifyAccountPassword
|
2017-05-15 21:49:22 +00:00
|
|
|
func VerifyAccountPassword(keyStoreDir, address, password *C.char) *C.char {
|
2017-05-16 12:09:52 +00:00
|
|
|
_, err := statusAPI.VerifyAccountPassword(C.GoString(keyStoreDir), C.GoString(address), C.GoString(password))
|
|
|
|
return makeJSONResponse(err)
|
2017-05-06 21:53:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//Login loads a key file (for a given address), tries to decrypt it using the password, to verify ownership
|
|
|
|
// if verified, purges all the previous identities from Whisper, and injects verified key as shh identity
|
2016-06-29 11:32:04 +00:00
|
|
|
//export Login
|
|
|
|
func Login(address, password *C.char) *C.char {
|
2017-05-16 12:09:52 +00:00
|
|
|
err := statusAPI.SelectAccount(C.GoString(address), C.GoString(password))
|
|
|
|
return makeJSONResponse(err)
|
2016-06-29 11:32:04 +00:00
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//Logout is equivalent to clearing whisper identities
|
2016-08-29 00:31:16 +00:00
|
|
|
//export Logout
|
|
|
|
func Logout() *C.char {
|
2017-05-16 12:09:52 +00:00
|
|
|
err := statusAPI.Logout()
|
|
|
|
return makeJSONResponse(err)
|
2016-08-29 00:31:16 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
//ApproveSignRequest instructs backend to complete sending of a given transaction
|
|
|
|
//export ApproveSignRequest
|
|
|
|
func ApproveSignRequest(id, password *C.char) *C.char {
|
|
|
|
result := statusAPI.ApproveSignRequest(C.GoString(id), C.GoString(password))
|
2016-07-27 11:47:41 +00:00
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
errString := ""
|
2018-04-10 10:02:54 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, result.Error)
|
|
|
|
errString = result.Error.Error()
|
2016-07-27 11:47:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
out := SignRequestResult{
|
2017-05-03 14:24:48 +00:00
|
|
|
ID: C.GoString(id),
|
2018-04-10 10:02:54 +00:00
|
|
|
Hash: result.Response.Hex(),
|
2016-07-27 11:47:41 +00:00
|
|
|
Error: errString,
|
|
|
|
}
|
2017-10-18 20:03:05 +00:00
|
|
|
outBytes, err := json.Marshal(out)
|
2017-09-04 12:56:58 +00:00
|
|
|
if err != nil {
|
2018-04-10 10:02:54 +00:00
|
|
|
logger.Error("failed to marshal ApproveSignRequest output", "error", err)
|
2017-09-04 12:56:58 +00:00
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2016-07-27 11:47:41 +00:00
|
|
|
|
|
|
|
return C.CString(string(outBytes))
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
//ApproveSignRequests instructs backend to complete sending of multiple transactions
|
|
|
|
//export ApproveSignRequests
|
|
|
|
func ApproveSignRequests(ids, password *C.char) *C.char {
|
|
|
|
out := SignRequestsResult{}
|
|
|
|
out.Results = make(map[string]SignRequestResult)
|
2016-11-05 17:12:24 +00:00
|
|
|
|
2018-03-29 09:20:55 +00:00
|
|
|
parsedIDs, err := ParseJSONArray(C.GoString(ids))
|
2017-09-04 12:56:58 +00:00
|
|
|
if err != nil {
|
2018-04-10 10:02:54 +00:00
|
|
|
out.Results["none"] = SignRequestResult{
|
2017-09-04 12:56:58 +00:00
|
|
|
Error: err.Error(),
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-04 17:39:38 +00:00
|
|
|
txIDs := make([]string, len(parsedIDs))
|
2017-09-04 12:56:58 +00:00
|
|
|
for i, id := range parsedIDs {
|
2018-04-04 17:39:38 +00:00
|
|
|
txIDs[i] = id
|
2016-11-05 17:12:24 +00:00
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
results := statusAPI.ApproveSignRequests(txIDs, C.GoString(password))
|
2017-09-04 12:56:58 +00:00
|
|
|
for txID, result := range results {
|
2018-04-10 10:02:54 +00:00
|
|
|
txResult := SignRequestResult{
|
2018-04-04 17:39:38 +00:00
|
|
|
ID: txID,
|
2018-04-10 10:02:54 +00:00
|
|
|
Hash: result.Response.Hex(),
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
|
|
|
if result.Error != nil {
|
|
|
|
txResult.Error = result.Error.Error()
|
|
|
|
}
|
2018-04-04 17:39:38 +00:00
|
|
|
out.Results[txID] = txResult
|
2016-11-05 17:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2017-10-18 20:03:05 +00:00
|
|
|
outBytes, err := json.Marshal(out)
|
2017-09-04 12:56:58 +00:00
|
|
|
if err != nil {
|
2018-04-10 10:02:54 +00:00
|
|
|
logger.Error("failed to marshal ApproveSignRequests output", "error", err)
|
2017-09-04 12:56:58 +00:00
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2016-11-05 17:12:24 +00:00
|
|
|
|
|
|
|
return C.CString(string(outBytes))
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
//DiscardSignRequest discards a given transaction from transaction queue
|
|
|
|
//export DiscardSignRequest
|
|
|
|
func DiscardSignRequest(id *C.char) *C.char {
|
|
|
|
err := statusAPI.DiscardSignRequest(C.GoString(id))
|
2016-10-30 22:35:10 +00:00
|
|
|
|
|
|
|
errString := ""
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
errString = err.Error()
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
out := DiscardSignRequestResult{
|
2017-05-03 14:24:48 +00:00
|
|
|
ID: C.GoString(id),
|
2016-10-30 22:35:10 +00:00
|
|
|
Error: errString,
|
|
|
|
}
|
2017-10-18 20:03:05 +00:00
|
|
|
outBytes, err := json.Marshal(out)
|
2017-09-04 12:56:58 +00:00
|
|
|
if err != nil {
|
2018-04-10 10:02:54 +00:00
|
|
|
log.Error("failed to marshal DiscardSignRequest output", "error", err)
|
2017-09-04 12:56:58 +00:00
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2016-10-30 22:35:10 +00:00
|
|
|
|
|
|
|
return C.CString(string(outBytes))
|
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
//DiscardSignRequests discards given multiple transactions from transaction queue
|
|
|
|
//export DiscardSignRequests
|
|
|
|
func DiscardSignRequests(ids *C.char) *C.char {
|
|
|
|
out := DiscardSignRequestsResult{}
|
|
|
|
out.Results = make(map[string]DiscardSignRequestResult)
|
2016-11-05 17:12:24 +00:00
|
|
|
|
2018-03-29 09:20:55 +00:00
|
|
|
parsedIDs, err := ParseJSONArray(C.GoString(ids))
|
2017-09-04 12:56:58 +00:00
|
|
|
if err != nil {
|
2018-04-10 10:02:54 +00:00
|
|
|
out.Results["none"] = DiscardSignRequestResult{
|
2017-09-04 12:56:58 +00:00
|
|
|
Error: err.Error(),
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-04 17:39:38 +00:00
|
|
|
txIDs := make([]string, len(parsedIDs))
|
2017-09-04 12:56:58 +00:00
|
|
|
for i, id := range parsedIDs {
|
2018-04-04 17:39:38 +00:00
|
|
|
txIDs[i] = id
|
2016-11-05 17:12:24 +00:00
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
results := statusAPI.DiscardSignRequests(txIDs)
|
2018-04-04 17:39:38 +00:00
|
|
|
for txID, err := range results {
|
2018-04-10 10:02:54 +00:00
|
|
|
out.Results[txID] = DiscardSignRequestResult{
|
2018-04-04 17:39:38 +00:00
|
|
|
ID: txID,
|
|
|
|
Error: err.Error(),
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
2016-11-05 17:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-04 12:56:58 +00:00
|
|
|
|
2017-10-18 20:03:05 +00:00
|
|
|
outBytes, err := json.Marshal(out)
|
2017-09-04 12:56:58 +00:00
|
|
|
if err != nil {
|
2018-04-10 10:02:54 +00:00
|
|
|
logger.Error("failed to marshal DiscardSignRequests output", "error", err)
|
2017-09-04 12:56:58 +00:00
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2016-11-05 17:12:24 +00:00
|
|
|
|
|
|
|
return C.CString(string(outBytes))
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//InitJail setup initial JavaScript
|
2016-09-11 11:44:14 +00:00
|
|
|
//export InitJail
|
|
|
|
func InitJail(js *C.char) {
|
2017-11-07 17:36:42 +00:00
|
|
|
statusAPI.SetJailBaseJS(C.GoString(js))
|
2016-06-22 09:17:51 +00:00
|
|
|
}
|
|
|
|
|
2017-11-23 12:37:59 +00:00
|
|
|
//Parse creates a new jail cell context and executes provided JavaScript code.
|
2017-11-23 12:51:52 +00:00
|
|
|
//DEPRECATED in favour of CreateAndInitCell.
|
2017-11-23 12:37:59 +00:00
|
|
|
//export Parse
|
|
|
|
func Parse(chatID *C.char, js *C.char) *C.char {
|
2017-12-08 15:32:30 +00:00
|
|
|
res := statusAPI.CreateAndInitCell(C.GoString(chatID), C.GoString(js))
|
2017-11-23 12:37:59 +00:00
|
|
|
return C.CString(res)
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
//CreateAndInitCell creates a new jail cell context and executes provided JavaScript code.
|
|
|
|
//export CreateAndInitCell
|
|
|
|
func CreateAndInitCell(chatID *C.char, js *C.char) *C.char {
|
|
|
|
res := statusAPI.CreateAndInitCell(C.GoString(chatID), C.GoString(js))
|
|
|
|
return C.CString(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
//ExecuteJS allows to run arbitrary JS code within a cell.
|
|
|
|
//export ExecuteJS
|
|
|
|
func ExecuteJS(chatID *C.char, code *C.char) *C.char {
|
|
|
|
res := statusAPI.JailExecute(C.GoString(chatID), C.GoString(code))
|
2016-06-22 09:17:51 +00:00
|
|
|
return C.CString(res)
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//Call executes given JavaScript function
|
2016-09-11 11:44:14 +00:00
|
|
|
//export Call
|
2017-05-03 14:24:48 +00:00
|
|
|
func Call(chatID *C.char, path *C.char, params *C.char) *C.char {
|
2017-05-16 12:09:52 +00:00
|
|
|
res := statusAPI.JailCall(C.GoString(chatID), C.GoString(path), C.GoString(params))
|
2016-09-11 11:44:14 +00:00
|
|
|
return C.CString(res)
|
2016-07-04 16:16:18 +00:00
|
|
|
}
|
2016-07-04 16:00:29 +00:00
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//StartCPUProfile runs pprof for cpu
|
2017-09-01 14:09:11 +00:00
|
|
|
//export StartCPUProfile
|
|
|
|
func StartCPUProfile(dataDir *C.char) *C.char {
|
|
|
|
err := profiling.StartCPUProfile(C.GoString(dataDir))
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//StopCPUProfiling stops pprof for cpu
|
2017-09-01 14:09:11 +00:00
|
|
|
//export StopCPUProfiling
|
2017-10-20 09:06:22 +00:00
|
|
|
func StopCPUProfiling() *C.char { //nolint: deadcode
|
2017-09-01 14:09:11 +00:00
|
|
|
err := profiling.StopCPUProfile()
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:06:22 +00:00
|
|
|
//WriteHeapProfile starts pprof for heap
|
2017-09-01 14:09:11 +00:00
|
|
|
//export WriteHeapProfile
|
2017-10-20 09:06:22 +00:00
|
|
|
func WriteHeapProfile(dataDir *C.char) *C.char { //nolint: deadcode
|
2017-09-01 14:09:11 +00:00
|
|
|
err := profiling.WriteHeapFile(C.GoString(dataDir))
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
func makeJSONResponse(err error) *C.char {
|
2016-12-11 12:19:20 +00:00
|
|
|
errString := ""
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
errString = err.Error()
|
|
|
|
}
|
|
|
|
|
2018-03-29 09:20:55 +00:00
|
|
|
out := APIResponse{
|
2016-12-11 12:19:20 +00:00
|
|
|
Error: errString,
|
|
|
|
}
|
2017-10-18 20:03:05 +00:00
|
|
|
outBytes, _ := json.Marshal(out)
|
2016-12-11 12:19:20 +00:00
|
|
|
|
|
|
|
return C.CString(string(outBytes))
|
|
|
|
}
|
2017-09-15 15:57:34 +00:00
|
|
|
|
2017-10-23 15:46:51 +00:00
|
|
|
// NotifyUsers sends push notifications by given tokens.
|
|
|
|
//export NotifyUsers
|
|
|
|
func NotifyUsers(message, payloadJSON, tokensArray *C.char) (outCBytes *C.char) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
outBytes []byte
|
|
|
|
)
|
2017-10-12 14:31:39 +00:00
|
|
|
errString := ""
|
2017-10-23 15:46:51 +00:00
|
|
|
|
|
|
|
defer func() {
|
2018-03-29 09:20:55 +00:00
|
|
|
out := NotifyResult{
|
2017-10-23 15:46:51 +00:00
|
|
|
Status: err == nil,
|
|
|
|
Error: errString,
|
|
|
|
}
|
|
|
|
|
|
|
|
outBytes, err = json.Marshal(out)
|
|
|
|
if err != nil {
|
2018-03-20 18:35:28 +00:00
|
|
|
logger.Error("failed to marshal Notify output", "error", err)
|
2017-10-23 15:46:51 +00:00
|
|
|
outCBytes = makeJSONResponse(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
outCBytes = C.CString(string(outBytes))
|
|
|
|
}()
|
|
|
|
|
2018-03-29 09:20:55 +00:00
|
|
|
tokens, err := ParseJSONArray(C.GoString(tokensArray))
|
2017-10-12 14:31:39 +00:00
|
|
|
if err != nil {
|
|
|
|
errString = err.Error()
|
2017-10-23 15:46:51 +00:00
|
|
|
return
|
2017-10-12 14:31:39 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 15:46:51 +00:00
|
|
|
var payload fcm.NotificationPayload
|
|
|
|
err = json.Unmarshal([]byte(C.GoString(payloadJSON)), &payload)
|
|
|
|
if err != nil {
|
|
|
|
errString = err.Error()
|
|
|
|
return
|
2017-10-12 14:31:39 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 15:46:51 +00:00
|
|
|
err = statusAPI.NotifyUsers(C.GoString(message), payload, tokens...)
|
2017-10-12 14:31:39 +00:00
|
|
|
if err != nil {
|
2017-10-23 15:46:51 +00:00
|
|
|
errString = err.Error()
|
|
|
|
return
|
2017-10-12 14:31:39 +00:00
|
|
|
}
|
|
|
|
|
2017-10-23 15:46:51 +00:00
|
|
|
return
|
2017-09-15 15:57:34 +00:00
|
|
|
}
|
2017-12-04 16:21:02 +00:00
|
|
|
|
2017-12-21 10:26:01 +00:00
|
|
|
// AddPeer adds an enode as a peer.
|
2017-12-04 16:21:02 +00:00
|
|
|
//export AddPeer
|
|
|
|
func AddPeer(enode *C.char) *C.char {
|
2018-04-05 09:45:26 +00:00
|
|
|
err := statusAPI.StatusNode().AddPeer(C.GoString(enode))
|
2017-12-04 16:21:02 +00:00
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2018-02-09 20:28:16 +00:00
|
|
|
|
|
|
|
// ConnectionChange handles network state changes as reported
|
|
|
|
// by ReactNative (see https://facebook.github.io/react-native/docs/netinfo.html)
|
2018-02-20 08:22:15 +00:00
|
|
|
//export ConnectionChange
|
2018-02-09 20:28:16 +00:00
|
|
|
func ConnectionChange(typ *C.char, expensive C.int) {
|
|
|
|
statusAPI.ConnectionChange(C.GoString(typ), expensive == 1)
|
|
|
|
}
|
2018-03-14 15:46:21 +00:00
|
|
|
|
|
|
|
// AppStateChange handles app state changes (background/foreground).
|
|
|
|
//export AppStateChange
|
|
|
|
func AppStateChange(state *C.char) {
|
|
|
|
statusAPI.AppStateChange(C.GoString(state))
|
|
|
|
}
|