From f4267fff01670a846f3a0b9ac886320046c7c454 Mon Sep 17 00:00:00 2001 From: First-Terraner Date: Wed, 27 Mar 2024 21:37:18 +0100 Subject: [PATCH] Add keysetId to Recovering screen --- src/components/hooks/Restore.tsx | 61 +++++++++++++++++++----------- src/model/nav.ts | 3 ++ src/screens/Dashboard.tsx | 8 ++++ src/screens/Restore/Recover.tsx | 5 ++- src/screens/Restore/Recovering.tsx | 45 ++++++++++++---------- 5 files changed, 79 insertions(+), 43 deletions(-) diff --git a/src/components/hooks/Restore.tsx b/src/components/hooks/Restore.tsx index 176fdd6d..4e0295f7 100644 --- a/src/components/hooks/Restore.tsx +++ b/src/components/hooks/Restore.tsx @@ -18,25 +18,26 @@ type StackNavigation = NavigationProp type TRestoreInterval = Promise<{ proofs: Proof[]; newKeys?: MintKeys; lastCount: number } | undefined> interface IUseRestoreProps { + from?: number + to?: number mintUrl: string + keysetId: string mnemonic: string comingFromOnboarding?: boolean } -const defaultRestoreState = { - proofs: [] as Proof[], - from: 0, - to: RESTORE_INTERVAL, - overshoot: 0, -} - -export function useRestore({ mintUrl, mnemonic, comingFromOnboarding }: IUseRestoreProps) { +export function useRestore({ from, to, mintUrl, keysetId, mnemonic, comingFromOnboarding }: IUseRestoreProps) { const navigation = useNavigation() const { t } = useTranslation([NS.common]) const { openPromptAutoClose } = usePromptContext() - const [restored, setRestored] = useState({ ...defaultRestoreState }) + const [restored, setRestored] = useState({ + proofs: [] as Proof[], + start: from ?? 0, + end: to ?? RESTORE_INTERVAL, + overshoot: 0, + }) useEffect(() => { const restore = async () => { @@ -45,7 +46,12 @@ export function useRestore({ mintUrl, mnemonic, comingFromOnboarding }: IUseRest const proofs = await restoreWallet(mintUrl, mnemonic) if (!proofs?.length) { openPromptAutoClose({ msg: t('noProofsRestored'), success: false }) - setRestored({ ...defaultRestoreState }) + setRestored({ + proofs: [] as Proof[], + start: from ?? 0, + end: to ?? RESTORE_INTERVAL, + overshoot: 0, + }) if (comingFromOnboarding) { return navigation.navigate('auth', { pinHash: '' }) } @@ -58,7 +64,12 @@ export function useRestore({ mintUrl, mnemonic, comingFromOnboarding }: IUseRest type: 4, value: '', }) - setRestored({ ...defaultRestoreState }) + setRestored({ + proofs: [] as Proof[], + start: from ?? 0, + end: to ?? RESTORE_INTERVAL, + overshoot: 0, + }) navigation.navigate('success', { mint: mintUrl, amount: bal, @@ -67,7 +78,12 @@ export function useRestore({ mintUrl, mnemonic, comingFromOnboarding }: IUseRest }) } catch (e) { l('[handleRecovery] error: ', e) - setRestored({ ...defaultRestoreState }) + setRestored({ + proofs: [] as Proof[], + start: from ?? 0, + end: to ?? RESTORE_INTERVAL, + overshoot: 0, + }) navigation.navigate('processingError', { errorMsg: isErr(e) ? e.message : t('restoreErr'), comingFromOnboarding, @@ -77,8 +93,7 @@ export function useRestore({ mintUrl, mnemonic, comingFromOnboarding }: IUseRest const restoreWallet = async (mintUrl: string, mnemonic: string) => { try { const { wallet, seed } = await getSeedWalletByMnemonic({ mintUrl, mnemonic }) - // TODO get previous keysets from mint and try to restore from them - const resp = await restoreInterval(wallet, 0, RESTORE_INTERVAL) + const resp = await restoreInterval(wallet, from ?? 0, to ?? RESTORE_INTERVAL) if (!resp) { l('[restoreWallet] restore interval did not return a proper object!') throw new Error('[restoreWallet] restore interval did not return a proper object!') @@ -102,29 +117,29 @@ export function useRestore({ mintUrl, mnemonic, comingFromOnboarding }: IUseRest } const restoreInterval = async ( wallet: CashuWallet, - from: number, - to: number, + start: number, + end: number, restoredProofs: Proof[] = [], overshoot: number = 0 ): TRestoreInterval => { try { - setRestored({ proofs: restoredProofs, from, to, overshoot }) - const { proofs, newKeys } = await wallet.restore(from, to) - from += RESTORE_INTERVAL - to += RESTORE_INTERVAL + setRestored({ proofs: restoredProofs, start, end, overshoot }) + const { proofs, newKeys } = await wallet.restore(start, end, keysetId) + start += RESTORE_INTERVAL + end += RESTORE_INTERVAL if (proofs.length) { l('[restoreInterval] restored proofs: ', { from, to, proofsLength: proofs.length }) restoredProofs.push(...proofs) overshoot = 0 - return restoreInterval(wallet, from, to, restoredProofs, overshoot) + return restoreInterval(wallet, start, end, restoredProofs, overshoot) } if (overshoot < RESTORE_OVERSHOOT) { l('[restoreInterval] no proofs to restore! overshooting now: ', { from, to, proofsLength: proofs.length, overshoot }) overshoot++ - return restoreInterval(wallet, from, to, restoredProofs, overshoot) + return restoreInterval(wallet, start, end, restoredProofs, overshoot) } l('[restoreInterval] no proofs to restore! overshooting limit reached: ', { from, to, restoredProofs: restoredProofs.length, overshoot }) - return { proofs: restoredProofs, newKeys, lastCount: to } + return { proofs: restoredProofs, newKeys, lastCount: end } } catch (e) { l('[restoreInterval] error', { e }) } diff --git a/src/model/nav.ts b/src/model/nav.ts index d513f067..638a0783 100644 --- a/src/model/nav.ts +++ b/src/model/nav.ts @@ -236,7 +236,10 @@ export type RootStackParamList = { comingFromOnboarding?: boolean } Recovering: { + from?: number + to?: number mintUrl: string + keysetId: string mnemonic: string comingFromOnboarding?: boolean } diff --git a/src/screens/Dashboard.tsx b/src/screens/Dashboard.tsx index 6f0e0626..9c4bf19f 100644 --- a/src/screens/Dashboard.tsx +++ b/src/screens/Dashboard.tsx @@ -308,6 +308,14 @@ export default function Dashboard({ navigation, route }: TDashboardPageProps) { color={hi[highlight]} onPress={() => { setModal(prev => ({ ...prev, sendOpts: true })) + // navigation.navigate('Recovering', { + // from: 500, + // to: 550, + // mintUrl: 'https://testnut.cashu.space', + // keysetId: 'asfdafh8u2h3', + // mnemonic: '', + // comingFromOnboarding: false, + // }) }} /> : diff --git a/src/screens/Restore/Recover.tsx b/src/screens/Restore/Recover.tsx index 5c56ce04..98f2af39 100644 --- a/src/screens/Restore/Recover.tsx +++ b/src/screens/Restore/Recover.tsx @@ -7,6 +7,7 @@ import TxtInput from '@comps/TxtInput' import { isIOS } from '@consts' import type { IRecoverPageProps } from '@model/nav' import { NS } from '@src/i18n' +import { getMintCurrentKeySetId } from '@src/wallet' import { createRef, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import { KeyboardAvoidingView, type TextInput, View } from 'react-native' @@ -19,10 +20,12 @@ export default function RecoverScreen({ navigation, route }: IRecoverPageProps) const { loading } = useLoading() const inputRef = createRef() - const handleBtnPress = () => { + const handleBtnPress = async () => { if (loading || !input.length) { return } + const keysetId = await getMintCurrentKeySetId(route.params.mintUrl) navigation.navigate('Recovering', { mintUrl: route.params.mintUrl, + keysetId, mnemonic: input, comingFromOnboarding: route.params.comingFromOnboarding, }) diff --git a/src/screens/Restore/Recovering.tsx b/src/screens/Restore/Recovering.tsx index 328897d5..4987390a 100644 --- a/src/screens/Restore/Recovering.tsx +++ b/src/screens/Restore/Recovering.tsx @@ -15,11 +15,11 @@ import { s, ScaledSheet } from 'react-native-size-matters' export default function RecoveringScreen({ navigation, route }: IRecoveringPageProps) { - const { mintUrl, mnemonic, comingFromOnboarding } = route.params + const { from, to, mintUrl, keysetId, mnemonic, comingFromOnboarding } = route.params const { t } = useTranslation([NS.common]) // Seed recovery process in useRestore hook - const { proofs, from, to, overshoot } = useRestore({ mintUrl, mnemonic, comingFromOnboarding }) + const { proofs, start, end, overshoot } = useRestore({ from, to, mintUrl, keysetId, mnemonic, comingFromOnboarding }) const { color } = useThemeContext() @@ -37,6 +37,24 @@ export default function RecoveringScreen({ navigation, route }: IRecoveringPageP styles={[styles.descText]} txt={t('recoveringWallet')} /> + 0} + styles={[styles.hint, { color: overshoot > 0 ? mainColors.VALID : mainColors.WARN, marginBottom: s(40) }]} + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + txt={overshoot > 0 ? `${t('doneSafety')} ${overshoot}/${RESTORE_OVERSHOOT}` : t('dontClose')} + /> + + + + @@ -56,16 +74,10 @@ export default function RecoveringScreen({ navigation, route }: IRecoveringPageP /> acc + p.amount, 0))})`} + txt={`${proofs.length} ${t('proofs', { ns: NS.wallet })} (${formatSatStr(proofs.reduce((acc, p) => acc + p.amount, 0))})`} /> - 0} - styles={[styles.hint, { color: overshoot > 0 ? mainColors.VALID : mainColors.WARN, marginTop: s(40) }]} - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - txt={overshoot > 0 ? `${t('doneSafety')} ${overshoot}/${RESTORE_OVERSHOOT}` : t('dontClose')} - /> + ) } @@ -78,18 +90,13 @@ const styles = ScaledSheet.create({ paddingHorizontal: '20@s', }, descText: { - marginTop: '20@s', - marginBottom: '30@s', + marginVertical: '20@s', textAlign: 'center', fontSize: '20@s', }, - warn: { - marginTop: '10@s', - marginBottom: '40@s', - }, hint: { - fontSize: '12@s', - marginTop: '10@s', + fontSize: '14@s', + marginBottom: '10@s', }, progress: { width: '100%',