Skip to content

Commit 810241f

Browse files
committed
chore_: code review - move to separate file and use AES encryption
1 parent d88eb25 commit 810241f

File tree

6 files changed

+294
-181
lines changed

6 files changed

+294
-181
lines changed

protocol/messenger.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,9 @@ func (m *Messenger) Start() (*MessengerResponse, error) {
785785
if !m.config.featureFlags.DisableCheckingForBackup {
786786
m.startBackupLoop()
787787
}
788+
if m.config.featureFlags.EnableLocalBackup {
789+
m.startLocalBackupLoop()
790+
}
788791
if !m.config.featureFlags.DisableAutoMessageLoop {
789792
err = m.startAutoMessageLoop()
790793
if err != nil {

protocol/messenger_backup.go

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package protocol
22

33
import (
44
"context"
5-
"os"
6-
"path/filepath"
75
"time"
86

97
"github.com/golang/protobuf/proto"
@@ -15,9 +13,7 @@ import (
1513
"github.com/status-im/status-go/multiaccounts/settings"
1614
"github.com/status-im/status-go/protocol/common"
1715
"github.com/status-im/status-go/protocol/communities"
18-
"github.com/status-im/status-go/protocol/encryption"
1916
"github.com/status-im/status-go/protocol/protobuf"
20-
v1protocol "github.com/status-im/status-go/protocol/v1"
2117
)
2218

2319
const (
@@ -157,14 +153,11 @@ func (m *Messenger) BackupData(ctx context.Context) (uint64, error) {
157153
}
158154
}
159155

160-
fullBackup := &protobuf.Backup{}
161-
162156
// Update contacts messages encode and dispatch
163157
for i, d := range contactsToBackup {
164158
pb := backupDetailsOnly()
165159
pb.ContactsDetails.DataNumber = uint32(i + 1)
166160
pb.Contacts = d.Contacts
167-
fullBackup.Contacts = append(fullBackup.Contacts, d.Contacts...)
168161
err = m.encodeAndDispatchBackupMessage(ctx, pb, chat.ID)
169162
if err != nil {
170163
return 0, err
@@ -176,7 +169,6 @@ func (m *Messenger) BackupData(ctx context.Context) (uint64, error) {
176169
pb := backupDetailsOnly()
177170
pb.CommunitiesDetails.DataNumber = uint32(i + 1)
178171
pb.Communities = d.Communities
179-
fullBackup.Communities = append(fullBackup.Communities, d.Communities...)
180172
err = m.encodeAndDispatchBackupMessage(ctx, pb, chat.ID)
181173
if err != nil {
182174
return 0, err
@@ -187,7 +179,6 @@ func (m *Messenger) BackupData(ctx context.Context) (uint64, error) {
187179
pb := backupDetailsOnly()
188180
pb.ProfileDetails.DataNumber = uint32(1)
189181
pb.Profile = profileToBackup.Profile
190-
fullBackup.Profile = profileToBackup.Profile
191182
err = m.encodeAndDispatchBackupMessage(ctx, pb, chat.ID)
192183
if err != nil {
193184
return 0, err
@@ -198,7 +189,6 @@ func (m *Messenger) BackupData(ctx context.Context) (uint64, error) {
198189
pb := backupDetailsOnly()
199190
pb.ChatsDetails.DataNumber = uint32(i + 1)
200191
pb.Chats = d.Chats
201-
fullBackup.Chats = append(fullBackup.Chats, d.Chats...)
202192
err = m.encodeAndDispatchBackupMessage(ctx, pb, chat.ID)
203193
if err != nil {
204194
return 0, err
@@ -210,8 +200,6 @@ func (m *Messenger) BackupData(ctx context.Context) (uint64, error) {
210200
pb := backupDetailsOnly()
211201
pb.SettingsDetails.DataNumber = uint32(i + 1)
212202
pb.Setting = d
213-
// TODO find a way to get all settings
214-
// fullBackup.Setting = append(fullBackup.Setting, d)
215203
err = m.encodeAndDispatchBackupMessage(ctx, pb, chat.ID)
216204
if err != nil {
217205
return 0, err
@@ -223,8 +211,6 @@ func (m *Messenger) BackupData(ctx context.Context) (uint64, error) {
223211
pb := backupDetailsOnly()
224212
pb.KeypairDetails.DataNumber = uint32(i + 1)
225213
pb.Keypair = d.Keypair
226-
// TODO find a way to get all settings
227-
// fullBackup.Keypair = append(fullBackup.Keypair, d.Keypair)
228214
err = m.encodeAndDispatchBackupMessage(ctx, pb, chat.ID)
229215
if err != nil {
230216
return 0, err
@@ -236,49 +222,12 @@ func (m *Messenger) BackupData(ctx context.Context) (uint64, error) {
236222
pb := backupDetailsOnly()
237223
pb.WatchOnlyAccountDetails.DataNumber = uint32(i + 1)
238224
pb.WatchOnlyAccount = d.WatchOnlyAccount
239-
// TODO find a way to get all settings
240-
// fullBackup.WatchOnlyAccount = append(fullBackup.WatchOnlyAccount, d.Keypair)
241225
err = m.encodeAndDispatchBackupMessage(ctx, pb, chat.ID)
242226
if err != nil {
243227
return 0, err
244228
}
245229
}
246230

247-
if m.config.featureFlags.EnableLocalBackup {
248-
// TODO put file in a constant
249-
path := filepath.Join(m.config.backupConfig.DataDir, "user_data.bkp")
250-
251-
if err := os.MkdirAll(m.config.backupConfig.DataDir, 0700); err != nil {
252-
return 0, err
253-
}
254-
255-
file, err := os.Create(path)
256-
if err != nil {
257-
return 0, err
258-
}
259-
defer file.Close()
260-
261-
mashalledMessage, err := proto.Marshal(fullBackup)
262-
if err != nil {
263-
return 0, err
264-
}
265-
266-
messageSpec, err := m.encryptor.BuildDHMessage(m.identity, &m.identity.PublicKey, mashalledMessage)
267-
if err != nil {
268-
return 0, err
269-
}
270-
271-
encryptedMessage, err := proto.Marshal(messageSpec.Message)
272-
if err != nil {
273-
return 0, err
274-
}
275-
err = os.WriteFile(path, encryptedMessage, 0600)
276-
if err != nil {
277-
m.logger.Error("failed to write backup message to file", zap.Error(err), zap.String("path", path))
278-
return 0, err
279-
}
280-
}
281-
282231
chat.LastClockValue = clock
283232
err = m.saveChat(chat)
284233
if err != nil {
@@ -297,62 +246,6 @@ func (m *Messenger) BackupData(ctx context.Context) (uint64, error) {
297246
return clockInSeconds, nil
298247
}
299248

300-
func (m *Messenger) importLocalBackupFile(filePath string) (*MessengerResponse, error) {
301-
if !m.config.featureFlags.EnableLocalBackup {
302-
return nil, nil
303-
}
304-
305-
// Make sure the backup file exists
306-
content, err := os.ReadFile(filePath)
307-
if err != nil {
308-
return nil, err
309-
}
310-
311-
// Decrypt the backup file
312-
// Unmarshal the content to get the message spec
313-
var messageSpec encryption.ProtocolMessage
314-
err = proto.Unmarshal(content, &messageSpec)
315-
if err != nil {
316-
return nil, err
317-
}
318-
319-
// Decrypt the payload
320-
var defaultMessageID = []byte("default")
321-
decryptedPayload1, err := m.encryptor.HandleMessage(m.identity, &m.identity.PublicKey, &messageSpec, defaultMessageID)
322-
if err != nil {
323-
return nil, err
324-
}
325-
326-
// Unmarshal the decrypted payload to get the backup message
327-
var backupMessage protobuf.Backup
328-
err = proto.Unmarshal(decryptedPayload1.DecryptedMessage, &backupMessage)
329-
if err != nil {
330-
return nil, err
331-
}
332-
333-
// Handle the backup
334-
state := ReceivedMessageState{
335-
Response: &MessengerResponse{},
336-
AllChats: &chatMap{},
337-
AllContacts: &contactMap{
338-
me: m.selfContact,
339-
},
340-
Timesource: m.getTimesource(),
341-
ModifiedContacts: &stringBoolMap{},
342-
ModifiedInstallations: &stringBoolMap{},
343-
}
344-
err = m.HandleBackup(
345-
&state,
346-
&backupMessage,
347-
&v1protocol.StatusMessage{},
348-
)
349-
if err != nil {
350-
return nil, err
351-
}
352-
353-
return m.saveDataAndPrepareResponse(&state)
354-
}
355-
356249
func (m *Messenger) encodeAndDispatchBackupMessage(ctx context.Context, message *protobuf.Backup, chatID string) error {
357250
encodedMessage, err := proto.Marshal(message)
358251
if err != nil {

protocol/messenger_backup_test.go

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"os"
8-
"path/filepath"
97
"reflect"
108
"testing"
119
"time"
@@ -15,7 +13,6 @@ import (
1513
"github.com/ethereum/go-ethereum/common"
1614
"github.com/ethereum/go-ethereum/event"
1715

18-
"github.com/status-im/status-go/params"
1916
v1protocol "github.com/status-im/status-go/protocol/v1"
2017
"github.com/status-im/status-go/protocol/wakusync"
2118
"github.com/status-im/status-go/services/accounts/accountsevent"
@@ -113,76 +110,6 @@ func (s *MessengerBackupSuite) TestBackupContacts() {
113110
s.Require().Equal(clock, lastBackup)
114111
}
115112

116-
func (s *MessengerBackupSuite) TestBackupContactsLocally() {
117-
backupOptions := []Option{
118-
WithLocalBackup(&params.BackupConfig{
119-
DataDir: filepath.Join(s.tmpdir, params.BackupsRelativePath),
120-
}),
121-
}
122-
123-
// Create bob1
124-
privateKey, err := crypto.GenerateKey()
125-
s.Require().NoError(err)
126-
bob1, err := newMessengerWithKey(s.shh, privateKey, s.logger, backupOptions)
127-
s.Require().NoError(err)
128-
defer TearDownMessenger(&s.Suite, bob1)
129-
130-
// Create bob2
131-
bob2, err := newMessengerWithKey(s.shh, bob1.identity, s.logger, backupOptions)
132-
s.Require().NoError(err)
133-
defer TearDownMessenger(&s.Suite, bob2)
134-
135-
// Make sure there is no backup at first
136-
backupFile := filepath.Join(bob1.config.backupConfig.DataDir, "user_data.bkp")
137-
err = os.RemoveAll(backupFile)
138-
s.Require().NoError(err)
139-
140-
// Create 2 contacts
141-
contact1Key, err := crypto.GenerateKey()
142-
s.Require().NoError(err)
143-
contactID1 := types.EncodeHex(crypto.FromECDSAPub(&contact1Key.PublicKey))
144-
145-
_, err = bob1.AddContact(context.Background(), &requests.AddContact{ID: contactID1})
146-
s.Require().NoError(err)
147-
148-
contact2Key, err := crypto.GenerateKey()
149-
s.Require().NoError(err)
150-
contactID2 := types.EncodeHex(crypto.FromECDSAPub(&contact2Key.PublicKey))
151-
152-
_, err = bob1.AddContact(context.Background(), &requests.AddContact{ID: contactID2})
153-
s.Require().NoError(err)
154-
155-
s.Require().Len(bob1.Contacts(), 2)
156-
157-
actualContacts := bob1.Contacts()
158-
if actualContacts[0].ID == contactID1 {
159-
s.Require().Equal(actualContacts[0].ID, contactID1)
160-
s.Require().Equal(actualContacts[1].ID, contactID2)
161-
} else {
162-
s.Require().Equal(actualContacts[0].ID, contactID2)
163-
s.Require().Equal(actualContacts[1].ID, contactID1)
164-
}
165-
166-
s.Require().Equal(ContactRequestStateSent, actualContacts[0].ContactRequestLocalState)
167-
s.Require().Equal(ContactRequestStateSent, actualContacts[1].ContactRequestLocalState)
168-
s.Require().True(actualContacts[0].added())
169-
s.Require().True(actualContacts[1].added())
170-
171-
// Backup
172-
_, err = bob1.BackupData(context.Background())
173-
s.Require().NoError(err)
174-
175-
// Safety check
176-
s.Require().Len(bob2.Contacts(), 0)
177-
178-
// Import the backup file and process it
179-
response, err := bob2.importLocalBackupFile(backupFile)
180-
s.Require().NoError(err)
181-
s.Require().NotNil(response)
182-
s.Require().Len(response.Contacts, 2)
183-
s.Require().Len(bob2.Contacts(), 2)
184-
}
185-
186113
func (s *MessengerBackupSuite) TestBackupProfile() {
187114
const bob1DisplayName = "bobby"
188115

0 commit comments

Comments
 (0)