1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Use hardware wallet constants when possible (#13634)

This commit is contained in:
David Walsh 2022-02-16 14:54:30 -06:00 committed by GitHub
parent fadad601b8
commit 009c6e1455
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 23 deletions

View File

@ -2047,7 +2047,7 @@ export default class MetamaskController extends EventEmitter {
if (deviceName === DEVICE_NAMES.LATTICE) { if (deviceName === DEVICE_NAMES.LATTICE) {
keyring.appName = 'MetaMask'; keyring.appName = 'MetaMask';
} }
if (deviceName === 'trezor') { if (deviceName === DEVICE_NAMES.TREZOR) {
const model = keyring.getModel(); const model = keyring.getModel();
this.appStateController.setTrezorModel(model); this.appStateController.setTrezorModel(model);
} }
@ -2058,7 +2058,7 @@ export default class MetamaskController extends EventEmitter {
} }
async attemptLedgerTransportCreation() { async attemptLedgerTransportCreation() {
const keyring = await this.getKeyringForDevice('ledger'); const keyring = await this.getKeyringForDevice(DEVICE_NAMES.LEDGER);
return await keyring.attemptMakeApp(); return await keyring.attemptMakeApp();
} }
@ -3720,7 +3720,7 @@ export default class MetamaskController extends EventEmitter {
transportType, transportType,
); );
const keyring = await this.getKeyringForDevice('ledger'); const keyring = await this.getKeyringForDevice(DEVICE_NAMES.LEDGER);
if (keyring?.updateTransportMethod) { if (keyring?.updateTransportMethod) {
return keyring.updateTransportMethod(newValue).catch((e) => { return keyring.updateTransportMethod(newValue).catch((e) => {
// If there was an error updating the transport, we should // If there was an error updating the transport, we should

View File

@ -9,7 +9,10 @@ import proxyquire from 'proxyquire';
import { TRANSACTION_STATUSES } from '../../shared/constants/transaction'; import { TRANSACTION_STATUSES } from '../../shared/constants/transaction';
import createTxMeta from '../../test/lib/createTxMeta'; import createTxMeta from '../../test/lib/createTxMeta';
import { NETWORK_TYPE_RPC } from '../../shared/constants/network'; import { NETWORK_TYPE_RPC } from '../../shared/constants/network';
import { KEYRING_TYPES } from '../../shared/constants/hardware-wallets'; import {
KEYRING_TYPES,
DEVICE_NAMES,
} from '../../shared/constants/hardware-wallets';
import { addHexPrefix } from './lib/util'; import { addHexPrefix } from './lib/util';
const Ganache = require('../../test/e2e/ganache'); const Ganache = require('../../test/e2e/ganache');
@ -490,7 +493,9 @@ describe('MetaMaskController', function () {
it('should add the Trezor Hardware keyring', async function () { it('should add the Trezor Hardware keyring', async function () {
sinon.spy(metamaskController.keyringController, 'addNewKeyring'); sinon.spy(metamaskController.keyringController, 'addNewKeyring');
await metamaskController.connectHardware('trezor', 0).catch(() => null); await metamaskController
.connectHardware(DEVICE_NAMES.TREZOR, 0)
.catch(() => null);
const keyrings = await metamaskController.keyringController.getKeyringsByType( const keyrings = await metamaskController.keyringController.getKeyringsByType(
KEYRING_TYPES.TREZOR, KEYRING_TYPES.TREZOR,
); );
@ -503,7 +508,9 @@ describe('MetaMaskController', function () {
it('should add the Ledger Hardware keyring', async function () { it('should add the Ledger Hardware keyring', async function () {
sinon.spy(metamaskController.keyringController, 'addNewKeyring'); sinon.spy(metamaskController.keyringController, 'addNewKeyring');
await metamaskController.connectHardware('ledger', 0).catch(() => null); await metamaskController
.connectHardware(DEVICE_NAMES.LEDGER, 0)
.catch(() => null);
const keyrings = await metamaskController.keyringController.getKeyringsByType( const keyrings = await metamaskController.keyringController.getKeyringsByType(
KEYRING_TYPES.LEDGER, KEYRING_TYPES.LEDGER,
); );
@ -531,8 +538,12 @@ describe('MetaMaskController', function () {
}); });
it('should be locked by default', async function () { it('should be locked by default', async function () {
await metamaskController.connectHardware('trezor', 0).catch(() => null); await metamaskController
const status = await metamaskController.checkHardwareStatus('trezor'); .connectHardware(DEVICE_NAMES.TREZOR, 0)
.catch(() => null);
const status = await metamaskController.checkHardwareStatus(
DEVICE_NAMES.TREZOR,
);
assert.equal(status, false); assert.equal(status, false);
}); });
}); });
@ -550,8 +561,10 @@ describe('MetaMaskController', function () {
}); });
it('should wipe all the keyring info', async function () { it('should wipe all the keyring info', async function () {
await metamaskController.connectHardware('trezor', 0).catch(() => null); await metamaskController
await metamaskController.forgetDevice('trezor'); .connectHardware(DEVICE_NAMES.TREZOR, 0)
.catch(() => null);
await metamaskController.forgetDevice(DEVICE_NAMES.TREZOR);
const keyrings = await metamaskController.keyringController.getKeyringsByType( const keyrings = await metamaskController.keyringController.getKeyringsByType(
KEYRING_TYPES.TREZOR, KEYRING_TYPES.TREZOR,
); );
@ -592,11 +605,11 @@ describe('MetaMaskController', function () {
sinon.spy(metamaskController.preferencesController, 'setSelectedAddress'); sinon.spy(metamaskController.preferencesController, 'setSelectedAddress');
sinon.spy(metamaskController.preferencesController, 'setAccountLabel'); sinon.spy(metamaskController.preferencesController, 'setAccountLabel');
await metamaskController await metamaskController
.connectHardware('trezor', 0, `m/44'/1'/0'/0`) .connectHardware(DEVICE_NAMES.TREZOR, 0, `m/44'/1'/0'/0`)
.catch(() => null); .catch(() => null);
await metamaskController.unlockHardwareWalletAccount( await metamaskController.unlockHardwareWalletAccount(
accountToUnlock, accountToUnlock,
'trezor', DEVICE_NAMES.TREZOR,
`m/44'/1'/0'/0`, `m/44'/1'/0'/0`,
); );
}); });

View File

@ -1,4 +1,5 @@
import * as actionConstants from '../../store/actionConstants'; import * as actionConstants from '../../store/actionConstants';
import { DEVICE_NAMES } from '../../../shared/constants/hardware-wallets';
import reduceApp from './app'; import reduceApp from './app';
const actions = actionConstants; const actions = actionConstants;
@ -260,7 +261,7 @@ describe('App State', () => {
const state = reduceApp(metamaskState, { const state = reduceApp(metamaskState, {
type: actions.SET_HARDWARE_WALLET_DEFAULT_HD_PATH, type: actions.SET_HARDWARE_WALLET_DEFAULT_HD_PATH,
value: { value: {
device: 'ledger', device: DEVICE_NAMES.LEDGER,
path: "m/44'/60'/0'", path: "m/44'/60'/0'",
}, },
}); });

View File

@ -8,6 +8,8 @@ import Dropdown from '../../../components/ui/dropdown';
import { getURLHostName } from '../../../helpers/utils/util'; import { getURLHostName } from '../../../helpers/utils/util';
import { DEVICE_NAMES } from '../../../../shared/constants/hardware-wallets';
class AccountList extends Component { class AccountList extends Component {
state = { state = {
pathValue: null, pathValue: null,
@ -61,9 +63,11 @@ class AccountList extends Component {
renderHeader() { renderHeader() {
const { device } = this.props; const { device } = this.props;
const shouldShowHDPaths = ['ledger', 'lattice', 'trezor'].includes( const shouldShowHDPaths = [
device.toLowerCase(), DEVICE_NAMES.LEDGER,
); DEVICE_NAMES.LATTICE,
DEVICE_NAMES.TREZOR,
].includes(device.toLowerCase());
return ( return (
<div className="hw-connect"> <div className="hw-connect">
<h3 className="hw-connect__unlock-title"> <h3 className="hw-connect__unlock-title">

View File

@ -398,7 +398,7 @@ export function connectHardware(deviceName, page, hdPath, t) {
let accounts; let accounts;
try { try {
if (deviceName === 'ledger') { if (deviceName === DEVICE_NAMES.LEDGER) {
await promisifiedBackground.establishLedgerTransportPreference(); await promisifiedBackground.establishLedgerTransportPreference();
} }
if ( if (
@ -424,7 +424,7 @@ export function connectHardware(deviceName, page, hdPath, t) {
} catch (error) { } catch (error) {
log.error(error); log.error(error);
if ( if (
deviceName === 'ledger' && deviceName === DEVICE_NAMES.LEDGER &&
ledgerTransportType === LEDGER_TRANSPORT_TYPES.WEBHID && ledgerTransportType === LEDGER_TRANSPORT_TYPES.WEBHID &&
error.message.match('Failed to open the device') error.message.match('Failed to open the device')
) { ) {

View File

@ -4,6 +4,7 @@ import thunk from 'redux-thunk';
import enLocale from '../../app/_locales/en/messages.json'; import enLocale from '../../app/_locales/en/messages.json';
import MetaMaskController from '../../app/scripts/metamask-controller'; import MetaMaskController from '../../app/scripts/metamask-controller';
import { TRANSACTION_STATUSES } from '../../shared/constants/transaction'; import { TRANSACTION_STATUSES } from '../../shared/constants/transaction';
import { DEVICE_NAMES } from '../../shared/constants/hardware-wallets';
import { GAS_LIMITS } from '../../shared/constants/gas'; import { GAS_LIMITS } from '../../shared/constants/gas';
import * as actions from './actions'; import * as actions from './actions';
@ -436,7 +437,7 @@ describe('Actions', () => {
actions._setBackgroundConnection(background); actions._setBackgroundConnection(background);
await store.dispatch( await store.dispatch(
actions.checkHardwareStatus('ledger', `m/44'/60'/0'/0`), actions.checkHardwareStatus(DEVICE_NAMES.LEDGER, `m/44'/60'/0'/0`),
); );
expect(checkHardwareStatus.callCount).toStrictEqual(1); expect(checkHardwareStatus.callCount).toStrictEqual(1);
}); });
@ -476,7 +477,7 @@ describe('Actions', () => {
actions._setBackgroundConnection(background); actions._setBackgroundConnection(background);
await store.dispatch(actions.forgetDevice('ledger')); await store.dispatch(actions.forgetDevice(DEVICE_NAMES.LEDGER));
expect(forgetDevice.callCount).toStrictEqual(1); expect(forgetDevice.callCount).toStrictEqual(1);
}); });
@ -518,7 +519,7 @@ describe('Actions', () => {
actions._setBackgroundConnection(background); actions._setBackgroundConnection(background);
await store.dispatch( await store.dispatch(
actions.connectHardware('ledger', 0, `m/44'/60'/0'/0`), actions.connectHardware(DEVICE_NAMES.LEDGER, 0, `m/44'/60'/0'/0`),
); );
expect(connectHardware.callCount).toStrictEqual(1); expect(connectHardware.callCount).toStrictEqual(1);
}); });
@ -544,7 +545,7 @@ describe('Actions', () => {
]; ];
await expect( await expect(
store.dispatch(actions.connectHardware('ledger')), store.dispatch(actions.connectHardware(DEVICE_NAMES.LEDGER)),
).rejects.toThrow('error'); ).rejects.toThrow('error');
expect(store.getActions()).toStrictEqual(expectedActions); expect(store.getActions()).toStrictEqual(expectedActions);
@ -567,7 +568,7 @@ describe('Actions', () => {
await store.dispatch( await store.dispatch(
actions.unlockHardwareWalletAccounts( actions.unlockHardwareWalletAccounts(
[0], [0],
'ledger', DEVICE_NAMES.LEDGER,
`m/44'/60'/0'/0`, `m/44'/60'/0'/0`,
'', '',
), ),