@metamask/multichain-account-service
Version:
Service to manage multichain accounts
46 lines • 2 kB
JavaScript
import { EthAccountType } from "@metamask/keyring-api";
import { KeyringTypes } from "@metamask/keyring-controller";
import { assertAreBip44Accounts, BaseAccountProvider } from "./BaseAccountProvider.mjs";
/**
* 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');
}
}
export class EvmAccountProvider extends BaseAccountProvider {
isAccountCompatible(account) {
return (account.type === EthAccountType.Eoa &&
account.metadata.keyring.type === 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];
assertAreBip44Accounts(accountsArray);
return accountsArray;
}
async discoverAndCreateAccounts(_) {
return []; // TODO: Implement account discovery.
}
}
//# sourceMappingURL=EvmAccountProvider.mjs.map