Skip to content

Commit 5a9cd1f

Browse files
authored
fix: mf-6688 do not encrypt/decrypt with account for google drive (#12191)
1 parent 60190e4 commit 5a9cd1f

File tree

6 files changed

+63
-52
lines changed

6 files changed

+63
-52
lines changed

packages/mask/dashboard/modals/BackupPreviewModal/BackupPreviewDialog.tsx

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { Trans } from '@lingui/react/macro'
55
import { encryptBackup } from '@masknet/backup-format'
66
import { Icons } from '@masknet/icons'
77
import { InjectedDialog, LoadingStatus } from '@masknet/shared'
8-
import type { BackupAccountType } from '@masknet/shared-base'
98
import { DashboardRoutes } from '@masknet/shared-base'
109
import { ActionButton, makeStyles, useCustomSnackbar } from '@masknet/theme'
1110
import { encode } from '@msgpack/msgpack'
@@ -14,7 +13,6 @@ import { memo, useCallback, useMemo, useRef } from 'react'
1413
import { Controller } from 'react-hook-form'
1514
import { useNavigate } from 'react-router-dom'
1615
import { useAsyncFn, useUpdateEffect } from 'react-use'
17-
import { UserContext } from '../../../shared-ui/index.js'
1816
import { PersonasBackupPreview, WalletsBackupPreview } from '../../components/BackupPreview/index.js'
1917
import PasswordField from '../../components/PasswordField/index.js'
2018
import { useBackupFormState, type BackupFormInputs } from '../../hooks/useBackupFormState.js'
@@ -59,10 +57,12 @@ const useStyles = makeStyles()((theme) => ({
5957
export interface BackupPreviewDialogProps {
6058
open: boolean
6159
isUpload?: boolean
62-
code: string
63-
type: BackupAccountType
6460
account: string
65-
abstract?: string
61+
/**
62+
* MaskNetwork backup use account + password as password since legacy version.
63+
* Will be removed in the future when we remove MaskNetwork backup.
64+
*/
65+
encryptWithAccount: boolean
6666
title?: React.ReactNode | string
6767
uploadButtonLabel?: React.ReactNode | string
6868
onClose: () => void
@@ -71,10 +71,8 @@ export interface BackupPreviewDialogProps {
7171
export const BackupPreviewDialog = memo<BackupPreviewDialogProps>(function BackupPreviewDialog({
7272
open,
7373
isUpload,
74-
code,
75-
type,
7674
account,
77-
abstract,
75+
encryptWithAccount,
7876
title,
7977
uploadButtonLabel,
8078
onClose,
@@ -84,7 +82,6 @@ export const BackupPreviewDialog = memo<BackupPreviewDialogProps>(function Backu
8482
const controllerRef = useRef<AbortController | null>(null)
8583
const { classes, theme } = useStyles()
8684
const navigate = useNavigate()
87-
const { updateUser } = UserContext.useContainer()
8885
const {
8986
hasPassword,
9087
backupWallets,
@@ -117,7 +114,8 @@ export const BackupPreviewDialog = memo<BackupPreviewDialogProps>(function Backu
117114
excludeWallet: !backupWallets,
118115
})
119116

120-
const encrypted = await encryptBackup(encode(account + data.backupPassword), encode(file))
117+
const password = encryptWithAccount ? account + data.backupPassword : data.backupPassword
118+
const encrypted = await encryptBackup(encode(password), encode(file))
121119
const controller = new AbortController()
122120
controllerRef.current = controller
123121
await onUpload?.(encrypted, controller.signal)
@@ -132,7 +130,18 @@ export const BackupPreviewDialog = memo<BackupPreviewDialogProps>(function Backu
132130
if ((error as any).status === 400) navigate(DashboardRoutes.BackupCloud, { replace: true })
133131
}
134132
},
135-
[code, hasPassword, backupWallets, abstract, code, account, type, _, navigate, updateUser],
133+
[
134+
backupWallets,
135+
hasPassword,
136+
encryptWithAccount,
137+
account,
138+
onUpload,
139+
showSnackbar,
140+
onClose,
141+
setError,
142+
_,
143+
navigate,
144+
],
136145
)
137146

138147
const handleClose = useCallback(() => {
@@ -207,19 +216,21 @@ export const BackupPreviewDialog = memo<BackupPreviewDialogProps>(function Backu
207216
</Box>
208217
: <LoadingStatus minHeight={320} />
209218
}, [
219+
uploadLoading,
220+
classes.container,
221+
classes.icon,
210222
loading,
211223
previewInfo,
212224
control,
213-
_,
214-
errors.backupPassword?.message,
215-
errors.paymentPassword?.message,
216225
backupWallets,
217226
setBackupWallets,
227+
hasPassword,
218228
isUpload,
219-
theme,
220-
value,
221-
uploadLoading,
222-
classes,
229+
theme.palette.maskColor.danger,
230+
_,
231+
errors.backupPassword?.message,
232+
errors.paymentPassword?.message,
233+
clearErrors,
223234
])
224235

225236
const action = useMemo(() => {
@@ -246,18 +257,16 @@ export const BackupPreviewDialog = memo<BackupPreviewDialogProps>(function Backu
246257
</ActionButton>
247258
)
248259
}, [
249-
backupWallets,
250-
isUpload,
251-
isDirty,
252-
isValid,
253-
hasPassword,
254-
backupWallets,
255260
value,
261+
onClose,
256262
uploadLoading,
257263
handleClose,
258264
handleSubmit,
259265
handleUploadBackup,
260-
onClose,
266+
isUpload,
267+
isDirty,
268+
isValid,
269+
uploadButtonLabel,
261270
])
262271

263272
return (

packages/mask/dashboard/modals/RestoreBackupModal/RestoreBackupDialog.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ export interface RestoreBackupDialogProps {
4949
strategy?: 'import' | 'merge'
5050
restoreSuccessMessage?: ReactNode
5151
restoreErrorMessage?: ReactNode
52+
/**
53+
* MaskNetwork backup use account + password as password since legacy version.
54+
* Will be removed in the future when we remove MaskNetwork backup.
55+
*/
56+
decryptWithAccount: boolean
5257
/**
5358
* A generator that yield progress of download,
5459
* and return the content of the downloaded file at the end
@@ -61,6 +66,7 @@ export const RestoreBackupDialog = memo<RestoreBackupDialogProps>(function Resto
6166
open,
6267
fileName,
6368
account,
69+
decryptWithAccount,
6470
uploadedAt,
6571
size,
6672
strategy = 'import',
@@ -96,13 +102,14 @@ export const RestoreBackupDialog = memo<RestoreBackupDialogProps>(function Resto
96102
handleClose()
97103
throw err
98104
}
99-
}, [handleClose, open, download])
105+
}, [open, download, showSnackbar, handleClose])
100106

101107
const isImport = strategy === 'import'
102108
const [{ loading }, handleRestore] = useAsyncFn(async () => {
103109
try {
104110
if (!encrypted) return
105-
const decrypted = await decryptBackup(encode(account + backupPassword), encrypted)
111+
const password = decryptWithAccount ? account + backupPassword : backupPassword
112+
const decrypted = await decryptBackup(encode(password), encrypted)
106113
const backupJson = JSON.stringify(decode(decrypted))
107114
const summary = await Services.Backup.generateBackupSummary(backupJson)
108115
if (summary.isErr()) {
@@ -141,13 +148,14 @@ export const RestoreBackupDialog = memo<RestoreBackupDialogProps>(function Resto
141148
}
142149
}, [
143150
encrypted,
144-
backupPassword,
151+
decryptWithAccount,
145152
account,
146-
isImport,
153+
backupPassword,
147154
showSnackbar,
155+
isImport,
148156
restoreSuccessMessage,
149-
restoreErrorMessage,
150157
onClose,
158+
restoreErrorMessage,
151159
])
152160

153161
return (
@@ -190,7 +198,7 @@ export const RestoreBackupDialog = memo<RestoreBackupDialogProps>(function Resto
190198
fontSize={12}
191199
lineHeight="16px"
192200
color={theme.palette.maskColor.third}>
193-
{formatDateTime(new Date(Number(uploadedAt)), 'yyyy-mm-dd HH:mm')}
201+
{formatDateTime(new Date(Number(uploadedAt)), 'yyyy-MM-dd HH:mm')}
194202
</Typography>
195203
</>
196204
}

packages/mask/dashboard/pages/SetupPersona/Backup/Cloud/GoogleDrive.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Trans } from '@lingui/react/macro'
22
import { Icons } from '@masknet/icons'
3-
import { BackupAccountType, EMPTY_LIST } from '@masknet/shared-base'
3+
import { EMPTY_LIST } from '@masknet/shared-base'
44
import { ActionButton, makeStyles } from '@masknet/theme'
55
import { GoogleDriveClient, type DriveFile } from '@masknet/web3-providers'
66
import { Box, Typography } from '@mui/material'
@@ -89,7 +89,7 @@ export const Component = memo(function GoogleDriveBackup() {
8989
})
9090
refetch()
9191
},
92-
[googleDriveClient],
92+
[googleDriveClient, refetch],
9393
)
9494

9595
const [{ loading: logoutLoading }, logout] = useAsyncFn(async () => {
@@ -98,7 +98,7 @@ export const Component = memo(function GoogleDriveBackup() {
9898
googleAccount: '',
9999
googleToken: '',
100100
})
101-
}, [googleDriveClient])
101+
}, [googleDriveClient, updateUser])
102102

103103
const mergedFiles = useMemo(() => uniqBy(compact([...files, uploadedFile]), (x) => x.id), [files, uploadedFile])
104104

@@ -108,6 +108,7 @@ export const Component = memo(function GoogleDriveBackup() {
108108

109109
const downloadAndMerge = async (file: DriveFile) => {
110110
await RestoreBackupModal.openAndWaitForClose({
111+
decryptWithAccount: false,
111112
strategy: 'merge',
112113
download: () => {
113114
return progressDownload(() => googleDriveClient.requestFile(file.id), file.size ? +file.size : 0)
@@ -162,8 +163,7 @@ export const Component = memo(function GoogleDriveBackup() {
162163
onClick={() => {
163164
if (!user.googleAccount) return
164165
BackupPreviewModal.open({
165-
code: 'google-drive',
166-
type: BackupAccountType.Email,
166+
encryptWithAccount: false,
167167
account: user.googleAccount!,
168168
isUpload: true,
169169
title: <Trans>Backup to Google Drive</Trans>,

packages/mask/dashboard/pages/SetupPersona/Backup/Cloud/Preview.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Trans } from '@lingui/react/macro'
22
import { Icons } from '@masknet/icons'
33
import { EmptyStatus, formatFileSize } from '@masknet/shared'
4-
import type { BackupAccountType } from '@masknet/shared-base'
54
import { DashboardRoutes } from '@masknet/shared-base'
65
import { ActionButton, TextOverflowTooltip, makeStyles } from '@masknet/theme'
76
import { Box, Button, Typography } from '@mui/material'
@@ -84,6 +83,7 @@ export const Component = memo(function CloudBackupPreview() {
8483
const [{ loading: mergeLoading }, handleMergeClick] = useAsyncFn(async () => {
8584
if (!previewInfo.downloadLink || !previewInfo.account || !previewInfo.size || !previewInfo.uploadedAt) return
8685
await RestoreBackupModal.openAndWaitForClose({
86+
decryptWithAccount: true,
8787
strategy: 'merge',
8888
download: () => progressDownload(previewInfo.downloadLink),
8989
fileName: getFileName(previewInfo.downloadLink) || createBackupName(),
@@ -96,10 +96,8 @@ export const Component = memo(function CloudBackupPreview() {
9696
const handleBackupClick = useCallback(() => {
9797
if (!previewInfo.type || !previewInfo.account || !previewInfo.code) return
9898
BackupPreviewModal.open({
99+
encryptWithAccount: true,
99100
isUpload: false,
100-
code: previewInfo.code,
101-
abstract: previewInfo.abstract ? previewInfo.abstract : undefined,
102-
type: previewInfo.type as BackupAccountType,
103101
account: previewInfo.account,
104102
})
105103
}, [previewInfo])

packages/mask/dashboard/pages/SetupPersona/CloudBackupPreview/index.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Trans } from '@lingui/react/macro'
22
import { Icons } from '@masknet/icons'
33
import { EmptyStatus, formatFileSize } from '@masknet/shared'
4-
import type { BackupAccountType } from '@masknet/shared-base'
54
import { DashboardRoutes } from '@masknet/shared-base'
65
import { ActionButton, TextOverflowTooltip, makeStyles } from '@masknet/theme'
76
import { Box, Typography } from '@mui/material'
@@ -79,6 +78,7 @@ export const Component = memo(function CloudBackupPreview() {
7978
)
8079
return
8180
await RestoreBackupModal.openAndWaitForClose({
81+
decryptWithAccount: true,
8282
strategy: 'merge',
8383
download: () => progressDownload(previewInfo.downloadLink),
8484
fileName: getFileName(previewInfo.downloadLink) || createBackupName(),
@@ -91,10 +91,8 @@ export const Component = memo(function CloudBackupPreview() {
9191
const handleBackupClick = useCallback(() => {
9292
if (!previewInfo.type || !previewInfo.account || !previewInfo.code) return
9393
BackupPreviewModal.open({
94+
encryptWithAccount: true,
9495
isUpload: false,
95-
code: previewInfo.code,
96-
abstract: previewInfo.abstract ? previewInfo.abstract : undefined,
97-
type: previewInfo.type as BackupAccountType,
9896
account: previewInfo.account,
9997
})
10098
}, [previewInfo])
@@ -113,10 +111,8 @@ export const Component = memo(function CloudBackupPreview() {
113111
if (!previewInfo.type || !previewInfo.account || !previewInfo.code) return
114112

115113
BackupPreviewModal.open({
114+
encryptWithAccount: true,
116115
isUpload: true,
117-
code: previewInfo.code,
118-
abstract: previewInfo.abstract ? previewInfo.abstract : undefined,
119-
type: previewInfo.type as BackupAccountType,
120116
account: previewInfo.account,
121117
})
122118
},

packages/mask/dashboard/pages/SetupPersona/Recovery/Cloud/GoogleDrive.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export const Component = memo(function GoogleDriveRecovery() {
8888

8989
const downloadAndMerge = async (file: DriveFile) => {
9090
await RestoreBackupModal.openAndWaitForClose({
91+
decryptWithAccount: false,
9192
strategy: 'merge',
9293
download: () => {
9394
return progressDownload(() => googleDriveClient.requestFile(file.id), file.size ? +file.size : 0)
@@ -125,14 +126,12 @@ export const Component = memo(function GoogleDriveRecovery() {
125126
selectable
126127
selectedFileId={selectedFile?.id}
127128
onSelect={setSelectedFile}
128-
onDownload={downloadAndMerge}
129-
onMerge={async (file) => {
129+
onMerge={downloadAndMerge}
130+
onDownload={async (file) => {
130131
const blob = await googleDriveClient.downloadFile(file.id)
131132
const url = URL.createObjectURL(blob)
132133
downloadBackup(url, file.name)
133-
Promise.resolve().then(() => {
134-
URL.revokeObjectURL(url)
135-
})
134+
URL.revokeObjectURL(url)
136135
}}
137136
/>
138137
</Box>
@@ -146,6 +145,7 @@ export const Component = memo(function GoogleDriveRecovery() {
146145
onClick={async () => {
147146
if (!user.googleAccount || !selectedFile?.id) return
148147
const result = await RestoreBackupModal.openAndWaitForClose({
148+
decryptWithAccount: false,
149149
download: () => {
150150
return progressDownload(
151151
() => googleDriveClient.requestFile(selectedFile.id),

0 commit comments

Comments
 (0)