@metamask-previews/profile-metrics-controller 3.2.0-preview-eca6f2051 → 3.2.0-preview-6da9f2b

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 CHANGED
@@ -9,11 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  ### Added
11
11
 
12
- - Add `ProofOfOwnershipService` for signing chain-native proofs of account ownership ([#9016](https://github.com/MetaMask/core/pull/9016))
13
- - Exposes `ProofOfOwnershipService:sign({ account, nonce })`, dispatching by the CAIP-2 namespace of the account's first scope.
14
- - EVM accounts are signed via `KeyringController:signPersonalMessage` (EIP-191); Solana, Tron, and Bitcoin accounts are signed via `SnapController:handleRequest` with the `onClientRequest` handler against the snap declared in `account.metadata.snap.id`, which keeps the request silent and client-internal.
15
- - The non-EVM snaps are expected to implement a `signProofOfOwnership` JSON-RPC method that validates the message prefix `metamask:proof-of-ownership:` before signing.
16
- - Add `profileMetricsServiceName` alias for the existing `serviceName` export, to disambiguate it from the new `proofOfOwnershipServiceName`. The original `serviceName` export is unchanged.
12
+ - **BREAKING:** Add chain-native proof-of-ownership signing for accounts, with profile-metric submissions now carrying a proof per account ([#9016](https://github.com/MetaMask/core/pull/9016), [#9190](https://github.com/MetaMask/core/pull/9190))
13
+ - New `ProofOfOwnershipService:sign({ account, nonce })` action, dispatching to `KeyringController:signPersonalMessage` for EVM accounts and to the account's snap (via the `signProofOfOwnership` JSON-RPC method) for Solana, Tron, and Bitcoin.
14
+ - `ProfileMetricsController._executePoll` signs a proof for each queued account and submits it alongside the canonicalized address (EIP-55 for `eip155`, lowercase bech32 / bech32m for `bip122`). Consumers must delegate `ProofOfOwnershipService:sign` onto the controller's messenger.
15
+ - Adds a `profileMetricsServiceName` alias for the existing `serviceName` export to disambiguate it from the new `proofOfOwnershipServiceName`.
16
+ - Re-enqueues all known accounts on the first unlock after upgrading so previously-synced records get a proof attached, gated by a new `proofBackfillEnqueued` state flag (fresh installs flip the flag on their initial sync).
17
17
 
18
18
  ### Changed
19
19
 
@@ -10,12 +10,13 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _ProfileMetricsController_instances, _ProfileMetricsController_mutex, _ProfileMetricsController_assertUserOptedIn, _ProfileMetricsController_getMetaMetricsId, _ProfileMetricsController_initialDelayDuration, _ProfileMetricsController_queueFirstSyncIfNeeded, _ProfileMetricsController_setInitialDelayEndTimestampIfNull, _ProfileMetricsController_isInitialDelayComplete, _ProfileMetricsController_addAccountToQueue, _ProfileMetricsController_removeAccountFromQueue;
13
+ var _ProfileMetricsController_instances, _ProfileMetricsController_mutex, _ProfileMetricsController_assertUserOptedIn, _ProfileMetricsController_getMetaMetricsId, _ProfileMetricsController_initialDelayDuration, _ProfileMetricsController_attachProofs, _ProfileMetricsController_getFullAccountsByAddress, _ProfileMetricsController_enqueueAccountsIfNeeded, _ProfileMetricsController_setInitialDelayEndTimestampIfNull, _ProfileMetricsController_isInitialDelayComplete, _ProfileMetricsController_addAccountToQueue, _ProfileMetricsController_removeAccountFromQueue;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.ProfileMetricsController = exports.getDefaultProfileMetricsControllerState = exports.DEFAULT_INITIAL_DELAY_DURATION = exports.controllerName = void 0;
16
16
  const polling_controller_1 = require("@metamask/polling-controller");
17
17
  const utils_1 = require("@metamask/utils");
18
18
  const async_mutex_1 = require("async-mutex");
19
+ const canonicalize_1 = require("./utils/canonicalize.cjs");
19
20
  /**
20
21
  * The name of the {@link ProfileMetricsController}, used to namespace the
21
22
  * controller's actions and events and to namespace the controller's state data
@@ -48,6 +49,12 @@ const profileMetricsControllerMetadata = {
48
49
  includeInStateLogs: true,
49
50
  usedInUi: false,
50
51
  },
52
+ proofBackfillEnqueued: {
53
+ persist: true,
54
+ includeInDebugSnapshot: true,
55
+ includeInStateLogs: true,
56
+ usedInUi: false,
57
+ },
51
58
  };
52
59
  /**
53
60
  * Constructs the default {@link ProfileMetricsController} state. This allows
@@ -61,6 +68,7 @@ function getDefaultProfileMetricsControllerState() {
61
68
  return {
62
69
  initialEnqueueCompleted: false,
63
70
  syncQueue: {},
71
+ proofBackfillEnqueued: false,
64
72
  };
65
73
  }
66
74
  exports.getDefaultProfileMetricsControllerState = getDefaultProfileMetricsControllerState;
@@ -114,7 +122,7 @@ class ProfileMetricsController extends (0, polling_controller_1.StaticIntervalPo
114
122
  // it must have opted in during onboarding, or during a previous session.
115
123
  this.skipInitialDelay();
116
124
  }
117
- __classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_queueFirstSyncIfNeeded).call(this).catch(this.messenger.captureException ?? console.error);
125
+ __classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_enqueueAccountsIfNeeded).call(this).catch(this.messenger.captureException ?? console.error);
118
126
  this.startPolling(null);
119
127
  });
120
128
  this.messenger.subscribe('KeyringController:lock', () => this.stopAllPolling());
@@ -139,9 +147,10 @@ class ProfileMetricsController extends (0, polling_controller_1.StaticIntervalPo
139
147
  /**
140
148
  * Execute a single poll to sync user profile data.
141
149
  *
142
- * The queued accounts are sent to the ProfileMetricsService, and the sync
143
- * queue is cleared. This operation is mutexed to prevent concurrent
144
- * executions.
150
+ * The queued accounts are sent to the ProfileMetricsService, each with
151
+ * a proof of ownership when one can be produced (see {@link #attachProofs}),
152
+ * and the sync queue is cleared. This operation is mutexed to prevent
153
+ * concurrent executions.
145
154
  *
146
155
  * @returns A promise that resolves when the poll is complete.
147
156
  */
@@ -154,12 +163,15 @@ class ProfileMetricsController extends (0, polling_controller_1.StaticIntervalPo
154
163
  if (!__classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_isInitialDelayComplete).call(this)) {
155
164
  return;
156
165
  }
166
+ const fullAccountsByAddress = __classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_getFullAccountsByAddress).call(this);
157
167
  for (const [entropySourceId, accounts] of Object.entries(this.state.syncQueue)) {
168
+ const normalizedEntropySourceId = entropySourceId === 'null' ? null : entropySourceId;
169
+ const accountsWithProofs = await __classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_attachProofs).call(this, accounts, fullAccountsByAddress, normalizedEntropySourceId);
158
170
  try {
159
171
  await this.messenger.call('ProfileMetricsService:submitMetrics', {
160
172
  metametricsId: __classPrivateFieldGet(this, _ProfileMetricsController_getMetaMetricsId, "f").call(this),
161
- entropySourceId: entropySourceId === 'null' ? null : entropySourceId,
162
- accounts,
173
+ entropySourceId: normalizedEntropySourceId,
174
+ accounts: accountsWithProofs,
163
175
  });
164
176
  this.update((state) => {
165
177
  delete state.syncQueue[entropySourceId];
@@ -174,28 +186,130 @@ class ProfileMetricsController extends (0, polling_controller_1.StaticIntervalPo
174
186
  }
175
187
  }
176
188
  exports.ProfileMetricsController = ProfileMetricsController;
177
- _ProfileMetricsController_mutex = new WeakMap(), _ProfileMetricsController_assertUserOptedIn = new WeakMap(), _ProfileMetricsController_getMetaMetricsId = new WeakMap(), _ProfileMetricsController_initialDelayDuration = new WeakMap(), _ProfileMetricsController_instances = new WeakSet(), _ProfileMetricsController_queueFirstSyncIfNeeded =
189
+ _ProfileMetricsController_mutex = new WeakMap(), _ProfileMetricsController_assertUserOptedIn = new WeakMap(), _ProfileMetricsController_getMetaMetricsId = new WeakMap(), _ProfileMetricsController_initialDelayDuration = new WeakMap(), _ProfileMetricsController_instances = new WeakSet(), _ProfileMetricsController_attachProofs =
190
+ /**
191
+ * Attach a proof of ownership to each account in a single entropy-source
192
+ * batch when possible, canonicalizing the address along the way.
193
+ *
194
+ * Per-account failures (unknown namespace, snap missing the
195
+ * `signProofOfOwnership` method, snap rejection) and whole-batch nonce
196
+ * failures are caught and downgraded to "submit without a proof" so the
197
+ * batch still goes through and the proof is retried on the next poll.
198
+ *
199
+ * @param accounts - The queued accounts for a single batch.
200
+ * @param fullAccountsByAddress - Live `InternalAccount` lookup keyed by address.
201
+ * @param entropySourceId - The entropy source ID for this batch.
202
+ * @returns The accounts with `proof` populated where signing succeeded.
203
+ */
204
+ async function _ProfileMetricsController_attachProofs(accounts, fullAccountsByAddress, entropySourceId) {
205
+ const candidates = new Map();
206
+ const identifiers = new Set();
207
+ for (const queued of accounts) {
208
+ const fullAccount = fullAccountsByAddress.get(queued.address);
209
+ if (!fullAccount) {
210
+ continue;
211
+ }
212
+ try {
213
+ const [scope] = fullAccount.scopes;
214
+ if (!scope) {
215
+ throw new Error(`Scope not found for account ${fullAccount.id}`);
216
+ }
217
+ const { namespace } = (0, utils_1.parseCaipChainId)(scope);
218
+ const canonicalAddress = (0, canonicalize_1.canonicalizeAddress)(fullAccount.address, namespace);
219
+ candidates.set(queued.address, {
220
+ account: fullAccount,
221
+ canonicalAddress,
222
+ });
223
+ identifiers.add(canonicalAddress);
224
+ }
225
+ catch (error) {
226
+ // Unsupported namespaces are an expected pass-through; anything
227
+ // else is logged so a new namespace doesn't go unnoticed.
228
+ if (!(error instanceof canonicalize_1.ProofUnsupportedNamespaceError)) {
229
+ console.error(`Skipping proof for account ${fullAccount.id}:`, error);
230
+ }
231
+ }
232
+ }
233
+ if (candidates.size === 0) {
234
+ return accounts;
235
+ }
236
+ let nonces = {};
237
+ try {
238
+ nonces = await this.messenger.call('ProfileMetricsService:fetchNonces', {
239
+ identifiers: [...identifiers],
240
+ entropySourceId,
241
+ });
242
+ }
243
+ catch (error) {
244
+ console.error(`Failed to fetch proof-of-ownership nonces for entropy source ID ${entropySourceId ?? 'null'}:`, error);
245
+ }
246
+ return await Promise.all(accounts.map(async (queued) => {
247
+ const candidate = candidates.get(queued.address);
248
+ if (!candidate) {
249
+ return queued;
250
+ }
251
+ const nonce = nonces[candidate.canonicalAddress];
252
+ if (!nonce) {
253
+ return { ...queued, address: candidate.canonicalAddress };
254
+ }
255
+ let proof;
256
+ try {
257
+ proof = await this.messenger.call('ProofOfOwnershipService:sign', {
258
+ account: candidate.account,
259
+ nonce,
260
+ });
261
+ }
262
+ catch (error) {
263
+ console.error(`Failed to sign proof of ownership for account ${candidate.account.id}:`, error);
264
+ return { ...queued, address: candidate.canonicalAddress };
265
+ }
266
+ return {
267
+ address: candidate.canonicalAddress,
268
+ scopes: queued.scopes,
269
+ proof,
270
+ };
271
+ }));
272
+ }, _ProfileMetricsController_getFullAccountsByAddress = function _ProfileMetricsController_getFullAccountsByAddress() {
273
+ const byAddress = new Map();
274
+ const accountsState = this.messenger.call('AccountsController:getState');
275
+ for (const account of Object.values(accountsState.internalAccounts.accounts)) {
276
+ byAddress.set(account.address, account);
277
+ }
278
+ return byAddress;
279
+ }, _ProfileMetricsController_enqueueAccountsIfNeeded =
178
280
  /**
179
- * Add existing accounts to the sync queue if it has not been done yet.
281
+ * Enqueue all currently-known accounts onto the sync queue if needed.
282
+ * Single entry point covering both the fresh-install first sync and
283
+ * the one-time proof-of-ownership backfill for users upgrading.
180
284
  *
181
- * This method ensures that the first sync is only executed once,
182
- * and only if the user has opted in to user profile features.
285
+ * Bails for opted-out users (the poll wouldn't drain the queue
286
+ * anyway), and bails once both bootstrap steps have already run.
287
+ * Otherwise enqueues all known accounts and flips both flags so this
288
+ * becomes a permanent no-op for the lifetime of the install.
183
289
  */
184
- async function _ProfileMetricsController_queueFirstSyncIfNeeded() {
290
+ async function _ProfileMetricsController_enqueueAccountsIfNeeded() {
185
291
  await __classPrivateFieldGet(this, _ProfileMetricsController_mutex, "f").runExclusive(async () => {
186
- if (this.state.initialEnqueueCompleted) {
292
+ if (!__classPrivateFieldGet(this, _ProfileMetricsController_assertUserOptedIn, "f").call(this)) {
187
293
  return;
188
294
  }
189
- const newGroupedAccounts = groupAccountsByEntropySourceId(Object.values(this.messenger.call('AccountsController:getState').internalAccounts
295
+ if (this.state.initialEnqueueCompleted &&
296
+ this.state.proofBackfillEnqueued) {
297
+ return;
298
+ }
299
+ const groupedAccounts = groupAccountsByEntropySourceId(Object.values(this.messenger.call('AccountsController:getState').internalAccounts
190
300
  .accounts));
191
301
  this.update((state) => {
192
- for (const key of Object.keys(newGroupedAccounts)) {
193
- if (!state.syncQueue[key]) {
194
- state.syncQueue[key] = [];
195
- }
196
- state.syncQueue[key].push(...newGroupedAccounts[key]);
197
- }
302
+ // Replace the queue rather than append. `AccountsController` is
303
+ // the source of truth and the queue is otherwise kept in sync
304
+ // with it via the `accountAdded` / `accountRemoved` subscriptions,
305
+ // so assigning here avoids duplicating entries that survived from
306
+ // a prior session or were pushed earlier in this same unlock
307
+ // cycle. Duplicates would matter because nonces are single-use:
308
+ // letting one through causes `#attachProofs` to sign and submit
309
+ // twice with the same nonce.
310
+ state.syncQueue = groupedAccounts;
198
311
  state.initialEnqueueCompleted = true;
312
+ state.proofBackfillEnqueued = true;
199
313
  });
200
314
  });
201
315
  }, _ProfileMetricsController_setInitialDelayEndTimestampIfNull = function _ProfileMetricsController_setInitialDelayEndTimestampIfNull() {
@@ -1 +1 @@
1
- {"version":3,"file":"ProfileMetricsController.cjs","sourceRoot":"","sources":["../src/ProfileMetricsController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAgBA,qEAA+E;AAE/E,2CAA2D;AAC3D,6CAAoC;AAMpC;;;;GAIG;AACU,QAAA,cAAc,GAAG,0BAA0B,CAAC;AAEzD;;GAEG;AACU,QAAA,8BAA8B,GAAG,IAAA,sBAAc,EAC1D,CAAC,EACD,gBAAQ,CAAC,MAAM,CAChB,CAAC;AAwBF;;GAEG;AACH,MAAM,gCAAgC,GAAG;IACvC,uBAAuB,EAAE;QACvB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;IACD,SAAS,EAAE;QACT,OAAO,EAAE,I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E,IAAI,KAAK,UAAU,EAAE,CAAC;QACjD,OAAO,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;IACpC,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;GAMG;AACH,SAAS,8BAA8B,CACrC,QAA2B;IAE3B,OAAO,QAAQ,CAAC,MAAM,CACpB,CAAC,MAA2C,EAAE,OAAO,EAAE,EAAE;QACvD,MAAM,eAAe,GAAG,yBAAyB,CAAC,OAAO,CAAC,CAAC;QAC3D,MAAM,GAAG,GAAG,eAAe,IAAI,MAAM,CAAC;QACtC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;YACjB,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;QACnB,CAAC;QACD,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QACvE,OAAO,MAAM,CAAC;IAChB,CAAC,EACD,EAAE,CACH,CAAC;AACJ,CAAC","sourcesContent":["import type {\n AccountsControllerAccountAddedEvent,\n AccountsControllerAccountRemovedEvent,\n AccountsControllerGetStateAction,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport type {\n KeyringControllerLockEvent,\n KeyringControllerUnlockEvent,\n} from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Messenger } from '@metamask/messenger';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport { TransactionControllerTransactionSubmittedEvent } from '@metamask/transaction-controller';\nimport { Duration, inMilliseconds } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport type { ProfileMetricsControllerMethodActions } from './ProfileMetricsController-method-action-types';\nimport type { AccountWithScopes } from './ProfileMetricsService';\nimport type { ProfileMetricsServiceMethodActions } from './ProfileMetricsService-method-action-types';\n\n/**\n * The name of the {@link ProfileMetricsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'ProfileMetricsController';\n\n/**\n * The default delay duration before data is sent for the first time.\n */\nexport const DEFAULT_INITIAL_DELAY_DURATION = inMilliseconds(\n 1,\n Duration.Minute,\n);\n\n/**\n * Describes the shape of the state object for {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerState = {\n /**\n * Whether existing accounts have been added\n * to the queue.\n */\n initialEnqueueCompleted: boolean;\n /**\n * The queue of accounts to be synced.\n * Each key is an entropy source ID, and each value is an array of account\n * addresses associated with that entropy source. Accounts with no entropy\n * source ID are grouped under the key \"null\".\n */\n syncQueue: Record<string, AccountWithScopes[]>;\n /**\n * The timestamp when the first data sending can be attempted.\n */\n initialDelayEndTimestamp?: number;\n};\n\n/**\n * The metadata for each property in {@link ProfileMetricsControllerState}.\n */\nconst profileMetricsControllerMetadata = {\n initialEnqueueCompleted: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n syncQueue: {\n persist: true,\n includeInDebugSnapshot: false,\n includeInStateLogs: true,\n usedInUi: false,\n },\n initialDelayEndTimestamp: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<ProfileMetricsControllerState>;\n\n/**\n * Constructs the default {@link ProfileMetricsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link ProfileMetricsController} state.\n */\nexport function getDefaultProfileMetricsControllerState(): ProfileMetricsControllerState {\n return {\n initialEnqueueCompleted: false,\n syncQueue: {},\n };\n}\n\nconst MESSENGER_EXPOSED_METHODS = ['skipInitialDelay'] as const;\n\n/**\n * Retrieves the state of the {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n ProfileMetricsControllerState\n>;\n\n/**\n * Actions that {@link ProfileMetricsControllerMessenger} exposes to other consumers.\n */\nexport type ProfileMetricsControllerActions =\n | ProfileMetricsControllerGetStateAction\n | ProfileMetricsControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link ProfileMetricsControllerMessenger} calls.\n */\ntype AllowedActions =\n | ProfileMetricsServiceMethodActions\n | AccountsControllerGetStateAction;\n\n/**\n * Published when the state of {@link ProfileMetricsController} changes.\n */\nexport type ProfileMetricsControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n ProfileMetricsControllerState\n >;\n\n/**\n * Events that {@link ProfileMetricsControllerMessenger} exposes to other consumers.\n */\nexport type ProfileMetricsControllerEvents =\n ProfileMetricsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link ProfileMetricsControllerMessenger} subscribes\n * to.\n */\ntype AllowedEvents =\n | KeyringControllerUnlockEvent\n | KeyringControllerLockEvent\n | AccountsControllerAccountAddedEvent\n | AccountsControllerAccountRemovedEvent\n | TransactionControllerTransactionSubmittedEvent;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerMessenger = Messenger<\n typeof controllerName,\n ProfileMetricsControllerActions | AllowedActions,\n ProfileMetricsControllerEvents | AllowedEvents\n>;\n\n/**\n * Manages user profile metrics.\n *\n * For users who opt-in to metrics, this controller ensures we have metrics about their user\n * profile (metrics ID and accounts).\n */\nexport class ProfileMetricsController extends StaticIntervalPollingController()<\n typeof controllerName,\n ProfileMetricsControllerState,\n ProfileMetricsControllerMessenger\n> {\n readonly #mutex = new Mutex();\n\n readonly #assertUserOptedIn: () => boolean;\n\n readonly #getMetaMetricsId: () => string;\n\n readonly #initialDelayDuration: number;\n\n /**\n * Constructs a new {@link ProfileMetricsController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n * @param args.assertUserOptedIn - A function that asserts whether the user has\n * opted in to user profile features. If the user has not opted in, sync\n * operations will be no-ops.\n * @param args.getMetaMetricsId - A function that returns the MetaMetrics ID\n * of the user.\n * @param args.interval - The interval, in milliseconds, at which the controller will\n * attempt to send user profile data. Defaults to 10 seconds.\n * @param args.initialDelayDuration - The delay duration before data is sent\n * for the first time, in milliseconds. Defaults to 10 minutes.\n */\n constructor({\n messenger,\n state,\n assertUserOptedIn,\n getMetaMetricsId,\n interval = 10 * 1000,\n initialDelayDuration = DEFAULT_INITIAL_DELAY_DURATION,\n }: {\n messenger: ProfileMetricsControllerMessenger;\n state?: Partial<ProfileMetricsControllerState>;\n interval?: number;\n assertUserOptedIn: () => boolean;\n getMetaMetricsId: () => string;\n initialDelayDuration?: number;\n }) {\n super({\n messenger,\n metadata: profileMetricsControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultProfileMetricsControllerState(),\n ...state,\n },\n });\n\n this.#assertUserOptedIn = assertUserOptedIn;\n this.#getMetaMetricsId = getMetaMetricsId;\n this.#initialDelayDuration = initialDelayDuration;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.messenger.subscribe('KeyringController:unlock', () => {\n if (this.#assertUserOptedIn()) {\n // If the user has already opted in at the start of the session,\n // it must have opted in during onboarding, or during a previous session.\n this.skipInitialDelay();\n }\n this.#queueFirstSyncIfNeeded().catch(\n this.messenger.captureException ?? console.error,\n );\n this.startPolling(null);\n });\n\n this.messenger.subscribe('KeyringController:lock', () =>\n this.stopAllPolling(),\n );\n\n this.messenger.subscribe('TransactionController:transactionSubmitted', () =>\n this.skipInitialDelay(),\n );\n\n this.messenger.subscribe('AccountsController:accountAdded', (account) => {\n this.#addAccountToQueue(account).catch(console.error);\n });\n\n this.messenger.subscribe('AccountsController:accountRemoved', (account) => {\n this.#removeAccountFromQueue(account).catch(console.error);\n });\n\n this.setIntervalLength(interval);\n }\n\n /**\n * Skip the initial delay period by setting the end timestamp to the current time.\n * Metrics will be sent on the next poll.\n */\n skipInitialDelay(): void {\n this.update((state) => {\n state.initialDelayEndTimestamp = Date.now();\n });\n }\n\n /**\n * Execute a single poll to sync user profile data.\n *\n * The queued accounts are sent to the ProfileMetricsService, and the sync\n * queue is cleared. This operation is mutexed to prevent concurrent\n * executions.\n *\n * @returns A promise that resolves when the poll is complete.\n */\n async _executePoll(): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n if (!this.#assertUserOptedIn()) {\n return;\n }\n this.#setInitialDelayEndTimestampIfNull();\n if (!this.#isInitialDelayComplete()) {\n return;\n }\n for (const [entropySourceId, accounts] of Object.entries(\n this.state.syncQueue,\n )) {\n try {\n await this.messenger.call('ProfileMetricsService:submitMetrics', {\n metametricsId: this.#getMetaMetricsId(),\n entropySourceId:\n entropySourceId === 'null' ? null : entropySourceId,\n accounts,\n });\n this.update((state) => {\n delete state.syncQueue[entropySourceId];\n });\n } catch (error) {\n // We want to log the error but continue processing other batches.\n console.error(\n `Failed to submit profile metrics for entropy source ID ${entropySourceId}:`,\n error,\n );\n }\n }\n });\n }\n\n /**\n * Add existing accounts to the sync queue if it has not been done yet.\n *\n * This method ensures that the first sync is only executed once,\n * and only if the user has opted in to user profile features.\n */\n async #queueFirstSyncIfNeeded(): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n if (this.state.initialEnqueueCompleted) {\n return;\n }\n const newGroupedAccounts = groupAccountsByEntropySourceId(\n Object.values(\n this.messenger.call('AccountsController:getState').internalAccounts\n .accounts,\n ),\n );\n this.update((state) => {\n for (const key of Object.keys(newGroupedAccounts)) {\n if (!state.syncQueue[key]) {\n state.syncQueue[key] = [];\n }\n state.syncQueue[key].push(...newGroupedAccounts[key]);\n }\n state.initialEnqueueCompleted = true;\n });\n });\n }\n\n /**\n * Set the initial delay end timestamp if it is not already set.\n */\n #setInitialDelayEndTimestampIfNull(): void {\n this.update((state) => {\n state.initialDelayEndTimestamp ??=\n Date.now() + this.#initialDelayDuration;\n });\n }\n\n /**\n * Check if the initial delay end timestamp is in the past.\n *\n * @returns True if the initial delay period has completed, false otherwise.\n */\n #isInitialDelayComplete(): boolean {\n return (\n this.state.initialDelayEndTimestamp !== undefined &&\n Date.now() >= this.state.initialDelayEndTimestamp\n );\n }\n\n /**\n * Queue the given account to be synced at the next poll.\n *\n * @param account - The account to sync.\n */\n async #addAccountToQueue(account: InternalAccount): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n this.update((state) => {\n const entropySourceId = getAccountEntropySourceId(account) ?? 'null';\n if (!state.syncQueue[entropySourceId]) {\n state.syncQueue[entropySourceId] = [];\n }\n state.syncQueue[entropySourceId].push({\n address: account.address,\n scopes: account.scopes,\n });\n });\n });\n }\n\n /**\n * Remove the given account from the sync queue.\n *\n * @param account - The account address to remove.\n */\n async #removeAccountFromQueue(account: string): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n this.update((state) => {\n for (const [entropySourceId, groupedAddresses] of Object.entries(\n state.syncQueue,\n )) {\n const index = groupedAddresses.findIndex(\n ({ address }) => address === account,\n );\n if (index === -1) {\n continue;\n }\n groupedAddresses.splice(index, 1);\n if (groupedAddresses.length === 0) {\n delete state.syncQueue[entropySourceId];\n }\n break;\n }\n });\n });\n }\n}\n\n/**\n * Retrieves the entropy source ID from the given account, if it exists.\n *\n * @param account - The account from which to retrieve the entropy source ID.\n * @returns The entropy source ID, or null if it does not exist.\n */\nfunction getAccountEntropySourceId(account: InternalAccount): string | null {\n if (account.options.entropy?.type === 'mnemonic') {\n return account.options.entropy.id;\n }\n return null;\n}\n\n/**\n * Groups accounts by their entropy source ID.\n *\n * @param accounts - The accounts to group.\n * @returns An object where each key is an entropy source ID and each value is\n * an array of account addresses associated with that entropy source ID.\n */\nfunction groupAccountsByEntropySourceId(\n accounts: InternalAccount[],\n): Record<string, AccountWithScopes[]> {\n return accounts.reduce(\n (result: Record<string, AccountWithScopes[]>, account) => {\n const entropySourceId = getAccountEntropySourceId(account);\n const key = entropySourceId ?? 'null';\n if (!result[key]) {\n result[key] = [];\n }\n result[key].push({ address: account.address, scopes: account.scopes });\n return result;\n },\n {},\n );\n}\n"]}
1
+ {"version":3,"file":"ProfileMetricsController.cjs","sourceRoot":"","sources":["../src/ProfileMetricsController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAgBA,qEAA+E;AAE/E,2CAA6E;AAC7E,6CAAoC;AASpC,2DAG8B;AAE9B;;;;GAIG;AACU,QAAA,cAAc,GAAG,0BAA0B,CAAC;AAEzD;;GAEG;AACU,QAAA,8BAA8B,GAAG,IAAA,sBAAc,EAC1D,CAAC,EACD,gBAAQ,CAAC,MAAM,CAChB,CAAC;AAgCF;;GAEG;AACH,MAAM,gCAAgC,GAAG;IACvC,uBAAuB,EAAE;QACvB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;IACD,SAAS,EAAE;QACT,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,KAAK;QAC7B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;IACD,wBAAwB,EAAE;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;IACD,qBAAqB,EAAE;QACrB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;CACqD,CAAC;AAEzD;;;;;;;GAOG;AACH,SAAgB,uCAAuC;IACrD,OAAO;QACL,uBAAuB,EAAE,KAAK;QAC9B,SAAS,EAAE,EAAE;QACb,qBAAqB,EAAE,KAAK;KAC7B,CAAC;AACJ,CAAC;AAND,0FAMC;AAED,MAAM,yBAAyB,GAAG,CAAC,kBAAkB,CAAU,CAAC;AA6DhE;;;;;GAKG;AACH,MAAa,wBAAyB,SAAQ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AK,CAAC,EAAE,CAAC;oBAClC,OAAO,KAAK,CAAC,SAAS,CAAC,eAAe,CAAC,CAAC;gBAC1C,CAAC;gBACD,MAAM;YACR,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAGH;;;;;GAKG;AACH,SAAS,yBAAyB,CAAC,OAAwB;IACzD,IAAI,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,KAAK,UAAU,EAAE,CAAC;QACjD,OAAO,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;IACpC,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;GAMG;AACH,SAAS,8BAA8B,CACrC,QAA2B;IAE3B,OAAO,QAAQ,CAAC,MAAM,CACpB,CAAC,MAA2C,EAAE,OAAO,EAAE,EAAE;QACvD,MAAM,eAAe,GAAG,yBAAyB,CAAC,OAAO,CAAC,CAAC;QAC3D,MAAM,GAAG,GAAG,eAAe,IAAI,MAAM,CAAC;QACtC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;YACjB,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;QACnB,CAAC;QACD,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QACvE,OAAO,MAAM,CAAC;IAChB,CAAC,EACD,EAAE,CACH,CAAC;AACJ,CAAC","sourcesContent":["import type {\n AccountsControllerAccountAddedEvent,\n AccountsControllerAccountRemovedEvent,\n AccountsControllerGetStateAction,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport type {\n KeyringControllerLockEvent,\n KeyringControllerUnlockEvent,\n} from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Messenger } from '@metamask/messenger';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport { TransactionControllerTransactionSubmittedEvent } from '@metamask/transaction-controller';\nimport { Duration, inMilliseconds, parseCaipChainId } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport type { ProfileMetricsControllerMethodActions } from './ProfileMetricsController-method-action-types';\nimport type {\n AccountOwnershipProof,\n AccountWithScopes,\n} from './ProfileMetricsService';\nimport type { ProfileMetricsServiceMethodActions } from './ProfileMetricsService-method-action-types';\nimport type { ProofOfOwnershipServiceMethodActions } from './ProofOfOwnershipService-method-action-types';\nimport {\n canonicalizeAddress,\n ProofUnsupportedNamespaceError,\n} from './utils/canonicalize';\n\n/**\n * The name of the {@link ProfileMetricsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'ProfileMetricsController';\n\n/**\n * The default delay duration before data is sent for the first time.\n */\nexport const DEFAULT_INITIAL_DELAY_DURATION = inMilliseconds(\n 1,\n Duration.Minute,\n);\n\n/**\n * Describes the shape of the state object for {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerState = {\n /**\n * Whether existing accounts have been added\n * to the queue.\n */\n initialEnqueueCompleted: boolean;\n /**\n * The queue of accounts to be synced.\n * Each key is an entropy source ID, and each value is an array of account\n * addresses associated with that entropy source. Accounts with no entropy\n * source ID are grouped under the key \"null\".\n */\n syncQueue: Record<string, AccountWithScopes[]>;\n /**\n * The timestamp when the first data sending can be attempted.\n */\n initialDelayEndTimestamp?: number;\n /**\n * Whether previously-synced accounts have been re-enqueued so their\n * proofs of ownership are submitted alongside everything else. Set on\n * the first unlock after upgrading to a version that signs proofs of\n * ownership; fresh installs flip this on their initial sync since the\n * first poll already attaches proofs.\n */\n proofBackfillEnqueued: boolean;\n};\n\n/**\n * The metadata for each property in {@link ProfileMetricsControllerState}.\n */\nconst profileMetricsControllerMetadata = {\n initialEnqueueCompleted: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n syncQueue: {\n persist: true,\n includeInDebugSnapshot: false,\n includeInStateLogs: true,\n usedInUi: false,\n },\n initialDelayEndTimestamp: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n proofBackfillEnqueued: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<ProfileMetricsControllerState>;\n\n/**\n * Constructs the default {@link ProfileMetricsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link ProfileMetricsController} state.\n */\nexport function getDefaultProfileMetricsControllerState(): ProfileMetricsControllerState {\n return {\n initialEnqueueCompleted: false,\n syncQueue: {},\n proofBackfillEnqueued: false,\n };\n}\n\nconst MESSENGER_EXPOSED_METHODS = ['skipInitialDelay'] as const;\n\n/**\n * Retrieves the state of the {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n ProfileMetricsControllerState\n>;\n\n/**\n * Actions that {@link ProfileMetricsControllerMessenger} exposes to other consumers.\n */\nexport type ProfileMetricsControllerActions =\n | ProfileMetricsControllerGetStateAction\n | ProfileMetricsControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link ProfileMetricsControllerMessenger} calls.\n */\ntype AllowedActions =\n | ProfileMetricsServiceMethodActions\n | ProofOfOwnershipServiceMethodActions\n | AccountsControllerGetStateAction;\n\n/**\n * Published when the state of {@link ProfileMetricsController} changes.\n */\nexport type ProfileMetricsControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n ProfileMetricsControllerState\n >;\n\n/**\n * Events that {@link ProfileMetricsControllerMessenger} exposes to other consumers.\n */\nexport type ProfileMetricsControllerEvents =\n ProfileMetricsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link ProfileMetricsControllerMessenger} subscribes\n * to.\n */\ntype AllowedEvents =\n | KeyringControllerUnlockEvent\n | KeyringControllerLockEvent\n | AccountsControllerAccountAddedEvent\n | AccountsControllerAccountRemovedEvent\n | TransactionControllerTransactionSubmittedEvent;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerMessenger = Messenger<\n typeof controllerName,\n ProfileMetricsControllerActions | AllowedActions,\n ProfileMetricsControllerEvents | AllowedEvents\n>;\n\n/**\n * Manages user profile metrics.\n *\n * For users who opt-in to metrics, this controller ensures we have metrics about their user\n * profile (metrics ID and accounts).\n */\nexport class ProfileMetricsController extends StaticIntervalPollingController()<\n typeof controllerName,\n ProfileMetricsControllerState,\n ProfileMetricsControllerMessenger\n> {\n readonly #mutex = new Mutex();\n\n readonly #assertUserOptedIn: () => boolean;\n\n readonly #getMetaMetricsId: () => string;\n\n readonly #initialDelayDuration: number;\n\n /**\n * Constructs a new {@link ProfileMetricsController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n * @param args.assertUserOptedIn - A function that asserts whether the user has\n * opted in to user profile features. If the user has not opted in, sync\n * operations will be no-ops.\n * @param args.getMetaMetricsId - A function that returns the MetaMetrics ID\n * of the user.\n * @param args.interval - The interval, in milliseconds, at which the controller will\n * attempt to send user profile data. Defaults to 10 seconds.\n * @param args.initialDelayDuration - The delay duration before data is sent\n * for the first time, in milliseconds. Defaults to 10 minutes.\n */\n constructor({\n messenger,\n state,\n assertUserOptedIn,\n getMetaMetricsId,\n interval = 10 * 1000,\n initialDelayDuration = DEFAULT_INITIAL_DELAY_DURATION,\n }: {\n messenger: ProfileMetricsControllerMessenger;\n state?: Partial<ProfileMetricsControllerState>;\n interval?: number;\n assertUserOptedIn: () => boolean;\n getMetaMetricsId: () => string;\n initialDelayDuration?: number;\n }) {\n super({\n messenger,\n metadata: profileMetricsControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultProfileMetricsControllerState(),\n ...state,\n },\n });\n\n this.#assertUserOptedIn = assertUserOptedIn;\n this.#getMetaMetricsId = getMetaMetricsId;\n this.#initialDelayDuration = initialDelayDuration;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.messenger.subscribe('KeyringController:unlock', () => {\n if (this.#assertUserOptedIn()) {\n // If the user has already opted in at the start of the session,\n // it must have opted in during onboarding, or during a previous session.\n this.skipInitialDelay();\n }\n this.#enqueueAccountsIfNeeded().catch(\n this.messenger.captureException ?? console.error,\n );\n this.startPolling(null);\n });\n\n this.messenger.subscribe('KeyringController:lock', () =>\n this.stopAllPolling(),\n );\n\n this.messenger.subscribe('TransactionController:transactionSubmitted', () =>\n this.skipInitialDelay(),\n );\n\n this.messenger.subscribe('AccountsController:accountAdded', (account) => {\n this.#addAccountToQueue(account).catch(console.error);\n });\n\n this.messenger.subscribe('AccountsController:accountRemoved', (account) => {\n this.#removeAccountFromQueue(account).catch(console.error);\n });\n\n this.setIntervalLength(interval);\n }\n\n /**\n * Skip the initial delay period by setting the end timestamp to the current time.\n * Metrics will be sent on the next poll.\n */\n skipInitialDelay(): void {\n this.update((state) => {\n state.initialDelayEndTimestamp = Date.now();\n });\n }\n\n /**\n * Execute a single poll to sync user profile data.\n *\n * The queued accounts are sent to the ProfileMetricsService, each with\n * a proof of ownership when one can be produced (see {@link #attachProofs}),\n * and the sync queue is cleared. This operation is mutexed to prevent\n * concurrent executions.\n *\n * @returns A promise that resolves when the poll is complete.\n */\n async _executePoll(): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n if (!this.#assertUserOptedIn()) {\n return;\n }\n this.#setInitialDelayEndTimestampIfNull();\n if (!this.#isInitialDelayComplete()) {\n return;\n }\n const fullAccountsByAddress = this.#getFullAccountsByAddress();\n for (const [entropySourceId, accounts] of Object.entries(\n this.state.syncQueue,\n )) {\n const normalizedEntropySourceId =\n entropySourceId === 'null' ? null : entropySourceId;\n const accountsWithProofs = await this.#attachProofs(\n accounts,\n fullAccountsByAddress,\n normalizedEntropySourceId,\n );\n try {\n await this.messenger.call('ProfileMetricsService:submitMetrics', {\n metametricsId: this.#getMetaMetricsId(),\n entropySourceId: normalizedEntropySourceId,\n accounts: accountsWithProofs,\n });\n this.update((state) => {\n delete state.syncQueue[entropySourceId];\n });\n } catch (error) {\n // We want to log the error but continue processing other batches.\n console.error(\n `Failed to submit profile metrics for entropy source ID ${entropySourceId}:`,\n error,\n );\n }\n }\n });\n }\n\n /**\n * Attach a proof of ownership to each account in a single entropy-source\n * batch when possible, canonicalizing the address along the way.\n *\n * Per-account failures (unknown namespace, snap missing the\n * `signProofOfOwnership` method, snap rejection) and whole-batch nonce\n * failures are caught and downgraded to \"submit without a proof\" so the\n * batch still goes through and the proof is retried on the next poll.\n *\n * @param accounts - The queued accounts for a single batch.\n * @param fullAccountsByAddress - Live `InternalAccount` lookup keyed by address.\n * @param entropySourceId - The entropy source ID for this batch.\n * @returns The accounts with `proof` populated where signing succeeded.\n */\n async #attachProofs(\n accounts: AccountWithScopes[],\n fullAccountsByAddress: Map<string, InternalAccount>,\n entropySourceId: string | null,\n ): Promise<AccountWithScopes[]> {\n const candidates = new Map<\n string,\n { account: InternalAccount; canonicalAddress: string }\n >();\n const identifiers = new Set<string>();\n for (const queued of accounts) {\n const fullAccount = fullAccountsByAddress.get(queued.address);\n if (!fullAccount) {\n continue;\n }\n try {\n const [scope] = fullAccount.scopes;\n if (!scope) {\n throw new Error(`Scope not found for account ${fullAccount.id}`);\n }\n const { namespace } = parseCaipChainId(scope);\n const canonicalAddress = canonicalizeAddress(\n fullAccount.address,\n namespace,\n );\n candidates.set(queued.address, {\n account: fullAccount,\n canonicalAddress,\n });\n identifiers.add(canonicalAddress);\n } catch (error) {\n // Unsupported namespaces are an expected pass-through; anything\n // else is logged so a new namespace doesn't go unnoticed.\n if (!(error instanceof ProofUnsupportedNamespaceError)) {\n console.error(`Skipping proof for account ${fullAccount.id}:`, error);\n }\n }\n }\n\n if (candidates.size === 0) {\n return accounts;\n }\n\n let nonces: Record<string, string> = {};\n try {\n nonces = await this.messenger.call('ProfileMetricsService:fetchNonces', {\n identifiers: [...identifiers],\n entropySourceId,\n });\n } catch (error) {\n console.error(\n `Failed to fetch proof-of-ownership nonces for entropy source ID ${entropySourceId ?? 'null'}:`,\n error,\n );\n }\n\n return await Promise.all(\n accounts.map(async (queued): Promise<AccountWithScopes> => {\n const candidate = candidates.get(queued.address);\n if (!candidate) {\n return queued;\n }\n const nonce = nonces[candidate.canonicalAddress];\n if (!nonce) {\n return { ...queued, address: candidate.canonicalAddress };\n }\n let proof: AccountOwnershipProof;\n try {\n proof = await this.messenger.call('ProofOfOwnershipService:sign', {\n account: candidate.account,\n nonce,\n });\n } catch (error) {\n console.error(\n `Failed to sign proof of ownership for account ${candidate.account.id}:`,\n error,\n );\n return { ...queued, address: candidate.canonicalAddress };\n }\n return {\n address: candidate.canonicalAddress,\n scopes: queued.scopes,\n proof,\n };\n }),\n );\n }\n\n /**\n * Snapshot the live `InternalAccount` map keyed by address for the\n * current poll.\n *\n * @returns A map of address → `InternalAccount`.\n */\n #getFullAccountsByAddress(): Map<string, InternalAccount> {\n const byAddress = new Map<string, InternalAccount>();\n const accountsState = this.messenger.call('AccountsController:getState');\n for (const account of Object.values(\n accountsState.internalAccounts.accounts,\n )) {\n byAddress.set(account.address, account);\n }\n return byAddress;\n }\n\n /**\n * Enqueue all currently-known accounts onto the sync queue if needed.\n * Single entry point covering both the fresh-install first sync and\n * the one-time proof-of-ownership backfill for users upgrading.\n *\n * Bails for opted-out users (the poll wouldn't drain the queue\n * anyway), and bails once both bootstrap steps have already run.\n * Otherwise enqueues all known accounts and flips both flags so this\n * becomes a permanent no-op for the lifetime of the install.\n */\n async #enqueueAccountsIfNeeded(): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n if (!this.#assertUserOptedIn()) {\n return;\n }\n if (\n this.state.initialEnqueueCompleted &&\n this.state.proofBackfillEnqueued\n ) {\n return;\n }\n const groupedAccounts = groupAccountsByEntropySourceId(\n Object.values(\n this.messenger.call('AccountsController:getState').internalAccounts\n .accounts,\n ),\n );\n this.update((state) => {\n // Replace the queue rather than append. `AccountsController` is\n // the source of truth and the queue is otherwise kept in sync\n // with it via the `accountAdded` / `accountRemoved` subscriptions,\n // so assigning here avoids duplicating entries that survived from\n // a prior session or were pushed earlier in this same unlock\n // cycle. Duplicates would matter because nonces are single-use:\n // letting one through causes `#attachProofs` to sign and submit\n // twice with the same nonce.\n state.syncQueue = groupedAccounts;\n state.initialEnqueueCompleted = true;\n state.proofBackfillEnqueued = true;\n });\n });\n }\n\n /**\n * Set the initial delay end timestamp if it is not already set.\n */\n #setInitialDelayEndTimestampIfNull(): void {\n this.update((state) => {\n state.initialDelayEndTimestamp ??=\n Date.now() + this.#initialDelayDuration;\n });\n }\n\n /**\n * Check if the initial delay end timestamp is in the past.\n *\n * @returns True if the initial delay period has completed, false otherwise.\n */\n #isInitialDelayComplete(): boolean {\n return (\n this.state.initialDelayEndTimestamp !== undefined &&\n Date.now() >= this.state.initialDelayEndTimestamp\n );\n }\n\n /**\n * Queue the given account to be synced at the next poll.\n *\n * @param account - The account to sync.\n */\n async #addAccountToQueue(account: InternalAccount): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n this.update((state) => {\n const entropySourceId = getAccountEntropySourceId(account) ?? 'null';\n if (!state.syncQueue[entropySourceId]) {\n state.syncQueue[entropySourceId] = [];\n }\n state.syncQueue[entropySourceId].push({\n address: account.address,\n scopes: account.scopes,\n });\n });\n });\n }\n\n /**\n * Remove the given account from the sync queue.\n *\n * @param account - The account address to remove.\n */\n async #removeAccountFromQueue(account: string): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n this.update((state) => {\n for (const [entropySourceId, groupedAddresses] of Object.entries(\n state.syncQueue,\n )) {\n const index = groupedAddresses.findIndex(\n ({ address }) => address === account,\n );\n if (index === -1) {\n continue;\n }\n groupedAddresses.splice(index, 1);\n if (groupedAddresses.length === 0) {\n delete state.syncQueue[entropySourceId];\n }\n break;\n }\n });\n });\n }\n}\n\n/**\n * Retrieves the entropy source ID from the given account, if it exists.\n *\n * @param account - The account from which to retrieve the entropy source ID.\n * @returns The entropy source ID, or null if it does not exist.\n */\nfunction getAccountEntropySourceId(account: InternalAccount): string | null {\n if (account.options.entropy?.type === 'mnemonic') {\n return account.options.entropy.id;\n }\n return null;\n}\n\n/**\n * Groups accounts by their entropy source ID.\n *\n * @param accounts - The accounts to group.\n * @returns An object where each key is an entropy source ID and each value is\n * an array of account addresses associated with that entropy source ID.\n */\nfunction groupAccountsByEntropySourceId(\n accounts: InternalAccount[],\n): Record<string, AccountWithScopes[]> {\n return accounts.reduce(\n (result: Record<string, AccountWithScopes[]>, account) => {\n const entropySourceId = getAccountEntropySourceId(account);\n const key = entropySourceId ?? 'null';\n if (!result[key]) {\n result[key] = [];\n }\n result[key].push({ address: account.address, scopes: account.scopes });\n return result;\n },\n {},\n );\n}\n"]}
@@ -7,6 +7,7 @@ import { TransactionControllerTransactionSubmittedEvent } from "@metamask/transa
7
7
  import type { ProfileMetricsControllerMethodActions } from "./ProfileMetricsController-method-action-types.cjs";
8
8
  import type { AccountWithScopes } from "./ProfileMetricsService.cjs";
9
9
  import type { ProfileMetricsServiceMethodActions } from "./ProfileMetricsService-method-action-types.cjs";
10
+ import type { ProofOfOwnershipServiceMethodActions } from "./ProofOfOwnershipService-method-action-types.cjs";
10
11
  /**
11
12
  * The name of the {@link ProfileMetricsController}, used to namespace the
12
13
  * controller's actions and events and to namespace the controller's state data
@@ -37,6 +38,14 @@ export type ProfileMetricsControllerState = {
37
38
  * The timestamp when the first data sending can be attempted.
38
39
  */
39
40
  initialDelayEndTimestamp?: number;
41
+ /**
42
+ * Whether previously-synced accounts have been re-enqueued so their
43
+ * proofs of ownership are submitted alongside everything else. Set on
44
+ * the first unlock after upgrading to a version that signs proofs of
45
+ * ownership; fresh installs flip this on their initial sync since the
46
+ * first poll already attaches proofs.
47
+ */
48
+ proofBackfillEnqueued: boolean;
40
49
  };
41
50
  /**
42
51
  * Constructs the default {@link ProfileMetricsController} state. This allows
@@ -58,7 +67,7 @@ export type ProfileMetricsControllerActions = ProfileMetricsControllerGetStateAc
58
67
  /**
59
68
  * Actions from other messengers that {@link ProfileMetricsControllerMessenger} calls.
60
69
  */
61
- type AllowedActions = ProfileMetricsServiceMethodActions | AccountsControllerGetStateAction;
70
+ type AllowedActions = ProfileMetricsServiceMethodActions | ProofOfOwnershipServiceMethodActions | AccountsControllerGetStateAction;
62
71
  /**
63
72
  * Published when the state of {@link ProfileMetricsController} changes.
64
73
  */
@@ -79,7 +88,11 @@ type AllowedEvents = KeyringControllerUnlockEvent | KeyringControllerLockEvent |
79
88
  export type ProfileMetricsControllerMessenger = Messenger<typeof controllerName, ProfileMetricsControllerActions | AllowedActions, ProfileMetricsControllerEvents | AllowedEvents>;
80
89
  declare const ProfileMetricsController_base: (abstract new (...args: any[]) => {
81
90
  readonly "__#17@#intervalIds": Record<string, NodeJS.Timeout>;
82
- "__#17@#intervalLength": number | undefined;
91
+ "__#17@#intervalLength": number | undefined; /**
92
+ * The name of the {@link ProfileMetricsController}, used to namespace the
93
+ * controller's actions and events and to namespace the controller's state data
94
+ * when composed with other controllers.
95
+ */
83
96
  setIntervalLength(intervalLength: number): void;
84
97
  getIntervalLength(): number | undefined;
85
98
  _startPolling(input: import("@metamask/utils").Json): void;
@@ -133,9 +146,10 @@ export declare class ProfileMetricsController extends ProfileMetricsController_b
133
146
  /**
134
147
  * Execute a single poll to sync user profile data.
135
148
  *
136
- * The queued accounts are sent to the ProfileMetricsService, and the sync
137
- * queue is cleared. This operation is mutexed to prevent concurrent
138
- * executions.
149
+ * The queued accounts are sent to the ProfileMetricsService, each with
150
+ * a proof of ownership when one can be produced (see {@link #attachProofs}),
151
+ * and the sync queue is cleared. This operation is mutexed to prevent
152
+ * concurrent executions.
139
153
  *
140
154
  * @returns A promise that resolves when the poll is complete.
141
155
  */
@@ -1 +1 @@
1
- {"version":3,"file":"ProfileMetricsController.d.cts","sourceRoot":"","sources":["../src/ProfileMetricsController.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EACV,mCAAmC,EACnC,qCAAqC,EACrC,gCAAgC,EACjC,sCAAsC;AACvC,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,KAAK,EACV,0BAA0B,EAC1B,4BAA4B,EAC7B,qCAAqC;AAEtC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,EAAE,8CAA8C,EAAE,yCAAyC;AAIlG,OAAO,KAAK,EAAE,qCAAqC,EAAE,2DAAuD;AAC5G,OAAO,KAAK,EAAE,iBAAiB,EAAE,oCAAgC;AACjE,OAAO,KAAK,EAAE,kCAAkC,EAAE,wDAAoD;AAEtG;;;;GAIG;AACH,eAAO,MAAM,cAAc,6BAA6B,CAAC;AAEzD;;GAEG;AACH,eAAO,MAAM,8BAA8B,QAG1C,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;;OAGG;IACH,uBAAuB,EAAE,OAAO,CAAC;IACjC;;;;;OAKG;IACH,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,iBAAiB,EAAE,CAAC,CAAC;IAC/C;;OAEG;IACH,wBAAwB,CAAC,EAAE,MAAM,CAAC;CACnC,CAAC;AA0BF;;;;;;;GAOG;AACH,wBAAgB,uCAAuC,IAAI,6BAA6B,CAKvF;AAID;;GAEG;AACH,MAAM,MAAM,sCAAsC,GAAG,wBAAwB,CAC3E,OAAO,cAAc,EACrB,6BAA6B,CAC9B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,+BAA+B,GACvC,sCAAsC,GACtC,qCAAqC,CAAC;AAE1C;;GAEG;AACH,KAAK,cAAc,GACf,kCAAkC,GAClC,gCAAgC,CAAC;AAErC;;GAEG;AACH,MAAM,MAAM,wCAAwC,GAClD,0BAA0B,CACxB,OAAO,cAAc,EACrB,6BAA6B,CAC9B,CAAC;AAEJ;;GAEG;AACH,MAAM,MAAM,8BAA8B,GACxC,wCAAwC,CAAC;AAE3C;;;GAGG;AACH,KAAK,aAAa,GACd,4BAA4B,GAC5B,0BAA0B,GAC1B,mCAAmC,GACnC,qCAAqC,GACrC,8CAA8C,CAAC;AAEnD;;;GAGG;AACH,MAAM,MAAM,iCAAiC,GAAG,SAAS,CACvD,OAAO,cAAc,EACrB,+BAA+B,GAAG,cAAc,EAChD,8BAA8B,GAAG,aAAa,CAC/C,CAAC;;;;;;;;;;;;;;;;AAEF;;;;;GAKG;AACH,qBAAa,wBAAyB,SAAQ,8BAC5C,OAAO,cAAc,EACrB,6BAA6B,EAC7B,iCAAiC,CAClC;;IASC;;;;;;;;;;;;;;;;OAgBG;gBACS,EACV,SAAS,EACT,KAAK,EACL,iBAAiB,EACjB,gBAAgB,EAChB,QAAoB,EACpB,oBAAqD,GACtD,EAAE;QACD,SAAS,EAAE,iCAAiC,CAAC;QAC7C,KAAK,CAAC,EAAE,OAAO,CAAC,6BAA6B,CAAC,CAAC;QAC/C,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,iBAAiB,EAAE,MAAM,OAAO,CAAC;QACjC,gBAAgB,EAAE,MAAM,MAAM,CAAC;QAC/B,oBAAoB,CAAC,EAAE,MAAM,CAAC;KAC/B;IAmDD;;;OAGG;IACH,gBAAgB,IAAI,IAAI;IAMxB;;;;;;;;OAQG;IACG,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;CAkIpC"}
1
+ {"version":3,"file":"ProfileMetricsController.d.cts","sourceRoot":"","sources":["../src/ProfileMetricsController.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EACV,mCAAmC,EACnC,qCAAqC,EACrC,gCAAgC,EACjC,sCAAsC;AACvC,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,KAAK,EACV,0BAA0B,EAC1B,4BA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1B,mCAAmC,GACnC,qCAAqC,GACrC,8CAA8C,CAAC;AAEnD;;;GAGG;AACH,MAAM,MAAM,iCAAiC,GAAG,SAAS,CACvD,OAAO,cAAc,EACrB,+BAA+B,GAAG,cAAc,EAChD,8BAA8B,GAAG,aAAa,CAC/C,CAAC;;;iDAtJF;;;;OAIG;;;;;;;;;;;;;AAoJH;;;;;GAKG;AACH,qBAAa,wBAAyB,SAAQ,8BAC5C,OAAO,cAAc,EACrB,6BAA6B,EAC7B,iCAAiC,CAClC;;IASC;;;;;;;;;;;;;;;;OAgBG;gBACS,EACV,SAAS,EACT,KAAK,EACL,iBAAiB,EACjB,gBAAgB,EAChB,QAAoB,EACpB,oBAAqD,GACtD,EAAE;QACD,SAAS,EAAE,iCAAiC,CAAC;QAC7C,KAAK,CAAC,EAAE,OAAO,CAAC,6BAA6B,CAAC,CAAC;QAC/C,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,iBAAiB,EAAE,MAAM,OAAO,CAAC;QACjC,gBAAgB,EAAE,MAAM,MAAM,CAAC;QAC/B,oBAAoB,CAAC,EAAE,MAAM,CAAC;KAC/B;IAmDD;;;OAGG;IACH,gBAAgB,IAAI,IAAI;IAMxB;;;;;;;;;OASG;IACG,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;CA8QpC"}
@@ -7,6 +7,7 @@ import { TransactionControllerTransactionSubmittedEvent } from "@metamask/transa
7
7
  import type { ProfileMetricsControllerMethodActions } from "./ProfileMetricsController-method-action-types.mjs";
8
8
  import type { AccountWithScopes } from "./ProfileMetricsService.mjs";
9
9
  import type { ProfileMetricsServiceMethodActions } from "./ProfileMetricsService-method-action-types.mjs";
10
+ import type { ProofOfOwnershipServiceMethodActions } from "./ProofOfOwnershipService-method-action-types.mjs";
10
11
  /**
11
12
  * The name of the {@link ProfileMetricsController}, used to namespace the
12
13
  * controller's actions and events and to namespace the controller's state data
@@ -37,6 +38,14 @@ export type ProfileMetricsControllerState = {
37
38
  * The timestamp when the first data sending can be attempted.
38
39
  */
39
40
  initialDelayEndTimestamp?: number;
41
+ /**
42
+ * Whether previously-synced accounts have been re-enqueued so their
43
+ * proofs of ownership are submitted alongside everything else. Set on
44
+ * the first unlock after upgrading to a version that signs proofs of
45
+ * ownership; fresh installs flip this on their initial sync since the
46
+ * first poll already attaches proofs.
47
+ */
48
+ proofBackfillEnqueued: boolean;
40
49
  };
41
50
  /**
42
51
  * Constructs the default {@link ProfileMetricsController} state. This allows
@@ -58,7 +67,7 @@ export type ProfileMetricsControllerActions = ProfileMetricsControllerGetStateAc
58
67
  /**
59
68
  * Actions from other messengers that {@link ProfileMetricsControllerMessenger} calls.
60
69
  */
61
- type AllowedActions = ProfileMetricsServiceMethodActions | AccountsControllerGetStateAction;
70
+ type AllowedActions = ProfileMetricsServiceMethodActions | ProofOfOwnershipServiceMethodActions | AccountsControllerGetStateAction;
62
71
  /**
63
72
  * Published when the state of {@link ProfileMetricsController} changes.
64
73
  */
@@ -79,7 +88,11 @@ type AllowedEvents = KeyringControllerUnlockEvent | KeyringControllerLockEvent |
79
88
  export type ProfileMetricsControllerMessenger = Messenger<typeof controllerName, ProfileMetricsControllerActions | AllowedActions, ProfileMetricsControllerEvents | AllowedEvents>;
80
89
  declare const ProfileMetricsController_base: (abstract new (...args: any[]) => {
81
90
  readonly "__#17@#intervalIds": Record<string, NodeJS.Timeout>;
82
- "__#17@#intervalLength": number | undefined;
91
+ "__#17@#intervalLength": number | undefined; /**
92
+ * The name of the {@link ProfileMetricsController}, used to namespace the
93
+ * controller's actions and events and to namespace the controller's state data
94
+ * when composed with other controllers.
95
+ */
83
96
  setIntervalLength(intervalLength: number): void;
84
97
  getIntervalLength(): number | undefined;
85
98
  _startPolling(input: import("@metamask/utils").Json): void;
@@ -133,9 +146,10 @@ export declare class ProfileMetricsController extends ProfileMetricsController_b
133
146
  /**
134
147
  * Execute a single poll to sync user profile data.
135
148
  *
136
- * The queued accounts are sent to the ProfileMetricsService, and the sync
137
- * queue is cleared. This operation is mutexed to prevent concurrent
138
- * executions.
149
+ * The queued accounts are sent to the ProfileMetricsService, each with
150
+ * a proof of ownership when one can be produced (see {@link #attachProofs}),
151
+ * and the sync queue is cleared. This operation is mutexed to prevent
152
+ * concurrent executions.
139
153
  *
140
154
  * @returns A promise that resolves when the poll is complete.
141
155
  */
@@ -1 +1 @@
1
- {"version":3,"file":"ProfileMetricsController.d.mts","sourceRoot":"","sources":["../src/ProfileMetricsController.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EACV,mCAAmC,EACnC,qCAAqC,EACrC,gCAAgC,EACjC,sCAAsC;AACvC,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,KAAK,EACV,0BAA0B,EAC1B,4BAA4B,EAC7B,qCAAqC;AAEtC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,EAAE,8CAA8C,EAAE,yCAAyC;AAIlG,OAAO,KAAK,EAAE,qCAAqC,EAAE,2DAAuD;AAC5G,OAAO,KAAK,EAAE,iBAAiB,EAAE,oCAAgC;AACjE,OAAO,KAAK,EAAE,kCAAkC,EAAE,wDAAoD;AAEtG;;;;GAIG;AACH,eAAO,MAAM,cAAc,6BAA6B,CAAC;AAEzD;;GAEG;AACH,eAAO,MAAM,8BAA8B,QAG1C,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;;OAGG;IACH,uBAAuB,EAAE,OAAO,CAAC;IACjC;;;;;OAKG;IACH,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,iBAAiB,EAAE,CAAC,CAAC;IAC/C;;OAEG;IACH,wBAAwB,CAAC,EAAE,MAAM,CAAC;CACnC,CAAC;AA0BF;;;;;;;GAOG;AACH,wBAAgB,uCAAuC,IAAI,6BAA6B,CAKvF;AAID;;GAEG;AACH,MAAM,MAAM,sCAAsC,GAAG,wBAAwB,CAC3E,OAAO,cAAc,EACrB,6BAA6B,CAC9B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,+BAA+B,GACvC,sCAAsC,GACtC,qCAAqC,CAAC;AAE1C;;GAEG;AACH,KAAK,cAAc,GACf,kCAAkC,GAClC,gCAAgC,CAAC;AAErC;;GAEG;AACH,MAAM,MAAM,wCAAwC,GAClD,0BAA0B,CACxB,OAAO,cAAc,EACrB,6BAA6B,CAC9B,CAAC;AAEJ;;GAEG;AACH,MAAM,MAAM,8BAA8B,GACxC,wCAAwC,CAAC;AAE3C;;;GAGG;AACH,KAAK,aAAa,GACd,4BAA4B,GAC5B,0BAA0B,GAC1B,mCAAmC,GACnC,qCAAqC,GACrC,8CAA8C,CAAC;AAEnD;;;GAGG;AACH,MAAM,MAAM,iCAAiC,GAAG,SAAS,CACvD,OAAO,cAAc,EACrB,+BAA+B,GAAG,cAAc,EAChD,8BAA8B,GAAG,aAAa,CAC/C,CAAC;;;;;;;;;;;;;;;;AAEF;;;;;GAKG;AACH,qBAAa,wBAAyB,SAAQ,8BAC5C,OAAO,cAAc,EACrB,6BAA6B,EAC7B,iCAAiC,CAClC;;IASC;;;;;;;;;;;;;;;;OAgBG;gBACS,EACV,SAAS,EACT,KAAK,EACL,iBAAiB,EACjB,gBAAgB,EAChB,QAAoB,EACpB,oBAAqD,GACtD,EAAE;QACD,SAAS,EAAE,iCAAiC,CAAC;QAC7C,KAAK,CAAC,EAAE,OAAO,CAAC,6BAA6B,CAAC,CAAC;QAC/C,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,iBAAiB,EAAE,MAAM,OAAO,CAAC;QACjC,gBAAgB,EAAE,MAAM,MAAM,CAAC;QAC/B,oBAAoB,CAAC,EAAE,MAAM,CAAC;KAC/B;IAmDD;;;OAGG;IACH,gBAAgB,IAAI,IAAI;IAMxB;;;;;;;;OAQG;IACG,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;CAkIpC"}
1
+ {"version":3,"file":"ProfileMetricsController.d.mts","sourceRoot":"","sources":["../src/ProfileMetricsController.ts"],"names":[],"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AClD,0BAA0B,CACxB,OAAO,cAAc,EACrB,6BAA6B,CAC9B,CAAC;AAEJ;;GAEG;AACH,MAAM,MAAM,8BAA8B,GACxC,wCAAwC,CAAC;AAE3C;;;GAGG;AACH,KAAK,aAAa,GACd,4BAA4B,GAC5B,0BAA0B,GAC1B,mCAAmC,GACnC,qCAAqC,GACrC,8CAA8C,CAAC;AAEnD;;;GAGG;AACH,MAAM,MAAM,iCAAiC,GAAG,SAAS,CACvD,OAAO,cAAc,EACrB,+BAA+B,GAAG,cAAc,EAChD,8BAA8B,GAAG,aAAa,CAC/C,CAAC;;;iDAtJF;;;;OAIG;;;;;;;;;;;;;AAoJH;;;;;GAKG;AACH,qBAAa,wBAAyB,SAAQ,8BAC5C,OAAO,cAAc,EACrB,6BAA6B,EAC7B,iCAAiC,CAClC;;IASC;;;;;;;;;;;;;;;;OAgBG;gBACS,EACV,SAAS,EACT,KAAK,EACL,iBAAiB,EACjB,gBAAgB,EAChB,QAAoB,EACpB,oBAAqD,GACtD,EAAE;QACD,SAAS,EAAE,iCAAiC,CAAC;QAC7C,KAAK,CAAC,EAAE,OAAO,CAAC,6BAA6B,CAAC,CAAC;QAC/C,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,iBAAiB,EAAE,MAAM,OAAO,CAAC;QACjC,gBAAgB,EAAE,MAAM,MAAM,CAAC;QAC/B,oBAAoB,CAAC,EAAE,MAAM,CAAC;KAC/B;IAmDD;;;OAGG;IACH,gBAAgB,IAAI,IAAI;IAMxB;;;;;;;;;OASG;IACG,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;CA8QpC"}
@@ -9,10 +9,11 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _ProfileMetricsController_instances, _ProfileMetricsController_mutex, _ProfileMetricsController_assertUserOptedIn, _ProfileMetricsController_getMetaMetricsId, _ProfileMetricsController_initialDelayDuration, _ProfileMetricsController_queueFirstSyncIfNeeded, _ProfileMetricsController_setInitialDelayEndTimestampIfNull, _ProfileMetricsController_isInitialDelayComplete, _ProfileMetricsController_addAccountToQueue, _ProfileMetricsController_removeAccountFromQueue;
12
+ var _ProfileMetricsController_instances, _ProfileMetricsController_mutex, _ProfileMetricsController_assertUserOptedIn, _ProfileMetricsController_getMetaMetricsId, _ProfileMetricsController_initialDelayDuration, _ProfileMetricsController_attachProofs, _ProfileMetricsController_getFullAccountsByAddress, _ProfileMetricsController_enqueueAccountsIfNeeded, _ProfileMetricsController_setInitialDelayEndTimestampIfNull, _ProfileMetricsController_isInitialDelayComplete, _ProfileMetricsController_addAccountToQueue, _ProfileMetricsController_removeAccountFromQueue;
13
13
  import { StaticIntervalPollingController } from "@metamask/polling-controller";
14
- import { Duration, inMilliseconds } from "@metamask/utils";
14
+ import { Duration, inMilliseconds, parseCaipChainId } from "@metamask/utils";
15
15
  import { Mutex } from "async-mutex";
16
+ import { canonicalizeAddress, ProofUnsupportedNamespaceError } from "./utils/canonicalize.mjs";
16
17
  /**
17
18
  * The name of the {@link ProfileMetricsController}, used to namespace the
18
19
  * controller's actions and events and to namespace the controller's state data
@@ -45,6 +46,12 @@ const profileMetricsControllerMetadata = {
45
46
  includeInStateLogs: true,
46
47
  usedInUi: false,
47
48
  },
49
+ proofBackfillEnqueued: {
50
+ persist: true,
51
+ includeInDebugSnapshot: true,
52
+ includeInStateLogs: true,
53
+ usedInUi: false,
54
+ },
48
55
  };
49
56
  /**
50
57
  * Constructs the default {@link ProfileMetricsController} state. This allows
@@ -58,6 +65,7 @@ export function getDefaultProfileMetricsControllerState() {
58
65
  return {
59
66
  initialEnqueueCompleted: false,
60
67
  syncQueue: {},
68
+ proofBackfillEnqueued: false,
61
69
  };
62
70
  }
63
71
  const MESSENGER_EXPOSED_METHODS = ['skipInitialDelay'];
@@ -110,7 +118,7 @@ export class ProfileMetricsController extends StaticIntervalPollingController()
110
118
  // it must have opted in during onboarding, or during a previous session.
111
119
  this.skipInitialDelay();
112
120
  }
113
- __classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_queueFirstSyncIfNeeded).call(this).catch(this.messenger.captureException ?? console.error);
121
+ __classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_enqueueAccountsIfNeeded).call(this).catch(this.messenger.captureException ?? console.error);
114
122
  this.startPolling(null);
115
123
  });
116
124
  this.messenger.subscribe('KeyringController:lock', () => this.stopAllPolling());
@@ -135,9 +143,10 @@ export class ProfileMetricsController extends StaticIntervalPollingController()
135
143
  /**
136
144
  * Execute a single poll to sync user profile data.
137
145
  *
138
- * The queued accounts are sent to the ProfileMetricsService, and the sync
139
- * queue is cleared. This operation is mutexed to prevent concurrent
140
- * executions.
146
+ * The queued accounts are sent to the ProfileMetricsService, each with
147
+ * a proof of ownership when one can be produced (see {@link #attachProofs}),
148
+ * and the sync queue is cleared. This operation is mutexed to prevent
149
+ * concurrent executions.
141
150
  *
142
151
  * @returns A promise that resolves when the poll is complete.
143
152
  */
@@ -150,12 +159,15 @@ export class ProfileMetricsController extends StaticIntervalPollingController()
150
159
  if (!__classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_isInitialDelayComplete).call(this)) {
151
160
  return;
152
161
  }
162
+ const fullAccountsByAddress = __classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_getFullAccountsByAddress).call(this);
153
163
  for (const [entropySourceId, accounts] of Object.entries(this.state.syncQueue)) {
164
+ const normalizedEntropySourceId = entropySourceId === 'null' ? null : entropySourceId;
165
+ const accountsWithProofs = await __classPrivateFieldGet(this, _ProfileMetricsController_instances, "m", _ProfileMetricsController_attachProofs).call(this, accounts, fullAccountsByAddress, normalizedEntropySourceId);
154
166
  try {
155
167
  await this.messenger.call('ProfileMetricsService:submitMetrics', {
156
168
  metametricsId: __classPrivateFieldGet(this, _ProfileMetricsController_getMetaMetricsId, "f").call(this),
157
- entropySourceId: entropySourceId === 'null' ? null : entropySourceId,
158
- accounts,
169
+ entropySourceId: normalizedEntropySourceId,
170
+ accounts: accountsWithProofs,
159
171
  });
160
172
  this.update((state) => {
161
173
  delete state.syncQueue[entropySourceId];
@@ -169,28 +181,130 @@ export class ProfileMetricsController extends StaticIntervalPollingController()
169
181
  });
170
182
  }
171
183
  }
172
- _ProfileMetricsController_mutex = new WeakMap(), _ProfileMetricsController_assertUserOptedIn = new WeakMap(), _ProfileMetricsController_getMetaMetricsId = new WeakMap(), _ProfileMetricsController_initialDelayDuration = new WeakMap(), _ProfileMetricsController_instances = new WeakSet(), _ProfileMetricsController_queueFirstSyncIfNeeded =
184
+ _ProfileMetricsController_mutex = new WeakMap(), _ProfileMetricsController_assertUserOptedIn = new WeakMap(), _ProfileMetricsController_getMetaMetricsId = new WeakMap(), _ProfileMetricsController_initialDelayDuration = new WeakMap(), _ProfileMetricsController_instances = new WeakSet(), _ProfileMetricsController_attachProofs =
185
+ /**
186
+ * Attach a proof of ownership to each account in a single entropy-source
187
+ * batch when possible, canonicalizing the address along the way.
188
+ *
189
+ * Per-account failures (unknown namespace, snap missing the
190
+ * `signProofOfOwnership` method, snap rejection) and whole-batch nonce
191
+ * failures are caught and downgraded to "submit without a proof" so the
192
+ * batch still goes through and the proof is retried on the next poll.
193
+ *
194
+ * @param accounts - The queued accounts for a single batch.
195
+ * @param fullAccountsByAddress - Live `InternalAccount` lookup keyed by address.
196
+ * @param entropySourceId - The entropy source ID for this batch.
197
+ * @returns The accounts with `proof` populated where signing succeeded.
198
+ */
199
+ async function _ProfileMetricsController_attachProofs(accounts, fullAccountsByAddress, entropySourceId) {
200
+ const candidates = new Map();
201
+ const identifiers = new Set();
202
+ for (const queued of accounts) {
203
+ const fullAccount = fullAccountsByAddress.get(queued.address);
204
+ if (!fullAccount) {
205
+ continue;
206
+ }
207
+ try {
208
+ const [scope] = fullAccount.scopes;
209
+ if (!scope) {
210
+ throw new Error(`Scope not found for account ${fullAccount.id}`);
211
+ }
212
+ const { namespace } = parseCaipChainId(scope);
213
+ const canonicalAddress = canonicalizeAddress(fullAccount.address, namespace);
214
+ candidates.set(queued.address, {
215
+ account: fullAccount,
216
+ canonicalAddress,
217
+ });
218
+ identifiers.add(canonicalAddress);
219
+ }
220
+ catch (error) {
221
+ // Unsupported namespaces are an expected pass-through; anything
222
+ // else is logged so a new namespace doesn't go unnoticed.
223
+ if (!(error instanceof ProofUnsupportedNamespaceError)) {
224
+ console.error(`Skipping proof for account ${fullAccount.id}:`, error);
225
+ }
226
+ }
227
+ }
228
+ if (candidates.size === 0) {
229
+ return accounts;
230
+ }
231
+ let nonces = {};
232
+ try {
233
+ nonces = await this.messenger.call('ProfileMetricsService:fetchNonces', {
234
+ identifiers: [...identifiers],
235
+ entropySourceId,
236
+ });
237
+ }
238
+ catch (error) {
239
+ console.error(`Failed to fetch proof-of-ownership nonces for entropy source ID ${entropySourceId ?? 'null'}:`, error);
240
+ }
241
+ return await Promise.all(accounts.map(async (queued) => {
242
+ const candidate = candidates.get(queued.address);
243
+ if (!candidate) {
244
+ return queued;
245
+ }
246
+ const nonce = nonces[candidate.canonicalAddress];
247
+ if (!nonce) {
248
+ return { ...queued, address: candidate.canonicalAddress };
249
+ }
250
+ let proof;
251
+ try {
252
+ proof = await this.messenger.call('ProofOfOwnershipService:sign', {
253
+ account: candidate.account,
254
+ nonce,
255
+ });
256
+ }
257
+ catch (error) {
258
+ console.error(`Failed to sign proof of ownership for account ${candidate.account.id}:`, error);
259
+ return { ...queued, address: candidate.canonicalAddress };
260
+ }
261
+ return {
262
+ address: candidate.canonicalAddress,
263
+ scopes: queued.scopes,
264
+ proof,
265
+ };
266
+ }));
267
+ }, _ProfileMetricsController_getFullAccountsByAddress = function _ProfileMetricsController_getFullAccountsByAddress() {
268
+ const byAddress = new Map();
269
+ const accountsState = this.messenger.call('AccountsController:getState');
270
+ for (const account of Object.values(accountsState.internalAccounts.accounts)) {
271
+ byAddress.set(account.address, account);
272
+ }
273
+ return byAddress;
274
+ }, _ProfileMetricsController_enqueueAccountsIfNeeded =
173
275
  /**
174
- * Add existing accounts to the sync queue if it has not been done yet.
276
+ * Enqueue all currently-known accounts onto the sync queue if needed.
277
+ * Single entry point covering both the fresh-install first sync and
278
+ * the one-time proof-of-ownership backfill for users upgrading.
175
279
  *
176
- * This method ensures that the first sync is only executed once,
177
- * and only if the user has opted in to user profile features.
280
+ * Bails for opted-out users (the poll wouldn't drain the queue
281
+ * anyway), and bails once both bootstrap steps have already run.
282
+ * Otherwise enqueues all known accounts and flips both flags so this
283
+ * becomes a permanent no-op for the lifetime of the install.
178
284
  */
179
- async function _ProfileMetricsController_queueFirstSyncIfNeeded() {
285
+ async function _ProfileMetricsController_enqueueAccountsIfNeeded() {
180
286
  await __classPrivateFieldGet(this, _ProfileMetricsController_mutex, "f").runExclusive(async () => {
181
- if (this.state.initialEnqueueCompleted) {
287
+ if (!__classPrivateFieldGet(this, _ProfileMetricsController_assertUserOptedIn, "f").call(this)) {
182
288
  return;
183
289
  }
184
- const newGroupedAccounts = groupAccountsByEntropySourceId(Object.values(this.messenger.call('AccountsController:getState').internalAccounts
290
+ if (this.state.initialEnqueueCompleted &&
291
+ this.state.proofBackfillEnqueued) {
292
+ return;
293
+ }
294
+ const groupedAccounts = groupAccountsByEntropySourceId(Object.values(this.messenger.call('AccountsController:getState').internalAccounts
185
295
  .accounts));
186
296
  this.update((state) => {
187
- for (const key of Object.keys(newGroupedAccounts)) {
188
- if (!state.syncQueue[key]) {
189
- state.syncQueue[key] = [];
190
- }
191
- state.syncQueue[key].push(...newGroupedAccounts[key]);
192
- }
297
+ // Replace the queue rather than append. `AccountsController` is
298
+ // the source of truth and the queue is otherwise kept in sync
299
+ // with it via the `accountAdded` / `accountRemoved` subscriptions,
300
+ // so assigning here avoids duplicating entries that survived from
301
+ // a prior session or were pushed earlier in this same unlock
302
+ // cycle. Duplicates would matter because nonces are single-use:
303
+ // letting one through causes `#attachProofs` to sign and submit
304
+ // twice with the same nonce.
305
+ state.syncQueue = groupedAccounts;
193
306
  state.initialEnqueueCompleted = true;
307
+ state.proofBackfillEnqueued = true;
194
308
  });
195
309
  });
196
310
  }, _ProfileMetricsController_setInitialDelayEndTimestampIfNull = function _ProfileMetricsController_setInitialDelayEndTimestampIfNull() {
@@ -1 +1 @@
1
- {"version":3,"file":"ProfileMetricsController.mjs","sourceRoot":"","sources":["../src/ProfileMetricsController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAgBA,OAAO,EAAE,+BAA+B,EAAE,qCAAqC;AAE/E,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,wBAAwB;AAC3D,OAAO,EAAE,KAAK,EAAE,oBAAoB;AAMpC;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,0BAA0B,CAAC;AAEzD;;GAEG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAG,cAAc,CAC1D,CAAC,EACD,QAAQ,CAAC,MAAM,CAChB,CAAC;AAwBF;;GAEG;AACH,MAAM,gCAAgC,GAAG;IACvC,uBAAuB,EAAE;QACvB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;IACD,SAAS,EAAE;QACT,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,KAAK;QAC7B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;IACD,wBAAwB,EAAE;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,kBAAkB,EAAE,IAAI;QACxB,QAAQ,EAAE,KAAK;KAChB;CACqD,CAAC;AAEzD;;;;;;;GAOG;AACH,MAAM,UAAU,uCAAuC;IACrD,OAAO;QACL,uBAAuB,EAAE,KAAK;QAC9B,SAAS,EAAE,EAAE;KACd,CAAC;AACJ,C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I,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;YACjB,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;QACnB,CAAC;QACD,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QACvE,OAAO,MAAM,CAAC;IAChB,CAAC,EACD,EAAE,CACH,CAAC;AACJ,CAAC","sourcesContent":["import type {\n AccountsControllerAccountAddedEvent,\n AccountsControllerAccountRemovedEvent,\n AccountsControllerGetStateAction,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport type {\n KeyringControllerLockEvent,\n KeyringControllerUnlockEvent,\n} from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Messenger } from '@metamask/messenger';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport { TransactionControllerTransactionSubmittedEvent } from '@metamask/transaction-controller';\nimport { Duration, inMilliseconds } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport type { ProfileMetricsControllerMethodActions } from './ProfileMetricsController-method-action-types';\nimport type { AccountWithScopes } from './ProfileMetricsService';\nimport type { ProfileMetricsServiceMethodActions } from './ProfileMetricsService-method-action-types';\n\n/**\n * The name of the {@link ProfileMetricsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'ProfileMetricsController';\n\n/**\n * The default delay duration before data is sent for the first time.\n */\nexport const DEFAULT_INITIAL_DELAY_DURATION = inMilliseconds(\n 1,\n Duration.Minute,\n);\n\n/**\n * Describes the shape of the state object for {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerState = {\n /**\n * Whether existing accounts have been added\n * to the queue.\n */\n initialEnqueueCompleted: boolean;\n /**\n * The queue of accounts to be synced.\n * Each key is an entropy source ID, and each value is an array of account\n * addresses associated with that entropy source. Accounts with no entropy\n * source ID are grouped under the key \"null\".\n */\n syncQueue: Record<string, AccountWithScopes[]>;\n /**\n * The timestamp when the first data sending can be attempted.\n */\n initialDelayEndTimestamp?: number;\n};\n\n/**\n * The metadata for each property in {@link ProfileMetricsControllerState}.\n */\nconst profileMetricsControllerMetadata = {\n initialEnqueueCompleted: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n syncQueue: {\n persist: true,\n includeInDebugSnapshot: false,\n includeInStateLogs: true,\n usedInUi: false,\n },\n initialDelayEndTimestamp: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<ProfileMetricsControllerState>;\n\n/**\n * Constructs the default {@link ProfileMetricsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link ProfileMetricsController} state.\n */\nexport function getDefaultProfileMetricsControllerState(): ProfileMetricsControllerState {\n return {\n initialEnqueueCompleted: false,\n syncQueue: {},\n };\n}\n\nconst MESSENGER_EXPOSED_METHODS = ['skipInitialDelay'] as const;\n\n/**\n * Retrieves the state of the {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n ProfileMetricsControllerState\n>;\n\n/**\n * Actions that {@link ProfileMetricsControllerMessenger} exposes to other consumers.\n */\nexport type ProfileMetricsControllerActions =\n | ProfileMetricsControllerGetStateAction\n | ProfileMetricsControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link ProfileMetricsControllerMessenger} calls.\n */\ntype AllowedActions =\n | ProfileMetricsServiceMethodActions\n | AccountsControllerGetStateAction;\n\n/**\n * Published when the state of {@link ProfileMetricsController} changes.\n */\nexport type ProfileMetricsControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n ProfileMetricsControllerState\n >;\n\n/**\n * Events that {@link ProfileMetricsControllerMessenger} exposes to other consumers.\n */\nexport type ProfileMetricsControllerEvents =\n ProfileMetricsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link ProfileMetricsControllerMessenger} subscribes\n * to.\n */\ntype AllowedEvents =\n | KeyringControllerUnlockEvent\n | KeyringControllerLockEvent\n | AccountsControllerAccountAddedEvent\n | AccountsControllerAccountRemovedEvent\n | TransactionControllerTransactionSubmittedEvent;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerMessenger = Messenger<\n typeof controllerName,\n ProfileMetricsControllerActions | AllowedActions,\n ProfileMetricsControllerEvents | AllowedEvents\n>;\n\n/**\n * Manages user profile metrics.\n *\n * For users who opt-in to metrics, this controller ensures we have metrics about their user\n * profile (metrics ID and accounts).\n */\nexport class ProfileMetricsController extends StaticIntervalPollingController()<\n typeof controllerName,\n ProfileMetricsControllerState,\n ProfileMetricsControllerMessenger\n> {\n readonly #mutex = new Mutex();\n\n readonly #assertUserOptedIn: () => boolean;\n\n readonly #getMetaMetricsId: () => string;\n\n readonly #initialDelayDuration: number;\n\n /**\n * Constructs a new {@link ProfileMetricsController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n * @param args.assertUserOptedIn - A function that asserts whether the user has\n * opted in to user profile features. If the user has not opted in, sync\n * operations will be no-ops.\n * @param args.getMetaMetricsId - A function that returns the MetaMetrics ID\n * of the user.\n * @param args.interval - The interval, in milliseconds, at which the controller will\n * attempt to send user profile data. Defaults to 10 seconds.\n * @param args.initialDelayDuration - The delay duration before data is sent\n * for the first time, in milliseconds. Defaults to 10 minutes.\n */\n constructor({\n messenger,\n state,\n assertUserOptedIn,\n getMetaMetricsId,\n interval = 10 * 1000,\n initialDelayDuration = DEFAULT_INITIAL_DELAY_DURATION,\n }: {\n messenger: ProfileMetricsControllerMessenger;\n state?: Partial<ProfileMetricsControllerState>;\n interval?: number;\n assertUserOptedIn: () => boolean;\n getMetaMetricsId: () => string;\n initialDelayDuration?: number;\n }) {\n super({\n messenger,\n metadata: profileMetricsControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultProfileMetricsControllerState(),\n ...state,\n },\n });\n\n this.#assertUserOptedIn = assertUserOptedIn;\n this.#getMetaMetricsId = getMetaMetricsId;\n this.#initialDelayDuration = initialDelayDuration;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.messenger.subscribe('KeyringController:unlock', () => {\n if (this.#assertUserOptedIn()) {\n // If the user has already opted in at the start of the session,\n // it must have opted in during onboarding, or during a previous session.\n this.skipInitialDelay();\n }\n this.#queueFirstSyncIfNeeded().catch(\n this.messenger.captureException ?? console.error,\n );\n this.startPolling(null);\n });\n\n this.messenger.subscribe('KeyringController:lock', () =>\n this.stopAllPolling(),\n );\n\n this.messenger.subscribe('TransactionController:transactionSubmitted', () =>\n this.skipInitialDelay(),\n );\n\n this.messenger.subscribe('AccountsController:accountAdded', (account) => {\n this.#addAccountToQueue(account).catch(console.error);\n });\n\n this.messenger.subscribe('AccountsController:accountRemoved', (account) => {\n this.#removeAccountFromQueue(account).catch(console.error);\n });\n\n this.setIntervalLength(interval);\n }\n\n /**\n * Skip the initial delay period by setting the end timestamp to the current time.\n * Metrics will be sent on the next poll.\n */\n skipInitialDelay(): void {\n this.update((state) => {\n state.initialDelayEndTimestamp = Date.now();\n });\n }\n\n /**\n * Execute a single poll to sync user profile data.\n *\n * The queued accounts are sent to the ProfileMetricsService, and the sync\n * queue is cleared. This operation is mutexed to prevent concurrent\n * executions.\n *\n * @returns A promise that resolves when the poll is complete.\n */\n async _executePoll(): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n if (!this.#assertUserOptedIn()) {\n return;\n }\n this.#setInitialDelayEndTimestampIfNull();\n if (!this.#isInitialDelayComplete()) {\n return;\n }\n for (const [entropySourceId, accounts] of Object.entries(\n this.state.syncQueue,\n )) {\n try {\n await this.messenger.call('ProfileMetricsService:submitMetrics', {\n metametricsId: this.#getMetaMetricsId(),\n entropySourceId:\n entropySourceId === 'null' ? null : entropySourceId,\n accounts,\n });\n this.update((state) => {\n delete state.syncQueue[entropySourceId];\n });\n } catch (error) {\n // We want to log the error but continue processing other batches.\n console.error(\n `Failed to submit profile metrics for entropy source ID ${entropySourceId}:`,\n error,\n );\n }\n }\n });\n }\n\n /**\n * Add existing accounts to the sync queue if it has not been done yet.\n *\n * This method ensures that the first sync is only executed once,\n * and only if the user has opted in to user profile features.\n */\n async #queueFirstSyncIfNeeded(): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n if (this.state.initialEnqueueCompleted) {\n return;\n }\n const newGroupedAccounts = groupAccountsByEntropySourceId(\n Object.values(\n this.messenger.call('AccountsController:getState').internalAccounts\n .accounts,\n ),\n );\n this.update((state) => {\n for (const key of Object.keys(newGroupedAccounts)) {\n if (!state.syncQueue[key]) {\n state.syncQueue[key] = [];\n }\n state.syncQueue[key].push(...newGroupedAccounts[key]);\n }\n state.initialEnqueueCompleted = true;\n });\n });\n }\n\n /**\n * Set the initial delay end timestamp if it is not already set.\n */\n #setInitialDelayEndTimestampIfNull(): void {\n this.update((state) => {\n state.initialDelayEndTimestamp ??=\n Date.now() + this.#initialDelayDuration;\n });\n }\n\n /**\n * Check if the initial delay end timestamp is in the past.\n *\n * @returns True if the initial delay period has completed, false otherwise.\n */\n #isInitialDelayComplete(): boolean {\n return (\n this.state.initialDelayEndTimestamp !== undefined &&\n Date.now() >= this.state.initialDelayEndTimestamp\n );\n }\n\n /**\n * Queue the given account to be synced at the next poll.\n *\n * @param account - The account to sync.\n */\n async #addAccountToQueue(account: InternalAccount): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n this.update((state) => {\n const entropySourceId = getAccountEntropySourceId(account) ?? 'null';\n if (!state.syncQueue[entropySourceId]) {\n state.syncQueue[entropySourceId] = [];\n }\n state.syncQueue[entropySourceId].push({\n address: account.address,\n scopes: account.scopes,\n });\n });\n });\n }\n\n /**\n * Remove the given account from the sync queue.\n *\n * @param account - The account address to remove.\n */\n async #removeAccountFromQueue(account: string): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n this.update((state) => {\n for (const [entropySourceId, groupedAddresses] of Object.entries(\n state.syncQueue,\n )) {\n const index = groupedAddresses.findIndex(\n ({ address }) => address === account,\n );\n if (index === -1) {\n continue;\n }\n groupedAddresses.splice(index, 1);\n if (groupedAddresses.length === 0) {\n delete state.syncQueue[entropySourceId];\n }\n break;\n }\n });\n });\n }\n}\n\n/**\n * Retrieves the entropy source ID from the given account, if it exists.\n *\n * @param account - The account from which to retrieve the entropy source ID.\n * @returns The entropy source ID, or null if it does not exist.\n */\nfunction getAccountEntropySourceId(account: InternalAccount): string | null {\n if (account.options.entropy?.type === 'mnemonic') {\n return account.options.entropy.id;\n }\n return null;\n}\n\n/**\n * Groups accounts by their entropy source ID.\n *\n * @param accounts - The accounts to group.\n * @returns An object where each key is an entropy source ID and each value is\n * an array of account addresses associated with that entropy source ID.\n */\nfunction groupAccountsByEntropySourceId(\n accounts: InternalAccount[],\n): Record<string, AccountWithScopes[]> {\n return accounts.reduce(\n (result: Record<string, AccountWithScopes[]>, account) => {\n const entropySourceId = getAccountEntropySourceId(account);\n const key = entropySourceId ?? 'null';\n if (!result[key]) {\n result[key] = [];\n }\n result[key].push({ address: account.address, scopes: account.scopes });\n return result;\n },\n {},\n );\n}\n"]}
1
+ {"version":3,"file":"ProfileMetricsController.mjs","sourceRoot":"","sources":["../src/ProfileMetricsController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAgBA,OAAO,EAAE,+BAA+B,EAAE,qCAAqC;AAE/E,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,gBAAgB,EAAE,wBAAwB;AAC7E,OAAO,EAAE,KAAK,EAAE,oBAAoB;AASpC,OAAO,EACL,mBAAmB,EACnB,8BAA8B,E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,EAAE,IAAI,KAAK,UAAU,EAAE,CAAC;QACjD,OAAO,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;IACpC,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;GAMG;AACH,SAAS,8BAA8B,CACrC,QAA2B;IAE3B,OAAO,QAAQ,CAAC,MAAM,CACpB,CAAC,MAA2C,EAAE,OAAO,EAAE,EAAE;QACvD,MAAM,eAAe,GAAG,yBAAyB,CAAC,OAAO,CAAC,CAAC;QAC3D,MAAM,GAAG,GAAG,eAAe,IAAI,MAAM,CAAC;QACtC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;YACjB,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;QACnB,CAAC;QACD,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QACvE,OAAO,MAAM,CAAC;IAChB,CAAC,EACD,EAAE,CACH,CAAC;AACJ,CAAC","sourcesContent":["import type {\n AccountsControllerAccountAddedEvent,\n AccountsControllerAccountRemovedEvent,\n AccountsControllerGetStateAction,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport type {\n KeyringControllerLockEvent,\n KeyringControllerUnlockEvent,\n} from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { Messenger } from '@metamask/messenger';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport { TransactionControllerTransactionSubmittedEvent } from '@metamask/transaction-controller';\nimport { Duration, inMilliseconds, parseCaipChainId } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport type { ProfileMetricsControllerMethodActions } from './ProfileMetricsController-method-action-types';\nimport type {\n AccountOwnershipProof,\n AccountWithScopes,\n} from './ProfileMetricsService';\nimport type { ProfileMetricsServiceMethodActions } from './ProfileMetricsService-method-action-types';\nimport type { ProofOfOwnershipServiceMethodActions } from './ProofOfOwnershipService-method-action-types';\nimport {\n canonicalizeAddress,\n ProofUnsupportedNamespaceError,\n} from './utils/canonicalize';\n\n/**\n * The name of the {@link ProfileMetricsController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'ProfileMetricsController';\n\n/**\n * The default delay duration before data is sent for the first time.\n */\nexport const DEFAULT_INITIAL_DELAY_DURATION = inMilliseconds(\n 1,\n Duration.Minute,\n);\n\n/**\n * Describes the shape of the state object for {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerState = {\n /**\n * Whether existing accounts have been added\n * to the queue.\n */\n initialEnqueueCompleted: boolean;\n /**\n * The queue of accounts to be synced.\n * Each key is an entropy source ID, and each value is an array of account\n * addresses associated with that entropy source. Accounts with no entropy\n * source ID are grouped under the key \"null\".\n */\n syncQueue: Record<string, AccountWithScopes[]>;\n /**\n * The timestamp when the first data sending can be attempted.\n */\n initialDelayEndTimestamp?: number;\n /**\n * Whether previously-synced accounts have been re-enqueued so their\n * proofs of ownership are submitted alongside everything else. Set on\n * the first unlock after upgrading to a version that signs proofs of\n * ownership; fresh installs flip this on their initial sync since the\n * first poll already attaches proofs.\n */\n proofBackfillEnqueued: boolean;\n};\n\n/**\n * The metadata for each property in {@link ProfileMetricsControllerState}.\n */\nconst profileMetricsControllerMetadata = {\n initialEnqueueCompleted: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n syncQueue: {\n persist: true,\n includeInDebugSnapshot: false,\n includeInStateLogs: true,\n usedInUi: false,\n },\n initialDelayEndTimestamp: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n proofBackfillEnqueued: {\n persist: true,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<ProfileMetricsControllerState>;\n\n/**\n * Constructs the default {@link ProfileMetricsController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link ProfileMetricsController} state.\n */\nexport function getDefaultProfileMetricsControllerState(): ProfileMetricsControllerState {\n return {\n initialEnqueueCompleted: false,\n syncQueue: {},\n proofBackfillEnqueued: false,\n };\n}\n\nconst MESSENGER_EXPOSED_METHODS = ['skipInitialDelay'] as const;\n\n/**\n * Retrieves the state of the {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n ProfileMetricsControllerState\n>;\n\n/**\n * Actions that {@link ProfileMetricsControllerMessenger} exposes to other consumers.\n */\nexport type ProfileMetricsControllerActions =\n | ProfileMetricsControllerGetStateAction\n | ProfileMetricsControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link ProfileMetricsControllerMessenger} calls.\n */\ntype AllowedActions =\n | ProfileMetricsServiceMethodActions\n | ProofOfOwnershipServiceMethodActions\n | AccountsControllerGetStateAction;\n\n/**\n * Published when the state of {@link ProfileMetricsController} changes.\n */\nexport type ProfileMetricsControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n ProfileMetricsControllerState\n >;\n\n/**\n * Events that {@link ProfileMetricsControllerMessenger} exposes to other consumers.\n */\nexport type ProfileMetricsControllerEvents =\n ProfileMetricsControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link ProfileMetricsControllerMessenger} subscribes\n * to.\n */\ntype AllowedEvents =\n | KeyringControllerUnlockEvent\n | KeyringControllerLockEvent\n | AccountsControllerAccountAddedEvent\n | AccountsControllerAccountRemovedEvent\n | TransactionControllerTransactionSubmittedEvent;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link ProfileMetricsController}.\n */\nexport type ProfileMetricsControllerMessenger = Messenger<\n typeof controllerName,\n ProfileMetricsControllerActions | AllowedActions,\n ProfileMetricsControllerEvents | AllowedEvents\n>;\n\n/**\n * Manages user profile metrics.\n *\n * For users who opt-in to metrics, this controller ensures we have metrics about their user\n * profile (metrics ID and accounts).\n */\nexport class ProfileMetricsController extends StaticIntervalPollingController()<\n typeof controllerName,\n ProfileMetricsControllerState,\n ProfileMetricsControllerMessenger\n> {\n readonly #mutex = new Mutex();\n\n readonly #assertUserOptedIn: () => boolean;\n\n readonly #getMetaMetricsId: () => string;\n\n readonly #initialDelayDuration: number;\n\n /**\n * Constructs a new {@link ProfileMetricsController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n * @param args.assertUserOptedIn - A function that asserts whether the user has\n * opted in to user profile features. If the user has not opted in, sync\n * operations will be no-ops.\n * @param args.getMetaMetricsId - A function that returns the MetaMetrics ID\n * of the user.\n * @param args.interval - The interval, in milliseconds, at which the controller will\n * attempt to send user profile data. Defaults to 10 seconds.\n * @param args.initialDelayDuration - The delay duration before data is sent\n * for the first time, in milliseconds. Defaults to 10 minutes.\n */\n constructor({\n messenger,\n state,\n assertUserOptedIn,\n getMetaMetricsId,\n interval = 10 * 1000,\n initialDelayDuration = DEFAULT_INITIAL_DELAY_DURATION,\n }: {\n messenger: ProfileMetricsControllerMessenger;\n state?: Partial<ProfileMetricsControllerState>;\n interval?: number;\n assertUserOptedIn: () => boolean;\n getMetaMetricsId: () => string;\n initialDelayDuration?: number;\n }) {\n super({\n messenger,\n metadata: profileMetricsControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultProfileMetricsControllerState(),\n ...state,\n },\n });\n\n this.#assertUserOptedIn = assertUserOptedIn;\n this.#getMetaMetricsId = getMetaMetricsId;\n this.#initialDelayDuration = initialDelayDuration;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.messenger.subscribe('KeyringController:unlock', () => {\n if (this.#assertUserOptedIn()) {\n // If the user has already opted in at the start of the session,\n // it must have opted in during onboarding, or during a previous session.\n this.skipInitialDelay();\n }\n this.#enqueueAccountsIfNeeded().catch(\n this.messenger.captureException ?? console.error,\n );\n this.startPolling(null);\n });\n\n this.messenger.subscribe('KeyringController:lock', () =>\n this.stopAllPolling(),\n );\n\n this.messenger.subscribe('TransactionController:transactionSubmitted', () =>\n this.skipInitialDelay(),\n );\n\n this.messenger.subscribe('AccountsController:accountAdded', (account) => {\n this.#addAccountToQueue(account).catch(console.error);\n });\n\n this.messenger.subscribe('AccountsController:accountRemoved', (account) => {\n this.#removeAccountFromQueue(account).catch(console.error);\n });\n\n this.setIntervalLength(interval);\n }\n\n /**\n * Skip the initial delay period by setting the end timestamp to the current time.\n * Metrics will be sent on the next poll.\n */\n skipInitialDelay(): void {\n this.update((state) => {\n state.initialDelayEndTimestamp = Date.now();\n });\n }\n\n /**\n * Execute a single poll to sync user profile data.\n *\n * The queued accounts are sent to the ProfileMetricsService, each with\n * a proof of ownership when one can be produced (see {@link #attachProofs}),\n * and the sync queue is cleared. This operation is mutexed to prevent\n * concurrent executions.\n *\n * @returns A promise that resolves when the poll is complete.\n */\n async _executePoll(): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n if (!this.#assertUserOptedIn()) {\n return;\n }\n this.#setInitialDelayEndTimestampIfNull();\n if (!this.#isInitialDelayComplete()) {\n return;\n }\n const fullAccountsByAddress = this.#getFullAccountsByAddress();\n for (const [entropySourceId, accounts] of Object.entries(\n this.state.syncQueue,\n )) {\n const normalizedEntropySourceId =\n entropySourceId === 'null' ? null : entropySourceId;\n const accountsWithProofs = await this.#attachProofs(\n accounts,\n fullAccountsByAddress,\n normalizedEntropySourceId,\n );\n try {\n await this.messenger.call('ProfileMetricsService:submitMetrics', {\n metametricsId: this.#getMetaMetricsId(),\n entropySourceId: normalizedEntropySourceId,\n accounts: accountsWithProofs,\n });\n this.update((state) => {\n delete state.syncQueue[entropySourceId];\n });\n } catch (error) {\n // We want to log the error but continue processing other batches.\n console.error(\n `Failed to submit profile metrics for entropy source ID ${entropySourceId}:`,\n error,\n );\n }\n }\n });\n }\n\n /**\n * Attach a proof of ownership to each account in a single entropy-source\n * batch when possible, canonicalizing the address along the way.\n *\n * Per-account failures (unknown namespace, snap missing the\n * `signProofOfOwnership` method, snap rejection) and whole-batch nonce\n * failures are caught and downgraded to \"submit without a proof\" so the\n * batch still goes through and the proof is retried on the next poll.\n *\n * @param accounts - The queued accounts for a single batch.\n * @param fullAccountsByAddress - Live `InternalAccount` lookup keyed by address.\n * @param entropySourceId - The entropy source ID for this batch.\n * @returns The accounts with `proof` populated where signing succeeded.\n */\n async #attachProofs(\n accounts: AccountWithScopes[],\n fullAccountsByAddress: Map<string, InternalAccount>,\n entropySourceId: string | null,\n ): Promise<AccountWithScopes[]> {\n const candidates = new Map<\n string,\n { account: InternalAccount; canonicalAddress: string }\n >();\n const identifiers = new Set<string>();\n for (const queued of accounts) {\n const fullAccount = fullAccountsByAddress.get(queued.address);\n if (!fullAccount) {\n continue;\n }\n try {\n const [scope] = fullAccount.scopes;\n if (!scope) {\n throw new Error(`Scope not found for account ${fullAccount.id}`);\n }\n const { namespace } = parseCaipChainId(scope);\n const canonicalAddress = canonicalizeAddress(\n fullAccount.address,\n namespace,\n );\n candidates.set(queued.address, {\n account: fullAccount,\n canonicalAddress,\n });\n identifiers.add(canonicalAddress);\n } catch (error) {\n // Unsupported namespaces are an expected pass-through; anything\n // else is logged so a new namespace doesn't go unnoticed.\n if (!(error instanceof ProofUnsupportedNamespaceError)) {\n console.error(`Skipping proof for account ${fullAccount.id}:`, error);\n }\n }\n }\n\n if (candidates.size === 0) {\n return accounts;\n }\n\n let nonces: Record<string, string> = {};\n try {\n nonces = await this.messenger.call('ProfileMetricsService:fetchNonces', {\n identifiers: [...identifiers],\n entropySourceId,\n });\n } catch (error) {\n console.error(\n `Failed to fetch proof-of-ownership nonces for entropy source ID ${entropySourceId ?? 'null'}:`,\n error,\n );\n }\n\n return await Promise.all(\n accounts.map(async (queued): Promise<AccountWithScopes> => {\n const candidate = candidates.get(queued.address);\n if (!candidate) {\n return queued;\n }\n const nonce = nonces[candidate.canonicalAddress];\n if (!nonce) {\n return { ...queued, address: candidate.canonicalAddress };\n }\n let proof: AccountOwnershipProof;\n try {\n proof = await this.messenger.call('ProofOfOwnershipService:sign', {\n account: candidate.account,\n nonce,\n });\n } catch (error) {\n console.error(\n `Failed to sign proof of ownership for account ${candidate.account.id}:`,\n error,\n );\n return { ...queued, address: candidate.canonicalAddress };\n }\n return {\n address: candidate.canonicalAddress,\n scopes: queued.scopes,\n proof,\n };\n }),\n );\n }\n\n /**\n * Snapshot the live `InternalAccount` map keyed by address for the\n * current poll.\n *\n * @returns A map of address → `InternalAccount`.\n */\n #getFullAccountsByAddress(): Map<string, InternalAccount> {\n const byAddress = new Map<string, InternalAccount>();\n const accountsState = this.messenger.call('AccountsController:getState');\n for (const account of Object.values(\n accountsState.internalAccounts.accounts,\n )) {\n byAddress.set(account.address, account);\n }\n return byAddress;\n }\n\n /**\n * Enqueue all currently-known accounts onto the sync queue if needed.\n * Single entry point covering both the fresh-install first sync and\n * the one-time proof-of-ownership backfill for users upgrading.\n *\n * Bails for opted-out users (the poll wouldn't drain the queue\n * anyway), and bails once both bootstrap steps have already run.\n * Otherwise enqueues all known accounts and flips both flags so this\n * becomes a permanent no-op for the lifetime of the install.\n */\n async #enqueueAccountsIfNeeded(): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n if (!this.#assertUserOptedIn()) {\n return;\n }\n if (\n this.state.initialEnqueueCompleted &&\n this.state.proofBackfillEnqueued\n ) {\n return;\n }\n const groupedAccounts = groupAccountsByEntropySourceId(\n Object.values(\n this.messenger.call('AccountsController:getState').internalAccounts\n .accounts,\n ),\n );\n this.update((state) => {\n // Replace the queue rather than append. `AccountsController` is\n // the source of truth and the queue is otherwise kept in sync\n // with it via the `accountAdded` / `accountRemoved` subscriptions,\n // so assigning here avoids duplicating entries that survived from\n // a prior session or were pushed earlier in this same unlock\n // cycle. Duplicates would matter because nonces are single-use:\n // letting one through causes `#attachProofs` to sign and submit\n // twice with the same nonce.\n state.syncQueue = groupedAccounts;\n state.initialEnqueueCompleted = true;\n state.proofBackfillEnqueued = true;\n });\n });\n }\n\n /**\n * Set the initial delay end timestamp if it is not already set.\n */\n #setInitialDelayEndTimestampIfNull(): void {\n this.update((state) => {\n state.initialDelayEndTimestamp ??=\n Date.now() + this.#initialDelayDuration;\n });\n }\n\n /**\n * Check if the initial delay end timestamp is in the past.\n *\n * @returns True if the initial delay period has completed, false otherwise.\n */\n #isInitialDelayComplete(): boolean {\n return (\n this.state.initialDelayEndTimestamp !== undefined &&\n Date.now() >= this.state.initialDelayEndTimestamp\n );\n }\n\n /**\n * Queue the given account to be synced at the next poll.\n *\n * @param account - The account to sync.\n */\n async #addAccountToQueue(account: InternalAccount): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n this.update((state) => {\n const entropySourceId = getAccountEntropySourceId(account) ?? 'null';\n if (!state.syncQueue[entropySourceId]) {\n state.syncQueue[entropySourceId] = [];\n }\n state.syncQueue[entropySourceId].push({\n address: account.address,\n scopes: account.scopes,\n });\n });\n });\n }\n\n /**\n * Remove the given account from the sync queue.\n *\n * @param account - The account address to remove.\n */\n async #removeAccountFromQueue(account: string): Promise<void> {\n await this.#mutex.runExclusive(async () => {\n this.update((state) => {\n for (const [entropySourceId, groupedAddresses] of Object.entries(\n state.syncQueue,\n )) {\n const index = groupedAddresses.findIndex(\n ({ address }) => address === account,\n );\n if (index === -1) {\n continue;\n }\n groupedAddresses.splice(index, 1);\n if (groupedAddresses.length === 0) {\n delete state.syncQueue[entropySourceId];\n }\n break;\n }\n });\n });\n }\n}\n\n/**\n * Retrieves the entropy source ID from the given account, if it exists.\n *\n * @param account - The account from which to retrieve the entropy source ID.\n * @returns The entropy source ID, or null if it does not exist.\n */\nfunction getAccountEntropySourceId(account: InternalAccount): string | null {\n if (account.options.entropy?.type === 'mnemonic') {\n return account.options.entropy.id;\n }\n return null;\n}\n\n/**\n * Groups accounts by their entropy source ID.\n *\n * @param accounts - The accounts to group.\n * @returns An object where each key is an entropy source ID and each value is\n * an array of account addresses associated with that entropy source ID.\n */\nfunction groupAccountsByEntropySourceId(\n accounts: InternalAccount[],\n): Record<string, AccountWithScopes[]> {\n return accounts.reduce(\n (result: Record<string, AccountWithScopes[]>, account) => {\n const entropySourceId = getAccountEntropySourceId(account);\n const key = entropySourceId ?? 'null';\n if (!result[key]) {\n result[key] = [];\n }\n result[key].push({ address: account.address, scopes: account.scopes });\n return result;\n },\n {},\n );\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/profile-metrics-controller",
3
- "version": "3.2.0-preview-eca6f2051",
3
+ "version": "3.2.0-preview-6da9f2b",
4
4
  "description": "Manages user profile metrics",
5
5
  "keywords": [
6
6
  "Ethereum",