Skip to content

Commit

Permalink
feat(nami): [lw-11600] remove reset wallet option
Browse files Browse the repository at this point in the history
  • Loading branch information
vetalcore committed Oct 2, 2024
1 parent 4cb0754 commit de63c71
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 172 deletions.
64 changes: 3 additions & 61 deletions packages/nami/src/adapters/wallet.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { renderHook, act } from '@testing-library/react-hooks';
import { BehaviorSubject, of } from 'rxjs';
import { renderHook } from '@testing-library/react-hooks';
import { of } from 'rxjs';

import { ERROR } from '../config/config';

import { useChangePassword, useDeleteWalletWithPassword } from './wallet';
import { useChangePassword } from './wallet';

import type {
WalletManagerApi,
Expand All @@ -17,64 +17,6 @@ const extendedAccountPublicKey =
'2ac1f1560a893ea7937c5bfbfdeab459' +
'b1a396f1174b9c5a673a640d01880c35';

describe('useDeleteWalletWithPassword', () => {
const mockDeleteWallet = jest.fn();
const mockEmip3decrypt = jest.fn();
const mockActiveWalletId$ = of({
walletId: 'wallet1',
accountIndex: 0,
}) as WalletManagerApi['activeWalletId$'];
const mockWallets$ = of([
{
walletId: 'wallet1',
encryptedSecrets: { keyMaterial: '1234' },
},
]) as WalletRepositoryApi<
Wallet.WalletMetadata,
Wallet.AccountMetadata
>['wallets$'];

beforeEach(() => {
jest.clearAllMocks();
});

it('should call deleteWallet when the password is correct', async () => {
mockEmip3decrypt.mockResolvedValue(new Uint8Array());
const { result } = renderHook(() =>
useDeleteWalletWithPassword({
wallets$: mockWallets$,
activeWalletId$: mockActiveWalletId$,
deleteWallet: mockDeleteWallet,
emip3decrypt: mockEmip3decrypt,
}),
);

await result.current('correct-password');

expect(mockEmip3decrypt).toHaveBeenCalledWith(
Buffer.from('1234', 'hex'),
Buffer.from('correct-password'),
);
expect(mockDeleteWallet).toHaveBeenCalledWith(false);
});

it('should throw an error when the password is incorrect', async () => {
mockEmip3decrypt.mockRejectedValue(new Error('error'));
const { result } = renderHook(() =>
useDeleteWalletWithPassword({
wallets$: mockWallets$,
activeWalletId$: mockActiveWalletId$,
deleteWallet: mockDeleteWallet,
emip3decrypt: mockEmip3decrypt,
}),
);
await expect(result.current('wrong-password')).rejects.toEqual(
ERROR.wrongPassword,
);
expect(mockDeleteWallet).not.toHaveBeenCalled();
});
});

describe('useChangePassword', () => {
const mockCreateWallet = jest.fn();
const mockGetMnemonic = jest.fn();
Expand Down
54 changes: 0 additions & 54 deletions packages/nami/src/adapters/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,6 @@ import type {
} from '@cardano-sdk/web-extension';
import type { Observable } from 'rxjs';

const clearBytes = (bytes: Uint8Array) => {
for (let i = 0; i < bytes.length; i++) {
bytes[i] = 0;
}
};

interface DeleteWalletProps {
wallets$: Observable<
AnyWallet<Wallet.WalletMetadata, Wallet.AccountMetadata>[]
>;
activeWalletId$: Readonly<WalletManagerApi['activeWalletId$']>;
deleteWallet: (
isForgotPasswordFlow?: boolean,
) => Promise<WalletManagerActivateProps | undefined>;
emip3decrypt?: (
encrypted: Uint8Array,
passphrase: Uint8Array,
) => Promise<Uint8Array>;
}

export const useDeleteWalletWithPassword = ({
wallets$,
activeWalletId$,
deleteWallet,
emip3decrypt = Wallet.KeyManagement.emip3decrypt,
}: Readonly<DeleteWalletProps>) => {
const activeWallet = useObservable(activeWalletId$);
const wallets = useObservable(wallets$);
const { walletId } = activeWallet ?? {};
const wallet = wallets?.find(elm => elm.walletId === walletId);

return useCallback(
async (password: string) => {
if (!wallet) {
return;
}

try {
if ('encryptedSecrets' in wallet) {
const keyMaterialBytes = await emip3decrypt(
Buffer.from(wallet.encryptedSecrets.keyMaterial, 'hex'),
Buffer.from(password),
);
clearBytes(keyMaterialBytes);
await deleteWallet(false);
}
} catch {
throw ERROR.wrongPassword;
}
},
[wallet, deleteWallet],
);
};

interface ChangePasswordProps {
chainId: Wallet.Cardano.ChainId;
createWallet: (
Expand Down
4 changes: 0 additions & 4 deletions packages/nami/src/features/analytics/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ export enum Events {
SettingsNetworkMainnetClick = 'nami mode | settings | network | mainnet | click',
SettingsNetworkCustomNodeClick = 'nami mode | settings | network | custom node | click',

SettingsRemoveWalletClick = 'nami mode | settings | remove wallet | click',
SettingsHoldUpRemoveWalletClick = 'nami mode | settings | hold up | remove wallet | click',
SettingsHoldUpBackClick = 'nami mode | settings | hold up | back | click',

SettingsThemeLightModeClick = 'nami mode | settings | theme | light mode | click',
SettingsThemeDarkModeClick = 'nami mode | settings | theme | dark mode | click',

Expand Down
2 changes: 1 addition & 1 deletion packages/nami/src/ui/app/pages/send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ const Send = ({
return (
<>
<Box
height="100vh"
height="calc(100vh - 30px)"
display="flex"
alignItems="center"
flexDirection="column"
Expand Down
41 changes: 1 addition & 40 deletions packages/nami/src/ui/app/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ type Props = Pick<
currentPassword: string,
newPassword: string,
) => Promise<void>;
deleteWallet: (password: string) => Promise<void>;
accountName: string;
accountAvatar?: string;
updateAccountMetadata: UseAccount['updateAccountMetadata'];
Expand All @@ -84,7 +83,6 @@ const Settings = ({
removeDapp,
handleAnalyticsChoice,
changePassword,
deleteWallet,
updateAccountMetadata,
environmentName,
switchNetwork,
Expand All @@ -103,7 +101,7 @@ const Settings = ({

return (
<Box
minHeight="100vh"
minHeight="calc(100vh - 30px)"
display="flex"
alignItems="center"
flexDirection="column"
Expand All @@ -127,7 +125,6 @@ const Settings = ({
<Route path="/settings/general">
<GeneralSettings
changePassword={changePassword}
deleteWallet={deleteWallet}
currency={currency}
setCurrency={setCurrency}
theme={theme}
Expand Down Expand Up @@ -273,7 +270,6 @@ const GeneralSettings = ({
accountName,
accountAvatar,
changePassword,
deleteWallet,
updateAccountMetadata,
}: Readonly<
Pick<
Expand All @@ -282,7 +278,6 @@ const GeneralSettings = ({
| 'accountName'
| 'changePassword'
| 'currency'
| 'deleteWallet'
| 'setCurrency'
| 'setTheme'
| 'theme'
Expand All @@ -293,7 +288,6 @@ const GeneralSettings = ({
const [name, setName] = useState(accountName);
const [originalName, setOriginalName] = useState(accountName);
const { setColorMode } = useColorMode();
const ref = useRef();
const changePasswordRef = useRef();

const nameHandler = async () => {
Expand Down Expand Up @@ -407,39 +401,6 @@ const GeneralSettings = ({
>
Change Password
</Button>
<Box height="10" />
<Button
size="xs"
colorScheme="red"
variant="link"
onClick={() => {
capture(Events.SettingsRemoveWalletClick);
ref.current.openModal();
}}
>
Reset Wallet
</Button>
<ConfirmModal
info={
<Box mb="4" fontSize="sm" width="full">
The wallet will be reset.{' '}
<b>Make sure you have written down your seed phrase.</b> It's the
only way to recover your current wallet! <br />
Type your password below, if you want to continue.
</Box>
}
ref={ref}
onCloseBtn={() => {
capture(Events.SettingsHoldUpBackClick);
}}
sign={async password => {
capture(Events.SettingsHoldUpRemoveWalletClick);
return deleteWallet(password);
}}
onConfirm={async status => {
if (status) window.close();
}}
/>
<ChangePasswordModal
ref={changePasswordRef}
changePassword={changePassword}
Expand Down
3 changes: 2 additions & 1 deletion packages/nami/src/ui/app/pages/wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ const Wallet = ({
<>
<Box
background={containerBg}
minHeight="100vh"
minHeight="calc(100vh - 30px)"
display="flex"
alignItems="center"
flexDirection="column"
ma
>
<Box
height="52"
Expand Down
12 changes: 1 addition & 11 deletions packages/nami/src/ui/indexMain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import { useAccount } from '../adapters/account';
import { useAssets } from '../adapters/assets';
import { useBalance } from '../adapters/balance';
import { useFiatCurrency } from '../adapters/currency';
import {
useChangePassword,
useDeleteWalletWithPassword,
} from '../adapters/wallet';
import { useChangePassword } from '../adapters/wallet';

import Send from './app/pages/send';
import Settings from './app/pages/settings';
Expand Down Expand Up @@ -58,12 +55,6 @@ export const Main = () => {
setFiatCurrency,
);

const deleteWalletWithPassword = useDeleteWalletWithPassword({
wallets$: walletRepository.wallets$,
activeWalletId$: walletManager.activeWalletId$,
deleteWallet,
});

const changePassword = useChangePassword({
chainId: currentChain,
wallets$: walletRepository.wallets$,
Expand Down Expand Up @@ -112,7 +103,6 @@ export const Main = () => {
removeDapp={removeDapp}
connectedDapps={connectedDapps}
changePassword={changePassword}
deleteWallet={deleteWalletWithPassword}
currency={currency}
setCurrency={setCurrency}
theme={theme}
Expand Down

0 comments on commit de63c71

Please sign in to comment.