UNPKG

@metamask/multichain-account-service

Version:
67 lines 2.91 kB
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 _BaseAccountProvider_instances, _BaseAccountProvider_getAccounts; import { isBip44Account } from "@metamask/account-api"; /** * Asserts a keyring account is BIP-44 compatible. * * @param account - Keyring account to check. * @throws If the keyring account is not compatible. */ export function assertIsBip44Account(account) { if (!isBip44Account(account)) { throw new Error('Created account is not BIP-44 compatible'); } } /** * Asserts that a list of keyring accounts are all BIP-44 compatible. * * @param accounts - Keyring accounts to check. * @throws If any of the keyring account is not compatible. */ export function assertAreBip44Accounts(accounts) { accounts.forEach(assertIsBip44Account); } export class BaseAccountProvider { constructor(messenger) { _BaseAccountProvider_instances.add(this); this.messenger = messenger; } getAccounts() { return __classPrivateFieldGet(this, _BaseAccountProvider_instances, "m", _BaseAccountProvider_getAccounts).call(this); } getAccount(id) { // TODO: Maybe just use a proper find for faster lookup? const [found] = __classPrivateFieldGet(this, _BaseAccountProvider_instances, "m", _BaseAccountProvider_getAccounts).call(this, (account) => account.id === id); if (!found) { throw new Error(`Unable to find account: ${id}`); } return found; } async withKeyring(selector, operation) { const result = await this.messenger.call('KeyringController:withKeyring', selector, ({ keyring, metadata }) => operation({ keyring: keyring, metadata, })); return result; } } _BaseAccountProvider_instances = new WeakSet(), _BaseAccountProvider_getAccounts = function _BaseAccountProvider_getAccounts(filter = () => true) { const accounts = []; for (const account of this.messenger.call( // NOTE: Even though the name is misleading, this only fetches all internal // accounts, including EVM and non-EVM. We might wanna change this action // name once we fully support multichain accounts. 'AccountsController:listMultichainAccounts')) { if (isBip44Account(account) && this.isAccountCompatible(account) && filter(account)) { accounts.push(account); } } return accounts; }; //# sourceMappingURL=BaseAccountProvider.mjs.map