From a7df4ed388e4d78653326ed7a35e72221e23a5d9 Mon Sep 17 00:00:00 2001 From: Igor Sirotin Date: Thu, 1 Jun 2023 08:33:57 +0300 Subject: [PATCH] Local Pairing update for `StopNode` (#3536) --- VERSION | 2 +- api/geth_backend.go | 19 +++++++++++++---- mobile/status.go | 33 ++++++++++++++++++++++++++++++ server/pairing/events.go | 7 +++++++ server/pairing/payload_receiver.go | 18 ++++++++++------ 5 files changed, 68 insertions(+), 11 deletions(-) diff --git a/VERSION b/VERSION index 45866eec4..a1046a0b6 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.153.1 +0.154.0 diff --git a/api/geth_backend.go b/api/geth_backend.go index fb2845d89..a3a063430 100644 --- a/api/geth_backend.go +++ b/api/geth_backend.go @@ -85,7 +85,7 @@ type GethStatusBackend struct { selectedAccountKeyID string log log.Logger allowAllRPC bool // used only for tests, disables api method restrictions - + localPairing bool // used to disable login/logout signalling } // NewGethStatusBackend create a new GethStatusBackend instance @@ -109,6 +109,7 @@ func (b *GethStatusBackend) initialize() { b.personalAPI = personalAPI b.statusNode.SetMultiaccountsDB(b.multiaccountsDB) b.log = log.New("package", "status-go/api.GethStatusBackend") + b.localPairing = false } // StatusNode returns reference to node manager @@ -395,7 +396,9 @@ func (b *GethStatusBackend) StartNodeWithKey(acc multiaccounts.Account, password // Stop node for clean up _ = b.StopNode() } - signal.SendLoggedIn(err) + if !b.localPairing { + signal.SendLoggedIn(err) + } return err } @@ -512,7 +515,9 @@ func (b *GethStatusBackend) StartNodeWithAccount(acc multiaccounts.Account, pass // Stop node for clean up _ = b.StopNode() } - signal.SendLoggedIn(err) + if !b.localPairing { + signal.SendLoggedIn(err) + } return err } @@ -1241,7 +1246,9 @@ func (b *GethStatusBackend) stopNode() error { if b.statusNode == nil || !b.IsNodeRunning() { return nil } - defer signal.SendNodeStopped() + if !b.localPairing { + defer signal.SendNodeStopped() + } return b.statusNode.Stop() } @@ -1813,3 +1820,7 @@ func (b *GethStatusBackend) SwitchFleet(fleet string, conf *params.NodeConfig) e return nil } + +func (b *GethStatusBackend) SetLocalPairing(value bool) { + b.localPairing = value +} diff --git a/mobile/status.go b/mobile/status.go index 9e35c1f63..a09d8ac1e 100644 --- a/mobile/status.go +++ b/mobile/status.go @@ -1007,10 +1007,22 @@ func GetConnectionStringForBeingBootstrapped(configJSON string) string { if configJSON == "" { return makeJSONResponse(fmt.Errorf("no config given, PayloadSourceConfig is expected")) } + + statusBackend.SetLocalPairing(true) + defer func() { + statusBackend.SetLocalPairing(false) + }() + cs, err := pairing.StartUpReceiverServer(statusBackend, configJSON) if err != nil { return makeJSONResponse(err) } + + err = statusBackend.Logout() + if err != nil { + return makeJSONResponse(err) + } + return cs } @@ -1024,6 +1036,12 @@ func GetConnectionStringForBootstrappingAnotherDevice(configJSON string) string if configJSON == "" { return makeJSONResponse(fmt.Errorf("no config given, SendingServerConfig is expected")) } + + statusBackend.SetLocalPairing(true) + defer func() { + statusBackend.SetLocalPairing(false) + }() + cs, err := pairing.StartUpSenderServer(statusBackend, configJSON) if err != nil { return makeJSONResponse(err) @@ -1044,7 +1062,17 @@ func InputConnectionStringForBootstrapping(cs, configJSON string) string { return makeJSONResponse(fmt.Errorf("no config given, ReceiverClientConfig is expected")) } + statusBackend.SetLocalPairing(true) + defer func() { + statusBackend.SetLocalPairing(false) + }() + err := pairing.StartUpReceivingClient(statusBackend, cs, configJSON) + if err != nil { + return makeJSONResponse(err) + } + + err = statusBackend.Logout() return makeJSONResponse(err) } @@ -1060,6 +1088,11 @@ func InputConnectionStringForBootstrappingAnotherDevice(cs, configJSON string) s return makeJSONResponse(fmt.Errorf("no config given, SenderClientConfig is expected")) } + statusBackend.SetLocalPairing(true) + defer func() { + statusBackend.SetLocalPairing(false) + }() + err := pairing.StartUpSendingClient(statusBackend, cs, configJSON) return makeJSONResponse(err) } diff --git a/server/pairing/events.go b/server/pairing/events.go index 2e1203ae5..9f8efe655 100644 --- a/server/pairing/events.go +++ b/server/pairing/events.go @@ -1,5 +1,7 @@ package pairing +import "github.com/status-im/status-go/multiaccounts" + // EventType type for event types. type EventType string @@ -37,3 +39,8 @@ const ( ActionPairingInstallation ActionPeerDiscovery ) + +type AccountData struct { + Account *multiaccounts.Account `json:"account,omitempty"` + Password string `json:"password,omitempty"` +} diff --git a/server/pairing/payload_receiver.go b/server/pairing/payload_receiver.go index 267beb663..5d9e64f45 100644 --- a/server/pairing/payload_receiver.go +++ b/server/pairing/payload_receiver.go @@ -40,7 +40,7 @@ type BasePayloadReceiver struct { receiveCallback func() } -func NewBaseBasePayloadReceiver(e *PayloadEncryptor, um ProtobufUnmarshaller, s PayloadStorer, callback func()) *BasePayloadReceiver { +func NewBasePayloadReceiver(e *PayloadEncryptor, um ProtobufUnmarshaller, s PayloadStorer, callback func()) *BasePayloadReceiver { return &BasePayloadReceiver{ PayloadLockPayload: &PayloadLockPayload{e}, PayloadReceived: &PayloadReceived{e}, @@ -63,11 +63,16 @@ func (bpr *BasePayloadReceiver) Receive(data []byte) error { return err } + err = bpr.storer.Store() + if err != nil { + return err + } + if bpr.receiveCallback != nil { bpr.receiveCallback() } - return bpr.storer.Store() + return nil } /* @@ -92,9 +97,10 @@ func NewAccountPayloadReceiver(e *PayloadEncryptor, p *AccountPayload, config *R return nil, err } - return NewBaseBasePayloadReceiver(e, NewPairingPayloadMarshaller(p, l), aps, + return NewBasePayloadReceiver(e, NewPairingPayloadMarshaller(p, l), aps, func() { - signal.SendLocalPairingEvent(Event{Type: EventReceivedAccount, Action: ActionPairingAccount, Data: p.multiaccount}) + data := AccountData{Account: p.multiaccount, Password: p.password} + signal.SendLocalPairingEvent(Event{Type: EventReceivedAccount, Action: ActionPairingAccount, Data: data}) }, ), nil } @@ -220,7 +226,7 @@ func NewRawMessagePayloadReceiver(accountPayload *AccountPayload, e *PayloadEncr e = e.Renew() payload := NewRawMessagesPayload() - return NewBaseBasePayloadReceiver(e, + return NewBasePayloadReceiver(e, NewRawMessagePayloadMarshaller(payload), NewRawMessageStorer(backend, payload, accountPayload, config), nil) } @@ -268,7 +274,7 @@ func NewInstallationPayloadReceiver(e *PayloadEncryptor, backend *api.GethStatus e = e.Renew() payload := NewRawMessagesPayload() - return NewBaseBasePayloadReceiver(e, + return NewBasePayloadReceiver(e, NewRawMessagePayloadMarshaller(payload), NewInstallationPayloadStorer(backend, payload, deviceType), nil) }