@metamask/snaps-controllers 0.35.2-flask.1 → 0.36.1-flask.1

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,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.36.1-flask.1]
10
+ ### Changed
11
+ - No changes this release.
12
+
13
+ ## [0.36.0-flask.1]
14
+ ### Changed
15
+ - Allow omitting `jsonrpc` and `id` in `handleRequest` ([#1556](https://github.com/MetaMask/snaps/pull/1556))
16
+ - Unblock `wallet_requestSnaps` and add dynamic permission support ([#1421](https://github.com/MetaMask/snaps/pull/1421))
17
+
9
18
  ## [0.35.2-flask.1]
10
19
  ### Fixed
11
20
  - Fix type issue introduced by [#1532](https://github.com/MetaMask/snaps/pull/1532) ([#1541](https://github.com/MetaMask/snaps/pull/1541))
@@ -542,7 +551,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
542
551
  ### Added
543
552
  - First semi-stable release.
544
553
 
545
- [Unreleased]: https://github.com/MetaMask/snaps/compare/v0.35.2-flask.1...HEAD
554
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/v0.36.1-flask.1...HEAD
555
+ [0.36.1-flask.1]: https://github.com/MetaMask/snaps/compare/v0.36.0-flask.1...v0.36.1-flask.1
556
+ [0.36.0-flask.1]: https://github.com/MetaMask/snaps/compare/v0.35.2-flask.1...v0.36.0-flask.1
546
557
  [0.35.2-flask.1]: https://github.com/MetaMask/snaps/compare/v0.35.1-flask.1...v0.35.2-flask.1
547
558
  [0.35.1-flask.1]: https://github.com/MetaMask/snaps/compare/v0.35.0-flask.1...v0.35.1-flask.1
548
559
  [0.35.0-flask.1]: https://github.com/MetaMask/snaps/compare/v0.34.1-flask.1...v0.35.0-flask.1
@@ -138,7 +138,7 @@ const defaultState = {
138
138
  return truncatedSnap;
139
139
  }
140
140
  const name = 'SnapController';
141
- var _closeAllConnections = /*#__PURE__*/ new WeakMap(), _environmentEndowmentPermissions = /*#__PURE__*/ new WeakMap(), _excludedPermissions = /*#__PURE__*/ new WeakMap(), _featureFlags = /*#__PURE__*/ new WeakMap(), _fetchFunction = /*#__PURE__*/ new WeakMap(), _idleTimeCheckInterval = /*#__PURE__*/ new WeakMap(), _maxIdleTime = /*#__PURE__*/ new WeakMap(), _detectSnapLocation = /*#__PURE__*/ new WeakMap(), _rollbackSnapshots = /*#__PURE__*/ new WeakMap(), _timeoutForLastRequestStatus = /*#__PURE__*/ new WeakMap(), _statusMachine = /*#__PURE__*/ new WeakMap(), /**
141
+ var _closeAllConnections = /*#__PURE__*/ new WeakMap(), _dynamicPermissions = /*#__PURE__*/ new WeakMap(), _environmentEndowmentPermissions = /*#__PURE__*/ new WeakMap(), _excludedPermissions = /*#__PURE__*/ new WeakMap(), _featureFlags = /*#__PURE__*/ new WeakMap(), _fetchFunction = /*#__PURE__*/ new WeakMap(), _idleTimeCheckInterval = /*#__PURE__*/ new WeakMap(), _maxIdleTime = /*#__PURE__*/ new WeakMap(), _detectSnapLocation = /*#__PURE__*/ new WeakMap(), _rollbackSnapshots = /*#__PURE__*/ new WeakMap(), _timeoutForLastRequestStatus = /*#__PURE__*/ new WeakMap(), _statusMachine = /*#__PURE__*/ new WeakMap(), /**
142
142
  * We track status of a Snap using a finite-state-machine.
143
143
  * It keeps track of whether the snap is started / stopped / etc.
144
144
  *
@@ -170,7 +170,11 @@ _initializeStateMachine = /*#__PURE__*/ new WeakSet(), /**
170
170
  * Removes a snap's permission (caveat) from all subjects.
171
171
  *
172
172
  * @param snapId - The id of the Snap.
173
- */ _removeSnapFromSubjects = /*#__PURE__*/ new WeakSet(), _createApproval = /*#__PURE__*/ new WeakSet(), _updateApproval = /*#__PURE__*/ new WeakSet(), _add = /*#__PURE__*/ new WeakSet(), _startSnap = /*#__PURE__*/ new WeakSet(), _getEndowments = /*#__PURE__*/ new WeakSet(), /**
173
+ */ _removeSnapFromSubjects = /*#__PURE__*/ new WeakSet(), /**
174
+ * Safely revokes all permissions granted to a Snap.
175
+ *
176
+ * @param snapId - The snap ID.
177
+ */ _revokeAllSnapPermissions = /*#__PURE__*/ new WeakSet(), _createApproval = /*#__PURE__*/ new WeakSet(), _updateApproval = /*#__PURE__*/ new WeakSet(), _add = /*#__PURE__*/ new WeakSet(), _startSnap = /*#__PURE__*/ new WeakSet(), _getEndowments = /*#__PURE__*/ new WeakSet(), /**
174
178
  * Sets a snap in state. Called when a snap is installed or updated. Performs
175
179
  * various validation checks on the received arguments, and will throw if
176
180
  * validation fails.
@@ -458,7 +462,7 @@ class SnapController extends _basecontroller.BaseControllerV2 {
458
462
  _class_private_field_get(this, _closeAllConnections).call(this, snapId);
459
463
  });
460
464
  await this.messagingSystem.call('ExecutionService:terminateAllSnaps');
461
- snapIds.forEach((snapId)=>this.revokeAllSnapPermissions(snapId));
465
+ snapIds.forEach((snapId)=>_class_private_method_get(this, _revokeAllSnapPermissions, revokeAllSnapPermissions).call(this, snapId));
462
466
  this.update((state)=>{
463
467
  state.snaps = {};
464
468
  state.snapStates = {};
@@ -490,7 +494,7 @@ class SnapController extends _basecontroller.BaseControllerV2 {
490
494
  // it. This ensures that the snap will not be restarted or otherwise
491
495
  // affect the host environment while we are deleting it.
492
496
  await this.disableSnap(snapId);
493
- this.revokeAllSnapPermissions(snapId);
497
+ _class_private_method_get(this, _revokeAllSnapPermissions, revokeAllSnapPermissions).call(this, snapId);
494
498
  _class_private_method_get(this, _removeSnapFromSubjects, removeSnapFromSubjects).call(this, snapId);
495
499
  this.snapsRuntimeData.delete(snapId);
496
500
  this.update((state)=>{
@@ -529,13 +533,16 @@ class SnapController extends _basecontroller.BaseControllerV2 {
529
533
  }
530
534
  }
531
535
  /**
532
- * Safely revokes all permissions granted to a Snap.
536
+ * Checks if a list of permissions are dynamic and allowed to be revoked, if they are they will all be revoked.
533
537
  *
534
538
  * @param snapId - The snap ID.
535
- */ revokeAllSnapPermissions(snapId) {
536
- if (this.messagingSystem.call('PermissionController:hasPermissions', snapId)) {
537
- this.messagingSystem.call('PermissionController:revokeAllPermissions', snapId);
538
- }
539
+ * @param permissionNames - The names of the permissions.
540
+ * @throws If non-dynamic permissions are passed.
541
+ */ revokeDynamicSnapPermissions(snapId, permissionNames) {
542
+ (0, _utils.assert)(permissionNames.every((permissionName)=>_class_private_field_get(this, _dynamicPermissions).includes(permissionName)), 'Non-dynamic permissions cannot be revoked');
543
+ this.messagingSystem.call('PermissionController:revokePermissions', {
544
+ [snapId]: permissionNames
545
+ });
539
546
  }
540
547
  /**
541
548
  * Handles incrementing the activeReferences counter.
@@ -668,7 +675,7 @@ class SnapController extends _basecontroller.BaseControllerV2 {
668
675
  }
669
676
  // Existing snaps that should be re-installed should not maintain their existing permissions
670
677
  if (existingSnap && location.shouldAlwaysReload) {
671
- this.revokeAllSnapPermissions(snapId);
678
+ _class_private_method_get(this, _revokeAllSnapPermissions, revokeAllSnapPermissions).call(this, snapId);
672
679
  }
673
680
  try {
674
681
  const { sourceCode } = await _class_private_method_get(this, _add, add).call(this, {
@@ -884,7 +891,13 @@ class SnapController extends _basecontroller.BaseControllerV2 {
884
891
  * @param options.handler - The handler to trigger on the snap for the request.
885
892
  * @param options.request - The JSON-RPC request object.
886
893
  * @returns The result of the JSON-RPC request.
887
- */ async handleRequest({ snapId, origin, handler: handlerType, request }) {
894
+ */ async handleRequest({ snapId, origin, handler: handlerType, request: rawRequest }) {
895
+ const request = {
896
+ jsonrpc: '2.0',
897
+ id: (0, _nanoid.nanoid)(),
898
+ ...rawRequest
899
+ };
900
+ (0, _utils.assertIsJsonRpcRequest)(request);
888
901
  const permissionName = _endowments.handlerEndowments[handlerType];
889
902
  const hasPermission = this.messagingSystem.call('PermissionController:hasPermission', snapId, permissionName);
890
903
  if (!hasPermission) {
@@ -912,7 +925,9 @@ class SnapController extends _basecontroller.BaseControllerV2 {
912
925
  request
913
926
  });
914
927
  }
915
- constructor({ closeAllConnections, messenger, state, environmentEndowmentPermissions = [], excludedPermissions = {}, idleTimeCheckInterval = (0, _utils.inMilliseconds)(5, _utils.Duration.Second), maxIdleTime = (0, _utils.inMilliseconds)(30, _utils.Duration.Second), maxRequestTime = (0, _utils.inMilliseconds)(60, _utils.Duration.Second), fetchFunction = globalThis.fetch.bind(globalThis), featureFlags = {}, detectSnapLocation: detectSnapLocationFunction = _location.detectSnapLocation }){
928
+ constructor({ closeAllConnections, messenger, state, dynamicPermissions = [
929
+ 'eth_accounts'
930
+ ], environmentEndowmentPermissions = [], excludedPermissions = {}, idleTimeCheckInterval = (0, _utils.inMilliseconds)(5, _utils.Duration.Second), maxIdleTime = (0, _utils.inMilliseconds)(30, _utils.Duration.Second), maxRequestTime = (0, _utils.inMilliseconds)(60, _utils.Duration.Second), fetchFunction = globalThis.fetch.bind(globalThis), featureFlags = {}, detectSnapLocation: detectSnapLocationFunction = _location.detectSnapLocation }){
916
931
  super({
917
932
  messenger,
918
933
  metadata: {
@@ -983,6 +998,7 @@ class SnapController extends _basecontroller.BaseControllerV2 {
983
998
  * @param snapId - The snap to terminate.
984
999
  */ _class_private_method_init(this, _terminateSnap);
985
1000
  _class_private_method_init(this, _removeSnapFromSubjects);
1001
+ _class_private_method_init(this, _revokeAllSnapPermissions);
986
1002
  _class_private_method_init(this, _createApproval);
987
1003
  _class_private_method_init(this, _updateApproval);
988
1004
  /**
@@ -1058,6 +1074,10 @@ class SnapController extends _basecontroller.BaseControllerV2 {
1058
1074
  writable: true,
1059
1075
  value: void 0
1060
1076
  });
1077
+ _class_private_field_init(this, _dynamicPermissions, {
1078
+ writable: true,
1079
+ value: void 0
1080
+ });
1061
1081
  _class_private_field_init(this, _environmentEndowmentPermissions, {
1062
1082
  writable: true,
1063
1083
  value: void 0
@@ -1103,6 +1123,7 @@ class SnapController extends _basecontroller.BaseControllerV2 {
1103
1123
  value: void 0
1104
1124
  });
1105
1125
  _class_private_field_set(this, _closeAllConnections, closeAllConnections);
1126
+ _class_private_field_set(this, _dynamicPermissions, dynamicPermissions);
1106
1127
  _class_private_field_set(this, _environmentEndowmentPermissions, environmentEndowmentPermissions);
1107
1128
  _class_private_field_set(this, _excludedPermissions, excludedPermissions);
1108
1129
  _class_private_field_set(this, _featureFlags, featureFlags);
@@ -1200,6 +1221,7 @@ function registerMessageHandlers() {
1200
1221
  this.messagingSystem.registerActionHandler(`${controllerName}:decrementActiveReferences`, (...args)=>this.decrementActiveReferences(...args));
1201
1222
  this.messagingSystem.registerActionHandler(`${controllerName}:getRegistryMetadata`, async (...args)=>this.getRegistryMetadata(...args));
1202
1223
  this.messagingSystem.registerActionHandler(`${controllerName}:disconnectOrigin`, (...args)=>this.removeSnapFromSubject(...args));
1224
+ this.messagingSystem.registerActionHandler(`${controllerName}:revokeDynamicPermissions`, (...args)=>this.revokeDynamicSnapPermissions(...args));
1203
1225
  }
1204
1226
  function pollForLastRequestStatus() {
1205
1227
  _class_private_field_set(this, _timeoutForLastRequestStatus, setTimeout(()=>{
@@ -1270,6 +1292,11 @@ function removeSnapFromSubjects(snapId) {
1270
1292
  this.removeSnapFromSubject(subject, snapId);
1271
1293
  }
1272
1294
  }
1295
+ function revokeAllSnapPermissions(snapId) {
1296
+ if (this.messagingSystem.call('PermissionController:hasPermissions', snapId)) {
1297
+ this.messagingSystem.call('PermissionController:revokeAllPermissions', snapId);
1298
+ }
1299
+ }
1273
1300
  function createApproval({ origin, snapId, type }) {
1274
1301
  const id = (0, _nanoid.nanoid)();
1275
1302
  const promise = this.messagingSystem.call('ApprovalController:addRequest', {
@@ -1520,24 +1547,12 @@ function getRpcRequestHandler(snapId) {
1520
1547
  }
1521
1548
  }
1522
1549
  }
1523
- let _request = request;
1524
- if (!(0, _utils.hasProperty)(request, 'jsonrpc')) {
1525
- _request = {
1526
- ...request,
1527
- jsonrpc: '2.0'
1528
- };
1529
- } else if (request.jsonrpc !== '2.0') {
1530
- throw _ethrpcerrors.ethErrors.rpc.invalidRequest({
1531
- message: 'Invalid "jsonrpc" property. Must be "2.0" if provided.',
1532
- data: request.jsonrpc
1533
- });
1534
- }
1535
1550
  const timer = new _Timer.Timer(this.maxRequestTime);
1536
1551
  _class_private_method_get(this, _recordSnapRpcRequestStart, recordSnapRpcRequestStart).call(this, snapId, request.id, timer);
1537
1552
  const handleRpcRequestPromise = this.messagingSystem.call('ExecutionService:handleRpcRequest', snapId, {
1538
1553
  origin,
1539
1554
  handler: handlerType,
1540
- request: _request
1555
+ request
1541
1556
  });
1542
1557
  // This will either get the result or reject due to the timeout.
1543
1558
  try {