mirror of
https://github.com/status-im/status-go.git
synced 2025-02-17 17:28:38 +00:00
Adds Logout() binding
This commit is contained in:
parent
574f67999d
commit
acda0d1be5
@ -30,6 +30,7 @@ var (
|
|||||||
ErrAccountToKeyMappingFailure = errors.New("cannot retreive a valid key for a given account")
|
ErrAccountToKeyMappingFailure = errors.New("cannot retreive a valid key for a given account")
|
||||||
ErrUnlockCalled = errors.New("no need to unlock accounts, use Login() instead")
|
ErrUnlockCalled = errors.New("no need to unlock accounts, use Login() instead")
|
||||||
ErrWhisperIdentityInjectionFailure = errors.New("failed to inject identity into Whisper")
|
ErrWhisperIdentityInjectionFailure = errors.New("failed to inject identity into Whisper")
|
||||||
|
ErrWhisperClearIdentitiesFailure = errors.New("failed to clear whisper identities")
|
||||||
)
|
)
|
||||||
|
|
||||||
// createAccount creates an internal geth account
|
// createAccount creates an internal geth account
|
||||||
@ -143,6 +144,23 @@ func selectAccount(address, password string) error {
|
|||||||
return nil
|
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
|
// unlockAccount unlocks an existing account for a certain duration and
|
||||||
// inject the account as a whisper identity if the account was created as
|
// inject the account as a whisper identity if the account was created as
|
||||||
// a whisper enabled account
|
// a whisper enabled account
|
||||||
|
@ -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) {
|
func TestWhisperMessaging(t *testing.T) {
|
||||||
err := prepareTestNode()
|
err := prepareTestNode()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -76,6 +76,26 @@ func Login(address, password *C.char) *C.char {
|
|||||||
return C.CString(string(outBytes))
|
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
|
//export UnlockAccount
|
||||||
func UnlockAccount(address, password *C.char, seconds int) *C.char {
|
func UnlockAccount(address, password *C.char, seconds int) *C.char {
|
||||||
|
|
||||||
|
13
src/vendor/github.com/ethereum/go-ethereum/whisper/whisper.go
generated
vendored
13
src/vendor/github.com/ethereum/go-ethereum/whisper/whisper.go
generated
vendored
@ -170,6 +170,19 @@ func (self *Whisper) InjectIdentity(key *ecdsa.PrivateKey) error {
|
|||||||
return nil
|
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
|
// Watch installs a new message handler to run in case a matching packet arrives
|
||||||
// from the whisper network.
|
// from the whisper network.
|
||||||
func (self *Whisper) Watch(options Filter) int {
|
func (self *Whisper) Watch(options Filter) int {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user