@metamask/permission-controller 11.0.0 → 11.0.2

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 (185) hide show
  1. package/CHANGELOG.md +29 -1
  2. package/dist/Caveat.cjs +48 -0
  3. package/dist/Caveat.cjs.map +1 -0
  4. package/dist/{types/Caveat.d.ts → Caveat.d.cts} +3 -3
  5. package/dist/Caveat.d.cts.map +1 -0
  6. package/dist/Caveat.d.mts +210 -0
  7. package/dist/Caveat.d.mts.map +1 -0
  8. package/dist/Caveat.mjs +42 -11
  9. package/dist/Caveat.mjs.map +1 -1
  10. package/dist/Permission.cjs +65 -0
  11. package/dist/Permission.cjs.map +1 -0
  12. package/dist/{types/Permission.d.ts → Permission.d.cts} +7 -7
  13. package/dist/Permission.d.cts.map +1 -0
  14. package/dist/Permission.d.mts +417 -0
  15. package/dist/Permission.d.mts.map +1 -0
  16. package/dist/Permission.mjs +58 -13
  17. package/dist/Permission.mjs.map +1 -1
  18. package/dist/PermissionController.cjs +1481 -0
  19. package/dist/{chunk-COGJLF22.mjs.map → PermissionController.cjs.map} +1 -1
  20. package/dist/{types/PermissionController.d.ts → PermissionController.d.cts} +10 -10
  21. package/dist/PermissionController.d.cts.map +1 -0
  22. package/dist/PermissionController.d.mts +986 -0
  23. package/dist/PermissionController.d.mts.map +1 -0
  24. package/dist/PermissionController.mjs +1479 -13
  25. package/dist/PermissionController.mjs.map +1 -1
  26. package/dist/SubjectMetadataController.cjs +152 -0
  27. package/dist/SubjectMetadataController.cjs.map +1 -0
  28. package/dist/{types/SubjectMetadataController.d.ts → SubjectMetadataController.d.cts} +5 -5
  29. package/dist/SubjectMetadataController.d.cts.map +1 -0
  30. package/dist/SubjectMetadataController.d.mts +107 -0
  31. package/dist/SubjectMetadataController.d.mts.map +1 -0
  32. package/dist/SubjectMetadataController.mjs +146 -8
  33. package/dist/SubjectMetadataController.mjs.map +1 -1
  34. package/dist/errors.cjs +217 -0
  35. package/dist/errors.cjs.map +1 -0
  36. package/dist/{types/errors.d.ts → errors.d.cts} +6 -6
  37. package/dist/errors.d.cts.map +1 -0
  38. package/dist/errors.d.mts +185 -0
  39. package/dist/errors.d.mts.map +1 -0
  40. package/dist/errors.mjs +187 -57
  41. package/dist/errors.mjs.map +1 -1
  42. package/dist/index.cjs +38 -0
  43. package/dist/index.cjs.map +1 -0
  44. package/dist/index.d.cts +9 -0
  45. package/dist/index.d.cts.map +1 -0
  46. package/dist/index.d.mts +9 -0
  47. package/dist/index.d.mts.map +1 -0
  48. package/dist/index.mjs +7 -97
  49. package/dist/index.mjs.map +1 -1
  50. package/dist/permission-middleware.cjs +55 -0
  51. package/dist/permission-middleware.cjs.map +1 -0
  52. package/dist/{types/permission-middleware.d.ts → permission-middleware.d.cts} +4 -4
  53. package/dist/permission-middleware.d.cts.map +1 -0
  54. package/dist/permission-middleware.d.mts +33 -0
  55. package/dist/permission-middleware.d.mts.map +1 -0
  56. package/dist/permission-middleware.mjs +50 -8
  57. package/dist/permission-middleware.mjs.map +1 -1
  58. package/dist/rpc-methods/getPermissions.cjs +27 -0
  59. package/dist/rpc-methods/getPermissions.cjs.map +1 -0
  60. package/dist/rpc-methods/getPermissions.d.cts +9 -0
  61. package/dist/rpc-methods/getPermissions.d.cts.map +1 -0
  62. package/dist/rpc-methods/getPermissions.d.mts +9 -0
  63. package/dist/rpc-methods/getPermissions.d.mts.map +1 -0
  64. package/dist/rpc-methods/getPermissions.mjs +22 -7
  65. package/dist/rpc-methods/getPermissions.mjs.map +1 -1
  66. package/dist/rpc-methods/index.cjs +12 -0
  67. package/dist/rpc-methods/index.cjs.map +1 -0
  68. package/dist/rpc-methods/index.d.cts +6 -0
  69. package/dist/rpc-methods/index.d.cts.map +1 -0
  70. package/dist/rpc-methods/index.d.mts +6 -0
  71. package/dist/rpc-methods/index.d.mts.map +1 -0
  72. package/dist/rpc-methods/index.mjs +8 -12
  73. package/dist/rpc-methods/index.mjs.map +1 -1
  74. package/dist/rpc-methods/requestPermissions.cjs +36 -0
  75. package/dist/rpc-methods/requestPermissions.cjs.map +1 -0
  76. package/dist/{types/rpc-methods/requestPermissions.d.ts → rpc-methods/requestPermissions.d.cts} +3 -3
  77. package/dist/rpc-methods/requestPermissions.d.cts.map +1 -0
  78. package/dist/rpc-methods/requestPermissions.d.mts +17 -0
  79. package/dist/rpc-methods/requestPermissions.d.mts.map +1 -0
  80. package/dist/rpc-methods/requestPermissions.mjs +31 -8
  81. package/dist/rpc-methods/requestPermissions.mjs.map +1 -1
  82. package/dist/rpc-methods/revokePermissions.cjs +41 -0
  83. package/dist/rpc-methods/revokePermissions.cjs.map +1 -0
  84. package/dist/{types/rpc-methods/revokePermissions.d.ts → rpc-methods/revokePermissions.d.cts} +4 -4
  85. package/dist/rpc-methods/revokePermissions.d.cts.map +1 -0
  86. package/dist/rpc-methods/revokePermissions.d.mts +11 -0
  87. package/dist/rpc-methods/revokePermissions.d.mts.map +1 -0
  88. package/dist/rpc-methods/revokePermissions.mjs +36 -8
  89. package/dist/rpc-methods/revokePermissions.mjs.map +1 -1
  90. package/dist/utils.cjs +44 -0
  91. package/dist/utils.cjs.map +1 -0
  92. package/dist/{types/utils.d.ts → utils.d.cts} +5 -5
  93. package/dist/utils.d.cts.map +1 -0
  94. package/dist/utils.d.mts +57 -0
  95. package/dist/utils.d.mts.map +1 -0
  96. package/dist/utils.mjs +39 -9
  97. package/dist/utils.mjs.map +1 -1
  98. package/package.json +18 -12
  99. package/dist/Caveat.js +0 -12
  100. package/dist/Caveat.js.map +0 -1
  101. package/dist/Permission.js +0 -14
  102. package/dist/Permission.js.map +0 -1
  103. package/dist/PermissionController.js +0 -15
  104. package/dist/PermissionController.js.map +0 -1
  105. package/dist/SubjectMetadataController.js +0 -10
  106. package/dist/SubjectMetadataController.js.map +0 -1
  107. package/dist/chunk-2L4QPE5A.mjs +0 -25
  108. package/dist/chunk-2L4QPE5A.mjs.map +0 -1
  109. package/dist/chunk-3WWJKO7P.mjs +0 -37
  110. package/dist/chunk-3WWJKO7P.mjs.map +0 -1
  111. package/dist/chunk-3YOPLPVY.js +0 -37
  112. package/dist/chunk-3YOPLPVY.js.map +0 -1
  113. package/dist/chunk-42QSJHWO.mjs +0 -155
  114. package/dist/chunk-42QSJHWO.mjs.map +0 -1
  115. package/dist/chunk-4FMYQC3Y.mjs +0 -29
  116. package/dist/chunk-4FMYQC3Y.mjs.map +0 -1
  117. package/dist/chunk-4NAVRO44.mjs +0 -35
  118. package/dist/chunk-4NAVRO44.mjs.map +0 -1
  119. package/dist/chunk-5RFW5THA.mjs +0 -17
  120. package/dist/chunk-5RFW5THA.mjs.map +0 -1
  121. package/dist/chunk-6PXDVUYM.js +0 -29
  122. package/dist/chunk-6PXDVUYM.js.map +0 -1
  123. package/dist/chunk-74H4CVH7.mjs +0 -34
  124. package/dist/chunk-74H4CVH7.mjs.map +0 -1
  125. package/dist/chunk-7A3VYLCK.js +0 -30
  126. package/dist/chunk-7A3VYLCK.js.map +0 -1
  127. package/dist/chunk-B6PDRQ7N.js +0 -1679
  128. package/dist/chunk-B6PDRQ7N.js.map +0 -1
  129. package/dist/chunk-COGJLF22.mjs +0 -1679
  130. package/dist/chunk-F5TBMVWC.js +0 -46
  131. package/dist/chunk-F5TBMVWC.js.map +0 -1
  132. package/dist/chunk-FYADAA2G.js +0 -220
  133. package/dist/chunk-FYADAA2G.js.map +0 -1
  134. package/dist/chunk-G4BWJ7EA.mjs +0 -220
  135. package/dist/chunk-G4BWJ7EA.mjs.map +0 -1
  136. package/dist/chunk-HYMS7IGB.mjs +0 -31
  137. package/dist/chunk-HYMS7IGB.mjs.map +0 -1
  138. package/dist/chunk-I62TTXZ6.mjs +0 -46
  139. package/dist/chunk-I62TTXZ6.mjs.map +0 -1
  140. package/dist/chunk-OCLNDUYO.mjs +0 -30
  141. package/dist/chunk-OCLNDUYO.mjs.map +0 -1
  142. package/dist/chunk-VBIZGGQL.js +0 -17
  143. package/dist/chunk-VBIZGGQL.js.map +0 -1
  144. package/dist/chunk-VQPP5PWS.js +0 -35
  145. package/dist/chunk-VQPP5PWS.js.map +0 -1
  146. package/dist/chunk-VSDHL2GQ.js +0 -155
  147. package/dist/chunk-VSDHL2GQ.js.map +0 -1
  148. package/dist/chunk-X4ZAW4QR.js +0 -34
  149. package/dist/chunk-X4ZAW4QR.js.map +0 -1
  150. package/dist/chunk-Z2XKIXLS.js +0 -25
  151. package/dist/chunk-Z2XKIXLS.js.map +0 -1
  152. package/dist/chunk-ZH4MLSXX.js +0 -31
  153. package/dist/chunk-ZH4MLSXX.js.map +0 -1
  154. package/dist/errors.js +0 -58
  155. package/dist/errors.js.map +0 -1
  156. package/dist/index.js +0 -98
  157. package/dist/index.js.map +0 -1
  158. package/dist/permission-middleware.js +0 -9
  159. package/dist/permission-middleware.js.map +0 -1
  160. package/dist/rpc-methods/getPermissions.js +0 -9
  161. package/dist/rpc-methods/getPermissions.js.map +0 -1
  162. package/dist/rpc-methods/index.js +0 -13
  163. package/dist/rpc-methods/index.js.map +0 -1
  164. package/dist/rpc-methods/requestPermissions.js +0 -10
  165. package/dist/rpc-methods/requestPermissions.js.map +0 -1
  166. package/dist/rpc-methods/revokePermissions.js +0 -10
  167. package/dist/rpc-methods/revokePermissions.js.map +0 -1
  168. package/dist/tsconfig.build.tsbuildinfo +0 -1
  169. package/dist/types/Caveat.d.ts.map +0 -1
  170. package/dist/types/Permission.d.ts.map +0 -1
  171. package/dist/types/PermissionController.d.ts.map +0 -1
  172. package/dist/types/SubjectMetadataController.d.ts.map +0 -1
  173. package/dist/types/errors.d.ts.map +0 -1
  174. package/dist/types/index.d.ts +0 -9
  175. package/dist/types/index.d.ts.map +0 -1
  176. package/dist/types/permission-middleware.d.ts.map +0 -1
  177. package/dist/types/rpc-methods/getPermissions.d.ts +0 -9
  178. package/dist/types/rpc-methods/getPermissions.d.ts.map +0 -1
  179. package/dist/types/rpc-methods/index.d.ts +0 -6
  180. package/dist/types/rpc-methods/index.d.ts.map +0 -1
  181. package/dist/types/rpc-methods/requestPermissions.d.ts.map +0 -1
  182. package/dist/types/rpc-methods/revokePermissions.d.ts.map +0 -1
  183. package/dist/types/utils.d.ts.map +0 -1
  184. package/dist/utils.js +0 -10
  185. package/dist/utils.js.map +0 -1
@@ -1,7 +1,7 @@
1
- import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from '@metamask/json-rpc-engine';
2
- import type { Json, JsonRpcParams, JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';
3
- import type { CaveatConstraint, CaveatSpecificationConstraint, CaveatSpecificationMap } from './Caveat';
4
- import type { PermissionConstraint, PermissionSpecificationConstraint, PermissionSpecificationMap } from './Permission';
1
+ import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from "@metamask/json-rpc-engine";
2
+ import type { Json, JsonRpcParams, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
3
+ import type { CaveatConstraint, CaveatSpecificationConstraint, CaveatSpecificationMap } from "./Caveat.cjs";
4
+ import type { PermissionConstraint, PermissionSpecificationConstraint, PermissionSpecificationMap } from "./Permission.cjs";
5
5
  export declare enum MethodNames {
6
6
  RequestPermissions = "wallet_requestPermissions",
7
7
  GetPermissions = "wallet_getPermissions",
@@ -54,4 +54,4 @@ export declare function collectUniqueAndPairedCaveats(leftPermission: Partial<Pe
54
54
  leftUniqueCaveats: CaveatConstraint[];
55
55
  rightUniqueCaveats: CaveatConstraint[];
56
56
  };
57
- //# sourceMappingURL=utils.d.ts.map
57
+ //# sourceMappingURL=utils.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,yBAAyB,EAC1B,kCAAkC;AACnC,OAAO,KAAK,EACV,IAAI,EACJ,aAAa,EACb,cAAc,EACd,sBAAsB,EACvB,wBAAwB;AAEzB,OAAO,KAAK,EACV,gBAAgB,EAChB,6BAA6B,EAC7B,sBAAsB,EACvB,qBAAiB;AAClB,OAAO,KAAK,EACV,oBAAoB,EACpB,iCAAiC,EACjC,0BAA0B,EAC3B,yBAAqB;AAEtB,oBAAY,WAAW;IACrB,kBAAkB,8BAA8B;IAChD,cAAc,0BAA0B;IACxC,iBAAiB,6BAA6B;CAC/C;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,qBAAqB,CAC/B,iBAAiB,SACb,sBAAsB,CAAC,6BAA6B,CAAC,GACrD,0BAA0B,CAAC,iCAAiC,CAAC,IAC/D,iBAAiB,CAAC,MAAM,iBAAiB,CAAC,CAAC;AAE/C;;GAEG;AACH,MAAM,MAAM,yBAAyB,CACnC,KAAK,EACL,MAAM,SAAS,aAAa,EAC5B,MAAM,SAAS,IAAI,IACjB,CACF,GAAG,EAAE,cAAc,CAAC,MAAM,CAAC,EAC3B,GAAG,EAAE,sBAAsB,CAAC,MAAM,CAAC,EACnC,IAAI,EAAE,yBAAyB,EAC/B,GAAG,EAAE,wBAAwB,EAC7B,KAAK,EAAE,KAAK,KACT,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAE1B;;;;;GAKG;AACH,MAAM,MAAM,SAAS,CAAC,OAAO,IAAI;KAC9B,QAAQ,IAAI,MAAM,OAAO,GAAG,IAAI;CAClC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,sBAAsB,CAChC,KAAK,EACL,MAAM,SAAS,aAAa,EAC5B,MAAM,SAAS,IAAI,IACjB;IACF,cAAc,EAAE,yBAAyB,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IACjE,SAAS,EAAE,SAAS,CAAC,KAAK,CAAC,CAAC;IAC5B,WAAW,EAAE,MAAM,EAAE,CAAC;CACvB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,6BAA6B,CAC3C,cAAc,EAAE,OAAO,CAAC,oBAAoB,CAAC,GAAG,SAAS,EACzD,eAAe,EAAE,OAAO,CAAC,oBAAoB,CAAC;;;;EAyB/C"}
@@ -0,0 +1,57 @@
1
+ import type { JsonRpcEngineEndCallback, JsonRpcEngineNextCallback } from "@metamask/json-rpc-engine";
2
+ import type { Json, JsonRpcParams, JsonRpcRequest, PendingJsonRpcResponse } from "@metamask/utils";
3
+ import type { CaveatConstraint, CaveatSpecificationConstraint, CaveatSpecificationMap } from "./Caveat.mjs";
4
+ import type { PermissionConstraint, PermissionSpecificationConstraint, PermissionSpecificationMap } from "./Permission.mjs";
5
+ export declare enum MethodNames {
6
+ RequestPermissions = "wallet_requestPermissions",
7
+ GetPermissions = "wallet_getPermissions",
8
+ RevokePermissions = "wallet_revokePermissions"
9
+ }
10
+ /**
11
+ * Utility type for extracting a union of all individual caveat or permission
12
+ * specification types from a {@link CaveatSpecificationMap} or
13
+ * {@link PermissionSpecificationMap}.
14
+ *
15
+ * @template SpecificationsMap - The caveat or permission specifications map
16
+ * whose specification type union to extract.
17
+ */
18
+ export type ExtractSpecifications<SpecificationsMap extends CaveatSpecificationMap<CaveatSpecificationConstraint> | PermissionSpecificationMap<PermissionSpecificationConstraint>> = SpecificationsMap[keyof SpecificationsMap];
19
+ /**
20
+ * A middleware function for handling a permitted method.
21
+ */
22
+ export type HandlerMiddlewareFunction<Hooks, Params extends JsonRpcParams, Result extends Json> = (req: JsonRpcRequest<Params>, res: PendingJsonRpcResponse<Result>, next: JsonRpcEngineNextCallback, end: JsonRpcEngineEndCallback, hooks: Hooks) => void | Promise<void>;
23
+ /**
24
+ * We use a mapped object type in order to create a type that requires the
25
+ * presence of the names of all hooks for the given handler.
26
+ * This can then be used to select only the necessary hooks whenever a method
27
+ * is called for purposes of POLA.
28
+ */
29
+ export type HookNames<HookMap> = {
30
+ [Property in keyof HookMap]: true;
31
+ };
32
+ /**
33
+ * A handler for a permitted method.
34
+ */
35
+ export type PermittedHandlerExport<Hooks, Params extends JsonRpcParams, Result extends Json> = {
36
+ implementation: HandlerMiddlewareFunction<Hooks, Params, Result>;
37
+ hookNames: HookNames<Hooks>;
38
+ methodNames: string[];
39
+ };
40
+ /**
41
+ * Given two permission objects, computes 3 sets:
42
+ * - The set of caveat pairs that are common to both permissions.
43
+ * - The set of caveats that are unique to the existing permission.
44
+ * - The set of caveats that are unique to the requested permission.
45
+ *
46
+ * Assumes that the caveat arrays of both permissions are valid.
47
+ *
48
+ * @param leftPermission - The left-hand permission.
49
+ * @param rightPermission - The right-hand permission.
50
+ * @returns The sets of caveat pairs and unique caveats.
51
+ */
52
+ export declare function collectUniqueAndPairedCaveats(leftPermission: Partial<PermissionConstraint> | undefined, rightPermission: Partial<PermissionConstraint>): {
53
+ caveatPairs: [CaveatConstraint, CaveatConstraint][];
54
+ leftUniqueCaveats: CaveatConstraint[];
55
+ rightUniqueCaveats: CaveatConstraint[];
56
+ };
57
+ //# sourceMappingURL=utils.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,yBAAyB,EAC1B,kCAAkC;AACnC,OAAO,KAAK,EACV,IAAI,EACJ,aAAa,EACb,cAAc,EACd,sBAAsB,EACvB,wBAAwB;AAEzB,OAAO,KAAK,EACV,gBAAgB,EAChB,6BAA6B,EAC7B,sBAAsB,EACvB,qBAAiB;AAClB,OAAO,KAAK,EACV,oBAAoB,EACpB,iCAAiC,EACjC,0BAA0B,EAC3B,yBAAqB;AAEtB,oBAAY,WAAW;IACrB,kBAAkB,8BAA8B;IAChD,cAAc,0BAA0B;IACxC,iBAAiB,6BAA6B;CAC/C;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,qBAAqB,CAC/B,iBAAiB,SACb,sBAAsB,CAAC,6BAA6B,CAAC,GACrD,0BAA0B,CAAC,iCAAiC,CAAC,IAC/D,iBAAiB,CAAC,MAAM,iBAAiB,CAAC,CAAC;AAE/C;;GAEG;AACH,MAAM,MAAM,yBAAyB,CACnC,KAAK,EACL,MAAM,SAAS,aAAa,EAC5B,MAAM,SAAS,IAAI,IACjB,CACF,GAAG,EAAE,cAAc,CAAC,MAAM,CAAC,EAC3B,GAAG,EAAE,sBAAsB,CAAC,MAAM,CAAC,EACnC,IAAI,EAAE,yBAAyB,EAC/B,GAAG,EAAE,wBAAwB,EAC7B,KAAK,EAAE,KAAK,KACT,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAE1B;;;;;GAKG;AACH,MAAM,MAAM,SAAS,CAAC,OAAO,IAAI;KAC9B,QAAQ,IAAI,MAAM,OAAO,GAAG,IAAI;CAClC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,sBAAsB,CAChC,KAAK,EACL,MAAM,SAAS,aAAa,EAC5B,MAAM,SAAS,IAAI,IACjB;IACF,cAAc,EAAE,yBAAyB,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IACjE,SAAS,EAAE,SAAS,CAAC,KAAK,CAAC,CAAC;IAC5B,WAAW,EAAE,MAAM,EAAE,CAAC;CACvB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,6BAA6B,CAC3C,cAAc,EAAE,OAAO,CAAC,oBAAoB,CAAC,GAAG,SAAS,EACzD,eAAe,EAAE,OAAO,CAAC,oBAAoB,CAAC;;;;EAyB/C"}
package/dist/utils.mjs CHANGED
@@ -1,10 +1,40 @@
1
- import {
2
- MethodNames,
3
- collectUniqueAndPairedCaveats
4
- } from "./chunk-4NAVRO44.mjs";
5
- import "./chunk-2L4QPE5A.mjs";
6
- export {
7
- MethodNames,
8
- collectUniqueAndPairedCaveats
9
- };
1
+ export var MethodNames;
2
+ (function (MethodNames) {
3
+ MethodNames["RequestPermissions"] = "wallet_requestPermissions";
4
+ MethodNames["GetPermissions"] = "wallet_getPermissions";
5
+ MethodNames["RevokePermissions"] = "wallet_revokePermissions";
6
+ })(MethodNames || (MethodNames = {}));
7
+ /**
8
+ * Given two permission objects, computes 3 sets:
9
+ * - The set of caveat pairs that are common to both permissions.
10
+ * - The set of caveats that are unique to the existing permission.
11
+ * - The set of caveats that are unique to the requested permission.
12
+ *
13
+ * Assumes that the caveat arrays of both permissions are valid.
14
+ *
15
+ * @param leftPermission - The left-hand permission.
16
+ * @param rightPermission - The right-hand permission.
17
+ * @returns The sets of caveat pairs and unique caveats.
18
+ */
19
+ export function collectUniqueAndPairedCaveats(leftPermission, rightPermission) {
20
+ const leftCaveats = leftPermission?.caveats?.slice() ?? [];
21
+ const rightCaveats = rightPermission.caveats?.slice() ?? [];
22
+ const leftUniqueCaveats = [];
23
+ const caveatPairs = [];
24
+ leftCaveats.forEach((leftCaveat) => {
25
+ const rightCaveatIndex = rightCaveats.findIndex((rightCaveat) => rightCaveat.type === leftCaveat.type);
26
+ if (rightCaveatIndex === -1) {
27
+ leftUniqueCaveats.push(leftCaveat);
28
+ }
29
+ else {
30
+ caveatPairs.push([leftCaveat, rightCaveats[rightCaveatIndex]]);
31
+ rightCaveats.splice(rightCaveatIndex, 1);
32
+ }
33
+ });
34
+ return {
35
+ caveatPairs,
36
+ leftUniqueCaveats,
37
+ rightUniqueCaveats: [...rightCaveats],
38
+ };
39
+ }
10
40
  //# sourceMappingURL=utils.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
1
+ {"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAsBA,MAAM,CAAN,IAAY,WAIX;AAJD,WAAY,WAAW;IACrB,+DAAgD,CAAA;IAChD,uDAAwC,CAAA;IACxC,6DAA8C,CAAA;AAChD,CAAC,EAJW,WAAW,KAAX,WAAW,QAItB;AAsDD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,6BAA6B,CAC3C,cAAyD,EACzD,eAA8C;IAE9C,MAAM,WAAW,GAAG,cAAc,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;IAC3D,MAAM,YAAY,GAAG,eAAe,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;IAC5D,MAAM,iBAAiB,GAAuB,EAAE,CAAC;IACjD,MAAM,WAAW,GAA2C,EAAE,CAAC;IAE/D,WAAW,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;QACjC,MAAM,gBAAgB,GAAG,YAAY,CAAC,SAAS,CAC7C,CAAC,WAAW,EAAE,EAAE,CAAC,WAAW,CAAC,IAAI,KAAK,UAAU,CAAC,IAAI,CACtD,CAAC;QAEF,IAAI,gBAAgB,KAAK,CAAC,CAAC,EAAE;YAC3B,iBAAiB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;SACpC;aAAM;YACL,WAAW,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,YAAY,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC;YAC/D,YAAY,CAAC,MAAM,CAAC,gBAAgB,EAAE,CAAC,CAAC,CAAC;SAC1C;IACH,CAAC,CAAC,CAAC;IAEH,OAAO;QACL,WAAW;QACX,iBAAiB;QACjB,kBAAkB,EAAE,CAAC,GAAG,YAAY,CAAC;KACtC,CAAC;AACJ,CAAC","sourcesContent":["import type {\n JsonRpcEngineEndCallback,\n JsonRpcEngineNextCallback,\n} from '@metamask/json-rpc-engine';\nimport type {\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nimport type {\n CaveatConstraint,\n CaveatSpecificationConstraint,\n CaveatSpecificationMap,\n} from './Caveat';\nimport type {\n PermissionConstraint,\n PermissionSpecificationConstraint,\n PermissionSpecificationMap,\n} from './Permission';\n\nexport enum MethodNames {\n RequestPermissions = 'wallet_requestPermissions',\n GetPermissions = 'wallet_getPermissions',\n RevokePermissions = 'wallet_revokePermissions',\n}\n\n/**\n * Utility type for extracting a union of all individual caveat or permission\n * specification types from a {@link CaveatSpecificationMap} or\n * {@link PermissionSpecificationMap}.\n *\n * @template SpecificationsMap - The caveat or permission specifications map\n * whose specification type union to extract.\n */\nexport type ExtractSpecifications<\n SpecificationsMap extends\n | CaveatSpecificationMap<CaveatSpecificationConstraint>\n | PermissionSpecificationMap<PermissionSpecificationConstraint>,\n> = SpecificationsMap[keyof SpecificationsMap];\n\n/**\n * A middleware function for handling a permitted method.\n */\nexport type HandlerMiddlewareFunction<\n Hooks,\n Params extends JsonRpcParams,\n Result extends Json,\n> = (\n req: JsonRpcRequest<Params>,\n res: PendingJsonRpcResponse<Result>,\n next: JsonRpcEngineNextCallback,\n end: JsonRpcEngineEndCallback,\n hooks: Hooks,\n) => void | Promise<void>;\n\n/**\n * We use a mapped object type in order to create a type that requires the\n * presence of the names of all hooks for the given handler.\n * This can then be used to select only the necessary hooks whenever a method\n * is called for purposes of POLA.\n */\nexport type HookNames<HookMap> = {\n [Property in keyof HookMap]: true;\n};\n\n/**\n * A handler for a permitted method.\n */\nexport type PermittedHandlerExport<\n Hooks,\n Params extends JsonRpcParams,\n Result extends Json,\n> = {\n implementation: HandlerMiddlewareFunction<Hooks, Params, Result>;\n hookNames: HookNames<Hooks>;\n methodNames: string[];\n};\n\n/**\n * Given two permission objects, computes 3 sets:\n * - The set of caveat pairs that are common to both permissions.\n * - The set of caveats that are unique to the existing permission.\n * - The set of caveats that are unique to the requested permission.\n *\n * Assumes that the caveat arrays of both permissions are valid.\n *\n * @param leftPermission - The left-hand permission.\n * @param rightPermission - The right-hand permission.\n * @returns The sets of caveat pairs and unique caveats.\n */\nexport function collectUniqueAndPairedCaveats(\n leftPermission: Partial<PermissionConstraint> | undefined,\n rightPermission: Partial<PermissionConstraint>,\n) {\n const leftCaveats = leftPermission?.caveats?.slice() ?? [];\n const rightCaveats = rightPermission.caveats?.slice() ?? [];\n const leftUniqueCaveats: CaveatConstraint[] = [];\n const caveatPairs: [CaveatConstraint, CaveatConstraint][] = [];\n\n leftCaveats.forEach((leftCaveat) => {\n const rightCaveatIndex = rightCaveats.findIndex(\n (rightCaveat) => rightCaveat.type === leftCaveat.type,\n );\n\n if (rightCaveatIndex === -1) {\n leftUniqueCaveats.push(leftCaveat);\n } else {\n caveatPairs.push([leftCaveat, rightCaveats[rightCaveatIndex]]);\n rightCaveats.splice(rightCaveatIndex, 1);\n }\n });\n\n return {\n caveatPairs,\n leftUniqueCaveats,\n rightUniqueCaveats: [...rightCaveats],\n };\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/permission-controller",
3
- "version": "11.0.0",
3
+ "version": "11.0.2",
4
4
  "description": "Mediates access to JSON-RPC methods, used to interact with pieces of the MetaMask stack, via middleware for json-rpc-engine",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -18,32 +18,38 @@
18
18
  "sideEffects": false,
19
19
  "exports": {
20
20
  ".": {
21
- "import": "./dist/index.mjs",
22
- "require": "./dist/index.js",
23
- "types": "./dist/types/index.d.ts"
21
+ "import": {
22
+ "types": "./dist/index.d.mts",
23
+ "default": "./dist/index.mjs"
24
+ },
25
+ "require": {
26
+ "types": "./dist/index.d.cts",
27
+ "default": "./dist/index.cjs"
28
+ }
24
29
  },
25
30
  "./package.json": "./package.json"
26
31
  },
27
- "main": "./dist/index.js",
28
- "types": "./dist/types/index.d.ts",
32
+ "main": "./dist/index.cjs",
33
+ "types": "./dist/index.d.cts",
29
34
  "files": [
30
35
  "dist/"
31
36
  ],
32
37
  "scripts": {
33
- "build": "tsup --config ../../tsup.config.ts --tsconfig ./tsconfig.build.json --clean",
38
+ "build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
34
39
  "build:docs": "typedoc",
35
40
  "changelog:update": "../../scripts/update-changelog.sh @metamask/permission-controller",
36
41
  "changelog:validate": "../../scripts/validate-changelog.sh @metamask/permission-controller",
37
42
  "publish:preview": "yarn npm publish --tag preview",
43
+ "since-latest-release": "../../scripts/since-latest-release.sh",
38
44
  "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
39
45
  "test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
40
46
  "test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
41
47
  "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
42
48
  },
43
49
  "dependencies": {
44
- "@metamask/base-controller": "^6.0.2",
45
- "@metamask/controller-utils": "^11.0.2",
46
- "@metamask/json-rpc-engine": "^9.0.2",
50
+ "@metamask/base-controller": "^7.0.1",
51
+ "@metamask/controller-utils": "^11.3.0",
52
+ "@metamask/json-rpc-engine": "^9.0.3",
47
53
  "@metamask/rpc-errors": "^6.3.1",
48
54
  "@metamask/utils": "^9.1.0",
49
55
  "@types/deep-freeze-strict": "^1.1.0",
@@ -52,7 +58,7 @@
52
58
  "nanoid": "^3.1.31"
53
59
  },
54
60
  "devDependencies": {
55
- "@metamask/approval-controller": "^7.0.2",
61
+ "@metamask/approval-controller": "^7.0.4",
56
62
  "@metamask/auto-changelog": "^3.4.4",
57
63
  "@types/jest": "^27.4.1",
58
64
  "deepmerge": "^4.2.2",
@@ -60,7 +66,7 @@
60
66
  "ts-jest": "^27.1.4",
61
67
  "typedoc": "^0.24.8",
62
68
  "typedoc-plugin-missing-exports": "^2.0.0",
63
- "typescript": "~5.0.4"
69
+ "typescript": "~5.2.2"
64
70
  },
65
71
  "peerDependencies": {
66
72
  "@metamask/approval-controller": "^7.0.0"
package/dist/Caveat.js DELETED
@@ -1,12 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
-
3
-
4
- var _chunk3YOPLPVYjs = require('./chunk-3YOPLPVY.js');
5
- require('./chunk-ZH4MLSXX.js');
6
- require('./chunk-FYADAA2G.js');
7
- require('./chunk-Z2XKIXLS.js');
8
-
9
-
10
-
11
- exports.decorateWithCaveats = _chunk3YOPLPVYjs.decorateWithCaveats; exports.isRestrictedMethodCaveatSpecification = _chunk3YOPLPVYjs.isRestrictedMethodCaveatSpecification;
12
- //# sourceMappingURL=Caveat.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"names":[],"mappings":""}
@@ -1,14 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
-
3
-
4
-
5
-
6
- var _chunkZH4MLSXXjs = require('./chunk-ZH4MLSXX.js');
7
- require('./chunk-Z2XKIXLS.js');
8
-
9
-
10
-
11
-
12
-
13
- exports.PermissionType = _chunkZH4MLSXXjs.PermissionType; exports.constructPermission = _chunkZH4MLSXXjs.constructPermission; exports.findCaveat = _chunkZH4MLSXXjs.findCaveat; exports.hasSpecificationType = _chunkZH4MLSXXjs.hasSpecificationType;
14
- //# sourceMappingURL=Permission.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"names":[],"mappings":""}
@@ -1,15 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
-
3
-
4
- var _chunkB6PDRQ7Njs = require('./chunk-B6PDRQ7N.js');
5
- require('./chunk-3YOPLPVY.js');
6
- require('./chunk-ZH4MLSXX.js');
7
- require('./chunk-F5TBMVWC.js');
8
- require('./chunk-FYADAA2G.js');
9
- require('./chunk-VQPP5PWS.js');
10
- require('./chunk-Z2XKIXLS.js');
11
-
12
-
13
-
14
- exports.CaveatMutatorOperation = _chunkB6PDRQ7Njs.CaveatMutatorOperation; exports.PermissionController = _chunkB6PDRQ7Njs.PermissionController;
15
- //# sourceMappingURL=PermissionController.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"names":[],"mappings":""}
@@ -1,10 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
-
3
-
4
- var _chunkVSDHL2GQjs = require('./chunk-VSDHL2GQ.js');
5
- require('./chunk-Z2XKIXLS.js');
6
-
7
-
8
-
9
- exports.SubjectMetadataController = _chunkVSDHL2GQjs.SubjectMetadataController; exports.SubjectType = _chunkVSDHL2GQjs.SubjectType;
10
- //# sourceMappingURL=SubjectMetadataController.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"names":[],"mappings":""}
@@ -1,25 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __export = (target, all) => {
3
- for (var name in all)
4
- __defProp(target, name, { get: all[name], enumerable: true });
5
- };
6
- var __accessCheck = (obj, member, msg) => {
7
- if (!member.has(obj))
8
- throw TypeError("Cannot " + msg);
9
- };
10
- var __privateAdd = (obj, member, value) => {
11
- if (member.has(obj))
12
- throw TypeError("Cannot add the same private member more than once");
13
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
14
- };
15
- var __privateMethod = (obj, member, method) => {
16
- __accessCheck(obj, member, "access private method");
17
- return method;
18
- };
19
-
20
- export {
21
- __export,
22
- __privateAdd,
23
- __privateMethod
24
- };
25
- //# sourceMappingURL=chunk-2L4QPE5A.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,37 +0,0 @@
1
- import {
2
- CaveatSpecificationMismatchError,
3
- UnrecognizedCaveatTypeError
4
- } from "./chunk-G4BWJ7EA.mjs";
5
-
6
- // src/Caveat.ts
7
- import { hasProperty } from "@metamask/utils";
8
- function isRestrictedMethodCaveatSpecification(specification) {
9
- return hasProperty(specification, "decorator");
10
- }
11
- function decorateWithCaveats(methodImplementation, permission, caveatSpecifications) {
12
- const { caveats } = permission;
13
- if (!caveats) {
14
- return methodImplementation;
15
- }
16
- let decorated = async (args) => methodImplementation(args);
17
- for (const caveat of caveats) {
18
- const specification = caveatSpecifications[caveat.type];
19
- if (!specification) {
20
- throw new UnrecognizedCaveatTypeError(caveat.type);
21
- }
22
- if (!isRestrictedMethodCaveatSpecification(specification)) {
23
- throw new CaveatSpecificationMismatchError(
24
- specification,
25
- "RestrictedMethod" /* RestrictedMethod */
26
- );
27
- }
28
- decorated = specification.decorator(decorated, caveat);
29
- }
30
- return decorated;
31
- }
32
-
33
- export {
34
- isRestrictedMethodCaveatSpecification,
35
- decorateWithCaveats
36
- };
37
- //# sourceMappingURL=chunk-3WWJKO7P.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/Caveat.ts"],"sourcesContent":["import type { Json } from '@metamask/utils';\nimport { hasProperty } from '@metamask/utils';\n\nimport {\n CaveatSpecificationMismatchError,\n UnrecognizedCaveatTypeError,\n} from './errors';\nimport type {\n AsyncRestrictedMethod,\n RestrictedMethod,\n PermissionConstraint,\n RestrictedMethodParameters,\n} from './Permission';\nimport { PermissionType } from './Permission';\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { PermissionController } from './PermissionController';\n\nexport type CaveatConstraint = {\n /**\n * The type of the caveat. The type is presumed to be meaningful in the\n * context of the capability it is associated with.\n *\n * In MetaMask, every permission can only have one caveat of each type.\n */\n readonly type: string;\n\n /**\n * Any additional data necessary to enforce the caveat.\n */\n readonly value: Json;\n};\n\n/**\n * A `ZCAP-LD`-like caveat object. A caveat is associated with a particular\n * permission, and stored in its `caveats` array. Conceptually, a caveat is\n * an arbitrary attenuation of the authority granted by its associated\n * permission. It is the responsibility of the host to interpret and apply\n * the restriction represented by a caveat.\n *\n * @template Type - The type of the caveat.\n * @template Value - The value associated with the caveat.\n */\nexport type Caveat<Type extends string, Value extends Json> = {\n /**\n * The type of the caveat. The type is presumed to be meaningful in the\n * context of the capability it is associated with.\n *\n * In MetaMask, every permission can only have one caveat of each type.\n */\n readonly type: Type;\n\n /**\n * Any additional data necessary to enforce the caveat.\n */\n readonly value: Value;\n};\n\n// Next, we define types used for specifying caveats at the consumer layer,\n// and a function for applying caveats to a restricted method request. This is\n// Accomplished by decorating the restricted method implementation with the\n// the corresponding caveat functions.\n\n/**\n * A function for applying caveats to a restricted method request.\n *\n * @template ParentCaveat - The caveat type associated with this decorator.\n * @param decorated - The restricted method implementation to be decorated.\n * The method may have already been decorated with other caveats.\n * @param caveat - The caveat object.\n * @returns The decorated restricted method implementation.\n */\nexport type CaveatDecorator<ParentCaveat extends CaveatConstraint> = (\n decorated: AsyncRestrictedMethod<RestrictedMethodParameters, Json>,\n caveat: ParentCaveat,\n) => AsyncRestrictedMethod<RestrictedMethodParameters, Json>;\n\n/**\n * Extracts a caveat value type from a caveat decorator.\n *\n * @template Decorator - The {@link CaveatDecorator} to extract a caveat value\n * type from.\n */\ntype ExtractCaveatValueFromDecorator<\n Decorator extends CaveatDecorator<CaveatConstraint>,\n> = Decorator extends (\n decorated: AsyncRestrictedMethod<RestrictedMethodParameters, Json>,\n caveat: infer ParentCaveat,\n) => AsyncRestrictedMethod<RestrictedMethodParameters, Json>\n ? ParentCaveat extends CaveatConstraint\n ? ParentCaveat['value']\n : never\n : never;\n\n/**\n * A function for validating caveats of a particular type.\n *\n * @see `validator` in {@link CaveatSpecificationBase} for more details.\n * @template ParentCaveat - The caveat type associated with this validator.\n * @param caveat - The caveat object to validate.\n * @param origin - The origin associated with the parent permission.\n * @param target - The target of the parent permission.\n */\nexport type CaveatValidator<ParentCaveat extends CaveatConstraint> = (\n caveat: { type: ParentCaveat['type']; value: unknown },\n origin?: string,\n target?: string,\n) => void;\n\n/**\n * A map of caveat type strings to {@link CaveatDiff} values.\n */\nexport type CaveatDiffMap<ParentCaveat extends CaveatConstraint> = {\n [CaveatType in ParentCaveat['type']]: ParentCaveat['value'];\n};\n\n/**\n * A function that merges two caveat values of the same type. The values must be\n * merged in the fashion of a right-biased union.\n *\n * @see `ARCHITECTURE.md` for more details.\n * @template Value - The type of the values to merge.\n * @param leftValue - The left-hand value.\n * @param rightValue - The right-hand value.\n * @returns `[newValue, diff]`, i.e. the merged value and the diff between the left value\n * and the new value. The diff must be expressed in the same type as the value itself.\n */\nexport type CaveatValueMerger<Value extends Json> = (\n leftValue: Value,\n rightValue: Value,\n) => [Value, Value] | [];\n\nexport type CaveatSpecificationBase = {\n /**\n * The string type of the caveat.\n */\n type: string;\n\n /**\n * The validator function used to validate caveats of the associated type\n * whenever they are instantiated. Caveat are instantiated whenever they are\n * created or mutated.\n *\n * The validator should throw an appropriate JSON-RPC error if validation fails.\n *\n * If no validator is specified, no validation of caveat values will be\n * performed. Although caveats can also be validated by permission validators,\n * validating caveat values separately is strongly recommended.\n */\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n validator?: CaveatValidator<any>;\n\n /**\n * The merger function used to merge a pair of values of the associated caveat type\n * during incremental permission requests. The values must be merged in the fashion\n * of a right-biased union.\n *\n * @see `ARCHITECTURE.md` for more details.\n */\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n merger?: CaveatValueMerger<any>;\n};\n\nexport type RestrictedMethodCaveatSpecificationConstraint =\n CaveatSpecificationBase & {\n /**\n * The decorator function used to apply the caveat to restricted method\n * requests.\n */\n decorator: CaveatDecorator<CaveatConstraint>;\n };\n\nexport type EndowmentCaveatSpecificationConstraint = CaveatSpecificationBase;\n\n/**\n * The constraint for caveat specification objects. Every {@link Caveat}\n * supported by a {@link PermissionController} must have an associated\n * specification, which is the source of truth for all caveat-related types.\n * In addition, a caveat specification may include a decorator function used\n * to apply the caveat's attenuation to a restricted method. It may also include\n * a validator function specified by the consumer.\n *\n * See the README for more details.\n */\nexport type CaveatSpecificationConstraint =\n | RestrictedMethodCaveatSpecificationConstraint\n | EndowmentCaveatSpecificationConstraint;\n\n/**\n * Options for {@link CaveatSpecificationBuilder} functions.\n */\ntype CaveatSpecificationBuilderOptions<\n DecoratorHooks extends Record<string, unknown>,\n ValidatorHooks extends Record<string, unknown>,\n> = {\n type?: string;\n decoratorHooks?: DecoratorHooks;\n validatorHooks?: ValidatorHooks;\n};\n\n/**\n * A function that builds caveat specifications. Modules that specify caveats\n * for external consumption should make this their primary / default export so\n * that host applications can use them to generate concrete specifications\n * tailored to their requirements.\n */\nexport type CaveatSpecificationBuilder<\n Options extends CaveatSpecificationBuilderOptions<\n Record<string, unknown>,\n Record<string, unknown>\n >,\n Specification extends CaveatSpecificationConstraint,\n> = (options: Options) => Specification;\n\n/**\n * A caveat specification export object, containing the\n * {@link CaveatSpecificationBuilder} function and \"hook name\" objects.\n */\nexport type CaveatSpecificationBuilderExportConstraint = {\n specificationBuilder: CaveatSpecificationBuilder<\n CaveatSpecificationBuilderOptions<\n Record<string, unknown>,\n Record<string, unknown>\n >,\n CaveatSpecificationConstraint\n >;\n decoratorHookNames?: Record<string, true>;\n validatorHookNames?: Record<string, true>;\n};\n\n/**\n * The specifications for all caveats supported by a particular\n * {@link PermissionController}.\n *\n * @template Specifications - The union of all {@link CaveatSpecificationConstraint} types.\n */\nexport type CaveatSpecificationMap<\n CaveatSpecification extends CaveatSpecificationConstraint,\n> = Record<CaveatSpecification['type'], CaveatSpecification>;\n\n/**\n * Extracts the union of all caveat types specified by the given\n * {@link CaveatSpecificationConstraint} type.\n *\n * @template CaveatSpecification - The {@link CaveatSpecificationConstraint} to extract a\n * caveat type union from.\n */\nexport type ExtractCaveats<\n CaveatSpecification extends CaveatSpecificationConstraint,\n> = CaveatSpecification extends RestrictedMethodCaveatSpecificationConstraint\n ? Caveat<\n CaveatSpecification['type'],\n ExtractCaveatValueFromDecorator<\n RestrictedMethodCaveatSpecificationConstraint['decorator']\n >\n >\n : Caveat<CaveatSpecification['type'], Json>;\n\n/**\n * Extracts the type of a specific {@link Caveat} from a union of caveat\n * specifications.\n *\n * @template CaveatSpecifications - The union of all caveat specifications.\n * @template CaveatType - The type of the caveat to extract.\n */\nexport type ExtractCaveat<\n CaveatSpecifications extends CaveatSpecificationConstraint,\n CaveatType extends string,\n> = Extract<ExtractCaveats<CaveatSpecifications>, { type: CaveatType }>;\n\n/**\n * Extracts the value type of a specific {@link Caveat} from a union of caveat\n * specifications.\n *\n * @template CaveatSpecifications - The union of all caveat specifications.\n * @template CaveatType - The type of the caveat whose value to extract.\n */\nexport type ExtractCaveatValue<\n CaveatSpecifications extends CaveatSpecificationConstraint,\n CaveatType extends string,\n> = ExtractCaveat<CaveatSpecifications, CaveatType>['value'];\n\n/**\n * Determines whether a caveat specification is a restricted method caveat specification.\n *\n * @param specification - The caveat specification.\n * @returns True if the caveat specification is a restricted method caveat specification, otherwise false.\n */\nexport function isRestrictedMethodCaveatSpecification(\n specification: CaveatSpecificationConstraint,\n): specification is RestrictedMethodCaveatSpecificationConstraint {\n return hasProperty(specification, 'decorator');\n}\n\n/**\n * Decorate a restricted method implementation with its caveats.\n *\n * Note that all caveat functions (i.e. the argument and return value of the\n * decorator) must be awaited.\n *\n * @param methodImplementation - The restricted method implementation\n * @param permission - The origin's potential permission\n * @param caveatSpecifications - All caveat implementations\n * @returns The decorated method implementation\n */\nexport function decorateWithCaveats<\n CaveatSpecifications extends CaveatSpecificationConstraint,\n>(\n methodImplementation: RestrictedMethod<RestrictedMethodParameters, Json>,\n permission: Readonly<PermissionConstraint>, // bound to the requesting origin\n caveatSpecifications: CaveatSpecificationMap<CaveatSpecifications>, // all caveat implementations\n): RestrictedMethod<RestrictedMethodParameters, Json> {\n const { caveats } = permission;\n if (!caveats) {\n return methodImplementation;\n }\n\n let decorated = async (\n args: Parameters<RestrictedMethod<RestrictedMethodParameters, Json>>[0],\n ) => methodImplementation(args);\n\n for (const caveat of caveats) {\n const specification =\n caveatSpecifications[caveat.type as CaveatSpecifications['type']];\n if (!specification) {\n throw new UnrecognizedCaveatTypeError(caveat.type);\n }\n\n if (!isRestrictedMethodCaveatSpecification(specification)) {\n throw new CaveatSpecificationMismatchError(\n specification,\n PermissionType.RestrictedMethod,\n );\n }\n decorated = specification.decorator(decorated, caveat);\n }\n\n return decorated;\n}\n"],"mappings":";;;;;;AACA,SAAS,mBAAmB;AAgSrB,SAAS,sCACd,eACgE;AAChE,SAAO,YAAY,eAAe,WAAW;AAC/C;AAaO,SAAS,oBAGd,sBACA,YACA,sBACoD;AACpD,QAAM,EAAE,QAAQ,IAAI;AACpB,MAAI,CAAC,SAAS;AACZ,WAAO;AAAA,EACT;AAEA,MAAI,YAAY,OACd,SACG,qBAAqB,IAAI;AAE9B,aAAW,UAAU,SAAS;AAC5B,UAAM,gBACJ,qBAAqB,OAAO,IAAoC;AAClE,QAAI,CAAC,eAAe;AAClB,YAAM,IAAI,4BAA4B,OAAO,IAAI;AAAA,IACnD;AAEA,QAAI,CAAC,sCAAsC,aAAa,GAAG;AACzD,YAAM,IAAI;AAAA,QACR;AAAA;AAAA,MAEF;AAAA,IACF;AACA,gBAAY,cAAc,UAAU,WAAW,MAAM;AAAA,EACvD;AAEA,SAAO;AACT;","names":[]}
@@ -1,37 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
-
3
-
4
- var _chunkFYADAA2Gjs = require('./chunk-FYADAA2G.js');
5
-
6
- // src/Caveat.ts
7
- var _utils = require('@metamask/utils');
8
- function isRestrictedMethodCaveatSpecification(specification) {
9
- return _utils.hasProperty.call(void 0, specification, "decorator");
10
- }
11
- function decorateWithCaveats(methodImplementation, permission, caveatSpecifications) {
12
- const { caveats } = permission;
13
- if (!caveats) {
14
- return methodImplementation;
15
- }
16
- let decorated = async (args) => methodImplementation(args);
17
- for (const caveat of caveats) {
18
- const specification = caveatSpecifications[caveat.type];
19
- if (!specification) {
20
- throw new (0, _chunkFYADAA2Gjs.UnrecognizedCaveatTypeError)(caveat.type);
21
- }
22
- if (!isRestrictedMethodCaveatSpecification(specification)) {
23
- throw new (0, _chunkFYADAA2Gjs.CaveatSpecificationMismatchError)(
24
- specification,
25
- "RestrictedMethod" /* RestrictedMethod */
26
- );
27
- }
28
- decorated = specification.decorator(decorated, caveat);
29
- }
30
- return decorated;
31
- }
32
-
33
-
34
-
35
-
36
- exports.isRestrictedMethodCaveatSpecification = isRestrictedMethodCaveatSpecification; exports.decorateWithCaveats = decorateWithCaveats;
37
- //# sourceMappingURL=chunk-3YOPLPVY.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/Caveat.ts"],"names":[],"mappings":";;;;;;AACA,SAAS,mBAAmB;AAgSrB,SAAS,sCACd,eACgE;AAChE,SAAO,YAAY,eAAe,WAAW;AAC/C;AAaO,SAAS,oBAGd,sBACA,YACA,sBACoD;AACpD,QAAM,EAAE,QAAQ,IAAI;AACpB,MAAI,CAAC,SAAS;AACZ,WAAO;AAAA,EACT;AAEA,MAAI,YAAY,OACd,SACG,qBAAqB,IAAI;AAE9B,aAAW,UAAU,SAAS;AAC5B,UAAM,gBACJ,qBAAqB,OAAO,IAAoC;AAClE,QAAI,CAAC,eAAe;AAClB,YAAM,IAAI,4BAA4B,OAAO,IAAI;AAAA,IACnD;AAEA,QAAI,CAAC,sCAAsC,aAAa,GAAG;AACzD,YAAM,IAAI;AAAA,QACR;AAAA;AAAA,MAEF;AAAA,IACF;AACA,gBAAY,cAAc,UAAU,WAAW,MAAM;AAAA,EACvD;AAEA,SAAO;AACT","sourcesContent":["import type { Json } from '@metamask/utils';\nimport { hasProperty } from '@metamask/utils';\n\nimport {\n CaveatSpecificationMismatchError,\n UnrecognizedCaveatTypeError,\n} from './errors';\nimport type {\n AsyncRestrictedMethod,\n RestrictedMethod,\n PermissionConstraint,\n RestrictedMethodParameters,\n} from './Permission';\nimport { PermissionType } from './Permission';\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { PermissionController } from './PermissionController';\n\nexport type CaveatConstraint = {\n /**\n * The type of the caveat. The type is presumed to be meaningful in the\n * context of the capability it is associated with.\n *\n * In MetaMask, every permission can only have one caveat of each type.\n */\n readonly type: string;\n\n /**\n * Any additional data necessary to enforce the caveat.\n */\n readonly value: Json;\n};\n\n/**\n * A `ZCAP-LD`-like caveat object. A caveat is associated with a particular\n * permission, and stored in its `caveats` array. Conceptually, a caveat is\n * an arbitrary attenuation of the authority granted by its associated\n * permission. It is the responsibility of the host to interpret and apply\n * the restriction represented by a caveat.\n *\n * @template Type - The type of the caveat.\n * @template Value - The value associated with the caveat.\n */\nexport type Caveat<Type extends string, Value extends Json> = {\n /**\n * The type of the caveat. The type is presumed to be meaningful in the\n * context of the capability it is associated with.\n *\n * In MetaMask, every permission can only have one caveat of each type.\n */\n readonly type: Type;\n\n /**\n * Any additional data necessary to enforce the caveat.\n */\n readonly value: Value;\n};\n\n// Next, we define types used for specifying caveats at the consumer layer,\n// and a function for applying caveats to a restricted method request. This is\n// Accomplished by decorating the restricted method implementation with the\n// the corresponding caveat functions.\n\n/**\n * A function for applying caveats to a restricted method request.\n *\n * @template ParentCaveat - The caveat type associated with this decorator.\n * @param decorated - The restricted method implementation to be decorated.\n * The method may have already been decorated with other caveats.\n * @param caveat - The caveat object.\n * @returns The decorated restricted method implementation.\n */\nexport type CaveatDecorator<ParentCaveat extends CaveatConstraint> = (\n decorated: AsyncRestrictedMethod<RestrictedMethodParameters, Json>,\n caveat: ParentCaveat,\n) => AsyncRestrictedMethod<RestrictedMethodParameters, Json>;\n\n/**\n * Extracts a caveat value type from a caveat decorator.\n *\n * @template Decorator - The {@link CaveatDecorator} to extract a caveat value\n * type from.\n */\ntype ExtractCaveatValueFromDecorator<\n Decorator extends CaveatDecorator<CaveatConstraint>,\n> = Decorator extends (\n decorated: AsyncRestrictedMethod<RestrictedMethodParameters, Json>,\n caveat: infer ParentCaveat,\n) => AsyncRestrictedMethod<RestrictedMethodParameters, Json>\n ? ParentCaveat extends CaveatConstraint\n ? ParentCaveat['value']\n : never\n : never;\n\n/**\n * A function for validating caveats of a particular type.\n *\n * @see `validator` in {@link CaveatSpecificationBase} for more details.\n * @template ParentCaveat - The caveat type associated with this validator.\n * @param caveat - The caveat object to validate.\n * @param origin - The origin associated with the parent permission.\n * @param target - The target of the parent permission.\n */\nexport type CaveatValidator<ParentCaveat extends CaveatConstraint> = (\n caveat: { type: ParentCaveat['type']; value: unknown },\n origin?: string,\n target?: string,\n) => void;\n\n/**\n * A map of caveat type strings to {@link CaveatDiff} values.\n */\nexport type CaveatDiffMap<ParentCaveat extends CaveatConstraint> = {\n [CaveatType in ParentCaveat['type']]: ParentCaveat['value'];\n};\n\n/**\n * A function that merges two caveat values of the same type. The values must be\n * merged in the fashion of a right-biased union.\n *\n * @see `ARCHITECTURE.md` for more details.\n * @template Value - The type of the values to merge.\n * @param leftValue - The left-hand value.\n * @param rightValue - The right-hand value.\n * @returns `[newValue, diff]`, i.e. the merged value and the diff between the left value\n * and the new value. The diff must be expressed in the same type as the value itself.\n */\nexport type CaveatValueMerger<Value extends Json> = (\n leftValue: Value,\n rightValue: Value,\n) => [Value, Value] | [];\n\nexport type CaveatSpecificationBase = {\n /**\n * The string type of the caveat.\n */\n type: string;\n\n /**\n * The validator function used to validate caveats of the associated type\n * whenever they are instantiated. Caveat are instantiated whenever they are\n * created or mutated.\n *\n * The validator should throw an appropriate JSON-RPC error if validation fails.\n *\n * If no validator is specified, no validation of caveat values will be\n * performed. Although caveats can also be validated by permission validators,\n * validating caveat values separately is strongly recommended.\n */\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n validator?: CaveatValidator<any>;\n\n /**\n * The merger function used to merge a pair of values of the associated caveat type\n * during incremental permission requests. The values must be merged in the fashion\n * of a right-biased union.\n *\n * @see `ARCHITECTURE.md` for more details.\n */\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n merger?: CaveatValueMerger<any>;\n};\n\nexport type RestrictedMethodCaveatSpecificationConstraint =\n CaveatSpecificationBase & {\n /**\n * The decorator function used to apply the caveat to restricted method\n * requests.\n */\n decorator: CaveatDecorator<CaveatConstraint>;\n };\n\nexport type EndowmentCaveatSpecificationConstraint = CaveatSpecificationBase;\n\n/**\n * The constraint for caveat specification objects. Every {@link Caveat}\n * supported by a {@link PermissionController} must have an associated\n * specification, which is the source of truth for all caveat-related types.\n * In addition, a caveat specification may include a decorator function used\n * to apply the caveat's attenuation to a restricted method. It may also include\n * a validator function specified by the consumer.\n *\n * See the README for more details.\n */\nexport type CaveatSpecificationConstraint =\n | RestrictedMethodCaveatSpecificationConstraint\n | EndowmentCaveatSpecificationConstraint;\n\n/**\n * Options for {@link CaveatSpecificationBuilder} functions.\n */\ntype CaveatSpecificationBuilderOptions<\n DecoratorHooks extends Record<string, unknown>,\n ValidatorHooks extends Record<string, unknown>,\n> = {\n type?: string;\n decoratorHooks?: DecoratorHooks;\n validatorHooks?: ValidatorHooks;\n};\n\n/**\n * A function that builds caveat specifications. Modules that specify caveats\n * for external consumption should make this their primary / default export so\n * that host applications can use them to generate concrete specifications\n * tailored to their requirements.\n */\nexport type CaveatSpecificationBuilder<\n Options extends CaveatSpecificationBuilderOptions<\n Record<string, unknown>,\n Record<string, unknown>\n >,\n Specification extends CaveatSpecificationConstraint,\n> = (options: Options) => Specification;\n\n/**\n * A caveat specification export object, containing the\n * {@link CaveatSpecificationBuilder} function and \"hook name\" objects.\n */\nexport type CaveatSpecificationBuilderExportConstraint = {\n specificationBuilder: CaveatSpecificationBuilder<\n CaveatSpecificationBuilderOptions<\n Record<string, unknown>,\n Record<string, unknown>\n >,\n CaveatSpecificationConstraint\n >;\n decoratorHookNames?: Record<string, true>;\n validatorHookNames?: Record<string, true>;\n};\n\n/**\n * The specifications for all caveats supported by a particular\n * {@link PermissionController}.\n *\n * @template Specifications - The union of all {@link CaveatSpecificationConstraint} types.\n */\nexport type CaveatSpecificationMap<\n CaveatSpecification extends CaveatSpecificationConstraint,\n> = Record<CaveatSpecification['type'], CaveatSpecification>;\n\n/**\n * Extracts the union of all caveat types specified by the given\n * {@link CaveatSpecificationConstraint} type.\n *\n * @template CaveatSpecification - The {@link CaveatSpecificationConstraint} to extract a\n * caveat type union from.\n */\nexport type ExtractCaveats<\n CaveatSpecification extends CaveatSpecificationConstraint,\n> = CaveatSpecification extends RestrictedMethodCaveatSpecificationConstraint\n ? Caveat<\n CaveatSpecification['type'],\n ExtractCaveatValueFromDecorator<\n RestrictedMethodCaveatSpecificationConstraint['decorator']\n >\n >\n : Caveat<CaveatSpecification['type'], Json>;\n\n/**\n * Extracts the type of a specific {@link Caveat} from a union of caveat\n * specifications.\n *\n * @template CaveatSpecifications - The union of all caveat specifications.\n * @template CaveatType - The type of the caveat to extract.\n */\nexport type ExtractCaveat<\n CaveatSpecifications extends CaveatSpecificationConstraint,\n CaveatType extends string,\n> = Extract<ExtractCaveats<CaveatSpecifications>, { type: CaveatType }>;\n\n/**\n * Extracts the value type of a specific {@link Caveat} from a union of caveat\n * specifications.\n *\n * @template CaveatSpecifications - The union of all caveat specifications.\n * @template CaveatType - The type of the caveat whose value to extract.\n */\nexport type ExtractCaveatValue<\n CaveatSpecifications extends CaveatSpecificationConstraint,\n CaveatType extends string,\n> = ExtractCaveat<CaveatSpecifications, CaveatType>['value'];\n\n/**\n * Determines whether a caveat specification is a restricted method caveat specification.\n *\n * @param specification - The caveat specification.\n * @returns True if the caveat specification is a restricted method caveat specification, otherwise false.\n */\nexport function isRestrictedMethodCaveatSpecification(\n specification: CaveatSpecificationConstraint,\n): specification is RestrictedMethodCaveatSpecificationConstraint {\n return hasProperty(specification, 'decorator');\n}\n\n/**\n * Decorate a restricted method implementation with its caveats.\n *\n * Note that all caveat functions (i.e. the argument and return value of the\n * decorator) must be awaited.\n *\n * @param methodImplementation - The restricted method implementation\n * @param permission - The origin's potential permission\n * @param caveatSpecifications - All caveat implementations\n * @returns The decorated method implementation\n */\nexport function decorateWithCaveats<\n CaveatSpecifications extends CaveatSpecificationConstraint,\n>(\n methodImplementation: RestrictedMethod<RestrictedMethodParameters, Json>,\n permission: Readonly<PermissionConstraint>, // bound to the requesting origin\n caveatSpecifications: CaveatSpecificationMap<CaveatSpecifications>, // all caveat implementations\n): RestrictedMethod<RestrictedMethodParameters, Json> {\n const { caveats } = permission;\n if (!caveats) {\n return methodImplementation;\n }\n\n let decorated = async (\n args: Parameters<RestrictedMethod<RestrictedMethodParameters, Json>>[0],\n ) => methodImplementation(args);\n\n for (const caveat of caveats) {\n const specification =\n caveatSpecifications[caveat.type as CaveatSpecifications['type']];\n if (!specification) {\n throw new UnrecognizedCaveatTypeError(caveat.type);\n }\n\n if (!isRestrictedMethodCaveatSpecification(specification)) {\n throw new CaveatSpecificationMismatchError(\n specification,\n PermissionType.RestrictedMethod,\n );\n }\n decorated = specification.decorator(decorated, caveat);\n }\n\n return decorated;\n}\n"]}
@@ -1,155 +0,0 @@
1
- // src/SubjectMetadataController.ts
2
- import { BaseController } from "@metamask/base-controller";
3
- var controllerName = "SubjectMetadataController";
4
- var SubjectType = /* @__PURE__ */ ((SubjectType2) => {
5
- SubjectType2["Extension"] = "extension";
6
- SubjectType2["Internal"] = "internal";
7
- SubjectType2["Unknown"] = "unknown";
8
- SubjectType2["Website"] = "website";
9
- SubjectType2["Snap"] = "snap";
10
- return SubjectType2;
11
- })(SubjectType || {});
12
- var stateMetadata = {
13
- subjectMetadata: { persist: true, anonymous: false }
14
- };
15
- var defaultState = {
16
- subjectMetadata: {}
17
- };
18
- var SubjectMetadataController = class _SubjectMetadataController extends BaseController {
19
- constructor({
20
- messenger,
21
- subjectCacheLimit,
22
- state = {}
23
- }) {
24
- if (!Number.isInteger(subjectCacheLimit) || subjectCacheLimit < 1) {
25
- throw new Error(
26
- `subjectCacheLimit must be a positive integer. Received: "${subjectCacheLimit}"`
27
- );
28
- }
29
- const hasPermissions = (origin) => {
30
- return messenger.call("PermissionController:hasPermissions", origin);
31
- };
32
- super({
33
- name: controllerName,
34
- metadata: stateMetadata,
35
- messenger,
36
- state: {
37
- ..._SubjectMetadataController.getTrimmedState(state, hasPermissions)
38
- }
39
- });
40
- this.subjectHasPermissions = hasPermissions;
41
- this.subjectCacheLimit = subjectCacheLimit;
42
- this.subjectsWithoutPermissionsEncounteredSinceStartup = /* @__PURE__ */ new Set();
43
- this.messagingSystem.registerActionHandler(
44
- // ESLint is confused by the string literal type.
45
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
46
- `${this.name}:getSubjectMetadata`,
47
- this.getSubjectMetadata.bind(this)
48
- );
49
- this.messagingSystem.registerActionHandler(
50
- // ESLint is confused by the string literal type.
51
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
52
- `${this.name}:addSubjectMetadata`,
53
- this.addSubjectMetadata.bind(this)
54
- );
55
- }
56
- /**
57
- * Clears the state of this controller. Also resets the cache of subjects
58
- * encountered since startup, so as to not prematurely reach the cache limit.
59
- */
60
- clearState() {
61
- this.subjectsWithoutPermissionsEncounteredSinceStartup.clear();
62
- this.update((_draftState) => {
63
- return { ...defaultState };
64
- });
65
- }
66
- /**
67
- * Stores domain metadata for the given origin (subject). Deletes metadata for
68
- * subjects without permissions in a FIFO manner once more than
69
- * {@link SubjectMetadataController.subjectCacheLimit} distinct origins have
70
- * been added since boot.
71
- *
72
- * In order to prevent a degraded user experience,
73
- * metadata is never deleted for subjects with permissions, since metadata
74
- * cannot yet be requested on demand.
75
- *
76
- * @param metadata - The subject metadata to store.
77
- */
78
- addSubjectMetadata(metadata) {
79
- const { origin } = metadata;
80
- const newMetadata = {
81
- ...metadata,
82
- extensionId: metadata.extensionId || null,
83
- iconUrl: metadata.iconUrl || null,
84
- name: metadata.name || null,
85
- subjectType: metadata.subjectType || null
86
- };
87
- let originToForget = null;
88
- if (this.subjectsWithoutPermissionsEncounteredSinceStartup.size >= this.subjectCacheLimit) {
89
- const cachedOrigin = this.subjectsWithoutPermissionsEncounteredSinceStartup.values().next().value;
90
- this.subjectsWithoutPermissionsEncounteredSinceStartup.delete(
91
- cachedOrigin
92
- );
93
- if (!this.subjectHasPermissions(cachedOrigin)) {
94
- originToForget = cachedOrigin;
95
- }
96
- }
97
- this.subjectsWithoutPermissionsEncounteredSinceStartup.add(origin);
98
- this.update((draftState) => {
99
- draftState.subjectMetadata[origin] = newMetadata;
100
- if (typeof originToForget === "string") {
101
- delete draftState.subjectMetadata[originToForget];
102
- }
103
- });
104
- }
105
- /**
106
- * Gets the subject metadata for the given origin, if any.
107
- *
108
- * @param origin - The origin for which to get the subject metadata.
109
- * @returns The subject metadata, if any, or `undefined` otherwise.
110
- */
111
- getSubjectMetadata(origin) {
112
- return this.state.subjectMetadata[origin];
113
- }
114
- /**
115
- * Deletes all subjects without permissions from the controller's state.
116
- */
117
- trimMetadataState() {
118
- this.update((draftState) => {
119
- return _SubjectMetadataController.getTrimmedState(
120
- draftState,
121
- this.subjectHasPermissions
122
- );
123
- });
124
- }
125
- /**
126
- * Returns a new state object that only includes subjects with permissions.
127
- * This method is static because we want to call it in the constructor, before
128
- * the controller's state is initialized.
129
- *
130
- * @param state - The state object to trim.
131
- * @param hasPermissions - A function that returns a boolean indicating
132
- * whether a particular subject (identified by its origin) has any
133
- * permissions.
134
- * @returns The new state object. If the specified `state` object has no
135
- * subject metadata, the returned object will be equivalent to the default
136
- * state of this controller.
137
- */
138
- static getTrimmedState(state, hasPermissions) {
139
- const { subjectMetadata = {} } = state;
140
- return {
141
- subjectMetadata: Object.keys(subjectMetadata).reduce((newSubjectMetadata, origin) => {
142
- if (hasPermissions(origin)) {
143
- newSubjectMetadata[origin] = subjectMetadata[origin];
144
- }
145
- return newSubjectMetadata;
146
- }, {})
147
- };
148
- }
149
- };
150
-
151
- export {
152
- SubjectType,
153
- SubjectMetadataController
154
- };
155
- //# sourceMappingURL=chunk-42QSJHWO.mjs.map