Adds Logout() binding

This commit is contained in:
Victor Farazdagi 2016-08-29 03:31:16 +03:00
parent 574f67999d
commit acda0d1be5
4 changed files with 97 additions and 0 deletions

View File

@ -30,6 +30,7 @@ var (
ErrAccountToKeyMappingFailure = errors.New("cannot retreive a valid key for a given account")
ErrUnlockCalled = errors.New("no need to unlock accounts, use Login() instead")
ErrWhisperIdentityInjectionFailure = errors.New("failed to inject identity into Whisper")
ErrWhisperClearIdentitiesFailure = errors.New("failed to clear whisper identities")
)
// createAccount creates an internal geth account
@ -143,6 +144,23 @@ func selectAccount(address, password string) error {
return nil
}
// logout clears whisper identities
func logout() error {
if currentNode == nil {
return ErrInvalidGethNode
}
if whisperService == nil {
return ErrInvalidWhisperService
}
err := whisperService.ClearIdentities()
if err != nil {
return fmt.Errorf("%s: %v", ErrWhisperClearIdentitiesFailure, err)
}
return nil
}
// unlockAccount unlocks an existing account for a certain duration and
// inject the account as a whisper identity if the account was created as
// a whisper enabled account

View File

@ -127,6 +127,52 @@ func TestAccountSelect(t *testing.T) {
}
}
func TestAccountLogout(t *testing.T) {
err := prepareTestNode()
if err != nil {
t.Error(err)
return
}
var whisperInstance *whisper.Whisper
if err := currentNode.Service(&whisperInstance); err != nil {
t.Errorf("whisper service not running: %v", err)
}
// create an account
address, pubKey, _, err := createAccount(newAccountPassword)
if err != nil {
t.Errorf("could not create account: %v", err)
return
}
// make sure that identity doesn't exist (yet) in Whisper
if whisperInstance.HasIdentity(crypto.ToECDSAPub(common.FromHex(pubKey))) {
t.Error("identity already present in whisper")
}
// select/login
err = selectAccount(address, newAccountPassword)
if err != nil {
t.Errorf("Test failed: could not select account: %v", err)
return
}
if !whisperInstance.HasIdentity(crypto.ToECDSAPub(common.FromHex(pubKey))) {
t.Error("identity not injected into whisper")
}
err = logout()
if err != nil {
t.Errorf("cannot logout: %v", err)
}
// now, logout and check if identity is removed indeed
if whisperInstance.HasIdentity(crypto.ToECDSAPub(common.FromHex(pubKey))) {
t.Error("identity not cleared from whisper")
}
}
func TestWhisperMessaging(t *testing.T) {
err := prepareTestNode()
if err != nil {

View File

@ -76,6 +76,26 @@ func Login(address, password *C.char) *C.char {
return C.CString(string(outBytes))
}
//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))
}
//export UnlockAccount
func UnlockAccount(address, password *C.char, seconds int) *C.char {

View File

@ -170,6 +170,19 @@ func (self *Whisper) InjectIdentity(key *ecdsa.PrivateKey) error {
return nil
}
// ClearIdentities clears the current whisper identities in memory
func (self *Whisper) ClearIdentities() error {
self.keysMu.Lock()
defer self.keysMu.Unlock()
self.keys = make(map[string]*ecdsa.PrivateKey)
if len(self.keys) != 0 {
return fmt.Errorf("could not clear keys map")
}
return nil
}
// Watch installs a new message handler to run in case a matching packet arrives
// from the whisper network.
func (self *Whisper) Watch(options Filter) int {