@metamask/multichain-account-service
Version:
Service to manage multichain accounts
172 lines • 5.93 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isAccountProviderWrapper = exports.AccountProviderWrapper = void 0;
const BaseBip44AccountProvider_1 = require("./BaseBip44AccountProvider.cjs");
/**
* A simple wrapper that adds disable functionality to any BaseBip44AccountProvider.
* When disabled, the provider will not create new accounts and return empty results.
*/
class AccountProviderWrapper extends BaseBip44AccountProvider_1.BaseBip44AccountProvider {
constructor(messenger, provider) {
super(messenger);
this.isEnabled = true;
this.provider = provider;
}
getName() {
return this.provider.getName();
}
get capabilities() {
return this.provider.capabilities;
}
/**
* Forward initialization to the wrapped provider to ensure both
* instances share the same visible account IDs.
*
* @param accounts - Account IDs to initialize with.
*/
init(accounts) {
this.provider.init(accounts);
}
/**
* Returns the underlying (unwrapped) provider.
*
* Most callers should go through the wrapper's public surface so that the
* `disabled` gating is respected. This escape hatch is reserved for
* cleanup flows (e.g. wallet removal) that must see the wrapped
* provider's accounts regardless of enabled state, so that snap-backed
* accounts created while the provider was enabled can still be deleted
* after it has been disabled.
*
* @returns The wrapped provider instance.
*/
unwrap() {
return this.provider;
}
/**
* Set the enabled state for this provider.
*
* @param enabled - Whether the provider should be enabled.
*/
setEnabled(enabled) {
this.isEnabled = enabled;
}
/**
* Check if the provider is disabled.
*
* @returns True if the provider is disabled, false otherwise.
*/
isDisabled() {
return !this.isEnabled;
}
/**
* Override resyncAccounts to not execute it when disabled.
*
* @param accounts - List of local accounts.
*/
async resyncAccounts(accounts) {
if (!this.isEnabled) {
return;
}
await this.provider.resyncAccounts(accounts);
}
/**
* Override getAccounts to return empty array when disabled.
*
* @returns Array of accounts, or empty array if disabled.
*/
getAccounts() {
if (!this.isEnabled) {
return [];
}
return this.provider.getAccounts();
}
/**
* Override getAccount to throw when disabled.
*
* @param id - The account ID to retrieve.
* @returns The account with the specified ID.
* @throws When disabled or account not found.
*/
getAccount(id) {
if (!this.isEnabled) {
throw new Error('Provider is disabled');
}
return this.provider.getAccount(id);
}
/**
* Returns true immediately when disabled (a disabled provider is considered
* aligned by definition). Delegates to the wrapped provider otherwise.
*
* @param context - The entropy source and group index to check.
* @param context.entropySource - The entropy source to check against.
* @param context.groupIndex - The group index to check against.
* @param accountIds - Account IDs pre-filtered by the caller.
* @returns Whether the provider is aligned for the given context.
*/
isAligned(context, accountIds) {
if (!this.isEnabled) {
return true;
}
return this.provider.isAligned(context, accountIds);
}
/**
* Implement abstract method: Check if account is compatible.
* Delegates directly to wrapped provider - no runtime checks needed!
*
* @param account - The account to check.
* @returns True if the account is compatible.
*/
isAccountCompatible(account) {
return this.provider.isAccountCompatible(account);
}
/**
* Implement abstract method: Create accounts, returns empty array when disabled.
*
* @param options - Account creation options.
* @returns Promise resolving to created accounts, or empty array if disabled.
*/
async createAccounts(options) {
if (!this.isEnabled) {
return [];
}
return this.provider.createAccounts(options);
}
/**
* Forwards to the wrapped provider unconditionally, because deletion must run even
* when the wrapper is disabled, so that wallet-removal flows can clean up
* snap-backed accounts that were created while the provider was previously
* enabled.
*
* @param id - The id of the account to delete.
* @returns A promise that resolves when the account is deleted.
*/
async deleteAccount(id) {
return this.provider.deleteAccount(id);
}
/**
* Implement abstract method: Discover and create accounts, returns empty array when disabled.
*
* @param options - Account discovery options.
* @param options.entropySource - The entropy source to use.
* @param options.groupIndex - The group index to use.
* @returns Promise resolving to discovered accounts, or empty array if disabled.
*/
async discoverAccounts(options) {
if (!this.isEnabled) {
return [];
}
return this.provider.discoverAccounts(options);
}
}
exports.AccountProviderWrapper = AccountProviderWrapper;
/**
* Simple type guard to check if a provider is wrapped.
*
* @param provider - The provider to check.
* @returns True if the provider is an AccountProviderWrapper.
*/
function isAccountProviderWrapper(provider) {
return provider instanceof AccountProviderWrapper;
}
exports.isAccountProviderWrapper = isAccountProviderWrapper;
//# sourceMappingURL=AccountProviderWrapper.cjs.map