Skip to content

Commit

Permalink
fix: Same wallet import optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
devchenyan committed Jan 1, 2024
1 parent 9d8a530 commit 4b4c132
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Dialog from 'widgets/Dialog'
import RadioGroup from 'widgets/RadioGroup'
import { useState as useGlobalState, useDispatch, AppActions } from 'states'
import { requestPassword } from 'services/remote'
import PasswordRequest from 'components/PasswordRequest'
import styles from './detectDuplicateWalletDialog.module.scss'

const DetectDuplicateWalletDialog = ({ onClose }: { onClose: () => void }) => {
Expand Down Expand Up @@ -84,35 +83,32 @@ const DetectDuplicateWalletDialog = ({ onClose }: { onClose: () => void }) => {
}, [wallets, deletableWallets, requestPassword, onClose, dispatch])

return (
<>
<Dialog
show
title={t('settings.wallet-manager.detected-duplicate.title')}
onCancel={onClose}
onConfirm={onConfirm}
disabled={deletableWallets.length === 0}
>
<div className={styles.content}>
<p className={styles.detail}>{t('settings.wallet-manager.detected-duplicate.detail')}</p>
<div className={styles.groupWrap}>
{groups.map(group => (
<RadioGroup
inputIdPrefix="detect-duplicate-wallet"
key={group[0].extendedKey}
defaultValue=""
onChange={handleGroupChange}
itemClassName={styles.radioItem}
options={group.map(wallet => ({
value: `${wallet.extendedKey}_${wallet.id}`,
label: <span className={styles.walletName}>{wallet.name}</span>,
}))}
/>
))}
</div>
<Dialog
show
title={t('settings.wallet-manager.detected-duplicate.title')}
onCancel={onClose}
onConfirm={onConfirm}
disabled={deletableWallets.length === 0}
>
<div className={styles.content}>
<p className={styles.detail}>{t('settings.wallet-manager.detected-duplicate.detail')}</p>
<div className={styles.groupWrap}>
{groups.map(group => (
<RadioGroup
inputIdPrefix="detect-duplicate-wallet"
key={group[0].extendedKey}
defaultValue=""
onChange={handleGroupChange}
itemClassName={styles.radioItem}
options={group.map(wallet => ({
value: `${wallet.extendedKey}_${wallet.id}`,
label: <span className={styles.walletName}>{wallet.name}</span>,
}))}
/>
))}
</div>
</Dialog>
<PasswordRequest />
</>
</div>
</Dialog>
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import styles from './replaceDuplicateWalletDialog.module.scss'

const useReplaceDuplicateWallet = () => {
const [extendedKey, setExtendedKey] = useState('')
const [tmpId, setTmpId] = useState('')
const [importedWalletId, setImportedWalletId] = useState('')

const onClose = useCallback(() => {
setTmpId('')
}, [setTmpId])
setImportedWalletId('')
}, [setImportedWalletId])

const onImportingExitingWalletError = (
message:
Expand All @@ -29,22 +29,22 @@ const useReplaceDuplicateWallet = () => {
if (msg) {
const obj = JSON.parse(msg)
setExtendedKey(obj.extendedKey)
setTmpId(obj.id)
setImportedWalletId(obj.id)
}
} catch (error) {
onClose()
}
}

const show = useMemo(() => !!extendedKey && !!tmpId, [tmpId, extendedKey])
const show = useMemo(() => !!extendedKey && !!importedWalletId, [importedWalletId, extendedKey])

return {
onImportingExitingWalletError,
dialogProps: {
show,
onClose,
extendedKey,
tmpId,
importedWalletId,
},
}
}
Expand All @@ -53,12 +53,12 @@ const ReplaceDuplicateWalletDialog = ({
show,
onClose,
extendedKey,
tmpId,
importedWalletId,
}: {
show: boolean
onClose: () => void
extendedKey: string
tmpId: string
importedWalletId: string
}) => {
const {
settings: { wallets = [] },
Expand All @@ -79,8 +79,8 @@ const ReplaceDuplicateWalletDialog = ({

const onConfirm = useCallback(async () => {
replaceWallet({
id: selectedId,
tmpId,
existingWalletId: selectedId,
importedWalletId,
})
.then(res => {
if (isSuccessResponse(res)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-ui/src/types/Controller/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ declare namespace Controller {
}

interface ReplaceWalletParams {
id: string
tmpId: string
existingWalletId: string
importedWalletId: string
}

interface BackupWalletParams {
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-ui/src/widgets/RadioGroup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export interface RadioGroupOptions {
export interface RadioGroupProps {
options: RadioGroupOptions[]
onChange?: (arg: string) => void
defaultValue?: string | number
value?: string | number
defaultValue?: string
value?: string
itemClassName?: string
className?: string
inputIdPrefix?: string
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/controllers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ export default class ApiController {
return this.#walletsController.delete({ id, password })
})

handle('replace-wallet', async (_, { id = '', tmpId = '' }) => {
return this.#walletsController.replaceWallet(id, tmpId)
handle('replace-wallet', async (_, { existingWalletId = '', importedWalletId = '' }) => {
return this.#walletsController.replaceWallet(existingWalletId, importedWalletId)
})

handle('backup-wallet', async (_, { id = '', password = '' }) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-wallet/src/controllers/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,9 @@ export default class WalletsController {
}
}

public async replaceWallet(id: string, tmpId: string): Promise<Controller.Response<any>> {
public async replaceWallet(existingWalletId: string, importedWalletId: string): Promise<Controller.Response<any>> {
const walletsService = WalletsService.getInstance()
await walletsService.replace(id, tmpId)
await walletsService.replace(existingWalletId, importedWalletId)

return {
status: ResponseCode.Success,
Expand Down
35 changes: 15 additions & 20 deletions packages/neuron-wallet/src/services/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export default class WalletService {
private listStore: Store // Save wallets (meta info except keystore, which is persisted separately)
private walletsKey = 'wallets'
private currentWalletKey = 'current'
private tmpWallet: Wallet | undefined
private importedWallet: Wallet | undefined

public static getInstance = () => {
if (!WalletService.instance) {
Expand Down Expand Up @@ -384,7 +384,7 @@ export default class WalletService {
}

if (this.getAll().find(item => item.extendedKey === props.extendedKey)) {
this.tmpWallet = wallet
this.importedWallet = wallet
throw new ImportingExitingWallet(JSON.stringify({ extendedKey: props.extendedKey, id }))
}

Expand All @@ -394,35 +394,30 @@ export default class WalletService {
return wallet
}

public replace = async (id: string, tmpId: string) => {
const wallet = this.get(id)
if (!wallet || !this.tmpWallet) {
throw new WalletNotFound(id)
public replace = async (existingWalletId: string, importedWalletId: string) => {
const wallet = this.get(existingWalletId)
if (!wallet || !this.importedWallet) {
throw new WalletNotFound(existingWalletId)
}

const tmp = this.tmpWallet?.toJSON()
if (tmpId !== tmp.id) {
throw new WalletNotFound(tmpId)
const newWallet = this.importedWallet?.toJSON()
if (importedWalletId !== newWallet.id) {
throw new WalletNotFound(importedWalletId)
}
if (wallet.toJSON().extendedKey !== tmp.extendedKey) {
if (wallet.toJSON().extendedKey !== newWallet.extendedKey) {
throw new Error('The wallets are not the same and cannot be replaced.')
}

const wallets = this.getAll()

this.listStore.writeSync(this.walletsKey, [...wallets, tmp])
this.listStore.writeSync(this.walletsKey, [...wallets, newWallet])

const current = this.getCurrent()
const currentID = current ? current.id : ''
this.setCurrent(newWallet.id)

if (currentID === id) {
this.setCurrent(tmp.id)
}

await AddressService.deleteByWalletId(id)
await AddressService.deleteByWalletId(existingWalletId)

const newWallets = wallets.filter(w => w.id !== id)
this.listStore.writeSync(this.walletsKey, [...newWallets, tmp])
const newWallets = wallets.filter(w => w.id !== existingWalletId)
this.listStore.writeSync(this.walletsKey, [...newWallets, newWallet])

if (!wallet.isHardware()) {
wallet.deleteKeystore()
Expand Down

1 comment on commit 4b4c132

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 7378651605

Please sign in to comment.