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 bc33edb1..fc08c787 100644 --- a/src/model/nav.ts +++ b/src/model/nav.ts @@ -224,7 +224,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 5f3983a4..434691ae 100644 --- a/src/screens/Dashboard.tsx +++ b/src/screens/Dashboard.tsx @@ -325,7 +325,17 @@ export default function Dashboard({ navigation, route }: TDashboardPageProps) { icon={} txt={t('send', { ns: NS.wallet })} color={hi[highlight]} - onPress={() => setModal(prev => ({ ...prev, sendOpts: true }))} + onPress={() => { + setModal(prev => ({ ...prev, sendOpts: true })) + // navigation.navigate('Recovering', { + // from: 500, + // to: 550, + // mintUrl: 'https://testnut.cashu.space', + // keysetId: 'asfdafh8u2h3', + // mnemonic: '', + // comingFromOnboarding: false, + // }) + }} /> : () - 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%',