Skip to content

Commit

Permalink
CR: updayte
Browse files Browse the repository at this point in the history
  • Loading branch information
banklesss committed Sep 3, 2024
1 parent 7e288f2 commit e8ec7b5
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {SafeAreaView} from 'react-native-safe-area-context'
import {Button, KeyboardAvoidingView, Spacer, TextInput} from '../../../components'
import globalMessages from '../../../kernel/i18n/global-messages'
import {isEmptyString} from '../../../kernel/utils'
import {getWalletNameError, validateWalletName} from '../../../yoroi-wallets/utils/validators'
import {getWalletNameError} from '../../../yoroi-wallets/utils/validators'
import {useSelectedWallet} from '../../WalletManager/common/hooks/useSelectedWallet'
import {useWalletManager} from '../../WalletManager/context/WalletManagerProvider'

Expand All @@ -23,9 +23,8 @@ export const RenameWalletScreen = () => {
} = useSelectedWallet()

const {walletManager} = useWalletManager()
const walletNames = Array.from(walletManager.walletMetas.values()).map(({name}) => name)
const [newWalletName, setNewWalletName] = React.useState(walletName)
const validationErrors = validateWalletName(newWalletName, walletName, walletNames)
const validationErrors = walletManager.validateWalletName(newWalletName, walletName)
const hasErrors = Object.keys(validationErrors).length > 0
const errorText = getWalletNameError(
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import {Blockies} from '@yoroi/identicon'
import {useTheme} from '@yoroi/theme'
import * as React from 'react'
import {StyleSheet, Text, View} from 'react-native'

import {Button, Icon} from '../../../../components'
import {Space} from '../../../../components/Space/Space'
import {useWalletNavigation} from '../../../../kernel/navigation'
import {useWalletManager} from '../../../WalletManager/context/WalletManagerProvider'
import {useStrings} from '../useStrings'

export const WalletDuplicatedModal = ({publicKeyHex}: {publicKeyHex: string}) => {
const {styles} = useStyles()
const strings = useStrings()

const {resetToTxHistory} = useWalletNavigation()
const {walletManager} = useWalletManager()
const plate = walletManager.getWalletPlate(publicKeyHex)
const walletDuplicatedMeta = Array.from(walletManager.walletMetas.values()).find(
(walletMeta) => walletMeta.plate === plate.TextPart,
)

if (!walletDuplicatedMeta) throw new Error('Wallet Duplicated Modal: invalid state')

const handleOpenWalletWithDuplicatedName = React.useCallback(() => {
walletManager.setSelectedWalletId(walletDuplicatedMeta.id)
resetToTxHistory()
}, [walletManager, walletDuplicatedMeta.id, resetToTxHistory])

return (
<View style={styles.modal}>
<Text style={styles.modalText}>{strings.restoreDuplicatedWalletModalText}</Text>

<Space height="lg" />

<View style={styles.checksum}>
<Icon.WalletAvatar
image={new Blockies({seed: plate.ImagePart}).asBase64()}
style={styles.walletChecksum}
size={38}
/>

<Space width="sm" />

<View>
<Text style={styles.plateName}>{walletDuplicatedMeta.name}</Text>

<Text style={styles.plateText}>{plate.TextPart}</Text>
</View>
</View>

<Space fill />

<Button
title={strings.restoreDuplicatedWalletModalButton}
style={styles.button}
onPress={handleOpenWalletWithDuplicatedName}
/>

<Space height="xl" />
</View>
)
}

const useStyles = () => {
const {color, atoms} = useTheme()
const styles = StyleSheet.create({
plateName: {
...atoms.body_2_md_medium,
color: color.gray_900,
},
modal: {
...atoms.flex_1,
...atoms.px_lg,
},
walletChecksum: {
width: 38,
height: 38,
borderRadius: 8,
},
checksum: {
...atoms.flex_row,
...atoms.align_center,
textAlignVertical: 'center',
},
plateText: {
...atoms.body_3_sm_regular,
...atoms.text_center,
...atoms.justify_center,
color: color.gray_600,
},
button: {
backgroundColor: color.primary_500,
},
modalText: {
...atoms.body_1_lg_regular,
color: color.gray_900,
},
})
return {styles} as const
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import {Space} from '../../../../components/Space/Space'
import {StepperProgress} from '../../../../components/StepperProgress/StepperProgress'
import {useMetrics} from '../../../../kernel/metrics/metricsManager'
import {SetupWalletRouteNavigation} from '../../../../kernel/navigation'
import {keyManager} from '../../../../yoroi-wallets/cardano/key-manager/key-manager'
import {wrappedCsl} from '../../../../yoroi-wallets/cardano/wrappedCsl'
import {walletManager} from '../../../WalletManager/wallet-manager'
import {useStrings} from '../../common/useStrings'
import {Alert as AlertIllustration} from '../../illustrations/Alert'
import {Check2 as Check2Illustration} from '../../illustrations/Check2'
Expand Down Expand Up @@ -103,10 +102,13 @@ export const VerifyRecoveryPhraseScreen = () => {
title={strings.next}
disabled={disabled}
onPress={async () => {
const {csl, release} = wrappedCsl()
const {accountPubKeyHex} = await keyManager(walletImplementation)({mnemonic, csl, accountVisual})
const {accountPubKeyHex} = await walletManager.generateWalletKeys(
walletImplementation,
mnemonic,
accountVisual,
)
publicKeyHexChanged(accountPubKeyHex)
release()

navigation.navigate('setup-wallet-details-form')
}}
testId="setup-next-button"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {walletChecksum} from '@emurgo/cip4-js'
import {useFocusEffect, useNavigation} from '@react-navigation/native'
import {useAsyncStorage} from '@yoroi/common'
import {Blockies} from '@yoroi/identicon'
Expand Down Expand Up @@ -83,7 +82,7 @@ export const WalletDetailsScreen = () => {
walletIdChanged,
accountVisual,
} = useSetupWallet()
const plate = walletChecksum(publicKeyHex)
const plate = walletManager.getWalletPlate(publicKeyHex)
const [name, setName] = React.useState(features.prefillWalletInfo ? debugWalletInfo.WALLET_NAME : '')
const passwordRef = React.useRef<RNTextInput>(null)
const [password, setPassword] = React.useState(features.prefillWalletInfo ? debugWalletInfo.PASSWORD : '')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import {WalletChecksum, walletChecksum} from '@emurgo/cip4-js'
import {useNavigation} from '@react-navigation/native'
import {Blockies} from '@yoroi/identicon'
import {useSetupWallet} from '@yoroi/setup-wallet'
import {useTheme} from '@yoroi/theme'
import {HW, Wallet} from '@yoroi/types'
import React from 'react'
import {useIntl} from 'react-intl'
import {StyleSheet, Text, View} from 'react-native'
import {StyleSheet, View} from 'react-native'
import {SafeAreaView} from 'react-native-safe-area-context'

import {Button, Icon, useModal} from '../../../../components'
import {Space} from '../../../../components/Space/Space'
import {useModal} from '../../../../components'
import {StepperProgress} from '../../../../components/StepperProgress/StepperProgress'
import {showErrorDialog} from '../../../../kernel/dialogs'
import {errorMessages} from '../../../../kernel/i18n/global-messages'
import LocalizableError from '../../../../kernel/i18n/LocalizableError'
import {SetupWalletRouteNavigation, useWalletNavigation} from '../../../../kernel/navigation'
import {SetupWalletRouteNavigation} from '../../../../kernel/navigation'
import {LedgerConnect} from '../../../../legacy/HW'
import {getHWDeviceInfo} from '../../../../yoroi-wallets/cardano/hw'
import {Device, NetworkId} from '../../../../yoroi-wallets/types'
import {useWalletManager} from '../../../WalletManager/context/WalletManagerProvider'
import {useStrings} from '../../common/useStrings'
import {WalletDuplicatedModal} from '../../common/WalletDuplicatedModal/WalletDuplicatedModal'

export type Params = {
useUSB?: boolean
Expand All @@ -38,42 +36,27 @@ export const ConnectNanoXScreen = ({defaultDevices}: Props) => {
const {styles} = useStyles()
const {walletManager} = useWalletManager()
const {openModal} = useModal()
const {resetToTxHistory} = useWalletNavigation()

const navigation = useNavigation<SetupWalletRouteNavigation>()

const {hwDeviceInfoChanged, walletImplementation, useUSB} = useSetupWallet()

const onSuccess = (hwDeviceInfo: HW.DeviceInfo) => {
hwDeviceInfoChanged(hwDeviceInfo)
const plate = walletChecksum(hwDeviceInfo.bip44AccountPublic)

const duplicatedWalletMeta = Array.from(walletManager.walletMetas.values()).find(
(walletMeta) => walletMeta.plate === plate.TextPart,
)
const isWalletDuplicated = walletManager.getIsWalletDuplicated(hwDeviceInfo.bip44AccountPublic)

if (duplicatedWalletMeta) {
if (isWalletDuplicated) {
openModal(
strings.restoreDuplicatedWalletModalTitle,
<Modal
walletName={duplicatedWalletMeta.name}
plate={plate}
onPress={() => handleOpenWalletWithDuplicatedName(duplicatedWalletMeta)}
/>,
<WalletDuplicatedModal publicKeyHex={hwDeviceInfo.bip44AccountPublic} />,
)

return
}

navigation.navigate('setup-wallet-save-nano-x')
}

const handleOpenWalletWithDuplicatedName = React.useCallback(
(walletMeta: Wallet.Meta) => {
walletManager.setSelectedWalletId(walletMeta.id)
resetToTxHistory()
},
[walletManager, resetToTxHistory],
)

const onError = (error: Error) => {
if (error instanceof LocalizableError) {
showErrorDialog(errorMessages.generalLocalizableError, intl, {
Expand Down Expand Up @@ -110,86 +93,20 @@ export const ConnectNanoXScreen = ({defaultDevices}: Props) => {
)
}

const Modal = ({onPress, plate, walletName}: {onPress: () => void; plate: WalletChecksum; walletName: string}) => {
const {styles} = useStyles()
const strings = useStrings()

return (
<View style={styles.modal}>
<Text style={styles.modalText}>{strings.restoreDuplicatedWalletModalText}</Text>

<Space height="lg" />

<View style={styles.checksum}>
<Icon.WalletAvatar
image={new Blockies({seed: plate.ImagePart}).asBase64()}
style={styles.walletChecksum}
size={38}
/>

<Space width="sm" />

<View>
<Text style={styles.plateName}>{walletName}</Text>

<Text style={styles.plateText}>{plate.TextPart}</Text>
</View>
</View>

<Space fill />

<Button title={strings.restoreDuplicatedWalletModalButton} style={styles.button} onPress={onPress} />

<Space height="xl" />
</View>
)
}

const useStyles = () => {
const {color, atoms} = useTheme()
const styles = StyleSheet.create({
safeAreaView: {
flex: 1,
...atoms.flex_1,
backgroundColor: color.bg_color_max,
},
stepper: {
...atoms.p_lg,
},
content: {
flex: 1,
...atoms.flex_1,
...atoms.px_lg,
},
plateName: {
...atoms.body_2_md_medium,
color: color.gray_900,
},
modal: {
flex: 1,
...atoms.px_lg,
},
walletChecksum: {
width: 38,
height: 38,
borderRadius: 8,
},
checksum: {
...atoms.flex_row,
...atoms.align_center,
textAlignVertical: 'center',
},
plateText: {
...atoms.body_3_sm_regular,
...atoms.text_center,
...atoms.justify_center,
color: color.gray_600,
},
button: {
backgroundColor: color.primary_500,
},
modalText: {
...atoms.body_1_lg_regular,
color: color.gray_900,
},
})
return {styles} as const
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {walletChecksum} from '@emurgo/cip4-js'
import {useNavigation} from '@react-navigation/native'
import {useAsyncStorage} from '@yoroi/common'
import {Blockies} from '@yoroi/identicon'
Expand Down Expand Up @@ -29,7 +28,7 @@ import {logger} from '../../../../kernel/logger/logger'
import {useMetrics} from '../../../../kernel/metrics/metricsManager'
import {SetupWalletRouteNavigation} from '../../../../kernel/navigation'
import {isEmptyString} from '../../../../kernel/utils'
import {getWalletNameError, validateWalletName} from '../../../../yoroi-wallets/utils'
import {getWalletNameError} from '../../../../yoroi-wallets/utils'
import {useCreateWalletXPub} from '../../../WalletManager/common/hooks/useCreateWalletXPub'
import {parseWalletMeta} from '../../../WalletManager/common/validators/wallet-meta'
import {useWalletManager} from '../../../WalletManager/context/WalletManagerProvider'
Expand Down Expand Up @@ -71,9 +70,7 @@ export const SaveNanoXScreen = () => {
const {walletImplementation, hwDeviceInfo, accountVisual, walletIdChanged} = useSetupWallet()

if (!hwDeviceInfo) throw new Error('no hwDeviceInfo')
const plate = walletChecksum(hwDeviceInfo.bip44AccountPublic)

const walletNames = Array.from(walletManager.walletMetas.values()).map(({name}) => name)
const plate = walletManager.getWalletPlate(hwDeviceInfo.bip44AccountPublic)

const {createWallet, isLoading} = useCreateWalletXPub({
onSuccess: async (wallet) => {
Expand All @@ -100,12 +97,14 @@ export const SaveNanoXScreen = () => {
},
})

const nameErrors = validateWalletName(name, null, !isLoading ? walletNames : [])
const nameErrors = !isLoading ? walletManager.validateWalletName(name) : null
const walletNameErrorText = getWalletNameError(
{tooLong: strings.tooLong, nameAlreadyTaken: strings.nameAlreadyTaken, mustBeFilled: strings.mustBeFilled},
nameErrors,
)

const disabled = isLoading || Object.keys(nameErrors ?? {}).length > 0

const handleOnSubmit = React.useCallback(() => {
createWallet({
name,
Expand Down Expand Up @@ -248,7 +247,7 @@ export const SaveNanoXScreen = () => {
title={strings.next}
onPress={handleOnSubmit}
testId="setup-restore-step2-next-button"
disabled={isLoading || Object.keys(nameErrors).length > 0}
disabled={disabled}
/>
</View>
</SafeAreaView>
Expand Down
Loading

0 comments on commit e8ec7b5

Please sign in to comment.