@metamask/multichain-account-service
Version:
Service to manage multichain accounts
50 lines • 2.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EvmAccountProvider = void 0;
const keyring_api_1 = require("@metamask/keyring-api");
const keyring_controller_1 = require("@metamask/keyring-controller");
const BaseAccountProvider_1 = require("./BaseAccountProvider.cjs");
/**
* Asserts an internal account exists.
*
* @param account - The internal account to check.
* @throws An error if the internal account does not exist.
*/
function assertInternalAccountExists(account) {
if (!account) {
throw new Error('Internal account does not exist');
}
}
class EvmAccountProvider extends BaseAccountProvider_1.BaseAccountProvider {
isAccountCompatible(account) {
return (account.type === keyring_api_1.EthAccountType.Eoa &&
account.metadata.keyring.type === keyring_controller_1.KeyringTypes.hd);
}
async createAccounts({ entropySource, groupIndex, }) {
const [address] = await this.withKeyring({ id: entropySource }, async ({ keyring }) => {
const accounts = await keyring.getAccounts();
if (groupIndex < accounts.length) {
// Nothing new to create, we just re-use the existing accounts here,
return [accounts[groupIndex]];
}
// For now, we don't allow for gap, so if we need to create a new
// account, this has to be the next one.
if (groupIndex !== accounts.length) {
throw new Error('Trying to create too many accounts');
}
// Create next account (and returns their addresses).
return await keyring.addAccounts(1);
});
const account = this.messenger.call('AccountsController:getAccountByAddress', address);
// We MUST have the associated internal account.
assertInternalAccountExists(account);
const accountsArray = [account];
(0, BaseAccountProvider_1.assertAreBip44Accounts)(accountsArray);
return accountsArray;
}
async discoverAndCreateAccounts(_) {
return []; // TODO: Implement account discovery.
}
}
exports.EvmAccountProvider = EvmAccountProvider;
//# sourceMappingURL=EvmAccountProvider.cjs.map