2022-09-22 17:04:24 +02:00
|
|
|
import { addHexPrefix, isHexString } from 'ethereumjs-util';
|
2021-02-04 19:15:23 +01:00
|
|
|
import * as actionConstants from '../../store/actionConstants';
|
2023-01-27 19:28:03 +01:00
|
|
|
import { AlertTypes } from '../../../shared/constants/alerts';
|
2022-01-07 20:18:02 +01:00
|
|
|
import {
|
2023-01-27 19:28:03 +01:00
|
|
|
GasEstimateTypes,
|
|
|
|
NetworkCongestionThresholds,
|
2022-01-07 20:18:02 +01:00
|
|
|
} from '../../../shared/constants/gas';
|
2021-06-08 18:03:59 +02:00
|
|
|
import {
|
|
|
|
accountsWithSendEtherInfoSelector,
|
2021-08-06 01:59:58 +02:00
|
|
|
checkNetworkAndAccountSupports1559,
|
2021-06-08 18:03:59 +02:00
|
|
|
getAddressBook,
|
2023-01-27 18:17:47 +01:00
|
|
|
getUseCurrencyRateCheck,
|
2021-06-08 18:03:59 +02:00
|
|
|
} from '../../selectors';
|
2022-03-25 18:11:04 +01:00
|
|
|
import { updateTransactionGasFees } from '../../store/actions';
|
2021-07-08 22:23:00 +02:00
|
|
|
import { setCustomGasLimit, setCustomGasPrice } from '../gas/gas.duck';
|
2022-01-07 20:18:02 +01:00
|
|
|
|
2023-03-21 15:43:22 +01:00
|
|
|
import { KeyringType } from '../../../shared/constants/keyring';
|
2022-03-07 19:54:36 +01:00
|
|
|
import { isEqualCaseInsensitive } from '../../../shared/modules/string-utils';
|
2022-09-22 17:04:24 +02:00
|
|
|
import { stripHexPrefix } from '../../../shared/modules/hexstring-utils';
|
2023-01-25 17:33:06 +01:00
|
|
|
import {
|
|
|
|
decGWEIToHexWEI,
|
|
|
|
hexToDecimal,
|
|
|
|
} from '../../../shared/modules/conversion.utils';
|
2016-04-14 00:28:44 +02:00
|
|
|
|
2023-02-02 20:32:44 +01:00
|
|
|
const initialState = {
|
|
|
|
isInitialized: false,
|
|
|
|
isUnlocked: false,
|
|
|
|
isAccountMenuOpen: false,
|
2023-04-13 18:54:03 +02:00
|
|
|
isNetworkMenuOpen: false,
|
2023-02-02 20:32:44 +01:00
|
|
|
identities: {},
|
|
|
|
unapprovedTxs: {},
|
2023-03-09 22:00:28 +01:00
|
|
|
networkConfigurations: {},
|
2023-02-02 20:32:44 +01:00
|
|
|
addressBook: [],
|
|
|
|
contractExchangeRates: {},
|
|
|
|
pendingTokens: {},
|
|
|
|
customNonceValue: '',
|
|
|
|
useBlockie: false,
|
|
|
|
featureFlags: {},
|
|
|
|
welcomeScreenSeen: false,
|
|
|
|
currentLocale: '',
|
|
|
|
currentBlockGasLimit: '',
|
|
|
|
preferences: {
|
|
|
|
autoLockTimeLimit: undefined,
|
|
|
|
showFiatInTestnets: false,
|
|
|
|
showTestNetworks: false,
|
|
|
|
useNativeCurrencyAsPrimaryCurrency: true,
|
|
|
|
},
|
|
|
|
firstTimeFlowType: null,
|
|
|
|
completedOnboarding: false,
|
|
|
|
knownMethodData: {},
|
|
|
|
participateInMetaMetrics: null,
|
|
|
|
nextNonce: null,
|
|
|
|
conversionRate: null,
|
|
|
|
nativeCurrency: 'ETH',
|
|
|
|
};
|
2016-04-14 00:28:44 +02:00
|
|
|
|
2023-02-02 20:32:44 +01:00
|
|
|
/**
|
|
|
|
* Temporary types for this slice so that inferrence of MetaMask state tree can
|
|
|
|
* occur
|
|
|
|
*
|
|
|
|
* @param {typeof initialState} state - State
|
|
|
|
* @param {any} action
|
|
|
|
* @returns {typeof initialState}
|
|
|
|
*/
|
|
|
|
export default function reduceMetamask(state = initialState, action) {
|
|
|
|
// I don't think we should be spreading initialState into this. Once the
|
|
|
|
// state tree has begun by way of the first reduce call the initialState is
|
|
|
|
// set. The only time it should be used again is if we reset the state with a
|
|
|
|
// deliberate action. However, our tests are *relying upon the initialState
|
|
|
|
// tree above to be spread into the reducer as a way of hydrating the state
|
|
|
|
// for this slice*. I attempted to remove this and it caused nearly 40 test
|
|
|
|
// failures. We are going to refactor this slice anyways, possibly removing
|
|
|
|
// it so we will fix this issue when that time comes.
|
|
|
|
const metamaskState = { ...initialState, ...state };
|
2016-04-14 00:28:44 +02:00
|
|
|
switch (action.type) {
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.UPDATE_METAMASK_STATE:
|
2021-02-04 19:15:23 +01:00
|
|
|
return { ...metamaskState, ...action.value };
|
2016-06-21 22:18:32 +02:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.LOCK_METAMASK:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2016-06-21 22:18:32 +02:00
|
|
|
isUnlocked: false,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2016-06-21 22:18:32 +02:00
|
|
|
|
2020-08-12 18:41:27 +02:00
|
|
|
case actionConstants.SET_ACCOUNT_LABEL: {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { account } = action.value;
|
|
|
|
const name = action.value.label;
|
|
|
|
const id = {};
|
|
|
|
id[account] = { ...metamaskState.identities[account], name };
|
|
|
|
const identities = { ...metamaskState.identities, ...id };
|
|
|
|
return Object.assign(metamaskState, { identities });
|
2020-08-12 18:41:27 +02:00
|
|
|
}
|
2016-06-21 22:18:32 +02:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.UPDATE_CUSTOM_NONCE:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2019-09-27 06:30:36 +02:00
|
|
|
customNonceValue: action.value,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2017-10-12 18:42:14 +02:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.TOGGLE_ACCOUNT_MENU:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2017-10-16 07:28:25 +02:00
|
|
|
isAccountMenuOpen: !metamaskState.isAccountMenuOpen,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2017-10-16 07:28:25 +02:00
|
|
|
|
2023-04-13 18:54:03 +02:00
|
|
|
case actionConstants.TOGGLE_NETWORK_MENU:
|
|
|
|
return {
|
|
|
|
...metamaskState,
|
|
|
|
isNetworkMenuOpen: !metamaskState.isNetworkMenuOpen,
|
|
|
|
};
|
|
|
|
|
2020-08-12 18:41:27 +02:00
|
|
|
case actionConstants.UPDATE_TRANSACTION_PARAMS: {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { id: txId, value } = action;
|
|
|
|
let { currentNetworkTxList } = metamaskState;
|
2020-03-06 22:34:56 +01:00
|
|
|
currentNetworkTxList = currentNetworkTxList.map((tx) => {
|
2017-11-08 20:18:27 +01:00
|
|
|
if (tx.id === txId) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const newTx = { ...tx };
|
|
|
|
newTx.txParams = value;
|
|
|
|
return newTx;
|
2017-11-08 17:44:48 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return tx;
|
|
|
|
});
|
2017-11-08 17:44:48 +01:00
|
|
|
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2020-03-06 22:34:56 +01:00
|
|
|
currentNetworkTxList,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-08-12 18:41:27 +02:00
|
|
|
}
|
2017-11-08 17:44:48 +01:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.SET_PARTICIPATE_IN_METAMETRICS:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2019-03-05 16:45:01 +01:00
|
|
|
participateInMetaMetrics: action.value,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-03-05 16:45:01 +01:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.CLOSE_WELCOME_SCREEN:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2018-03-08 18:31:21 +01:00
|
|
|
welcomeScreenSeen: true,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-03-08 18:31:21 +01:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.SET_PENDING_TOKENS:
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2018-05-20 08:04:19 +02:00
|
|
|
pendingTokens: { ...action.payload },
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-20 08:04:19 +02:00
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.CLEAR_PENDING_TOKENS: {
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2018-05-20 08:04:19 +02:00
|
|
|
pendingTokens: {},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-20 08:04:19 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.COMPLETE_ONBOARDING: {
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2019-01-23 16:25:34 +01:00
|
|
|
completedOnboarding: true,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-01-23 16:25:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.SET_FIRST_TIME_FLOW_TYPE: {
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2019-03-05 16:45:01 +01:00
|
|
|
firstTimeFlowType: action.value,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-03-05 16:45:01 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 02:35:37 +02:00
|
|
|
case actionConstants.SET_NEXT_NONCE: {
|
2020-01-13 17:29:21 +01:00
|
|
|
return {
|
|
|
|
...metamaskState,
|
2023-02-03 18:56:44 +01:00
|
|
|
nextNonce: action.payload,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-09-27 06:30:36 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 20:35:00 +01:00
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(flask)
|
2023-02-23 17:39:48 +01:00
|
|
|
case actionConstants.FORCE_DISABLE_DESKTOP: {
|
|
|
|
return {
|
|
|
|
...metamaskState,
|
|
|
|
desktopEnabled: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
///: END:ONLY_INCLUDE_IN
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
default:
|
2021-02-04 19:15:23 +01:00
|
|
|
return metamaskState;
|
2016-04-14 00:28:44 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-23 18:07:05 +01:00
|
|
|
|
2021-07-08 22:23:00 +02:00
|
|
|
const toHexWei = (value, expectHexWei) => {
|
|
|
|
return addHexPrefix(expectHexWei ? value : decGWEIToHexWEI(value));
|
|
|
|
};
|
|
|
|
|
|
|
|
// Action Creators
|
2022-03-25 18:11:04 +01:00
|
|
|
export function updateGasFees({
|
2021-07-08 22:23:00 +02:00
|
|
|
gasPrice,
|
|
|
|
gasLimit,
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
maxFeePerGas,
|
|
|
|
transaction,
|
|
|
|
expectHexWei = false,
|
|
|
|
}) {
|
|
|
|
return async (dispatch) => {
|
|
|
|
const txParamsCopy = { ...transaction.txParams, gas: gasLimit };
|
|
|
|
if (gasPrice) {
|
|
|
|
dispatch(
|
|
|
|
setCustomGasPrice(toHexWei(txParamsCopy.gasPrice, expectHexWei)),
|
|
|
|
);
|
|
|
|
txParamsCopy.gasPrice = toHexWei(gasPrice, expectHexWei);
|
|
|
|
} else if (maxFeePerGas && maxPriorityFeePerGas) {
|
|
|
|
txParamsCopy.maxFeePerGas = toHexWei(maxFeePerGas, expectHexWei);
|
|
|
|
txParamsCopy.maxPriorityFeePerGas = addHexPrefix(
|
|
|
|
decGWEIToHexWEI(maxPriorityFeePerGas),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const updatedTx = {
|
|
|
|
...transaction,
|
|
|
|
txParams: txParamsCopy,
|
|
|
|
};
|
|
|
|
|
|
|
|
const customGasLimit = isHexString(addHexPrefix(gasLimit))
|
|
|
|
? addHexPrefix(gasLimit)
|
|
|
|
: addHexPrefix(gasLimit.toString(16));
|
|
|
|
dispatch(setCustomGasLimit(customGasLimit));
|
2022-03-25 18:11:04 +01:00
|
|
|
await dispatch(updateTransactionGasFees(updatedTx.id, updatedTx));
|
2021-07-08 22:23:00 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Selectors
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getAlertEnabledness = (state) => state.metamask.alertEnabledness;
|
2020-05-08 21:45:52 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export const getUnconnectedAccountAlertEnabledness = (state) =>
|
2023-01-27 19:28:03 +01:00
|
|
|
getAlertEnabledness(state)[AlertTypes.unconnectedAccount];
|
2020-05-12 15:01:52 +02:00
|
|
|
|
2020-12-11 00:40:29 +01:00
|
|
|
export const getWeb3ShimUsageAlertEnabledness = (state) =>
|
2023-01-27 19:28:03 +01:00
|
|
|
getAlertEnabledness(state)[AlertTypes.web3ShimUsage];
|
2020-12-11 00:40:29 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export const getUnconnectedAccountAlertShown = (state) =>
|
2021-02-04 19:15:23 +01:00
|
|
|
state.metamask.unconnectedAccountAlertShownOrigins;
|
2020-05-29 02:43:44 +02:00
|
|
|
|
2022-02-16 19:03:54 +01:00
|
|
|
export const getPendingTokens = (state) => state.metamask.pendingTokens;
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export const getTokens = (state) => state.metamask.tokens;
|
2021-06-08 18:03:59 +02:00
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
export function getNftsDropdownState(state) {
|
|
|
|
return state.metamask.nftsDropdownState;
|
2022-01-27 18:26:33 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
export const getNfts = (state) => {
|
2021-12-14 00:41:10 +01:00
|
|
|
const {
|
|
|
|
metamask: {
|
2022-11-15 19:49:42 +01:00
|
|
|
allNfts,
|
2021-12-14 00:41:10 +01:00
|
|
|
provider: { chainId },
|
|
|
|
selectedAddress,
|
|
|
|
},
|
|
|
|
} = state;
|
|
|
|
|
2023-01-25 17:33:06 +01:00
|
|
|
const chainIdAsDecimal = hexToDecimal(chainId);
|
|
|
|
|
|
|
|
return allNfts?.[selectedAddress]?.[chainIdAsDecimal] ?? [];
|
2021-12-14 00:41:10 +01:00
|
|
|
};
|
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
export const getNftContracts = (state) => {
|
2021-12-14 00:41:10 +01:00
|
|
|
const {
|
|
|
|
metamask: {
|
2022-11-15 19:49:42 +01:00
|
|
|
allNftContracts,
|
2021-12-14 00:41:10 +01:00
|
|
|
provider: { chainId },
|
|
|
|
selectedAddress,
|
|
|
|
},
|
|
|
|
} = state;
|
|
|
|
|
2023-01-25 17:33:06 +01:00
|
|
|
const chainIdAsDecimal = hexToDecimal(chainId);
|
|
|
|
|
|
|
|
return allNftContracts?.[selectedAddress]?.[chainIdAsDecimal] ?? [];
|
2021-12-14 00:41:10 +01:00
|
|
|
};
|
|
|
|
|
2021-06-08 18:03:59 +02:00
|
|
|
export function getBlockGasLimit(state) {
|
|
|
|
return state.metamask.currentBlockGasLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getConversionRate(state) {
|
|
|
|
return state.metamask.conversionRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getNativeCurrency(state) {
|
2023-01-27 18:17:47 +01:00
|
|
|
const useCurrencyRateCheck = getUseCurrencyRateCheck(state);
|
|
|
|
return useCurrencyRateCheck
|
|
|
|
? state.metamask.nativeCurrency
|
|
|
|
: state.metamask.provider.ticker;
|
2021-06-08 18:03:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getSendHexDataFeatureFlagState(state) {
|
|
|
|
return state.metamask.featureFlags.sendHexData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSendToAccounts(state) {
|
|
|
|
const fromAccounts = accountsWithSendEtherInfoSelector(state);
|
|
|
|
const addressBookAccounts = getAddressBook(state);
|
|
|
|
return [...fromAccounts, ...addressBookAccounts];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getUnapprovedTxs(state) {
|
|
|
|
return state.metamask.unapprovedTxs;
|
|
|
|
}
|
2021-06-25 18:24:00 +02:00
|
|
|
|
2021-09-30 13:57:59 +02:00
|
|
|
/**
|
|
|
|
* Function returns true if network details are fetched and it is found to not support EIP-1559
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
|
|
|
* @param state
|
2021-09-30 13:57:59 +02:00
|
|
|
*/
|
|
|
|
export function isNotEIP1559Network(state) {
|
|
|
|
return state.metamask.networkDetails?.EIPS[1559] === false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function returns true if network details are fetched and it is found to support EIP-1559
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
|
|
|
* @param state
|
2021-09-30 13:57:59 +02:00
|
|
|
*/
|
2021-06-25 18:24:00 +02:00
|
|
|
export function isEIP1559Network(state) {
|
2021-07-30 13:35:30 +02:00
|
|
|
return state.metamask.networkDetails?.EIPS[1559] === true;
|
2021-06-25 18:24:00 +02:00
|
|
|
}
|
2021-07-08 22:23:00 +02:00
|
|
|
|
|
|
|
export function getGasEstimateType(state) {
|
|
|
|
return state.metamask.gasEstimateType;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getGasFeeEstimates(state) {
|
|
|
|
return state.metamask.gasFeeEstimates;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getEstimatedGasFeeTimeBounds(state) {
|
|
|
|
return state.metamask.estimatedGasFeeTimeBounds;
|
|
|
|
}
|
2021-08-06 01:59:58 +02:00
|
|
|
|
|
|
|
export function getIsGasEstimatesLoading(state) {
|
2022-07-31 20:26:40 +02:00
|
|
|
const networkAndAccountSupports1559 =
|
|
|
|
checkNetworkAndAccountSupports1559(state);
|
2021-08-06 01:59:58 +02:00
|
|
|
const gasEstimateType = getGasEstimateType(state);
|
|
|
|
|
|
|
|
// We consider the gas estimate to be loading if the gasEstimateType is
|
|
|
|
// 'NONE' or if the current gasEstimateType cannot be supported by the current
|
|
|
|
// network
|
|
|
|
const isEIP1559TolerableEstimateType =
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType === GasEstimateTypes.feeMarket ||
|
|
|
|
gasEstimateType === GasEstimateTypes.ethGasPrice;
|
2021-08-06 01:59:58 +02:00
|
|
|
const isGasEstimatesLoading =
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType === GasEstimateTypes.none ||
|
2021-08-06 01:59:58 +02:00
|
|
|
(networkAndAccountSupports1559 && !isEIP1559TolerableEstimateType) ||
|
|
|
|
(!networkAndAccountSupports1559 &&
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType === GasEstimateTypes.feeMarket);
|
2021-08-06 01:59:58 +02:00
|
|
|
|
|
|
|
return isGasEstimatesLoading;
|
|
|
|
}
|
2021-09-30 23:34:11 +02:00
|
|
|
|
2022-01-07 20:18:02 +01:00
|
|
|
export function getIsNetworkBusy(state) {
|
|
|
|
const gasFeeEstimates = getGasFeeEstimates(state);
|
2023-01-27 19:28:03 +01:00
|
|
|
return gasFeeEstimates?.networkCongestion >= NetworkCongestionThresholds.busy;
|
2022-01-07 20:18:02 +01:00
|
|
|
}
|
|
|
|
|
2021-09-30 23:34:11 +02:00
|
|
|
export function getCompletedOnboarding(state) {
|
|
|
|
return state.metamask.completedOnboarding;
|
|
|
|
}
|
|
|
|
export function getIsInitialized(state) {
|
|
|
|
return state.metamask.isInitialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getIsUnlocked(state) {
|
|
|
|
return state.metamask.isUnlocked;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSeedPhraseBackedUp(state) {
|
|
|
|
return state.metamask.seedPhraseBackedUp;
|
|
|
|
}
|
2021-10-21 21:17:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given the redux state object and an address, finds a keyring that contains that address, if one exists
|
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} state - the redux state object
|
2021-10-21 21:17:03 +02:00
|
|
|
* @param {string} address - the address to search for among the keyring addresses
|
2022-07-26 20:10:51 +02:00
|
|
|
* @returns {object | undefined} The keyring which contains the passed address, or undefined
|
2021-10-21 21:17:03 +02:00
|
|
|
*/
|
|
|
|
export function findKeyringForAddress(state, address) {
|
|
|
|
const keyring = state.metamask.keyrings.find((kr) => {
|
|
|
|
return kr.accounts.some((account) => {
|
|
|
|
return (
|
|
|
|
isEqualCaseInsensitive(account, addHexPrefix(address)) ||
|
|
|
|
isEqualCaseInsensitive(account, stripHexPrefix(address))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return keyring;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given the redux state object, returns the users preferred ledger transport type
|
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} state - the redux state object
|
2021-10-21 21:17:03 +02:00
|
|
|
* @returns {string} The users preferred ledger transport type. One of'ledgerLive', 'webhid' or 'u2f'
|
|
|
|
*/
|
|
|
|
export function getLedgerTransportType(state) {
|
|
|
|
return state.metamask.ledgerTransportType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given the redux state object and an address, returns a boolean indicating whether the passed address is part of a Ledger keyring
|
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} state - the redux state object
|
2021-10-21 21:17:03 +02:00
|
|
|
* @param {string} address - the address to search for among all keyring addresses
|
|
|
|
* @returns {boolean} true if the passed address is part of a ledger keyring, and false otherwise
|
|
|
|
*/
|
|
|
|
export function isAddressLedger(state, address) {
|
|
|
|
const keyring = findKeyringForAddress(state, address);
|
|
|
|
|
2023-03-21 15:43:22 +01:00
|
|
|
return keyring?.type === KeyringType.ledger;
|
2021-10-21 21:17:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given the redux state object, returns a boolean indicating whether the user has any Ledger accounts added to MetaMask (i.e. Ledger keyrings
|
|
|
|
* in state)
|
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} state - the redux state object
|
2021-10-21 21:17:03 +02:00
|
|
|
* @returns {boolean} true if the user has a Ledger account and false otherwise
|
|
|
|
*/
|
|
|
|
export function doesUserHaveALedgerAccount(state) {
|
|
|
|
return state.metamask.keyrings.some((kr) => {
|
2023-03-21 15:43:22 +01:00
|
|
|
return kr.type === KeyringType.ledger;
|
2021-10-21 21:17:03 +02:00
|
|
|
});
|
|
|
|
}
|