Skip to content

Commit

Permalink
remove reSelectAccount from backend, SelectKeyPair and SelectedKeyPai…
Browse files Browse the repository at this point in the history
…rID methods from Whisper
  • Loading branch information
adambabik authored and Pedro Pombeiro committed Dec 30, 2019
1 parent 024f30f commit 7e3a385
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 700 deletions.
6 changes: 0 additions & 6 deletions api/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,6 @@ func TestBackendAccountsConcurrently(t *testing.T) {
wg.Done()
}(tuple)

wg.Add(1)
go func() {
assert.NoError(t, backend.reSelectAccount())
wg.Done()
}()

wg.Add(1)
go func() {
assert.NoError(t, backend.Logout())
Expand Down
129 changes: 52 additions & 77 deletions api/geth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,19 @@ var _ StatusBackend = (*GethStatusBackend)(nil)
type GethStatusBackend struct {
mu sync.Mutex
// rootDataDir is the same for all networks.
rootDataDir string
appDB *sql.DB
statusNode *node.StatusNode
personalAPI *personal.PublicAPI
rpcFilters *rpcfilters.Service
multiaccountsDB *multiaccounts.Database
accountManager *account.Manager
transactor *transactions.Transactor
connectionState connectionState
appState appState
log log.Logger
allowAllRPC bool // used only for tests, disables api method restrictions
rootDataDir string
appDB *sql.DB
statusNode *node.StatusNode
personalAPI *personal.PublicAPI
rpcFilters *rpcfilters.Service
multiaccountsDB *multiaccounts.Database
accountManager *account.Manager
transactor *transactions.Transactor
connectionState connectionState
appState appState
selectedAccountShhKeyID string
log log.Logger
allowAllRPC bool // used only for tests, disables api method restrictions
}

// NewGethStatusBackend create a new GethStatusBackend instance
Expand Down Expand Up @@ -112,6 +113,11 @@ func (b *GethStatusBackend) Transactor() *transactions.Transactor {
return b.transactor
}

// SelectedAccountShhKeyID returns a Whisper key ID of the selected chat key pair.
func (b *GethStatusBackend) SelectedAccountShhKeyID() string {
return b.selectedAccountShhKeyID
}

// IsNodeRunning confirm that node is running
func (b *GethStatusBackend) IsNodeRunning() bool {
return b.statusNode.IsRunning()
Expand All @@ -121,13 +127,10 @@ func (b *GethStatusBackend) IsNodeRunning() bool {
func (b *GethStatusBackend) StartNode(config *params.NodeConfig) error {
b.mu.Lock()
defer b.mu.Unlock()

if err := b.startNode(config); err != nil {
signal.SendNodeCrashed(err)

return err
}

return nil
}

Expand Down Expand Up @@ -458,12 +461,6 @@ func (b *GethStatusBackend) startNode(config *params.NodeConfig) (err error) {
}
b.log.Info("Handlers registered")

if err = b.reSelectAccount(); err != nil {
b.log.Error("Reselect account failed", "err", err)
return
}
b.log.Info("Account reselected")

if st, err := b.statusNode.StatusService(); err == nil {
st.SetAccountManager(b.accountManager)
}
Expand All @@ -472,6 +469,16 @@ func (b *GethStatusBackend) startNode(config *params.NodeConfig) (err error) {
st.SetDiscoverer(b.StatusNode())
}

// Handle a case when a node is stopped and resumed.
// If there is no account selected, an error is returned.
if _, err := b.accountManager.SelectedChatAccount(); err == nil {
if err := b.injectAccountIntoServices(); err != nil {
return err
}
} else if err != account.ErrNoAccountSelected {
return err
}

signal.SendNodeReady()

if err := b.statusNode.StartDiscovery(); err != nil {
Expand Down Expand Up @@ -786,6 +793,7 @@ func (b *GethStatusBackend) cleanupServices() error {
if err := whisperService.DeleteKeyPairs(); err != nil {
return fmt.Errorf("%s: %v", ErrWhisperClearIdentitiesFailure, err)
}
b.selectedAccountShhKeyID = ""
default:
return err
}
Expand Down Expand Up @@ -817,28 +825,6 @@ func (b *GethStatusBackend) closeAppDB() error {
return nil
}

// reSelectAccount selects previously selected account, often, after node restart.
func (b *GethStatusBackend) reSelectAccount() error {
b.AccountManager().RemoveOnboarding()

selectedChatAccount, err := b.accountManager.SelectedChatAccount()
if selectedChatAccount == nil || err == account.ErrNoAccountSelected {
return nil
}

whisperService, err := b.statusNode.WhisperService()
switch err {
case node.ErrServiceUnknown: // Whisper was never registered
case nil:
if err := whisperService.SelectKeyPair(selectedChatAccount.AccountKey.PrivateKey); err != nil {
return ErrWhisperIdentityInjectionFailure
}
default:
return err
}
return nil
}

// SelectAccount selects current wallet and chat accounts, by verifying that each address has corresponding account which can be decrypted
// using provided password. Once verification is done, the decrypted chat key is injected into Whisper (as a single identity,
// all previous identities are removed).
Expand All @@ -853,7 +839,15 @@ func (b *GethStatusBackend) SelectAccount(loginParams account.LoginParams) error
return err
}

return b.injectAccountIntoServices()
if err := b.injectAccountIntoServices(); err != nil {
return err
}

if err := b.startWallet(); err != nil {
return err
}

return nil
}

func (b *GethStatusBackend) injectAccountIntoServices() error {
Expand All @@ -862,11 +856,17 @@ func (b *GethStatusBackend) injectAccountIntoServices() error {
return err
}

identity := chatAccount.AccountKey.PrivateKey
whisperService, err := b.statusNode.WhisperService()

switch err {
case node.ErrServiceUnknown: // Whisper was never registered
case nil:
if err := whisperService.SelectKeyPair(chatAccount.AccountKey.PrivateKey); err != nil {
if err := whisperService.DeleteKeyPairs(); err != nil { // err is not possible; method return value is incorrect
return err
}
b.selectedAccountShhKeyID, err = whisperService.AddKeyPair(identity)
if err != nil {
return ErrWhisperIdentityInjectionFailure
}
default:
Expand All @@ -879,11 +879,11 @@ func (b *GethStatusBackend) injectAccountIntoServices() error {
return err
}

if err := st.InitProtocol(b.appDB); err != nil {
if err := st.InitProtocol(identity, b.appDB); err != nil {
return err
}
}
return b.startWallet()
return nil
}

func (b *GethStatusBackend) startWallet() error {
Expand All @@ -910,11 +910,13 @@ func (b *GethStatusBackend) startWallet() error {
return wallet.StartReactor(
b.statusNode.RPCClient().Ethclient(),
allAddresses,
new(big.Int).SetUint64(b.statusNode.Config().NetworkID))
new(big.Int).SetUint64(b.statusNode.Config().NetworkID),
)
}

// InjectChatAccount selects the current chat account using chatKeyHex and injects the key into whisper.
func (b *GethStatusBackend) InjectChatAccount(chatKeyHex, encryptionKeyHex string) error {
// TODO: change the interface and omit the last argument.
func (b *GethStatusBackend) InjectChatAccount(chatKeyHex, _ string) error {
b.mu.Lock()
defer b.mu.Unlock()

Expand All @@ -924,36 +926,9 @@ func (b *GethStatusBackend) InjectChatAccount(chatKeyHex, encryptionKeyHex strin
if err != nil {
return err
}

b.accountManager.SetChatAccount(chatKey)
chatAccount, err := b.accountManager.SelectedChatAccount()
if err != nil {
return err
}

whisperService, err := b.statusNode.WhisperService()
switch err {
case node.ErrServiceUnknown: // Whisper was never registered
case nil:
if err := whisperService.SelectKeyPair(chatAccount.AccountKey.PrivateKey); err != nil {
return ErrWhisperIdentityInjectionFailure
}
default:
return err
}

if whisperService != nil {
st, err := b.statusNode.ShhExtService()
if err != nil {
return err
}

if err := st.InitProtocol(b.appDB); err != nil {
return err
}
}

return nil
return b.injectAccountIntoServices()
}

func appendIf(condition bool, services []gethnode.ServiceConstructor, service gethnode.ServiceConstructor) []gethnode.ServiceConstructor {
Expand Down
6 changes: 0 additions & 6 deletions eth-node/bridge/geth/whisper.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ func (w *gethWhisperWrapper) SubscribeEnvelopeEvents(eventsProxy chan<- types.En
return NewGethSubscriptionWrapper(w.whisper.SubscribeEnvelopeEvents(events))
}

// SelectedKeyPairID returns the id of currently selected key pair.
// It helps distinguish between different users w/o exposing the user identity itself.
func (w *gethWhisperWrapper) SelectedKeyPairID() string {
return w.whisper.SelectedKeyPairID()
}

func (w *gethWhisperWrapper) GetPrivateKey(id string) (*ecdsa.PrivateKey, error) {
return w.whisper.GetPrivateKey(id)
}
Expand Down
6 changes: 0 additions & 6 deletions eth-node/bridge/nimbus/whisper.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ func (w *nimbusWhisperWrapper) SubscribeEnvelopeEvents(eventsProxy chan<- types.
panic("not implemented")
}

// SelectedKeyPairID returns the id of currently selected key pair.
// It helps distinguish between different users w/o exposing the user identity itself.
func (w *nimbusWhisperWrapper) SelectedKeyPairID() string {
return ""
}

func (w *nimbusWhisperWrapper) GetPrivateKey(id string) (*ecdsa.PrivateKey, error) {
retVal := w.routineQueue.Send(func(c chan<- callReturn) {
idC, err := decodeHexID(id)
Expand Down
3 changes: 0 additions & 3 deletions eth-node/types/whisper.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ type Whisper interface {
// GetCurrentTime returns current time.
GetCurrentTime() time.Time

// SelectedKeyPairID returns the id of currently selected key pair.
// It helps distinguish between different users w/o exposing the user identity itself.
SelectedKeyPairID() string
// GetPrivateKey retrieves the private key of the specified identity.
GetPrivateKey(id string) (*ecdsa.PrivateKey, error)

Expand Down
24 changes: 15 additions & 9 deletions services/shhext/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/rlp"
"github.com/status-im/status-go/services/shhext/dedup"

"github.com/status-im/status-go/db"
"github.com/status-im/status-go/mailserver"
Expand Down Expand Up @@ -403,24 +402,31 @@ func (api *PublicAPI) SyncMessages(ctx context.Context, r SyncMessagesRequest) (
}
}

type Author struct {
PublicKey types.HexBytes `json:"publicKey"`
Alias string `json:"alias"`
Identicon string `json:"identicon"`
}

type Metadata struct {
DedupID []byte `json:"dedupId"`
EncryptionID types.HexBytes `json:"encryptionId"`
MessageID types.HexBytes `json:"messageId"`
Author Author `json:"author"`
}

// ConfirmMessagesProcessedByID is a method to confirm that messages was consumed by
// the client side.
// TODO: this is broken now as it requires dedup ID while a message hash should be used.
func (api *PublicAPI) ConfirmMessagesProcessedByID(messageConfirmations []*dedup.Metadata) error {
func (api *PublicAPI) ConfirmMessagesProcessedByID(messageConfirmations []*Metadata) error {
confirmationCount := len(messageConfirmations)
dedupIDs := make([][]byte, confirmationCount)
encryptionIDs := make([][]byte, confirmationCount)

for i, confirmation := range messageConfirmations {
dedupIDs[i] = confirmation.DedupID
encryptionIDs[i] = confirmation.EncryptionID
}

if err := api.service.ConfirmMessagesProcessed(encryptionIDs); err != nil {
return err
}

return api.service.deduplicator.AddMessageByID(dedupIDs)
return api.service.ConfirmMessagesProcessed(encryptionIDs)
}

// Post is used to send one-to-one for those who did not enabled device-to-device sync,
Expand Down
Loading

0 comments on commit 7e3a385

Please # to comment.