@metamask/snaps-controllers 3.2.0 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [3.3.0]
10
+ ### Added
11
+ - Add manifest localization functionality ([#1889](https://github.com/MetaMask/snaps/pull/1889))
12
+ - Add support for unencrypted storage using `snap_manageState` ([#1902](https://github.com/MetaMask/snaps/pull/1902))
13
+ - Add `OnHomePage` export ([#1896](https://github.com/MetaMask/snaps/pull/1896))
14
+
9
15
  ## [3.2.0]
10
16
  ### Added
11
17
  - Add support for links in custom UI and notifications ([#1814](https://github.com/MetaMask/snaps/pull/1814))
@@ -101,7 +107,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
101
107
  - The version of the package no longer needs to match the version of all other
102
108
  MetaMask Snaps packages.
103
109
 
104
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@3.2.0...HEAD
110
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@3.3.0...HEAD
111
+ [3.3.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@3.2.0...@metamask/snaps-controllers@3.3.0
105
112
  [3.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@3.1.1...@metamask/snaps-controllers@3.2.0
106
113
  [3.1.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@3.1.0...@metamask/snaps-controllers@3.1.1
107
114
  [3.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@3.0.0...@metamask/snaps-controllers@3.1.0
@@ -121,7 +121,8 @@ const TRUNCATED_SNAP_PROPERTIES = new Set([
121
121
  ]);
122
122
  const defaultState = {
123
123
  snaps: {},
124
- snapStates: {}
124
+ snapStates: {},
125
+ unencryptedSnapStates: {}
125
126
  };
126
127
  /**
127
128
  * Truncates the properties of a snap to only ones that are easily serializable.
@@ -193,7 +194,7 @@ _initializeStateMachine = /*#__PURE__*/ new WeakSet(), /**
193
194
  *
194
195
  * @param snapId - The id of the Snap whose message handler to get.
195
196
  * @returns The RPC handler for the given snap.
196
- */ _getRpcRequestHandler = /*#__PURE__*/ new WeakSet(), _assertSnapRpcRequestResult = /*#__PURE__*/ new WeakSet(), _executeWithTimeout = /*#__PURE__*/ new WeakSet(), _recordSnapRpcRequestStart = /*#__PURE__*/ new WeakSet(), _recordSnapRpcRequestFinish = /*#__PURE__*/ new WeakSet(), /**
197
+ */ _getRpcRequestHandler = /*#__PURE__*/ new WeakSet(), _triggerPhishingListUpdate = /*#__PURE__*/ new WeakSet(), _checkPhishingList = /*#__PURE__*/ new WeakSet(), _assertSnapRpcRequestResult = /*#__PURE__*/ new WeakSet(), _executeWithTimeout = /*#__PURE__*/ new WeakSet(), _recordSnapRpcRequestStart = /*#__PURE__*/ new WeakSet(), _recordSnapRpcRequestFinish = /*#__PURE__*/ new WeakSet(), /**
197
198
  * Retrieves the rollback snapshot of a snap.
198
199
  *
199
200
  * @param snapId - The snap id.
@@ -402,9 +403,14 @@ class SnapController extends _basecontroller.BaseControllerV2 {
402
403
  *
403
404
  * @param snapId - The id of the Snap whose state should be updated.
404
405
  * @param newSnapState - The new state of the snap.
405
- */ async updateSnapState(snapId, newSnapState) {
406
+ * @param encrypted - A flag to indicate whether to use encrypted storage or not.
407
+ */ updateSnapState(snapId, newSnapState, encrypted) {
406
408
  this.update((state)=>{
407
- state.snapStates[snapId] = newSnapState;
409
+ if (encrypted) {
410
+ state.snapStates[snapId] = newSnapState;
411
+ } else {
412
+ state.unencryptedSnapStates[snapId] = newSnapState;
413
+ }
408
414
  });
409
415
  }
410
416
  /**
@@ -412,9 +418,14 @@ class SnapController extends _basecontroller.BaseControllerV2 {
412
418
  * This is distinct from the state MetaMask uses to manage snaps.
413
419
  *
414
420
  * @param snapId - The id of the Snap whose state should be cleared.
415
- */ clearSnapState(snapId) {
421
+ * @param encrypted - A flag to indicate whether to use encrypted storage or not.
422
+ */ clearSnapState(snapId, encrypted) {
416
423
  this.update((state)=>{
417
- state.snapStates[snapId] = null;
424
+ if (encrypted) {
425
+ state.snapStates[snapId] = null;
426
+ } else {
427
+ state.unencryptedSnapStates[snapId] = null;
428
+ }
418
429
  });
419
430
  }
420
431
  /**
@@ -422,10 +433,10 @@ class SnapController extends _basecontroller.BaseControllerV2 {
422
433
  * This is distinct from the state MetaMask uses to manage snaps.
423
434
  *
424
435
  * @param snapId - The id of the Snap whose state to get.
425
- * @returns A promise that resolves with the decrypted snap state or null if no state exists.
426
- * @throws If the snap state decryption fails.
427
- */ async getSnapState(snapId) {
428
- const state = this.state.snapStates[snapId];
436
+ * @param encrypted - A flag to indicate whether to use encrypted storage or not.
437
+ * @returns The requested snap state or null if no state exists.
438
+ */ getSnapState(snapId, encrypted) {
439
+ const state = encrypted ? this.state.snapStates[snapId] : this.state.unencryptedSnapStates[snapId];
429
440
  return state ?? null;
430
441
  }
431
442
  /**
@@ -942,6 +953,10 @@ class SnapController extends _basecontroller.BaseControllerV2 {
942
953
  persist: true,
943
954
  anonymous: false
944
955
  },
956
+ unencryptedSnapStates: {
957
+ persist: true,
958
+ anonymous: false
959
+ },
945
960
  snaps: {
946
961
  persist: (snaps)=>{
947
962
  return Object.values(snaps)// We should not persist snaps that are in the installing state,
@@ -1021,6 +1036,8 @@ class SnapController extends _basecontroller.BaseControllerV2 {
1021
1036
  */ _class_private_method_init(this, _fetchSnap);
1022
1037
  _class_private_method_init(this, _validateSnapPermissions);
1023
1038
  _class_private_method_init(this, _getRpcRequestHandler);
1039
+ _class_private_method_init(this, _triggerPhishingListUpdate);
1040
+ _class_private_method_init(this, _checkPhishingList);
1024
1041
  /**
1025
1042
  * Asserts that the returned result of a Snap RPC call is the expected shape.
1026
1043
  *
@@ -1219,11 +1236,11 @@ function initializeStateMachine() {
1219
1236
  function registerMessageHandlers() {
1220
1237
  this.messagingSystem.registerActionHandler(`${controllerName}:clearSnapState`, (...args)=>this.clearSnapState(...args));
1221
1238
  this.messagingSystem.registerActionHandler(`${controllerName}:get`, (...args)=>this.get(...args));
1222
- this.messagingSystem.registerActionHandler(`${controllerName}:getSnapState`, async (...args)=>this.getSnapState(...args));
1239
+ this.messagingSystem.registerActionHandler(`${controllerName}:getSnapState`, (...args)=>this.getSnapState(...args));
1223
1240
  this.messagingSystem.registerActionHandler(`${controllerName}:handleRequest`, async (...args)=>this.handleRequest(...args));
1224
1241
  this.messagingSystem.registerActionHandler(`${controllerName}:has`, (...args)=>this.has(...args));
1225
1242
  this.messagingSystem.registerActionHandler(`${controllerName}:updateBlockedSnaps`, async ()=>this.updateBlockedSnaps());
1226
- this.messagingSystem.registerActionHandler(`${controllerName}:updateSnapState`, async (...args)=>this.updateSnapState(...args));
1243
+ this.messagingSystem.registerActionHandler(`${controllerName}:updateSnapState`, (...args)=>this.updateSnapState(...args));
1227
1244
  this.messagingSystem.registerActionHandler(`${controllerName}:enable`, (...args)=>this.enableSnap(...args));
1228
1245
  this.messagingSystem.registerActionHandler(`${controllerName}:disable`, async (...args)=>this.disableSnap(...args));
1229
1246
  this.messagingSystem.registerActionHandler(`${controllerName}:remove`, async (...args)=>this.removeSnap(...args));
@@ -1432,7 +1449,7 @@ async function getEndowments(snapId) {
1432
1449
  }
1433
1450
  function set(args) {
1434
1451
  const { id: snapId, origin, files, isUpdate = false } = args;
1435
- const { manifest, sourceCode: sourceCodeFile, svgIcon, auxiliaryFiles: rawAuxiliaryFiles } = files;
1452
+ const { manifest, sourceCode: sourceCodeFile, svgIcon, auxiliaryFiles: rawAuxiliaryFiles, localizationFiles } = files;
1436
1453
  (0, _snapsutils.assertIsSnapManifest)(manifest.result);
1437
1454
  const { version } = manifest.result;
1438
1455
  const sourceCode = sourceCodeFile.toString();
@@ -1466,7 +1483,8 @@ function set(args) {
1466
1483
  sourceCode,
1467
1484
  version,
1468
1485
  versionHistory,
1469
- auxiliaryFiles
1486
+ auxiliaryFiles,
1487
+ localizationFiles: localizationFiles.map((file)=>file.result)
1470
1488
  };
1471
1489
  // If the snap was blocked, it isn't any longer
1472
1490
  delete snap.blockInformation;
@@ -1494,12 +1512,15 @@ async function fetchSnap(snapId, location) {
1494
1512
  const sourceCode = await location.fetch(manifest.result.source.location.npm.filePath);
1495
1513
  const { iconPath } = manifest.result.source.location.npm;
1496
1514
  const svgIcon = iconPath ? await location.fetch(iconPath) : undefined;
1497
- const auxiliaryFiles = manifest.result.source.files ? await Promise.all(manifest.result.source.files.map(async (filePath)=>location.fetch(filePath))) : [];
1515
+ const auxiliaryFiles = await (0, _utils1.getSnapFiles)(location, manifest.result.source.files);
1516
+ const localizationFiles = await (0, _utils1.getSnapFiles)(location, manifest.result.source.locales);
1517
+ const validatedLocalizationFiles = (0, _snapsutils.getValidatedLocalizationFiles)(localizationFiles);
1498
1518
  const files = {
1499
1519
  manifest,
1500
1520
  sourceCode,
1501
1521
  svgIcon,
1502
- auxiliaryFiles
1522
+ auxiliaryFiles,
1523
+ localizationFiles: validatedLocalizationFiles
1503
1524
  };
1504
1525
  (0, _snapsutils.validateFetchedSnap)(files);
1505
1526
  return {
@@ -1583,12 +1604,23 @@ function getRpcRequestHandler(snapId) {
1583
1604
  runtime.rpcHandler = rpcHandler;
1584
1605
  return rpcHandler;
1585
1606
  }
1607
+ async function triggerPhishingListUpdate() {
1608
+ return this.messagingSystem.call('PhishingController:maybeUpdateState');
1609
+ }
1610
+ function checkPhishingList(origin) {
1611
+ return this.messagingSystem.call('PhishingController:testOrigin', origin).result;
1612
+ }
1586
1613
  async function assertSnapRpcRequestResult(handlerType, result) {
1587
1614
  switch(handlerType){
1588
1615
  case _snapsutils.HandlerType.OnTransaction:
1589
1616
  (0, _utils.assertStruct)(result, _snapsutils.OnTransactionResponseStruct);
1590
- await this.messagingSystem.call('PhishingController:maybeUpdateState');
1591
- await (0, _snapsui.assertUILinksAreSafe)(result.content, (url)=>this.messagingSystem.call('PhishingController:testOrigin', url).result);
1617
+ await _class_private_method_get(this, _triggerPhishingListUpdate, triggerPhishingListUpdate).call(this);
1618
+ (0, _snapsui.assertUILinksAreSafe)(result.content, _class_private_method_get(this, _checkPhishingList, checkPhishingList).bind(this));
1619
+ break;
1620
+ case _snapsutils.HandlerType.OnHomePage:
1621
+ (0, _utils.assertStruct)(result, _snapsutils.OnHomePageResponseStruct);
1622
+ await _class_private_method_get(this, _triggerPhishingListUpdate, triggerPhishingListUpdate).call(this);
1623
+ (0, _snapsui.assertUILinksAreSafe)(result.content, _class_private_method_get(this, _checkPhishingList, checkPhishingList).bind(this));
1592
1624
  break;
1593
1625
  default:
1594
1626
  break;