-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathaccounts.ts
More file actions
169 lines (147 loc) · 5.02 KB
/
accounts.ts
File metadata and controls
169 lines (147 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { AccountsControllerState } from '@metamask/accounts-controller';
import {
EthAccountType,
BtcAccountType,
SolAccountType,
CaipChainId,
EthScope,
TrxAccountType,
isEvmAccountType,
} from '@metamask/keyring-api';
import { InternalAccount } from '@metamask/keyring-internal-api';
import { KnownCaipNamespace, parseCaipChainId } from '@metamask/utils';
import { createSelector } from 'reselect';
import { EMPTY_OBJECT } from './shared';
export type AccountsState = {
metamask: AccountsControllerState;
};
export function isBitcoinAccount(account: InternalAccount) {
return Boolean(
account &&
Object.values(BtcAccountType).includes(account.type as BtcAccountType),
);
}
export function isSolanaAccount(account: InternalAccount) {
const { DataAccount } = SolAccountType;
return Boolean(account && account.type === DataAccount);
}
export function isTronAccount(account: InternalAccount) {
const { Eoa } = TrxAccountType;
return Boolean(account && account.type === Eoa);
}
export function isNonEvmAccount(account: InternalAccount) {
return (
isBitcoinAccount(account) ||
isSolanaAccount(account) ||
isTronAccount(account)
);
}
export const getInternalAccountsObject = (state: AccountsState) =>
state.metamask.internalAccounts.accounts;
export const getInternalAccounts = createSelector(
getInternalAccountsObject,
(accounts) => Object.values(accounts),
);
// Uses EMPTY_OBJECT to preserve referential equality when accountIdByAddress
// is undefined, so downstream createSelector consumers don't recompute.
export const getAccountIdByAddress = (state: AccountsState) =>
state.metamask.accountIdByAddress ?? EMPTY_OBJECT;
export const getInternalAccountByAddress = createSelector(
[
getInternalAccountsObject,
getAccountIdByAddress,
(_, address: string) => address,
],
(accounts, accountIdByAddress, address) => {
const accountId =
accountIdByAddress[address] ?? accountIdByAddress[address?.toLowerCase()];
return accountId ? accounts[accountId] : undefined;
},
);
export function getSelectedInternalAccount(state: AccountsState) {
const accountId = state.metamask.internalAccounts.selectedAccount;
return state.metamask.internalAccounts.accounts[accountId];
}
/**
* Same as `getSelectedInternalAccount`, but might potentially be `undefined`:
* - This might happen during the onboarding
*
* @param state - The accounts state
* @returns The selected internal account or undefined
*/
export function getMaybeSelectedInternalAccount(state: AccountsState) {
const accountId = state.metamask.internalAccounts?.selectedAccount;
return accountId
? state.metamask.internalAccounts?.accounts[accountId]
: undefined;
}
export const isSelectedInternalAccountEth = createSelector(
getSelectedInternalAccount,
(account) => {
const { Eoa, Erc4337 } = EthAccountType;
return Boolean(
account && (account.type === Eoa || account.type === Erc4337),
);
},
);
export const selectEvmAddress = createSelector(
getSelectedInternalAccount,
(account) =>
account && isEvmAccountType(account.type) ? account.address : undefined,
);
export const isSelectedInternalAccountSolana = createSelector(
getSelectedInternalAccount,
(account) => isSolanaAccount(account),
);
export const hasCreatedSolanaAccount = createSelector(
getInternalAccounts,
(accounts) => accounts.some((account) => isSolanaAccount(account)),
);
/**
* Returns all internal accounts that declare support for the provided CAIP scope.
* The scope should be a CAIP-2 scope string (e.g., 'eip155:0', 'bip122:...').
*
* @param _state - Redux state (unused; required for selector signature)
* @param scope - The CAIP scope string to filter accounts by
*/
export const getInternalAccountsByScope = createSelector(
[getInternalAccounts, (_state: AccountsState, scope: CaipChainId) => scope],
(accounts, scope): InternalAccount[] => {
if (!Array.isArray(accounts) || accounts.length === 0) {
return [];
}
let namespace: string;
let reference: string;
try {
const parsed = parseCaipChainId(scope);
namespace = parsed.namespace;
reference = parsed.reference;
} catch {
return [];
}
if (namespace === KnownCaipNamespace.Eip155) {
// If requesting eip155:0 (wildcard), include any account that has any EVM scope
if (reference === '0') {
return accounts.filter(
(account) =>
Array.isArray(account.scopes) &&
account.scopes.some((s) =>
s.startsWith(`${KnownCaipNamespace.Eip155}:`),
),
);
}
// For a specific EVM chain, include accounts that either have the exact scope or the wildcard
return accounts.filter(
(account) =>
Array.isArray(account.scopes) &&
(account.scopes.includes(scope) ||
account.scopes.includes(EthScope.Eoa)),
);
}
// Non-EVM: exact scope match only
return accounts.filter(
(account) =>
Array.isArray(account.scopes) && account.scopes.includes(scope),
);
},
);