UNPKG

@metamask/multichain-account-service

Version:
98 lines 3.98 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 _BaseBip44AccountProvider_instances, _BaseBip44AccountProvider_getAccountIds; 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 BaseBip44AccountProvider { constructor(messenger) { _BaseBip44AccountProvider_instances.add(this); this.accounts = new Set(); this.messenger = messenger; } /** * Add accounts to the provider. * * Note: There's an implicit assumption that the accounts are BIP-44 compatible. * * @param accounts - The accounts to add. */ init(accounts) { for (const account of accounts) { this.accounts.add(account); } } /** * Get the accounts list for the provider from the AccountsController. * * @returns The accounts list. */ getAccounts() { const accountsIds = __classPrivateFieldGet(this, _BaseBip44AccountProvider_instances, "m", _BaseBip44AccountProvider_getAccountIds).call(this); const internalAccounts = this.messenger.call('AccountsController:getAccounts', accountsIds); // we cast here because we know that the accounts are BIP-44 compatible return internalAccounts; } /** * Get the account for the provider. * * @param id - The account ID. * @returns The account. * @throws If the account is not found. */ getAccount(id) { const hasAccount = this.accounts.has(id); if (!hasAccount) { throw new Error(`Unable to find account: ${id}`); } // We need to upcast here since InternalAccounts are not always BIP-44 compatible // but we know that the account is BIP-44 compatible here so it is safe to do so return this.messenger.call('AccountsController:getAccount', id); } /** * Run an operation against a V2 keyring selected by `selector`. * * Forwards to `KeyringController:withKeyringV2`. Use this for keyrings * that implement the unified V2 `Keyring` interface from * `@metamask/keyring-api/v2`. * * @param selector - The selector identifying the keyring. * @param operation - The operation to run with the selected keyring. * @returns The result of the operation. */ async withKeyringV2(selector, operation) { const result = await this.messenger.call('KeyringController:withKeyringV2', selector, ({ keyring, metadata }) => operation({ keyring: keyring, metadata, })); return result; } isAligned(_context, accountIds) { return (accountIds.length >= 1 && accountIds.every((id) => this.accounts.has(id))); } } _BaseBip44AccountProvider_instances = new WeakSet(), _BaseBip44AccountProvider_getAccountIds = function _BaseBip44AccountProvider_getAccountIds() { return [...this.accounts]; }; //# sourceMappingURL=BaseBip44AccountProvider.mjs.map