@metamask-previews/permission-controller 4.1.0-preview.d32a7cc

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 (48) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/LICENSE +20 -0
  3. package/README.md +19 -0
  4. package/dist/Caveat.d.ts +183 -0
  5. package/dist/Caveat.d.ts.map +1 -0
  6. package/dist/Caveat.js +57 -0
  7. package/dist/Caveat.js.map +1 -0
  8. package/dist/Permission.d.ts +417 -0
  9. package/dist/Permission.d.ts.map +1 -0
  10. package/dist/Permission.js +66 -0
  11. package/dist/Permission.js.map +1 -0
  12. package/dist/PermissionController.d.ts +897 -0
  13. package/dist/PermissionController.d.ts.map +1 -0
  14. package/dist/PermissionController.js +1287 -0
  15. package/dist/PermissionController.js.map +1 -0
  16. package/dist/SubjectMetadataController.d.ts +110 -0
  17. package/dist/SubjectMetadataController.d.ts.map +1 -0
  18. package/dist/SubjectMetadataController.js +139 -0
  19. package/dist/SubjectMetadataController.js.map +1 -0
  20. package/dist/errors.d.ts +159 -0
  21. package/dist/errors.d.ts.map +1 -0
  22. package/dist/errors.js +196 -0
  23. package/dist/errors.js.map +1 -0
  24. package/dist/index.d.ts +8 -0
  25. package/dist/index.d.ts.map +1 -0
  26. package/dist/index.js +37 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/permission-middleware.d.ts +33 -0
  29. package/dist/permission-middleware.d.ts.map +1 -0
  30. package/dist/permission-middleware.js +64 -0
  31. package/dist/permission-middleware.js.map +1 -0
  32. package/dist/rpc-methods/getPermissions.d.ts +8 -0
  33. package/dist/rpc-methods/getPermissions.d.ts.map +1 -0
  34. package/dist/rpc-methods/getPermissions.js +38 -0
  35. package/dist/rpc-methods/getPermissions.js.map +1 -0
  36. package/dist/rpc-methods/index.d.ts +5 -0
  37. package/dist/rpc-methods/index.d.ts.map +1 -0
  38. package/dist/rpc-methods/index.js +10 -0
  39. package/dist/rpc-methods/index.js.map +1 -0
  40. package/dist/rpc-methods/requestPermissions.d.ts +17 -0
  41. package/dist/rpc-methods/requestPermissions.d.ts.map +1 -0
  42. package/dist/rpc-methods/requestPermissions.js +55 -0
  43. package/dist/rpc-methods/requestPermissions.js.map +1 -0
  44. package/dist/utils.d.ts +39 -0
  45. package/dist/utils.d.ts.map +1 -0
  46. package/dist/utils.js +9 -0
  47. package/dist/utils.js.map +1 -0
  48. package/package.json +62 -0
@@ -0,0 +1,417 @@
1
+ import type { ActionConstraint, EventConstraint } from '@metamask/base-controller';
2
+ import type { NonEmptyArray } from '@metamask/controller-utils';
3
+ import type { Json } from '@metamask/utils';
4
+ import type { CaveatConstraint } from './Caveat';
5
+ import type { PermissionsRequest, SideEffectMessenger } from './PermissionController';
6
+ import type { SubjectType } from './SubjectMetadataController';
7
+ /**
8
+ * The origin of a subject.
9
+ * Effectively the GUID of an entity that can have permissions.
10
+ */
11
+ export declare type OriginString = string;
12
+ /**
13
+ * The name of a permission target.
14
+ */
15
+ declare type TargetName = string;
16
+ /**
17
+ * A `ZCAP-LD`-like permission object. A permission is associated with a
18
+ * particular `invoker`, which is the holder of the permission. Possessing the
19
+ * permission grants access to a particular restricted resource, identified by
20
+ * the `parentCapability`. The use of the restricted resource may be further
21
+ * restricted by any `caveats` associated with the permission.
22
+ *
23
+ * See the README for details.
24
+ */
25
+ export declare type PermissionConstraint = {
26
+ /**
27
+ * The context(s) in which this capability is meaningful.
28
+ *
29
+ * It is required by the standard, but we make it optional since there is only
30
+ * one context in our usage (i.e. the user's MetaMask instance).
31
+ */
32
+ readonly '@context'?: NonEmptyArray<string>;
33
+ /**
34
+ * The caveats of the permission.
35
+ *
36
+ * @see {@link Caveat} For more information.
37
+ */
38
+ readonly caveats: null | NonEmptyArray<CaveatConstraint>;
39
+ /**
40
+ * The creation date of the permission, in UNIX epoch time.
41
+ */
42
+ readonly date: number;
43
+ /**
44
+ * The GUID of the permission object.
45
+ */
46
+ readonly id: string;
47
+ /**
48
+ * The origin string of the subject that has the permission.
49
+ */
50
+ readonly invoker: OriginString;
51
+ /**
52
+ * A pointer to the resource that possession of the capability grants
53
+ * access to, for example a JSON-RPC method or endowment.
54
+ */
55
+ readonly parentCapability: string;
56
+ };
57
+ /**
58
+ * A `ZCAP-LD`-like permission object. A permission is associated with a
59
+ * particular `invoker`, which is the holder of the permission. Possessing the
60
+ * permission grants access to a particular restricted resource, identified by
61
+ * the `parentCapability`. The use of the restricted resource may be further
62
+ * restricted by any `caveats` associated with the permission.
63
+ *
64
+ * See the README for details.
65
+ *
66
+ * @template Name - The name of the permission that the target corresponds to.
67
+ * @template AllowedCaveat - A union of the allowed {@link Caveat} types
68
+ * for the permission.
69
+ */
70
+ export declare type ValidPermission<Name extends TargetName, AllowedCaveat extends CaveatConstraint> = PermissionConstraint & {
71
+ /**
72
+ * The caveats of the permission.
73
+ *
74
+ * @see {@link Caveat} For more information.
75
+ */
76
+ readonly caveats: AllowedCaveat extends never ? null : NonEmptyArray<AllowedCaveat> | null;
77
+ /**
78
+ * A pointer to the resource that possession of the capability grants
79
+ * access to, for example a JSON-RPC method or endowment.
80
+ */
81
+ readonly parentCapability: Name;
82
+ };
83
+ /**
84
+ * Internal utility for extracting the members types of an array. The type
85
+ * evalutes to `never` if the specified type is the empty tuple or neither
86
+ * an array nor a tuple.
87
+ *
88
+ * @template ArrayType - The array type whose members to extract.
89
+ */
90
+ declare type ExtractArrayMembers<ArrayType> = ArrayType extends [] ? never : ArrayType extends any[] | readonly any[] ? ArrayType[number] : never;
91
+ /**
92
+ * A utility type for extracting the allowed caveat types for a particular
93
+ * permission from a permission specification type.
94
+ *
95
+ * @template PermissionSpecification - The permission specification type to
96
+ * extract valid caveat types from.
97
+ */
98
+ export declare type ExtractAllowedCaveatTypes<PermissionSpecification extends PermissionSpecificationConstraint> = ExtractArrayMembers<PermissionSpecification['allowedCaveats']>;
99
+ /**
100
+ * The options object of {@link constructPermission}.
101
+ *
102
+ * @template TargetPermission - The {@link Permission} that will be constructed.
103
+ */
104
+ export declare type PermissionOptions<TargetPermission extends PermissionConstraint> = {
105
+ target: TargetPermission['parentCapability'];
106
+ /**
107
+ * The origin string of the subject that has the permission.
108
+ */
109
+ invoker: OriginString;
110
+ /**
111
+ * The caveats of the permission.
112
+ * See {@link Caveat}.
113
+ */
114
+ caveats?: NonEmptyArray<CaveatConstraint>;
115
+ };
116
+ /**
117
+ * The default permission factory function. Naively constructs a permission from
118
+ * the inputs. Sets a default, random `id` if none is provided.
119
+ *
120
+ * @see {@link Permission} For more details.
121
+ * @template TargetPermission- - The {@link Permission} that will be constructed.
122
+ * @param options - The options for the permission.
123
+ * @returns The new permission object.
124
+ */
125
+ export declare function constructPermission<TargetPermission extends PermissionConstraint>(options: PermissionOptions<TargetPermission>): TargetPermission;
126
+ /**
127
+ * Gets the caveat of the specified type belonging to the specified permission.
128
+ *
129
+ * @param permission - The permission whose caveat to retrieve.
130
+ * @param caveatType - The type of the caveat to retrieve.
131
+ * @returns The caveat, or undefined if no such caveat exists.
132
+ */
133
+ export declare function findCaveat(permission: PermissionConstraint, caveatType: string): CaveatConstraint | undefined;
134
+ /**
135
+ * A requested permission object. Just an object with any of the properties
136
+ * of a {@link PermissionConstraint} object.
137
+ */
138
+ declare type RequestedPermission = Partial<PermissionConstraint>;
139
+ /**
140
+ * A record of target names and their {@link RequestedPermission} objects.
141
+ */
142
+ export declare type RequestedPermissions = Record<TargetName, RequestedPermission>;
143
+ /**
144
+ * The restricted method context object. Essentially a way to pass internal
145
+ * arguments to restricted methods and caveat functions, most importantly the
146
+ * requesting origin.
147
+ */
148
+ declare type RestrictedMethodContext = Readonly<{
149
+ origin: OriginString;
150
+ [key: string]: any;
151
+ }>;
152
+ export declare type RestrictedMethodParameters = Json[] | Record<string, Json> | void;
153
+ /**
154
+ * The arguments passed to a restricted method implementation.
155
+ *
156
+ * @template Params - The JSON-RPC parameters of the restricted method.
157
+ */
158
+ export declare type RestrictedMethodOptions<Params extends RestrictedMethodParameters> = {
159
+ method: TargetName;
160
+ params?: Params;
161
+ context: RestrictedMethodContext;
162
+ };
163
+ /**
164
+ * A synchronous restricted method implementation.
165
+ *
166
+ * @template Params - The JSON-RPC parameters of the restricted method.
167
+ * @template Result - The JSON-RPC result of the restricted method.
168
+ */
169
+ export declare type SyncRestrictedMethod<Params extends RestrictedMethodParameters, Result extends Json> = (args: RestrictedMethodOptions<Params>) => Result;
170
+ /**
171
+ * An asynchronous restricted method implementation.
172
+ *
173
+ * @template Params - The JSON-RPC parameters of the restricted method.
174
+ * @template Result - The JSON-RPC result of the restricted method.
175
+ */
176
+ export declare type AsyncRestrictedMethod<Params extends RestrictedMethodParameters, Result extends Json> = (args: RestrictedMethodOptions<Params>) => Promise<Result>;
177
+ /**
178
+ * A synchronous or asynchronous restricted method implementation.
179
+ *
180
+ * @template Params - The JSON-RPC parameters of the restricted method.
181
+ * @template Result - The JSON-RPC result of the restricted method.
182
+ */
183
+ export declare type RestrictedMethod<Params extends RestrictedMethodParameters, Result extends Json> = SyncRestrictedMethod<Params, Result> | AsyncRestrictedMethod<Params, Result>;
184
+ export declare type ValidRestrictedMethod<MethodImplementation extends RestrictedMethod<any, any>> = MethodImplementation extends (args: infer Options) => Json | Promise<Json> ? Options extends RestrictedMethodOptions<RestrictedMethodParameters> ? MethodImplementation : never : never;
185
+ /**
186
+ * {@link EndowmentGetter} parameter object.
187
+ */
188
+ export declare type EndowmentGetterParams = {
189
+ /**
190
+ * The origin of the requesting subject.
191
+ */
192
+ origin: string;
193
+ /**
194
+ * Any additional data associated with the request.
195
+ */
196
+ requestData?: unknown;
197
+ [key: string]: unknown;
198
+ };
199
+ /**
200
+ * A synchronous or asynchronous function that gets the endowments for a
201
+ * particular endowment permission. The getter receives the origin of the
202
+ * requesting subject and, optionally, additional request metadata.
203
+ */
204
+ export declare type EndowmentGetter<Endowments extends Json> = (options: EndowmentGetterParams) => Endowments | Promise<Endowments>;
205
+ export declare type PermissionFactory<TargetPermission extends PermissionConstraint, RequestData extends Record<string, unknown>> = (options: PermissionOptions<TargetPermission>, requestData?: RequestData) => TargetPermission;
206
+ export declare type PermissionValidatorConstraint = (permission: PermissionConstraint, origin?: OriginString, target?: string) => void;
207
+ /**
208
+ * The parameters passed to the side-effect function.
209
+ */
210
+ export declare type SideEffectParams<Actions extends ActionConstraint, Events extends EventConstraint> = {
211
+ requestData: PermissionsRequest;
212
+ messagingSystem: SideEffectMessenger<Actions, Events>;
213
+ };
214
+ /**
215
+ * A function that will execute actions as a permission side-effect.
216
+ */
217
+ export declare type SideEffectHandler<Actions extends ActionConstraint, Events extends EventConstraint> = (params: SideEffectParams<Actions, Events>) => Promise<unknown>;
218
+ /**
219
+ * The permissions side effects.
220
+ */
221
+ export declare type PermissionSideEffect<Actions extends ActionConstraint, Events extends EventConstraint> = {
222
+ /**
223
+ * A method triggered when the permission is accepted by the user
224
+ */
225
+ onPermitted: SideEffectHandler<Actions, Events>;
226
+ /**
227
+ * A method triggered if a `onPermitted` method rejected.
228
+ */
229
+ onFailure?: SideEffectHandler<Actions, Events>;
230
+ };
231
+ /**
232
+ * The different possible types of permissions.
233
+ */
234
+ export declare enum PermissionType {
235
+ /**
236
+ * A restricted JSON-RPC method. A subject must have the requisite permission
237
+ * to call a restricted JSON-RPC method.
238
+ */
239
+ RestrictedMethod = "RestrictedMethod",
240
+ /**
241
+ * An "endowment" granted to subjects that possess the requisite permission,
242
+ * such as a global environment variable exposing a restricted API, etc.
243
+ */
244
+ Endowment = "Endowment"
245
+ }
246
+ /**
247
+ * The base constraint for permission specification objects. Every
248
+ * {@link Permission} supported by a {@link PermissionController} must have an
249
+ * associated specification, which is the source of truth for all permission-
250
+ * related types. A permission specification includes the list of permitted
251
+ * caveats, and any factory and validation functions specified by the consumer.
252
+ * A concrete permission specification may specify further fields as necessary.
253
+ *
254
+ * See the README for more details.
255
+ */
256
+ declare type PermissionSpecificationBase<Type extends PermissionType> = {
257
+ /**
258
+ * The type of the specified permission.
259
+ */
260
+ permissionType: Type;
261
+ /**
262
+ * The name of the target resource of the permission.
263
+ */
264
+ targetName: string;
265
+ /**
266
+ * An array of the caveat types that may be added to instances of this
267
+ * permission.
268
+ */
269
+ allowedCaveats: Readonly<NonEmptyArray<string>> | null;
270
+ /**
271
+ * The factory function used to get permission objects. Permissions returned
272
+ * by this function are presumed to valid, and they will not be passed to the
273
+ * validator function associated with this specification (if any). In other
274
+ * words, the factory function should validate the permissions it creates.
275
+ *
276
+ * If no factory is specified, the {@link Permission} constructor will be
277
+ * used, and the validator function (if specified) will be called on newly
278
+ * constructed permissions.
279
+ */
280
+ factory?: PermissionFactory<any, Record<string, unknown>>;
281
+ /**
282
+ * The validator function used to validate permissions of the associated type
283
+ * whenever they are mutated. The only way a permission can be legally mutated
284
+ * is when its caveats are modified by the permission controller.
285
+ *
286
+ * The validator should throw an appropriate JSON-RPC error if validation fails.
287
+ */
288
+ validator?: PermissionValidatorConstraint;
289
+ /**
290
+ * The side-effect triggered by the {@link PermissionController} once the user approved it.
291
+ * The side-effect can only be an action allowed to be called inside the {@link PermissionController}.
292
+ *
293
+ * If the side-effect action fails, the permission that triggered it is revoked.
294
+ */
295
+ sideEffect?: PermissionSideEffect<any, any>;
296
+ /**
297
+ * The Permission may be available to only a subset of the subject types. If so, specify the subject types as an array.
298
+ * If a subject with a type not in this array tries to request the permission, the call will fail.
299
+ *
300
+ * Leaving this as undefined uses default behaviour where the permission is available to request for all subject types.
301
+ */
302
+ subjectTypes?: readonly SubjectType[];
303
+ };
304
+ /**
305
+ * The constraint for restricted method permission specification objects.
306
+ * Permissions that correspond to JSON-RPC methods are specified using objects
307
+ * that conform to this type.
308
+ *
309
+ * See the README for more details.
310
+ */
311
+ export declare type RestrictedMethodSpecificationConstraint = PermissionSpecificationBase<PermissionType.RestrictedMethod> & {
312
+ /**
313
+ * The implementation of the restricted method that the permission
314
+ * corresponds to.
315
+ */
316
+ methodImplementation: RestrictedMethod<any, any>;
317
+ };
318
+ /**
319
+ * The constraint for endowment permission specification objects. Permissions
320
+ * that endow callers with some restricted resource are specified using objects
321
+ * that conform to this type.
322
+ *
323
+ * See the README for more details.
324
+ */
325
+ export declare type EndowmentSpecificationConstraint = PermissionSpecificationBase<PermissionType.Endowment> & {
326
+ /**
327
+ * The {@link EndowmentGetter} function for the permission. This function
328
+ * will be called by the {@link PermissionController} whenever the
329
+ * permission is invoked, after which the host can apply the endowments to
330
+ * the requesting subject in the intended manner.
331
+ */
332
+ endowmentGetter: EndowmentGetter<any>;
333
+ };
334
+ /**
335
+ * The constraint for permission specification objects. Every {@link Permission}
336
+ * supported by a {@link PermissionController} must have an associated
337
+ * specification, which is the source of truth for all permission-related types.
338
+ * All specifications must adhere to the {@link PermissionSpecificationBase}
339
+ * interface, but specifications may have different fields depending on the
340
+ * {@link PermissionType}.
341
+ *
342
+ * See the README for more details.
343
+ */
344
+ export declare type PermissionSpecificationConstraint = EndowmentSpecificationConstraint | RestrictedMethodSpecificationConstraint;
345
+ /**
346
+ * Options for {@link PermissionSpecificationBuilder} functions.
347
+ */
348
+ declare type PermissionSpecificationBuilderOptions<FactoryHooks extends Record<string, unknown>, MethodHooks extends Record<string, unknown>, ValidatorHooks extends Record<string, unknown>> = {
349
+ targetName?: string;
350
+ allowedCaveats?: Readonly<NonEmptyArray<string>> | null;
351
+ factoryHooks?: FactoryHooks;
352
+ methodHooks?: MethodHooks;
353
+ validatorHooks?: ValidatorHooks;
354
+ };
355
+ /**
356
+ * A function that builds a permission specification. Modules that specify
357
+ * permissions for external consumption should make this their primary /
358
+ * default export so that host applications can use them to generate concrete
359
+ * specifications tailored to their requirements.
360
+ */
361
+ export declare type PermissionSpecificationBuilder<Type extends PermissionType, Options extends PermissionSpecificationBuilderOptions<any, any, any>, Specification extends PermissionSpecificationConstraint & {
362
+ permissionType: Type;
363
+ }> = (options: Options) => Specification;
364
+ /**
365
+ * A restricted method permission export object, containing the
366
+ * {@link PermissionSpecificationBuilder} function and "hook name" objects.
367
+ */
368
+ export declare type PermissionSpecificationBuilderExportConstraint = {
369
+ targetName: string;
370
+ specificationBuilder: PermissionSpecificationBuilder<PermissionType, PermissionSpecificationBuilderOptions<any, any, any>, PermissionSpecificationConstraint>;
371
+ factoryHookNames?: Record<string, true>;
372
+ methodHookNames?: Record<string, true>;
373
+ validatorHookNames?: Record<string, true>;
374
+ };
375
+ declare type ValidRestrictedMethodSpecification<Specification extends RestrictedMethodSpecificationConstraint> = Specification['methodImplementation'] extends ValidRestrictedMethod<Specification['methodImplementation']> ? Specification : never;
376
+ /**
377
+ * Constraint for {@link PermissionSpecificationConstraint} objects that
378
+ * evaluates to `never` if the specification contains any invalid fields.
379
+ *
380
+ * @template Specification - The permission specification to validate.
381
+ */
382
+ export declare type ValidPermissionSpecification<Specification extends PermissionSpecificationConstraint> = Specification['targetName'] extends TargetName ? Specification['permissionType'] extends PermissionType.Endowment ? Specification : Specification['permissionType'] extends PermissionType.RestrictedMethod ? ValidRestrictedMethodSpecification<Extract<Specification, RestrictedMethodSpecificationConstraint>> : never : never;
383
+ /**
384
+ * Checks that the specification has the expected permission type.
385
+ *
386
+ * @param specification - The specification to check.
387
+ * @param expectedType - The expected permission type.
388
+ * @template Specification - The specification to check.
389
+ * @template Type - The expected permission type.
390
+ * @returns Whether or not the specification is of the expected type.
391
+ */
392
+ export declare function hasSpecificationType<Specification extends PermissionSpecificationConstraint, Type extends PermissionType>(specification: Specification, expectedType: Type): specification is Specification & {
393
+ permissionType: Type;
394
+ };
395
+ /**
396
+ * The specifications for all permissions supported by a particular
397
+ * {@link PermissionController}.
398
+ *
399
+ * @template Specifications - The union of all {@link PermissionSpecificationConstraint} types.
400
+ */
401
+ export declare type PermissionSpecificationMap<Specification extends PermissionSpecificationConstraint> = {
402
+ [Name in Specification['targetName']]: Specification extends {
403
+ targetName: Name;
404
+ } ? Specification : never;
405
+ };
406
+ /**
407
+ * Extracts a specific {@link PermissionSpecificationConstraint} from a union of
408
+ * permission specifications.
409
+ *
410
+ * @template Specification - The specification union type to extract from.
411
+ * @template Name - The `targetName` of the specification to extract.
412
+ */
413
+ export declare type ExtractPermissionSpecification<Specification extends PermissionSpecificationConstraint, Name extends Specification['targetName']> = Specification extends {
414
+ targetName: Name;
415
+ } ? Specification : never;
416
+ export {};
417
+ //# sourceMappingURL=Permission.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Permission.d.ts","sourceRoot":"","sources":["../src/Permission.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,eAAe,EAChB,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAI5C,OAAO,KAAK,EAAE,gBAAgB,EAAU,MAAM,UAAU,CAAC;AACzD,OAAO,KAAK,EAGV,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAE/D;;;GAGG;AACH,oBAAY,YAAY,GAAG,MAAM,CAAC;AAElC;;GAEG;AACH,aAAK,UAAU,GAAG,MAAM,CAAC;AAEzB;;;;;;;;GAQG;AACH,oBAAY,oBAAoB,GAAG;IACjC;;;;;OAKG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IAG5C;;;;OAIG;IACH,QAAQ,CAAC,OAAO,EAAE,IAAI,GAAG,aAAa,CAAC,gBAAgB,CAAC,CAAC;IAEzD;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC;IAE/B;;;OAGG;IACH,QAAQ,CAAC,gBAAgB,EAAE,MAAM,CAAC;CACnC,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,oBAAY,eAAe,CACzB,IAAI,SAAS,UA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;AAEV;;;;;GAKG;AACH,oBAAY,4BAA4B,CACtC,aAAa,SAAS,iCAAiC,IACrD,aAAa,CAAC,YAAY,CAAC,SAAS,UAAU,GAC9C,aAAa,CAAC,gBAAgB,CAAC,SAAS,cAAc,CAAC,SAAS,GAC9D,aAAa,GACb,aAAa,CAAC,gBAAgB,CAAC,SAAS,cAAc,CAAC,gBAAgB,GACvE,kCAAkC,CAChC,OAAO,CAAC,aAAa,EAAE,uCAAuC,CAAC,CAChE,GACD,KAAK,GACP,KAAK,CAAC;AAEV;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAClC,aAAa,SAAS,iCAAiC,EACvD,IAAI,SAAS,cAAc,EAE3B,aAAa,EAAE,aAAa,EAC5B,YAAY,EAAE,IAAI,GACjB,aAAa,IAAI,aAAa,GAAG;IAClC,cAAc,EAAE,IAAI,CAAC;CACtB,CAEA;AAED;;;;;GAKG;AACH,oBAAY,0BAA0B,CACpC,aAAa,SAAS,iCAAiC,IACrD;KACD,IAAI,IAAI,aAAa,CAAC,YAAY,CAAC,GAAG,aAAa,SAAS;QAC3D,UAAU,EAAE,IAAI,CAAC;KAClB,GACG,aAAa,GACb,KAAK;CACV,CAAC;AAEF;;;;;;GAMG;AACH,oBAAY,8BAA8B,CACxC,aAAa,SAAS,iCAAiC,EACvD,IAAI,SAAS,aAAa,CAAC,YAAY,CAAC,IACtC,aAAa,SAAS;IACxB,UAAU,EAAE,IAAI,CAAC;CAClB,GACG,aAAa,GACb,KAAK,CAAC"}
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hasSpecificationType = exports.PermissionType = exports.findCaveat = exports.constructPermission = void 0;
4
+ const nanoid_1 = require("nanoid");
5
+ /**
6
+ * The default permission factory function. Naively constructs a permission from
7
+ * the inputs. Sets a default, random `id` if none is provided.
8
+ *
9
+ * @see {@link Permission} For more details.
10
+ * @template TargetPermission- - The {@link Permission} that will be constructed.
11
+ * @param options - The options for the permission.
12
+ * @returns The new permission object.
13
+ */
14
+ function constructPermission(options) {
15
+ const { caveats = null, invoker, target } = options;
16
+ return {
17
+ id: (0, nanoid_1.nanoid)(),
18
+ parentCapability: target,
19
+ invoker,
20
+ caveats,
21
+ date: new Date().getTime(),
22
+ };
23
+ }
24
+ exports.constructPermission = constructPermission;
25
+ /**
26
+ * Gets the caveat of the specified type belonging to the specified permission.
27
+ *
28
+ * @param permission - The permission whose caveat to retrieve.
29
+ * @param caveatType - The type of the caveat to retrieve.
30
+ * @returns The caveat, or undefined if no such caveat exists.
31
+ */
32
+ function findCaveat(permission, caveatType) {
33
+ var _a;
34
+ return (_a = permission.caveats) === null || _a === void 0 ? void 0 : _a.find((caveat) => caveat.type === caveatType);
35
+ }
36
+ exports.findCaveat = findCaveat;
37
+ /**
38
+ * The different possible types of permissions.
39
+ */
40
+ var PermissionType;
41
+ (function (PermissionType) {
42
+ /**
43
+ * A restricted JSON-RPC method. A subject must have the requisite permission
44
+ * to call a restricted JSON-RPC method.
45
+ */
46
+ PermissionType["RestrictedMethod"] = "RestrictedMethod";
47
+ /**
48
+ * An "endowment" granted to subjects that possess the requisite permission,
49
+ * such as a global environment variable exposing a restricted API, etc.
50
+ */
51
+ PermissionType["Endowment"] = "Endowment";
52
+ })(PermissionType = exports.PermissionType || (exports.PermissionType = {}));
53
+ /**
54
+ * Checks that the specification has the expected permission type.
55
+ *
56
+ * @param specification - The specification to check.
57
+ * @param expectedType - The expected permission type.
58
+ * @template Specification - The specification to check.
59
+ * @template Type - The expected permission type.
60
+ * @returns Whether or not the specification is of the expected type.
61
+ */
62
+ function hasSpecificationType(specification, expectedType) {
63
+ return specification.permissionType === expectedType;
64
+ }
65
+ exports.hasSpecificationType = hasSpecificationType;
66
+ //# sourceMappingURL=Permission.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Permission.js","sourceRoot":"","sources":["../src/Permission.ts"],"names":[],"mappings":";;;AAMA,mCAAgC;AAoJhC;;;;;;;;GAQG;AACH,SAAgB,mBAAmB,CAEjC,OAA4C;IAC5C,MAAM,EAAE,OAAO,GAAG,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAEpD,OAAO;QACL,EAAE,EAAE,IAAA,eAAM,GAAE;QACZ,gBAAgB,EAAE,MAAM;QACxB,OAAO;QACP,OAAO;QACP,IAAI,EAAE,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE;KACP,CAAC;AACxB,CAAC;AAZD,kDAYC;AAED;;;;;;GAMG;AACH,SAAgB,UAAU,CACxB,UAAgC,EAChC,UAAkB;;IAElB,OAAO,MAAA,UAAU,CAAC,OAAO,0CAAE,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC;AAC1E,CAAC;AALD,gCAKC;AA4JD;;GAEG;AACH,IAAY,cAYX;AAZD,WAAY,cAAc;IACxB;;;OAGG;IACH,uDAAqC,CAAA;IAErC;;;OAGG;IACH,yCAAuB,CAAA;AACzB,CAAC,EAZW,cAAc,GAAd,sBAAc,KAAd,sBAAc,QAYzB;AA0LD;;;;;;;;GAQG;AACH,SAAgB,oBAAoB,CAIlC,aAA4B,EAC5B,YAAkB;IAIlB,OAAO,aAAa,CAAC,cAAc,KAAK,YAAY,CAAC;AACvD,CAAC;AAVD,oDAUC","sourcesContent":["import type {\n ActionConstraint,\n EventConstraint,\n} from '@metamask/base-controller';\nimport type { NonEmptyArray } from '@metamask/controller-utils';\nimport type { Json } from '@metamask/utils';\nimport { nanoid } from 'nanoid';\n\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { CaveatConstraint, Caveat } from './Caveat';\nimport type {\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n PermissionController,\n PermissionsRequest,\n SideEffectMessenger,\n} from './PermissionController';\nimport type { SubjectType } from './SubjectMetadataController';\n\n/**\n * The origin of a subject.\n * Effectively the GUID of an entity that can have permissions.\n */\nexport type OriginString = string;\n\n/**\n * The name of a permission target.\n */\ntype TargetName = string;\n\n/**\n * A `ZCAP-LD`-like permission object. A permission is associated with a\n * particular `invoker`, which is the holder of the permission. Possessing the\n * permission grants access to a particular restricted resource, identified by\n * the `parentCapability`. The use of the restricted resource may be further\n * restricted by any `caveats` associated with the permission.\n *\n * See the README for details.\n */\nexport type PermissionConstraint = {\n /**\n * The context(s) in which this capability is meaningful.\n *\n * It is required by the standard, but we make it optional since there is only\n * one context in our usage (i.e. the user's MetaMask instance).\n */\n readonly '@context'?: NonEmptyArray<string>;\n\n // TODO:TS4.4 Make optional\n /**\n * The caveats of the permission.\n *\n * @see {@link Caveat} For more information.\n */\n readonly caveats: null | NonEmptyArray<CaveatConstraint>;\n\n /**\n * The creation date of the permission, in UNIX epoch time.\n */\n readonly date: number;\n\n /**\n * The GUID of the permission object.\n */\n readonly id: string;\n\n /**\n * The origin string of the subject that has the permission.\n */\n readonly invoker: OriginString;\n\n /**\n * A pointer to the resource that possession of the capability grants\n * access to, for example a JSON-RPC method or endowment.\n */\n readonly parentCapability: string;\n};\n\n/**\n * A `ZCAP-LD`-like permission object. A permission is associated with a\n * particular `invoker`, which is the holder of the permission. Possessing the\n * permission grants access to a particular restricted resource, identified by\n * the `parentCapability`. The use of the restricted resource may be further\n * restricted by any `caveats` associated with the permission.\n *\n * See the README for details.\n *\n * @template Name - The name of the permission that the target corresponds to.\n * @template AllowedCaveat - A union of the allowed {@link Caveat} types\n * for the permission.\n */\nexport type ValidPermission<\n Name extends TargetName,\n AllowedCaveat extends CaveatConstraint,\n> = PermissionConstraint & {\n // TODO:TS4.4 Make optional\n /**\n * The caveats of the permission.\n *\n * @see {@link Caveat} For more information.\n */\n readonly caveats: AllowedCaveat extends never\n ? null\n : NonEmptyArray<AllowedCaveat> | null;\n\n /**\n * A pointer to the resource that possession of the capability grants\n * access to, for example a JSON-RPC method or endowment.\n */\n readonly parentCapability: Name;\n};\n\n/**\n * Internal utility for extracting the members types of an array. The type\n * evalutes to `never` if the specified type is the empty tuple or neither\n * an array nor a tuple.\n *\n * @template ArrayType - The array type whose members to extract.\n */\ntype ExtractArrayMembers<ArrayType> = ArrayType extends []\n ? never\n : ArrayType extends any[] | readonly any[]\n ? ArrayType[number]\n : never;\n\n/**\n * A utility type for extracting the allowed caveat types for a particular\n * permission from a permission specification type.\n *\n * @template PermissionSpecification - The permission specification type to\n * extract valid caveat types from.\n */\nexport type ExtractAllowedCaveatTypes<\n PermissionSpecification extends PermissionSpecificationConstraint,\n> = ExtractArrayMembers<PermissionSpecification['allowedCaveats']>;\n\n/**\n * The options object of {@link constructPermission}.\n *\n * @template TargetPermission - The {@link Permission} that will be constructed.\n */\nexport type PermissionOptions<TargetPermission extends PermissionConstraint> = {\n target: TargetPermission['parentCapability'];\n /**\n * The origin string of the subject that has the permission.\n */\n invoker: OriginString;\n\n /**\n * The caveats of the permission.\n * See {@link Caveat}.\n */\n caveats?: NonEmptyArray<CaveatConstraint>;\n};\n\n/**\n * The default permission factory function. Naively constructs a permission from\n * the inputs. Sets a default, random `id` if none is provided.\n *\n * @see {@link Permission} For more details.\n * @template TargetPermission- - The {@link Permission} that will be constructed.\n * @param options - The options for the permission.\n * @returns The new permission object.\n */\nexport function constructPermission<\n TargetPermission extends PermissionConstraint,\n>(options: PermissionOptions<TargetPermission>): TargetPermission {\n const { caveats = null, invoker, target } = options;\n\n return {\n id: nanoid(),\n parentCapability: target,\n invoker,\n caveats,\n date: new Date().getTime(),\n } as TargetPermission;\n}\n\n/**\n * Gets the caveat of the specified type belonging to the specified permission.\n *\n * @param permission - The permission whose caveat to retrieve.\n * @param caveatType - The type of the caveat to retrieve.\n * @returns The caveat, or undefined if no such caveat exists.\n */\nexport function findCaveat(\n permission: PermissionConstraint,\n caveatType: string,\n): CaveatConstraint | undefined {\n return permission.caveats?.find((caveat) => caveat.type === caveatType);\n}\n\n/**\n * A requested permission object. Just an object with any of the properties\n * of a {@link PermissionConstraint} object.\n */\ntype RequestedPermission = Partial<PermissionConstraint>;\n\n/**\n * A record of target names and their {@link RequestedPermission} objects.\n */\nexport type RequestedPermissions = Record<TargetName, RequestedPermission>;\n\n/**\n * The restricted method context object. Essentially a way to pass internal\n * arguments to restricted methods and caveat functions, most importantly the\n * requesting origin.\n */\ntype RestrictedMethodContext = Readonly<{\n origin: OriginString;\n [key: string]: any;\n}>;\n\nexport type RestrictedMethodParameters = Json[] | Record<string, Json> | void;\n\n/**\n * The arguments passed to a restricted method implementation.\n *\n * @template Params - The JSON-RPC parameters of the restricted method.\n */\nexport type RestrictedMethodOptions<Params extends RestrictedMethodParameters> =\n {\n method: TargetName;\n params?: Params;\n context: RestrictedMethodContext;\n };\n\n/**\n * A synchronous restricted method implementation.\n *\n * @template Params - The JSON-RPC parameters of the restricted method.\n * @template Result - The JSON-RPC result of the restricted method.\n */\nexport type SyncRestrictedMethod<\n Params extends RestrictedMethodParameters,\n Result extends Json,\n> = (args: RestrictedMethodOptions<Params>) => Result;\n\n/**\n * An asynchronous restricted method implementation.\n *\n * @template Params - The JSON-RPC parameters of the restricted method.\n * @template Result - The JSON-RPC result of the restricted method.\n */\nexport type AsyncRestrictedMethod<\n Params extends RestrictedMethodParameters,\n Result extends Json,\n> = (args: RestrictedMethodOptions<Params>) => Promise<Result>;\n\n/**\n * A synchronous or asynchronous restricted method implementation.\n *\n * @template Params - The JSON-RPC parameters of the restricted method.\n * @template Result - The JSON-RPC result of the restricted method.\n */\nexport type RestrictedMethod<\n Params extends RestrictedMethodParameters,\n Result extends Json,\n> =\n | SyncRestrictedMethod<Params, Result>\n | AsyncRestrictedMethod<Params, Result>;\n\nexport type ValidRestrictedMethod<\n MethodImplementation extends RestrictedMethod<any, any>,\n> = MethodImplementation extends (args: infer Options) => Json | Promise<Json>\n ? Options extends RestrictedMethodOptions<RestrictedMethodParameters>\n ? MethodImplementation\n : never\n : never;\n\n/**\n * {@link EndowmentGetter} parameter object.\n */\nexport type EndowmentGetterParams = {\n /**\n * The origin of the requesting subject.\n */\n origin: string;\n\n /**\n * Any additional data associated with the request.\n */\n requestData?: unknown;\n\n [key: string]: unknown;\n};\n\n/**\n * A synchronous or asynchronous function that gets the endowments for a\n * particular endowment permission. The getter receives the origin of the\n * requesting subject and, optionally, additional request metadata.\n */\nexport type EndowmentGetter<Endowments extends Json> = (\n options: EndowmentGetterParams,\n) => Endowments | Promise<Endowments>;\n\nexport type PermissionFactory<\n TargetPermission extends PermissionConstraint,\n RequestData extends Record<string, unknown>,\n> = (\n options: PermissionOptions<TargetPermission>,\n requestData?: RequestData,\n) => TargetPermission;\n\nexport type PermissionValidatorConstraint = (\n permission: PermissionConstraint,\n origin?: OriginString,\n target?: string,\n) => void;\n\n/**\n * The parameters passed to the side-effect function.\n */\nexport type SideEffectParams<\n Actions extends ActionConstraint,\n Events extends EventConstraint,\n> = {\n requestData: PermissionsRequest;\n messagingSystem: SideEffectMessenger<Actions, Events>;\n};\n\n/**\n * A function that will execute actions as a permission side-effect.\n */\nexport type SideEffectHandler<\n Actions extends ActionConstraint,\n Events extends EventConstraint,\n> = (params: SideEffectParams<Actions, Events>) => Promise<unknown>;\n\n/**\n * The permissions side effects.\n */\nexport type PermissionSideEffect<\n Actions extends ActionConstraint,\n Events extends EventConstraint,\n> = {\n /**\n * A method triggered when the permission is accepted by the user\n */\n onPermitted: SideEffectHandler<Actions, Events>;\n /**\n * A method triggered if a `onPermitted` method rejected.\n */\n onFailure?: SideEffectHandler<Actions, Events>;\n};\n\n/**\n * The different possible types of permissions.\n */\nexport enum PermissionType {\n /**\n * A restricted JSON-RPC method. A subject must have the requisite permission\n * to call a restricted JSON-RPC method.\n */\n RestrictedMethod = 'RestrictedMethod',\n\n /**\n * An \"endowment\" granted to subjects that possess the requisite permission,\n * such as a global environment variable exposing a restricted API, etc.\n */\n Endowment = 'Endowment',\n}\n\n/**\n * The base constraint for permission specification objects. Every\n * {@link Permission} supported by a {@link PermissionController} must have an\n * associated specification, which is the source of truth for all permission-\n * related types. A permission specification includes the list of permitted\n * caveats, and any factory and validation functions specified by the consumer.\n * A concrete permission specification may specify further fields as necessary.\n *\n * See the README for more details.\n */\ntype PermissionSpecificationBase<Type extends PermissionType> = {\n /**\n * The type of the specified permission.\n */\n permissionType: Type;\n\n /**\n * The name of the target resource of the permission.\n */\n targetName: string;\n\n /**\n * An array of the caveat types that may be added to instances of this\n * permission.\n */\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n\n /**\n * The factory function used to get permission objects. Permissions returned\n * by this function are presumed to valid, and they will not be passed to the\n * validator function associated with this specification (if any). In other\n * words, the factory function should validate the permissions it creates.\n *\n * If no factory is specified, the {@link Permission} constructor will be\n * used, and the validator function (if specified) will be called on newly\n * constructed permissions.\n */\n factory?: PermissionFactory<any, Record<string, unknown>>;\n\n /**\n * The validator function used to validate permissions of the associated type\n * whenever they are mutated. The only way a permission can be legally mutated\n * is when its caveats are modified by the permission controller.\n *\n * The validator should throw an appropriate JSON-RPC error if validation fails.\n */\n validator?: PermissionValidatorConstraint;\n\n /**\n * The side-effect triggered by the {@link PermissionController} once the user approved it.\n * The side-effect can only be an action allowed to be called inside the {@link PermissionController}.\n *\n * If the side-effect action fails, the permission that triggered it is revoked.\n */\n sideEffect?: PermissionSideEffect<any, any>;\n\n /**\n * The Permission may be available to only a subset of the subject types. If so, specify the subject types as an array.\n * If a subject with a type not in this array tries to request the permission, the call will fail.\n *\n * Leaving this as undefined uses default behaviour where the permission is available to request for all subject types.\n */\n subjectTypes?: readonly SubjectType[];\n};\n\n/**\n * The constraint for restricted method permission specification objects.\n * Permissions that correspond to JSON-RPC methods are specified using objects\n * that conform to this type.\n *\n * See the README for more details.\n */\nexport type RestrictedMethodSpecificationConstraint =\n PermissionSpecificationBase<PermissionType.RestrictedMethod> & {\n /**\n * The implementation of the restricted method that the permission\n * corresponds to.\n */\n methodImplementation: RestrictedMethod<any, any>;\n };\n\n/**\n * The constraint for endowment permission specification objects. Permissions\n * that endow callers with some restricted resource are specified using objects\n * that conform to this type.\n *\n * See the README for more details.\n */\nexport type EndowmentSpecificationConstraint =\n PermissionSpecificationBase<PermissionType.Endowment> & {\n /**\n * The {@link EndowmentGetter} function for the permission. This function\n * will be called by the {@link PermissionController} whenever the\n * permission is invoked, after which the host can apply the endowments to\n * the requesting subject in the intended manner.\n */\n endowmentGetter: EndowmentGetter<any>;\n };\n\n/**\n * The constraint for permission specification objects. Every {@link Permission}\n * supported by a {@link PermissionController} must have an associated\n * specification, which is the source of truth for all permission-related types.\n * All specifications must adhere to the {@link PermissionSpecificationBase}\n * interface, but specifications may have different fields depending on the\n * {@link PermissionType}.\n *\n * See the README for more details.\n */\nexport type PermissionSpecificationConstraint =\n | EndowmentSpecificationConstraint\n | RestrictedMethodSpecificationConstraint;\n\n/**\n * Options for {@link PermissionSpecificationBuilder} functions.\n */\ntype PermissionSpecificationBuilderOptions<\n FactoryHooks extends Record<string, unknown>,\n MethodHooks extends Record<string, unknown>,\n ValidatorHooks extends Record<string, unknown>,\n> = {\n targetName?: string;\n allowedCaveats?: Readonly<NonEmptyArray<string>> | null;\n factoryHooks?: FactoryHooks;\n methodHooks?: MethodHooks;\n validatorHooks?: ValidatorHooks;\n};\n\n/**\n * A function that builds a permission specification. Modules that specify\n * permissions for external consumption should make this their primary /\n * default export so that host applications can use them to generate concrete\n * specifications tailored to their requirements.\n */\nexport type PermissionSpecificationBuilder<\n Type extends PermissionType,\n Options extends PermissionSpecificationBuilderOptions<any, any, any>,\n Specification extends PermissionSpecificationConstraint & {\n permissionType: Type;\n },\n> = (options: Options) => Specification;\n\n/**\n * A restricted method permission export object, containing the\n * {@link PermissionSpecificationBuilder} function and \"hook name\" objects.\n */\nexport type PermissionSpecificationBuilderExportConstraint = {\n targetName: string;\n specificationBuilder: PermissionSpecificationBuilder<\n PermissionType,\n PermissionSpecificationBuilderOptions<any, any, any>,\n PermissionSpecificationConstraint\n >;\n factoryHookNames?: Record<string, true>;\n methodHookNames?: Record<string, true>;\n validatorHookNames?: Record<string, true>;\n};\n\ntype ValidRestrictedMethodSpecification<\n Specification extends RestrictedMethodSpecificationConstraint,\n> = Specification['methodImplementation'] extends ValidRestrictedMethod<\n Specification['methodImplementation']\n>\n ? Specification\n : never;\n\n/**\n * Constraint for {@link PermissionSpecificationConstraint} objects that\n * evaluates to `never` if the specification contains any invalid fields.\n *\n * @template Specification - The permission specification to validate.\n */\nexport type ValidPermissionSpecification<\n Specification extends PermissionSpecificationConstraint,\n> = Specification['targetName'] extends TargetName\n ? Specification['permissionType'] extends PermissionType.Endowment\n ? Specification\n : Specification['permissionType'] extends PermissionType.RestrictedMethod\n ? ValidRestrictedMethodSpecification<\n Extract<Specification, RestrictedMethodSpecificationConstraint>\n >\n : never\n : never;\n\n/**\n * Checks that the specification has the expected permission type.\n *\n * @param specification - The specification to check.\n * @param expectedType - The expected permission type.\n * @template Specification - The specification to check.\n * @template Type - The expected permission type.\n * @returns Whether or not the specification is of the expected type.\n */\nexport function hasSpecificationType<\n Specification extends PermissionSpecificationConstraint,\n Type extends PermissionType,\n>(\n specification: Specification,\n expectedType: Type,\n): specification is Specification & {\n permissionType: Type;\n} {\n return specification.permissionType === expectedType;\n}\n\n/**\n * The specifications for all permissions supported by a particular\n * {@link PermissionController}.\n *\n * @template Specifications - The union of all {@link PermissionSpecificationConstraint} types.\n */\nexport type PermissionSpecificationMap<\n Specification extends PermissionSpecificationConstraint,\n> = {\n [Name in Specification['targetName']]: Specification extends {\n targetName: Name;\n }\n ? Specification\n : never;\n};\n\n/**\n * Extracts a specific {@link PermissionSpecificationConstraint} from a union of\n * permission specifications.\n *\n * @template Specification - The specification union type to extract from.\n * @template Name - The `targetName` of the specification to extract.\n */\nexport type ExtractPermissionSpecification<\n Specification extends PermissionSpecificationConstraint,\n Name extends Specification['targetName'],\n> = Specification extends {\n targetName: Name;\n}\n ? Specification\n : never;\n"]}