2016-06-15 13:54:07 +00:00
|
|
|
package main
|
|
|
|
|
2018-09-24 18:07:34 +00:00
|
|
|
// #include <stdlib.h>
|
2016-06-15 19:50:35 +00:00
|
|
|
import "C"
|
2016-06-15 13:54:07 +00:00
|
|
|
import (
|
2019-02-15 11:31:20 +00:00
|
|
|
"encoding/hex"
|
2016-06-22 18:56:27 +00:00
|
|
|
"encoding/json"
|
2019-03-04 14:33:48 +00:00
|
|
|
"errors"
|
2016-06-15 13:54:07 +00:00
|
|
|
"fmt"
|
2016-07-27 11:47:41 +00:00
|
|
|
"os"
|
2018-06-27 23:16:27 +00:00
|
|
|
"unsafe"
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2019-02-21 09:53:39 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-03-20 18:35:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-06-19 07:49:24 +00:00
|
|
|
"github.com/status-im/status-go/api"
|
2019-03-04 14:33:48 +00:00
|
|
|
"github.com/status-im/status-go/exportlogs"
|
2018-04-22 16:50:34 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2018-02-02 12:23:47 +00:00
|
|
|
"github.com/status-im/status-go/profiling"
|
2018-08-16 11:37:53 +00:00
|
|
|
"github.com/status-im/status-go/services/personal"
|
2018-11-06 06:26:12 +00:00
|
|
|
"github.com/status-im/status-go/services/typeddata"
|
2018-06-27 23:16:27 +00:00
|
|
|
"github.com/status-im/status-go/signal"
|
2018-08-16 11:37:53 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
2018-12-20 08:31:17 +00:00
|
|
|
validator "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
|
|
|
//StartNode - start Status node
|
2017-05-16 12:09:52 +00:00
|
|
|
//export StartNode
|
|
|
|
func StartNode(configJSON *C.char) *C.char {
|
2018-09-13 16:31:29 +00:00
|
|
|
config, err := params.NewConfigFromJSON(C.GoString(configJSON))
|
2017-05-16 12:09:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2019-03-01 13:37:13 +00:00
|
|
|
if err := logutils.OverrideRootLogWithConfig(config, false); err != nil {
|
2018-04-22 16:50:34 +00:00
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2018-06-19 07:49:24 +00:00
|
|
|
api.RunAsync(func() error { return statusBackend.StartNode(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-06-19 07:49:24 +00:00
|
|
|
api.RunAsync(statusBackend.StopNode)
|
2018-02-09 13:37:56 +00:00
|
|
|
return makeJSONResponse(nil)
|
2017-05-25 13:14:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:07:34 +00:00
|
|
|
// Create an X3DH bundle
|
|
|
|
//export CreateContactCode
|
|
|
|
func CreateContactCode() *C.char {
|
|
|
|
bundle, err := statusBackend.CreateContactCode()
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cstr := C.CString(bundle)
|
|
|
|
|
|
|
|
return cstr
|
|
|
|
}
|
|
|
|
|
|
|
|
//export ProcessContactCode
|
|
|
|
func ProcessContactCode(bundleString *C.char) *C.char {
|
|
|
|
err := statusBackend.ProcessContactCode(C.GoString(bundleString))
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-12 11:07:13 +00:00
|
|
|
// Get an X3DH bundle
|
|
|
|
//export GetContactCode
|
|
|
|
func GetContactCode(identityString *C.char) *C.char {
|
|
|
|
bundle, err := statusBackend.GetContactCode(C.GoString(identityString))
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(struct {
|
|
|
|
ContactCode string `json:"code"`
|
|
|
|
}{ContactCode: bundle})
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.CString(string(data))
|
|
|
|
}
|
|
|
|
|
2018-09-24 18:07:34 +00:00
|
|
|
//export ExtractIdentityFromContactCode
|
|
|
|
func ExtractIdentityFromContactCode(bundleString *C.char) *C.char {
|
|
|
|
bundle := C.GoString(bundleString)
|
|
|
|
|
|
|
|
identity, err := statusBackend.ExtractIdentityFromContactCode(bundle)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := statusBackend.ProcessContactCode(bundle); err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(struct {
|
|
|
|
Identity string `json:"identity"`
|
|
|
|
}{Identity: identity})
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.CString(string(data))
|
|
|
|
}
|
|
|
|
|
2018-11-06 08:05:32 +00:00
|
|
|
// ExtractGroupMembershipSignatures extract public keys from tuples of content/signature
|
|
|
|
//export ExtractGroupMembershipSignatures
|
|
|
|
func ExtractGroupMembershipSignatures(signaturePairsStr *C.char) *C.char {
|
|
|
|
var signaturePairs [][2]string
|
2018-09-27 13:07:32 +00:00
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(C.GoString(signaturePairsStr)), &signaturePairs); err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2018-11-06 08:05:32 +00:00
|
|
|
identities, err := statusBackend.ExtractGroupMembershipSignatures(signaturePairs)
|
|
|
|
if err != nil {
|
2018-09-27 13:07:32 +00:00
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2018-09-28 11:53:36 +00:00
|
|
|
data, err := json.Marshal(struct {
|
2018-11-06 08:05:32 +00:00
|
|
|
Identities []string `json:"identities"`
|
|
|
|
}{Identities: identities})
|
2018-09-28 11:53:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.CString(string(data))
|
2018-09-27 13:07:32 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 08:05:32 +00:00
|
|
|
// Sign signs a string containing group membership information
|
|
|
|
//export SignGroupMembership
|
|
|
|
func SignGroupMembership(content *C.char) *C.char {
|
|
|
|
signature, err := statusBackend.SignGroupMembership(C.GoString(content))
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2018-10-04 16:53:48 +00:00
|
|
|
|
2018-11-06 08:05:32 +00:00
|
|
|
data, err := json.Marshal(struct {
|
|
|
|
Signature string `json:"signature"`
|
|
|
|
}{Signature: signature})
|
|
|
|
if err != nil {
|
2018-10-04 16:53:48 +00:00
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2018-11-06 08:05:32 +00:00
|
|
|
return C.CString(string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnableInstallation enables an installation for multi-device sync.
|
|
|
|
//export EnableInstallation
|
|
|
|
func EnableInstallation(installationID *C.char) *C.char {
|
|
|
|
err := statusBackend.EnableInstallation(C.GoString(installationID))
|
2018-10-04 16:53:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(struct {
|
2018-11-06 08:05:32 +00:00
|
|
|
Response string `json:"response"`
|
|
|
|
}{Response: "ok"})
|
2018-10-04 16:53:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.CString(string(data))
|
|
|
|
}
|
|
|
|
|
2018-11-06 08:05:32 +00:00
|
|
|
// DisableInstallation disables an installation for multi-device sync.
|
|
|
|
//export DisableInstallation
|
|
|
|
func DisableInstallation(installationID *C.char) *C.char {
|
|
|
|
err := statusBackend.DisableInstallation(C.GoString(installationID))
|
2018-09-27 13:07:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(struct {
|
2018-11-06 08:05:32 +00:00
|
|
|
Response string `json:"response"`
|
|
|
|
}{Response: "ok"})
|
2018-09-27 13:07:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.CString(string(data))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
_, err := params.NewConfigFromJSON(C.GoString(configJSON))
|
2017-08-10 15:31:29 +00:00
|
|
|
|
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-06-19 07:49:24 +00:00
|
|
|
api.RunAsync(statusBackend.ResetChainData)
|
2018-02-09 13:37:56 +00:00
|
|
|
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 {
|
2018-12-20 08:31:17 +00:00
|
|
|
outputJSON, err := statusBackend.CallRPC(C.GoString(inputJSON))
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2017-05-28 13:57:30 +00:00
|
|
|
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 {
|
2018-12-20 08:31:17 +00:00
|
|
|
outputJSON, err := statusBackend.CallPrivateRPC(C.GoString(inputJSON))
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2018-04-16 08:01:37 +00:00
|
|
|
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 {
|
2019-01-18 09:01:14 +00:00
|
|
|
info, mnemonic, err := statusBackend.AccountManager().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{
|
2019-01-18 09:01:14 +00:00
|
|
|
Address: info.WalletAddress,
|
|
|
|
PubKey: info.WalletPubKey,
|
|
|
|
WalletAddress: info.WalletAddress,
|
|
|
|
WalletPubKey: info.WalletPubKey,
|
|
|
|
ChatAddress: info.ChatAddress,
|
|
|
|
ChatPubKey: info.ChatPubKey,
|
|
|
|
Mnemonic: mnemonic,
|
|
|
|
Error: errString,
|
2016-08-18 00:15:58 +00:00
|
|
|
}
|
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 {
|
2018-06-19 07:49:24 +00:00
|
|
|
address, pubKey, err := statusBackend.AccountManager().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 {
|
2019-01-18 09:01:14 +00:00
|
|
|
info, err := statusBackend.AccountManager().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{
|
2019-01-18 09:01:14 +00:00
|
|
|
Address: info.WalletAddress,
|
|
|
|
PubKey: info.WalletPubKey,
|
|
|
|
WalletAddress: info.WalletAddress,
|
|
|
|
WalletPubKey: info.WalletPubKey,
|
|
|
|
ChatAddress: info.ChatAddress,
|
|
|
|
ChatPubKey: info.ChatPubKey,
|
|
|
|
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 {
|
2018-06-19 07:49:24 +00:00
|
|
|
_, err := statusBackend.AccountManager().VerifyAccountPassword(C.GoString(keyStoreDir), C.GoString(address), C.GoString(password))
|
2017-05-16 12:09:52 +00:00
|
|
|
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 {
|
2019-01-18 09:01:14 +00:00
|
|
|
err := statusBackend.SelectAccount(C.GoString(address), C.GoString(address), C.GoString(password))
|
2017-05-16 12:09:52 +00:00
|
|
|
return makeJSONResponse(err)
|
2016-06-29 11:32:04 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 15:44:46 +00:00
|
|
|
// LoginWithKeycard initializes an account with a chat key and encryption key used for PFS.
|
|
|
|
// It purges all the previous identities from Whisper, and injects the key as shh identity.
|
|
|
|
//export LoginWithKeycard
|
|
|
|
func LoginWithKeycard(chatKeyData, encryptionKeyData *C.char) *C.char {
|
|
|
|
err := statusBackend.InjectChatAccount(C.GoString(chatKeyData), C.GoString(encryptionKeyData))
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-06-19 07:49:24 +00:00
|
|
|
err := statusBackend.Logout()
|
2017-05-16 12:09:52 +00:00
|
|
|
return makeJSONResponse(err)
|
2016-08-29 00:31:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
// SignMessage unmarshals rpc params {data, address, password} and passes
|
|
|
|
// them onto backend.SignMessage
|
|
|
|
//export SignMessage
|
|
|
|
func SignMessage(rpcParams *C.char) *C.char {
|
|
|
|
var params personal.SignParams
|
|
|
|
err := json.Unmarshal([]byte(C.GoString(rpcParams)), ¶ms)
|
2017-09-04 12:56:58 +00:00
|
|
|
if err != nil {
|
2018-08-16 11:37:53 +00:00
|
|
|
return C.CString(prepareJSONResponseWithCode(nil, err, codeFailedParseParams))
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
result, err := statusBackend.SignMessage(params)
|
|
|
|
return C.CString(prepareJSONResponse(result.String(), err))
|
2016-11-05 17:12:24 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
// Recover unmarshals rpc params {signDataString, signedData} and passes
|
|
|
|
// them onto backend.
|
|
|
|
//export Recover
|
|
|
|
func Recover(rpcParams *C.char) *C.char {
|
|
|
|
var params personal.RecoverParams
|
|
|
|
err := json.Unmarshal([]byte(C.GoString(rpcParams)), ¶ms)
|
2016-10-30 22:35:10 +00:00
|
|
|
if err != nil {
|
2018-08-16 11:37:53 +00:00
|
|
|
return C.CString(prepareJSONResponseWithCode(nil, err, codeFailedParseParams))
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
addr, err := statusBackend.Recover(params)
|
|
|
|
return C.CString(prepareJSONResponse(addr.String(), err))
|
2016-10-30 22:35:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
// SendTransaction converts RPC args and calls backend.SendTransaction
|
|
|
|
//export SendTransaction
|
|
|
|
func SendTransaction(txArgsJSON, password *C.char) *C.char {
|
|
|
|
var params transactions.SendTxArgs
|
|
|
|
err := json.Unmarshal([]byte(C.GoString(txArgsJSON)), ¶ms)
|
2017-09-04 12:56:58 +00:00
|
|
|
if err != nil {
|
2018-08-16 11:37:53 +00:00
|
|
|
return C.CString(prepareJSONResponseWithCode(nil, err, codeFailedParseParams))
|
2016-11-05 17:12:24 +00:00
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
hash, err := statusBackend.SendTransaction(params, C.GoString(password))
|
|
|
|
code := codeUnknown
|
|
|
|
if c, ok := errToCodeMap[err]; ok {
|
|
|
|
code = c
|
2017-09-04 12:56:58 +00:00
|
|
|
}
|
2018-08-16 11:37:53 +00:00
|
|
|
return C.CString(prepareJSONResponseWithCode(hash.String(), err, code))
|
2016-11-05 17:12:24 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 11:31:20 +00:00
|
|
|
// SendTransactionWithSignature converts RPC args and calls backend.SendTransactionWithSignature
|
|
|
|
//export SendTransactionWithSignature
|
|
|
|
func SendTransactionWithSignature(txArgsJSON, sigString *C.char) *C.char {
|
|
|
|
var params transactions.SendTxArgs
|
|
|
|
err := json.Unmarshal([]byte(C.GoString(txArgsJSON)), ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return C.CString(prepareJSONResponseWithCode(nil, err, codeFailedParseParams))
|
|
|
|
}
|
|
|
|
|
|
|
|
sig, err := hex.DecodeString(C.GoString(sigString))
|
|
|
|
if err != nil {
|
|
|
|
return C.CString(prepareJSONResponseWithCode(nil, err, codeFailedParseParams))
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := statusBackend.SendTransactionWithSignature(params, sig)
|
|
|
|
code := codeUnknown
|
|
|
|
if c, ok := errToCodeMap[err]; ok {
|
|
|
|
code = c
|
|
|
|
}
|
|
|
|
return C.CString(prepareJSONResponseWithCode(hash.String(), err, code))
|
|
|
|
}
|
|
|
|
|
2019-02-21 09:53:39 +00:00
|
|
|
// HashTransaction validate the transaction and returns new txArgs and the transaction hash.
|
|
|
|
//export HashTransaction
|
|
|
|
func HashTransaction(txArgsJSON *C.char) *C.char {
|
|
|
|
var params transactions.SendTxArgs
|
|
|
|
err := json.Unmarshal([]byte(C.GoString(txArgsJSON)), ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return C.CString(prepareJSONResponseWithCode(nil, err, codeFailedParseParams))
|
|
|
|
}
|
|
|
|
|
|
|
|
newTxArgs, hash, err := statusBackend.HashTransaction(params)
|
|
|
|
code := codeUnknown
|
|
|
|
if c, ok := errToCodeMap[err]; ok {
|
|
|
|
code = c
|
|
|
|
}
|
|
|
|
|
|
|
|
result := struct {
|
|
|
|
Transaction transactions.SendTxArgs `json:"transaction"`
|
|
|
|
Hash common.Hash `json:"hash"`
|
|
|
|
}{
|
|
|
|
Transaction: newTxArgs,
|
|
|
|
Hash: hash,
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.CString(prepareJSONResponseWithCode(result, err, code))
|
|
|
|
}
|
|
|
|
|
2018-11-06 06:26:12 +00:00
|
|
|
// SignTypedData unmarshall data into TypedData, validate it and signs with selected account,
|
|
|
|
// if password matches selected account.
|
|
|
|
//export SignTypedData
|
|
|
|
func SignTypedData(data, password *C.char) *C.char {
|
|
|
|
var typed typeddata.TypedData
|
|
|
|
err := json.Unmarshal([]byte(C.GoString(data)), &typed)
|
|
|
|
if err != nil {
|
|
|
|
return C.CString(prepareJSONResponseWithCode(nil, err, codeFailedParseParams))
|
|
|
|
}
|
|
|
|
if err := typed.Validate(); err != nil {
|
|
|
|
return C.CString(prepareJSONResponseWithCode(nil, err, codeFailedParseParams))
|
|
|
|
}
|
|
|
|
result, err := statusBackend.SignTypedData(typed, C.GoString(password))
|
|
|
|
return C.CString(prepareJSONResponse(result.String(), err))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-01-18 13:03:32 +00:00
|
|
|
// SendDataNotification sends push notifications by given tokens.
|
|
|
|
// dataPayloadJSON is a JSON string that looks like this:
|
|
|
|
// {
|
2019-03-01 13:37:13 +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
|
|
|
// }
|
|
|
|
//export SendDataNotification
|
|
|
|
func SendDataNotification(dataPayloadJSON, tokensArray *C.char) (outCBytes *C.char) {
|
2017-10-23 15:46:51 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
outBytes []byte
|
|
|
|
)
|
2017-10-12 14:31:39 +00:00
|
|
|
errString := ""
|
2017-10-23 15:46:51 +00:00
|
|
|
|
|
|
|
defer func() {
|
2019-01-18 13:03:32 +00:00
|
|
|
out := SendDataNotificationResult{
|
2017-10-23 15:46:51 +00:00
|
|
|
Status: err == nil,
|
|
|
|
Error: errString,
|
|
|
|
}
|
|
|
|
|
|
|
|
outBytes, err = json.Marshal(out)
|
|
|
|
if err != nil {
|
2019-01-18 13:03:32 +00:00
|
|
|
logger.Error("failed to marshal SendDataNotification 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
|
|
|
}
|
|
|
|
|
2019-01-18 13:03:32 +00:00
|
|
|
err = statusBackend.SendDataNotification(C.GoString(dataPayloadJSON), 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
|
|
|
|
2018-11-21 10:22:30 +00:00
|
|
|
// UpdateMailservers updates mail servers in status backend.
|
|
|
|
//export UpdateMailservers
|
|
|
|
func UpdateMailservers(data *C.char) *C.char {
|
|
|
|
var enodes []string
|
|
|
|
err := json.Unmarshal([]byte(C.GoString(data)), &enodes)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
err = statusBackend.UpdateMailservers(enodes)
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
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-06-19 07:49:24 +00:00
|
|
|
err := statusBackend.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) {
|
2018-06-19 07:49:24 +00:00
|
|
|
statusBackend.ConnectionChange(C.GoString(typ), expensive == 1)
|
2018-02-09 20:28:16 +00:00
|
|
|
}
|
2018-03-14 15:46:21 +00:00
|
|
|
|
|
|
|
// AppStateChange handles app state changes (background/foreground).
|
|
|
|
//export AppStateChange
|
|
|
|
func AppStateChange(state *C.char) {
|
2018-06-19 07:49:24 +00:00
|
|
|
statusBackend.AppStateChange(C.GoString(state))
|
2018-03-14 15:46:21 +00:00
|
|
|
}
|
2018-06-27 23:16:27 +00:00
|
|
|
|
2018-07-24 10:05:21 +00:00
|
|
|
// SetSignalEventCallback setup geth callback to notify about new signal
|
2018-06-27 23:16:27 +00:00
|
|
|
//export SetSignalEventCallback
|
|
|
|
func SetSignalEventCallback(cb unsafe.Pointer) {
|
|
|
|
signal.SetSignalEventCallback(cb)
|
|
|
|
}
|
2019-03-04 14:33:48 +00:00
|
|
|
|
|
|
|
// ExportNodeLogs reads current node log and returns content to a caller.
|
|
|
|
//export ExportNodeLogs
|
|
|
|
func ExportNodeLogs() *C.char {
|
|
|
|
node := statusBackend.StatusNode()
|
|
|
|
if node == nil {
|
|
|
|
return makeJSONResponse(errors.New("node is not running"))
|
|
|
|
}
|
|
|
|
config := node.Config()
|
|
|
|
if config == nil {
|
|
|
|
return makeJSONResponse(errors.New("config and log file are not available"))
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(exportlogs.ExportFromBaseFile(config.LogFile))
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(fmt.Errorf("error marshalling to json: %v", err))
|
|
|
|
}
|
|
|
|
return C.CString(string(data))
|
|
|
|
}
|