@metamask-previews/multichain-account-service 4.1.0-preview-1d3aa020 → 4.1.0-preview-67bc4e39
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 +0 -11
- package/dist/MultichainAccountService.cjs +2 -9
- package/dist/MultichainAccountService.cjs.map +1 -1
- package/dist/MultichainAccountService.d.cts +0 -1
- package/dist/MultichainAccountService.d.cts.map +1 -1
- package/dist/MultichainAccountService.d.mts +0 -1
- package/dist/MultichainAccountService.d.mts.map +1 -1
- package/dist/MultichainAccountService.mjs +2 -9
- package/dist/MultichainAccountService.mjs.map +1 -1
- package/dist/providers/BtcAccountProvider.cjs +32 -46
- package/dist/providers/BtcAccountProvider.cjs.map +1 -1
- package/dist/providers/BtcAccountProvider.d.cts +1 -2
- package/dist/providers/BtcAccountProvider.d.cts.map +1 -1
- package/dist/providers/BtcAccountProvider.d.mts +1 -2
- package/dist/providers/BtcAccountProvider.d.mts.map +1 -1
- package/dist/providers/BtcAccountProvider.mjs +32 -46
- package/dist/providers/BtcAccountProvider.mjs.map +1 -1
- package/dist/providers/SnapAccountProvider.cjs +54 -81
- package/dist/providers/SnapAccountProvider.cjs.map +1 -1
- package/dist/providers/SnapAccountProvider.d.cts +4 -16
- package/dist/providers/SnapAccountProvider.d.cts.map +1 -1
- package/dist/providers/SnapAccountProvider.d.mts +4 -16
- package/dist/providers/SnapAccountProvider.d.mts.map +1 -1
- package/dist/providers/SnapAccountProvider.mjs +54 -81
- package/dist/providers/SnapAccountProvider.mjs.map +1 -1
- package/dist/providers/SolAccountProvider.cjs +31 -36
- package/dist/providers/SolAccountProvider.cjs.map +1 -1
- package/dist/providers/SolAccountProvider.d.cts.map +1 -1
- package/dist/providers/SolAccountProvider.d.mts.map +1 -1
- package/dist/providers/SolAccountProvider.mjs +31 -36
- package/dist/providers/SolAccountProvider.mjs.map +1 -1
- package/dist/providers/TrxAccountProvider.cjs +29 -47
- package/dist/providers/TrxAccountProvider.cjs.map +1 -1
- package/dist/providers/TrxAccountProvider.d.cts +0 -1
- package/dist/providers/TrxAccountProvider.d.cts.map +1 -1
- package/dist/providers/TrxAccountProvider.d.mts +0 -1
- package/dist/providers/TrxAccountProvider.d.mts.map +1 -1
- package/dist/providers/TrxAccountProvider.mjs +29 -47
- package/dist/providers/TrxAccountProvider.mjs.map +1 -1
- package/dist/tests/messenger.cjs +1 -8
- package/dist/tests/messenger.cjs.map +1 -1
- package/dist/tests/messenger.d.cts +4 -9
- package/dist/tests/messenger.d.cts.map +1 -1
- package/dist/tests/messenger.d.mts +4 -9
- package/dist/tests/messenger.d.mts.map +1 -1
- package/dist/tests/messenger.mjs +1 -8
- package/dist/tests/messenger.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +4 -8
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +4 -8
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +2 -3
- package/dist/snaps/SnapPlatformWatcher.cjs +0 -60
- package/dist/snaps/SnapPlatformWatcher.cjs.map +0 -1
- package/dist/snaps/SnapPlatformWatcher.d.cts +0 -8
- package/dist/snaps/SnapPlatformWatcher.d.cts.map +0 -1
- package/dist/snaps/SnapPlatformWatcher.d.mts +0 -8
- package/dist/snaps/SnapPlatformWatcher.d.mts.map +0 -1
- package/dist/snaps/SnapPlatformWatcher.mjs +0 -57
- package/dist/snaps/SnapPlatformWatcher.mjs.map +0 -1
|
@@ -9,7 +9,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
10
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
11
|
};
|
|
12
|
-
var _SnapAccountProvider_instances,
|
|
12
|
+
var _SnapAccountProvider_instances, _SnapAccountProvider_queue, _SnapAccountProvider_trace, _SnapAccountProvider_getKeyringClientFromSnapId, _SnapAccountProvider_withSnapKeyring;
|
|
13
13
|
import { KeyringTypes } from "@metamask/keyring-controller";
|
|
14
14
|
import { KeyringClient } from "@metamask/keyring-snap-client";
|
|
15
15
|
import { HandlerType } from "@metamask/snaps-utils";
|
|
@@ -23,11 +23,10 @@ export class SnapAccountProvider extends BaseBip44AccountProvider {
|
|
|
23
23
|
trace = traceFallback) {
|
|
24
24
|
super(messenger);
|
|
25
25
|
_SnapAccountProvider_instances.add(this);
|
|
26
|
-
_SnapAccountProvider_client.set(this, void 0);
|
|
27
26
|
_SnapAccountProvider_queue.set(this, void 0);
|
|
28
27
|
_SnapAccountProvider_trace.set(this, void 0);
|
|
29
28
|
this.snapId = snapId;
|
|
30
|
-
|
|
29
|
+
this.client = __classPrivateFieldGet(this, _SnapAccountProvider_instances, "m", _SnapAccountProvider_getKeyringClientFromSnapId).call(this, snapId);
|
|
31
30
|
const maxConcurrency = config.maxConcurrency ?? Infinity;
|
|
32
31
|
this.config = {
|
|
33
32
|
...config,
|
|
@@ -43,15 +42,6 @@ export class SnapAccountProvider extends BaseBip44AccountProvider {
|
|
|
43
42
|
}
|
|
44
43
|
__classPrivateFieldSet(this, _SnapAccountProvider_trace, trace, "f");
|
|
45
44
|
}
|
|
46
|
-
/**
|
|
47
|
-
* Ensures that the Snap platform is ready to be used.
|
|
48
|
-
*
|
|
49
|
-
* @returns A promise that resolves when the platform is ready.
|
|
50
|
-
* @throws An error if the platform is not ready (only effective once the platform has been ready at least once).
|
|
51
|
-
*/
|
|
52
|
-
async ensureCanUseSnapPlatform() {
|
|
53
|
-
return this.messenger.call('MultichainAccountService:ensureCanUseSnapPlatform');
|
|
54
|
-
}
|
|
55
45
|
/**
|
|
56
46
|
* Wraps an async operation with concurrency limiting based on maxConcurrency config.
|
|
57
47
|
* If maxConcurrency is Infinity (the default), the operation runs immediately without throttling.
|
|
@@ -69,81 +59,64 @@ export class SnapAccountProvider extends BaseBip44AccountProvider {
|
|
|
69
59
|
async trace(request, fn) {
|
|
70
60
|
return __classPrivateFieldGet(this, _SnapAccountProvider_trace, "f").call(this, request, fn);
|
|
71
61
|
}
|
|
62
|
+
async getRestrictedSnapAccountCreator() {
|
|
63
|
+
// NOTE: We're not supposed to make the keyring instance escape `withKeyring` but
|
|
64
|
+
// we have to use the `SnapKeyring` instance to be able to create Solana account
|
|
65
|
+
// without triggering UI confirmation.
|
|
66
|
+
// Also, creating account that way won't invalidate the Snap keyring state. The
|
|
67
|
+
// account will get created and persisted properly with the Snap account creation
|
|
68
|
+
// flow "asynchronously" (with `notify:accountCreated`).
|
|
69
|
+
const createAccount = await __classPrivateFieldGet(this, _SnapAccountProvider_instances, "m", _SnapAccountProvider_withSnapKeyring).call(this, async ({ keyring }) => keyring.createAccount.bind(keyring));
|
|
70
|
+
return (options) => createAccount(this.snapId, options, {
|
|
71
|
+
displayAccountNameSuggestion: false,
|
|
72
|
+
displayConfirmation: false,
|
|
73
|
+
setSelectedAccount: false,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
72
76
|
async resyncAccounts(accounts) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
entropySource,
|
|
101
|
-
groupIndex,
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
catch (error) {
|
|
106
|
-
const sentryError = createSentryError(`Unable to re-sync account: ${groupIndex}`, error, {
|
|
107
|
-
provider: this.getName(),
|
|
77
|
+
const localSnapAccounts = accounts.filter((account) => account.metadata.snap && account.metadata.snap.id === this.snapId);
|
|
78
|
+
const snapAccounts = new Set((await this.client.listAccounts()).map((account) => account.id));
|
|
79
|
+
// NOTE: This should never happen, but we want to report that kind of errors still
|
|
80
|
+
// in case states are de-sync.
|
|
81
|
+
if (localSnapAccounts.length < snapAccounts.size) {
|
|
82
|
+
this.messenger.captureException?.(new Error(`Snap "${this.snapId}" has de-synced accounts, Snap has more accounts than MetaMask!`));
|
|
83
|
+
// We don't recover from this case yet.
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
// We want this part to be fast, so we only check for sizes, but we might need
|
|
87
|
+
// to make a real "diff" between the 2 states to not miss any de-sync.
|
|
88
|
+
if (localSnapAccounts.length > snapAccounts.size) {
|
|
89
|
+
// Accounts should never really be de-synced, so we want to log this to see how often this
|
|
90
|
+
// happens, cause that means that something else is buggy elsewhere...
|
|
91
|
+
this.messenger.captureException?.(new Error(`Snap "${this.snapId}" has de-synced accounts, we'll attempt to re-sync them...`));
|
|
92
|
+
// We always use the MetaMask list as the main reference here.
|
|
93
|
+
await Promise.all(localSnapAccounts.map(async (account) => {
|
|
94
|
+
const { id: entropySource, groupIndex } = account.options.entropy;
|
|
95
|
+
try {
|
|
96
|
+
if (!snapAccounts.has(account.id)) {
|
|
97
|
+
// We still need to remove the accounts from the Snap keyring since we're
|
|
98
|
+
// about to create the same account again, which will use a new ID, but will
|
|
99
|
+
// keep using the same address, and the Snap keyring does not allow this.
|
|
100
|
+
await __classPrivateFieldGet(this, _SnapAccountProvider_instances, "m", _SnapAccountProvider_withSnapKeyring).call(this, async ({ keyring }) => await keyring.removeAccount(account.address));
|
|
101
|
+
// The Snap has no account in its state for this one, we re-create it.
|
|
102
|
+
await this.createAccounts({
|
|
103
|
+
entropySource,
|
|
108
104
|
groupIndex,
|
|
109
105
|
});
|
|
110
|
-
this.messenger.captureException?.(sentryError);
|
|
111
106
|
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
const sentryError = createSentryError(`Unable to re-sync account: ${groupIndex}`, error, {
|
|
110
|
+
provider: this.getName(),
|
|
111
|
+
groupIndex,
|
|
112
|
+
});
|
|
113
|
+
this.messenger.captureException?.(sentryError);
|
|
114
|
+
}
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
122
117
|
}
|
|
123
118
|
}
|
|
124
|
-
|
|
125
|
-
// NOTE: We're not supposed to make the keyring instance escape `withKeyring` but
|
|
126
|
-
// we have to use the `SnapKeyring` instance to be able to create Solana account
|
|
127
|
-
// without triggering UI confirmation.
|
|
128
|
-
// Also, creating account that way won't invalidate the Snap keyring state. The
|
|
129
|
-
// account will get created and persisted properly with the Snap account creation
|
|
130
|
-
// flow "asynchronously" (with `notify:accountCreated`).
|
|
131
|
-
const createAccount = await __classPrivateFieldGet(this, _SnapAccountProvider_instances, "m", _SnapAccountProvider_withSnapKeyring).call(this, async ({ keyring }) => keyring.createAccount.bind(keyring));
|
|
132
|
-
return {
|
|
133
|
-
createAccount: async (options) =>
|
|
134
|
-
// We use the "unguarded" account creation here (see explanation above).
|
|
135
|
-
await createAccount(this.snapId, options, {
|
|
136
|
-
displayAccountNameSuggestion: false,
|
|
137
|
-
displayConfirmation: false,
|
|
138
|
-
setSelectedAccount: false,
|
|
139
|
-
}),
|
|
140
|
-
removeAccount: async (address) =>
|
|
141
|
-
// Though, when removing account, we can use the normal flow.
|
|
142
|
-
await __classPrivateFieldGet(this, _SnapAccountProvider_instances, "m", _SnapAccountProvider_withSnapKeyring).call(this, async ({ keyring }) => {
|
|
143
|
-
await keyring.removeAccount(address);
|
|
144
|
-
}),
|
|
145
|
-
};
|
|
146
|
-
}, _SnapAccountProvider_getKeyringClientFromSnapId = function _SnapAccountProvider_getKeyringClientFromSnapId(snapId) {
|
|
119
|
+
_SnapAccountProvider_queue = new WeakMap(), _SnapAccountProvider_trace = new WeakMap(), _SnapAccountProvider_instances = new WeakSet(), _SnapAccountProvider_getKeyringClientFromSnapId = function _SnapAccountProvider_getKeyringClientFromSnapId(snapId) {
|
|
147
120
|
return new KeyringClient({
|
|
148
121
|
send: async (request) => {
|
|
149
122
|
const response = await this.messenger.call('SnapController:handleRequest', {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnapAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/SnapAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAKA,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,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,OAAO,EAAE,aAAa,EAAE,+BAAqB;AAE7C,OAAO,EAAE,iBAAiB,EAAE,qBAAiB;AAoB7C,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,IAAI,EAA6B,MAAM,CAAC,MAAA,CAAC;QAExD,MAAM,cAAc,GAAG,MAAM,CAAC,cAAc,IAAI,QAAQ,CAAC;QACzD,IAAI,CAAC,MAAM,GAAG;YACZ,GAAG,MAAM;YACT,SAAS,EAAE;gBACT,GAAG,MAAM,CAAC,SAAS;gBACnB,OAAO,EAAE,MAAM,CAAC,SAAS,CAAC,OAAO,IAAI,IAAI;aAC1C;YACD,cAAc;SACf,CAA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AE;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,yCAwED,KAAK,+CACH,SAM6B;IAE7B,OAAO,IAAI,CAAC,WAAW,CACrB,EAAE,IAAI,EAAE,YAAY,CAAC,IAAI,EAAE,EAC3B,CAAC,IAAI,EAAE,EAAE;QACP,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC,CACF,CAAC;AACJ,CAAC;AA6BH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CACnC,QAAiB,EACgB,EAAE;IACnC,OAAO,QAAQ,YAAY,mBAAmB,CAAC;AACjD,CAAC,CAAC","sourcesContent":["import type { Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback, TraceRequest } from '@metamask/controller-utils';\nimport type { SnapKeyring } from '@metamask/eth-snap-keyring';\nimport type { EntropySourceId, KeyringAccount } 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 { BaseBip44AccountProvider } from './BaseBip44AccountProvider';\nimport { traceFallback } from '../analytics';\nimport type { MultichainAccountServiceMessenger } from '../types';\nimport { createSentryError } from '../utils';\n\nexport type RestrictedSnapKeyring = {\n createAccount: (options: Record<string, Json>) => 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 timeoutMs: number;\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 platform is ready to be used.\n *\n * @returns A promise that resolves when the platform is ready.\n * @throws An error if the platform is not ready (only effective once the platform has been ready at least once).\n */\n async ensureCanUseSnapPlatform(): Promise<void> {\n return this.messenger.call(\n 'MultichainAccountService:ensureCanUseSnapPlatform',\n );\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 = await this.#withSnapKeyring<\n SnapKeyring['createAccount']\n >(async ({ keyring }) => keyring.createAccount.bind(keyring));\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 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) =>\n account.metadata.snap && 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 we want to report that kind of errors still\n // in case states are de-sync.\n if (localSnapAccounts.length < snapAccounts.size) {\n this.messenger.captureException?.(\n new Error(\n `Snap \"${this.snapId}\" has de-synced accounts, Snap has more accounts than MetaMask!`,\n ),\n );\n\n // We don't recover from this case yet.\n return;\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 // Accounts should never really be de-synced, so we want to log this to see how often this\n // happens, cause that means that something else is buggy elsewhere...\n this.messenger.captureException?.(\n new Error(\n `Snap \"${this.snapId}\" has de-synced accounts, we'll attempt to re-sync them...`,\n ),\n );\n\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 entropySource,\n groupIndex,\n });\n }\n } catch (error) {\n const sentryError = createSentryError(\n `Unable to re-sync account: ${groupIndex}`,\n error as Error,\n {\n provider: this.getName(),\n groupIndex,\n },\n );\n this.messenger.captureException?.(sentryError);\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.ensureCanUseSnapPlatform();\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 abstract createAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]>;\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":";;;;;;;;;;;;AAKA,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,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,OAAO,EAAE,aAAa,EAAE,+BAAqB;AAE7C,OAAO,EAAE,iBAAiB,EAAE,qBAAiB;AAmB7C,MAAM,OAAgB,mBAAoB,SAAQ,wBAAwB;IAWxE,YACE,MAAc,EACd,SAA4C,EAC5C,MAAiC;IACjC,0BAA0B;IAC1B,QAAuB,aAAa;QAEpC,KAAK,CAAC,SAAS,CAAC,CAAC;;QAXV,6CAAmB;QAEnB,6CAAsB;QAW7B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,uBAAA,IAAI,uFAA4B,MAAhC,IAAI,EAA6B,MAAM,CAAC,CAAC;QAEvD,MAAM,cAAc,GAAG,MAAM,CAAC,cAAc,IAAI,QAAQ,CAAC;QACzD,IAAI,CAAC,MAAM,GAAG;YACZ,GAAG,MAAM;YACT,SAAS,EAAE;gBACT,GAAG,MAAM,CAAC,SAAS;gBACnB,OAAO,EAAE,MAAM,CAAC,SAAS,CAAC,OAAO,IAAI,IAAI;aAC1C;YACD,cAAc;SACf,CAAC;QAEF,kDAAkD;QAClD,IAAI,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC;YAC7B,uBAAA,IAAI,8BAAU,IAAI,SAAS,CAAC,cAAc,CAAC,MAAA,CAAC;QAC9C,CAAC;QAED,uBAAA,IAAI,8BAAU,KAAK,MAAA,CAAC;IACtB,CAAC;IAED;;;;;;;OAOG;IACO,KAAK,CAAC,kBAAk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AAE,EAC1C,KAAc,EACd;wBACE,QAAQ,EAAE,IAAI,CAAC,OAAO,EAAE;wBACxB,UAAU;qBACX,CACF,CAAC;oBACF,IAAI,CAAC,SAAS,CAAC,gBAAgB,EAAE,CAAC,WAAW,CAAC,CAAC;gBACjD,CAAC;YACH,CAAC,CAAC,CACH,CAAC;QACJ,CAAC;IACH,CAAC;CA8BF;mPArH6B,MAAc;IACxC,OAAO,IAAI,aAAa,CAAC;QACvB,IAAI,EAAE,KAAK,EAAE,OAAuB,EAAE,EAAE;YACtC,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,yCA0ED,KAAK,+CACH,SAM6B;IAE7B,OAAO,IAAI,CAAC,WAAW,CACrB,EAAE,IAAI,EAAE,YAAY,CAAC,IAAI,EAAE,EAC3B,CAAC,IAAI,EAAE,EAAE;QACP,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC,CACF,CAAC;AACJ,CAAC;AAeH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CACnC,QAAiB,EACgB,EAAE;IACnC,OAAO,QAAQ,YAAY,mBAAmB,CAAC;AACjD,CAAC,CAAC","sourcesContent":["import type { Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback, TraceRequest } from '@metamask/controller-utils';\nimport type { SnapKeyring } from '@metamask/eth-snap-keyring';\nimport type { EntropySourceId, KeyringAccount } 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 { BaseBip44AccountProvider } from './BaseBip44AccountProvider';\nimport { traceFallback } from '../analytics';\nimport type { MultichainAccountServiceMessenger } from '../types';\nimport { createSentryError } from '../utils';\n\nexport type RestrictedSnapKeyringCreateAccount = (\n options: Record<string, Json>,\n) => Promise<KeyringAccount>;\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 timeoutMs: number;\n };\n};\n\nexport abstract class SnapAccountProvider extends BaseBip44AccountProvider {\n readonly snapId: SnapId;\n\n protected readonly config: SnapAccountProviderConfig;\n\n protected 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 * 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<T>(\n operation: () => Promise<T>,\n ): Promise<T> {\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 protected async getRestrictedSnapAccountCreator(): Promise<RestrictedSnapKeyringCreateAccount> {\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 = await this.#withSnapKeyring<\n SnapKeyring['createAccount']\n >(async ({ keyring }) => keyring.createAccount.bind(keyring));\n\n return (options) =>\n createAccount(this.snapId, options, {\n displayAccountNameSuggestion: false,\n displayConfirmation: false,\n setSelectedAccount: false,\n });\n }\n\n #getKeyringClientFromSnapId(snapId: string): KeyringClient {\n return new KeyringClient({\n send: async (request: JsonRpcRequest) => {\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 const localSnapAccounts = accounts.filter(\n (account) =>\n account.metadata.snap && 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 we want to report that kind of errors still\n // in case states are de-sync.\n if (localSnapAccounts.length < snapAccounts.size) {\n this.messenger.captureException?.(\n new Error(\n `Snap \"${this.snapId}\" has de-synced accounts, Snap has more accounts than MetaMask!`,\n ),\n );\n\n // We don't recover from this case yet.\n return;\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 // Accounts should never really be de-synced, so we want to log this to see how often this\n // happens, cause that means that something else is buggy elsewhere...\n this.messenger.captureException?.(\n new Error(\n `Snap \"${this.snapId}\" has de-synced accounts, we'll attempt to re-sync them...`,\n ),\n );\n\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 this.#withSnapKeyring(\n async ({ keyring }) =>\n await keyring.removeAccount(account.address),\n );\n\n // The Snap has no account in its state for this one, we re-create it.\n await this.createAccounts({\n entropySource,\n groupIndex,\n });\n }\n } catch (error) {\n const sentryError = createSentryError(\n `Unable to re-sync account: ${groupIndex}`,\n error as Error,\n {\n provider: this.getName(),\n groupIndex,\n },\n );\n this.messenger.captureException?.(sentryError);\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 abstract isAccountCompatible(account: Bip44Account<InternalAccount>): boolean;\n\n abstract createAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]>;\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"]}
|
|
@@ -41,51 +41,46 @@ class SolAccountProvider extends SnapAccountProvider_1.SnapAccountProvider {
|
|
|
41
41
|
account.metadata.keyring.type === keyring_controller_1.KeyringTypes.snap);
|
|
42
42
|
}
|
|
43
43
|
async createAccounts({ entropySource, groupIndex, }) {
|
|
44
|
-
return this.
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
groupIndex,
|
|
51
|
-
derivationPath,
|
|
52
|
-
});
|
|
53
|
-
return [account];
|
|
44
|
+
return this.withMaxConcurrency(async () => {
|
|
45
|
+
const derivationPath = `m/44'/501'/${groupIndex}'/0'`;
|
|
46
|
+
const account = await __classPrivateFieldGet(this, _SolAccountProvider_instances, "m", _SolAccountProvider_createAccount).call(this, {
|
|
47
|
+
entropySource,
|
|
48
|
+
groupIndex,
|
|
49
|
+
derivationPath,
|
|
54
50
|
});
|
|
51
|
+
return [account];
|
|
55
52
|
});
|
|
56
53
|
}
|
|
57
54
|
async discoverAccounts({ entropySource, groupIndex, }) {
|
|
58
|
-
return
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
backOffMs: this.config.discovery.backOffMs,
|
|
71
|
-
});
|
|
72
|
-
if (!discoveredAccounts.length) {
|
|
73
|
-
return [];
|
|
74
|
-
}
|
|
75
|
-
const createdAccounts = await Promise.all(discoveredAccounts.map((d) => __classPrivateFieldGet(this, _SolAccountProvider_instances, "m", _SolAccountProvider_createAccount).call(this, {
|
|
76
|
-
keyring,
|
|
77
|
-
entropySource,
|
|
78
|
-
groupIndex,
|
|
79
|
-
derivationPath: d.derivationPath,
|
|
80
|
-
})));
|
|
81
|
-
return createdAccounts;
|
|
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)(this.client.discoverAccounts([keyring_api_1.SolScope.Mainnet], entropySource, groupIndex), this.config.discovery.timeoutMs), {
|
|
65
|
+
maxAttempts: this.config.discovery.maxAttempts,
|
|
66
|
+
backOffMs: this.config.discovery.backOffMs,
|
|
82
67
|
});
|
|
68
|
+
if (!discoveredAccounts.length) {
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
const createdAccounts = await Promise.all(discoveredAccounts.map((d) => __classPrivateFieldGet(this, _SolAccountProvider_instances, "m", _SolAccountProvider_createAccount).call(this, {
|
|
72
|
+
entropySource,
|
|
73
|
+
groupIndex,
|
|
74
|
+
derivationPath: d.derivationPath,
|
|
75
|
+
})));
|
|
76
|
+
return createdAccounts;
|
|
83
77
|
});
|
|
84
78
|
}
|
|
85
79
|
}
|
|
86
80
|
exports.SolAccountProvider = SolAccountProvider;
|
|
87
|
-
_SolAccountProvider_instances = new WeakSet(), _SolAccountProvider_createAccount = async function _SolAccountProvider_createAccount({
|
|
88
|
-
const
|
|
81
|
+
_SolAccountProvider_instances = new WeakSet(), _SolAccountProvider_createAccount = async function _SolAccountProvider_createAccount({ entropySource, groupIndex, derivationPath, }) {
|
|
82
|
+
const createAccount = await this.getRestrictedSnapAccountCreator();
|
|
83
|
+
const account = await (0, utils_1.withTimeout)(createAccount({ entropySource, derivationPath }), this.config.createAccounts.timeoutMs);
|
|
89
84
|
// Ensure entropy is present before type assertion validation
|
|
90
85
|
account.options.entropy = {
|
|
91
86
|
type: keyring_api_2.KeyringAccountEntropyTypeOption.Mnemonic,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SolAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/SolAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;AAAA,uDAA6D;AAI7D,uDAAiD;AACjD,uDAG+B;AAC/B,qEAA4D;AAI5D,mEAA4D;
|
|
1
|
+
{"version":3,"file":"SolAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/SolAccountProvider.ts"],"names":[],"mappings":";;;;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AAM,OAAO,GAAG,MAAM,IAAA,mBAAW,EAC/B,aAAa,CAAC,EAAE,aAAa,EAAE,cAAc,EAAE,CAAC,EAChD,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,SAAS,CACrC,CAAC;IAEF,6DAA6D;IAC7D,OAAO,CAAC,OAAO,CAAC,OAAO,GAAG;QACxB,IAAI,EAAE,6CAA+B,CAAC,QAAQ;QAC9C,EAAE,EAAE,aAAa;QACjB,UAAU;QACV,cAAc;KACf,CAAC;IAEF,IAAA,kCAAoB,EAAC,OAAO,CAAC,CAAC;IAC9B,OAAO,OAAO,CAAC;AACjB,CAAC;AAhDM,uBAAI,GAAG,iCAAyB,AAA5B,CAA6B;AAEjC,iCAAc,GAAG,kCAA4C,AAA/C,CAAgD","sourcesContent":["import { assertIsBip44Account } from '@metamask/account-api';\nimport type { Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { SolScope } from '@metamask/keyring-api';\nimport {\n KeyringAccountEntropyTypeOption,\n SolAccountType,\n} from '@metamask/keyring-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { SnapId } from '@metamask/snaps-sdk';\n\nimport { SnapAccountProvider } from './SnapAccountProvider';\nimport type { SnapAccountProviderConfig } from './SnapAccountProvider';\nimport { withRetry, withTimeout } from './utils';\nimport { traceFallback } from '../analytics';\nimport { TraceName } from '../constants/traces';\nimport type { MultichainAccountServiceMessenger } from '../types';\n\nexport type SolAccountProviderConfig = SnapAccountProviderConfig;\n\nexport const SOL_ACCOUNT_PROVIDER_NAME = 'Solana';\n\nexport const SOL_ACCOUNT_PROVIDER_DEFAULT_CONFIG: SnapAccountProviderConfig = {\n maxConcurrency: 3,\n discovery: {\n enabled: true,\n timeoutMs: 2000,\n maxAttempts: 3,\n backOffMs: 1000,\n },\n createAccounts: {\n timeoutMs: 3000,\n },\n};\n\nexport class SolAccountProvider extends SnapAccountProvider {\n static NAME = SOL_ACCOUNT_PROVIDER_NAME;\n\n static SOLANA_SNAP_ID = 'npm:@metamask/solana-wallet-snap' as SnapId;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n config: SolAccountProviderConfig = SOL_ACCOUNT_PROVIDER_DEFAULT_CONFIG,\n trace: TraceCallback = traceFallback,\n ) {\n super(SolAccountProvider.SOLANA_SNAP_ID, messenger, config, trace);\n }\n\n getName(): string {\n return SolAccountProvider.NAME;\n }\n\n isAccountCompatible(account: Bip44Account<InternalAccount>): boolean {\n return (\n account.type === SolAccountType.DataAccount &&\n account.metadata.keyring.type === (KeyringTypes.snap as string)\n );\n }\n\n async #createAccount({\n entropySource,\n groupIndex,\n derivationPath,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n derivationPath: string;\n }): Promise<Bip44Account<KeyringAccount>> {\n const createAccount = await this.getRestrictedSnapAccountCreator();\n const account = await withTimeout(\n createAccount({ entropySource, derivationPath }),\n this.config.createAccounts.timeoutMs,\n );\n\n // Ensure entropy is present before type assertion validation\n account.options.entropy = {\n type: KeyringAccountEntropyTypeOption.Mnemonic,\n id: entropySource,\n groupIndex,\n derivationPath,\n };\n\n assertIsBip44Account(account);\n return account;\n }\n\n async createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withMaxConcurrency(async () => {\n const derivationPath = `m/44'/501'/${groupIndex}'/0'`;\n const account = await this.#createAccount({\n entropySource,\n groupIndex,\n derivationPath,\n });\n\n return [account];\n });\n }\n\n async discoverAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return await super.trace(\n {\n name: TraceName.SnapDiscoverAccounts,\n data: {\n provider: this.getName(),\n },\n },\n async () => {\n if (!this.config.discovery.enabled) {\n return [];\n }\n\n const discoveredAccounts = await withRetry(\n () =>\n withTimeout(\n this.client.discoverAccounts(\n [SolScope.Mainnet],\n entropySource,\n groupIndex,\n ),\n this.config.discovery.timeoutMs,\n ),\n {\n maxAttempts: this.config.discovery.maxAttempts,\n backOffMs: this.config.discovery.backOffMs,\n },\n );\n\n if (!discoveredAccounts.length) {\n return [];\n }\n\n const createdAccounts = await Promise.all(\n discoveredAccounts.map((d) =>\n this.#createAccount({\n entropySource,\n groupIndex,\n derivationPath: d.derivationPath,\n }),\n ),\n );\n\n return createdAccounts;\n },\n );\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SolAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/SolAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAO7E,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AACtE,OAAO,KAAK,EAAE,MAAM,EAAE,4BAA4B;AAElD,OAAO,EAAE,mBAAmB,EAAE,kCAA8B;AAC5D,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"SolAccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/SolAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAO7E,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AACtE,OAAO,KAAK,EAAE,MAAM,EAAE,4BAA4B;AAElD,OAAO,EAAE,mBAAmB,EAAE,kCAA8B;AAC5D,OAAO,KAAK,EAAE,yBAAyB,EAAE,kCAA8B;AAIvE,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE,MAAM,MAAM,wBAAwB,GAAG,yBAAyB,CAAC;AAEjE,eAAO,MAAM,yBAAyB,WAAW,CAAC;AAElD,eAAO,MAAM,mCAAmC,EAAE,yBAWjD,CAAC;AAEF,qBAAa,kBAAmB,SAAQ,mBAAmB;;IACzD,MAAM,CAAC,IAAI,SAA6B;IAExC,MAAM,CAAC,cAAc,SAAgD;gBAGnE,SAAS,EAAE,iCAAiC,EAC5C,MAAM,GAAE,wBAA8D,EACtE,KAAK,GAAE,aAA6B;IAKtC,OAAO,IAAI,MAAM;IAIjB,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,eAAe,CAAC,GAAG,OAAO;IAkC9D,cAAc,CAAC,EACnB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;IAarC,gBAAgB,CAAC,EACrB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;CA+C5C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SolAccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/SolAccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC;AAChE,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAO7E,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AACtE,OAAO,KAAK,EAAE,MAAM,EAAE,4BAA4B;AAElD,OAAO,EAAE,mBAAmB,EAAE,kCAA8B;AAC5D,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"SolAccountProvider.d.mts","sourceRo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apAccountProvider {
|
|
|
38
38
|
account.metadata.keyring.type === KeyringTypes.snap);
|
|
39
39
|
}
|
|
40
40
|
async createAccounts({ entropySource, groupIndex, }) {
|
|
41
|
-
return this.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
groupIndex,
|
|
48
|
-
derivationPath,
|
|
49
|
-
});
|
|
50
|
-
return [account];
|
|
41
|
+
return this.withMaxConcurrency(async () => {
|
|
42
|
+
const derivationPath = `m/44'/501'/${groupIndex}'/0'`;
|
|
43
|
+
const account = await __classPrivateFieldGet(this, _SolAccountProvider_instances, "m", _SolAccountProvider_createAccount).call(this, {
|
|
44
|
+
entropySource,
|
|
45
|
+
groupIndex,
|
|
46
|
+
derivationPath,
|
|
51
47
|
});
|
|
48
|
+
return [account];
|
|
52
49
|
});
|
|
53
50
|
}
|
|
54
51
|
async discoverAccounts({ entropySource, groupIndex, }) {
|
|
55
|
-
return
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
backOffMs: this.config.discovery.backOffMs,
|
|
68
|
-
});
|
|
69
|
-
if (!discoveredAccounts.length) {
|
|
70
|
-
return [];
|
|
71
|
-
}
|
|
72
|
-
const createdAccounts = await Promise.all(discoveredAccounts.map((d) => __classPrivateFieldGet(this, _SolAccountProvider_instances, "m", _SolAccountProvider_createAccount).call(this, {
|
|
73
|
-
keyring,
|
|
74
|
-
entropySource,
|
|
75
|
-
groupIndex,
|
|
76
|
-
derivationPath: d.derivationPath,
|
|
77
|
-
})));
|
|
78
|
-
return createdAccounts;
|
|
52
|
+
return await super.trace({
|
|
53
|
+
name: TraceName.SnapDiscoverAccounts,
|
|
54
|
+
data: {
|
|
55
|
+
provider: this.getName(),
|
|
56
|
+
},
|
|
57
|
+
}, async () => {
|
|
58
|
+
if (!this.config.discovery.enabled) {
|
|
59
|
+
return [];
|
|
60
|
+
}
|
|
61
|
+
const discoveredAccounts = await withRetry(() => withTimeout(this.client.discoverAccounts([SolScope.Mainnet], entropySource, groupIndex), this.config.discovery.timeoutMs), {
|
|
62
|
+
maxAttempts: this.config.discovery.maxAttempts,
|
|
63
|
+
backOffMs: this.config.discovery.backOffMs,
|
|
79
64
|
});
|
|
65
|
+
if (!discoveredAccounts.length) {
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
const createdAccounts = await Promise.all(discoveredAccounts.map((d) => __classPrivateFieldGet(this, _SolAccountProvider_instances, "m", _SolAccountProvider_createAccount).call(this, {
|
|
69
|
+
entropySource,
|
|
70
|
+
groupIndex,
|
|
71
|
+
derivationPath: d.derivationPath,
|
|
72
|
+
})));
|
|
73
|
+
return createdAccounts;
|
|
80
74
|
});
|
|
81
75
|
}
|
|
82
76
|
}
|
|
83
|
-
_SolAccountProvider_instances = new WeakSet(), _SolAccountProvider_createAccount = async function _SolAccountProvider_createAccount({
|
|
84
|
-
const
|
|
77
|
+
_SolAccountProvider_instances = new WeakSet(), _SolAccountProvider_createAccount = async function _SolAccountProvider_createAccount({ entropySource, groupIndex, derivationPath, }) {
|
|
78
|
+
const createAccount = await this.getRestrictedSnapAccountCreator();
|
|
79
|
+
const account = await withTimeout(createAccount({ entropySource, derivationPath }), this.config.createAccounts.timeoutMs);
|
|
85
80
|
// Ensure entropy is present before type assertion validation
|
|
86
81
|
account.options.entropy = {
|
|
87
82
|
type: KeyringAccountEntropyTypeOption.Mnemonic,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SolAccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/SolAccountProvider.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,oBAAoB,EAAE,8BAA8B;AAI7D,OAAO,EAAE,QAAQ,EAAE,8BAA8B;AACjD,OAAO,EACL,+BAA+B,EAC/B,cAAc,EACf,8BAA8B;AAC/B,OAA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vC,kBAAkB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAC3B,uBAAA,IAAI,wEAAe,MAAnB,IAAI,EAAgB;gBAClB,aAAa;gBACb,UAAU;gBACV,cAAc,EAAE,CAAC,CAAC,cAAc;aACjC,CAAC,CACH,CACF,CAAC;YAEF,OAAO,eAAe,CAAC;QACzB,CAAC,CACF,CAAC;IACJ,CAAC;;mFAlGD,KAAK,4CAAgB,EACnB,aAAa,EACb,UAAU,EACV,cAAc,GAKf;IACC,MAAM,aAAa,GAAG,MAAM,IAAI,CAAC,+BAA+B,EAAE,CAAC;IACnE,MAAM,OAAO,GAAG,MAAM,WAAW,CAC/B,aAAa,CAAC,EAAE,aAAa,EAAE,cAAc,EAAE,CAAC,EAChD,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,SAAS,CACrC,CAAC;IAEF,6DAA6D;IAC7D,OAAO,CAAC,OAAO,CAAC,OAAO,GAAG;QACxB,IAAI,EAAE,+BAA+B,CAAC,QAAQ;QAC9C,EAAE,EAAE,aAAa;QACjB,UAAU;QACV,cAAc;KACf,CAAC;IAEF,oBAAoB,CAAC,OAAO,CAAC,CAAC;IAC9B,OAAO,OAAO,CAAC;AACjB,CAAC;AAhDM,uBAAI,GAAG,yBAAyB,AAA5B,CAA6B;AAEjC,iCAAc,GAAG,kCAA4C,AAA/C,CAAgD","sourcesContent":["import { assertIsBip44Account } from '@metamask/account-api';\nimport type { Bip44Account } from '@metamask/account-api';\nimport type { TraceCallback } from '@metamask/controller-utils';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { SolScope } from '@metamask/keyring-api';\nimport {\n KeyringAccountEntropyTypeOption,\n SolAccountType,\n} from '@metamask/keyring-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { SnapId } from '@metamask/snaps-sdk';\n\nimport { SnapAccountProvider } from './SnapAccountProvider';\nimport type { SnapAccountProviderConfig } from './SnapAccountProvider';\nimport { withRetry, withTimeout } from './utils';\nimport { traceFallback } from '../analytics';\nimport { TraceName } from '../constants/traces';\nimport type { MultichainAccountServiceMessenger } from '../types';\n\nexport type SolAccountProviderConfig = SnapAccountProviderConfig;\n\nexport const SOL_ACCOUNT_PROVIDER_NAME = 'Solana';\n\nexport const SOL_ACCOUNT_PROVIDER_DEFAULT_CONFIG: SnapAccountProviderConfig = {\n maxConcurrency: 3,\n discovery: {\n enabled: true,\n timeoutMs: 2000,\n maxAttempts: 3,\n backOffMs: 1000,\n },\n createAccounts: {\n timeoutMs: 3000,\n },\n};\n\nexport class SolAccountProvider extends SnapAccountProvider {\n static NAME = SOL_ACCOUNT_PROVIDER_NAME;\n\n static SOLANA_SNAP_ID = 'npm:@metamask/solana-wallet-snap' as SnapId;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n config: SolAccountProviderConfig = SOL_ACCOUNT_PROVIDER_DEFAULT_CONFIG,\n trace: TraceCallback = traceFallback,\n ) {\n super(SolAccountProvider.SOLANA_SNAP_ID, messenger, config, trace);\n }\n\n getName(): string {\n return SolAccountProvider.NAME;\n }\n\n isAccountCompatible(account: Bip44Account<InternalAccount>): boolean {\n return (\n account.type === SolAccountType.DataAccount &&\n account.metadata.keyring.type === (KeyringTypes.snap as string)\n );\n }\n\n async #createAccount({\n entropySource,\n groupIndex,\n derivationPath,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n derivationPath: string;\n }): Promise<Bip44Account<KeyringAccount>> {\n const createAccount = await this.getRestrictedSnapAccountCreator();\n const account = await withTimeout(\n createAccount({ entropySource, derivationPath }),\n this.config.createAccounts.timeoutMs,\n );\n\n // Ensure entropy is present before type assertion validation\n account.options.entropy = {\n type: KeyringAccountEntropyTypeOption.Mnemonic,\n id: entropySource,\n groupIndex,\n derivationPath,\n };\n\n assertIsBip44Account(account);\n return account;\n }\n\n async createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return this.withMaxConcurrency(async () => {\n const derivationPath = `m/44'/501'/${groupIndex}'/0'`;\n const account = await this.#createAccount({\n entropySource,\n groupIndex,\n derivationPath,\n });\n\n return [account];\n });\n }\n\n async discoverAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n return await super.trace(\n {\n name: TraceName.SnapDiscoverAccounts,\n data: {\n provider: this.getName(),\n },\n },\n async () => {\n if (!this.config.discovery.enabled) {\n return [];\n }\n\n const discoveredAccounts = await withRetry(\n () =>\n withTimeout(\n this.client.discoverAccounts(\n [SolScope.Mainnet],\n entropySource,\n groupIndex,\n ),\n this.config.discovery.timeoutMs,\n ),\n {\n maxAttempts: this.config.discovery.maxAttempts,\n backOffMs: this.config.discovery.backOffMs,\n },\n );\n\n if (!discoveredAccounts.length) {\n return [];\n }\n\n const createdAccounts = await Promise.all(\n discoveredAccounts.map((d) =>\n this.#createAccount({\n entropySource,\n groupIndex,\n derivationPath: d.derivationPath,\n }),\n ),\n );\n\n return createdAccounts;\n },\n );\n }\n}\n"]}
|