From 9530797a7a1ddbc0444f2bfa3475019d6355e420 Mon Sep 17 00:00:00 2001 From: David Walsh Date: Mon, 21 Nov 2022 08:23:35 -0600 Subject: [PATCH] Create constants for all keyring types (#16575) --- .storybook/test-data.js | 12 ++++--- app/scripts/lib/seed-phrase-verifier.js | 6 +++- app/scripts/lib/seed-phrase-verifier.test.js | 3 +- app/scripts/metamask-controller.js | 36 +++++++++++-------- app/scripts/metamask-controller.test.js | 8 ++--- shared/constants/hardware-wallets.js | 3 +- shared/constants/keyrings.js | 7 ++++ test/jest/mock-store.js | 7 ++-- .../app/account-menu/account-menu.test.js | 5 +-- .../app/account-menu/keyring-label.js | 6 ++-- .../unconnected-account-alert.test.js | 4 ++- .../app/menu-bar/account-options-menu.js | 3 +- ui/components/app/menu-bar/menu-bar.test.js | 3 +- .../wallet-overview/token-overview.test.js | 5 +-- ui/ducks/metamask/metamask.js | 2 +- ui/ducks/send/send.test.js | 3 +- ui/pages/mobile-sync/mobile-sync.component.js | 3 +- ui/pages/send/send.test.js | 3 +- ui/selectors/selectors.js | 4 +-- ui/selectors/selectors.test.js | 6 ++-- 20 files changed, 77 insertions(+), 52 deletions(-) create mode 100644 shared/constants/keyrings.js diff --git a/.storybook/test-data.js b/.storybook/test-data.js index 97984fa13..eabd90864 100644 --- a/.storybook/test-data.js +++ b/.storybook/test-data.js @@ -1,4 +1,6 @@ import { draftTransactionInitialState } from '../ui/ducks/send'; +import { KEYRING_TYPES } from '../shared/constants/keyrings'; + const state = { invalidCustomNetwork: { state: 'CLOSED', @@ -1114,14 +1116,14 @@ const state = { unapprovedTypedMessages: {}, unapprovedTypedMessagesCount: 0, keyringTypes: [ - 'Simple Key Pair', - 'HD Key Tree', - 'Trezor Hardware', - 'Ledger Hardware', + KEYRING_TYPES.IMPORTED, + KEYRING_TYPES.HD_KEY_TREE, + KEYRING_TYPES.TREZOR, + KEYRING_TYPES.LEDGER, ], keyrings: [ { - type: 'HD Key Tree', + type: KEYRING_TYPES.HD_KEY_TREE, accounts: [ '0x64a845a5b02460acf8a3d84503b0d68d028b4bb4', '0xb19ac54efa18cc3a14a5b821bfec73d284bf0c5e', diff --git a/app/scripts/lib/seed-phrase-verifier.js b/app/scripts/lib/seed-phrase-verifier.js index e1c18c1eb..58d4acdf9 100644 --- a/app/scripts/lib/seed-phrase-verifier.js +++ b/app/scripts/lib/seed-phrase-verifier.js @@ -1,6 +1,8 @@ import KeyringController from 'eth-keyring-controller'; import log from 'loglevel'; +import { KEYRING_TYPES } from '../../../shared/constants/keyrings'; + const seedPhraseVerifier = { /** * Verifies if the seed words can restore the accounts. @@ -20,7 +22,9 @@ const seedPhraseVerifier = { } const keyringController = new KeyringController({}); - const Keyring = keyringController.getKeyringClassForType('HD Key Tree'); + const Keyring = keyringController.getKeyringClassForType( + KEYRING_TYPES.HD_KEY_TREE, + ); const opts = { mnemonic: seedPhrase, numberOfAccounts: createdAccounts.length, diff --git a/app/scripts/lib/seed-phrase-verifier.test.js b/app/scripts/lib/seed-phrase-verifier.test.js index 2f56c4b9a..aaa161d80 100644 --- a/app/scripts/lib/seed-phrase-verifier.test.js +++ b/app/scripts/lib/seed-phrase-verifier.test.js @@ -6,12 +6,13 @@ import { cloneDeep } from 'lodash'; import KeyringController from 'eth-keyring-controller'; import firstTimeState from '../first-time-state'; import mockEncryptor from '../../../test/lib/mock-encryptor'; +import { KEYRING_TYPES } from '../../../shared/constants/keyrings'; import seedPhraseVerifier from './seed-phrase-verifier'; describe('SeedPhraseVerifier', () => { describe('verifyAccounts', () => { const password = 'passw0rd1'; - const hdKeyTree = 'HD Key Tree'; + const hdKeyTree = KEYRING_TYPES.HD_KEY_TREE; let keyringController; let primaryKeyring; diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 02d2dd530..fdf441cef 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -66,11 +66,9 @@ import { GAS_DEV_API_BASE_URL, SWAPS_CLIENT_ID, } from '../../shared/constants/swaps'; +import { KEYRING_TYPES } from '../../shared/constants/keyrings'; import { CHAIN_IDS } from '../../shared/constants/network'; -import { - DEVICE_NAMES, - KEYRING_TYPES, -} from '../../shared/constants/hardware-wallets'; +import { DEVICE_NAMES } from '../../shared/constants/hardware-wallets'; import { CaveatTypes, RestrictedMethods, @@ -2136,8 +2134,9 @@ export default class MetamaskController extends EventEmitter { ethQuery, ); - const [primaryKeyring] = - keyringController.getKeyringsByType('HD Key Tree'); + const [primaryKeyring] = keyringController.getKeyringsByType( + KEYRING_TYPES.HD_KEY_TREE, + ); if (!primaryKeyring) { throw new Error('MetamaskController - No HD Key Tree found'); } @@ -2262,9 +2261,12 @@ export default class MetamaskController extends EventEmitter { }); // Accounts - const [hdKeyring] = this.keyringController.getKeyringsByType('HD Key Tree'); - const simpleKeyPairKeyrings = - this.keyringController.getKeyringsByType('Simple Key Pair'); + const [hdKeyring] = this.keyringController.getKeyringsByType( + KEYRING_TYPES.HD_KEY_TREE, + ); + const simpleKeyPairKeyrings = this.keyringController.getKeyringsByType( + KEYRING_TYPES.IMPORTED, + ); const hdAccounts = await hdKeyring.getAccounts(); const simpleKeyPairKeyringAccounts = await Promise.all( simpleKeyPairKeyrings.map((keyring) => keyring.getAccounts()), @@ -2361,7 +2363,9 @@ export default class MetamaskController extends EventEmitter { * Gets the mnemonic of the user's primary keyring. */ getPrimaryKeyringMnemonic() { - const [keyring] = this.keyringController.getKeyringsByType('HD Key Tree'); + const [keyring] = this.keyringController.getKeyringsByType( + KEYRING_TYPES.HD_KEY_TREE, + ); if (!keyring.mnemonic) { throw new Error('Primary keyring mnemonic unavailable.'); } @@ -2592,8 +2596,9 @@ export default class MetamaskController extends EventEmitter { * @returns {} keyState */ async addNewAccount(accountCount) { - const [primaryKeyring] = - this.keyringController.getKeyringsByType('HD Key Tree'); + const [primaryKeyring] = this.keyringController.getKeyringsByType( + KEYRING_TYPES.HD_KEY_TREE, + ); if (!primaryKeyring) { throw new Error('MetamaskController - No HD Key Tree found'); } @@ -2636,8 +2641,9 @@ export default class MetamaskController extends EventEmitter { * encoded as an array of UTF-8 bytes. */ async verifySeedPhrase() { - const [primaryKeyring] = - this.keyringController.getKeyringsByType('HD Key Tree'); + const [primaryKeyring] = this.keyringController.getKeyringsByType( + KEYRING_TYPES.HD_KEY_TREE, + ); if (!primaryKeyring) { throw new Error('MetamaskController - No HD Key Tree found'); } @@ -2758,7 +2764,7 @@ export default class MetamaskController extends EventEmitter { async importAccountWithStrategy(strategy, args) { const privateKey = await accountImporter.importAccount(strategy, args); const keyring = await this.keyringController.addNewKeyring( - 'Simple Key Pair', + KEYRING_TYPES.IMPORTED, [privateKey], ); const [firstAccount] = await keyring.getAccounts(); diff --git a/app/scripts/metamask-controller.test.js b/app/scripts/metamask-controller.test.js index 313d345ea..5fc7232ce 100644 --- a/app/scripts/metamask-controller.test.js +++ b/app/scripts/metamask-controller.test.js @@ -10,10 +10,8 @@ import browser from 'webextension-polyfill'; import { TRANSACTION_STATUSES } from '../../shared/constants/transaction'; import createTxMeta from '../../test/lib/createTxMeta'; import { NETWORK_TYPES } from '../../shared/constants/network'; -import { - KEYRING_TYPES, - DEVICE_NAMES, -} from '../../shared/constants/hardware-wallets'; +import { KEYRING_TYPES } from '../../shared/constants/keyrings'; +import { DEVICE_NAMES } from '../../shared/constants/hardware-wallets'; import { addHexPrefix } from './lib/util'; const Ganache = require('../../test/e2e/ganache'); @@ -192,7 +190,7 @@ describe('MetaMaskController', function () { it('adds private key to keyrings in KeyringController', async function () { const simpleKeyrings = metamaskController.keyringController.getKeyringsByType( - 'Simple Key Pair', + KEYRING_TYPES.IMPORTED, ); const privKeyBuffer = simpleKeyrings[0].wallets[0].privateKey; const pubKeyBuffer = simpleKeyrings[0].wallets[0].publicKey; diff --git a/shared/constants/hardware-wallets.js b/shared/constants/hardware-wallets.js index aa4d86d58..981c2cf48 100644 --- a/shared/constants/hardware-wallets.js +++ b/shared/constants/hardware-wallets.js @@ -3,12 +3,11 @@ * keyring types. Both simple and HD are treated as default but we do special * case accounts managed by a hardware wallet. */ -export const KEYRING_TYPES = { +export const HARDWARE_KEYRING_TYPES = { LEDGER: 'Ledger Hardware', TREZOR: 'Trezor Hardware', LATTICE: 'Lattice Hardware', QR: 'QR Hardware Wallet Device', - IMPORTED: 'Simple Key Pair', }; export const DEVICE_NAMES = { diff --git a/shared/constants/keyrings.js b/shared/constants/keyrings.js new file mode 100644 index 000000000..bcbd17953 --- /dev/null +++ b/shared/constants/keyrings.js @@ -0,0 +1,7 @@ +import { HARDWARE_KEYRING_TYPES } from './hardware-wallets'; + +export const KEYRING_TYPES = { + HD_KEY_TREE: 'HD Key Tree', + IMPORTED: 'Simple Key Pair', + ...HARDWARE_KEYRING_TYPES, +}; diff --git a/test/jest/mock-store.js b/test/jest/mock-store.js index 5d4055628..a447124a4 100644 --- a/test/jest/mock-store.js +++ b/test/jest/mock-store.js @@ -1,4 +1,5 @@ import { CHAIN_IDS } from '../../shared/constants/network'; +import { KEYRING_TYPES } from '../../shared/constants/keyrings'; const createGetSmartTransactionFeesApiResponse = () => { return { @@ -251,10 +252,10 @@ export const createSwapsMockStore = () => { }, }, selectedAddress: '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc', - keyringTypes: ['Simple Key Pair', 'HD Key Tree'], + keyringTypes: [KEYRING_TYPES.IMPORTED, KEYRING_TYPES.HD_KEY_TREE], keyrings: [ { - type: 'HD Key Tree', + type: KEYRING_TYPES.HD_KEY_TREE, accounts: [ '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc', 'c5b8dbac4c1d3f152cdeb400e2313f309c410acb', @@ -262,7 +263,7 @@ export const createSwapsMockStore = () => { ], }, { - type: 'Simple Key Pair', + type: KEYRING_TYPES.IMPORTED, accounts: ['0xd85a4b6a394794842887b8284293d69163007bbb'], }, ], diff --git a/ui/components/app/account-menu/account-menu.test.js b/ui/components/app/account-menu/account-menu.test.js index 86bfcf66d..4366556b6 100644 --- a/ui/components/app/account-menu/account-menu.test.js +++ b/ui/components/app/account-menu/account-menu.test.js @@ -3,6 +3,7 @@ import sinon from 'sinon'; import configureMockStore from 'redux-mock-store'; import { fireEvent, screen } from '@testing-library/react'; import { renderWithProvider } from '../../../../test/lib/render-helpers'; +import { KEYRING_TYPES } from '../../../../shared/constants/keyrings'; import AccountMenu from '.'; describe('Account Menu', () => { @@ -36,11 +37,11 @@ describe('Account Menu', () => { ], keyrings: [ { - type: 'HD Key Tree', + type: KEYRING_TYPES.HD_KEY_TREE, accounts: ['0xAdress'], }, { - type: 'Simple Key Pair', + type: KEYRING_TYPES.IMPORTED, accounts: ['0x1'], }, ], diff --git a/ui/components/app/account-menu/keyring-label.js b/ui/components/app/account-menu/keyring-label.js index 65007390c..3549a95b4 100644 --- a/ui/components/app/account-menu/keyring-label.js +++ b/ui/components/app/account-menu/keyring-label.js @@ -2,10 +2,8 @@ import React from 'react'; import PropTypes from 'prop-types'; import { useI18nContext } from '../../../hooks/useI18nContext'; -import { - KEYRING_NAMES, - KEYRING_TYPES, -} from '../../../../shared/constants/hardware-wallets'; +import { KEYRING_TYPES } from '../../../../shared/constants/keyrings'; +import { KEYRING_NAMES } from '../../../../shared/constants/hardware-wallets'; export default function KeyRingLabel({ keyring }) { const t = useI18nContext(); diff --git a/ui/components/app/alerts/unconnected-account-alert/unconnected-account-alert.test.js b/ui/components/app/alerts/unconnected-account-alert/unconnected-account-alert.test.js index 00edd64a5..d8fe1ddef 100644 --- a/ui/components/app/alerts/unconnected-account-alert/unconnected-account-alert.test.js +++ b/ui/components/app/alerts/unconnected-account-alert/unconnected-account-alert.test.js @@ -11,6 +11,8 @@ import { renderWithProvider } from '../../../../../test/lib/render-helpers'; import * as actions from '../../../../store/actions'; import { CHAIN_IDS } from '../../../../../shared/constants/network'; +import { KEYRING_TYPES } from '../../../../../shared/constants/keyrings'; + import UnconnectedAccountAlert from '.'; describe('Unconnected Account Alert', () => { @@ -47,7 +49,7 @@ describe('Unconnected Account Alert', () => { const keyrings = [ { - type: 'HD Key Tree', + type: KEYRING_TYPES.HD_KEY_TREE, accounts: [ '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc', '0xec1adf982415d2ef5ec55899b9bfb8bc0f29251b', diff --git a/ui/components/app/menu-bar/account-options-menu.js b/ui/components/app/menu-bar/account-options-menu.js index 5ab170ece..20f0a1549 100644 --- a/ui/components/app/menu-bar/account-options-menu.js +++ b/ui/components/app/menu-bar/account-options-menu.js @@ -21,6 +21,7 @@ import { import { useI18nContext } from '../../../hooks/useI18nContext'; import { getEnvironmentType } from '../../../../app/scripts/lib/util'; import { ENVIRONMENT_TYPE_FULLSCREEN } from '../../../../shared/constants/app'; +import { KEYRING_TYPES } from '../../../../shared/constants/keyrings'; import { EVENT, EVENT_NAMES } from '../../../../shared/constants/metametrics'; import { MetaMetricsContext } from '../../../contexts/metametrics'; @@ -40,7 +41,7 @@ export default function AccountOptionsMenu({ anchorElement, onClose }) { const trackEvent = useContext(MetaMetricsContext); const blockExplorerLinkText = useSelector(getBlockExplorerLinkText); - const isRemovable = keyring.type !== 'HD Key Tree'; + const isRemovable = keyring.type !== KEYRING_TYPES.HD_KEY_TREE; const routeToAddBlockExplorerUrl = () => { history.push(`${NETWORKS_ROUTE}#blockExplorerUrl`); diff --git a/ui/components/app/menu-bar/menu-bar.test.js b/ui/components/app/menu-bar/menu-bar.test.js index 55be5ca13..7bc041fe2 100644 --- a/ui/components/app/menu-bar/menu-bar.test.js +++ b/ui/components/app/menu-bar/menu-bar.test.js @@ -3,6 +3,7 @@ import configureStore from 'redux-mock-store'; import { fireEvent, screen, waitFor } from '@testing-library/react'; import { renderWithProvider } from '../../../../test/lib/render-helpers'; import { CHAIN_IDS } from '../../../../shared/constants/network'; +import { KEYRING_TYPES } from '../../../../shared/constants/keyrings'; import MenuBar from './menu-bar'; const initState = { @@ -20,7 +21,7 @@ const initState = { }, keyrings: [ { - type: 'HD Key Tree', + type: KEYRING_TYPES.HD_KEY_TREE, accounts: ['0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc'], }, ], diff --git a/ui/components/app/wallet-overview/token-overview.test.js b/ui/components/app/wallet-overview/token-overview.test.js index 77ab8ec16..2a801cadd 100644 --- a/ui/components/app/wallet-overview/token-overview.test.js +++ b/ui/components/app/wallet-overview/token-overview.test.js @@ -2,6 +2,7 @@ import React from 'react'; import configureMockStore from 'redux-mock-store'; import thunk from 'redux-thunk'; import { renderWithProvider } from '../../../../test/jest/rendering'; +import { KEYRING_TYPES } from '../../../../shared/constants/keyrings'; import TokenOverview from './token-overview'; describe('TokenOverview', () => { @@ -21,11 +22,11 @@ describe('TokenOverview', () => { selectedAddress: '0x1', keyrings: [ { - type: 'HD Key Tree', + type: KEYRING_TYPES.HD_KEY_TREE, accounts: ['0x1', '0x2'], }, { - type: 'Ledger Hardware', + type: KEYRING_TYPES.LEDGER, accounts: [], }, ], diff --git a/ui/ducks/metamask/metamask.js b/ui/ducks/metamask/metamask.js index 5e0c925c9..d27b68657 100644 --- a/ui/ducks/metamask/metamask.js +++ b/ui/ducks/metamask/metamask.js @@ -15,7 +15,7 @@ import { updateTransactionGasFees } from '../../store/actions'; import { setCustomGasLimit, setCustomGasPrice } from '../gas/gas.duck'; import { decGWEIToHexWEI } from '../../helpers/utils/conversions.util'; -import { KEYRING_TYPES } from '../../../shared/constants/hardware-wallets'; +import { KEYRING_TYPES } from '../../../shared/constants/keyrings'; import { isEqualCaseInsensitive } from '../../../shared/modules/string-utils'; import { stripHexPrefix } from '../../../shared/modules/hexstring-utils'; diff --git a/ui/ducks/send/send.test.js b/ui/ducks/send/send.test.js index 47c7adec4..539f013c3 100644 --- a/ui/ducks/send/send.test.js +++ b/ui/ducks/send/send.test.js @@ -12,6 +12,7 @@ import { } from '../../pages/send/send.constants'; import { CHAIN_IDS } from '../../../shared/constants/network'; import { GAS_ESTIMATE_TYPES, GAS_LIMITS } from '../../../shared/constants/gas'; +import { KEYRING_TYPES } from '../../../shared/constants/keyrings'; import { ASSET_TYPES, TOKEN_STANDARDS, @@ -1276,7 +1277,7 @@ describe('Send Slice', () => { identities: { '0xAddress': { address: '0xAddress' } }, keyrings: [ { - type: 'HD Key Tree', + type: KEYRING_TYPES.HD_KEY_TREE, accounts: ['0xAddress'], }, ], diff --git a/ui/pages/mobile-sync/mobile-sync.component.js b/ui/pages/mobile-sync/mobile-sync.component.js index f23badab4..ecad8d466 100644 --- a/ui/pages/mobile-sync/mobile-sync.component.js +++ b/ui/pages/mobile-sync/mobile-sync.component.js @@ -8,6 +8,7 @@ import qrCode from 'qrcode-generator'; import Button from '../../components/ui/button'; import LoadingScreen from '../../components/ui/loading-screen'; import { MINUTE, SECOND } from '../../../shared/constants/time'; +import { KEYRING_TYPES } from '../../../shared/constants/keyrings'; const PASSWORD_PROMPT_SCREEN = 'PASSWORD_PROMPT_SCREEN'; const REVEAL_SEED_SCREEN = 'REVEAL_SEED_SCREEN'; @@ -81,7 +82,7 @@ export default class MobileSyncPage extends Component { async exportAccounts() { const addresses = []; this.props.keyrings.forEach((keyring) => { - if (keyring.type === 'Simple Key Pair') { + if (keyring.type === KEYRING_TYPES.IMPORTED) { addresses.push(keyring.accounts[0]); } }); diff --git a/ui/pages/send/send.test.js b/ui/pages/send/send.test.js index 96ac7aebe..3a4ee9b9d 100644 --- a/ui/pages/send/send.test.js +++ b/ui/pages/send/send.test.js @@ -7,6 +7,7 @@ import { ensInitialState } from '../../ducks/ens'; import { renderWithProvider } from '../../../test/jest'; import { CHAIN_IDS } from '../../../shared/constants/network'; import { GAS_ESTIMATE_TYPES } from '../../../shared/constants/gas'; +import { KEYRING_TYPES } from '../../../shared/constants/keyrings'; import { INITIAL_SEND_STATE_FOR_EXISTING_DRAFT } from '../../../test/jest/mocks'; import Send from './send'; @@ -68,7 +69,7 @@ const baseStore = { selectedAddress: '0x0', keyrings: [ { - type: 'HD Key Tree', + type: KEYRING_TYPES.HD_KEY_TREE, accounts: ['0x0'], }, ], diff --git a/ui/selectors/selectors.js b/ui/selectors/selectors.js index 7d937b789..07290eab2 100644 --- a/ui/selectors/selectors.js +++ b/ui/selectors/selectors.js @@ -22,8 +22,8 @@ import { CHAIN_IDS, NETWORK_TYPES, } from '../../shared/constants/network'; +import { KEYRING_TYPES } from '../../shared/constants/keyrings'; import { - KEYRING_TYPES, WEBHID_CONNECTED_STATUSES, LEDGER_TRANSPORT_TYPES, TRANSPORT_STATES, @@ -243,7 +243,7 @@ export function getAccountType(state) { case KEYRING_TYPES.LEDGER: case KEYRING_TYPES.LATTICE: return 'hardware'; - case 'Simple Key Pair': + case KEYRING_TYPES.IMPORTED: return 'imported'; default: return 'default'; diff --git a/ui/selectors/selectors.test.js b/ui/selectors/selectors.test.js index f6f3ebcc0..a87cd35b5 100644 --- a/ui/selectors/selectors.test.js +++ b/ui/selectors/selectors.test.js @@ -1,5 +1,5 @@ import mockState from '../../test/data/mock-state.json'; -import { KEYRING_TYPES } from '../../shared/constants/hardware-wallets'; +import { KEYRING_TYPES } from '../../shared/constants/keyrings'; import * as selectors from './selectors'; describe('Selectors', () => { @@ -18,7 +18,7 @@ describe('Selectors', () => { describe('#isHardwareWallet', () => { it('returns false if it is not a HW wallet', () => { - mockState.metamask.keyrings[0].type = 'Simple Key Pair'; + mockState.metamask.keyrings[0].type = KEYRING_TYPES.IMPORTED; expect(selectors.isHardwareWallet(mockState)).toBe(false); }); @@ -35,7 +35,7 @@ describe('Selectors', () => { describe('#getHardwareWalletType', () => { it('returns undefined if it is not a HW wallet', () => { - mockState.metamask.keyrings[0].type = 'Simple Key Pair'; + mockState.metamask.keyrings[0].type = KEYRING_TYPES.IMPORTED; expect(selectors.getHardwareWalletType(mockState)).toBeUndefined(); });