From 25885f9c377d9994c22a3248f8540a9f20e7854b Mon Sep 17 00:00:00 2001 From: heisenberg Date: Wed, 18 Dec 2024 11:37:07 +0800 Subject: [PATCH] fix: address list error when importing more addresses after creating a mnemonic --- .../components/HDSetting/SettingHDKeyring.tsx | 33 ++++++++++++------- .../ImportMoreAddress/ImportMoreAddress.tsx | 24 ++++++++++---- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/apps/mobile/src/components/HDSetting/SettingHDKeyring.tsx b/apps/mobile/src/components/HDSetting/SettingHDKeyring.tsx index 3e7674dde..87afb2b39 100644 --- a/apps/mobile/src/components/HDSetting/SettingHDKeyring.tsx +++ b/apps/mobile/src/components/HDSetting/SettingHDKeyring.tsx @@ -2,16 +2,14 @@ import { LedgerHDPathType } from '@rabby-wallet/eth-keyring-ledger/dist/utils'; import React from 'react'; import { useTranslation } from 'react-i18next'; import { MainContainer, settingAtom } from './MainContainer'; -import { requestKeyring } from '@/core/apis/keyring'; -import { KEYRING_CLASS } from '@rabby-wallet/keyring-utils'; import { useAtom } from 'jotai'; +import { apiMnemonic } from '@/core/apis'; export const SettingHDKeyring: React.FC<{ onDone: () => void; mnemonics: string; passphrase: string; - keyringId: number; -}> = ({ onDone, keyringId }) => { +}> = ({ onDone, mnemonics, passphrase }) => { const { t } = useTranslation(); const [setting, setSetting] = useAtom(settingAtom); @@ -45,19 +43,30 @@ export const SettingHDKeyring: React.FC<{ [t], ); + const mnemonicKeyringRef = React.useRef< + ReturnType | undefined + >(undefined); + const getMnemonicKeyring = React.useCallback(() => { + if (mnemonics) { + if (!mnemonicKeyringRef.current) { + mnemonicKeyringRef.current = apiMnemonic.getKeyringByMnemonic( + mnemonics!, + passphrase!, + ); + } + return mnemonicKeyringRef.current; + } + return undefined; + }, [mnemonics, passphrase]); + const handleConfirm = React.useCallback( async value => { - await requestKeyring( - KEYRING_CLASS.MNEMONIC, - 'setHDPathType', - keyringId || null, - value.hdPath, - ); - + const keyring = await getMnemonicKeyring(); + await keyring?.setHDPathType(value.hdPath); setSetting(value); onDone?.(); }, - [keyringId, onDone, setSetting], + [getMnemonicKeyring, onDone, setSetting], ); return ( diff --git a/apps/mobile/src/components2024/ImportMoreAddress/ImportMoreAddress.tsx b/apps/mobile/src/components2024/ImportMoreAddress/ImportMoreAddress.tsx index 7789b3886..994d67f18 100644 --- a/apps/mobile/src/components2024/ImportMoreAddress/ImportMoreAddress.tsx +++ b/apps/mobile/src/components2024/ImportMoreAddress/ImportMoreAddress.tsx @@ -133,10 +133,7 @@ export const ImportMoreAddress: React.FC = ({ params, onCancel }) => { async (index: number) => { const res = params.type === KEYRING_TYPE.HdKeyring - ? await apiKeyring.requestKeyring( - KEYRING_TYPE.HdKeyring, - 'getAddresses', - params?.keyringId ?? null, + ? await getMnemonicKeyring()?.getAddresses( index, index + stepCountRef.current, ) @@ -171,7 +168,7 @@ export const ImportMoreAddress: React.FC = ({ params, onCancel }) => { }); } }, - [apiHD, params?.keyringId, params.type], + [apiHD, getMnemonicKeyring, params.type], ); const handleLoadAddress = React.useCallback(async () => { @@ -384,9 +381,22 @@ export const ImportMoreAddress: React.FC = ({ params, onCancel }) => { onDone: () => { removeGlobalBottomSheetModal2024(id); }, - ...(params.type ? { keyringId: params.keyringId } : {}), + ...(params.type + ? { + keyringId: params.keyringId, + mnemonics: params.mnemonics, + passphrase: params.passphrase, + } + : {}), }); - }, [params.brand, params.keyringId, params.type, settingModalName]); + }, [ + params.brand, + params.keyringId, + params.mnemonics, + params.passphrase, + params.type, + settingModalName, + ]); return (