Skip to content

Commit

Permalink
Add keysetId to Recovering screen
Browse files Browse the repository at this point in the history
  • Loading branch information
KKA11010 committed Mar 27, 2024
1 parent aaf0a8a commit d746518
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 44 deletions.
61 changes: 38 additions & 23 deletions src/components/hooks/Restore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,26 @@ type StackNavigation = NavigationProp<RootStackParamList>
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<StackNavigation>()
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 () => {
Expand All @@ -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: '' })
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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!')
Expand All @@ -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 })
}
Expand Down
3 changes: 3 additions & 0 deletions src/model/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ export type RootStackParamList = {
comingFromOnboarding?: boolean
}
Recovering: {
from?: number
to?: number
mintUrl: string
keysetId: string
mnemonic: string
comingFromOnboarding?: boolean
}
Expand Down
12 changes: 11 additions & 1 deletion src/screens/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,17 @@ export default function Dashboard({ navigation, route }: TDashboardPageProps) {
icon={<SendIcon width={s(32)} height={vs(32)} color={hi[highlight]} />}
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,
// })
}}
/>
:
<ActionBtn
Expand Down
5 changes: 4 additions & 1 deletion src/screens/Restore/Recover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -19,10 +20,12 @@ export default function RecoverScreen({ navigation, route }: IRecoverPageProps)
const { loading } = useLoading()
const inputRef = createRef<TextInput>()

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,
})
Expand Down
45 changes: 26 additions & 19 deletions src/screens/Restore/Recovering.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -37,6 +37,24 @@ export default function RecoveringScreen({ navigation, route }: IRecoveringPageP
styles={[styles.descText]}
txt={t('recoveringWallet')}
/>
<Txt
center
bold={overshoot > 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')}
/>
<View style={styles.progress}>
<Txt
bold
styles={[styles.hint, { color: color.TEXT_SECONDARY }]}
txt='Keyset-ID'
/>
<Txt
styles={[styles.hint, { color: color.TEXT_SECONDARY }]}
txt={keysetId}
/>
</View>
<View style={styles.progress}>
<Txt
bold
Expand All @@ -45,7 +63,7 @@ export default function RecoveringScreen({ navigation, route }: IRecoveringPageP
/>
<Txt
styles={[styles.hint, { color: color.TEXT_SECONDARY }]}
txt={`${from} ${t('to')} ${to}`}
txt={`${start} ${t('to')} ${end}`}
/>
</View>
<View style={styles.progress}>
Expand All @@ -56,16 +74,10 @@ export default function RecoveringScreen({ navigation, route }: IRecoveringPageP
/>
<Txt
styles={[styles.hint, { color: color.TEXT_SECONDARY }]}
txt={`${proofs.length} (${formatSatStr(proofs.reduce((acc, p) => acc + p.amount, 0))})`}
txt={`${proofs.length} ${t('proofs', { ns: NS.wallet })} (${formatSatStr(proofs.reduce((acc, p) => acc + p.amount, 0))})`}
/>
</View>
<Txt
center
bold={overshoot > 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')}
/>

</View>
)
}
Expand All @@ -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%',
Expand Down

0 comments on commit d746518

Please sign in to comment.