Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove unused onboarding code #1173

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import SpyInstance = jest.SpyInstance;
const mockEmip3decrypt = jest.fn();
const mockEmip3encrypt = jest.fn();
const mockConnectDevice = jest.fn();
const mockConnectDeviceByUsbDeviceObject = jest.fn();
const mockGetHwExtendedAccountPublicKey = jest.fn();
const mockRestoreWalletFromKeyAgent = jest.fn();
const mockSwitchKeyAgents = jest.fn();
Expand Down Expand Up @@ -81,6 +82,7 @@ jest.mock('@lace/cardano', () => {
restoreWalletFromKeyAgent: mockRestoreWalletFromKeyAgent,
switchKeyAgents: mockSwitchKeyAgents,
connectDevice: mockConnectDevice,
connectDeviceByUsbDeviceObject: mockConnectDeviceByUsbDeviceObject,
getHwExtendedAccountPublicKey: mockGetHwExtendedAccountPublicKey,
KeyManagement: {
...actual.Wallet.KeyManagement,
Expand Down Expand Up @@ -397,7 +399,6 @@ describe('Testing useWalletManager hook', () => {
networkMagic: 0
}
}));
const connectedDevice = 'Ledger' as any;
const deviceConnection = 'deviceConnection' as any;

const {
Expand All @@ -408,10 +409,12 @@ describe('Testing useWalletManager hook', () => {
wrapper: getWrapper({})
});
await createHardwareWallet({
deviceConnection,
accountIndex,
name,
connectedDevice
connection: {
type: WalletType.Ledger,
value: deviceConnection
}
});
expect(walletApiUi.walletRepository.addWallet).toBeCalledTimes(1);
expect(walletApiUi.walletManager.activate).toBeCalledTimes(1);
Expand All @@ -420,10 +423,11 @@ describe('Testing useWalletManager hook', () => {

describe('connectHardwareWallet', () => {
test('should call proper connect method', async () => {
const model = 'Trezor' as any;
const mockConnectDeviceMockedResult = 'mockConnectDeviceMocked';
const mockConnectDeviceMocked = jest.fn().mockReturnValue(mockConnectDeviceMockedResult);
mockConnectDevice.mockImplementation(mockConnectDeviceMocked);
mockConnectDeviceByUsbDeviceObject.mockImplementation(mockConnectDeviceMocked);

const usbDevice = Wallet.ledgerDescriptors[0] as USBDevice;

const {
result: {
Expand All @@ -433,8 +437,8 @@ describe('Testing useWalletManager hook', () => {
wrapper: getWrapper({})
});
expect(connectHardwareWallet).toBeDefined();
expect(await connectHardwareWallet(model)).toEqual(mockConnectDeviceMockedResult);
expect(mockConnectDeviceMocked).toBeCalledWith(model);
expect(await connectHardwareWallet(usbDevice)).toEqual(mockConnectDeviceMockedResult);
expect(mockConnectDeviceMocked).toBeCalledWith(usbDevice);
});
});

Expand Down
55 changes: 9 additions & 46 deletions apps/browser-extension-wallet/src/hooks/useWalletManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ export interface CreateWalletParams {
chainId?: Wallet.Cardano.ChainId;
}

export interface CreateHardwareWallet {
accountIndex?: number;
name: string;
deviceConnection: Wallet.DeviceConnection;
connectedDevice: Wallet.HardwareWallets;
}

type WalletManagerAddAccountProps = {
wallet: AnyBip32Wallet<Wallet.WalletMetadata, Wallet.AccountMetadata>;
metadata: Wallet.AccountMetadata;
Expand All @@ -62,13 +55,13 @@ type WalletManagerAddAccountProps = {

type ActivateWalletProps = Omit<WalletManagerActivateProps, 'chainId'>;

type CreateHardwareWalletRevampedParams = {
type ConnectHardwareWallet = (usbDevice: USBDevice) => Promise<Wallet.HardwareWalletConnection>;

type CreateHardwareWallet = (params: {
accountIndex: number;
name: string;
connection: Wallet.HardwareWalletConnection;
};

type CreateHardwareWalletRevamped = (params: CreateHardwareWalletRevampedParams) => Promise<Wallet.CardanoWallet>;
}) => Promise<Wallet.CardanoWallet>;

export interface UseWalletManager {
walletManager: WalletManagerApi;
Expand All @@ -81,10 +74,8 @@ export interface UseWalletManager {
) => Promise<Wallet.CardanoWallet | null>;
createWallet: (args: CreateWalletParams) => Promise<Wallet.CardanoWallet>;
activateWallet: (args: Omit<WalletManagerActivateProps, 'chainId'>) => Promise<void>;
createHardwareWallet: (args: CreateHardwareWallet) => Promise<Wallet.CardanoWallet>;
createHardwareWalletRevamped: CreateHardwareWalletRevamped;
connectHardwareWallet: (model: Wallet.HardwareWallets) => Promise<Wallet.DeviceConnection>;
connectHardwareWalletRevamped: typeof connectHardwareWalletRevamped;
createHardwareWallet: CreateHardwareWallet;
connectHardwareWallet: ConnectHardwareWallet;
saveHardwareWallet: (wallet: Wallet.CardanoWallet, chainName?: Wallet.ChainName) => Promise<void>;
/**
* @returns active wallet id after deleting the wallet; undefined if deleted the last wallet
Expand Down Expand Up @@ -198,12 +189,8 @@ const encryptMnemonic = async (mnemonic: string[], passphrase: Uint8Array) => {
return HexBlob.fromBytes(walletEncrypted);
};

/** Connects a hardware wallet device */
export const connectHardwareWallet = async (model: Wallet.HardwareWallets): Promise<Wallet.DeviceConnection> =>
await Wallet.connectDevice(model);

const connectHardwareWalletRevamped = async (usbDevice: USBDevice): Promise<Wallet.HardwareWalletConnection> =>
Wallet.connectDeviceRevamped(usbDevice);
const connectHardwareWallet = async (usbDevice: USBDevice): Promise<Wallet.HardwareWalletConnection> =>
Wallet.connectDeviceByUsbDeviceObject(usbDevice);

export const useWalletManager = (): UseWalletManager => {
const {
Expand Down Expand Up @@ -237,7 +224,7 @@ export const useWalletManager = (): UseWalletManager => {
return (storedChain?.chainName && chainIdFromName(storedChain.chainName)) || DEFAULT_CHAIN_ID;
}, [currentChain]);

const createHardwareWalletRevamped = useCallback<CreateHardwareWalletRevamped>(
const createHardwareWallet = useCallback<CreateHardwareWallet>(
async ({ accountIndex, connection, name }) => {
let extendedAccountPublicKey;
try {
Expand Down Expand Up @@ -283,28 +270,6 @@ export const useWalletManager = (): UseWalletManager => {
[getCurrentChainId]
);

/**
* Creates a Ledger or Trezor hardware wallet
* and saves it in browser storage with the data to lock/unlock it
*/
const createHardwareWallet = useCallback(
async ({
accountIndex = 0,
deviceConnection,
name,
connectedDevice
}: CreateHardwareWallet): Promise<Wallet.CardanoWallet> =>
createHardwareWalletRevamped({
accountIndex,
connection: {
type: connectedDevice,
value: typeof deviceConnection !== 'boolean' ? deviceConnection : undefined
},
name
}),
[createHardwareWalletRevamped]
);

const tryMigrateToWalletRepository = useCallback(async (): Promise<
AnyWallet<Wallet.WalletMetadata, Wallet.AccountMetadata>[] | undefined
> => {
Expand Down Expand Up @@ -787,9 +752,7 @@ export const useWalletManager = (): UseWalletManager => {
loadWallet,
createWallet,
createHardwareWallet,
createHardwareWalletRevamped,
connectHardwareWallet,
connectHardwareWalletRevamped,
saveHardwareWallet,
deleteWallet,
reloadWallet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const onboardingActions = {
WALLET_ADDED: makeOnboardingRestoreEvent('added'),
HD_WALLET: makeOnboardingRestoreEvent('hd wallet')
},
hw: {
hardware: {
SETUP_OPTION_CLICK: makeOnboardingHardwareEvent('connect | click'),
CONNECT_HW_VIEW: makeOnboardingHardwareEvent('connect your device | view'),
HW_POPUP_CONNECT_CLICK: makeOnboardingHardwareEvent('native browser pop-up with HWs | connect | click'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type ValidateOnboardingActionsStructure<
T extends {
create: CreateFlowActions;
restore: RestoreFlowActions;
hw: HardwareFlowActions;
hardware: HardwareFlowActions;
// eslint-disable-next-line camelcase
forgot_password: Record<
'ENTER_RECOVERY_PHRASE_NEXT_CLICK' | 'ENTER_WALLET' | 'RECOVERY_PHRASE_PASTE_FROM_CLIPBOARD_CLICK',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { DrawerContent, DrawerUIContainer } from '../Drawer';
import { useNetworkError } from '@hooks/useNetworkError';
import { LeftSidePanel } from '../LeftSidePanel';
import styles from './Layout.module.scss';
import { PinExtension } from '@views/browser/features/wallet-setup/components/PinExtension';
import { PinExtension } from '@views/browser/features/wallet-setup/PinExtension';
import { useLocalStorage } from '@hooks';
import { useWalletStore } from '@stores';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WalletSetupNamePasswordStepRevamp } from '@lace/core';
import { WalletSetupNamePasswordStep } from '@lace/core';
import { useAnalyticsContext } from '@providers';
import React from 'react';
import { useTranslation } from 'react-i18next';
Expand Down Expand Up @@ -31,7 +31,7 @@ export const Setup = (): JSX.Element => {
};

return (
<WalletSetupNamePasswordStepRevamp
<WalletSetupNamePasswordStep
initialWalletName={createWalletData.name}
onChange={onNameAndPasswordChange}
onBack={back}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,14 @@ const createStep = async () => {
};

describe('Multi Wallet Setup/Hardware Wallet', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const originalUsbDeviceClass = globalThis.USBDevice;
const originalNavigatorUsbObject = navigator.usb;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let addEventListenerCallback: (event: { device: any }) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let deviceObject: any;

beforeEach(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
globalThis.USBDevice = class USBDevice {};

jest.spyOn(Wallet, 'connectDeviceRevamped').mockImplementation(() =>
jest.spyOn(Wallet, 'connectDeviceByUsbDeviceObject').mockImplementation(() =>
Promise.resolve({
type: WalletType.Ledger,
value: {
Expand All @@ -107,8 +100,7 @@ describe('Multi Wallet Setup/Hardware Wallet', () => {
})
);

const nanoS = Wallet.ledgerDescriptors[0];
deviceObject = Object.assign(new USBDevice(), nanoS);
deviceObject = Wallet.ledgerDescriptors[0];
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore usb api is not available in the jest env
navigator.usb = {
Expand All @@ -121,9 +113,6 @@ describe('Multi Wallet Setup/Hardware Wallet', () => {
});

afterEach(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
globalThis.USBDevice = originalUsbDeviceClass;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore usb api is not available in the jest env
navigator.usb = originalNavigatorUsbObject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StartOverDialog } from '@views/browser/features/wallet-setup/components/StartOverDialog';
import React from 'react';
import { HardwareWalletProvider } from './context';
import { ErrorDialog } from './ErrorDialog';
import { StartOverDialog } from './StartOverDialog';
import { Connect } from './steps/Connect';
import { Setup } from './steps/Setup';
import { Create } from './steps/Create';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@import '../../../../../../../../packages/common/src/ui/styles/theme.scss';
@import '../../../../../../../../packages/common/src/ui/styles/theme';
@import '../../../../../../../../packages/common/src/ui/styles/abstracts/typography';

.startOverDialog {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ export const HardwareWalletProvider = ({ children }: HardwareWalletProviderProps
const history = useHistory();
const { aliasEventRequired, mergeEventRequired } = useWalletOnboarding();
const { postHogActions, setFormDirty } = useWalletOnboarding();
const { connectHardwareWalletRevamped, createHardwareWalletRevamped, saveHardwareWallet, walletRepository } =
useWalletManager();
const { connectHardwareWallet, createHardwareWallet, saveHardwareWallet, walletRepository } = useWalletManager();
const [step, setStep] = useState<WalletConnectStep>(WalletConnectStep.Connect);
const [connectedUsbDevice, setConnectedUsbDevice] = useState<USBDevice>();
const [errorDialogCode, setErrorDialogCode] = useState<ErrorDialogCode>();
Expand Down Expand Up @@ -144,7 +143,7 @@ export const HardwareWalletProvider = ({ children }: HardwareWalletProviderProps
[cleanupConnectionState]
);

const connectDevice = useWrapWithTimeout(connectHardwareWalletRevamped);
const connectDevice = useWrapWithTimeout(connectHardwareWallet);

const connect = useCallback(
async (usbDevice: USBDevice) => {
Expand All @@ -168,7 +167,7 @@ export const HardwareWalletProvider = ({ children }: HardwareWalletProviderProps
const createWallet = useCallback(async () => {
let cardanoWallet: Wallet.CardanoWallet;
try {
cardanoWallet = await createHardwareWalletRevamped({
cardanoWallet = await createHardwareWallet({
connection,
...walletData
});
Expand Down Expand Up @@ -222,7 +221,7 @@ export const HardwareWalletProvider = ({ children }: HardwareWalletProviderProps
analytics,
closeConnection,
connection,
createHardwareWalletRevamped,
createHardwareWallet,
postHogActions.hardware.HD_WALLET,
postHogActions.hardware.WALLET_ADDED,
saveHardwareWallet,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WalletSetupSelectAccountsStepRevamp } from '@lace/core';
import { WalletSetupSelectAccountsStep } from '@lace/core';
import React, { VFC } from 'react';
import { useHardwareWallet } from '@views/browser/features/multi-wallet/hardware-wallet/context';
import { useHardwareWallet } from '@views/browser/components/WalletOnboardingFlows/hardware-wallet/context';
import { useAnalyticsContext } from '@providers';
import { useWalletOnboarding } from '../../walletOnboardingContext';

Expand All @@ -11,7 +11,7 @@ export const Setup: VFC = () => {
const { postHogActions } = useWalletOnboarding();
const { back, next, onNameAndAccountChange } = useHardwareWallet();
return (
<WalletSetupSelectAccountsStepRevamp
<WalletSetupSelectAccountsStep
accounts={TOTAL_ACCOUNTS}
onBack={back}
onSubmit={(accountIndex, name) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { WalletOnboardingFlows } from './WalletOnboardingFlows';
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { UseWalletManager } from '@hooks/useWalletManager';
import { AnalyticsTracker, postHogMultiWalletActions } from '@providers/AnalyticsProvider/analyticsTracker';
import { MemoryRouter } from 'react-router-dom';
import { walletRoutePaths } from '@routes';
import { WalletOnboardingFlows } from '@views/browser/features/multi-wallet/WalletOnboardingFlows';
import { WalletOnboardingFlows } from '@views/browser/components/WalletOnboardingFlows/WalletOnboardingFlows';

jest.mock('@providers/AnalyticsProvider', () => ({
useAnalyticsContext: jest.fn<Pick<AnalyticsTracker, 'sendMergeEvent' | 'sendEventToPostHog'>, []>().mockReturnValue({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WalletSetupNamePasswordStepRevamp } from '@lace/core';
import { WalletSetupNamePasswordStep } from '@lace/core';
import React from 'react';
import { useRestoreWallet } from '../context';
import { useTranslation } from 'react-i18next';
Expand Down Expand Up @@ -49,7 +49,7 @@ export const Setup = (): JSX.Element => {
};

return (
<WalletSetupNamePasswordStepRevamp
<WalletSetupNamePasswordStep
initialWalletName={createWalletData.name}
onChange={onNameAndPasswordChange}
onBack={back}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ export type Flows = 'create' | 'restore' | 'hardware';

export type WalletOnboardingPostHogActions =
| PostHogMultiWalletActions
| (Pick<PostHogOnboardingActions, 'create' | 'restore'> & Record<'hardware', PostHogOnboardingActions['hw']>);
| Pick<PostHogOnboardingActions, 'create' | 'restore' | 'hardware'>;
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './SocialNetworks';
export * from './WalletUsedAddressesDrawer';
export * from './TopUpWallet';
export * from './EmptySearch';
export * from './WalletOnboardingFlows';
Loading
Loading