@metamask/multichain-account-service
Version:
Service to manage multichain accounts
242 lines • 10.6 kB
text/typescript
import type { Bip44Account } from "@metamask/account-api";
import type { EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
import type { MultichainAccountGroup } from "./MultichainAccountGroup.mjs";
import { MultichainAccountWallet } from "./MultichainAccountWallet.mjs";
import { EvmAccountProviderConfig, Bip44AccountProvider, EVM_ACCOUNT_PROVIDER_NAME, BtcAccountProviderConfig, TrxAccountProviderConfig, BTC_ACCOUNT_PROVIDER_NAME, TRX_ACCOUNT_PROVIDER_NAME } from "./providers/index.mjs";
import { SOL_ACCOUNT_PROVIDER_NAME, SolAccountProviderConfig } from "./providers/SolAccountProvider.mjs";
import type { MultichainAccountServiceConfig, MultichainAccountServiceMessenger } from "./types.mjs";
/**
* Per-account failure detail attached to the aggregated Sentry report
* produced by {@link MultichainAccountService.removeMultichainAccountWallet}.
*
* Names the on-the-wire shape so consumers reading the Sentry context (and
* tests asserting on it) have one place to look.
*/
export type RemoveMultichainAccountWalletFailure = {
provider: string;
accountId?: Bip44Account<KeyringAccount>['id'];
error: unknown;
};
/**
* Aggregated context payload attached to the Sentry report produced by
* {@link MultichainAccountService.removeMultichainAccountWallet} when one
* or more per-account deletions fail.
*/
export type RemoveMultichainAccountWalletFailureContext = {
failures: RemoveMultichainAccountWalletFailure[];
};
export declare const serviceName = "MultichainAccountService";
/**
* The options that {@link MultichainAccountService} takes.
*/
export type MultichainAccountServiceOptions = {
messenger: MultichainAccountServiceMessenger;
providers?: Bip44AccountProvider[];
providerConfigs?: {
[EVM_ACCOUNT_PROVIDER_NAME]?: EvmAccountProviderConfig;
[SOL_ACCOUNT_PROVIDER_NAME]?: SolAccountProviderConfig;
[BTC_ACCOUNT_PROVIDER_NAME]?: BtcAccountProviderConfig;
[TRX_ACCOUNT_PROVIDER_NAME]?: TrxAccountProviderConfig;
};
config?: MultichainAccountServiceConfig;
};
/**
* The keys used to identify an account in the service state.
*/
export type StateKeys = {
entropySource: EntropySourceId;
groupIndex: number;
providerName: string;
};
/**
* The service state.
*/
export type ServiceState = {
[entropySource: StateKeys['entropySource']]: {
[groupIndex: string]: {
[providerName: StateKeys['providerName']]: Bip44Account<KeyringAccount>['id'][];
};
};
};
export type CreateWalletParams = {
type: 'restore';
password: string;
mnemonic: Uint8Array;
} | {
type: 'import';
mnemonic: Uint8Array;
} | {
type: 'create';
password: string;
};
/**
* Service to expose multichain accounts capabilities.
*/
export declare class MultichainAccountService {
#private;
/**
* The name of the service.
*/
name: typeof serviceName;
/**
* Constructs a new MultichainAccountService.
*
* @param options - The options.
* @param options.messenger - The messenger suited to this
* MultichainAccountService.
* @param options.providers - Optional list of account
* @param options.providerConfigs - Optional provider configs
* @param options.config - Optional config.
*/
constructor({ messenger, providers, providerConfigs, config, }: MultichainAccountServiceOptions);
/**
* Initialize the service and constructs the internal reprensentation of
* multichain accounts and wallets.
*/
init(): Promise<void>;
/**
* Re-synchronize MetaMask accounts and the providers accounts if needed.
*
* NOTE: This is mostly required if one of the providers (keyrings or Snaps)
* have different sets of accounts. This method would ensure that both are
* in-sync and use the same accounts (and same IDs).
*
* READ THIS CAREFULLY (State inconsistency bugs/de-sync)
* We've seen some problems were keyring accounts on some Snaps were not synchronized
* with the accounts on MM side. This causes problems where we cannot interact with
* those accounts because the Snap does know about them.
* To "workaround" this de-sync problem for now, we make sure that both parties are
* in-sync when the service boots up.
* ----------------------------------------------------------------------------------
*/
resyncAccounts(): Promise<void>;
/**
* Gets a reference to the multichain account wallet matching this entropy source.
*
* @param options - Options.
* @param options.entropySource - The entropy source of the multichain account.
* @throws If none multichain account match this entropy.
* @returns A reference to the multichain account wallet.
*/
getMultichainAccountWallet({ entropySource, }: {
entropySource: EntropySourceId;
}): MultichainAccountWallet<Bip44Account<KeyringAccount>>;
/**
* Gets an array of all multichain account wallets.
*
* @returns An array of all multichain account wallets.
*/
getMultichainAccountWallets(): MultichainAccountWallet<Bip44Account<KeyringAccount>>[];
/**
* Creates a new multichain account wallet by either importing an existing mnemonic,
* creating a new vault and keychain, or restoring a vault and keyring.
*
* NOTE: This method should only be called in client code where a mutex lock is acquired.
* `discoverAccounts` should be called after this method to discover and create accounts.
*
* @param params - The parameters to use to create the new wallet.
* @param params.mnemonic - The mnemonic to use to create the new wallet.
* @param params.password - The password to encrypt the vault with.
* @param params.type - The flow type to use to create the new wallet.
* @throws If the mnemonic has already been imported.
* @returns The new multichain account wallet.
*/
createMultichainAccountWallet(params: CreateWalletParams): Promise<MultichainAccountWallet<Bip44Account<KeyringAccount>>>;
/**
* Removes a multichain account wallet, deleting all of its accounts across
* every registered provider (EVM and snap-based).
*
* The deletion iterates providers (the source of truth for their own
* account lists) and filters each provider's accounts to those matching
* the wallet's entropy source. Cleanup is best-effort end-to-end: neither
* a single account deletion failure nor a failure to enumerate a given
* provider's accounts aborts cleanup of the remaining providers. If one or
* more operations fail, a single aggregated error is reported via
* `reportError` with all per-failure details in its context. The wallet is
* always removed from the service's internal map at the end.
*
* @param entropySource - The entropy source of the multichain account wallet.
*/
removeMultichainAccountWallet(entropySource: EntropySourceId): Promise<void>;
/**
* Gets a reference to the multichain account group matching this entropy source
* and a group index.
*
* @param options - Options.
* @param options.entropySource - The entropy source of the multichain account.
* @param options.groupIndex - The group index of the multichain account.
* @throws If none multichain account match this entropy source and group index.
* @returns A reference to the multichain account.
*/
getMultichainAccountGroup({ entropySource, groupIndex, }: {
entropySource: EntropySourceId;
groupIndex: number;
}): MultichainAccountGroup<Bip44Account<KeyringAccount>>;
/**
* Gets all multichain account groups for a given entropy source.
*
* @param options - Options.
* @param options.entropySource - The entropy source to query.
* @throws If no multichain accounts match this entropy source.
* @returns A list of all multichain accounts.
*/
getMultichainAccountGroups({ entropySource, }: {
entropySource: EntropySourceId;
}): MultichainAccountGroup<Bip44Account<KeyringAccount>>[];
/**
* Creates the next multichain account group.
*
* @param options - Options.
* @param options.entropySource - The wallet's entropy source.
* @returns The next multichain account group.
*/
createNextMultichainAccountGroup({ entropySource, }: {
entropySource: EntropySourceId;
}): Promise<MultichainAccountGroup<Bip44Account<KeyringAccount>>>;
/**
* Creates a multichain account group.
*
* @param options - Options.
* @param options.groupIndex - The group index to use.
* @param options.entropySource - The wallet's entropy source.
* @returns The multichain account group for this group index.
*/
createMultichainAccountGroup({ groupIndex, entropySource, }: {
groupIndex: number;
entropySource: EntropySourceId;
}): Promise<MultichainAccountGroup<Bip44Account<KeyringAccount>>>;
/**
* Creates multiple multichain account groups up to maxGroupIndex.
*
* @param params - Parameters for creating account groups.
* @param params.fromGroupIndex - Starting group index to create (inclusive) (defaults to 0).
* @param params.toGroupIndex - Maximum group index to create (inclusive).
* @param params.entropySource - The entropy source ID.
* @returns Array of created multichain account groups.
*/
createMultichainAccountGroups({ fromGroupIndex, toGroupIndex, entropySource, }: {
fromGroupIndex?: number;
toGroupIndex: number;
entropySource: EntropySourceId;
}): Promise<MultichainAccountGroup<Bip44Account<KeyringAccount>>[]>;
/**
* Set basic functionality state and trigger alignment if enabled.
* When basic functionality is disabled, snap-based providers are disabled.
* When enabled, all snap providers are enabled and wallet alignment is triggered.
* EVM providers are never disabled as they're required for basic wallet functionality.
*
* @param enabled - Whether basic functionality is enabled.
*/
setBasicFunctionality(enabled: boolean): Promise<void>;
/**
* Align all multichain account wallets.
*/
alignWallets(): Promise<void>;
/**
* Align a specific multichain account wallet.
*
* @param entropySource - The entropy source of the multichain account wallet.
*/
alignWallet(entropySource: EntropySourceId): Promise<void>;
}
//# sourceMappingURL=MultichainAccountService.d.mts.map