added addPeer binding

This commit is contained in:
Adrian Tiberius 2016-07-04 19:00:29 +03:00
parent 032cc27901
commit 72b84b05f8
3 changed files with 39 additions and 1 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/discover"
errextra "github.com/pkg/errors"
)
@ -125,3 +126,18 @@ func createAndStartNode(inputDir string) error {
return errors.New("Could not create the in-memory node object")
}
func doAddPeer(url string) (bool, error) {
server := currentNode.Server()
if server == nil {
return false, errors.New("node not started")
}
// Try to add the url as a static peer and return
node, err := discover.ParseNode(url)
if err != nil {
return false, fmt.Errorf("invalid enode: %v", err)
}
server.AddPeer(node)
return true, nil
}

View File

@ -36,7 +36,7 @@ func CreateAccount(password *C.char) *C.char {
func Login(address, password *C.char) *C.char {
// Equivalent to unlocking an account briefly, to inject a whisper identity,
// then locking the account again
out := UnlockAccount(address, password, 5)
out := UnlockAccount(address, password, 1)
return out
}
@ -97,4 +97,21 @@ func call(chatId *C.char, path *C.char, params *C.char) *C.char {
//export initJail
func initJail(js *C.char) {
Init(C.GoString(js))
//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))
}

View File

@ -9,3 +9,8 @@ type AccountInfo struct {
type JSONError struct {
Error string `json:"error"`
}
type AddPeerResult struct {
Success bool `json:"success"`
Error string `json:"error"`
}