@metamask-previews/notification-services-controller 0.0.0-preview-23a96ae → 0.0.0-preview-e751859

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.
Files changed (40) hide show
  1. package/dist/NotificationServicesController/NotificationServicesController.js +3 -3
  2. package/dist/NotificationServicesController/NotificationServicesController.mjs +2 -2
  3. package/dist/NotificationServicesController/index.js +4 -4
  4. package/dist/NotificationServicesController/index.mjs +3 -3
  5. package/dist/NotificationServicesController/services/onchain-notifications.js +2 -2
  6. package/dist/NotificationServicesController/services/onchain-notifications.mjs +1 -1
  7. package/dist/NotificationServicesPushController/NotificationServicesPushController.js +3 -3
  8. package/dist/NotificationServicesPushController/NotificationServicesPushController.mjs +3 -3
  9. package/dist/NotificationServicesPushController/index.js +5 -5
  10. package/dist/NotificationServicesPushController/index.mjs +4 -4
  11. package/dist/NotificationServicesPushController/services/push/push-web.js +3 -3
  12. package/dist/NotificationServicesPushController/services/push/push-web.mjs +3 -3
  13. package/dist/NotificationServicesPushController/services/services.js +3 -3
  14. package/dist/NotificationServicesPushController/services/services.mjs +3 -3
  15. package/dist/NotificationServicesPushController/utils/get-notification-message.js +3 -3
  16. package/dist/NotificationServicesPushController/utils/get-notification-message.mjs +3 -3
  17. package/dist/NotificationServicesPushController/utils/index.js +3 -3
  18. package/dist/NotificationServicesPushController/utils/index.mjs +3 -3
  19. package/dist/{chunk-ZDG7PIUJ.js → chunk-3M76IZD5.js} +10 -10
  20. package/dist/{chunk-53Y2GNFU.js → chunk-3O5NGZWB.js} +336 -1
  21. package/dist/chunk-3O5NGZWB.js.map +1 -0
  22. package/dist/{chunk-KKWCKF5W.mjs → chunk-4QS664EH.mjs} +2 -2
  23. package/dist/{chunk-F6TSWKZK.mjs → chunk-IG3CMJBW.mjs} +336 -1
  24. package/dist/chunk-IG3CMJBW.mjs.map +1 -0
  25. package/dist/{chunk-AXZU6W3P.js → chunk-LJ533Y4L.js} +7 -7
  26. package/dist/{chunk-6PPEQMF5.mjs → chunk-VNTBP7AI.mjs} +2 -2
  27. package/dist/{chunk-G5RGQ3AG.js → chunk-WYAE7RGD.js} +3 -3
  28. package/dist/{chunk-G2ROCNJN.mjs → chunk-Y7CUC6PM.mjs} +3 -3
  29. package/dist/index.js +5 -5
  30. package/dist/index.mjs +4 -4
  31. package/dist/tsconfig.build.tsbuildinfo +1 -1
  32. package/package.json +1 -1
  33. package/dist/chunk-53Y2GNFU.js.map +0 -1
  34. package/dist/chunk-F6TSWKZK.mjs.map +0 -1
  35. /package/dist/{chunk-ZDG7PIUJ.js.map → chunk-3M76IZD5.js.map} +0 -0
  36. /package/dist/{chunk-KKWCKF5W.mjs.map → chunk-4QS664EH.mjs.map} +0 -0
  37. /package/dist/{chunk-AXZU6W3P.js.map → chunk-LJ533Y4L.js.map} +0 -0
  38. /package/dist/{chunk-6PPEQMF5.mjs.map → chunk-VNTBP7AI.mjs.map} +0 -0
  39. /package/dist/{chunk-G5RGQ3AG.js.map → chunk-WYAE7RGD.js.map} +0 -0
  40. /package/dist/{chunk-G2ROCNJN.mjs.map → chunk-Y7CUC6PM.mjs.map} +0 -0
@@ -10,6 +10,7 @@ var _chunkPIZCE3JDjs = require('./chunk-PIZCE3JD.js');
10
10
 
11
11
 
12
12
 
13
+
13
14
  var _chunkV46WVGWNjs = require('./chunk-V46WVGWN.js');
14
15
 
15
16
  // ../../node_modules/ms/index.js
@@ -4702,13 +4703,48 @@ var SHA256 = class extends HashMD {
4702
4703
  };
4703
4704
  var sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
4704
4705
 
4706
+ // ../../node_modules/@metamask/snaps-utils/dist/chunk-5R7UF7KM.mjs
4707
+ var HandlerType = /* @__PURE__ */ ((HandlerType2) => {
4708
+ HandlerType2["OnRpcRequest"] = "onRpcRequest";
4709
+ HandlerType2["OnSignature"] = "onSignature";
4710
+ HandlerType2["OnTransaction"] = "onTransaction";
4711
+ HandlerType2["OnCronjob"] = "onCronjob";
4712
+ HandlerType2["OnInstall"] = "onInstall";
4713
+ HandlerType2["OnUpdate"] = "onUpdate";
4714
+ HandlerType2["OnNameLookup"] = "onNameLookup";
4715
+ HandlerType2["OnKeyringRequest"] = "onKeyringRequest";
4716
+ HandlerType2["OnHomePage"] = "onHomePage";
4717
+ HandlerType2["OnUserInput"] = "onUserInput";
4718
+ return HandlerType2;
4719
+ })(HandlerType || {});
4720
+ var SNAP_EXPORT_NAMES = Object.values(HandlerType);
4721
+
4722
+ // ../profile-sync-controller/src/controllers/authentication/auth-snap-requests.ts
4723
+ var snapId = "npm:@metamask/message-signing-snap";
4724
+ function createSnapSignMessageRequest(message) {
4725
+ return {
4726
+ snapId,
4727
+ origin: "",
4728
+ handler: HandlerType.OnRpcRequest,
4729
+ request: {
4730
+ method: "signMessage",
4731
+ params: { message }
4732
+ }
4733
+ };
4734
+ }
4735
+
4705
4736
  // ../profile-sync-controller/src/controllers/user-storage/index.ts
4706
4737
  var user_storage_exports = {};
4707
4738
  _chunkV46WVGWNjs.__export.call(void 0, user_storage_exports, {
4739
+ Controller: () => UserStorageController,
4740
+ Encryption: () => encryption_default,
4708
4741
  Mocks: () => fixtures_exports,
4709
4742
  createSHA256Hash: () => createSHA256Hash
4710
4743
  });
4711
4744
 
4745
+ // ../profile-sync-controller/src/controllers/user-storage/UserStorageController.ts
4746
+ var _basecontroller = require('@metamask/base-controller');
4747
+
4712
4748
  // ../../node_modules/@noble/hashes/esm/scrypt.js
4713
4749
  function XorAndSalsa(prev, pi, input, ii, out, oi) {
4714
4750
  let y00 = prev[pi++] ^ input[ii++], y01 = prev[pi++] ^ input[ii++];
@@ -5083,6 +5119,305 @@ function createEntryPath(entryKey, storageKey) {
5083
5119
  var ENV_URLS2 = getEnvUrls("prd" /* PRD */);
5084
5120
  var USER_STORAGE_API = ENV_URLS2.userStorageApiUrl;
5085
5121
  var USER_STORAGE_ENDPOINT = `${USER_STORAGE_API}/api/v1/userstorage`;
5122
+ async function getUserStorage(opts) {
5123
+ try {
5124
+ const path = createEntryPath(opts.entryKey, opts.storageKey);
5125
+ const url = new URL(`${USER_STORAGE_ENDPOINT}${path}`);
5126
+ const userStorageResponse = await fetch(url.toString(), {
5127
+ headers: {
5128
+ "Content-Type": "application/json",
5129
+ Authorization: `Bearer ${opts.bearerToken}`
5130
+ }
5131
+ });
5132
+ if (userStorageResponse.status === 404) {
5133
+ return null;
5134
+ }
5135
+ if (userStorageResponse.status !== 200) {
5136
+ throw new Error("Unable to get User Storage");
5137
+ }
5138
+ const userStorage = await userStorageResponse.json();
5139
+ const encryptedData = userStorage?.Data ?? null;
5140
+ if (!encryptedData) {
5141
+ return null;
5142
+ }
5143
+ const decryptedData = encryption_default2.decryptString(
5144
+ encryptedData,
5145
+ opts.storageKey
5146
+ );
5147
+ return decryptedData;
5148
+ } catch (e) {
5149
+ _loglevel2.default.error("Failed to get user storage", e);
5150
+ return null;
5151
+ }
5152
+ }
5153
+ async function upsertUserStorage(data, opts) {
5154
+ const encryptedData = encryption_default2.encryptString(data, opts.storageKey);
5155
+ const path = createEntryPath(opts.entryKey, opts.storageKey);
5156
+ const url = new URL(`${USER_STORAGE_ENDPOINT}${path}`);
5157
+ const res = await fetch(url.toString(), {
5158
+ method: "PUT",
5159
+ headers: {
5160
+ "Content-Type": "application/json",
5161
+ Authorization: `Bearer ${opts.bearerToken}`
5162
+ },
5163
+ body: JSON.stringify({ data: encryptedData })
5164
+ });
5165
+ if (!res.ok) {
5166
+ throw new Error("user-storage - unable to upsert data");
5167
+ }
5168
+ }
5169
+
5170
+ // ../profile-sync-controller/src/controllers/user-storage/UserStorageController.ts
5171
+ var controllerName = "UserStorageController";
5172
+ var defaultState = {
5173
+ isProfileSyncingEnabled: true,
5174
+ isProfileSyncingUpdateLoading: false
5175
+ };
5176
+ var metadata = {
5177
+ isProfileSyncingEnabled: {
5178
+ persist: true,
5179
+ anonymous: true
5180
+ },
5181
+ isProfileSyncingUpdateLoading: {
5182
+ persist: false,
5183
+ anonymous: false
5184
+ }
5185
+ };
5186
+ var _auth, _notificationServices, _registerMessageHandlers, registerMessageHandlers_fn, _assertProfileSyncingEnabled, assertProfileSyncingEnabled_fn, _getStorageKeyAndBearerToken, getStorageKeyAndBearerToken_fn, _createStorageKey, createStorageKey_fn, _snapSignMessage, snapSignMessage_fn, _setIsProfileSyncingUpdateLoading, setIsProfileSyncingUpdateLoading_fn;
5187
+ var UserStorageController = class extends _basecontroller.BaseController {
5188
+ constructor(params) {
5189
+ super({
5190
+ messenger: params.messenger,
5191
+ metadata,
5192
+ name: controllerName,
5193
+ state: { ...defaultState, ...params.state }
5194
+ });
5195
+ /**
5196
+ * Constructor helper for registering this controller's messaging system
5197
+ * actions.
5198
+ */
5199
+ _chunkV46WVGWNjs.__privateAdd.call(void 0, this, _registerMessageHandlers);
5200
+ _chunkV46WVGWNjs.__privateAdd.call(void 0, this, _assertProfileSyncingEnabled);
5201
+ /**
5202
+ * Utility to get the bearer token and storage key
5203
+ */
5204
+ _chunkV46WVGWNjs.__privateAdd.call(void 0, this, _getStorageKeyAndBearerToken);
5205
+ /**
5206
+ * Rather than storing the storage key, we can compute the storage key when needed.
5207
+ *
5208
+ * @returns the storage key
5209
+ */
5210
+ _chunkV46WVGWNjs.__privateAdd.call(void 0, this, _createStorageKey);
5211
+ /**
5212
+ * Signs a specific message using an underlying auth snap.
5213
+ *
5214
+ * @param message - A specific tagged message to sign.
5215
+ * @returns A Signature created by the snap.
5216
+ */
5217
+ _chunkV46WVGWNjs.__privateAdd.call(void 0, this, _snapSignMessage);
5218
+ _chunkV46WVGWNjs.__privateAdd.call(void 0, this, _setIsProfileSyncingUpdateLoading);
5219
+ _chunkV46WVGWNjs.__privateAdd.call(void 0, this, _auth, {
5220
+ getBearerToken: async () => {
5221
+ return await this.messagingSystem.call(
5222
+ "AuthenticationController:getBearerToken"
5223
+ );
5224
+ },
5225
+ getProfileId: async () => {
5226
+ const sessionProfile = await this.messagingSystem.call(
5227
+ "AuthenticationController:getSessionProfile"
5228
+ );
5229
+ return sessionProfile?.profileId;
5230
+ },
5231
+ isAuthEnabled: () => {
5232
+ return this.messagingSystem.call("AuthenticationController:isSignedIn");
5233
+ },
5234
+ signIn: async () => {
5235
+ return await this.messagingSystem.call(
5236
+ "AuthenticationController:performSignIn"
5237
+ );
5238
+ },
5239
+ signOut: async () => {
5240
+ return this.messagingSystem.call(
5241
+ "AuthenticationController:performSignOut"
5242
+ );
5243
+ }
5244
+ });
5245
+ _chunkV46WVGWNjs.__privateAdd.call(void 0, this, _notificationServices, {
5246
+ disableNotificationServices: async () => {
5247
+ return await this.messagingSystem.call(
5248
+ "NotificationServicesController:disableNotificationServices"
5249
+ );
5250
+ },
5251
+ selectIsNotificationServicesEnabled: async () => {
5252
+ return this.messagingSystem.call(
5253
+ "NotificationServicesController:selectIsNotificationServicesEnabled"
5254
+ );
5255
+ }
5256
+ });
5257
+ this.getMetaMetricsState = params.getMetaMetricsState;
5258
+ _chunkV46WVGWNjs.__privateMethod.call(void 0, this, _registerMessageHandlers, registerMessageHandlers_fn).call(this);
5259
+ }
5260
+ async enableProfileSyncing() {
5261
+ try {
5262
+ _chunkV46WVGWNjs.__privateMethod.call(void 0, this, _setIsProfileSyncingUpdateLoading, setIsProfileSyncingUpdateLoading_fn).call(this, true);
5263
+ const authEnabled = _chunkV46WVGWNjs.__privateGet.call(void 0, this, _auth).isAuthEnabled();
5264
+ if (!authEnabled) {
5265
+ await _chunkV46WVGWNjs.__privateGet.call(void 0, this, _auth).signIn();
5266
+ }
5267
+ this.update((state) => {
5268
+ state.isProfileSyncingEnabled = true;
5269
+ });
5270
+ _chunkV46WVGWNjs.__privateMethod.call(void 0, this, _setIsProfileSyncingUpdateLoading, setIsProfileSyncingUpdateLoading_fn).call(this, false);
5271
+ } catch (e) {
5272
+ _chunkV46WVGWNjs.__privateMethod.call(void 0, this, _setIsProfileSyncingUpdateLoading, setIsProfileSyncingUpdateLoading_fn).call(this, false);
5273
+ const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);
5274
+ throw new Error(
5275
+ `${controllerName} - failed to enable profile syncing - ${errorMessage}`
5276
+ );
5277
+ }
5278
+ }
5279
+ async setIsProfileSyncingEnabled(isProfileSyncingEnabled) {
5280
+ this.update((state) => {
5281
+ state.isProfileSyncingEnabled = isProfileSyncingEnabled;
5282
+ });
5283
+ }
5284
+ async disableProfileSyncing() {
5285
+ const isAlreadyDisabled = !this.state.isProfileSyncingEnabled;
5286
+ if (isAlreadyDisabled) {
5287
+ return;
5288
+ }
5289
+ try {
5290
+ _chunkV46WVGWNjs.__privateMethod.call(void 0, this, _setIsProfileSyncingUpdateLoading, setIsProfileSyncingUpdateLoading_fn).call(this, true);
5291
+ const isNotificationServicesEnabled = await _chunkV46WVGWNjs.__privateGet.call(void 0, this, _notificationServices).selectIsNotificationServicesEnabled();
5292
+ if (isNotificationServicesEnabled) {
5293
+ await _chunkV46WVGWNjs.__privateGet.call(void 0, this, _notificationServices).disableNotificationServices();
5294
+ }
5295
+ const isMetaMetricsParticipation = this.getMetaMetricsState();
5296
+ if (!isMetaMetricsParticipation) {
5297
+ this.messagingSystem.call("AuthenticationController:performSignOut");
5298
+ }
5299
+ _chunkV46WVGWNjs.__privateMethod.call(void 0, this, _setIsProfileSyncingUpdateLoading, setIsProfileSyncingUpdateLoading_fn).call(this, false);
5300
+ this.update((state) => {
5301
+ state.isProfileSyncingEnabled = false;
5302
+ });
5303
+ } catch (e) {
5304
+ _chunkV46WVGWNjs.__privateMethod.call(void 0, this, _setIsProfileSyncingUpdateLoading, setIsProfileSyncingUpdateLoading_fn).call(this, false);
5305
+ const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);
5306
+ throw new Error(
5307
+ `${controllerName} - failed to disable profile syncing - ${errorMessage}`
5308
+ );
5309
+ }
5310
+ }
5311
+ /**
5312
+ * Allows retrieval of stored data. Data stored is string formatted.
5313
+ * Developers can extend the entry path and entry name through the `schema.ts` file.
5314
+ *
5315
+ * @param entryKey - entry key that matches schema
5316
+ * @returns the decrypted string contents found from user storage (or null if not found)
5317
+ */
5318
+ async performGetStorage(entryKey) {
5319
+ _chunkV46WVGWNjs.__privateMethod.call(void 0, this, _assertProfileSyncingEnabled, assertProfileSyncingEnabled_fn).call(this);
5320
+ const { bearerToken, storageKey } = await _chunkV46WVGWNjs.__privateMethod.call(void 0, this, _getStorageKeyAndBearerToken, getStorageKeyAndBearerToken_fn).call(this);
5321
+ const result = await getUserStorage({
5322
+ entryKey,
5323
+ bearerToken,
5324
+ storageKey
5325
+ });
5326
+ return result;
5327
+ }
5328
+ /**
5329
+ * Allows storage of user data. Data stored must be string formatted.
5330
+ * Developers can extend the entry path and entry name through the `schema.ts` file.
5331
+ *
5332
+ * @param entryKey - entry key that matches schema
5333
+ * @param value - The string data you want to store.
5334
+ * @returns nothing. NOTE that an error is thrown if fails to store data.
5335
+ */
5336
+ async performSetStorage(entryKey, value) {
5337
+ _chunkV46WVGWNjs.__privateMethod.call(void 0, this, _assertProfileSyncingEnabled, assertProfileSyncingEnabled_fn).call(this);
5338
+ const { bearerToken, storageKey } = await _chunkV46WVGWNjs.__privateMethod.call(void 0, this, _getStorageKeyAndBearerToken, getStorageKeyAndBearerToken_fn).call(this);
5339
+ await upsertUserStorage(value, {
5340
+ entryKey,
5341
+ bearerToken,
5342
+ storageKey
5343
+ });
5344
+ }
5345
+ /**
5346
+ * Retrieves the storage key, for internal use only!
5347
+ *
5348
+ * @returns the storage key
5349
+ */
5350
+ async getStorageKey() {
5351
+ _chunkV46WVGWNjs.__privateMethod.call(void 0, this, _assertProfileSyncingEnabled, assertProfileSyncingEnabled_fn).call(this);
5352
+ const storageKey = await _chunkV46WVGWNjs.__privateMethod.call(void 0, this, _createStorageKey, createStorageKey_fn).call(this);
5353
+ return storageKey;
5354
+ }
5355
+ };
5356
+ _auth = new WeakMap();
5357
+ _notificationServices = new WeakMap();
5358
+ _registerMessageHandlers = new WeakSet();
5359
+ registerMessageHandlers_fn = function() {
5360
+ this.messagingSystem.registerActionHandler(
5361
+ "UserStorageController:performGetStorage",
5362
+ this.performGetStorage.bind(this)
5363
+ );
5364
+ this.messagingSystem.registerActionHandler(
5365
+ "UserStorageController:performSetStorage",
5366
+ this.performSetStorage.bind(this)
5367
+ );
5368
+ this.messagingSystem.registerActionHandler(
5369
+ "UserStorageController:getStorageKey",
5370
+ this.getStorageKey.bind(this)
5371
+ );
5372
+ this.messagingSystem.registerActionHandler(
5373
+ "UserStorageController:enableProfileSyncing",
5374
+ this.enableProfileSyncing.bind(this)
5375
+ );
5376
+ this.messagingSystem.registerActionHandler(
5377
+ "UserStorageController:disableProfileSyncing",
5378
+ this.disableProfileSyncing.bind(this)
5379
+ );
5380
+ };
5381
+ _assertProfileSyncingEnabled = new WeakSet();
5382
+ assertProfileSyncingEnabled_fn = function() {
5383
+ if (!this.state.isProfileSyncingEnabled) {
5384
+ throw new Error(
5385
+ `${controllerName}: Unable to call method, user is not authenticated`
5386
+ );
5387
+ }
5388
+ };
5389
+ _getStorageKeyAndBearerToken = new WeakSet();
5390
+ getStorageKeyAndBearerToken_fn = async function() {
5391
+ const bearerToken = await _chunkV46WVGWNjs.__privateGet.call(void 0, this, _auth).getBearerToken();
5392
+ if (!bearerToken) {
5393
+ throw new Error("UserStorageController - unable to get bearer token");
5394
+ }
5395
+ const storageKey = await _chunkV46WVGWNjs.__privateMethod.call(void 0, this, _createStorageKey, createStorageKey_fn).call(this);
5396
+ return { bearerToken, storageKey };
5397
+ };
5398
+ _createStorageKey = new WeakSet();
5399
+ createStorageKey_fn = async function() {
5400
+ const id = await _chunkV46WVGWNjs.__privateGet.call(void 0, this, _auth).getProfileId();
5401
+ if (!id) {
5402
+ throw new Error("UserStorageController - unable to create storage key");
5403
+ }
5404
+ const storageKeySignature = await _chunkV46WVGWNjs.__privateMethod.call(void 0, this, _snapSignMessage, snapSignMessage_fn).call(this, `metamask:${id}`);
5405
+ const storageKey = createSHA256Hash(storageKeySignature);
5406
+ return storageKey;
5407
+ };
5408
+ _snapSignMessage = new WeakSet();
5409
+ snapSignMessage_fn = function(message) {
5410
+ return this.messagingSystem.call(
5411
+ "SnapController:handleRequest",
5412
+ createSnapSignMessageRequest(message)
5413
+ );
5414
+ };
5415
+ _setIsProfileSyncingUpdateLoading = new WeakSet();
5416
+ setIsProfileSyncingUpdateLoading_fn = function(isProfileSyncingUpdateLoading) {
5417
+ this.update((state) => {
5418
+ state.isProfileSyncingUpdateLoading = isProfileSyncingUpdateLoading;
5419
+ });
5420
+ };
5086
5421
 
5087
5422
  // ../profile-sync-controller/src/controllers/user-storage/__fixtures__/index.ts
5088
5423
  var fixtures_exports = {};
@@ -5336,4 +5671,4 @@ exports.require_nock = require_nock; exports.TRIGGER_API = TRIGGER_API; exports.
5336
5671
  @noble/hashes/esm/utils.js:
5337
5672
  (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
5338
5673
  */
5339
- //# sourceMappingURL=chunk-53Y2GNFU.js.map
5674
+ //# sourceMappingURL=chunk-3O5NGZWB.js.map