@metamask/permission-controller 14.0.0 → 15.0.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.
@@ -16,7 +16,9 @@ const controllerName = 'PermissionController';
16
16
  const MESSENGER_EXPOSED_METHODS = [
17
17
  'clearState',
18
18
  'executeRestrictedMethod',
19
- 'getEndowments',
19
+ 'getEndowmentsGlobals',
20
+ 'getEndowmentsRpcMethods',
21
+ 'isEndowmentRpcMethod',
20
22
  'getSubjectNames',
21
23
  'getPermissions',
22
24
  'hasPermission',
@@ -535,9 +537,6 @@ export class PermissionController extends BaseController {
535
537
  if (permission.caveats) {
536
538
  const caveatIndex = permission.caveats.findIndex((existingCaveat) => existingCaveat.type === caveat.type);
537
539
  if (caveatIndex === -1) {
538
- // This error appeared when upgrading to TypeScript 7.
539
- // @ts-expect-error: TS2589: Type instantiation is excessively deep
540
- // and possibly infinite.
541
540
  permission.caveats.push(caveat);
542
541
  addedCaveat = true;
543
542
  }
@@ -707,7 +706,11 @@ export class PermissionController extends BaseController {
707
706
  if (!this.#targetExists(permission.parentCapability)) {
708
707
  throw new Error(`Fatal: Existing permission target "${permission.parentCapability}" has no specification.`);
709
708
  }
710
- this.#validatePermission(this.#getPermissionSpecification(permission.parentCapability), permission, origin, validationFlags);
709
+ this.#validatePermission(this.#getPermissionSpecification(permission.parentCapability),
710
+ /* oxlint-disable typescript/no-unnecessary-type-assertion */
711
+ permission,
712
+ /* oxlint-enable typescript/no-unnecessary-type-assertion */
713
+ origin, validationFlags);
711
714
  }
712
715
  /**
713
716
  * Verifies the existence the specified permission target, i.e. whether it has
@@ -795,6 +798,7 @@ export class PermissionController extends BaseController {
795
798
  }
796
799
  // We have verified that the target exists, and reassign it to change its
797
800
  // type.
801
+ // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
798
802
  const targetName = requestedTarget;
799
803
  const specification = this.#getPermissionSpecification(targetName);
800
804
  // The requested caveats are validated here.
@@ -1452,22 +1456,50 @@ export class PermissionController extends BaseController {
1452
1456
  return this.messenger.call('ApprovalController:rejectRequest', id, error);
1453
1457
  }
1454
1458
  /**
1455
- * Gets the subject's endowments per the specified endowment permission.
1456
- * Throws if the subject does not have the required permission or if the
1457
- * permission is not an endowment permission.
1458
- *
1459
- * @param origin - The origin of the subject whose endowments to retrieve.
1460
- * @param targetName - The name of the endowment permission. This must be a
1461
- * valid permission target name.
1462
- * @param requestData - Additional data associated with the request, if any.
1463
- * Forwarded to the endowment getter function for the permission.
1464
- * @returns The endowments, if any.
1459
+ * Gets the JavaScript globals tied to the endowment permissions for a
1460
+ * subject.
1461
+ *
1462
+ * @param origin - The origin of the subject whose globals to retrieve.
1463
+ * @returns The names of the globals, if any.
1465
1464
  */
1466
- async getEndowments(origin, targetName, requestData) {
1467
- if (!this.hasPermission(origin, targetName)) {
1468
- throw unauthorized({ data: { origin, targetName } });
1469
- }
1470
- return this.#getTypedPermissionSpecification(PermissionType.Endowment, targetName, origin).endowmentGetter({ origin, requestData });
1465
+ getEndowmentsGlobals(origin) {
1466
+ const permissions = Object.values(this.getPermissions(origin) ?? {});
1467
+ return permissions.flatMap((permission) => {
1468
+ const specification = this.#getPermissionSpecification(permission.parentCapability);
1469
+ if (hasSpecificationType(specification, PermissionType.Endowment) &&
1470
+ specification.globals) {
1471
+ return specification.globals;
1472
+ }
1473
+ return [];
1474
+ });
1475
+ }
1476
+ /**
1477
+ * Gets the RPC methods tied to the endowment permissions for a subject.
1478
+ *
1479
+ * @param origin - The origin of the subject whose RPC methods to retrieve.
1480
+ * @returns The RPC method names, if any.
1481
+ */
1482
+ getEndowmentsRpcMethods(origin) {
1483
+ const permissions = Object.values(this.getPermissions(origin) ?? {});
1484
+ return permissions.flatMap((permission) => {
1485
+ const specification = this.#getPermissionSpecification(permission.parentCapability);
1486
+ if (hasSpecificationType(specification, PermissionType.Endowment) &&
1487
+ specification.rpcMethods) {
1488
+ return specification.rpcMethods;
1489
+ }
1490
+ return [];
1491
+ });
1492
+ }
1493
+ /**
1494
+ * Checks whether an RPC method is tied to an endowment.
1495
+ *
1496
+ * @param method - The name of the RPC method.
1497
+ * @returns True if the RPC method is tied to an endowment, otherwise false.
1498
+ */
1499
+ isEndowmentRpcMethod(method) {
1500
+ const specifications = Object.values(this.#permissionSpecifications);
1501
+ return specifications.some((specification) => hasSpecificationType(specification, PermissionType.Endowment) &&
1502
+ (specification.rpcMethods?.includes(method) ?? false));
1471
1503
  }
1472
1504
  /**
1473
1505
  * Executes a restricted method as the subject with the given origin.