Skip to content

Commit

Permalink
feat: 🎸 update eoa change pin
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-portkey committed Feb 5, 2025
1 parent 0e86096 commit 2c7b496
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 195 deletions.
43 changes: 33 additions & 10 deletions packages/hooks/hooks-eoa/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
addWallet,
resetWallet,
setHideAssetsAction,
updateWalletList,
} from '@portkey-wallet/store/store-eoa/wallet/actions';
import { TAccountInfo, TWalletInfo } from '@portkey-wallet/types/types-eoa/wallet';
import aes from '@portkey-wallet/utils/aes';
import { useCurrentNetwork, useIsMainnet } from '../network';
import { ChainId } from '@portkey-wallet/types';
import { changeNetworkType } from '@portkey-wallet/store/store-eoa/wallet/slice';
import { useChainList } from '../network/chain';

export const useWalletState = () => useAppEOASelector(state => state.wallet);
Expand Down Expand Up @@ -53,7 +53,7 @@ export const useAddWallet = () => {
}

const wallet = formatWalletInfoV2(walletInfo, pin, `Wallet ${walletListLength + 1}`);
console.log('after: formatWalletInfoV2: ', wallet, walletList);

if (!wallet) {
return {
success: false,
Expand All @@ -67,7 +67,7 @@ export const useAddWallet = () => {
message: 'Wallet already exists',
};
}
console.log('wallet======', JSON.stringify(wallet));

dispatch(
addWallet({
wallet,
Expand Down Expand Up @@ -252,12 +252,35 @@ export const useChainIdList = () => {
}, [chainList, isMainnet]);
};

export const useSwitchNetworkType = () => {
export const useUpdateWalletAES = () => {
const walletList = useWalletListState();
const dispatch = useAppCommonDispatch();
const networkType = useCurrentNetwork();
const switchNetwork = useCallback(() => {
const switchedNetwork = networkType === 'MAINNET' ? 'TESTNET' : 'MAINNET';
dispatch(changeNetworkType(switchedNetwork));
}, [dispatch, networkType]);
return switchNetwork;

return useCallback(
(oldPin: string, newPin: string) => {
const newWalletList = walletList.map(wallet => {
const newWallet: TWalletInfo = { ...wallet };
const isPrivate = newWallet.AESEncryptMnemonic === '';
if (!isPrivate) {
const mnemonic = aes.decrypt(newWallet.AESEncryptMnemonic, oldPin);
if (!mnemonic) throw 'Error Pin mnemonic';
newWallet.AESEncryptMnemonic = aes.encrypt(mnemonic, newPin);
}
newWallet.accountList = newWallet.accountList.map(account => {
const newAccount: TAccountInfo = { ...account };
const privateKey = aes.decrypt(newAccount.AESEncryptPrivateKey, oldPin);
if (!privateKey) throw 'Error Pin privateKey';
newAccount.AESEncryptPrivateKey = aes.encrypt(privateKey, newPin);
return newAccount;
});
return newWallet;
});
dispatch(
updateWalletList({
walletList: newWalletList,
}),
);
},
[dispatch, walletList],
);
};
3 changes: 2 additions & 1 deletion packages/mobile-aelf/js/hooks/securityLock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ export const useCheckSecurityLock = () => {
const dispatch = useAppDispatch();

return useCallback(
async (callback?: () => void) => {
async (callback?: () => void, isBackAllow = false) => {
if (!biometrics) {
navigationService.push('SecurityLock', {
isCheck: true,
checkCallback: callback,
isBackAllow,
});
return;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/mobile-aelf/js/pages/Home/HomeTab/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ const HomeTab: React.FC<any> = ({ _ }) => {
Switch Network
</CommonButton>

<CommonButton type="primary" onPress={() => navigationService.push('Security')} style={{ marginTop: 20 }}>
Security
</CommonButton>

<CommonButton type="primary" onPress={() => navigationService.push('Home')} style={{ marginTop: 20 }}>
Home
</CommonButton>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import React, { useCallback } from 'react';
import PageContainer from 'components/PageContainer';
import { touchAuth } from '@portkey-wallet/utils/mobile/authentication';
import CommonToast from 'components/CommonToast';
import useBiometricsReady, { useSetBiometrics } from 'hooks/useBiometrics';
import { useSetBiometrics } from 'hooks/useBiometrics';
import navigationService from 'utils/navigationService';
import { View } from 'react-native';
import { checkPin } from 'utils/redux';
import fonts from 'assets/theme/fonts';
import useEffectOnce from 'hooks/useEffectOnce';
import { useLanguage } from 'i18n/hooks';
import i18n from 'i18n';
import { useUser } from 'hooks/store';
import { usePin, useUser } from 'hooks/store';
import { TextL, TextM } from 'components/CommonText';
import CommonSwitch from 'components/CommonSwitch';
import ActionSheet from 'components/ActionSheet';
import { setSecureStoreItem } from '@portkey-wallet/utils/mobile/biometric';
import myEvents from 'utils/deviceEvent';
import { changeCanLock } from 'utils/LockManager';
import { makeStyles } from '@rneui/themed';
import { pTd } from 'utils/unit';
import { useCheckSecurityLock } from 'hooks/securityLock';
import { SetBiometricsTypeEnum } from 'pages/Pin/SetBiometrics';

export default function Biometric() {
const styles = getStyles();
const { biometrics } = useUser();
const setBiometrics = useSetBiometrics();
const biometricsReady = useBiometricsReady();
const { t } = useLanguage();
const openBiometrics = useCallback(
async (pin: string) => {
Expand All @@ -46,55 +45,38 @@ export default function Biometric() {
const listener = myEvents.openBiometrics.addListener(openBiometrics);
return () => listener.remove();
});

const checkSecurityLock = useCheckSecurityLock();
const pin = usePin();
const onValueChange = useCallback(
async (value: boolean) => {
if (value) {
// const result = await authenticationReady();
// if (!result) return CommonToast.fail('This device does not currently support biometrics');
navigationService.navigate('CheckPin', { openBiometrics: true });
} else {
ActionSheet.alert({
title2: 'Disable fingerprint login?',
buttons: [
{ type: 'outline', title: 'Cancel' },
{
type: 'primary',
title: 'Confirm',
onPress: async () => {
changeCanLock(false);
try {
const enrolled = await touchAuth();
if (enrolled.success) {
await setBiometrics(value);
} else {
CommonToast.fail(enrolled.warning || enrolled.error);
}
} catch (error) {
CommonToast.failError(error, i18n.t('Failed to enable biometrics'));
}
changeCanLock(true);
},
},
],
});
}
checkSecurityLock(() => {
if (value) {
navigationService.pop(1);
navigationService.push('SetBiometrics', {
type: SetBiometricsTypeEnum.update,
});
} else {
navigationService.push('SetPin', {
oldPin: pin,
});
}
}, true);
},
[setBiometrics],
[checkSecurityLock, pin],
);
return (
<PageContainer
containerStyles={styles.containerStyles}
safeAreaColor={['black']}
titleDom={t('Biometric Authentication')}>
{biometricsReady && (
<View style={styles.wrap}>
<View style={styles.switchWrap}>
<TextL style={styles.switchText}>Biometric authentication</TextL>
<CommonSwitch style={styles.switchButton} value={biometrics} onValueChange={onValueChange} />
</View>
<TextM style={styles.tipText}>Enable biometric authentication to quickly unlock the device.</TextM>
<View style={styles.wrap}>
<View style={styles.switchWrap}>
<TextL style={styles.switchText}>Biometric authentication</TextL>
<CommonSwitch style={styles.switchButton} value={biometrics} onValueChange={onValueChange} />
</View>
)}
<TextM style={styles.tipText}>Enable biometric authentication to quickly unlock the device.</TextM>
</View>
</PageContainer>
);
}
Expand Down
34 changes: 26 additions & 8 deletions packages/mobile-aelf/js/pages/My/Security/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PageContainer from 'components/PageContainer';
import { useLanguage } from 'i18n/hooks';
import React, { useEffect } from 'react';
import React, { useCallback, useEffect } from 'react';
import { pTd } from 'utils/unit';
import { ScrollView, View } from 'react-native';
import GStyles from 'assets/theme/GStyles';
Expand All @@ -10,6 +10,8 @@ import { useAppDispatch } from 'store/hooks';
import { getCaHolderInfoAsync } from '@portkey-wallet/store/store-ca/wallet/actions';
import { StyleSheet } from 'react-native';
import useBiometricsReady from 'hooks/useBiometrics';
import { usePin, useUser } from 'hooks/store';
import { useCheckSecurityLock } from 'hooks/securityLock';

interface SecurityProps {
name?: string;
Expand All @@ -24,6 +26,20 @@ const Security: React.FC<SecurityProps> = () => {
appDispatch(getCaHolderInfoAsync());
}, [appDispatch]);

const pin = usePin();
const checkSecurityLock = useCheckSecurityLock();

const changePin = useCallback(() => {
checkSecurityLock(() => {
navigationService.pop(1);
navigationService.push('SetPin', {
oldPin: pin,
});
}, true);
}, [checkSecurityLock, pin]);

const { biometrics } = useUser();

return (
<PageContainer
titleDom={t('Security')}
Expand All @@ -48,13 +64,15 @@ const Security: React.FC<SecurityProps> = () => {
size={pTd(24)}
/>
)}
<MenuItem
style={pageStyles.menuItem}
onPress={() => navigationService.navigate('CheckPin')}
title={t('Change PIN')}
icon="my-pin"
size={pTd(24)}
/>
{!biometrics && (
<MenuItem
style={pageStyles.menuItem}
onPress={changePin}
title={t('Change PIN')}
icon="my-pin"
size={pTd(24)}
/>
)}
</View>
</ScrollView>
</PageContainer>
Expand Down
69 changes: 0 additions & 69 deletions packages/mobile-aelf/js/pages/Pin/CheckPin/index.tsx

This file was deleted.

Loading

0 comments on commit 2c7b496

Please sign in to comment.