Skip to content

Commit

Permalink
Merge pull request #242 from CityOfZion/CU-86a5j1vwe
Browse files Browse the repository at this point in the history
CU-86a5j1vwe-NEON3 - Backup - Implement Backup feature for Swaps
  • Loading branch information
thiagocbalducci authored Nov 15, 2024
2 parents d7c644c + 4b47e9b commit 2dd4404
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { DateHelper } from '@renderer/helpers/DateHelper'
import { ToastHelper } from '@renderer/helpers/ToastHelper'
import { useAccountsSelector } from '@renderer/hooks/useAccountSelector'
import { useActions } from '@renderer/hooks/useActions'
import { useCurrentLoginSessionSelector } from '@renderer/hooks/useAuthSelector'
import { useCurrentLoginSessionSelector, useSwapRecordsSelector } from '@renderer/hooks/useAuthSelector'
import { useContactsSelector } from '@renderer/hooks/useContactSelector'
import { useModalNavigate, useModalState } from '@renderer/hooks/useModalRouter'
import { useWalletsSelector } from '@renderer/hooks/useWalletSelector'
Expand Down Expand Up @@ -46,6 +46,7 @@ export const ConfirmPasswordBackupModal = () => {
const { contacts } = useContactsSelector()
const { wallets } = useWalletsSelector()
const { accounts } = useAccountsSelector()
const { swapRecords } = useSwapRecordsSelector()
const { selectedFilePath } = useModalState<TLocationState>()
const { modalNavigate } = useModalNavigate()

Expand All @@ -69,7 +70,7 @@ export const ConfirmPasswordBackupModal = () => {
}

try {
const backupFile: TBackupFormat = { wallets: [], contacts }
const backupFile: TBackupFormat = { wallets: [], contacts, swapRecords }

backupFile.wallets = wallets.map(({ encryptedMnemonic, ...wallet }) => {
let mnemonic: string | undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import { useAccountUtils } from '@renderer/hooks/useAccountSelector'
import { useActions } from '@renderer/hooks/useActions'
import { useBlockchainActions } from '@renderer/hooks/useBlockchainActions'
import { useModalNavigate, useModalState } from '@renderer/hooks/useModalRouter'
import { useAppDispatch } from '@renderer/hooks/useRedux'
import { SideModalLayout } from '@renderer/layouts/SideModal'
import { authReducerActions } from '@renderer/store/reducers/AuthReducer'
import { TAccountsToImport, TBackupFormat } from '@shared/@types/blockchain'

type TFormData = {
Expand Down Expand Up @@ -40,6 +42,7 @@ export const ConfirmPasswordRecoverModal = () => {
const { doesAccountExist } = useAccountUtils()
const { modalNavigate } = useModalNavigate()
const { createContacts, createWallet, importAccounts } = useBlockchainActions()
const dispatch = useAppDispatch()

const { actionData, actionState, handleAct, setDataFromEventWrapper, setError, reset } = useActions<TFormData>({
password: '',
Expand All @@ -53,15 +56,18 @@ export const ConfirmPasswordRecoverModal = () => {

try {
const contentDecrypted = await window.api.sendAsync('decryptBasedSecret', { value: content, secret: password })
const backupFile = JSON.parse(contentDecrypted) as TBackupFormat
const backupFile = JSON.parse(contentDecrypted as string) as TBackupFormat

ApplicationDataHelper.convertTypes(backupFile.wallets)

if (!backupFile.swapRecords) backupFile.swapRecords = []

if (onDecrypt) {
onDecrypt(backupFile)
return
}

backupFile.swapRecords.forEach(swapRecord => dispatch(authReducerActions.persistSwapRecord(swapRecord)))
createContacts(backupFile.contacts)

const importPromises = backupFile.wallets.map(async wallet => {
Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/routes/modals/SwapDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const SwapDetailsModal = () => {
const response = await swapServiceHelper.getStatus(swapRecord.swapId)

const updatedSwapRecord: TSwapRecord = { ...swapRecord, swapStatus: response.status, txTo: response.txTo }

setSwapRecord(updatedSwapRecord)
dispatch(authReducerActions.persistSwapRecord(updatedSwapRecord))

Expand Down
9 changes: 7 additions & 2 deletions src/renderer/src/routes/pages/Welcome/ImportWallet/Step4.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import { Progress } from '@renderer/components/Progress'
import { ToastHelper } from '@renderer/helpers/ToastHelper'
import { UtilsHelper } from '@renderer/helpers/UtilsHelper'
import { useBlockchainActions } from '@renderer/hooks/useBlockchainActions'
import { useAppDispatch } from '@renderer/hooks/useRedux'
import { useSettingsActions } from '@renderer/hooks/useSettingsSelector'
import { authReducerActions } from '@renderer/store/reducers/AuthReducer'
import { TAccountsToImport, TWalletToCreate } from '@shared/@types/blockchain'
import { IContactState } from '@shared/@types/store'
import { IContactState, TSwapRecord } from '@shared/@types/store'

type TLocationState = {
wallets: (TWalletToCreate & {
accounts: TAccountsToImport
})[]
swapRecords?: TSwapRecord[]
contacts?: IContactState[]
password: string
}
Expand All @@ -24,20 +27,22 @@ export const WelcomeImportWalletStep4Page = () => {
const navigate = useNavigate()
const { createWallet, importAccounts, createContacts } = useBlockchainActions()
const { setHasPassword } = useSettingsActions()
const dispatch = useAppDispatch()

const isImporting = useRef(false)

const [progress, setProgress] = useState(0)

const handleImport = async () => {
try {
const { wallets, contacts, password } = state
const { wallets, contacts, password, swapRecords } = state
const progressByStep = 100 / wallets.length + 3

await setHasPassword(password)

setProgress(progress => progress + progressByStep)

if (swapRecords) swapRecords.forEach(swapRecord => dispatch(authReducerActions.persistSwapRecord(swapRecord)))
if (contacts) createContacts(contacts)

await UtilsHelper.sleep(1000)
Expand Down
3 changes: 2 additions & 1 deletion src/shared/@types/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BSEthereumNetworkId } from '@cityofzion/bs-ethereum'
import { BSNeoLegacyNetworkId } from '@cityofzion/bs-neo-legacy'
import { BSNeo3NetworkId } from '@cityofzion/bs-neo3'

import { IAccountState, IContactState, IWalletState, TAccountType, TSkin, TWalletType } from './store'
import { IAccountState, IContactState, IWalletState, TAccountType, TSkin, TSwapRecord, TWalletType } from './store'

export type TBlockchainServiceKey = 'neo3' | 'neoLegacy' | 'ethereum' | 'neox'
export type TBlockchainImageColor = 'white' | 'gray' | 'blue' | 'green'
Expand Down Expand Up @@ -66,6 +66,7 @@ export type TWalletBackupFormat = Omit<IWalletState, 'encryptedMnemonic'> & {
export type TBackupFormat = {
wallets: TWalletBackupFormat[]
contacts: IContactState[]
swapRecords: TSwapRecord[]
}

export type TAccountToEdit = {
Expand Down

0 comments on commit 2dd4404

Please sign in to comment.