1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-22 17:33:23 +01:00

Create constants for all keyring types (#16575)

This commit is contained in:
David Walsh 2022-11-21 08:23:35 -06:00 committed by GitHub
parent 8a529bca47
commit 9530797a7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 77 additions and 52 deletions

View File

@ -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',

View File

@ -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,

View File

@ -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;

View File

@ -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();

View File

@ -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;

View File

@ -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 = {

View File

@ -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,
};

View File

@ -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'],
},
],

View File

@ -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'],
},
],

View File

@ -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();

View File

@ -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',

View File

@ -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`);

View File

@ -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'],
},
],

View File

@ -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: [],
},
],

View File

@ -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';

View File

@ -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'],
},
],

View File

@ -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]);
}
});

View File

@ -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'],
},
],

View File

@ -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';

View File

@ -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();
});