status-go/cmd/statusd/library.go

293 lines
6.8 KiB
Go
Raw Normal View History

package main
import "C"
import (
"encoding/json"
"fmt"
"os"
"github.com/status-im/status-go/geth"
"github.com/status-im/status-go/geth/jail"
"github.com/status-im/status-go/geth/params"
)
2016-06-29 11:32:04 +00:00
//export CreateAccount
func CreateAccount(password *C.char) *C.char {
2016-06-20 01:46:13 +00:00
// This is equivalent to creating an account from the command line,
// just modified to handle the function arg passing
address, pubKey, mnemonic, err := geth.CreateAccount(C.GoString(password))
2016-06-30 13:23:07 +00:00
errString := ""
2016-06-30 13:23:07 +00:00
if err != nil {
fmt.Fprintln(os.Stderr, err)
errString = err.Error()
}
out := geth.AccountInfo{
Address: address,
PubKey: pubKey,
Mnemonic: mnemonic,
Error: errString,
}
outBytes, _ := json.Marshal(&out)
return C.CString(string(outBytes))
}
2016-08-23 21:32:04 +00:00
//export CreateChildAccount
func CreateChildAccount(parentAddress, password *C.char) *C.char {
address, pubKey, err := geth.CreateChildAccount(C.GoString(parentAddress), C.GoString(password))
2016-08-23 21:32:04 +00:00
errString := ""
2016-08-23 21:32:04 +00:00
if err != nil {
fmt.Fprintln(os.Stderr, err)
errString = err.Error()
}
out := geth.AccountInfo{
2016-08-23 21:32:04 +00:00
Address: address,
PubKey: pubKey,
Error: errString,
}
outBytes, _ := json.Marshal(&out)
return C.CString(string(outBytes))
}
//export RecoverAccount
func RecoverAccount(password, mnemonic *C.char) *C.char {
address, pubKey, err := geth.RecoverAccount(C.GoString(password), C.GoString(mnemonic))
errString := ""
if err != nil {
fmt.Fprintln(os.Stderr, err)
errString = err.Error()
}
out := geth.AccountInfo{
Address: address,
PubKey: pubKey,
Mnemonic: C.GoString(mnemonic),
Error: errString,
}
outBytes, _ := json.Marshal(&out)
2016-06-30 13:23:07 +00:00
return C.CString(string(outBytes))
}
2016-06-20 15:21:45 +00:00
//export VerifyAccountPassword
func VerifyAccountPassword(keyStoreDir, address, password *C.char) *C.char {
_, err := geth.VerifyAccountPassword(C.GoString(keyStoreDir), C.GoString(address), C.GoString(password))
return makeJSONErrorResponse(err)
}
2016-06-29 11:32:04 +00:00
//export Login
func Login(address, password *C.char) *C.char {
// 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
err := geth.SelectAccount(C.GoString(address), C.GoString(password))
return makeJSONErrorResponse(err)
2016-06-29 11:32:04 +00:00
}
2016-08-29 00:31:16 +00:00
//export Logout
func Logout() *C.char {
// This is equivalent to clearing whisper identities
err := geth.Logout()
return makeJSONErrorResponse(err)
2016-08-29 00:31:16 +00:00
}
//export CompleteTransaction
func CompleteTransaction(id, password *C.char) *C.char {
txHash, err := geth.CompleteTransaction(C.GoString(id), C.GoString(password))
errString := ""
if err != nil {
fmt.Fprintln(os.Stderr, err)
errString = err.Error()
}
out := geth.CompleteTransactionResult{
ID: C.GoString(id),
2016-08-09 16:41:42 +00:00
Hash: txHash.Hex(),
Error: errString,
}
outBytes, _ := json.Marshal(&out)
return C.CString(string(outBytes))
}
//export CompleteTransactions
func CompleteTransactions(ids, password *C.char) *C.char {
out := geth.CompleteTransactionsResult{}
out.Results = make(map[string]geth.CompleteTransactionResult)
results := geth.CompleteTransactions(C.GoString(ids), C.GoString(password))
for txID, result := range results {
txResult := geth.CompleteTransactionResult{
ID: txID,
Hash: result.Hash.Hex(),
}
if result.Error != nil {
txResult.Error = result.Error.Error()
}
out.Results[txID] = txResult
}
outBytes, _ := json.Marshal(&out)
return C.CString(string(outBytes))
}
//export DiscardTransaction
func DiscardTransaction(id *C.char) *C.char {
err := geth.DiscardTransaction(C.GoString(id))
errString := ""
if err != nil {
fmt.Fprintln(os.Stderr, err)
errString = err.Error()
}
out := geth.DiscardTransactionResult{
ID: C.GoString(id),
Error: errString,
}
outBytes, _ := json.Marshal(&out)
return C.CString(string(outBytes))
}
//export DiscardTransactions
func DiscardTransactions(ids *C.char) *C.char {
out := geth.DiscardTransactionsResult{}
out.Results = make(map[string]geth.DiscardTransactionResult)
results := geth.DiscardTransactions(C.GoString(ids))
for txID, result := range results {
txResult := geth.DiscardTransactionResult{
ID: txID,
}
if result.Error != nil {
txResult.Error = result.Error.Error()
}
out.Results[txID] = txResult
}
outBytes, _ := json.Marshal(&out)
return C.CString(string(outBytes))
}
//export GenerateConfig
func GenerateConfig(datadir *C.char, networkID C.int, devMode C.int) *C.char {
config, err := params.NewNodeConfig(C.GoString(datadir), uint64(networkID), devMode == 1)
if err != nil {
return makeJSONErrorResponse(err)
}
outBytes, err := json.Marshal(&config)
if err != nil {
return makeJSONErrorResponse(err)
}
return C.CString(string(outBytes))
}
2016-06-29 11:32:04 +00:00
//export StartNode
func StartNode(configJSON *C.char) *C.char {
config, err := params.LoadNodeConfig(C.GoString(configJSON))
if err != nil {
return makeJSONErrorResponse(err)
}
2016-06-30 13:23:07 +00:00
err = geth.CreateAndRunNode(config)
2017-01-23 06:25:24 +00:00
return makeJSONErrorResponse(err)
}
2016-06-30 13:23:07 +00:00
//export StopNode
func StopNode() *C.char {
err := geth.NodeManagerInstance().StopNode()
return makeJSONErrorResponse(err)
}
//export ResumeNode
func ResumeNode() *C.char {
err := geth.NodeManagerInstance().ResumeNode()
return makeJSONErrorResponse(err)
}
2017-01-23 06:25:24 +00:00
//export ResetChainData
func ResetChainData() *C.char {
err := geth.NodeManagerInstance().ResetChainData()
return makeJSONErrorResponse(err)
2016-06-20 15:21:45 +00:00
}
2016-06-22 09:17:51 +00:00
//export StopNodeRPCServer
func StopNodeRPCServer() *C.char {
_, err := geth.NodeManagerInstance().StopNodeRPCServer()
return makeJSONErrorResponse(err)
}
//export StartNodeRPCServer
func StartNodeRPCServer() *C.char {
_, err := geth.NodeManagerInstance().StartNodeRPCServer()
return makeJSONErrorResponse(err)
}
//export InitJail
func InitJail(js *C.char) {
jail.Init(C.GoString(js))
2016-06-22 09:17:51 +00:00
}
//export Parse
func Parse(chatID *C.char, js *C.char) *C.char {
res := jail.GetInstance().Parse(C.GoString(chatID), C.GoString(js))
2016-06-22 09:17:51 +00:00
return C.CString(res)
}
//export Call
func Call(chatID *C.char, path *C.char, params *C.char) *C.char {
res := jail.GetInstance().Call(C.GoString(chatID), C.GoString(path), C.GoString(params))
return C.CString(res)
}
2016-07-04 16:00:29 +00:00
//export PopulateStaticPeers
func PopulateStaticPeers() {
geth.NodeManagerInstance().PopulateStaticPeers()
}
//export AddPeer
func AddPeer(url *C.char) *C.char {
success, err := geth.NodeManagerInstance().AddPeer(C.GoString(url))
errString := ""
2016-07-04 16:00:29 +00:00
if err != nil {
fmt.Fprintln(os.Stderr, err)
errString = err.Error()
}
out := geth.AddPeerResult{
2016-07-04 16:00:29 +00:00
Success: success,
Error: errString,
}
outBytes, _ := json.Marshal(&out)
return C.CString(string(outBytes))
2016-06-22 09:17:51 +00:00
}
2016-07-12 18:10:37 +00:00
func makeJSONErrorResponse(err error) *C.char {
errString := ""
if err != nil {
fmt.Fprintln(os.Stderr, err)
errString = err.Error()
}
out := geth.JSONError{
Error: errString,
}
outBytes, _ := json.Marshal(&out)
return C.CString(string(outBytes))
}