@metamask/multichain-account-service 10.0.3 → 11.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +43 -2
- package/dist/MultichainAccountGroup.cjs +15 -0
- package/dist/MultichainAccountGroup.cjs.map +1 -1
- package/dist/MultichainAccountGroup.d.cts +10 -0
- package/dist/MultichainAccountGroup.d.cts.map +1 -1
- package/dist/MultichainAccountGroup.d.mts +10 -0
- package/dist/MultichainAccountGroup.d.mts.map +1 -1
- package/dist/MultichainAccountGroup.mjs +15 -0
- package/dist/MultichainAccountGroup.mjs.map +1 -1
- package/dist/MultichainAccountService-method-action-types.cjs.map +1 -1
- package/dist/MultichainAccountService-method-action-types.d.cts +10 -6
- package/dist/MultichainAccountService-method-action-types.d.cts.map +1 -1
- package/dist/MultichainAccountService-method-action-types.d.mts +10 -6
- package/dist/MultichainAccountService-method-action-types.d.mts.map +1 -1
- package/dist/MultichainAccountService-method-action-types.mjs.map +1 -1
- package/dist/MultichainAccountService.cjs +67 -8
- package/dist/MultichainAccountService.cjs.map +1 -1
- package/dist/MultichainAccountService.d.cts +31 -7
- package/dist/MultichainAccountService.d.cts.map +1 -1
- package/dist/MultichainAccountService.d.mts +31 -7
- package/dist/MultichainAccountService.d.mts.map +1 -1
- package/dist/MultichainAccountService.mjs +67 -8
- package/dist/MultichainAccountService.mjs.map +1 -1
- package/dist/MultichainAccountWallet.cjs +20 -0
- package/dist/MultichainAccountWallet.cjs.map +1 -1
- package/dist/MultichainAccountWallet.d.cts +10 -0
- package/dist/MultichainAccountWallet.d.cts.map +1 -1
- package/dist/MultichainAccountWallet.d.mts +10 -0
- package/dist/MultichainAccountWallet.d.mts.map +1 -1
- package/dist/MultichainAccountWallet.mjs +20 -0
- package/dist/MultichainAccountWallet.mjs.map +1 -1
- package/dist/index.cjs +3 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/providers/AccountProviderWrapper.cjs +43 -0
- package/dist/providers/AccountProviderWrapper.cjs.map +1 -1
- package/dist/providers/AccountProviderWrapper.d.cts +37 -0
- package/dist/providers/AccountProviderWrapper.d.cts.map +1 -1
- package/dist/providers/AccountProviderWrapper.d.mts +37 -0
- package/dist/providers/AccountProviderWrapper.d.mts.map +1 -1
- package/dist/providers/AccountProviderWrapper.mjs +43 -0
- package/dist/providers/AccountProviderWrapper.mjs.map +1 -1
- package/dist/providers/BaseBip44AccountProvider.cjs +3 -18
- package/dist/providers/BaseBip44AccountProvider.cjs.map +1 -1
- package/dist/providers/BaseBip44AccountProvider.d.cts +36 -16
- package/dist/providers/BaseBip44AccountProvider.d.cts.map +1 -1
- package/dist/providers/BaseBip44AccountProvider.d.mts +36 -16
- package/dist/providers/BaseBip44AccountProvider.d.mts.map +1 -1
- package/dist/providers/BaseBip44AccountProvider.mjs +3 -18
- package/dist/providers/BaseBip44AccountProvider.mjs.map +1 -1
- package/dist/providers/EvmAccountProvider.cjs +19 -0
- package/dist/providers/EvmAccountProvider.cjs.map +1 -1
- package/dist/providers/EvmAccountProvider.d.cts +12 -0
- package/dist/providers/EvmAccountProvider.d.cts.map +1 -1
- package/dist/providers/EvmAccountProvider.d.mts +12 -0
- package/dist/providers/EvmAccountProvider.d.mts.map +1 -1
- package/dist/providers/EvmAccountProvider.mjs +19 -0
- package/dist/providers/EvmAccountProvider.mjs.map +1 -1
- package/dist/providers/SnapAccountProvider.cjs +28 -11
- package/dist/providers/SnapAccountProvider.cjs.map +1 -1
- package/dist/providers/SnapAccountProvider.d.cts +16 -4
- package/dist/providers/SnapAccountProvider.d.cts.map +1 -1
- package/dist/providers/SnapAccountProvider.d.mts +16 -4
- package/dist/providers/SnapAccountProvider.d.mts.map +1 -1
- package/dist/providers/SnapAccountProvider.mjs +28 -11
- package/dist/providers/SnapAccountProvider.mjs.map +1 -1
- package/dist/providers/XlmAccountProvider.cjs +83 -0
- package/dist/providers/XlmAccountProvider.cjs.map +1 -0
- package/dist/providers/XlmAccountProvider.d.cts +29 -0
- package/dist/providers/XlmAccountProvider.d.cts.map +1 -0
- package/dist/providers/XlmAccountProvider.d.mts +29 -0
- package/dist/providers/XlmAccountProvider.d.mts.map +1 -0
- package/dist/providers/XlmAccountProvider.mjs +79 -0
- package/dist/providers/XlmAccountProvider.mjs.map +1 -0
- package/dist/providers/index.cjs +1 -0
- package/dist/providers/index.cjs.map +1 -1
- package/dist/providers/index.d.cts +1 -0
- package/dist/providers/index.d.cts.map +1 -1
- package/dist/providers/index.d.mts +1 -0
- package/dist/providers/index.d.mts.map +1 -1
- package/dist/providers/index.mjs +1 -0
- package/dist/providers/index.mjs.map +1 -1
- package/dist/tests/accounts.cjs +29 -1
- package/dist/tests/accounts.cjs.map +1 -1
- package/dist/tests/accounts.d.cts +2 -0
- package/dist/tests/accounts.d.cts.map +1 -1
- package/dist/tests/accounts.d.mts +2 -0
- package/dist/tests/accounts.d.mts.map +1 -1
- package/dist/tests/accounts.mjs +29 -1
- package/dist/tests/accounts.mjs.map +1 -1
- package/dist/tests/providers.cjs +5 -0
- package/dist/tests/providers.cjs.map +1 -1
- package/dist/tests/providers.d.cts +2 -0
- package/dist/tests/providers.d.cts.map +1 -1
- package/dist/tests/providers.d.mts +2 -0
- package/dist/tests/providers.d.mts.map +1 -1
- package/dist/tests/providers.mjs +6 -1
- package/dist/tests/providers.mjs.map +1 -1
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +14 -1
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +14 -1
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs.map +1 -1
- package/package.json +9 -9
|
@@ -14,8 +14,8 @@ var _SnapAccountProvider_instances, _SnapAccountProvider_client, _SnapAccountPro
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.isSnapAccountProvider = exports.SnapAccountProvider = void 0;
|
|
16
16
|
const account_api_1 = require("@metamask/account-api");
|
|
17
|
+
const v2_1 = require("@metamask/eth-snap-keyring/v2");
|
|
17
18
|
const keyring_api_1 = require("@metamask/keyring-api");
|
|
18
|
-
const keyring_controller_1 = require("@metamask/keyring-controller");
|
|
19
19
|
const keyring_snap_client_1 = require("@metamask/keyring-snap-client");
|
|
20
20
|
const snaps_utils_1 = require("@metamask/snaps-utils");
|
|
21
21
|
const async_mutex_1 = require("async-mutex");
|
|
@@ -124,7 +124,7 @@ class SnapAccountProvider extends BaseBip44AccountProvider_1.BaseBip44AccountPro
|
|
|
124
124
|
// We still need to remove the accounts from the Snap keyring since we're
|
|
125
125
|
// about to create the same account again, which will use a new ID, but will
|
|
126
126
|
// keep using the same address, and the Snap keyring does not allow this.
|
|
127
|
-
await keyring.
|
|
127
|
+
await keyring.deleteAccount(account.id);
|
|
128
128
|
// The Snap has no account in its state for this one, we re-create it.
|
|
129
129
|
await this.createAccounts({
|
|
130
130
|
type: keyring_api_1.AccountCreationType.Bip44DeriveIndex,
|
|
@@ -223,11 +223,28 @@ class SnapAccountProvider extends BaseBip44AccountProvider_1.BaseBip44AccountPro
|
|
|
223
223
|
]);
|
|
224
224
|
return this.withSnap(async ({ keyring }) => this.createBip44Accounts(keyring, options));
|
|
225
225
|
}
|
|
226
|
+
/**
|
|
227
|
+
* Delete a snap account by id.
|
|
228
|
+
*
|
|
229
|
+
* Resolves the account's address from the tracked account, then forwards to
|
|
230
|
+
* the legacy `SnapKeyring.removeAccount(address)`. The Snap keyring takes
|
|
231
|
+
* care of notifying the snap to clean up its own state through the normal
|
|
232
|
+
* account-removal flow (same path used by `resyncAccounts`).
|
|
233
|
+
*
|
|
234
|
+
* @param id - The id of the account to delete.
|
|
235
|
+
*/
|
|
236
|
+
async deleteAccount(id) {
|
|
237
|
+
const account = this.getAccount(id);
|
|
238
|
+
await __classPrivateFieldGet(this, _SnapAccountProvider_instances, "m", _SnapAccountProvider_withSnapKeyring).call(this, async ({ keyring }) => {
|
|
239
|
+
await keyring.deleteAccount(account.id);
|
|
240
|
+
});
|
|
241
|
+
this.accounts.delete(id);
|
|
242
|
+
}
|
|
226
243
|
}
|
|
227
244
|
exports.SnapAccountProvider = SnapAccountProvider;
|
|
228
245
|
_SnapAccountProvider_client = new WeakMap(), _SnapAccountProvider_queue = new WeakMap(), _SnapAccountProvider_trace = new WeakMap(), _SnapAccountProvider_instances = new WeakSet(), _SnapAccountProvider_getRestrictedSnapKeyring = async function _SnapAccountProvider_getRestrictedSnapKeyring() {
|
|
229
|
-
// NOTE: We're not supposed to make the keyring instance escape `
|
|
230
|
-
// we have to use the `
|
|
246
|
+
// NOTE: We're not supposed to make the keyring instance escape `withKeyringV2` but
|
|
247
|
+
// we have to use the `SnapKeyringV2` instance to be able to create Solana account
|
|
231
248
|
// without triggering UI confirmation.
|
|
232
249
|
// Also, creating account that way won't invalidate the Snap keyring state. The
|
|
233
250
|
// account will get created and persisted properly with the Snap account creation
|
|
@@ -239,16 +256,16 @@ _SnapAccountProvider_client = new WeakMap(), _SnapAccountProvider_queue = new We
|
|
|
239
256
|
return {
|
|
240
257
|
createAccount: async (options) =>
|
|
241
258
|
// We use the "unguarded" account creation here (see explanation above).
|
|
242
|
-
await createAccount(
|
|
259
|
+
await createAccount(options, {
|
|
243
260
|
displayAccountNameSuggestion: false,
|
|
244
261
|
displayConfirmation: false,
|
|
245
262
|
setSelectedAccount: false,
|
|
246
263
|
}),
|
|
247
|
-
createAccounts: async (options) => await createAccounts(
|
|
248
|
-
|
|
264
|
+
createAccounts: async (options) => await createAccounts(options),
|
|
265
|
+
deleteAccount: async (id) =>
|
|
249
266
|
// Though, when removing account, we can use the normal flow.
|
|
250
267
|
await __classPrivateFieldGet(this, _SnapAccountProvider_instances, "m", _SnapAccountProvider_withSnapKeyring).call(this, async ({ keyring }) => {
|
|
251
|
-
await keyring.
|
|
268
|
+
await keyring.deleteAccount(id);
|
|
252
269
|
}),
|
|
253
270
|
};
|
|
254
271
|
}, _SnapAccountProvider_getKeyringClientFromSnapId = function _SnapAccountProvider_getKeyringClientFromSnapId(snapId) {
|
|
@@ -264,9 +281,9 @@ _SnapAccountProvider_client = new WeakMap(), _SnapAccountProvider_queue = new We
|
|
|
264
281
|
},
|
|
265
282
|
});
|
|
266
283
|
}, _SnapAccountProvider_withSnapKeyring = async function _SnapAccountProvider_withSnapKeyring(operation) {
|
|
267
|
-
return this.
|
|
268
|
-
|
|
269
|
-
});
|
|
284
|
+
return this.withKeyringV2({
|
|
285
|
+
filter: (keyring) => (0, v2_1.isSnapKeyring)(keyring) && keyring.snapId === this.snapId,
|
|
286
|
+
}, (args) => operation(args));
|
|
270
287
|
};
|
|
271
288
|
const isSnapAccountProvider = (provider) => {
|
|
272
289
|
return provider instanceof SnapAccountProvider;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/SnapAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uDAA6D;AAI7D,uDAG+B;AAS/B,qEAA4D;AAE5D,uEAA8D;AAE9D,uDAAoD;AACpD,6CAAwC;AAExC,sDAIsB;AACtB,0CAAwC;AACxC,0CAAiE;AAEjE,6EAAsE;AACtE,uCAAsC;AA0CtC,MAAsB,mBAAoB,SAAQ,mDAAwB;IAWxE,YACE,MAAc,EACd,SAA4C,EAC5C,MAAiC;IACjC,0BAA0B;IAC1B,QAAuB,yBAAa;QAEpC,KAAK,CAAC,SAAS,CAAC,CAAC;;QAbV,8CAAuB;QAEvB,6CAAmB;QAEnB,6CAAsB;QAW7B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,uBAAA,IAAI,+BAAW,uBAAA,IAAI,uFAA4B,MAAhC,IAAI,EAA6B,MAAM,CAAC,MAAA,CAAC;QAExD,MAAM,cAAc,GAAG,MAAM,CAAC,cAAc,IAAI,QAAQ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D,CAAC,CAAC;AAJW,QAAA,qBAAqB,yBAIhC","sourcesContent":["import { assertIsBip44Account } from '@metamask/account-api';\nimport type { Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback, TraceRequest } from '@metamask/controller-utils';\nimport type { SnapKeyring } from '@metamask/eth-snap-keyring';\nimport {\n AccountCreationType,\n assertCreateAccountOptionIsSupported,\n} from '@metamask/keyring-api';\nimport type {\n CreateAccountBip44DeriveIndexOptions,\n CreateAccountBip44DeriveIndexRangeOptions,\n CreateAccountOptions,\n EntropySourceId,\n KeyringAccount,\n} from '@metamask/keyring-api';\nimport type { KeyringMetadata } from '@metamask/keyring-controller';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport { KeyringClient } from '@metamask/keyring-snap-client';\nimport type { Json, JsonRpcRequest, SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport { Semaphore } from 'async-mutex';\n\nimport {\n toCreateAccountsV2DataTraces,\n traceFallback,\n TraceName,\n} from '../analytics';\nimport { reportError } from '../errors';\nimport { projectLogger as log, WARNING_PREFIX } from '../logger';\nimport type { MultichainAccountServiceMessenger } from '../types';\nimport { BaseBip44AccountProvider } from './BaseBip44AccountProvider';\nimport { withTimeout } from './utils';\n\nexport type RestrictedSnapKeyring = {\n createAccount: (options: Record<string, Json>) => Promise<KeyringAccount>;\n createAccounts: (options: CreateAccountOptions) => Promise<KeyringAccount[]>;\n removeAccount: (address: string) => Promise<void>;\n};\n\nexport type SnapAccountProviderConfig = {\n maxConcurrency?: number;\n discovery: {\n enabled?: boolean;\n maxAttempts: number;\n timeoutMs: number;\n backOffMs: number;\n };\n createAccounts: {\n /**\n * Whether to enable account batching with `createAccounts` method. If `true`, accounts will\n * be created in batch.\n *\n * NOTE: The Snap has to implement this optional method for batching support.\n * Defaults to `false`.\n */\n batched: boolean;\n /**\n * Timeout for account creation operations.\n *\n * NOTE: The value might have to be adapted in case batching is enabled!\n */\n timeoutMs: number;\n };\n resyncAccounts?: {\n /**\n * Whether to automatically remove extra Snap accounts when the Snap has\n * more accounts than MetaMask. If `false`, a warning is logged instead.\n * Defaults to `true`.\n */\n autoRemoveExtraSnapAccounts?: boolean;\n };\n};\n\nexport abstract class SnapAccountProvider extends BaseBip44AccountProvider {\n readonly snapId: SnapId;\n\n protected readonly config: SnapAccountProviderConfig;\n\n readonly #client: KeyringClient;\n\n readonly #queue?: Semaphore;\n\n readonly #trace: TraceCallback;\n\n constructor(\n snapId: SnapId,\n messenger: MultichainAccountServiceMessenger,\n config: SnapAccountProviderConfig,\n /* istanbul ignore next */\n trace: TraceCallback = traceFallback,\n ) {\n super(messenger);\n\n this.snapId = snapId;\n this.#client = this.#getKeyringClientFromSnapId(snapId);\n\n const maxConcurrency = config.maxConcurrency ?? Infinity;\n this.config = {\n ...config,\n discovery: {\n ...config.discovery,\n enabled: config.discovery.enabled ?? true,\n },\n maxConcurrency,\n };\n\n // Create semaphore only if concurrency is limited\n if (isFinite(maxConcurrency)) {\n this.#queue = new Semaphore(maxConcurrency);\n }\n\n this.#trace = trace;\n }\n\n /**\n * Ensures that the Snap is ready to be used.\n *\n * @returns A promise that resolves when the Snap is ready.\n * @throws An error if the Snap could not become ready.\n */\n async ensureReady(): Promise<void> {\n return this.messenger.call('SnapAccountService:ensureReady', this.snapId);\n }\n\n /**\n * Wraps an async operation with concurrency limiting based on maxConcurrency config.\n * If maxConcurrency is Infinity (the default), the operation runs immediately without throttling.\n * Otherwise, it's queued through the semaphore to respect the concurrency limit.\n *\n * @param operation - The async operation to execute.\n * @returns The result of the operation.\n */\n protected async withMaxConcurrency<Result>(\n operation: () => Promise<Result>,\n ): Promise<Result> {\n if (this.#queue) {\n return this.#queue.runExclusive(operation);\n }\n return operation();\n }\n\n protected async trace<ReturnType>(\n request: TraceRequest,\n fn: () => Promise<ReturnType>,\n ): Promise<ReturnType> {\n return this.#trace(request, fn);\n }\n\n async #getRestrictedSnapKeyring(): Promise<RestrictedSnapKeyring> {\n // NOTE: We're not supposed to make the keyring instance escape `withKeyring` but\n // we have to use the `SnapKeyring` instance to be able to create Solana account\n // without triggering UI confirmation.\n // Also, creating account that way won't invalidate the Snap keyring state. The\n // account will get created and persisted properly with the Snap account creation\n // flow \"asynchronously\" (with `notify:accountCreated`).\n const { createAccount, createAccounts } = await this.#withSnapKeyring<{\n createAccount: SnapKeyring['createAccount'];\n createAccounts: SnapKeyring['createAccounts'];\n }>(async ({ keyring }) => ({\n createAccount: keyring.createAccount.bind(keyring),\n createAccounts: keyring.createAccounts.bind(keyring),\n }));\n\n return {\n createAccount: async (options) =>\n // We use the \"unguarded\" account creation here (see explanation above).\n await createAccount(this.snapId, options, {\n displayAccountNameSuggestion: false,\n displayConfirmation: false,\n setSelectedAccount: false,\n }),\n createAccounts: async (options) =>\n await createAccounts(this.snapId, options),\n removeAccount: async (address: string) =>\n // Though, when removing account, we can use the normal flow.\n await this.#withSnapKeyring(async ({ keyring }) => {\n await keyring.removeAccount(address);\n }),\n };\n }\n\n #getKeyringClientFromSnapId(snapId: string): KeyringClient {\n return new KeyringClient({\n send: async (request: JsonRpcRequest): Promise<Json> => {\n const response = await this.messenger.call(\n 'SnapController:handleRequest',\n {\n snapId: snapId as SnapId,\n origin: 'metamask',\n handler: HandlerType.OnKeyringRequest,\n request,\n },\n );\n return response as Json;\n },\n });\n }\n\n async resyncAccounts(\n accounts: Bip44Account<InternalAccount>[],\n ): Promise<void> {\n await this.withSnap(async ({ keyring }) => {\n const localSnapAccounts = accounts.filter(\n (account) => account.metadata.snap?.id === this.snapId,\n );\n const snapAccounts = new Set(\n (await this.#client.listAccounts()).map((account) => account.id),\n );\n\n // NOTE: This should never happen, but if it does, we recover by deleting the\n // extra accounts from the Snap to bring it back in sync with MetaMask.\n if (localSnapAccounts.length < snapAccounts.size) {\n const autoRemoveExtraSnapAccounts =\n this.config.resyncAccounts?.autoRemoveExtraSnapAccounts ?? true;\n\n if (autoRemoveExtraSnapAccounts) {\n // Build a set of local account IDs for quick lookup\n const localAccountIds = new Set(\n localSnapAccounts.map((account) => account.id),\n );\n\n // Find and delete accounts that exist in Snap but not in MetaMask\n await Promise.all(\n [...snapAccounts].map(async (snapAccountId) => {\n try {\n if (!localAccountIds.has(snapAccountId)) {\n // This account exists in the Snap but not in MetaMask, delete it from\n // the Snap.\n await this.#client.deleteAccount(snapAccountId);\n // Update the local Set so subsequent checks use the correct size\n snapAccounts.delete(snapAccountId);\n }\n } catch (error) {\n reportError(\n this.messenger,\n `Unable to delete de-synced Snap account: ${this.snapId}`,\n error,\n {\n provider: this.getName(),\n snapAccountId,\n },\n );\n }\n }),\n );\n } else {\n const message = `Snap \"${this.snapId}\" has de-synced accounts, Snap has more accounts than MetaMask! (${localSnapAccounts.length} < ${snapAccounts.size})`;\n log(`${WARNING_PREFIX} ${message}`);\n console.warn(message);\n return;\n }\n }\n\n // We want this part to be fast, so we only check for sizes, but we might need\n // to make a real \"diff\" between the 2 states to not miss any de-sync.\n if (localSnapAccounts.length > snapAccounts.size) {\n // We always use the MetaMask list as the main reference here.\n await Promise.all(\n localSnapAccounts.map(async (account) => {\n const { id: entropySource, groupIndex } = account.options.entropy;\n\n try {\n if (!snapAccounts.has(account.id)) {\n // We still need to remove the accounts from the Snap keyring since we're\n // about to create the same account again, which will use a new ID, but will\n // keep using the same address, and the Snap keyring does not allow this.\n await keyring.removeAccount(account.address);\n // The Snap has no account in its state for this one, we re-create it.\n await this.createAccounts({\n type: AccountCreationType.Bip44DeriveIndex,\n entropySource,\n groupIndex,\n });\n }\n } catch (error) {\n reportError(this.messenger, 'Unable to re-sync accounts', error, {\n provider: this.getName(),\n groupIndex,\n });\n }\n }),\n );\n }\n });\n }\n\n async #withSnapKeyring<CallbackResult = void>(\n operation: ({\n keyring,\n metadata,\n }: {\n keyring: SnapKeyring;\n metadata: KeyringMetadata;\n }) => Promise<CallbackResult>,\n ): Promise<CallbackResult> {\n return this.withKeyring<SnapKeyring, CallbackResult>(\n { type: KeyringTypes.snap },\n (args) => {\n return operation(args);\n },\n );\n }\n\n protected async withSnap<CallbackResult = void>(\n operation: (snap: {\n client: KeyringClient;\n keyring: RestrictedSnapKeyring;\n }) => Promise<CallbackResult>,\n ): Promise<CallbackResult> {\n await this.ensureReady();\n\n return await operation({\n client: this.#client,\n keyring: await this.#getRestrictedSnapKeyring(),\n });\n }\n\n abstract isAccountCompatible(account: Bip44Account<InternalAccount>): boolean;\n\n protected abstract createAccountV1(\n keyring: RestrictedSnapKeyring,\n options: { entropySource: EntropySourceId; groupIndex: number },\n ): Promise<KeyringAccount>;\n\n protected toBip44Account(\n account: KeyringAccount,\n _options: { entropySource: EntropySourceId; groupIndex: number },\n ): Bip44Account<KeyringAccount> {\n assertIsBip44Account(account);\n return account;\n }\n\n protected async createBip44Accounts(\n keyring: RestrictedSnapKeyring,\n options:\n | CreateAccountBip44DeriveIndexOptions\n | CreateAccountBip44DeriveIndexRangeOptions,\n ): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withMaxConcurrency(async () => {\n let groupIndexOffset = 0;\n let snapAccounts: KeyringAccount[] = [];\n\n const batched = this.config.createAccounts.batched ?? false;\n const { entropySource } = options;\n\n const createAccountV1 = async (\n groupIndex: number,\n ): Promise<KeyringAccount> =>\n await withTimeout(\n () =>\n this.trace(\n {\n name: TraceName.ProviderCreateAccountV1,\n data: {\n provider: this.getName(),\n groupIndex,\n },\n },\n () =>\n this.createAccountV1(keyring, { entropySource, groupIndex }),\n ),\n this.config.createAccounts.timeoutMs,\n );\n const createAccountsV2 = async (\n optionsV2:\n | CreateAccountBip44DeriveIndexOptions\n | CreateAccountBip44DeriveIndexRangeOptions,\n ): Promise<KeyringAccount[]> =>\n await withTimeout(\n () =>\n this.trace(\n {\n name: TraceName.ProviderCreateAccounts,\n data: {\n provider: this.getName(),\n ...toCreateAccountsV2DataTraces(optionsV2),\n },\n },\n () => keyring.createAccounts(optionsV2),\n ),\n this.config.createAccounts.timeoutMs,\n );\n\n if (options.type === `${AccountCreationType.Bip44DeriveIndexRange}`) {\n if (batched) {\n // Batch account creations.\n snapAccounts = await createAccountsV2(options);\n } else {\n const { range } = options;\n\n // Create accounts one by one.\n for (\n let groupIndex = range.from;\n groupIndex <= range.to;\n groupIndex++\n ) {\n const snapAccount = await createAccountV1(groupIndex);\n\n snapAccounts.push(snapAccount);\n }\n }\n\n // Group indices are sequential, so we just need the starting index.\n groupIndexOffset = options.range.from;\n } else {\n if (batched) {\n // Create account using new v2-like flow (no async flow + no Snap keyring events).\n snapAccounts = await createAccountsV2(options);\n } else {\n const { groupIndex } = options;\n\n // Create account using the existing v1 flow.\n const snapAccount = await createAccountV1(groupIndex);\n\n snapAccounts = [snapAccount];\n }\n\n // For single account, there will only be 1 account, so we can use the\n // provided group index directly.\n groupIndexOffset = options.groupIndex;\n }\n\n return snapAccounts.map((snapAccount, index) => {\n const groupIndex = groupIndexOffset + index;\n const account = this.toBip44Account(snapAccount, {\n entropySource,\n groupIndex,\n });\n\n this.accounts.add(snapAccount.id);\n return account;\n });\n });\n }\n\n async createAccounts(\n options: CreateAccountOptions,\n ): Promise<Bip44Account<KeyringAccount>[]> {\n assertCreateAccountOptionIsSupported(options, [\n `${AccountCreationType.Bip44DeriveIndex}`,\n `${AccountCreationType.Bip44DeriveIndexRange}`,\n ]);\n\n return this.withSnap(async ({ keyring }) =>\n this.createBip44Accounts(keyring, options),\n );\n }\n\n abstract discoverAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]>;\n}\n\nexport const isSnapAccountProvider = (\n provider: unknown,\n): provider is SnapAccountProvider => {\n return provider instanceof SnapAccountProvider;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"SnapAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/SnapAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uDAA6D;AAI7D,sDAA8D;AAC9D,uDAG+B;AAU/B,uEAA8D;AAE9D,uDAAoD;AAC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KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE;YAChD,MAAM,OAAO,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC;QAClC,CAAC,CAAC;KACL,CAAC;AACJ,CAAC,6GAE2B,MAAc;IACxC,OAAO,IAAI,mCAAa,CAAC;QACvB,IAAI,EAAE,KAAK,EAAE,OAAuB,EAAiB,EAAE;YACrD,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CACxC,8BAA8B,EAC9B;gBACE,MAAM,EAAE,MAAgB;gBACxB,MAAM,EAAE,UAAU;gBAClB,OAAO,EAAE,yBAAW,CAAC,gBAAgB;gBACrC,OAAO;aACR,CACF,CAAC;YACF,OAAO,QAAgB,CAAC;QAC1B,CAAC;KACF,CAAC,CAAC;AACL,CAAC,yCA0FD,KAAK,+CACH,SAM6B;IAE7B,OAAO,IAAI,CAAC,aAAa,CACvB;QACE,MAAM,EAAE,CAAC,OAAO,EAAE,EAAE,CAClB,IAAA,kBAAa,EAAC,OAAO,CAAC,IAAI,OAAO,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM;KAC3D,EACD,CAAC,IAAI,EAAE,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,CAC1B,CAAC;AACJ,CAAC;AA6KI,MAAM,qBAAqB,GAAG,CACnC,QAAiB,EACgB,EAAE;IACnC,OAAO,QAAQ,YAAY,mBAAmB,CAAC;AACjD,CAAC,CAAC;AAJW,QAAA,qBAAqB,yBAIhC","sourcesContent":["import { assertIsBip44Account } from '@metamask/account-api';\nimport type { Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback, TraceRequest } from '@metamask/controller-utils';\nimport type { SnapKeyring as SnapKeyringV2 } from '@metamask/eth-snap-keyring/v2';\nimport { isSnapKeyring } from '@metamask/eth-snap-keyring/v2';\nimport {\n AccountCreationType,\n assertCreateAccountOptionIsSupported,\n} from '@metamask/keyring-api';\nimport type {\n CreateAccountBip44DeriveIndexOptions,\n CreateAccountBip44DeriveIndexRangeOptions,\n CreateAccountOptions,\n EntropySourceId,\n KeyringAccount,\n} from '@metamask/keyring-api';\nimport type { KeyringMetadata } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport { KeyringClient } from '@metamask/keyring-snap-client';\nimport type { Json, JsonRpcRequest, SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport { Semaphore } from 'async-mutex';\n\nimport {\n toCreateAccountsV2DataTraces,\n traceFallback,\n TraceName,\n} from '../analytics';\nimport { reportError } from '../errors';\nimport { projectLogger as log, WARNING_PREFIX } from '../logger';\nimport type { MultichainAccountServiceMessenger } from '../types';\nimport { BaseBip44AccountProvider } from './BaseBip44AccountProvider';\nimport { withTimeout } from './utils';\n\nexport type RestrictedSnapKeyring = {\n createAccount: SnapKeyringV2['createAccount'];\n createAccounts: SnapKeyringV2['createAccounts'];\n deleteAccount: SnapKeyringV2['deleteAccount'];\n};\n\nexport type SnapAccountProviderConfig = {\n maxConcurrency?: number;\n discovery: {\n enabled?: boolean;\n maxAttempts: number;\n timeoutMs: number;\n backOffMs: number;\n };\n createAccounts: {\n /**\n * Whether to enable account batching with `createAccounts` method. If `true`, accounts will\n * be created in batch.\n *\n * NOTE: The Snap has to implement this optional method for batching support.\n * Defaults to `false`.\n */\n batched: boolean;\n /**\n * Timeout for account creation operations.\n *\n * NOTE: The value might have to be adapted in case batching is enabled!\n */\n timeoutMs: number;\n };\n resyncAccounts?: {\n /**\n * Whether to automatically remove extra Snap accounts when the Snap has\n * more accounts than MetaMask. If `false`, a warning is logged instead.\n * Defaults to `true`.\n */\n autoRemoveExtraSnapAccounts?: boolean;\n };\n};\n\nexport abstract class SnapAccountProvider extends BaseBip44AccountProvider {\n readonly snapId: SnapId;\n\n protected readonly config: SnapAccountProviderConfig;\n\n readonly #client: KeyringClient;\n\n readonly #queue?: Semaphore;\n\n readonly #trace: TraceCallback;\n\n constructor(\n snapId: SnapId,\n messenger: MultichainAccountServiceMessenger,\n config: SnapAccountProviderConfig,\n /* istanbul ignore next */\n trace: TraceCallback = traceFallback,\n ) {\n super(messenger);\n\n this.snapId = snapId;\n this.#client = this.#getKeyringClientFromSnapId(snapId);\n\n const maxConcurrency = config.maxConcurrency ?? Infinity;\n this.config = {\n ...config,\n discovery: {\n ...config.discovery,\n enabled: config.discovery.enabled ?? true,\n },\n maxConcurrency,\n };\n\n // Create semaphore only if concurrency is limited\n if (isFinite(maxConcurrency)) {\n this.#queue = new Semaphore(maxConcurrency);\n }\n\n this.#trace = trace;\n }\n\n /**\n * Ensures that the Snap is ready to be used.\n *\n * @returns A promise that resolves when the Snap is ready.\n * @throws An error if the Snap could not become ready.\n */\n async ensureReady(): Promise<void> {\n return this.messenger.call('SnapAccountService:ensureReady', this.snapId);\n }\n\n /**\n * Wraps an async operation with concurrency limiting based on maxConcurrency config.\n * If maxConcurrency is Infinity (the default), the operation runs immediately without throttling.\n * Otherwise, it's queued through the semaphore to respect the concurrency limit.\n *\n * @param operation - The async operation to execute.\n * @returns The result of the operation.\n */\n protected async withMaxConcurrency<Result>(\n operation: () => Promise<Result>,\n ): Promise<Result> {\n if (this.#queue) {\n return this.#queue.runExclusive(operation);\n }\n return operation();\n }\n\n protected async trace<ReturnType>(\n request: TraceRequest,\n fn: () => Promise<ReturnType>,\n ): Promise<ReturnType> {\n return this.#trace(request, fn);\n }\n\n async #getRestrictedSnapKeyring(): Promise<RestrictedSnapKeyring> {\n // NOTE: We're not supposed to make the keyring instance escape `withKeyringV2` but\n // we have to use the `SnapKeyringV2` instance to be able to create Solana account\n // without triggering UI confirmation.\n // Also, creating account that way won't invalidate the Snap keyring state. The\n // account will get created and persisted properly with the Snap account creation\n // flow \"asynchronously\" (with `notify:accountCreated`).\n const { createAccount, createAccounts } = await this.#withSnapKeyring<{\n createAccount: SnapKeyringV2['createAccount'];\n createAccounts: SnapKeyringV2['createAccounts'];\n }>(async ({ keyring }) => ({\n createAccount: keyring.createAccount.bind(keyring),\n createAccounts: keyring.createAccounts.bind(keyring),\n }));\n\n return {\n createAccount: async (options) =>\n // We use the \"unguarded\" account creation here (see explanation above).\n await createAccount(options, {\n displayAccountNameSuggestion: false,\n displayConfirmation: false,\n setSelectedAccount: false,\n }),\n createAccounts: async (options) => await createAccounts(options),\n deleteAccount: async (id: string) =>\n // Though, when removing account, we can use the normal flow.\n await this.#withSnapKeyring(async ({ keyring }) => {\n await keyring.deleteAccount(id);\n }),\n };\n }\n\n #getKeyringClientFromSnapId(snapId: string): KeyringClient {\n return new KeyringClient({\n send: async (request: JsonRpcRequest): Promise<Json> => {\n const response = await this.messenger.call(\n 'SnapController:handleRequest',\n {\n snapId: snapId as SnapId,\n origin: 'metamask',\n handler: HandlerType.OnKeyringRequest,\n request,\n },\n );\n return response as Json;\n },\n });\n }\n\n async resyncAccounts(\n accounts: Bip44Account<InternalAccount>[],\n ): Promise<void> {\n await this.withSnap(async ({ keyring }) => {\n const localSnapAccounts = accounts.filter(\n (account) => account.metadata.snap?.id === this.snapId,\n );\n const snapAccounts = new Set(\n (await this.#client.listAccounts()).map((account) => account.id),\n );\n\n // NOTE: This should never happen, but if it does, we recover by deleting the\n // extra accounts from the Snap to bring it back in sync with MetaMask.\n if (localSnapAccounts.length < snapAccounts.size) {\n const autoRemoveExtraSnapAccounts =\n this.config.resyncAccounts?.autoRemoveExtraSnapAccounts ?? true;\n\n if (autoRemoveExtraSnapAccounts) {\n // Build a set of local account IDs for quick lookup\n const localAccountIds = new Set(\n localSnapAccounts.map((account) => account.id),\n );\n\n // Find and delete accounts that exist in Snap but not in MetaMask\n await Promise.all(\n [...snapAccounts].map(async (snapAccountId) => {\n try {\n if (!localAccountIds.has(snapAccountId)) {\n // This account exists in the Snap but not in MetaMask, delete it from\n // the Snap.\n await this.#client.deleteAccount(snapAccountId);\n // Update the local Set so subsequent checks use the correct size\n snapAccounts.delete(snapAccountId);\n }\n } catch (error) {\n reportError(\n this.messenger,\n `Unable to delete de-synced Snap account: ${this.snapId}`,\n error,\n {\n provider: this.getName(),\n snapAccountId,\n },\n );\n }\n }),\n );\n } else {\n const message = `Snap \"${this.snapId}\" has de-synced accounts, Snap has more accounts than MetaMask! (${localSnapAccounts.length} < ${snapAccounts.size})`;\n log(`${WARNING_PREFIX} ${message}`);\n console.warn(message);\n return;\n }\n }\n\n // We want this part to be fast, so we only check for sizes, but we might need\n // to make a real \"diff\" between the 2 states to not miss any de-sync.\n if (localSnapAccounts.length > snapAccounts.size) {\n // We always use the MetaMask list as the main reference here.\n await Promise.all(\n localSnapAccounts.map(async (account) => {\n const { id: entropySource, groupIndex } = account.options.entropy;\n\n try {\n if (!snapAccounts.has(account.id)) {\n // We still need to remove the accounts from the Snap keyring since we're\n // about to create the same account again, which will use a new ID, but will\n // keep using the same address, and the Snap keyring does not allow this.\n await keyring.deleteAccount(account.id);\n // The Snap has no account in its state for this one, we re-create it.\n await this.createAccounts({\n type: AccountCreationType.Bip44DeriveIndex,\n entropySource,\n groupIndex,\n });\n }\n } catch (error) {\n reportError(this.messenger, 'Unable to re-sync accounts', error, {\n provider: this.getName(),\n groupIndex,\n });\n }\n }),\n );\n }\n });\n }\n\n async #withSnapKeyring<CallbackResult = void>(\n operation: ({\n keyring,\n metadata,\n }: {\n keyring: SnapKeyringV2;\n metadata: KeyringMetadata;\n }) => Promise<CallbackResult>,\n ): Promise<CallbackResult> {\n return this.withKeyringV2<SnapKeyringV2, CallbackResult>(\n {\n filter: (keyring) =>\n isSnapKeyring(keyring) && keyring.snapId === this.snapId,\n },\n (args) => operation(args),\n );\n }\n\n protected async withSnap<CallbackResult = void>(\n operation: (snap: {\n client: KeyringClient;\n keyring: RestrictedSnapKeyring;\n }) => Promise<CallbackResult>,\n ): Promise<CallbackResult> {\n await this.ensureReady();\n\n return await operation({\n client: this.#client,\n keyring: await this.#getRestrictedSnapKeyring(),\n });\n }\n\n abstract isAccountCompatible(account: Bip44Account<InternalAccount>): boolean;\n\n protected abstract createAccountV1(\n keyring: RestrictedSnapKeyring,\n options: { entropySource: EntropySourceId; groupIndex: number },\n ): Promise<KeyringAccount>;\n\n protected toBip44Account(\n account: KeyringAccount,\n _options: { entropySource: EntropySourceId; groupIndex: number },\n ): Bip44Account<KeyringAccount> {\n assertIsBip44Account(account);\n return account;\n }\n\n protected async createBip44Accounts(\n keyring: RestrictedSnapKeyring,\n options:\n | CreateAccountBip44DeriveIndexOptions\n | CreateAccountBip44DeriveIndexRangeOptions,\n ): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withMaxConcurrency(async () => {\n let groupIndexOffset = 0;\n let snapAccounts: KeyringAccount[] = [];\n\n const batched = this.config.createAccounts.batched ?? false;\n const { entropySource } = options;\n\n const createAccountV1 = async (\n groupIndex: number,\n ): Promise<KeyringAccount> =>\n await withTimeout(\n () =>\n this.trace(\n {\n name: TraceName.ProviderCreateAccountV1,\n data: {\n provider: this.getName(),\n groupIndex,\n },\n },\n () =>\n this.createAccountV1(keyring, { entropySource, groupIndex }),\n ),\n this.config.createAccounts.timeoutMs,\n );\n const createAccountsV2 = async (\n optionsV2:\n | CreateAccountBip44DeriveIndexOptions\n | CreateAccountBip44DeriveIndexRangeOptions,\n ): Promise<KeyringAccount[]> =>\n await withTimeout(\n () =>\n this.trace(\n {\n name: TraceName.ProviderCreateAccounts,\n data: {\n provider: this.getName(),\n ...toCreateAccountsV2DataTraces(optionsV2),\n },\n },\n () => keyring.createAccounts(optionsV2),\n ),\n this.config.createAccounts.timeoutMs,\n );\n\n if (options.type === `${AccountCreationType.Bip44DeriveIndexRange}`) {\n if (batched) {\n // Batch account creations.\n snapAccounts = await createAccountsV2(options);\n } else {\n const { range } = options;\n\n // Create accounts one by one.\n for (\n let groupIndex = range.from;\n groupIndex <= range.to;\n groupIndex++\n ) {\n const snapAccount = await createAccountV1(groupIndex);\n\n snapAccounts.push(snapAccount);\n }\n }\n\n // Group indices are sequential, so we just need the starting index.\n groupIndexOffset = options.range.from;\n } else {\n if (batched) {\n // Create account using new v2-like flow (no async flow + no Snap keyring events).\n snapAccounts = await createAccountsV2(options);\n } else {\n const { groupIndex } = options;\n\n // Create account using the existing v1 flow.\n const snapAccount = await createAccountV1(groupIndex);\n\n snapAccounts = [snapAccount];\n }\n\n // For single account, there will only be 1 account, so we can use the\n // provided group index directly.\n groupIndexOffset = options.groupIndex;\n }\n\n return snapAccounts.map((snapAccount, index) => {\n const groupIndex = groupIndexOffset + index;\n const account = this.toBip44Account(snapAccount, {\n entropySource,\n groupIndex,\n });\n\n this.accounts.add(snapAccount.id);\n return account;\n });\n });\n }\n\n async createAccounts(\n options: CreateAccountOptions,\n ): Promise<Bip44Account<KeyringAccount>[]> {\n assertCreateAccountOptionIsSupported(options, [\n `${AccountCreationType.Bip44DeriveIndex}`,\n `${AccountCreationType.Bip44DeriveIndexRange}`,\n ]);\n\n return this.withSnap(async ({ keyring }) =>\n this.createBip44Accounts(keyring, options),\n );\n }\n\n /**\n * Delete a snap account by id.\n *\n * Resolves the account's address from the tracked account, then forwards to\n * the legacy `SnapKeyring.removeAccount(address)`. The Snap keyring takes\n * care of notifying the snap to clean up its own state through the normal\n * account-removal flow (same path used by `resyncAccounts`).\n *\n * @param id - The id of the account to delete.\n */\n async deleteAccount(id: Bip44Account<KeyringAccount>['id']): Promise<void> {\n const account = this.getAccount(id);\n\n await this.#withSnapKeyring(async ({ keyring }) => {\n await keyring.deleteAccount(account.id);\n });\n\n this.accounts.delete(id);\n }\n\n abstract discoverAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]>;\n}\n\nexport const isSnapAccountProvider = (\n provider: unknown,\n): provider is SnapAccountProvider => {\n return provider instanceof SnapAccountProvider;\n};\n"]}
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import type { Bip44Account } from "@metamask/account-api";
|
|
2
2
|
import type { TraceCallback, TraceRequest } from "@metamask/controller-utils";
|
|
3
|
+
import type { SnapKeyring as SnapKeyringV2 } from "@metamask/eth-snap-keyring/v2";
|
|
3
4
|
import type { CreateAccountBip44DeriveIndexOptions, CreateAccountBip44DeriveIndexRangeOptions, CreateAccountOptions, EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
|
|
4
5
|
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
|
5
6
|
import { KeyringClient } from "@metamask/keyring-snap-client";
|
|
6
|
-
import type {
|
|
7
|
+
import type { SnapId } from "@metamask/snaps-sdk";
|
|
7
8
|
import type { MultichainAccountServiceMessenger } from "../types.cjs";
|
|
8
9
|
import { BaseBip44AccountProvider } from "./BaseBip44AccountProvider.cjs";
|
|
9
10
|
export type RestrictedSnapKeyring = {
|
|
10
|
-
createAccount:
|
|
11
|
-
createAccounts:
|
|
12
|
-
|
|
11
|
+
createAccount: SnapKeyringV2['createAccount'];
|
|
12
|
+
createAccounts: SnapKeyringV2['createAccounts'];
|
|
13
|
+
deleteAccount: SnapKeyringV2['deleteAccount'];
|
|
13
14
|
};
|
|
14
15
|
export type SnapAccountProviderConfig = {
|
|
15
16
|
maxConcurrency?: number;
|
|
@@ -82,6 +83,17 @@ export declare abstract class SnapAccountProvider extends BaseBip44AccountProvid
|
|
|
82
83
|
}): Bip44Account<KeyringAccount>;
|
|
83
84
|
protected createBip44Accounts(keyring: RestrictedSnapKeyring, options: CreateAccountBip44DeriveIndexOptions | CreateAccountBip44DeriveIndexRangeOptions): Promise<Bip44Account<KeyringAccount>[]>;
|
|
84
85
|
createAccounts(options: CreateAccountOptions): Promise<Bip44Account<KeyringAccount>[]>;
|
|
86
|
+
/**
|
|
87
|
+
* Delete a snap account by id.
|
|
88
|
+
*
|
|
89
|
+
* Resolves the account's address from the tracked account, then forwards to
|
|
90
|
+
* the legacy `SnapKeyring.removeAccount(address)`. The Snap keyring takes
|
|
91
|
+
* care of notifying the snap to clean up its own state through the normal
|
|
92
|
+
* account-removal flow (same path used by `resyncAccounts`).
|
|
93
|
+
*
|
|
94
|
+
* @param id - The id of the account to delete.
|
|
95
|
+
*/
|
|
96
|
+
deleteAccount(id: Bip44Account<KeyringAccount>['id']): Promise<void>;
|
|
85
97
|
abstract discoverAccounts(options: {
|
|
86
98
|
entropySource: EntropySourceId;
|
|
87
99
|
groupIndex: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/SnapAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,mCAAmC;
|
|
1
|
+
{"version":3,"file":"SnapAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/SnapAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,mCAAmC;AAC9E,OAAO,KAAK,EAAE,WAAW,IAAI,aAAa,EAAE,sCAAsC;AAMlF,OAAO,KAAK,EACV,oCAAoC,EACpC,yCAAyC,EACzC,oBAAoB,EACpB,eAAe,EACf,cAAc,EACf,8BAA8B;AAE/B,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AACtE,OAAO,EAAE,aAAa,EAAE,sCAAsC;AAC9D,OAAO,KAAK,EAAwB,MAAM,EAAE,4BAA4B;AAWxE,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAClE,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AAGtE,MAAM,MAAM,qBAAqB,GAAG;IAClC,aAAa,EAAE,aAAa,CAAC,eAAe,CAAC,CAAC;IAC9C,cAAc,EAAE,aAAa,CAAC,gBAAgB,CAAC,CAAC;IAChD,aAAa,EAAE,aAAa,CAAC,eAAe,CAAC,CAAC;CAC/C,CAAC;AAEF,MAAM,MAAM,yBAAyB,GAAG;IACtC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,SAAS,EAAE;QACT,OAAO,CAAC,EAAE,OAAO,CAAC;QAClB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eAAe,CAAC,GAAG,OAAO;IAE7E,SAAS,CAAC,QAAQ,CAAC,eAAe,CAChC,OAAO,EAAE,qBAAqB,EAC9B,OAAO,EAAE;QAAE,aAAa,EAAE,eAAe,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,GAC9D,OAAO,CAAC,cAAc,CAAC;IAE1B,SAAS,CAAC,cAAc,CACtB,OAAO,EAAE,cAAc,EACvB,QAAQ,EAAE;QAAE,aAAa,EAAE,eAAe,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,GAC/D,YAAY,CAAC,cAAc,CAAC;cAKf,mBAAmB,CACjC,OAAO,EAAE,qBAAqB,EAC9B,OAAO,EACH,oCAAoC,GACpC,yCAAyC,GAC5C,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;IAkGpC,cAAc,CAClB,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;IAW1C;;;;;;;;;OASG;IACG,aAAa,CAAC,EAAE,EAAE,YAAY,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAU1E,QAAQ,CAAC,gBAAgB,CAAC,OAAO,EAAE;QACjC,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;CAC5C;AAED,eAAO,MAAM,qBAAqB,aACtB,OAAO,oCAGlB,CAAC"}
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import type { Bip44Account } from "@metamask/account-api";
|
|
2
2
|
import type { TraceCallback, TraceRequest } from "@metamask/controller-utils";
|
|
3
|
+
import type { SnapKeyring as SnapKeyringV2 } from "@metamask/eth-snap-keyring/v2";
|
|
3
4
|
import type { CreateAccountBip44DeriveIndexOptions, CreateAccountBip44DeriveIndexRangeOptions, CreateAccountOptions, EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
|
|
4
5
|
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
|
5
6
|
import { KeyringClient } from "@metamask/keyring-snap-client";
|
|
6
|
-
import type {
|
|
7
|
+
import type { SnapId } from "@metamask/snaps-sdk";
|
|
7
8
|
import type { MultichainAccountServiceMessenger } from "../types.mjs";
|
|
8
9
|
import { BaseBip44AccountProvider } from "./BaseBip44AccountProvider.mjs";
|
|
9
10
|
export type RestrictedSnapKeyring = {
|
|
10
|
-
createAccount:
|
|
11
|
-
createAccounts:
|
|
12
|
-
|
|
11
|
+
createAccount: SnapKeyringV2['createAccount'];
|
|
12
|
+
createAccounts: SnapKeyringV2['createAccounts'];
|
|
13
|
+
deleteAccount: SnapKeyringV2['deleteAccount'];
|
|
13
14
|
};
|
|
14
15
|
export type SnapAccountProviderConfig = {
|
|
15
16
|
maxConcurrency?: number;
|
|
@@ -82,6 +83,17 @@ export declare abstract class SnapAccountProvider extends BaseBip44AccountProvid
|
|
|
82
83
|
}): Bip44Account<KeyringAccount>;
|
|
83
84
|
protected createBip44Accounts(keyring: RestrictedSnapKeyring, options: CreateAccountBip44DeriveIndexOptions | CreateAccountBip44DeriveIndexRangeOptions): Promise<Bip44Account<KeyringAccount>[]>;
|
|
84
85
|
createAccounts(options: CreateAccountOptions): Promise<Bip44Account<KeyringAccount>[]>;
|
|
86
|
+
/**
|
|
87
|
+
* Delete a snap account by id.
|
|
88
|
+
*
|
|
89
|
+
* Resolves the account's address from the tracked account, then forwards to
|
|
90
|
+
* the legacy `SnapKeyring.removeAccount(address)`. The Snap keyring takes
|
|
91
|
+
* care of notifying the snap to clean up its own state through the normal
|
|
92
|
+
* account-removal flow (same path used by `resyncAccounts`).
|
|
93
|
+
*
|
|
94
|
+
* @param id - The id of the account to delete.
|
|
95
|
+
*/
|
|
96
|
+
deleteAccount(id: Bip44Account<KeyringAccount>['id']): Promise<void>;
|
|
85
97
|
abstract discoverAccounts(options: {
|
|
86
98
|
entropySource: EntropySourceId;
|
|
87
99
|
groupIndex: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapAccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/SnapAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,mCAAmC;
|
|
1
|
+
{"version":3,"file":"SnapAccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/SnapAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,mCAAmC;AAC9E,OAAO,KAAK,EAAE,WAAW,IAAI,aAAa,EAAE,sCAAsC;AAMlF,OAAO,KAAK,EACV,oCAAoC,EACpC,yCAAyC,EACzC,oBAAoB,EACpB,eAAe,EACf,cAAc,EACf,8BAA8B;AAE/B,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AACtE,OAAO,EAAE,aAAa,EAAE,sCAAsC;AAC9D,OAAO,KAAK,EAAwB,MAAM,EAAE,4BAA4B;AAWxE,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAClE,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AAGtE,MAAM,MAAM,qBAAqB,GAAG;IAClC,aAAa,EAAE,aAAa,CAAC,eAAe,CAAC,CAAC;IAC9C,cAAc,EAAE,aAAa,CAAC,gBAAgB,CAAC,CAAC;IAChD,aAAa,EAAE,aAAa,CAAC,eAAe,CAAC,CAAC;CAC/C,CAAC;AAEF,MAAM,MAAM,yBAAyB,GAAG;IACtC,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,SAAS,EAAE;QACT,OAAO,CAAC,EAAE,OAAO,CAAC;QAClB,WAAW,EAAE,MAAM,CAAC;QACpB,SAAS,EAAE,MAAM,CAAC;QAClB,SAAS,EAAE,MAAM,CAAC;KACnB,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C9B,OAAO,EAAE;QAAE,aAAa,EAAE,eAAe,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,GAC9D,OAAO,CAAC,cAAc,CAAC;IAE1B,SAAS,CAAC,cAAc,CACtB,OAAO,EAAE,cAAc,EACvB,QAAQ,EAAE;QAAE,aAAa,EAAE,eAAe,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,GAC/D,YAAY,CAAC,cAAc,CAAC;cAKf,mBAAmB,CACjC,OAAO,EAAE,qBAAqB,EAC9B,OAAO,EACH,oCAAoC,GACpC,yCAAyC,GAC5C,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;IAkGpC,cAAc,CAClB,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;IAW1C;;;;;;;;;OASG;IACG,aAAa,CAAC,EAAE,EAAE,YAAY,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAU1E,QAAQ,CAAC,gBAAgB,CAAC,OAAO,EAAE;QACjC,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;CAC5C;AAED,eAAO,MAAM,qBAAqB,aACtB,OAAO,oCAGlB,CAAC"}
|
|
@@ -11,8 +11,8 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
11
11
|
};
|
|
12
12
|
var _SnapAccountProvider_instances, _SnapAccountProvider_client, _SnapAccountProvider_queue, _SnapAccountProvider_trace, _SnapAccountProvider_getRestrictedSnapKeyring, _SnapAccountProvider_getKeyringClientFromSnapId, _SnapAccountProvider_withSnapKeyring;
|
|
13
13
|
import { assertIsBip44Account } from "@metamask/account-api";
|
|
14
|
+
import { isSnapKeyring } from "@metamask/eth-snap-keyring/v2";
|
|
14
15
|
import { AccountCreationType, assertCreateAccountOptionIsSupported } from "@metamask/keyring-api";
|
|
15
|
-
import { KeyringTypes } from "@metamask/keyring-controller";
|
|
16
16
|
import { KeyringClient } from "@metamask/keyring-snap-client";
|
|
17
17
|
import { HandlerType } from "@metamask/snaps-utils";
|
|
18
18
|
import { Semaphore } from "async-mutex";
|
|
@@ -121,7 +121,7 @@ export class SnapAccountProvider extends BaseBip44AccountProvider {
|
|
|
121
121
|
// We still need to remove the accounts from the Snap keyring since we're
|
|
122
122
|
// about to create the same account again, which will use a new ID, but will
|
|
123
123
|
// keep using the same address, and the Snap keyring does not allow this.
|
|
124
|
-
await keyring.
|
|
124
|
+
await keyring.deleteAccount(account.id);
|
|
125
125
|
// The Snap has no account in its state for this one, we re-create it.
|
|
126
126
|
await this.createAccounts({
|
|
127
127
|
type: AccountCreationType.Bip44DeriveIndex,
|
|
@@ -220,10 +220,27 @@ export class SnapAccountProvider extends BaseBip44AccountProvider {
|
|
|
220
220
|
]);
|
|
221
221
|
return this.withSnap(async ({ keyring }) => this.createBip44Accounts(keyring, options));
|
|
222
222
|
}
|
|
223
|
+
/**
|
|
224
|
+
* Delete a snap account by id.
|
|
225
|
+
*
|
|
226
|
+
* Resolves the account's address from the tracked account, then forwards to
|
|
227
|
+
* the legacy `SnapKeyring.removeAccount(address)`. The Snap keyring takes
|
|
228
|
+
* care of notifying the snap to clean up its own state through the normal
|
|
229
|
+
* account-removal flow (same path used by `resyncAccounts`).
|
|
230
|
+
*
|
|
231
|
+
* @param id - The id of the account to delete.
|
|
232
|
+
*/
|
|
233
|
+
async deleteAccount(id) {
|
|
234
|
+
const account = this.getAccount(id);
|
|
235
|
+
await __classPrivateFieldGet(this, _SnapAccountProvider_instances, "m", _SnapAccountProvider_withSnapKeyring).call(this, async ({ keyring }) => {
|
|
236
|
+
await keyring.deleteAccount(account.id);
|
|
237
|
+
});
|
|
238
|
+
this.accounts.delete(id);
|
|
239
|
+
}
|
|
223
240
|
}
|
|
224
241
|
_SnapAccountProvider_client = new WeakMap(), _SnapAccountProvider_queue = new WeakMap(), _SnapAccountProvider_trace = new WeakMap(), _SnapAccountProvider_instances = new WeakSet(), _SnapAccountProvider_getRestrictedSnapKeyring = async function _SnapAccountProvider_getRestrictedSnapKeyring() {
|
|
225
|
-
// NOTE: We're not supposed to make the keyring instance escape `
|
|
226
|
-
// we have to use the `
|
|
242
|
+
// NOTE: We're not supposed to make the keyring instance escape `withKeyringV2` but
|
|
243
|
+
// we have to use the `SnapKeyringV2` instance to be able to create Solana account
|
|
227
244
|
// without triggering UI confirmation.
|
|
228
245
|
// Also, creating account that way won't invalidate the Snap keyring state. The
|
|
229
246
|
// account will get created and persisted properly with the Snap account creation
|
|
@@ -235,16 +252,16 @@ _SnapAccountProvider_client = new WeakMap(), _SnapAccountProvider_queue = new We
|
|
|
235
252
|
return {
|
|
236
253
|
createAccount: async (options) =>
|
|
237
254
|
// We use the "unguarded" account creation here (see explanation above).
|
|
238
|
-
await createAccount(
|
|
255
|
+
await createAccount(options, {
|
|
239
256
|
displayAccountNameSuggestion: false,
|
|
240
257
|
displayConfirmation: false,
|
|
241
258
|
setSelectedAccount: false,
|
|
242
259
|
}),
|
|
243
|
-
createAccounts: async (options) => await createAccounts(
|
|
244
|
-
|
|
260
|
+
createAccounts: async (options) => await createAccounts(options),
|
|
261
|
+
deleteAccount: async (id) =>
|
|
245
262
|
// Though, when removing account, we can use the normal flow.
|
|
246
263
|
await __classPrivateFieldGet(this, _SnapAccountProvider_instances, "m", _SnapAccountProvider_withSnapKeyring).call(this, async ({ keyring }) => {
|
|
247
|
-
await keyring.
|
|
264
|
+
await keyring.deleteAccount(id);
|
|
248
265
|
}),
|
|
249
266
|
};
|
|
250
267
|
}, _SnapAccountProvider_getKeyringClientFromSnapId = function _SnapAccountProvider_getKeyringClientFromSnapId(snapId) {
|
|
@@ -260,9 +277,9 @@ _SnapAccountProvider_client = new WeakMap(), _SnapAccountProvider_queue = new We
|
|
|
260
277
|
},
|
|
261
278
|
});
|
|
262
279
|
}, _SnapAccountProvider_withSnapKeyring = async function _SnapAccountProvider_withSnapKeyring(operation) {
|
|
263
|
-
return this.
|
|
264
|
-
|
|
265
|
-
});
|
|
280
|
+
return this.withKeyringV2({
|
|
281
|
+
filter: (keyring) => isSnapKeyring(keyring) && keyring.snapId === this.snapId,
|
|
282
|
+
}, (args) => operation(args));
|
|
266
283
|
};
|
|
267
284
|
export const isSnapAccountProvider = (provider) => {
|
|
268
285
|
return provider instanceof SnapAccountProvider;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/SnapAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,oBAAoB,EAAE,8BAA8B;AAI7D,OAAO,EACL,mBAAmB,EACnB,oCAAoC,EACrC,8BAA8B;AAS/B,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAE5D,OAAO,EAAE,aAAa,EAAE,sCAAsC;AAE9D,OAAO,EAAE,WAAW,EAAE,8BAA8B;AACpD,OAAO,EAAE,SAAS,EAAE,oBAAoB;AAExC,OAAO,EACL,4BAA4B,EAC5B,aAAa,EACb,SAAS,EACV,+BAAqB;AACtB,OAAO,EAAE,WAAW,EAAE,sBAAkB;AACxC,OAAO,EAAE,aAAa,IAAI,GAAG,EAAE,cAAc,EAAE,sBAAkB;AAEjE,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,OAAO,EAAE,WAAW,EAAE,oBAAgB;AA0CtC,MAAM,OAAgB,mBAAoB,SAAQ,wBAAwB;IAWxE,YACE,MAAc,EACd,SAA4C,EAC5C,MAAiC;IACjC,0BAA0B;IAC1B,QAAuB,aAAa;QAEpC,KAAK,CAAC,SAAS,CAAC,CAAC;;QAbV,8CAAuB;QAEvB,6CAAmB;QAEnB,6CAAsB;QAW7B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,uBAAA,IAAI,+BAAW,uBAAA,IAAI,uFAA4B,MAAhC,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,YAAY,mBAAmB,CAAC;AACjD,CAAC,CAAC","sourcesContent":["import { assertIsBip44Account } from '@metamask/account-api';\nimport type { Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback, TraceRequest } from '@metamask/controller-utils';\nimport type { SnapKeyring } from '@metamask/eth-snap-keyring';\nimport {\n AccountCreationType,\n assertCreateAccountOptionIsSupported,\n} from '@metamask/keyring-api';\nimport type {\n CreateAccountBip44DeriveIndexOptions,\n CreateAccountBip44DeriveIndexRangeOptions,\n CreateAccountOptions,\n EntropySourceId,\n KeyringAccount,\n} from '@metamask/keyring-api';\nimport type { KeyringMetadata } from '@metamask/keyring-controller';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport { KeyringClient } from '@metamask/keyring-snap-client';\nimport type { Json, JsonRpcRequest, SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport { Semaphore } from 'async-mutex';\n\nimport {\n toCreateAccountsV2DataTraces,\n traceFallback,\n TraceName,\n} from '../analytics';\nimport { reportError } from '../errors';\nimport { projectLogger as log, WARNING_PREFIX } from '../logger';\nimport type { MultichainAccountServiceMessenger } from '../types';\nimport { BaseBip44AccountProvider } from './BaseBip44AccountProvider';\nimport { withTimeout } from './utils';\n\nexport type RestrictedSnapKeyring = {\n createAccount: (options: Record<string, Json>) => Promise<KeyringAccount>;\n createAccounts: (options: CreateAccountOptions) => Promise<KeyringAccount[]>;\n removeAccount: (address: string) => Promise<void>;\n};\n\nexport type SnapAccountProviderConfig = {\n maxConcurrency?: number;\n discovery: {\n enabled?: boolean;\n maxAttempts: number;\n timeoutMs: number;\n backOffMs: number;\n };\n createAccounts: {\n /**\n * Whether to enable account batching with `createAccounts` method. If `true`, accounts will\n * be created in batch.\n *\n * NOTE: The Snap has to implement this optional method for batching support.\n * Defaults to `false`.\n */\n batched: boolean;\n /**\n * Timeout for account creation operations.\n *\n * NOTE: The value might have to be adapted in case batching is enabled!\n */\n timeoutMs: number;\n };\n resyncAccounts?: {\n /**\n * Whether to automatically remove extra Snap accounts when the Snap has\n * more accounts than MetaMask. If `false`, a warning is logged instead.\n * Defaults to `true`.\n */\n autoRemoveExtraSnapAccounts?: boolean;\n };\n};\n\nexport abstract class SnapAccountProvider extends BaseBip44AccountProvider {\n readonly snapId: SnapId;\n\n protected readonly config: SnapAccountProviderConfig;\n\n readonly #client: KeyringClient;\n\n readonly #queue?: Semaphore;\n\n readonly #trace: TraceCallback;\n\n constructor(\n snapId: SnapId,\n messenger: MultichainAccountServiceMessenger,\n config: SnapAccountProviderConfig,\n /* istanbul ignore next */\n trace: TraceCallback = traceFallback,\n ) {\n super(messenger);\n\n this.snapId = snapId;\n this.#client = this.#getKeyringClientFromSnapId(snapId);\n\n const maxConcurrency = config.maxConcurrency ?? Infinity;\n this.config = {\n ...config,\n discovery: {\n ...config.discovery,\n enabled: config.discovery.enabled ?? true,\n },\n maxConcurrency,\n };\n\n // Create semaphore only if concurrency is limited\n if (isFinite(maxConcurrency)) {\n this.#queue = new Semaphore(maxConcurrency);\n }\n\n this.#trace = trace;\n }\n\n /**\n * Ensures that the Snap is ready to be used.\n *\n * @returns A promise that resolves when the Snap is ready.\n * @throws An error if the Snap could not become ready.\n */\n async ensureReady(): Promise<void> {\n return this.messenger.call('SnapAccountService:ensureReady', this.snapId);\n }\n\n /**\n * Wraps an async operation with concurrency limiting based on maxConcurrency config.\n * If maxConcurrency is Infinity (the default), the operation runs immediately without throttling.\n * Otherwise, it's queued through the semaphore to respect the concurrency limit.\n *\n * @param operation - The async operation to execute.\n * @returns The result of the operation.\n */\n protected async withMaxConcurrency<Result>(\n operation: () => Promise<Result>,\n ): Promise<Result> {\n if (this.#queue) {\n return this.#queue.runExclusive(operation);\n }\n return operation();\n }\n\n protected async trace<ReturnType>(\n request: TraceRequest,\n fn: () => Promise<ReturnType>,\n ): Promise<ReturnType> {\n return this.#trace(request, fn);\n }\n\n async #getRestrictedSnapKeyring(): Promise<RestrictedSnapKeyring> {\n // NOTE: We're not supposed to make the keyring instance escape `withKeyring` but\n // we have to use the `SnapKeyring` instance to be able to create Solana account\n // without triggering UI confirmation.\n // Also, creating account that way won't invalidate the Snap keyring state. The\n // account will get created and persisted properly with the Snap account creation\n // flow \"asynchronously\" (with `notify:accountCreated`).\n const { createAccount, createAccounts } = await this.#withSnapKeyring<{\n createAccount: SnapKeyring['createAccount'];\n createAccounts: SnapKeyring['createAccounts'];\n }>(async ({ keyring }) => ({\n createAccount: keyring.createAccount.bind(keyring),\n createAccounts: keyring.createAccounts.bind(keyring),\n }));\n\n return {\n createAccount: async (options) =>\n // We use the \"unguarded\" account creation here (see explanation above).\n await createAccount(this.snapId, options, {\n displayAccountNameSuggestion: false,\n displayConfirmation: false,\n setSelectedAccount: false,\n }),\n createAccounts: async (options) =>\n await createAccounts(this.snapId, options),\n removeAccount: async (address: string) =>\n // Though, when removing account, we can use the normal flow.\n await this.#withSnapKeyring(async ({ keyring }) => {\n await keyring.removeAccount(address);\n }),\n };\n }\n\n #getKeyringClientFromSnapId(snapId: string): KeyringClient {\n return new KeyringClient({\n send: async (request: JsonRpcRequest): Promise<Json> => {\n const response = await this.messenger.call(\n 'SnapController:handleRequest',\n {\n snapId: snapId as SnapId,\n origin: 'metamask',\n handler: HandlerType.OnKeyringRequest,\n request,\n },\n );\n return response as Json;\n },\n });\n }\n\n async resyncAccounts(\n accounts: Bip44Account<InternalAccount>[],\n ): Promise<void> {\n await this.withSnap(async ({ keyring }) => {\n const localSnapAccounts = accounts.filter(\n (account) => account.metadata.snap?.id === this.snapId,\n );\n const snapAccounts = new Set(\n (await this.#client.listAccounts()).map((account) => account.id),\n );\n\n // NOTE: This should never happen, but if it does, we recover by deleting the\n // extra accounts from the Snap to bring it back in sync with MetaMask.\n if (localSnapAccounts.length < snapAccounts.size) {\n const autoRemoveExtraSnapAccounts =\n this.config.resyncAccounts?.autoRemoveExtraSnapAccounts ?? true;\n\n if (autoRemoveExtraSnapAccounts) {\n // Build a set of local account IDs for quick lookup\n const localAccountIds = new Set(\n localSnapAccounts.map((account) => account.id),\n );\n\n // Find and delete accounts that exist in Snap but not in MetaMask\n await Promise.all(\n [...snapAccounts].map(async (snapAccountId) => {\n try {\n if (!localAccountIds.has(snapAccountId)) {\n // This account exists in the Snap but not in MetaMask, delete it from\n // the Snap.\n await this.#client.deleteAccount(snapAccountId);\n // Update the local Set so subsequent checks use the correct size\n snapAccounts.delete(snapAccountId);\n }\n } catch (error) {\n reportError(\n this.messenger,\n `Unable to delete de-synced Snap account: ${this.snapId}`,\n error,\n {\n provider: this.getName(),\n snapAccountId,\n },\n );\n }\n }),\n );\n } else {\n const message = `Snap \"${this.snapId}\" has de-synced accounts, Snap has more accounts than MetaMask! (${localSnapAccounts.length} < ${snapAccounts.size})`;\n log(`${WARNING_PREFIX} ${message}`);\n console.warn(message);\n return;\n }\n }\n\n // We want this part to be fast, so we only check for sizes, but we might need\n // to make a real \"diff\" between the 2 states to not miss any de-sync.\n if (localSnapAccounts.length > snapAccounts.size) {\n // We always use the MetaMask list as the main reference here.\n await Promise.all(\n localSnapAccounts.map(async (account) => {\n const { id: entropySource, groupIndex } = account.options.entropy;\n\n try {\n if (!snapAccounts.has(account.id)) {\n // We still need to remove the accounts from the Snap keyring since we're\n // about to create the same account again, which will use a new ID, but will\n // keep using the same address, and the Snap keyring does not allow this.\n await keyring.removeAccount(account.address);\n // The Snap has no account in its state for this one, we re-create it.\n await this.createAccounts({\n type: AccountCreationType.Bip44DeriveIndex,\n entropySource,\n groupIndex,\n });\n }\n } catch (error) {\n reportError(this.messenger, 'Unable to re-sync accounts', error, {\n provider: this.getName(),\n groupIndex,\n });\n }\n }),\n );\n }\n });\n }\n\n async #withSnapKeyring<CallbackResult = void>(\n operation: ({\n keyring,\n metadata,\n }: {\n keyring: SnapKeyring;\n metadata: KeyringMetadata;\n }) => Promise<CallbackResult>,\n ): Promise<CallbackResult> {\n return this.withKeyring<SnapKeyring, CallbackResult>(\n { type: KeyringTypes.snap },\n (args) => {\n return operation(args);\n },\n );\n }\n\n protected async withSnap<CallbackResult = void>(\n operation: (snap: {\n client: KeyringClient;\n keyring: RestrictedSnapKeyring;\n }) => Promise<CallbackResult>,\n ): Promise<CallbackResult> {\n await this.ensureReady();\n\n return await operation({\n client: this.#client,\n keyring: await this.#getRestrictedSnapKeyring(),\n });\n }\n\n abstract isAccountCompatible(account: Bip44Account<InternalAccount>): boolean;\n\n protected abstract createAccountV1(\n keyring: RestrictedSnapKeyring,\n options: { entropySource: EntropySourceId; groupIndex: number },\n ): Promise<KeyringAccount>;\n\n protected toBip44Account(\n account: KeyringAccount,\n _options: { entropySource: EntropySourceId; groupIndex: number },\n ): Bip44Account<KeyringAccount> {\n assertIsBip44Account(account);\n return account;\n }\n\n protected async createBip44Accounts(\n keyring: RestrictedSnapKeyring,\n options:\n | CreateAccountBip44DeriveIndexOptions\n | CreateAccountBip44DeriveIndexRangeOptions,\n ): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withMaxConcurrency(async () => {\n let groupIndexOffset = 0;\n let snapAccounts: KeyringAccount[] = [];\n\n const batched = this.config.createAccounts.batched ?? false;\n const { entropySource } = options;\n\n const createAccountV1 = async (\n groupIndex: number,\n ): Promise<KeyringAccount> =>\n await withTimeout(\n () =>\n this.trace(\n {\n name: TraceName.ProviderCreateAccountV1,\n data: {\n provider: this.getName(),\n groupIndex,\n },\n },\n () =>\n this.createAccountV1(keyring, { entropySource, groupIndex }),\n ),\n this.config.createAccounts.timeoutMs,\n );\n const createAccountsV2 = async (\n optionsV2:\n | CreateAccountBip44DeriveIndexOptions\n | CreateAccountBip44DeriveIndexRangeOptions,\n ): Promise<KeyringAccount[]> =>\n await withTimeout(\n () =>\n this.trace(\n {\n name: TraceName.ProviderCreateAccounts,\n data: {\n provider: this.getName(),\n ...toCreateAccountsV2DataTraces(optionsV2),\n },\n },\n () => keyring.createAccounts(optionsV2),\n ),\n this.config.createAccounts.timeoutMs,\n );\n\n if (options.type === `${AccountCreationType.Bip44DeriveIndexRange}`) {\n if (batched) {\n // Batch account creations.\n snapAccounts = await createAccountsV2(options);\n } else {\n const { range } = options;\n\n // Create accounts one by one.\n for (\n let groupIndex = range.from;\n groupIndex <= range.to;\n groupIndex++\n ) {\n const snapAccount = await createAccountV1(groupIndex);\n\n snapAccounts.push(snapAccount);\n }\n }\n\n // Group indices are sequential, so we just need the starting index.\n groupIndexOffset = options.range.from;\n } else {\n if (batched) {\n // Create account using new v2-like flow (no async flow + no Snap keyring events).\n snapAccounts = await createAccountsV2(options);\n } else {\n const { groupIndex } = options;\n\n // Create account using the existing v1 flow.\n const snapAccount = await createAccountV1(groupIndex);\n\n snapAccounts = [snapAccount];\n }\n\n // For single account, there will only be 1 account, so we can use the\n // provided group index directly.\n groupIndexOffset = options.groupIndex;\n }\n\n return snapAccounts.map((snapAccount, index) => {\n const groupIndex = groupIndexOffset + index;\n const account = this.toBip44Account(snapAccount, {\n entropySource,\n groupIndex,\n });\n\n this.accounts.add(snapAccount.id);\n return account;\n });\n });\n }\n\n async createAccounts(\n options: CreateAccountOptions,\n ): Promise<Bip44Account<KeyringAccount>[]> {\n assertCreateAccountOptionIsSupported(options, [\n `${AccountCreationType.Bip44DeriveIndex}`,\n `${AccountCreationType.Bip44DeriveIndexRange}`,\n ]);\n\n return this.withSnap(async ({ keyring }) =>\n this.createBip44Accounts(keyring, options),\n );\n }\n\n abstract discoverAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]>;\n}\n\nexport const isSnapAccountProvider = (\n provider: unknown,\n): provider is SnapAccountProvider => {\n return provider instanceof SnapAccountProvider;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"SnapAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/SnapAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,oBAAoB,EAAE,8BAA8B;AAI7D,OAAO,EAAE,aAAa,EAAE,sCAAsC;AAC9D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B,EAAE,KAAK;YAC1B,kBAAkB,EAAE,KAAK;SAC1B,CAAC;QACJ,cAAc,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,CAAC,MAAM,cAAc,CAAC,OAAO,CAAC;QAChE,aAAa,EAAE,KAAK,EAAE,EAAU,EAAE,EAAE;QAClC,6DAA6D;QAC7D,MAAM,uBAAA,IAAI,4EAAiB,MAArB,IAAI,EAAkB,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE;YAChD,MAAM,OAAO,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC;QAClC,CAAC,CAAC;KACL,CAAC;AACJ,CAAC,6GAE2B,MAAc;IACxC,OAAO,IAAI,aAAa,CAAC;QACvB,IAAI,EAAE,KAAK,EAAE,OAAuB,EAAiB,EAAE;YACrD,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CACxC,8BAA8B,EAC9B;gBACE,MAAM,EAAE,MAAgB;gBACxB,MAAM,EAAE,UAAU;gBAClB,OAAO,EAAE,WAAW,CAAC,gBAAgB;gBACrC,OAAO;aACR,CACF,CAAC;YACF,OAAO,QAAgB,CAAC;QAC1B,CAAC;KACF,CAAC,CAAC;AACL,CAAC,yCA0FD,KAAK,+CACH,SAM6B;IAE7B,OAAO,IAAI,CAAC,aAAa,CACvB;QACE,MAAM,EAAE,CAAC,OAAO,EAAE,EAAE,CAClB,aAAa,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,MAAM,KAAK,IAAI,CAAC,MAAM;KAC3D,EACD,CAAC,IAAI,EAAE,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,CAC1B,CAAC;AACJ,CAAC;AA6KH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CACnC,QAAiB,EACgB,EAAE;IACnC,OAAO,QAAQ,YAAY,mBAAmB,CAAC;AACjD,CAAC,CAAC","sourcesContent":["import { assertIsBip44Account } from '@metamask/account-api';\nimport type { Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback, TraceRequest } from '@metamask/controller-utils';\nimport type { SnapKeyring as SnapKeyringV2 } from '@metamask/eth-snap-keyring/v2';\nimport { isSnapKeyring } from '@metamask/eth-snap-keyring/v2';\nimport {\n AccountCreationType,\n assertCreateAccountOptionIsSupported,\n} from '@metamask/keyring-api';\nimport type {\n CreateAccountBip44DeriveIndexOptions,\n CreateAccountBip44DeriveIndexRangeOptions,\n CreateAccountOptions,\n EntropySourceId,\n KeyringAccount,\n} from '@metamask/keyring-api';\nimport type { KeyringMetadata } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport { KeyringClient } from '@metamask/keyring-snap-client';\nimport type { Json, JsonRpcRequest, SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport { Semaphore } from 'async-mutex';\n\nimport {\n toCreateAccountsV2DataTraces,\n traceFallback,\n TraceName,\n} from '../analytics';\nimport { reportError } from '../errors';\nimport { projectLogger as log, WARNING_PREFIX } from '../logger';\nimport type { MultichainAccountServiceMessenger } from '../types';\nimport { BaseBip44AccountProvider } from './BaseBip44AccountProvider';\nimport { withTimeout } from './utils';\n\nexport type RestrictedSnapKeyring = {\n createAccount: SnapKeyringV2['createAccount'];\n createAccounts: SnapKeyringV2['createAccounts'];\n deleteAccount: SnapKeyringV2['deleteAccount'];\n};\n\nexport type SnapAccountProviderConfig = {\n maxConcurrency?: number;\n discovery: {\n enabled?: boolean;\n maxAttempts: number;\n timeoutMs: number;\n backOffMs: number;\n };\n createAccounts: {\n /**\n * Whether to enable account batching with `createAccounts` method. If `true`, accounts will\n * be created in batch.\n *\n * NOTE: The Snap has to implement this optional method for batching support.\n * Defaults to `false`.\n */\n batched: boolean;\n /**\n * Timeout for account creation operations.\n *\n * NOTE: The value might have to be adapted in case batching is enabled!\n */\n timeoutMs: number;\n };\n resyncAccounts?: {\n /**\n * Whether to automatically remove extra Snap accounts when the Snap has\n * more accounts than MetaMask. If `false`, a warning is logged instead.\n * Defaults to `true`.\n */\n autoRemoveExtraSnapAccounts?: boolean;\n };\n};\n\nexport abstract class SnapAccountProvider extends BaseBip44AccountProvider {\n readonly snapId: SnapId;\n\n protected readonly config: SnapAccountProviderConfig;\n\n readonly #client: KeyringClient;\n\n readonly #queue?: Semaphore;\n\n readonly #trace: TraceCallback;\n\n constructor(\n snapId: SnapId,\n messenger: MultichainAccountServiceMessenger,\n config: SnapAccountProviderConfig,\n /* istanbul ignore next */\n trace: TraceCallback = traceFallback,\n ) {\n super(messenger);\n\n this.snapId = snapId;\n this.#client = this.#getKeyringClientFromSnapId(snapId);\n\n const maxConcurrency = config.maxConcurrency ?? Infinity;\n this.config = {\n ...config,\n discovery: {\n ...config.discovery,\n enabled: config.discovery.enabled ?? true,\n },\n maxConcurrency,\n };\n\n // Create semaphore only if concurrency is limited\n if (isFinite(maxConcurrency)) {\n this.#queue = new Semaphore(maxConcurrency);\n }\n\n this.#trace = trace;\n }\n\n /**\n * Ensures that the Snap is ready to be used.\n *\n * @returns A promise that resolves when the Snap is ready.\n * @throws An error if the Snap could not become ready.\n */\n async ensureReady(): Promise<void> {\n return this.messenger.call('SnapAccountService:ensureReady', this.snapId);\n }\n\n /**\n * Wraps an async operation with concurrency limiting based on maxConcurrency config.\n * If maxConcurrency is Infinity (the default), the operation runs immediately without throttling.\n * Otherwise, it's queued through the semaphore to respect the concurrency limit.\n *\n * @param operation - The async operation to execute.\n * @returns The result of the operation.\n */\n protected async withMaxConcurrency<Result>(\n operation: () => Promise<Result>,\n ): Promise<Result> {\n if (this.#queue) {\n return this.#queue.runExclusive(operation);\n }\n return operation();\n }\n\n protected async trace<ReturnType>(\n request: TraceRequest,\n fn: () => Promise<ReturnType>,\n ): Promise<ReturnType> {\n return this.#trace(request, fn);\n }\n\n async #getRestrictedSnapKeyring(): Promise<RestrictedSnapKeyring> {\n // NOTE: We're not supposed to make the keyring instance escape `withKeyringV2` but\n // we have to use the `SnapKeyringV2` instance to be able to create Solana account\n // without triggering UI confirmation.\n // Also, creating account that way won't invalidate the Snap keyring state. The\n // account will get created and persisted properly with the Snap account creation\n // flow \"asynchronously\" (with `notify:accountCreated`).\n const { createAccount, createAccounts } = await this.#withSnapKeyring<{\n createAccount: SnapKeyringV2['createAccount'];\n createAccounts: SnapKeyringV2['createAccounts'];\n }>(async ({ keyring }) => ({\n createAccount: keyring.createAccount.bind(keyring),\n createAccounts: keyring.createAccounts.bind(keyring),\n }));\n\n return {\n createAccount: async (options) =>\n // We use the \"unguarded\" account creation here (see explanation above).\n await createAccount(options, {\n displayAccountNameSuggestion: false,\n displayConfirmation: false,\n setSelectedAccount: false,\n }),\n createAccounts: async (options) => await createAccounts(options),\n deleteAccount: async (id: string) =>\n // Though, when removing account, we can use the normal flow.\n await this.#withSnapKeyring(async ({ keyring }) => {\n await keyring.deleteAccount(id);\n }),\n };\n }\n\n #getKeyringClientFromSnapId(snapId: string): KeyringClient {\n return new KeyringClient({\n send: async (request: JsonRpcRequest): Promise<Json> => {\n const response = await this.messenger.call(\n 'SnapController:handleRequest',\n {\n snapId: snapId as SnapId,\n origin: 'metamask',\n handler: HandlerType.OnKeyringRequest,\n request,\n },\n );\n return response as Json;\n },\n });\n }\n\n async resyncAccounts(\n accounts: Bip44Account<InternalAccount>[],\n ): Promise<void> {\n await this.withSnap(async ({ keyring }) => {\n const localSnapAccounts = accounts.filter(\n (account) => account.metadata.snap?.id === this.snapId,\n );\n const snapAccounts = new Set(\n (await this.#client.listAccounts()).map((account) => account.id),\n );\n\n // NOTE: This should never happen, but if it does, we recover by deleting the\n // extra accounts from the Snap to bring it back in sync with MetaMask.\n if (localSnapAccounts.length < snapAccounts.size) {\n const autoRemoveExtraSnapAccounts =\n this.config.resyncAccounts?.autoRemoveExtraSnapAccounts ?? true;\n\n if (autoRemoveExtraSnapAccounts) {\n // Build a set of local account IDs for quick lookup\n const localAccountIds = new Set(\n localSnapAccounts.map((account) => account.id),\n );\n\n // Find and delete accounts that exist in Snap but not in MetaMask\n await Promise.all(\n [...snapAccounts].map(async (snapAccountId) => {\n try {\n if (!localAccountIds.has(snapAccountId)) {\n // This account exists in the Snap but not in MetaMask, delete it from\n // the Snap.\n await this.#client.deleteAccount(snapAccountId);\n // Update the local Set so subsequent checks use the correct size\n snapAccounts.delete(snapAccountId);\n }\n } catch (error) {\n reportError(\n this.messenger,\n `Unable to delete de-synced Snap account: ${this.snapId}`,\n error,\n {\n provider: this.getName(),\n snapAccountId,\n },\n );\n }\n }),\n );\n } else {\n const message = `Snap \"${this.snapId}\" has de-synced accounts, Snap has more accounts than MetaMask! (${localSnapAccounts.length} < ${snapAccounts.size})`;\n log(`${WARNING_PREFIX} ${message}`);\n console.warn(message);\n return;\n }\n }\n\n // We want this part to be fast, so we only check for sizes, but we might need\n // to make a real \"diff\" between the 2 states to not miss any de-sync.\n if (localSnapAccounts.length > snapAccounts.size) {\n // We always use the MetaMask list as the main reference here.\n await Promise.all(\n localSnapAccounts.map(async (account) => {\n const { id: entropySource, groupIndex } = account.options.entropy;\n\n try {\n if (!snapAccounts.has(account.id)) {\n // We still need to remove the accounts from the Snap keyring since we're\n // about to create the same account again, which will use a new ID, but will\n // keep using the same address, and the Snap keyring does not allow this.\n await keyring.deleteAccount(account.id);\n // The Snap has no account in its state for this one, we re-create it.\n await this.createAccounts({\n type: AccountCreationType.Bip44DeriveIndex,\n entropySource,\n groupIndex,\n });\n }\n } catch (error) {\n reportError(this.messenger, 'Unable to re-sync accounts', error, {\n provider: this.getName(),\n groupIndex,\n });\n }\n }),\n );\n }\n });\n }\n\n async #withSnapKeyring<CallbackResult = void>(\n operation: ({\n keyring,\n metadata,\n }: {\n keyring: SnapKeyringV2;\n metadata: KeyringMetadata;\n }) => Promise<CallbackResult>,\n ): Promise<CallbackResult> {\n return this.withKeyringV2<SnapKeyringV2, CallbackResult>(\n {\n filter: (keyring) =>\n isSnapKeyring(keyring) && keyring.snapId === this.snapId,\n },\n (args) => operation(args),\n );\n }\n\n protected async withSnap<CallbackResult = void>(\n operation: (snap: {\n client: KeyringClient;\n keyring: RestrictedSnapKeyring;\n }) => Promise<CallbackResult>,\n ): Promise<CallbackResult> {\n await this.ensureReady();\n\n return await operation({\n client: this.#client,\n keyring: await this.#getRestrictedSnapKeyring(),\n });\n }\n\n abstract isAccountCompatible(account: Bip44Account<InternalAccount>): boolean;\n\n protected abstract createAccountV1(\n keyring: RestrictedSnapKeyring,\n options: { entropySource: EntropySourceId; groupIndex: number },\n ): Promise<KeyringAccount>;\n\n protected toBip44Account(\n account: KeyringAccount,\n _options: { entropySource: EntropySourceId; groupIndex: number },\n ): Bip44Account<KeyringAccount> {\n assertIsBip44Account(account);\n return account;\n }\n\n protected async createBip44Accounts(\n keyring: RestrictedSnapKeyring,\n options:\n | CreateAccountBip44DeriveIndexOptions\n | CreateAccountBip44DeriveIndexRangeOptions,\n ): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withMaxConcurrency(async () => {\n let groupIndexOffset = 0;\n let snapAccounts: KeyringAccount[] = [];\n\n const batched = this.config.createAccounts.batched ?? false;\n const { entropySource } = options;\n\n const createAccountV1 = async (\n groupIndex: number,\n ): Promise<KeyringAccount> =>\n await withTimeout(\n () =>\n this.trace(\n {\n name: TraceName.ProviderCreateAccountV1,\n data: {\n provider: this.getName(),\n groupIndex,\n },\n },\n () =>\n this.createAccountV1(keyring, { entropySource, groupIndex }),\n ),\n this.config.createAccounts.timeoutMs,\n );\n const createAccountsV2 = async (\n optionsV2:\n | CreateAccountBip44DeriveIndexOptions\n | CreateAccountBip44DeriveIndexRangeOptions,\n ): Promise<KeyringAccount[]> =>\n await withTimeout(\n () =>\n this.trace(\n {\n name: TraceName.ProviderCreateAccounts,\n data: {\n provider: this.getName(),\n ...toCreateAccountsV2DataTraces(optionsV2),\n },\n },\n () => keyring.createAccounts(optionsV2),\n ),\n this.config.createAccounts.timeoutMs,\n );\n\n if (options.type === `${AccountCreationType.Bip44DeriveIndexRange}`) {\n if (batched) {\n // Batch account creations.\n snapAccounts = await createAccountsV2(options);\n } else {\n const { range } = options;\n\n // Create accounts one by one.\n for (\n let groupIndex = range.from;\n groupIndex <= range.to;\n groupIndex++\n ) {\n const snapAccount = await createAccountV1(groupIndex);\n\n snapAccounts.push(snapAccount);\n }\n }\n\n // Group indices are sequential, so we just need the starting index.\n groupIndexOffset = options.range.from;\n } else {\n if (batched) {\n // Create account using new v2-like flow (no async flow + no Snap keyring events).\n snapAccounts = await createAccountsV2(options);\n } else {\n const { groupIndex } = options;\n\n // Create account using the existing v1 flow.\n const snapAccount = await createAccountV1(groupIndex);\n\n snapAccounts = [snapAccount];\n }\n\n // For single account, there will only be 1 account, so we can use the\n // provided group index directly.\n groupIndexOffset = options.groupIndex;\n }\n\n return snapAccounts.map((snapAccount, index) => {\n const groupIndex = groupIndexOffset + index;\n const account = this.toBip44Account(snapAccount, {\n entropySource,\n groupIndex,\n });\n\n this.accounts.add(snapAccount.id);\n return account;\n });\n });\n }\n\n async createAccounts(\n options: CreateAccountOptions,\n ): Promise<Bip44Account<KeyringAccount>[]> {\n assertCreateAccountOptionIsSupported(options, [\n `${AccountCreationType.Bip44DeriveIndex}`,\n `${AccountCreationType.Bip44DeriveIndexRange}`,\n ]);\n\n return this.withSnap(async ({ keyring }) =>\n this.createBip44Accounts(keyring, options),\n );\n }\n\n /**\n * Delete a snap account by id.\n *\n * Resolves the account's address from the tracked account, then forwards to\n * the legacy `SnapKeyring.removeAccount(address)`. The Snap keyring takes\n * care of notifying the snap to clean up its own state through the normal\n * account-removal flow (same path used by `resyncAccounts`).\n *\n * @param id - The id of the account to delete.\n */\n async deleteAccount(id: Bip44Account<KeyringAccount>['id']): Promise<void> {\n const account = this.getAccount(id);\n\n await this.#withSnapKeyring(async ({ keyring }) => {\n await keyring.deleteAccount(account.id);\n });\n\n this.accounts.delete(id);\n }\n\n abstract discoverAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]>;\n}\n\nexport const isSnapAccountProvider = (\n provider: unknown,\n): provider is SnapAccountProvider => {\n return provider instanceof SnapAccountProvider;\n};\n"]}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.XlmAccountProvider = exports.XLM_ACCOUNT_PROVIDER_DEFAULT_CONFIG = exports.XLM_ACCOUNT_PROVIDER_NAME = void 0;
|
|
4
|
+
const keyring_api_1 = require("@metamask/keyring-api");
|
|
5
|
+
const keyring_controller_1 = require("@metamask/keyring-controller");
|
|
6
|
+
const analytics_1 = require("../analytics/index.cjs");
|
|
7
|
+
const traces_1 = require("../analytics/traces.cjs");
|
|
8
|
+
const SnapAccountProvider_1 = require("./SnapAccountProvider.cjs");
|
|
9
|
+
const utils_1 = require("./utils.cjs");
|
|
10
|
+
exports.XLM_ACCOUNT_PROVIDER_NAME = 'Stellar';
|
|
11
|
+
exports.XLM_ACCOUNT_PROVIDER_DEFAULT_CONFIG = {
|
|
12
|
+
maxConcurrency: 3,
|
|
13
|
+
discovery: {
|
|
14
|
+
enabled: true,
|
|
15
|
+
timeoutMs: 2000,
|
|
16
|
+
maxAttempts: 3,
|
|
17
|
+
backOffMs: 1000,
|
|
18
|
+
},
|
|
19
|
+
createAccounts: {
|
|
20
|
+
batched: true,
|
|
21
|
+
timeoutMs: 10000,
|
|
22
|
+
},
|
|
23
|
+
resyncAccounts: {
|
|
24
|
+
autoRemoveExtraSnapAccounts: true,
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
class XlmAccountProvider extends SnapAccountProvider_1.SnapAccountProvider {
|
|
28
|
+
constructor(messenger, config = exports.XLM_ACCOUNT_PROVIDER_DEFAULT_CONFIG, trace = analytics_1.traceFallback) {
|
|
29
|
+
super(XlmAccountProvider.XLM_SNAP_ID, messenger, config, trace);
|
|
30
|
+
this.capabilities = {
|
|
31
|
+
scopes: [keyring_api_1.XlmScope.Pubnet, keyring_api_1.XlmScope.Testnet],
|
|
32
|
+
bip44: {
|
|
33
|
+
deriveIndex: true,
|
|
34
|
+
deriveIndexRange: true,
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
getName() {
|
|
39
|
+
return XlmAccountProvider.NAME;
|
|
40
|
+
}
|
|
41
|
+
isAccountCompatible(account) {
|
|
42
|
+
return (account.type === keyring_api_1.XlmAccountType.Account &&
|
|
43
|
+
account.metadata.keyring.type === keyring_controller_1.KeyringTypes.snap);
|
|
44
|
+
}
|
|
45
|
+
createAccountV1(keyring, { entropySource, groupIndex, }) {
|
|
46
|
+
return keyring.createAccount({
|
|
47
|
+
entropySource,
|
|
48
|
+
index: groupIndex,
|
|
49
|
+
addressType: keyring_api_1.XlmAccountType.Account,
|
|
50
|
+
scope: keyring_api_1.XlmScope.Pubnet,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
async discoverAccounts({ entropySource, groupIndex, }) {
|
|
54
|
+
return this.withSnap(async ({ client, keyring }) => {
|
|
55
|
+
return await super.trace({
|
|
56
|
+
name: traces_1.TraceName.SnapDiscoverAccounts,
|
|
57
|
+
data: {
|
|
58
|
+
provider: this.getName(),
|
|
59
|
+
},
|
|
60
|
+
}, async () => {
|
|
61
|
+
if (!this.config.discovery.enabled) {
|
|
62
|
+
return [];
|
|
63
|
+
}
|
|
64
|
+
const discoveredAccounts = await (0, utils_1.withRetry)(() => (0, utils_1.withTimeout)(() => client.discoverAccounts([keyring_api_1.XlmScope.Pubnet], entropySource, groupIndex), this.config.discovery.timeoutMs), {
|
|
65
|
+
maxAttempts: this.config.discovery.maxAttempts,
|
|
66
|
+
backOffMs: this.config.discovery.backOffMs,
|
|
67
|
+
});
|
|
68
|
+
if (!discoveredAccounts.length) {
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
return await this.createBip44Accounts(keyring, {
|
|
72
|
+
type: keyring_api_1.AccountCreationType.Bip44DeriveIndex,
|
|
73
|
+
entropySource,
|
|
74
|
+
groupIndex,
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
exports.XlmAccountProvider = XlmAccountProvider;
|
|
81
|
+
XlmAccountProvider.NAME = exports.XLM_ACCOUNT_PROVIDER_NAME;
|
|
82
|
+
XlmAccountProvider.XLM_SNAP_ID = 'npm:@metamask/stellar-wallet-snap';
|
|
83
|
+
//# sourceMappingURL=XlmAccountProvider.cjs.map
|