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
|
|
|
|
2016-11-25 08:06:47 +00:00
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv2"
|
2016-09-11 11:44:14 +00:00
|
|
|
"github.com/status-im/status-go/geth"
|
|
|
|
"github.com/status-im/status-go/jail"
|
2016-06-15 13:54:07 +00:00
|
|
|
)
|
|
|
|
|
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 {
|
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
|
2016-09-11 11:44:14 +00:00
|
|
|
address, pubKey, mnemonic, err := geth.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()
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
out := geth.AccountInfo{
|
2016-08-18 00:15:58 +00:00
|
|
|
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 {
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
address, pubKey, err := geth.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()
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2016-08-24 15:10:42 +00:00
|
|
|
//export RecoverAccount
|
|
|
|
func RecoverAccount(password, mnemonic *C.char) *C.char {
|
2016-08-18 00:15:58 +00:00
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
address, pubKey, err := geth.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()
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
out := geth.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
|
|
|
}
|
2016-06-22 18:56:27 +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
|
|
|
|
2016-06-29 11:32:04 +00:00
|
|
|
//export Login
|
|
|
|
func Login(address, password *C.char) *C.char {
|
2016-08-21 06:45:59 +00:00
|
|
|
// 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-09-11 11:44:14 +00:00
|
|
|
err := geth.SelectAccount(C.GoString(address), C.GoString(password))
|
2016-08-21 06:45:59 +00:00
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
errString := ""
|
2016-08-21 06:45:59 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
errString = err.Error()
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
out := geth.JSONError{
|
2016-08-21 06:45:59 +00:00
|
|
|
Error: errString,
|
|
|
|
}
|
|
|
|
outBytes, _ := json.Marshal(&out)
|
|
|
|
|
|
|
|
return C.CString(string(outBytes))
|
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
|
2016-09-11 11:44:14 +00:00
|
|
|
err := geth.Logout()
|
2016-08-29 00:31:16 +00:00
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
errString := ""
|
2016-08-29 00:31:16 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
errString = err.Error()
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
out := geth.JSONError{
|
2016-08-29 00:31:16 +00:00
|
|
|
Error: errString,
|
|
|
|
}
|
|
|
|
outBytes, _ := json.Marshal(&out)
|
|
|
|
|
|
|
|
return C.CString(string(outBytes))
|
|
|
|
}
|
|
|
|
|
2016-07-27 11:47:41 +00:00
|
|
|
//export CompleteTransaction
|
2016-08-31 18:02:06 +00:00
|
|
|
func CompleteTransaction(id, password *C.char) *C.char {
|
2016-09-11 11:44:14 +00:00
|
|
|
txHash, err := geth.CompleteTransaction(C.GoString(id), C.GoString(password))
|
2016-07-27 11:47:41 +00:00
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
errString := ""
|
2016-07-27 11:47:41 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
errString = err.Error()
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
out := geth.CompleteTransactionResult{
|
2016-11-05 17:12:24 +00:00
|
|
|
Id: C.GoString(id),
|
2016-08-09 16:41:42 +00:00
|
|
|
Hash: txHash.Hex(),
|
2016-07-27 11:47:41 +00:00
|
|
|
Error: errString,
|
|
|
|
}
|
|
|
|
outBytes, _ := json.Marshal(&out)
|
|
|
|
|
|
|
|
return C.CString(string(outBytes))
|
|
|
|
}
|
|
|
|
|
2016-11-05 17:12:24 +00:00
|
|
|
//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))
|
|
|
|
}
|
|
|
|
|
2016-10-30 22:35:10 +00:00
|
|
|
//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))
|
|
|
|
}
|
|
|
|
|
2016-11-05 17:12:24 +00:00
|
|
|
//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))
|
|
|
|
}
|
|
|
|
|
2016-06-29 11:32:04 +00:00
|
|
|
//export StartNode
|
|
|
|
func StartNode(datadir *C.char) *C.char {
|
2016-06-20 15:21:45 +00:00
|
|
|
// This starts a geth node with the given datadir
|
2017-01-26 00:39:20 +00:00
|
|
|
err := geth.CreateAndRunNode(&geth.NodeConfig{
|
|
|
|
DataDir: C.GoString(datadir),
|
|
|
|
HTTPPort: geth.HTTPPort,
|
|
|
|
WSEnabled: true,
|
|
|
|
WSPort: geth.WSPort,
|
|
|
|
TLSEnabled: false,
|
|
|
|
})
|
2016-06-30 13:23:07 +00:00
|
|
|
|
2017-01-23 06:25:24 +00:00
|
|
|
return makeJSONErrorResponse(err)
|
|
|
|
}
|
2016-06-30 13:23:07 +00:00
|
|
|
|
2017-01-24 18:42:55 +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
|
|
|
|
2017-01-26 00:39:20 +00:00
|
|
|
//export StartTLSNode
|
|
|
|
func StartTLSNode(datadir *C.char) *C.char {
|
|
|
|
// This starts a geth node with the given datadir
|
|
|
|
err := geth.CreateAndRunNode(&geth.NodeConfig{
|
|
|
|
DataDir: C.GoString(datadir),
|
|
|
|
HTTPPort: geth.HTTPPort,
|
|
|
|
WSPort: geth.WSPort,
|
|
|
|
TLSEnabled: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
return makeJSONErrorResponse(err)
|
|
|
|
}
|
|
|
|
|
2016-12-11 12:19:20 +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)
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
//export InitJail
|
|
|
|
func InitJail(js *C.char) {
|
|
|
|
jail.Init(C.GoString(js))
|
2016-06-22 09:17:51 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +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)
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
//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:16:18 +00:00
|
|
|
}
|
2016-07-04 16:00:29 +00:00
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
//export AddPeer
|
|
|
|
func AddPeer(url *C.char) *C.char {
|
2016-12-07 21:07:08 +00:00
|
|
|
success, err := geth.NodeManagerInstance().AddPeer(C.GoString(url))
|
2016-09-11 11:44:14 +00:00
|
|
|
errString := ""
|
2016-07-04 16:00:29 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
errString = err.Error()
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
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
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
//export AddWhisperFilter
|
|
|
|
func AddWhisperFilter(filterJson *C.char) *C.char {
|
2016-07-12 18:10:37 +00:00
|
|
|
|
2016-07-27 11:47:41 +00:00
|
|
|
var id int
|
|
|
|
var filter whisper.NewFilterArgs
|
2016-07-12 18:10:37 +00:00
|
|
|
|
2016-07-27 11:47:41 +00:00
|
|
|
err := json.Unmarshal([]byte(C.GoString(filterJson)), &filter)
|
|
|
|
if err == nil {
|
2016-09-11 11:44:14 +00:00
|
|
|
id = geth.AddWhisperFilter(filter)
|
2016-07-12 18:10:37 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
errString := ""
|
2016-07-12 18:10:37 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
errString = err.Error()
|
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
out := geth.AddWhisperFilterResult{
|
2016-07-27 11:47:41 +00:00
|
|
|
Id: id,
|
2016-07-12 18:10:37 +00:00
|
|
|
Error: errString,
|
|
|
|
}
|
|
|
|
outBytes, _ := json.Marshal(&out)
|
|
|
|
|
|
|
|
return C.CString(string(outBytes))
|
2016-07-27 11:47:41 +00:00
|
|
|
|
2016-07-12 18:10:37 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
//export RemoveWhisperFilter
|
|
|
|
func RemoveWhisperFilter(idFilter int) {
|
|
|
|
geth.RemoveWhisperFilter(idFilter)
|
2016-07-12 18:10:37 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 11:44:14 +00:00
|
|
|
//export ClearWhisperFilters
|
|
|
|
func ClearWhisperFilters() {
|
|
|
|
geth.ClearWhisperFilters()
|
2016-07-27 11:47:41 +00:00
|
|
|
}
|
2016-12-11 12:19:20 +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))
|
|
|
|
}
|