@metamask/multichain-account-service
Version:
Service to manage multichain accounts
73 lines • 3.27 kB
JavaScript
;
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;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseAccountProvider = exports.assertAreBip44Accounts = exports.assertIsBip44Account = void 0;
const account_api_1 = require("@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.
*/
function assertIsBip44Account(account) {
if (!(0, account_api_1.isBip44Account)(account)) {
throw new Error('Created account is not BIP-44 compatible');
}
}
exports.assertIsBip44Account = assertIsBip44Account;
/**
* 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.
*/
function assertAreBip44Accounts(accounts) {
accounts.forEach(assertIsBip44Account);
}
exports.assertAreBip44Accounts = assertAreBip44Accounts;
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;
}
}
exports.BaseAccountProvider = BaseAccountProvider;
_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 ((0, account_api_1.isBip44Account)(account) &&
this.isAccountCompatible(account) &&
filter(account)) {
accounts.push(account);
}
}
return accounts;
};
//# sourceMappingURL=BaseAccountProvider.cjs.map