|
| 1 | +import type { Bip44Account } from '@metamask/account-api'; |
| 2 | +import type { AccountProvider } from '@metamask/account-api'; |
| 3 | +import type { AccountSelector } from '@metamask/account-api'; |
| 4 | +import { AccountGroupType } from '@metamask/account-api'; |
| 5 | +import { |
| 6 | + toMultichainAccountGroupId, |
| 7 | + type MultichainAccountGroupId, |
| 8 | + type MultichainAccountGroup as MultichainAccountGroupDefinition, |
| 9 | +} from '@metamask/account-api'; |
| 10 | +import { type KeyringAccount } from '@metamask/keyring-api'; |
| 11 | +import { isScopeEqualToAny } from '@metamask/keyring-utils'; |
| 12 | + |
| 13 | +import type { MultichainAccountWallet } from './MultichainAccountWallet'; |
| 14 | + |
| 15 | +/** |
| 16 | + * A multichain account group that holds multiple accounts. |
| 17 | + */ |
| 18 | +export class MultichainAccountGroup< |
| 19 | + Account extends Bip44Account<KeyringAccount>, |
| 20 | +> implements MultichainAccountGroupDefinition<Account> |
| 21 | +{ |
| 22 | + readonly #id: MultichainAccountGroupId; |
| 23 | + |
| 24 | + readonly #wallet: MultichainAccountWallet<Account>; |
| 25 | + |
| 26 | + readonly #index: number; |
| 27 | + |
| 28 | + readonly #providers: AccountProvider<Account>[]; |
| 29 | + |
| 30 | + readonly #providerToAccounts: Map<AccountProvider<Account>, Account['id'][]>; |
| 31 | + |
| 32 | + readonly #accountToProvider: Map<Account['id'], AccountProvider<Account>>; |
| 33 | + |
| 34 | + constructor({ |
| 35 | + groupIndex, |
| 36 | + wallet, |
| 37 | + providers, |
| 38 | + }: { |
| 39 | + groupIndex: number; |
| 40 | + wallet: MultichainAccountWallet<Account>; |
| 41 | + providers: AccountProvider<Account>[]; |
| 42 | + }) { |
| 43 | + this.#id = toMultichainAccountGroupId(wallet.id, groupIndex); |
| 44 | + this.#index = groupIndex; |
| 45 | + this.#wallet = wallet; |
| 46 | + this.#providers = providers; |
| 47 | + this.#providerToAccounts = new Map(); |
| 48 | + this.#accountToProvider = new Map(); |
| 49 | + |
| 50 | + this.sync(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Force multichain account synchronization. |
| 55 | + * |
| 56 | + * This can be used if account providers got new accounts that the multichain |
| 57 | + * account doesn't know about. |
| 58 | + */ |
| 59 | + sync(): void { |
| 60 | + // Clear reverse mapping and re-construct it entirely based on the refreshed |
| 61 | + // list of accounts from each providers. |
| 62 | + this.#accountToProvider.clear(); |
| 63 | + |
| 64 | + for (const provider of this.#providers) { |
| 65 | + // Filter account only for that index. |
| 66 | + const accounts = []; |
| 67 | + for (const account of provider.getAccounts()) { |
| 68 | + if ( |
| 69 | + account.options.entropy.id === this.wallet.entropySource && |
| 70 | + account.options.entropy.groupIndex === this.index |
| 71 | + ) { |
| 72 | + // We only use IDs to always fetch the latest version of accounts. |
| 73 | + accounts.push(account.id); |
| 74 | + } |
| 75 | + } |
| 76 | + this.#providerToAccounts.set(provider, accounts); |
| 77 | + |
| 78 | + // Reverse-mapping for fast indexing. |
| 79 | + for (const id of accounts) { |
| 80 | + this.#accountToProvider.set(id, provider); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Gets the multichain account ID. |
| 87 | + * |
| 88 | + * @returns The multichain account ID. |
| 89 | + */ |
| 90 | + get id(): MultichainAccountGroupId { |
| 91 | + return this.#id; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Gets the multichain account type. |
| 96 | + * |
| 97 | + * @returns The multichain account type. |
| 98 | + */ |
| 99 | + get type(): AccountGroupType.MultichainAccount { |
| 100 | + return AccountGroupType.MultichainAccount; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Gets the multichain account's wallet reference (parent). |
| 105 | + * |
| 106 | + * @returns The multichain account's wallet. |
| 107 | + */ |
| 108 | + get wallet(): MultichainAccountWallet<Account> { |
| 109 | + return this.#wallet; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Gets the multichain account group index. |
| 114 | + * |
| 115 | + * @returns The multichain account group index. |
| 116 | + */ |
| 117 | + get index(): number { |
| 118 | + return this.#index; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Checks if there's any underlying accounts for this multichain accounts. |
| 123 | + * |
| 124 | + * @returns True if there's any underlying accounts, false otherwise. |
| 125 | + */ |
| 126 | + hasAccounts(): boolean { |
| 127 | + // If there's anything in the reverse-map, it means we have some accounts. |
| 128 | + return this.#accountToProvider.size > 0; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Gets the accounts for this multichain account. |
| 133 | + * |
| 134 | + * @returns The accounts. |
| 135 | + */ |
| 136 | + getAccounts(): Account[] { |
| 137 | + const allAccounts: Account[] = []; |
| 138 | + |
| 139 | + for (const [provider, accounts] of this.#providerToAccounts.entries()) { |
| 140 | + for (const id of accounts) { |
| 141 | + const account = provider.getAccount(id); |
| 142 | + |
| 143 | + if (account) { |
| 144 | + // If for some reason we cannot get this account from the provider, it |
| 145 | + // might means it has been deleted or something, so we just filter it |
| 146 | + // out. |
| 147 | + allAccounts.push(account); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return allAccounts; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Gets the account for a given account ID. |
| 157 | + * |
| 158 | + * @param id - Account ID. |
| 159 | + * @returns The account or undefined if not found. |
| 160 | + */ |
| 161 | + getAccount(id: Account['id']): Account | undefined { |
| 162 | + const provider = this.#accountToProvider.get(id); |
| 163 | + |
| 164 | + // If there's nothing in the map, it means we tried to get an account |
| 165 | + // that does not belong to this multichain account. |
| 166 | + if (!provider) { |
| 167 | + return undefined; |
| 168 | + } |
| 169 | + return provider.getAccount(id); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Query an account matching the selector. |
| 174 | + * |
| 175 | + * @param selector - Query selector. |
| 176 | + * @returns The account matching the selector or undefined if not matching. |
| 177 | + * @throws If multiple accounts match the selector. |
| 178 | + */ |
| 179 | + get(selector: AccountSelector<Account>): Account | undefined { |
| 180 | + const accounts = this.select(selector); |
| 181 | + |
| 182 | + if (accounts.length > 1) { |
| 183 | + throw new Error( |
| 184 | + `Too many account candidates, expected 1, got: ${accounts.length}`, |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + if (accounts.length === 0) { |
| 189 | + return undefined; |
| 190 | + } |
| 191 | + |
| 192 | + return accounts[0]; // This is safe, see checks above. |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Query accounts matching the selector. |
| 197 | + * |
| 198 | + * @param selector - Query selector. |
| 199 | + * @returns The accounts matching the selector. |
| 200 | + */ |
| 201 | + select(selector: AccountSelector<Account>): Account[] { |
| 202 | + return this.getAccounts().filter((account) => { |
| 203 | + let selected = true; |
| 204 | + |
| 205 | + if (selector.id) { |
| 206 | + selected &&= account.id === selector.id; |
| 207 | + } |
| 208 | + if (selector.address) { |
| 209 | + selected &&= account.address === selector.address; |
| 210 | + } |
| 211 | + if (selector.type) { |
| 212 | + selected &&= account.type === selector.type; |
| 213 | + } |
| 214 | + if (selector.methods !== undefined) { |
| 215 | + selected &&= selector.methods.some((method) => |
| 216 | + account.methods.includes(method), |
| 217 | + ); |
| 218 | + } |
| 219 | + if (selector.scopes !== undefined) { |
| 220 | + selected &&= selector.scopes.some((scope) => { |
| 221 | + return ( |
| 222 | + // This will cover specific EVM EOA scopes as well. |
| 223 | + isScopeEqualToAny(scope, account.scopes) |
| 224 | + ); |
| 225 | + }); |
| 226 | + } |
| 227 | + |
| 228 | + return selected; |
| 229 | + }); |
| 230 | + } |
| 231 | +} |
0 commit comments