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

feat: Enable hardware wallets for smart transactions in swaps #25742

Merged
merged 1 commit into from
Jul 19, 2024
Merged
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
8 changes: 8 additions & 0 deletions app/scripts/lib/transaction/smart-transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ describe('submitSmartTransactionHook', () => {
smartTransactionsController: createSmartTransactionsControllerMock(),
transactionController: createTransactionControllerMock(),
isSmartTransaction: true,
isHardwareWallet: false,
controllerMessenger: createSmartTransactionsControllerMessengerMock(),
featureFlags: {
extensionActive: true,
Expand Down Expand Up @@ -148,6 +149,13 @@ describe('submitSmartTransactionHook', () => {
expect(result).toEqual({ transactionHash: undefined });
});

it('falls back to regular transaction submit if a hardware wallet is used', async () => {
const request: SubmitSmartTransactionRequestMocked = createRequest();
request.isHardwareWallet = true;
const result = await submitSmartTransactionHook(request);
expect(result).toEqual({ transactionHash: undefined });
});

it('falls back to regular transaction submit if /getFees throws an error', async () => {
const request: SubmitSmartTransactionRequestMocked = createRequest();
jest
Expand Down
6 changes: 6 additions & 0 deletions app/scripts/lib/transaction/smart-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export type SubmitSmartTransactionRequest = {
smartTransactionsController: SmartTransactionsController;
transactionController: TransactionController;
isSmartTransaction: boolean;
isHardwareWallet: boolean;
controllerMessenger: SmartTransactionsControllerMessenger;
featureFlags: FeatureFlags;
};
Expand Down Expand Up @@ -90,6 +91,8 @@ class SmartTransactionHook {

#isSmartTransaction: boolean;

#isHardwareWallet: boolean;

#smartTransactionsController: SmartTransactionsController;

#transactionController: TransactionController;
Expand All @@ -104,6 +107,7 @@ class SmartTransactionHook {
smartTransactionsController,
transactionController,
isSmartTransaction,
isHardwareWallet,
controllerMessenger,
featureFlags,
} = request;
Expand All @@ -113,6 +117,7 @@ class SmartTransactionHook {
this.#smartTransactionsController = smartTransactionsController;
this.#transactionController = transactionController;
this.#isSmartTransaction = isSmartTransaction;
this.#isHardwareWallet = isHardwareWallet;
this.#controllerMessenger = controllerMessenger;
this.#featureFlags = featureFlags;
this.#isDapp = transactionMeta.origin !== ORIGIN_METAMASK;
Expand All @@ -132,6 +137,7 @@ class SmartTransactionHook {
const useRegularTransactionSubmit = { transactionHash: undefined };
if (
!this.#isSmartTransaction ||
this.#isHardwareWallet ||
dan437 marked this conversation as resolved.
Show resolved Hide resolved
isUnsupportedTransactionTypeForSmartTransaction
) {
return useRegularTransactionSubmit;
Expand Down
2 changes: 2 additions & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ import { isManifestV3 } from '../../shared/modules/mv3.utils';
import { convertNetworkId } from '../../shared/modules/network.utils';
import {
getIsSmartTransaction,
isHardwareWallet,
getFeatureFlagsByChainId,
getSmartTransactionsOptInStatus,
getCurrentChainSupportsSmartTransactions,
Expand Down Expand Up @@ -6748,6 +6749,7 @@ export default class MetamaskController extends EventEmitter {
smartTransactionsController: this.smartTransactionsController,
controllerMessenger: this.controllerMessenger,
isSmartTransaction,
isHardwareWallet: isHardwareWallet(state),
featureFlags,
});
}
Expand Down
3 changes: 3 additions & 0 deletions shared/modules/selectors/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { isHardwareWallet } from '../../../ui/selectors/selectors';

export { isHardwareWallet };
4 changes: 2 additions & 2 deletions shared/modules/selectors/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('Selectors', () => {
expect(getSmartTransactionsEnabled(state)).toBe(false);
});

it('returns false if feature flag is enabled, is a HW and is Ethereum network', () => {
it('returns true if feature flag is enabled, is a HW and is Ethereum network', () => {
const state = createSwapsMockStore();
const newState = {
...state,
Expand All @@ -150,7 +150,7 @@ describe('Selectors', () => {
},
},
};
expect(getSmartTransactionsEnabled(newState)).toBe(false);
expect(getSmartTransactionsEnabled(newState)).toBe(true);
});

it('returns false if feature flag is enabled, not a HW and is Polygon network', () => {
Expand Down
1 change: 1 addition & 0 deletions shared/modules/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './smart-transactions';
export * from './feature-flags';
export * from './token-auto-detect';
export * from './nft-auto-detect';
export * from './account';
3 changes: 1 addition & 2 deletions ui/selectors/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ export function isHardwareWallet(state) {
*/
export function accountSupportsSmartTx(state) {
const accountType = getAccountType(state);

return Boolean(accountType !== 'hardware' && accountType !== 'snap');
return Boolean(accountType !== 'snap');
}

/**
Expand Down
40 changes: 40 additions & 0 deletions ui/selectors/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,46 @@ describe('Selectors', () => {
const mockStateWithTrezor = modifyStateWithHWKeyring(KeyringType.trezor);
expect(selectors.isHardwareWallet(mockStateWithTrezor)).toBe(true);
});

it('returns true if it is a Lattice HW wallet', () => {
const mockStateWithLattice = modifyStateWithHWKeyring(
KeyringType.lattice,
);
expect(selectors.isHardwareWallet(mockStateWithLattice)).toBe(true);
});

it('returns true if it is a QR HW wallet', () => {
const mockStateWithQr = modifyStateWithHWKeyring(KeyringType.qr);
expect(selectors.isHardwareWallet(mockStateWithQr)).toBe(true);
});
});

describe('#accountSupportsSmartTx', () => {
it('returns false if the account type is "snap"', () => {
const state = {
metamask: {
internalAccounts: {
accounts: {
'mock-id-1': {
address: '0x987654321',
metadata: {
name: 'Account 1',
keyring: {
type: 'Snap Keyring',
},
},
},
},
selectedAccount: 'mock-id-1',
},
},
};
expect(selectors.accountSupportsSmartTx(state)).toBe(false);
});

it('returns true if the account type is not "snap"', () => {
expect(selectors.accountSupportsSmartTx(mockState)).toBe(true);
});
});

describe('#getHardwareWalletType', () => {
Expand Down