status-go/src/library.go

254 lines
5.2 KiB
Go
Raw Normal View History

package main
import "C"
import (
"encoding/json"
"fmt"
2016-07-12 18:10:37 +00:00
"github.com/ethereum/go-ethereum/whisper"
"os"
)
2016-06-30 13:43:51 +00:00
var emptyError = ""
2016-06-29 11:32:04 +00:00
//export CreateAccount
func CreateAccount(password *C.char) *C.char {
2016-06-30 13:23:07 +00:00
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 := createAccount(C.GoString(password))
2016-06-30 13:23:07 +00:00
2016-06-30 13:43:51 +00:00
errString := emptyError
2016-06-30 13:23:07 +00:00
if err != nil {
fmt.Fprintln(os.Stderr, err)
errString = err.Error()
}
out := 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 := createChildAccount(C.GoString(parentAddress), C.GoString(password))
errString := emptyError
if err != nil {
fmt.Fprintln(os.Stderr, err)
errString = err.Error()
}
out := AccountInfo{
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 := recoverAccount(C.GoString(password), C.GoString(mnemonic))
errString := emptyError
if err != nil {
fmt.Fprintln(os.Stderr, err)
errString = err.Error()
}
out := 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
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 := selectAccount(C.GoString(address), C.GoString(password))
errString := emptyError
if err != nil {
fmt.Fprintln(os.Stderr, err)
errString = err.Error()
}
out := JSONError{
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
err := logout()
errString := emptyError
if err != nil {
fmt.Fprintln(os.Stderr, err)
errString = err.Error()
}
out := JSONError{
Error: errString,
}
outBytes, _ := json.Marshal(&out)
return C.CString(string(outBytes))
}
2016-06-29 11:32:04 +00:00
//export UnlockAccount
func UnlockAccount(address, password *C.char, seconds int) *C.char {
2016-06-30 13:23:07 +00:00
2016-06-21 18:29:38 +00:00
// This is equivalent to unlocking an account from the command line,
// just modified to unlock the account for the currently running geth node
// based on the provided arguments
err := unlockAccount(C.GoString(address), C.GoString(password), seconds)
2016-06-30 13:23:07 +00:00
2016-06-30 13:43:51 +00:00
errString := emptyError
2016-06-22 11:29:35 +00:00
if err != nil {
2016-06-21 18:29:38 +00:00
fmt.Fprintln(os.Stderr, err)
2016-06-30 13:23:07 +00:00
errString = err.Error()
}
out := JSONError{
Error: errString,
2016-06-21 18:29:38 +00:00
}
outBytes, _ := json.Marshal(&out)
2016-06-30 13:23:07 +00:00
return C.CString(string(outBytes))
2016-06-21 18:29:38 +00:00
}
//export CompleteTransaction
func CompleteTransaction(id, password *C.char) *C.char {
txHash, err := completeTransaction(C.GoString(id), C.GoString(password))
errString := emptyError
if err != nil {
fmt.Fprintln(os.Stderr, err)
errString = err.Error()
}
2016-08-09 16:41:42 +00:00
out := CompleteTransactionResult{
Hash: txHash.Hex(),
Error: errString,
}
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-30 13:23:07 +00:00
2016-06-20 15:21:45 +00:00
// This starts a geth node with the given datadir
2016-06-22 11:29:35 +00:00
err := createAndStartNode(C.GoString(datadir))
2016-06-30 13:23:07 +00:00
2016-06-30 13:43:51 +00:00
errString := emptyError
2016-06-22 11:29:35 +00:00
if err != nil {
2016-06-20 15:21:45 +00:00
fmt.Fprintln(os.Stderr, err)
2016-06-30 13:23:07 +00:00
errString = err.Error()
}
out := JSONError{
Error: errString,
2016-06-20 15:21:45 +00:00
}
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
}
2016-06-22 09:17:51 +00:00
//export parse
func parse(chatId *C.char, js *C.char) *C.char {
res := Parse(C.GoString(chatId), C.GoString(js))
return C.CString(res)
}
//export call
func call(chatId *C.char, path *C.char, params *C.char) *C.char {
res := Call(C.GoString(chatId), C.GoString(path), C.GoString(params))
return C.CString(res)
}
//export initJail
func initJail(js *C.char) {
Init(C.GoString(js))
}
2016-07-04 16:00:29 +00:00
//export addPeer
func addPeer(url *C.char) *C.char {
success, err := doAddPeer(C.GoString(url))
errString := emptyError
if err != nil {
fmt.Fprintln(os.Stderr, err)
errString = err.Error()
}
out := AddPeerResult{
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
//export addWhisperFilter
func addWhisperFilter(filterJson *C.char) *C.char {
var id int
var filter whisper.NewFilterArgs
2016-07-12 18:10:37 +00:00
err := json.Unmarshal([]byte(C.GoString(filterJson)), &filter)
if err == nil {
2016-07-12 18:10:37 +00:00
id = doAddWhisperFilter(filter)
}
errString := emptyError
if err != nil {
fmt.Fprintln(os.Stderr, err)
errString = err.Error()
}
out := AddWhisperFilterResult{
Id: id,
2016-07-12 18:10:37 +00:00
Error: errString,
}
outBytes, _ := json.Marshal(&out)
return C.CString(string(outBytes))
2016-07-12 18:10:37 +00:00
}
//export removeWhisperFilter
func removeWhisperFilter(idFilter int) {
doRemoveWhisperFilter(idFilter)
}
//export clearWhisperFilters
func clearWhisperFilters() {
doClearWhisperFilters()
}