-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33e831e
commit d46ad0f
Showing
12 changed files
with
166 additions
and
25 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
packages/browser-wallet/src/popup/popupX/pages/ChangePasscode/ChangePasscode.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.change-passcode-x { | ||
.divider { | ||
display: block; | ||
margin: rem(16px) 0 rem(24px) 0; | ||
border-bottom: 1px solid rgba($color-white, 0.1); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
packages/browser-wallet/src/popup/popupX/pages/ChangePasscode/ChangePasscode.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import React, { useRef } from 'react'; | ||
import Page from '@popup/popupX/shared/Page'; | ||
import { useTranslation } from 'react-i18next'; | ||
import Button from '@popup/popupX/shared/Button'; | ||
import FormPassword from '@popup/popupX/shared/Form/Password'; | ||
import Form, { useForm } from '@popup/popupX/shared/Form/Form'; | ||
import { useAtom, useSetAtom } from 'jotai'; | ||
import { encryptedSeedPhraseAtom, sessionPasscodeAtom } from '@popup/store/settings'; | ||
import { addToastAtom } from '@popup/state'; | ||
import { SubmitHandler, UseFormGetValues, Validate } from 'react-hook-form'; | ||
import { decrypt, encrypt } from '@shared/utils/crypto'; | ||
|
||
type FormValues = { | ||
currentPasscode: string; | ||
newPasscode: string; | ||
newPasscodeRepeated: string; | ||
}; | ||
|
||
export default function ChangePasscode() { | ||
const { t } = useTranslation('x', { keyPrefix: 'passcode' }); | ||
|
||
const formRef = useRef<HTMLFormElement>(null); | ||
const form = useForm<FormValues>(); | ||
const [encryptedSeedPhrase, setEncryptedSeedPhrase] = useAtom(encryptedSeedPhraseAtom); | ||
const [passcode, setPasscode] = useAtom(sessionPasscodeAtom); | ||
const addToast = useSetAtom(addToastAtom); | ||
|
||
if (passcode.loading || !passcode.value || encryptedSeedPhrase.loading) { | ||
return null; | ||
} | ||
|
||
const handleSubmit: SubmitHandler<FormValues> = async (vs) => { | ||
if (encryptedSeedPhrase.value && passcode.value) { | ||
const decryptedSeedPhrase = await decrypt(encryptedSeedPhrase.value, passcode.value); | ||
const encryptedSeedPhraseWithNewPasscode = await encrypt(decryptedSeedPhrase, vs.newPasscode); | ||
|
||
setEncryptedSeedPhrase(encryptedSeedPhraseWithNewPasscode); | ||
setPasscode(vs.newPasscode); | ||
addToast(t('passcodeUpdated')); | ||
} | ||
}; | ||
|
||
function validateCurrentPasscode(): Validate<string> { | ||
return (currentPasscode) => (currentPasscode !== passcode.value ? t('incorrectPasscode') : undefined); | ||
} | ||
|
||
function validateNewPasscode(getValues: UseFormGetValues<FormValues>): Validate<string> { | ||
return (newPasscodeRepeated) => | ||
getValues().newPasscode !== newPasscodeRepeated ? t('form.passcodeMismatch') : undefined; | ||
} | ||
|
||
return ( | ||
<Page className="change-passcode-x"> | ||
<Page.Top heading={t('changePasscode')} /> | ||
<Page.Main> | ||
<Form | ||
id="test-form" | ||
onSubmit={handleSubmit} | ||
className="change-passcode-page__form" | ||
formMethods={form} | ||
ref={formRef} | ||
> | ||
{(f) => { | ||
return ( | ||
<> | ||
<FormPassword | ||
control={f.control} | ||
name="currentPasscode" | ||
label={t('labels.currentPasscode')} | ||
rules={{ | ||
required: t('form.passcodeRequired'), | ||
validate: validateCurrentPasscode(), | ||
}} | ||
/> | ||
<span className="divider" /> | ||
<FormPassword | ||
control={f.control} | ||
name="newPasscode" | ||
label={t('labels.newPasscode')} | ||
rules={{ | ||
required: t('form.passcodeRequired'), | ||
minLength: { | ||
value: 6, | ||
message: t('form.passcodeMinLength'), | ||
}, | ||
}} | ||
/> | ||
<FormPassword | ||
control={f.control} | ||
name="newPasscodeRepeated" | ||
label={t('labels.newPasscodeRepeated')} | ||
rules={{ validate: validateNewPasscode(f.getValues) }} | ||
/> | ||
</> | ||
); | ||
}} | ||
</Form> | ||
</Page.Main> | ||
<Page.Footer> | ||
<Button.Main | ||
form="test-form" | ||
type="submit" | ||
label={t('changePasscode')} | ||
disabled={form.formState.isSubmitting} | ||
/> | ||
</Page.Footer> | ||
</Page> | ||
); | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/browser-wallet/src/popup/popupX/pages/ChangePasscode/i18n/en.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const t = { | ||
changePasscode: 'Change passcode', | ||
incorrectPasscode: 'Incorrect passcode', | ||
passcodeUpdated: 'Successfully updated passcode', | ||
labels: { | ||
currentPasscode: 'Enter current passcode', | ||
newPasscode: 'Enter new passcode', | ||
newPasscodeRepeated: 'Confirm new passcode', | ||
}, | ||
form: { | ||
passcodeRequired: 'A passcode must be entered', | ||
passcodeMismatch: 'Passcode does not match', | ||
passcodeMinLength: 'Passcode must be at least 6 characters', | ||
}, | ||
}; | ||
|
||
export default t; |
1 change: 1 addition & 0 deletions
1
packages/browser-wallet/src/popup/popupX/pages/ChangePasscode/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './ChangePasscode'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters