Skip to content

Commit a1ee305

Browse files
Merge pull request #11456 from brave/use-common-password-validation
Use Common Password Validation on Desktop
2 parents cbdf457 + 094330c commit a1ee305

File tree

7 files changed

+50
-21
lines changed

7 files changed

+50
-21
lines changed

components/brave_wallet_ui/common/async/lib.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ export const getBalance = (address: string): Promise<string> => {
7474
})
7575
}
7676

77+
export async function isStrongPassword (value: string) {
78+
const apiProxy = getAPIProxy()
79+
return (await apiProxy.keyringController.isStrongPassword(value)).result
80+
}
81+
7782
export async function findENSAddress (address: string) {
7883
const apiProxy = getAPIProxy()
7984
return apiProxy.ethJsonRpcController.ensGetEthAddr(address)

components/brave_wallet_ui/components/desktop/wallet-onboarding/restore/index.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import { PasswordInput, BackButton } from '../../../shared'
1616
import { NavButton } from '../../../extension'
1717
import { getLocale } from '../../../../../common/locale'
1818
import { Checkbox } from 'brave-ui'
19-
import { isStrongPassword } from '../../../../utils/password-utils'
2019

2120
export interface Props {
21+
checkIsStrongPassword: (value: string) => Promise<boolean>
2222
toggleShowRestore: () => void
2323
onRestore: (phrase: string, password: string, isLegacy: boolean) => void
2424
hasRestoreError: boolean
@@ -28,10 +28,12 @@ function OnboardingRestore (props: Props) {
2828
const {
2929
onRestore,
3030
toggleShowRestore,
31+
checkIsStrongPassword,
3132
hasRestoreError
3233
} = props
3334
const [showRecoveryPhrase, setShowRecoveryPhrase] = React.useState<boolean>(false)
3435
const [isLegacyWallet, setIsLegacyWallet] = React.useState<boolean>(false)
36+
const [isStrongPassword, setIsStrongPassword] = React.useState<boolean>(false)
3537
const [password, setPassword] = React.useState<string>('')
3638
const [confirmedPassword, setConfirmedPassword] = React.useState<string>('')
3739
const [recoveryPhrase, setRecoveryPhrase] = React.useState<string>('')
@@ -47,8 +49,10 @@ function OnboardingRestore (props: Props) {
4749
onRestore(recoveryPhrase.trimEnd(), password, isLegacyWallet)
4850
}
4951

50-
const handlePasswordChanged = (value: string) => {
52+
const handlePasswordChanged = async (value: string) => {
5153
setPassword(value)
54+
const isStrong = await checkIsStrongPassword(value)
55+
setIsStrongPassword(isStrong)
5256
}
5357

5458
const handleConfirmPasswordChanged = (value: string) => {
@@ -87,13 +91,9 @@ function OnboardingRestore (props: Props) {
8791
const checkPassword = React.useMemo(() => {
8892
if (password === '') {
8993
return false
90-
} else {
91-
if (!isStrongPassword.test(password)) {
92-
return true
93-
}
94-
return false
9594
}
96-
}, [password])
95+
return !isStrongPassword
96+
}, [password, isStrongPassword])
9797

9898
const checkConfirmedPassword = React.useMemo(() => {
9999
if (confirmedPassword === '') {

components/brave_wallet_ui/constants/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,10 @@ export interface GetEthAddrReturnInfo {
377377
address: string
378378
}
379379

380+
export interface GetIsStrongPassswordReturnInfo {
381+
result: boolean
382+
}
383+
380384
export interface RecoveryObject {
381385
value: string
382386
id: number

components/brave_wallet_ui/page/container.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ import {
5353
findUnstoppableDomainAddress,
5454
getBalance,
5555
getERC20Allowance,
56-
onConnectHardwareWallet
56+
onConnectHardwareWallet,
57+
isStrongPassword
5758
} from '../common/async/lib'
5859

5960
import { formatBalance } from '../utils/format-balances'
@@ -517,6 +518,7 @@ function Container (props: Props) {
517518
{isWalletLocked &&
518519
<OnboardingWrapper>
519520
<OnboardingRestore
521+
checkIsStrongPassword={isStrongPassword}
520522
onRestore={restoreWallet}
521523
toggleShowRestore={onToggleShowRestore}
522524
hasRestoreError={restoreError}
@@ -526,6 +528,7 @@ function Container (props: Props) {
526528
</Route>
527529
<Route path={WalletRoutes.Onboarding} exact={true}>
528530
<Onboarding
531+
checkIsStrongPassword={isStrongPassword}
529532
recoveryPhrase={recoveryPhrase}
530533
onPasswordProvided={passwordProvided}
531534
onSubmit={completeWalletSetup}

components/brave_wallet_ui/stories/screens/onboarding.tsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import {
1010
} from '../../constants/types'
1111
import { BackButton } from '../../components/shared'
1212
import BackupWallet from './backup-wallet'
13-
import { isStrongPassword } from '../../utils/password-utils'
1413
import { OnboardingWrapper } from '../style'
1514

1615
export interface Props {
1716
recoveryPhrase: string[]
1817
metaMaskWalletDetected: boolean
1918
braveLegacyWalletDetected: boolean
2019
importError: ImportWalletError
20+
checkIsStrongPassword: (value: string) => Promise<boolean>
2121
onSetImportError: (hasError: boolean) => void
2222
onPasswordProvided: (password: string) => void
2323
onImportMetaMask: (password: string, newPassword: string) => void
@@ -32,6 +32,7 @@ function Onboarding (props: Props) {
3232
metaMaskWalletDetected,
3333
braveLegacyWalletDetected,
3434
importError,
35+
checkIsStrongPassword,
3536
onSetImportError,
3637
onPasswordProvided,
3738
onSubmit,
@@ -40,6 +41,8 @@ function Onboarding (props: Props) {
4041
onImportCryptoWallets
4142
} = props
4243
const [onboardingStep, setOnboardingStep] = React.useState<WalletOnboardingSteps>(WalletOnboardingSteps.OnboardingWelcome)
44+
const [isStrongPassword, setIsStrongPassword] = React.useState<boolean>(false)
45+
const [isStrongImportPassword, setIsStrongImportPassword] = React.useState<boolean>(false)
4346
const [importPassword, setImportPassword] = React.useState<string>('')
4447
const [password, setPassword] = React.useState<string>('')
4548
const [confirmedPassword, setConfirmedPassword] = React.useState<string>('')
@@ -89,7 +92,7 @@ function Onboarding (props: Props) {
8992
onSubmit(false)
9093
}
9194

92-
const handleImportPasswordChanged = (value: string) => {
95+
const handleImportPasswordChanged = async (value: string) => {
9396
if (importError.hasError) {
9497
onSetImportError(false)
9598
}
@@ -98,10 +101,14 @@ function Onboarding (props: Props) {
98101
setUseSamePassword(false)
99102
}
100103
setImportPassword(value)
104+
const isStrong = await checkIsStrongPassword(value)
105+
setIsStrongImportPassword(isStrong)
101106
}
102107

103-
const handlePasswordChanged = (value: string) => {
108+
const handlePasswordChanged = async (value: string) => {
104109
setPassword(value)
110+
const isStrong = await checkIsStrongPassword(value)
111+
setIsStrongPassword(isStrong)
105112
}
106113

107114
const handleConfirmPasswordChanged = (value: string) => {
@@ -138,13 +145,9 @@ function Onboarding (props: Props) {
138145
const checkPassword = React.useMemo(() => {
139146
if (password === '') {
140147
return false
141-
} else {
142-
if (!isStrongPassword.test(password)) {
143-
return true
144-
}
145-
return false
146148
}
147-
}, [password])
149+
return !isStrongPassword
150+
}, [password, isStrongPassword])
148151

149152
const checkConfirmedPassword = React.useMemo(() => {
150153
if (confirmedPassword === '') {
@@ -158,11 +161,11 @@ function Onboarding (props: Props) {
158161
setOnboardingStep(WalletOnboardingSteps.OnboardingCreatePassword)
159162
}
160163

161-
React.useMemo(() => {
164+
React.useEffect(() => {
162165
if (useSamePassword) {
163-
setPassword(importPassword)
166+
handlePasswordChanged(importPassword)
164167
setConfirmedPassword(importPassword)
165-
if (!isStrongPassword.test(importPassword)) {
168+
if (!isStrongImportPassword) {
166169
setNeedsNewPassword(true)
167170
setUseSamePasswordVerified(false)
168171
} else {
@@ -174,6 +177,7 @@ function Onboarding (props: Props) {
174177
setConfirmedPassword('')
175178
setNeedsNewPassword(false)
176179
setUseSamePasswordVerified(false)
180+
setIsStrongPassword(false)
177181
}
178182
}, [useSamePassword])
179183

components/brave_wallet_ui/stories/wallet-components.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import './locale'
1010
import {
1111
recoveryPhrase
1212
} from './mock-data/user-accounts'
13+
import { isStrongPassword } from '../utils/password-utils'
1314

1415
export default {
1516
title: 'Wallet/Desktop/Components',
@@ -100,10 +101,15 @@ export const _Onboarding = () => {
100101
// Does nothing here
101102
}
102103

104+
const checkIsStrongPassword = async (value: string) => {
105+
return isStrongPassword.test(value)
106+
}
107+
103108
return (
104109
<WalletPageLayout>
105110
<WalletSubViewLayout>
106111
<Onboarding
112+
checkIsStrongPassword={checkIsStrongPassword}
107113
importError={{ hasError: false }}
108114
recoveryPhrase={recoveryPhrase}
109115
onSubmit={complete}

components/brave_wallet_ui/stories/wallet-concept.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
} from '../components/desktop/popup-modals/add-account-modal/hardware-wallet-connect/types'
5050
import { mockNetworks } from './mock-data/mock-networks'
5151
import { HardwareWalletAccount } from '../common/hardware/types'
52+
import { isStrongPassword } from '../utils/password-utils'
5253
export default {
5354
title: 'Wallet/Desktop',
5455
argTypes: {
@@ -665,6 +666,10 @@ export const _DesktopWalletConcept = (args: { onboarding: boolean, locked: boole
665666
setCustomTolerance(value)
666667
}
667668

669+
const checkIsStrongPassword = async (value: string) => {
670+
return isStrongPassword.test(value)
671+
}
672+
668673
return (
669674
<WalletPageLayout>
670675
{/* <SideNav
@@ -675,6 +680,7 @@ export const _DesktopWalletConcept = (args: { onboarding: boolean, locked: boole
675680
<WalletSubViewLayout>
676681
{isRestoring ? (
677682
<OnboardingRestore
683+
checkIsStrongPassword={checkIsStrongPassword}
678684
hasRestoreError={hasRestoreError}
679685
onRestore={onRestore}
680686
toggleShowRestore={onToggleRestore}
@@ -684,6 +690,7 @@ export const _DesktopWalletConcept = (args: { onboarding: boolean, locked: boole
684690
{needsOnboarding
685691
? (
686692
<Onboarding
693+
checkIsStrongPassword={checkIsStrongPassword}
687694
importError={importWalletError}
688695
recoveryPhrase={recoveryPhrase}
689696
onSubmit={completeWalletSetup}

0 commit comments

Comments
 (0)