2019-02-01 17:02:52 +00:00
|
|
|
package statusgo
|
|
|
|
|
|
|
|
import (
|
2019-02-15 11:31:20 +00:00
|
|
|
"encoding/hex"
|
2019-02-01 17:02:52 +00:00
|
|
|
"encoding/json"
|
2019-03-04 14:33:48 +00:00
|
|
|
"errors"
|
2019-02-01 17:02:52 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"unsafe"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
validator "gopkg.in/go-playground/validator.v9"
|
|
|
|
|
2019-02-01 17:02:52 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2019-02-01 17:02:52 +00:00
|
|
|
"github.com/status-im/status-go/api"
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2019-03-04 14:33:48 +00:00
|
|
|
"github.com/status-im/status-go/exportlogs"
|
2019-12-16 13:55:02 +00:00
|
|
|
"github.com/status-im/status-go/extkeys"
|
2019-08-20 15:38:40 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts"
|
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2019-02-01 17:02:52 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/profiling"
|
2019-11-21 16:19:22 +00:00
|
|
|
protocol "github.com/status-im/status-go/protocol"
|
2019-02-01 17:02:52 +00:00
|
|
|
"github.com/status-im/status-go/services/personal"
|
2019-03-04 13:45:27 +00:00
|
|
|
"github.com/status-im/status-go/services/typeddata"
|
2019-02-01 17:02:52 +00:00
|
|
|
"github.com/status-im/status-go/signal"
|
|
|
|
"github.com/status-im/status-go/transactions"
|
|
|
|
)
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
var statusBackend = api.NewGethStatusBackend()
|
2019-02-01 17:02:52 +00:00
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
// OpenAccounts opens database and returns accounts list.
|
|
|
|
func OpenAccounts(datadir string) string {
|
|
|
|
statusBackend.UpdateRootDataDir(datadir)
|
|
|
|
err := statusBackend.OpenAccounts()
|
2019-02-01 17:02:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2019-08-20 15:38:40 +00:00
|
|
|
accs, err := statusBackend.GetAccounts()
|
2019-02-01 17:02:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2019-08-20 15:38:40 +00:00
|
|
|
data, err := json.Marshal(accs)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
return string(data)
|
2019-02-01 17:02:52 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
// GenerateConfig for status node.
|
|
|
|
func GenerateConfig(datadir string, networkID int) string {
|
|
|
|
config, err := params.NewNodeConfig(datadir, uint64(networkID))
|
2019-02-01 17:02:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
outBytes, err := json.Marshal(config)
|
|
|
|
if err != nil {
|
2019-02-01 17:02:52 +00:00
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
return string(outBytes)
|
2019-02-01 17:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExtractGroupMembershipSignatures extract public keys from tuples of content/signature.
|
|
|
|
func ExtractGroupMembershipSignatures(signaturePairsStr string) string {
|
|
|
|
var signaturePairs [][2]string
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(signaturePairsStr), &signaturePairs); err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
identities, err := statusBackend.ExtractGroupMembershipSignatures(signaturePairs)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(struct {
|
|
|
|
Identities []string `json:"identities"`
|
|
|
|
}{Identities: identities})
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignGroupMembership signs a string containing group membership information.
|
|
|
|
func SignGroupMembership(content string) string {
|
|
|
|
signature, err := statusBackend.SignGroupMembership(content)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(struct {
|
|
|
|
Signature string `json:"signature"`
|
|
|
|
}{Signature: signature})
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
2019-07-01 13:15:13 +00:00
|
|
|
// EnableInstallation enables an installation for multi-device sync.
|
|
|
|
func EnableInstallation(installationID string) string {
|
|
|
|
err := statusBackend.EnableInstallation(installationID)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(struct {
|
|
|
|
Response string `json:"response"`
|
|
|
|
}{Response: "ok"})
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableInstallation disables an installation for multi-device sync.
|
|
|
|
func DisableInstallation(installationID string) string {
|
|
|
|
err := statusBackend.DisableInstallation(installationID)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(struct {
|
|
|
|
Response string `json:"response"`
|
|
|
|
}{Response: "ok"})
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:02:52 +00:00
|
|
|
// ValidateNodeConfig validates config for the Status node.
|
|
|
|
func ValidateNodeConfig(configJSON string) string {
|
|
|
|
var resp APIDetailedResponse
|
|
|
|
|
|
|
|
_, err := params.NewConfigFromJSON(configJSON)
|
|
|
|
|
|
|
|
// Convert errors to APIDetailedResponse
|
|
|
|
switch err := err.(type) {
|
|
|
|
case validator.ValidationErrors:
|
|
|
|
resp = APIDetailedResponse{
|
|
|
|
Message: "validation: validation failed",
|
|
|
|
FieldErrors: make([]APIFieldError, len(err)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, ve := range err {
|
|
|
|
resp.FieldErrors[i] = APIFieldError{
|
|
|
|
Parameter: ve.Namespace(),
|
|
|
|
Errors: []APIError{
|
|
|
|
{
|
|
|
|
Message: fmt.Sprintf("field validation failed on the '%s' tag", ve.Tag()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case error:
|
|
|
|
resp = APIDetailedResponse{
|
|
|
|
Message: fmt.Sprintf("validation: %s", err.Error()),
|
|
|
|
}
|
|
|
|
case nil:
|
|
|
|
resp = APIDetailedResponse{
|
|
|
|
Status: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
respJSON, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(respJSON)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetChainData removes chain data from data directory.
|
|
|
|
func ResetChainData() string {
|
|
|
|
api.RunAsync(statusBackend.ResetChainData)
|
|
|
|
return makeJSONResponse(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CallRPC calls public APIs via RPC.
|
|
|
|
func CallRPC(inputJSON string) string {
|
|
|
|
resp, err := statusBackend.CallRPC(inputJSON)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
// CallPrivateRPC calls both public and private APIs via RPC.
|
|
|
|
func CallPrivateRPC(inputJSON string) string {
|
|
|
|
resp, err := statusBackend.CallPrivateRPC(inputJSON)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateAccount is equivalent to creating an account from the command line,
|
|
|
|
// just modified to handle the function arg passing.
|
|
|
|
func CreateAccount(password string) string {
|
|
|
|
info, mnemonic, err := statusBackend.AccountManager().CreateAccount(password)
|
|
|
|
errString := ""
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
errString = err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
out := AccountInfo{
|
|
|
|
Address: info.WalletAddress,
|
|
|
|
PubKey: info.WalletPubKey,
|
|
|
|
WalletAddress: info.WalletAddress,
|
|
|
|
WalletPubKey: info.WalletPubKey,
|
|
|
|
ChatAddress: info.ChatAddress,
|
|
|
|
ChatPubKey: info.ChatPubKey,
|
|
|
|
Mnemonic: mnemonic,
|
|
|
|
Error: errString,
|
|
|
|
}
|
|
|
|
outBytes, _ := json.Marshal(out)
|
|
|
|
return string(outBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RecoverAccount re-creates master key using given details.
|
|
|
|
func RecoverAccount(password, mnemonic string) string {
|
|
|
|
info, err := statusBackend.AccountManager().RecoverAccount(password, mnemonic)
|
|
|
|
|
|
|
|
errString := ""
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
errString = err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
out := AccountInfo{
|
|
|
|
Address: info.WalletAddress,
|
|
|
|
PubKey: info.WalletPubKey,
|
|
|
|
WalletAddress: info.WalletAddress,
|
|
|
|
WalletPubKey: info.WalletPubKey,
|
|
|
|
ChatAddress: info.ChatAddress,
|
|
|
|
ChatPubKey: info.ChatPubKey,
|
|
|
|
Mnemonic: mnemonic,
|
|
|
|
Error: errString,
|
|
|
|
}
|
|
|
|
outBytes, _ := json.Marshal(out)
|
|
|
|
return string(outBytes)
|
|
|
|
}
|
|
|
|
|
2019-06-26 22:28:16 +00:00
|
|
|
// StartOnboarding initialize the onboarding with n random accounts
|
|
|
|
func StartOnboarding(n, mnemonicPhraseLength int) string {
|
|
|
|
out := struct {
|
|
|
|
Accounts []OnboardingAccount `json:"accounts"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}{
|
|
|
|
Accounts: make([]OnboardingAccount, 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
accounts, err := statusBackend.AccountManager().StartOnboarding(n, mnemonicPhraseLength)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
out.Error = err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
for _, account := range accounts {
|
|
|
|
out.Accounts = append(out.Accounts, OnboardingAccount{
|
|
|
|
ID: account.ID,
|
|
|
|
Address: account.Info.WalletAddress,
|
|
|
|
PubKey: account.Info.WalletPubKey,
|
|
|
|
WalletAddress: account.Info.WalletAddress,
|
|
|
|
WalletPubKey: account.Info.WalletPubKey,
|
|
|
|
ChatAddress: account.Info.ChatAddress,
|
|
|
|
ChatPubKey: account.Info.ChatPubKey,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
outBytes, _ := json.Marshal(out)
|
|
|
|
return string(outBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
//ImportOnboardingAccount re-creates and imports an account created during onboarding.
|
|
|
|
func ImportOnboardingAccount(id, password string) string {
|
|
|
|
info, mnemonic, err := statusBackend.AccountManager().ImportOnboardingAccount(id, password)
|
|
|
|
|
|
|
|
errString := ""
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
errString = err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
out := AccountInfo{
|
|
|
|
Address: info.WalletAddress,
|
|
|
|
PubKey: info.WalletPubKey,
|
|
|
|
WalletAddress: info.WalletAddress,
|
|
|
|
WalletPubKey: info.WalletPubKey,
|
|
|
|
ChatAddress: info.ChatAddress,
|
|
|
|
ChatPubKey: info.ChatPubKey,
|
|
|
|
Mnemonic: mnemonic,
|
|
|
|
Error: errString,
|
|
|
|
}
|
|
|
|
outBytes, _ := json.Marshal(out)
|
|
|
|
return string(outBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveOnboarding resets the current onboarding removing from memory all the generated keys.
|
|
|
|
func RemoveOnboarding() {
|
|
|
|
statusBackend.AccountManager().RemoveOnboarding()
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:02:52 +00:00
|
|
|
// VerifyAccountPassword verifies account password.
|
|
|
|
func VerifyAccountPassword(keyStoreDir, address, password string) string {
|
|
|
|
_, err := statusBackend.AccountManager().VerifyAccountPassword(keyStoreDir, address, password)
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2019-08-20 15:38:40 +00:00
|
|
|
func Login(accountData, password string) string {
|
|
|
|
var account multiaccounts.Account
|
|
|
|
err := json.Unmarshal([]byte(accountData), &account)
|
2019-07-26 14:45:10 +00:00
|
|
|
if err != nil {
|
2019-08-20 15:38:40 +00:00
|
|
|
return makeJSONResponse(err)
|
2019-07-26 14:45:10 +00:00
|
|
|
}
|
2019-08-20 15:38:40 +00:00
|
|
|
api.RunAsync(func() error {
|
2019-12-05 08:00:57 +00:00
|
|
|
log.Debug("start a node with account", "key-uid", account.KeyUID)
|
2019-08-20 15:38:40 +00:00
|
|
|
err := statusBackend.StartNodeWithAccount(account, password)
|
|
|
|
if err != nil {
|
2019-12-05 08:00:57 +00:00
|
|
|
log.Error("failed to start a node", "key-uid", account.KeyUID, "error", err)
|
2019-08-20 15:38:40 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-12-05 08:00:57 +00:00
|
|
|
log.Debug("started a node with", "key-uid", account.KeyUID)
|
2019-08-20 15:38:40 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return makeJSONResponse(nil)
|
|
|
|
}
|
2019-07-26 14:45:10 +00:00
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
// SaveAccountAndLogin saves account in status-go database..
|
2019-12-27 09:58:25 +00:00
|
|
|
func SaveAccountAndLogin(accountData, password, settingsJSON, configJSON, subaccountData string) string {
|
2019-08-20 15:38:40 +00:00
|
|
|
var account multiaccounts.Account
|
|
|
|
err := json.Unmarshal([]byte(accountData), &account)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2019-12-27 09:58:25 +00:00
|
|
|
var settings accounts.Settings
|
|
|
|
err = json.Unmarshal([]byte(settingsJSON), &settings)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2019-08-20 15:38:40 +00:00
|
|
|
var conf params.NodeConfig
|
|
|
|
err = json.Unmarshal([]byte(configJSON), &conf)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
var subaccs []accounts.Account
|
|
|
|
err = json.Unmarshal([]byte(subaccountData), &subaccs)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
api.RunAsync(func() error {
|
2019-12-05 08:00:57 +00:00
|
|
|
log.Debug("starting a node, and saving account with configuration", "key-uid", account.KeyUID)
|
2019-12-27 09:58:25 +00:00
|
|
|
err := statusBackend.StartNodeWithAccountAndConfig(account, password, settings, &conf, subaccs)
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
2019-12-05 08:00:57 +00:00
|
|
|
log.Error("failed to start node and save account", "key-uid", account.KeyUID, "error", err)
|
2019-08-20 15:38:40 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-12-05 08:00:57 +00:00
|
|
|
log.Debug("started a node, and saved account", "key-uid", account.KeyUID)
|
2019-08-20 15:38:40 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return makeJSONResponse(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitKeystore initialize keystore before doing any operations with keys.
|
|
|
|
func InitKeystore(keydir string) string {
|
|
|
|
err := statusBackend.AccountManager().InitKeystore(keydir)
|
2019-02-01 17:02:52 +00:00
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2019-09-02 19:03:15 +00:00
|
|
|
// SaveAccountAndLoginWithKeycard saves account in status-go database..
|
2019-12-27 09:58:25 +00:00
|
|
|
func SaveAccountAndLoginWithKeycard(accountData, password, settingsJSON, configJSON, subaccountData string, keyHex string) string {
|
2019-09-02 19:03:15 +00:00
|
|
|
var account multiaccounts.Account
|
|
|
|
err := json.Unmarshal([]byte(accountData), &account)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2019-12-27 09:58:25 +00:00
|
|
|
var settings accounts.Settings
|
|
|
|
err = json.Unmarshal([]byte(settingsJSON), &settings)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2019-09-02 19:03:15 +00:00
|
|
|
var conf params.NodeConfig
|
|
|
|
err = json.Unmarshal([]byte(configJSON), &conf)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2019-12-18 15:09:04 +00:00
|
|
|
var subaccs []accounts.Account
|
|
|
|
err = json.Unmarshal([]byte(subaccountData), &subaccs)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2019-09-02 19:03:15 +00:00
|
|
|
api.RunAsync(func() error {
|
2019-12-05 08:00:57 +00:00
|
|
|
log.Debug("starting a node, and saving account with configuration", "key-uid", account.KeyUID)
|
2019-12-27 09:58:25 +00:00
|
|
|
err := statusBackend.SaveAccountAndStartNodeWithKey(account, password, settings, &conf, subaccs, keyHex)
|
2019-09-02 19:03:15 +00:00
|
|
|
if err != nil {
|
2019-12-05 08:00:57 +00:00
|
|
|
log.Error("failed to start node and save account", "key-uid", account.KeyUID, "error", err)
|
2019-09-02 19:03:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-12-05 08:00:57 +00:00
|
|
|
log.Debug("started a node, and saved account", "key-uid", account.KeyUID)
|
2019-09-02 19:03:15 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return makeJSONResponse(nil)
|
|
|
|
}
|
|
|
|
|
2019-02-11 20:20:59 +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.
|
2019-09-02 19:03:15 +00:00
|
|
|
func LoginWithKeycard(accountData, password, keyHex string) string {
|
|
|
|
var account multiaccounts.Account
|
|
|
|
err := json.Unmarshal([]byte(accountData), &account)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
api.RunAsync(func() error {
|
2019-12-05 08:00:57 +00:00
|
|
|
log.Debug("start a node with account", "key-uid", account.KeyUID)
|
2019-09-02 19:03:15 +00:00
|
|
|
err := statusBackend.StartNodeWithKey(account, password, keyHex)
|
|
|
|
if err != nil {
|
2019-12-05 08:00:57 +00:00
|
|
|
log.Error("failed to start a node", "key-uid", account.KeyUID, "error", err)
|
2019-09-02 19:03:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-12-05 08:00:57 +00:00
|
|
|
log.Debug("started a node with", "key-uid", account.KeyUID)
|
2019-09-02 19:03:15 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return makeJSONResponse(nil)
|
2019-02-11 20:20:59 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 17:02:52 +00:00
|
|
|
// Logout is equivalent to clearing whisper identities.
|
|
|
|
func Logout() string {
|
|
|
|
err := statusBackend.Logout()
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
makeJSONResponse(err)
|
|
|
|
}
|
2019-08-21 11:53:33 +00:00
|
|
|
return makeJSONResponse(statusBackend.StopNode())
|
2019-02-01 17:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SignMessage unmarshals rpc params {data, address, password} and
|
|
|
|
// passes them onto backend.SignMessage.
|
|
|
|
func SignMessage(rpcParams string) string {
|
|
|
|
var params personal.SignParams
|
|
|
|
err := json.Unmarshal([]byte(rpcParams), ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return prepareJSONResponseWithCode(nil, err, codeFailedParseParams)
|
|
|
|
}
|
|
|
|
result, err := statusBackend.SignMessage(params)
|
|
|
|
return prepareJSONResponse(result.String(), err)
|
|
|
|
}
|
|
|
|
|
2019-03-04 13:45:27 +00:00
|
|
|
// SignTypedData unmarshall data into TypedData, validate it and signs with selected account,
|
|
|
|
// if password matches selected account.
|
|
|
|
//export SignTypedData
|
2019-07-26 14:45:10 +00:00
|
|
|
func SignTypedData(data, address, password string) string {
|
2019-03-04 13:45:27 +00:00
|
|
|
var typed typeddata.TypedData
|
|
|
|
err := json.Unmarshal([]byte(data), &typed)
|
|
|
|
if err != nil {
|
|
|
|
return prepareJSONResponseWithCode(nil, err, codeFailedParseParams)
|
|
|
|
}
|
|
|
|
if err := typed.Validate(); err != nil {
|
|
|
|
return prepareJSONResponseWithCode(nil, err, codeFailedParseParams)
|
|
|
|
}
|
2019-07-26 14:45:10 +00:00
|
|
|
result, err := statusBackend.SignTypedData(typed, address, password)
|
2019-03-04 13:45:27 +00:00
|
|
|
return prepareJSONResponse(result.String(), err)
|
|
|
|
}
|
|
|
|
|
2019-03-28 14:56:21 +00:00
|
|
|
// HashTypedData unmarshalls data into TypedData, validates it and hashes it.
|
|
|
|
func HashTypedData(data string) string {
|
|
|
|
var typed typeddata.TypedData
|
|
|
|
err := json.Unmarshal([]byte(data), &typed)
|
|
|
|
if err != nil {
|
|
|
|
return prepareJSONResponseWithCode(nil, err, codeFailedParseParams)
|
|
|
|
}
|
|
|
|
if err := typed.Validate(); err != nil {
|
|
|
|
return prepareJSONResponseWithCode(nil, err, codeFailedParseParams)
|
|
|
|
}
|
|
|
|
result, err := statusBackend.HashTypedData(typed)
|
|
|
|
return prepareJSONResponse(result.String(), err)
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:02:52 +00:00
|
|
|
// Recover unmarshals rpc params {signDataString, signedData} and passes
|
|
|
|
// them onto backend.
|
|
|
|
func Recover(rpcParams string) string {
|
|
|
|
var params personal.RecoverParams
|
|
|
|
err := json.Unmarshal([]byte(rpcParams), ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return prepareJSONResponseWithCode(nil, err, codeFailedParseParams)
|
|
|
|
}
|
|
|
|
addr, err := statusBackend.Recover(params)
|
|
|
|
return prepareJSONResponse(addr.String(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendTransaction converts RPC args and calls backend.SendTransaction.
|
|
|
|
func SendTransaction(txArgsJSON, password string) string {
|
|
|
|
var params transactions.SendTxArgs
|
|
|
|
err := json.Unmarshal([]byte(txArgsJSON), ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return prepareJSONResponseWithCode(nil, err, codeFailedParseParams)
|
|
|
|
}
|
|
|
|
hash, err := statusBackend.SendTransaction(params, password)
|
|
|
|
code := codeUnknown
|
|
|
|
if c, ok := errToCodeMap[err]; ok {
|
|
|
|
code = c
|
|
|
|
}
|
|
|
|
return prepareJSONResponseWithCode(hash.String(), err, code)
|
|
|
|
}
|
|
|
|
|
2019-02-15 11:31:20 +00:00
|
|
|
// SendTransactionWithSignature converts RPC args and calls backend.SendTransactionWithSignature
|
|
|
|
func SendTransactionWithSignature(txArgsJSON, sigString string) string {
|
|
|
|
var params transactions.SendTxArgs
|
|
|
|
err := json.Unmarshal([]byte(txArgsJSON), ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return prepareJSONResponseWithCode(nil, err, codeFailedParseParams)
|
|
|
|
}
|
|
|
|
|
|
|
|
sig, err := hex.DecodeString(sigString)
|
|
|
|
if err != nil {
|
|
|
|
return prepareJSONResponseWithCode(nil, err, codeFailedParseParams)
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := statusBackend.SendTransactionWithSignature(params, sig)
|
|
|
|
code := codeUnknown
|
|
|
|
if c, ok := errToCodeMap[err]; ok {
|
|
|
|
code = c
|
|
|
|
}
|
|
|
|
return 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.
|
|
|
|
func HashTransaction(txArgsJSON string) string {
|
|
|
|
var params transactions.SendTxArgs
|
|
|
|
err := json.Unmarshal([]byte(txArgsJSON), ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return 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"`
|
2019-11-23 17:57:05 +00:00
|
|
|
Hash types.Hash `json:"hash"`
|
2019-02-21 09:53:39 +00:00
|
|
|
}{
|
|
|
|
Transaction: newTxArgs,
|
|
|
|
Hash: hash,
|
|
|
|
}
|
|
|
|
|
|
|
|
return prepareJSONResponseWithCode(result, err, code)
|
|
|
|
}
|
|
|
|
|
2019-03-21 10:48:47 +00:00
|
|
|
// HashMessage calculates the hash of a message to be safely signed by the keycard
|
|
|
|
// The hash is calulcated as
|
|
|
|
// keccak256("\x19Ethereum Signed Message:\n"${message length}${message}).
|
|
|
|
// This gives context to the signed message and prevents signing of transactions.
|
2019-04-29 23:26:41 +00:00
|
|
|
func HashMessage(message string) string {
|
|
|
|
hash, err := api.HashMessage(message)
|
|
|
|
code := codeUnknown
|
|
|
|
if c, ok := errToCodeMap[err]; ok {
|
|
|
|
code = c
|
2019-03-21 10:48:47 +00:00
|
|
|
}
|
2019-04-29 23:26:41 +00:00
|
|
|
return prepareJSONResponseWithCode(fmt.Sprintf("0x%x", hash), err, code)
|
2019-03-21 10:48:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-01 17:02:52 +00:00
|
|
|
// StartCPUProfile runs pprof for CPU.
|
|
|
|
func StartCPUProfile(dataDir string) string {
|
|
|
|
err := profiling.StartCPUProfile(dataDir)
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopCPUProfiling stops pprof for cpu.
|
|
|
|
func StopCPUProfiling() string { //nolint: deadcode
|
|
|
|
err := profiling.StopCPUProfile()
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//WriteHeapProfile starts pprof for heap
|
|
|
|
func WriteHeapProfile(dataDir string) string { //nolint: deadcode
|
|
|
|
err := profiling.WriteHeapFile(dataDir)
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeJSONResponse(err error) string {
|
|
|
|
errString := ""
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
errString = err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
out := APIResponse{
|
|
|
|
Error: errString,
|
|
|
|
}
|
|
|
|
outBytes, _ := json.Marshal(out)
|
|
|
|
|
|
|
|
return string(outBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateMailservers updates mail servers in status backend.
|
|
|
|
//export UpdateMailservers
|
|
|
|
func UpdateMailservers(data string) string {
|
|
|
|
var enodes []string
|
|
|
|
err := json.Unmarshal([]byte(data), &enodes)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
err = statusBackend.UpdateMailservers(enodes)
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
2019-03-29 10:20:24 +00:00
|
|
|
// GetNodesFromContract returns a list of nodes from a given contract
|
|
|
|
//export GetNodesFromContract
|
|
|
|
func GetNodesFromContract(rpcEndpoint string, contractAddress string) string {
|
|
|
|
nodes, err := statusBackend.GetNodesFromContract(
|
|
|
|
rpcEndpoint,
|
|
|
|
contractAddress,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(struct {
|
|
|
|
Nodes []string `json:"result"`
|
|
|
|
}{Nodes: nodes})
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:02:52 +00:00
|
|
|
// AddPeer adds an enode as a peer.
|
|
|
|
func AddPeer(enode string) string {
|
|
|
|
err := statusBackend.StatusNode().AddPeer(enode)
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectionChange handles network state changes as reported
|
|
|
|
// by ReactNative (see https://facebook.github.io/react-native/docs/netinfo.html)
|
|
|
|
func ConnectionChange(typ string, expensive int) {
|
|
|
|
statusBackend.ConnectionChange(typ, expensive == 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppStateChange handles app state changes (background/foreground).
|
|
|
|
func AppStateChange(state string) {
|
|
|
|
statusBackend.AppStateChange(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMobileSignalHandler setup geth callback to notify about new signal
|
|
|
|
// used for gomobile builds
|
|
|
|
func SetMobileSignalHandler(handler SignalHandler) {
|
|
|
|
signal.SetMobileSignalHandler(func(data []byte) {
|
|
|
|
if len(data) > 0 {
|
|
|
|
handler.HandleSignal(string(data))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSignalEventCallback setup geth callback to notify about new signal
|
|
|
|
func SetSignalEventCallback(cb unsafe.Pointer) {
|
|
|
|
signal.SetSignalEventCallback(cb)
|
|
|
|
}
|
2019-02-12 11:07:13 +00:00
|
|
|
|
2019-03-04 14:33:48 +00:00
|
|
|
// ExportNodeLogs reads current node log and returns content to a caller.
|
|
|
|
//export ExportNodeLogs
|
|
|
|
func ExportNodeLogs() string {
|
|
|
|
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 string(data)
|
|
|
|
}
|
2019-03-21 12:02:16 +00:00
|
|
|
|
|
|
|
// ChaosModeUpdate sets the Chaos Mode on or off.
|
|
|
|
func ChaosModeUpdate(on bool) string {
|
|
|
|
node := statusBackend.StatusNode()
|
|
|
|
if node == nil {
|
|
|
|
return makeJSONResponse(errors.New("node is not running"))
|
|
|
|
}
|
|
|
|
|
|
|
|
err := node.ChaosModeCheckRPCClientsUpstreamURL(on)
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
2019-04-18 13:52:08 +00:00
|
|
|
|
|
|
|
// SignHash exposes vanilla ECDSA signing required for Swarm messages
|
|
|
|
func SignHash(hexEncodedHash string) string {
|
|
|
|
hexEncodedSignature, err := statusBackend.SignHash(hexEncodedHash)
|
|
|
|
if err != nil {
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hexEncodedSignature
|
|
|
|
}
|
2019-09-26 07:01:17 +00:00
|
|
|
|
|
|
|
func GenerateAlias(pk string) string {
|
|
|
|
// We ignore any error, empty string is considered an error
|
|
|
|
name, _ := protocol.GenerateAlias(pk)
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
func Identicon(pk string) string {
|
|
|
|
// We ignore any error, empty string is considered an error
|
|
|
|
identicon, _ := protocol.Identicon(pk)
|
|
|
|
return identicon
|
|
|
|
}
|
2019-12-16 13:55:02 +00:00
|
|
|
|
|
|
|
func ValidateMnemonic(mnemonic string) string {
|
|
|
|
m := extkeys.NewMnemonic()
|
|
|
|
err := m.ValidateMnemonic(mnemonic, extkeys.Language(0))
|
|
|
|
return makeJSONResponse(err)
|
|
|
|
}
|