Skip to content

Commit

Permalink
feat(nami): disable password change for hw wallet (#1481)
Browse files Browse the repository at this point in the history
feat(nami): disable password change for hw wallet
  • Loading branch information
mirceahasegan authored Oct 28, 2024
1 parent e628f2e commit 7dfc3dd
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/nami/src/adapters/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ describe('useChangePassword', () => {

await expect(
result.current('wrong-password', 'new-password'),
).rejects.toEqual(ERROR.wrongPassword);
).rejects.toEqual(new Error(ERROR.wrongPassword));
expect(mockDeleteWallet).not.toHaveBeenCalled();
});
});
18 changes: 9 additions & 9 deletions packages/nami/src/adapters/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ export const useChangePassword = ({

return useCallback(
async (currentPassword: string, newPassword: string) => {
try {
if (
!wallet?.metadata?.name ||
!('encryptedSecrets' in wallet) ||
!('accounts' in wallet)
) {
return;
}
if (
!wallet?.metadata?.name ||
!('encryptedSecrets' in wallet) ||
!('accounts' in wallet)
) {
throw new Error(ERROR.passwordChangeNotPossible);
}

try {
const decryptedRootPrivateKeyBytes =
await Wallet.KeyManagement.emip3decrypt(
Buffer.from(wallet.encryptedSecrets.rootPrivateKeyBytes, 'hex'),
Expand Down Expand Up @@ -106,7 +106,7 @@ export const useChangePassword = ({
}
await activateWallet({ chainId, walletId, accountIndex });
} catch {
throw ERROR.wrongPassword;
throw new Error(ERROR.wrongPassword);
}
},
[
Expand Down
1 change: 1 addition & 0 deletions packages/nami/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const POPUP_WINDOW = {
export const ERROR = {
accessDenied: 'Access denied',
wrongPassword: 'Wrong password',
passwordChangeNotPossible: 'Cannot change password',
txTooBig: 'Transaction too big',
txNotPossible: 'Transaction not possible',
storeNotEmpty: 'Storage key is already set',
Expand Down
61 changes: 36 additions & 25 deletions packages/nami/src/ui/app/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-disable react/no-multi-comp */
import React, { useCallback, useEffect, useRef, useState } from 'react';

import { WalletType } from '@cardano-sdk/web-extension';
import {
ChevronLeftIcon,
ChevronRightIcon,
Expand Down Expand Up @@ -44,6 +45,7 @@ import type { UseAccount } from '../../../adapters/account';
import type { OutsideHandlesContextValue } from '../../../features/outside-handles-provider';
import type { ChangePasswordModalComponentRef } from '../components/changePasswordModal';
import type { Wallet } from '@lace/cardano';
import type { CommonOutsideHandlesContextValue } from 'features/common-outside-handles-provider';

type Props = Pick<
OutsideHandlesContextValue,
Expand All @@ -60,17 +62,18 @@ type Props = Pick<
| 'setTheme'
| 'switchNetwork'
| 'theme'
> & {
currency: CurrencyCode;
setCurrency: (currency: CurrencyCode) => void;
changePassword: (
currentPassword: string,
newPassword: string,
) => Promise<void>;
accountName: string;
accountAvatar?: string;
updateAccountMetadata: UseAccount['updateAccountMetadata'];
};
> &
Pick<CommonOutsideHandlesContextValue, 'walletType'> & {
currency: CurrencyCode;
setCurrency: (currency: CurrencyCode) => void;
changePassword: (
currentPassword: string,
newPassword: string,
) => Promise<void>;
accountName: string;
accountAvatar?: string;
updateAccountMetadata: UseAccount['updateAccountMetadata'];
};

const Settings = ({
currency,
Expand All @@ -92,6 +95,7 @@ const Settings = ({
getCustomSubmitApiForNetwork,
defaultSubmitApi,
isValidURL,
walletType,
}: Readonly<Props>) => {
const history = useHistory();
const location = useLocation();
Expand Down Expand Up @@ -136,6 +140,7 @@ const Settings = ({
accountAvatar={accountAvatar}
accountName={accountName}
updateAccountMetadata={updateAccountMetadata}
walletType={walletType}
/>
</Route>
<Route path="/settings/whitelisted" exact>
Expand Down Expand Up @@ -275,6 +280,7 @@ const GeneralSettings = ({
accountAvatar,
changePassword,
updateAccountMetadata,
walletType,
}: Readonly<
Pick<
Props,
Expand All @@ -286,6 +292,7 @@ const GeneralSettings = ({
| 'setTheme'
| 'theme'
| 'updateAccountMetadata'
| 'walletType'
>
>) => {
const capture = useCaptureEvent();
Expand Down Expand Up @@ -395,20 +402,24 @@ const GeneralSettings = ({
</Box>
<Box height="15" />
<Box height="5" />
<Button
colorScheme="orange"
size="sm"
onClick={() => {
capture(Events.SettingsChangePasswordClick);
changePasswordRef.current?.openModal();
}}
>
Change Password
</Button>
<ChangePasswordModal
ref={changePasswordRef}
changePassword={changePassword}
/>
{walletType === WalletType.InMemory && (
<>
<Button
colorScheme="orange"
size="sm"
onClick={() => {
capture(Events.SettingsChangePasswordClick);
changePasswordRef.current?.openModal();
}}
>
Change Password
</Button>
<ChangePasswordModal
ref={changePasswordRef}
changePassword={changePassword}
/>
</>
)}
</>
);
};
Expand Down
10 changes: 8 additions & 2 deletions packages/nami/src/ui/indexMain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ const App = () => {
setDeletingWallet,
} = useOutsideHandles();

const { inMemoryWallet, withSignTxConfirmation, cardanoCoin, openHWFlow } =
useCommonOutsideHandles();
const {
inMemoryWallet,
withSignTxConfirmation,
cardanoCoin,
openHWFlow,
walletType,
} = useCommonOutsideHandles();

const { currency, setCurrency } = useFiatCurrency(
fiatCurrency,
Expand Down Expand Up @@ -188,6 +193,7 @@ const App = () => {
getCustomSubmitApiForNetwork={getCustomSubmitApiForNetwork}
defaultSubmitApi={defaultSubmitApi}
isValidURL={isValidURL}
walletType={walletType}
/>
</Route>
<Route path="/send">
Expand Down

0 comments on commit 7dfc3dd

Please sign in to comment.