diff --git a/src/gethdep.go b/src/gethdep.go index 14591ec1d..86bd98d2e 100644 --- a/src/gethdep.go +++ b/src/gethdep.go @@ -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 +} \ No newline at end of file diff --git a/src/library.go b/src/library.go index 1b523cf02..53fd9692e 100644 --- a/src/library.go +++ b/src/library.go @@ -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)) } diff --git a/src/types.go b/src/types.go index 2bd82534f..1b043337b 100644 --- a/src/types.go +++ b/src/types.go @@ -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"` +}