Skip to content

Commit

Permalink
Merge pull request #1006 from Koniverse/issue-444b
Browse files Browse the repository at this point in the history
[issue-444] Change password affect transactions flow
  • Loading branch information
nguyenduythuc authored Sep 8, 2023
2 parents 9a725bc + 419115a commit b6f9eaa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
76 changes: 45 additions & 31 deletions src/screens/MasterPassword/ChangeMasterPassword/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ import { keyringChangeMasterPassword, keyringUnlock } from 'messaging/index';
import { useNavigation } from '@react-navigation/native';
import { RootNavigationProps } from 'routes/index';
import ChangeMasterPasswordStyle from './style';
import { backToHome } from 'utils/navigation';
import useGoHome from 'hooks/screen/useGoHome';
import i18n from 'utils/i18n/i18n';
import AlertBox from 'components/design-system-ui/alert-box';
import { FontSemiBold } from 'styles/sharedStyles';
import { useSelector } from 'react-redux';
import { RootState } from 'stores/index';
import { createKeychainPassword, resetKeychainPassword } from 'utils/account';
import { createKeychainPassword } from 'utils/account';

function checkValidateForm(isValidated: Record<string, boolean>) {
return isValidated.password && isValidated.repeatPassword;
Expand All @@ -32,7 +30,6 @@ const ChangeMasterPassword = () => {
const navigation = useNavigation<RootNavigationProps>();
const { isUseBiometric } = useSelector((state: RootState) => state.mobileSettings);
const theme = useSubWalletTheme().swThemes;
const goHome = useGoHome();
const _style = ChangeMasterPasswordStyle(theme);
const [isBusy, setIsBusy] = useState(false);
const [errors, setErrors] = useState<string[]>([]);
Expand Down Expand Up @@ -63,15 +60,11 @@ const ChangeMasterPassword = () => {
useHandlerHardwareBackPress(isBusy);

const _backToHome = useCallback(() => {
backToHome(goHome);
}, [goHome]);

async function handleUpdateKeychain(password: string) {
if (isUseBiometric) {
await resetKeychainPassword();
createKeychainPassword(password);
}
}
navigation.reset({
index: 0,
routes: [{ name: 'Home' }],
});
}, [navigation]);

const onSubmit = () => {
if (checkValidateForm(formState.isValidated)) {
Expand All @@ -80,28 +73,49 @@ const ChangeMasterPassword = () => {

if (password && oldPassword) {
setIsBusy(true);
keyringChangeMasterPassword({
createNew: false,
newPassword: password,
oldPassword: oldPassword,
})
.then(res => {
if (!res.status) {
setErrors(res.errors);
} else {
handleUpdateKeychain(password);
_backToHome();
if (isUseBiometric) {
(async () => {
try {
const res = await createKeychainPassword(password);
if (!res) {
setIsBusy(false);
return;
}
handleUnlock(password, oldPassword);
} catch (e) {
setIsBusy(false);
}
})
.catch(e => {
setErrors([e.message]);
})
.finally(() => {
setIsBusy(false);
});
})();
} else {
handleUnlock(password, oldPassword);
}
}
}
};

function handleUnlock(password: string, oldPassword: string) {
keyringChangeMasterPassword({
createNew: false,
newPassword: password,
oldPassword: oldPassword,
})
.then(res => {
if (!res.status) {
setErrors(res.errors);
isUseBiometric && createKeychainPassword(password);
return;
}
_backToHome();
})
.catch(e => {
isUseBiometric && createKeychainPassword(password);
setErrors([e.message]);
})
.finally(() => {
setIsBusy(false);
});
}

const { formState, onChangeValue, onUpdateErrors, onSubmitField } = useFormControl(formConfig, {
onSubmitForm: onSubmit,
});
Expand Down
5 changes: 3 additions & 2 deletions src/screens/MasterPassword/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,10 @@ const Login: React.FC<LoginProps> = ({ navigation }) => {
async function requestUnlockWithBiometric() {
try {
const password = await getKeychainPassword();
if (password) {
onUnlock(password);
if (!password) {
throw 'Biometry is not available';
}
onUnlock(password);
} catch (e) {
console.warn(e);
if (JSON.stringify(e).indexOf('Biometry is not available') !== -1) {
Expand Down

0 comments on commit b6f9eaa

Please sign in to comment.