UNPKG

@metamask/multichain-account-service

Version:
214 lines 11.2 kB
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _MultichainAccountGroup_instances, _MultichainAccountGroup_id, _MultichainAccountGroup_wallet, _MultichainAccountGroup_groupIndex, _MultichainAccountGroup_providers, _MultichainAccountGroup_providerToAccounts, _MultichainAccountGroup_accountToProvider, _MultichainAccountGroup_messenger, _MultichainAccountGroup_log, _MultichainAccountGroup_initialized, _MultichainAccountGroup_clearAccountToProviderState, _MultichainAccountGroup_setState; import { AccountGroupType, select, selectOne } from "@metamask/account-api"; import { toMultichainAccountGroupId } from "@metamask/account-api"; import { projectLogger as log, createModuleLogger } from "./logger.mjs"; /** * A multichain account group that holds multiple accounts. */ export class MultichainAccountGroup { constructor({ groupIndex, wallet, providers, messenger, }) { _MultichainAccountGroup_instances.add(this); _MultichainAccountGroup_id.set(this, void 0); _MultichainAccountGroup_wallet.set(this, void 0); _MultichainAccountGroup_groupIndex.set(this, void 0); _MultichainAccountGroup_providers.set(this, void 0); _MultichainAccountGroup_providerToAccounts.set(this, void 0); _MultichainAccountGroup_accountToProvider.set(this, void 0); _MultichainAccountGroup_messenger.set(this, void 0); _MultichainAccountGroup_log.set(this, void 0); _MultichainAccountGroup_initialized.set(this, false); __classPrivateFieldSet(this, _MultichainAccountGroup_id, toMultichainAccountGroupId(wallet.id, groupIndex), "f"); __classPrivateFieldSet(this, _MultichainAccountGroup_groupIndex, groupIndex, "f"); __classPrivateFieldSet(this, _MultichainAccountGroup_wallet, wallet, "f"); __classPrivateFieldSet(this, _MultichainAccountGroup_providers, providers, "f"); __classPrivateFieldSet(this, _MultichainAccountGroup_messenger, messenger, "f"); __classPrivateFieldSet(this, _MultichainAccountGroup_providerToAccounts, new Map(), "f"); __classPrivateFieldSet(this, _MultichainAccountGroup_accountToProvider, new Map(), "f"); __classPrivateFieldSet(this, _MultichainAccountGroup_log, createModuleLogger(log, `[${__classPrivateFieldGet(this, _MultichainAccountGroup_id, "f")}]`), "f"); } /** * Initialize the multichain account group and construct the internal representation of accounts. * * @param groupState - The group state. */ init(groupState) { __classPrivateFieldGet(this, _MultichainAccountGroup_log, "f").call(this, 'Initializing group state...'); __classPrivateFieldGet(this, _MultichainAccountGroup_instances, "m", _MultichainAccountGroup_setState).call(this, groupState); __classPrivateFieldGet(this, _MultichainAccountGroup_log, "f").call(this, 'Finished initializing group state...'); __classPrivateFieldSet(this, _MultichainAccountGroup_initialized, true, "f"); } /** * Update the group state. * * @param groupState - The group state. */ update(groupState) { __classPrivateFieldGet(this, _MultichainAccountGroup_log, "f").call(this, 'Updating group state...'); __classPrivateFieldGet(this, _MultichainAccountGroup_instances, "m", _MultichainAccountGroup_setState).call(this, groupState); __classPrivateFieldGet(this, _MultichainAccountGroup_log, "f").call(this, 'Finished updating group state...'); if (__classPrivateFieldGet(this, _MultichainAccountGroup_initialized, "f")) { __classPrivateFieldGet(this, _MultichainAccountGroup_messenger, "f").publish('MultichainAccountService:multichainAccountGroupUpdated', this); } } /** * Gets the multichain account group ID. * * @returns The multichain account group ID. */ get id() { return __classPrivateFieldGet(this, _MultichainAccountGroup_id, "f"); } /** * Gets the multichain account group type. * * @returns The multichain account type. */ get type() { return AccountGroupType.MultichainAccount; } /** * Gets the multichain account's wallet reference (parent). * * @returns The multichain account's wallet. */ get wallet() { return __classPrivateFieldGet(this, _MultichainAccountGroup_wallet, "f"); } /** * Gets the multichain account group index. * * @returns The multichain account group index. */ get groupIndex() { return __classPrivateFieldGet(this, _MultichainAccountGroup_groupIndex, "f"); } /** * Checks if there's any underlying accounts for this multichain accounts. * * @returns True if there's any underlying accounts, false otherwise. */ hasAccounts() { // If there's anything in the reverse-map, it means we have some accounts. return __classPrivateFieldGet(this, _MultichainAccountGroup_accountToProvider, "f").size > 0; } /** * Gets the accounts for this multichain account. * * @returns The accounts. */ getAccounts() { const allAccounts = []; for (const [provider, accounts] of __classPrivateFieldGet(this, _MultichainAccountGroup_providerToAccounts, "f").entries()) { for (const id of accounts) { const account = provider.getAccount(id); if (account) { // If for some reason we cannot get this account from the provider, it // might means it has been deleted or something, so we just filter it // out. allAccounts.push(account); } } } return allAccounts; } /** * Gets the account IDs for this multichain account. * * @returns The account IDs. */ getAccountIds() { return [...__classPrivateFieldGet(this, _MultichainAccountGroup_accountToProvider, "f").keys()]; } /** * Gets the account for a given account ID. * * @param id - Account ID. * @returns The account or undefined if not found. */ getAccount(id) { const provider = __classPrivateFieldGet(this, _MultichainAccountGroup_accountToProvider, "f").get(id); // If there's nothing in the map, it means we tried to get an account // that does not belong to this multichain account. if (!provider) { return undefined; } return provider.getAccount(id); } /** * Query an account matching the selector. * * @param selector - Query selector. * @returns The account matching the selector or undefined if not matching. * @throws If multiple accounts match the selector. */ get(selector) { return selectOne(this.getAccounts(), selector); } /** * Query accounts matching the selector. * * @param selector - Query selector. * @returns The accounts matching the selector. */ select(selector) { return select(this.getAccounts(), selector); } /** * Check whether every provider has an aligned account in this group. * * A group is aligned when every registered provider reports that the * account IDs it contributed to this group are non-empty and owned by it. * Disabled {@link AccountProviderWrapper} instances always report `true`. * * @returns `true` when all providers are aligned for this group. */ isAligned() { return __classPrivateFieldGet(this, _MultichainAccountGroup_providers, "f").every((provider) => this.isProviderAligned(provider)); } /** * Check whether a single provider has an aligned account in this group. * * A provider is aligned when the account IDs it contributed to this group are * non-empty and owned by it. Disabled {@link AccountProviderWrapper} instances * always report `true`. * * @param provider - The provider to check. * @returns `true` when the provider is aligned for this group. */ isProviderAligned(provider) { return provider.isAligned({ entropySource: __classPrivateFieldGet(this, _MultichainAccountGroup_wallet, "f").entropySource, groupIndex: __classPrivateFieldGet(this, _MultichainAccountGroup_groupIndex, "f"), }, __classPrivateFieldGet(this, _MultichainAccountGroup_providerToAccounts, "f").get(provider) ?? []); } } _MultichainAccountGroup_id = new WeakMap(), _MultichainAccountGroup_wallet = new WeakMap(), _MultichainAccountGroup_groupIndex = new WeakMap(), _MultichainAccountGroup_providers = new WeakMap(), _MultichainAccountGroup_providerToAccounts = new WeakMap(), _MultichainAccountGroup_accountToProvider = new WeakMap(), _MultichainAccountGroup_messenger = new WeakMap(), _MultichainAccountGroup_log = new WeakMap(), _MultichainAccountGroup_initialized = new WeakMap(), _MultichainAccountGroup_instances = new WeakSet(), _MultichainAccountGroup_clearAccountToProviderState = function _MultichainAccountGroup_clearAccountToProviderState(provider) { __classPrivateFieldGet(this, _MultichainAccountGroup_accountToProvider, "f").forEach((accountProvider, id) => { if (accountProvider === provider) { __classPrivateFieldGet(this, _MultichainAccountGroup_accountToProvider, "f").delete(id); } }); }, _MultichainAccountGroup_setState = function _MultichainAccountGroup_setState(groupState) { for (const provider of __classPrivateFieldGet(this, _MultichainAccountGroup_providers, "f")) { const accountIds = groupState[provider.getName()]; if (accountIds) { __classPrivateFieldGet(this, _MultichainAccountGroup_instances, "m", _MultichainAccountGroup_clearAccountToProviderState).call(this, provider); __classPrivateFieldGet(this, _MultichainAccountGroup_providerToAccounts, "f").set(provider, accountIds); for (const accountId of accountIds) { __classPrivateFieldGet(this, _MultichainAccountGroup_accountToProvider, "f").set(accountId, provider); } } } }; //# sourceMappingURL=MultichainAccountGroup.mjs.map