@metamask/permission-controller 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,422 @@
1
+ import { Json } from '@metamask/types';
2
+ import { NonEmptyArray } from '@metamask/controller-utils';
3
+ import { CaveatConstraint } from './Caveat';
4
+ /**
5
+ * The origin of a subject.
6
+ * Effectively the GUID of an entity that can have permissions.
7
+ */
8
+ export declare type OriginString = string;
9
+ /**
10
+ * The name of a permission target.
11
+ */
12
+ declare type TargetName = string;
13
+ /**
14
+ * A `ZCAP-LD`-like permission object. A permission is associated with a
15
+ * particular `invoker`, which is the holder of the permission. Possessing the
16
+ * permission grants access to a particular restricted resource, identified by
17
+ * the `parentCapability`. The use of the restricted resource may be further
18
+ * restricted by any `caveats` associated with the permission.
19
+ *
20
+ * See the README for details.
21
+ */
22
+ export declare type PermissionConstraint = {
23
+ /**
24
+ * The context(s) in which this capability is meaningful.
25
+ *
26
+ * It is required by the standard, but we make it optional since there is only
27
+ * one context in our usage (i.e. the user's MetaMask instance).
28
+ */
29
+ readonly '@context'?: NonEmptyArray<string>;
30
+ /**
31
+ * The caveats of the permission.
32
+ *
33
+ * @see {@link Caveat} For more information.
34
+ */
35
+ readonly caveats: null | NonEmptyArray<CaveatConstraint>;
36
+ /**
37
+ * The creation date of the permission, in UNIX epoch time.
38
+ */
39
+ readonly date: number;
40
+ /**
41
+ * The GUID of the permission object.
42
+ */
43
+ readonly id: string;
44
+ /**
45
+ * The origin string of the subject that has the permission.
46
+ */
47
+ readonly invoker: OriginString;
48
+ /**
49
+ * A pointer to the resource that possession of the capability grants
50
+ * access to, for example a JSON-RPC method or endowment.
51
+ */
52
+ readonly parentCapability: string;
53
+ };
54
+ /**
55
+ * A `ZCAP-LD`-like permission object. A permission is associated with a
56
+ * particular `invoker`, which is the holder of the permission. Possessing the
57
+ * permission grants access to a particular restricted resource, identified by
58
+ * the `parentCapability`. The use of the restricted resource may be further
59
+ * restricted by any `caveats` associated with the permission.
60
+ *
61
+ * See the README for details.
62
+ *
63
+ * @template TargetKey - They key of the permission target that the permission
64
+ * corresponds to.
65
+ * @template AllowedCaveat - A union of the allowed {@link Caveat} types
66
+ * for the permission.
67
+ */
68
+ export declare type ValidPermission<TargetKey extends TargetName, AllowedCaveat extends CaveatConstraint> = PermissionConstraint & {
69
+ /**
70
+ * The caveats of the permission.
71
+ *
72
+ * @see {@link Caveat} For more information.
73
+ */
74
+ readonly caveats: AllowedCaveat extends never ? null : NonEmptyArray<AllowedCaveat> | null;
75
+ /**
76
+ * A pointer to the resource that possession of the capability grants
77
+ * access to, for example a JSON-RPC method or endowment.
78
+ */
79
+ readonly parentCapability: ExtractPermissionTargetNames<TargetKey>;
80
+ };
81
+ /**
82
+ * A utility type for ensuring that the given permission target name conforms to
83
+ * our naming conventions.
84
+ *
85
+ * See the README for the distinction between target names and keys.
86
+ */
87
+ declare type ValidTargetName<Name extends string> = Name extends `${string}*` ? never : Name extends `${string}_` ? never : Name;
88
+ /**
89
+ * A utility type for extracting permission target names from a union of target
90
+ * keys.
91
+ *
92
+ * See the README for the distinction between target names and keys.
93
+ *
94
+ * @template Key - The target key type to extract target names from.
95
+ */
96
+ export declare type ExtractPermissionTargetNames<Key extends string> = ValidTargetName<Key extends `${infer Base}_*` ? `${Base}_${string}` : Key>;
97
+ /**
98
+ * Extracts the permission key of a particular name from a union of keys.
99
+ * An internal utility type used in {@link ExtractPermissionTargetKey}.
100
+ *
101
+ * @template Key - The target key type to extract from.
102
+ * @template Name - The name whose key to extract.
103
+ */
104
+ declare type KeyOfTargetName<Key extends string, Name extends string> = Name extends ExtractPermissionTargetNames<Key> ? Key : never;
105
+ /**
106
+ * A utility type for finding the permission target key corresponding to a
107
+ * target name. In a way, the inverse of {@link ExtractPermissionTargetNames}.
108
+ *
109
+ * See the README for the distinction between target names and keys.
110
+ *
111
+ * @template Key - The target key type to extract from.
112
+ * @template Name - The name whose key to extract.
113
+ */
114
+ export declare type ExtractPermissionTargetKey<Key extends string, Name extends string> = Key extends Name ? Key : Extract<Key, KeyOfTargetName<Key, Name>>;
115
+ /**
116
+ * Internal utility for extracting the members types of an array. The type
117
+ * evalutes to `never` if the specified type is the empty tuple or neither
118
+ * an array nor a tuple.
119
+ *
120
+ * @template ArrayType - The array type whose members to extract.
121
+ */
122
+ declare type ExtractArrayMembers<ArrayType> = ArrayType extends [] ? never : ArrayType extends any[] | readonly any[] ? ArrayType[number] : never;
123
+ /**
124
+ * A utility type for extracting the allowed caveat types for a particular
125
+ * permission from a permission specification type.
126
+ *
127
+ * @template PermissionSpecification - The permission specification type to
128
+ * extract valid caveat types from.
129
+ */
130
+ export declare type ExtractAllowedCaveatTypes<PermissionSpecification extends PermissionSpecificationConstraint> = ExtractArrayMembers<PermissionSpecification['allowedCaveats']>;
131
+ /**
132
+ * The options object of {@link constructPermission}.
133
+ *
134
+ * @template TargetPermission - The {@link Permission} that will be constructed.
135
+ */
136
+ export declare type PermissionOptions<TargetPermission extends PermissionConstraint> = {
137
+ target: TargetPermission['parentCapability'];
138
+ /**
139
+ * The origin string of the subject that has the permission.
140
+ */
141
+ invoker: OriginString;
142
+ /**
143
+ * The caveats of the permission.
144
+ * See {@link Caveat}.
145
+ */
146
+ caveats?: NonEmptyArray<CaveatConstraint>;
147
+ };
148
+ /**
149
+ * The default permission factory function. Naively constructs a permission from
150
+ * the inputs. Sets a default, random `id` if none is provided.
151
+ *
152
+ * @see {@link Permission} For more details.
153
+ * @template TargetPermission- - The {@link Permission} that will be constructed.
154
+ * @param options - The options for the permission.
155
+ * @returns The new permission object.
156
+ */
157
+ export declare function constructPermission<TargetPermission extends PermissionConstraint>(options: PermissionOptions<TargetPermission>): TargetPermission;
158
+ /**
159
+ * Gets the caveat of the specified type belonging to the specified permission.
160
+ *
161
+ * @param permission - The permission whose caveat to retrieve.
162
+ * @param caveatType - The type of the caveat to retrieve.
163
+ * @returns The caveat, or undefined if no such caveat exists.
164
+ */
165
+ export declare function findCaveat(permission: PermissionConstraint, caveatType: string): CaveatConstraint | undefined;
166
+ /**
167
+ * A requested permission object. Just an object with any of the properties
168
+ * of a {@link PermissionConstraint} object.
169
+ */
170
+ declare type RequestedPermission = Partial<PermissionConstraint>;
171
+ /**
172
+ * A record of target names and their {@link RequestedPermission} objects.
173
+ */
174
+ export declare type RequestedPermissions = Record<TargetName, RequestedPermission>;
175
+ /**
176
+ * The restricted method context object. Essentially a way to pass internal
177
+ * arguments to restricted methods and caveat functions, most importantly the
178
+ * requesting origin.
179
+ */
180
+ declare type RestrictedMethodContext = Readonly<{
181
+ origin: OriginString;
182
+ [key: string]: any;
183
+ }>;
184
+ export declare type RestrictedMethodParameters = Json[] | Record<string, Json> | void;
185
+ /**
186
+ * The arguments passed to a restricted method implementation.
187
+ *
188
+ * @template Params - The JSON-RPC parameters of the restricted method.
189
+ */
190
+ export declare type RestrictedMethodOptions<Params extends RestrictedMethodParameters> = {
191
+ method: TargetName;
192
+ params?: Params;
193
+ context: RestrictedMethodContext;
194
+ };
195
+ /**
196
+ * A synchronous restricted method implementation.
197
+ *
198
+ * @template Params - The JSON-RPC parameters of the restricted method.
199
+ * @template Result - The JSON-RPC result of the restricted method.
200
+ */
201
+ export declare type SyncRestrictedMethod<Params extends RestrictedMethodParameters, Result extends Json> = (args: RestrictedMethodOptions<Params>) => Result;
202
+ /**
203
+ * An asynchronous restricted method implementation.
204
+ *
205
+ * @template Params - The JSON-RPC parameters of the restricted method.
206
+ * @template Result - The JSON-RPC result of the restricted method.
207
+ */
208
+ export declare type AsyncRestrictedMethod<Params extends RestrictedMethodParameters, Result extends Json> = (args: RestrictedMethodOptions<Params>) => Promise<Result>;
209
+ /**
210
+ * A synchronous or asynchronous restricted method implementation.
211
+ *
212
+ * @template Params - The JSON-RPC parameters of the restricted method.
213
+ * @template Result - The JSON-RPC result of the restricted method.
214
+ */
215
+ export declare type RestrictedMethod<Params extends RestrictedMethodParameters, Result extends Json> = SyncRestrictedMethod<Params, Result> | AsyncRestrictedMethod<Params, Result>;
216
+ export declare type ValidRestrictedMethod<MethodImplementation extends RestrictedMethod<any, any>> = MethodImplementation extends (args: infer Options) => Json | Promise<Json> ? Options extends RestrictedMethodOptions<RestrictedMethodParameters> ? MethodImplementation : never : never;
217
+ /**
218
+ * {@link EndowmentGetter} parameter object.
219
+ */
220
+ export declare type EndowmentGetterParams = {
221
+ /**
222
+ * The origin of the requesting subject.
223
+ */
224
+ origin: string;
225
+ /**
226
+ * Any additional data associated with the request.
227
+ */
228
+ requestData?: unknown;
229
+ [key: string]: unknown;
230
+ };
231
+ /**
232
+ * A synchronous or asynchronous function that gets the endowments for a
233
+ * particular endowment permission. The getter receives the origin of the
234
+ * requesting subject and, optionally, additional request metadata.
235
+ */
236
+ export declare type EndowmentGetter<Endowments extends Json> = (options: EndowmentGetterParams) => Endowments | Promise<Endowments>;
237
+ export declare type PermissionFactory<TargetPermission extends PermissionConstraint, RequestData extends Record<string, unknown>> = (options: PermissionOptions<TargetPermission>, requestData?: RequestData) => TargetPermission;
238
+ export declare type PermissionValidatorConstraint = (permission: PermissionConstraint, origin?: OriginString, target?: string) => void;
239
+ /**
240
+ * A utility type for ensuring that the given permission target key conforms to
241
+ * our naming conventions.
242
+ *
243
+ * See the README for the distinction between target names and keys.
244
+ *
245
+ * @template Key - The target key string to apply the constraint to.
246
+ */
247
+ declare type ValidTargetKey<Key extends string> = Key extends `${string}_*` ? Key : Key extends `${string}_` ? never : Key extends `${string}*` ? never : Key;
248
+ /**
249
+ * The different possible types of permissions.
250
+ */
251
+ export declare enum PermissionType {
252
+ /**
253
+ * A restricted JSON-RPC method. A subject must have the requisite permission
254
+ * to call a restricted JSON-RPC method.
255
+ */
256
+ RestrictedMethod = "RestrictedMethod",
257
+ /**
258
+ * An "endowment" granted to subjects that possess the requisite permission,
259
+ * such as a global environment variable exposing a restricted API, etc.
260
+ */
261
+ Endowment = "Endowment"
262
+ }
263
+ /**
264
+ * The base constraint for permission specification objects. Every
265
+ * {@link Permission} supported by a {@link PermissionController} must have an
266
+ * associated specification, which is the source of truth for all permission-
267
+ * related types. A permission specification includes the list of permitted
268
+ * caveats, and any factory and validation functions specified by the consumer.
269
+ * A concrete permission specification may specify further fields as necessary.
270
+ *
271
+ * See the README for more details.
272
+ */
273
+ declare type PermissionSpecificationBase<Type extends PermissionType> = {
274
+ /**
275
+ * The type of the specified permission.
276
+ */
277
+ permissionType: Type;
278
+ /**
279
+ * The target resource of the permission. The shape of this string depends on
280
+ * the permission type. For example, a restricted method target key will
281
+ * consist of either a complete method name or the prefix of a namespaced
282
+ * method, e.g. `wallet_snap_*`.
283
+ */
284
+ targetKey: string;
285
+ /**
286
+ * An array of the caveat types that may be added to instances of this
287
+ * permission.
288
+ */
289
+ allowedCaveats: Readonly<NonEmptyArray<string>> | null;
290
+ /**
291
+ * The factory function used to get permission objects. Permissions returned
292
+ * by this function are presumed to valid, and they will not be passed to the
293
+ * validator function associated with this specification (if any). In other
294
+ * words, the factory function should validate the permissions it creates.
295
+ *
296
+ * If no factory is specified, the {@link Permission} constructor will be
297
+ * used, and the validator function (if specified) will be called on newly
298
+ * constructed permissions.
299
+ */
300
+ factory?: PermissionFactory<any, Record<string, unknown>>;
301
+ /**
302
+ * The validator function used to validate permissions of the associated type
303
+ * whenever they are mutated. The only way a permission can be legally mutated
304
+ * is when its caveats are modified by the permission controller.
305
+ *
306
+ * The validator should throw an appropriate JSON-RPC error if validation fails.
307
+ */
308
+ validator?: PermissionValidatorConstraint;
309
+ };
310
+ /**
311
+ * The constraint for restricted method permission specification objects.
312
+ * Permissions that correspond to JSON-RPC methods are specified using objects
313
+ * that conform to this type.
314
+ *
315
+ * See the README for more details.
316
+ */
317
+ export declare type RestrictedMethodSpecificationConstraint = PermissionSpecificationBase<PermissionType.RestrictedMethod> & {
318
+ /**
319
+ * The implementation of the restricted method that the permission
320
+ * corresponds to.
321
+ */
322
+ methodImplementation: RestrictedMethod<any, any>;
323
+ };
324
+ /**
325
+ * The constraint for endowment permission specification objects. Permissions
326
+ * that endow callers with some restricted resource are specified using objects
327
+ * that conform to this type.
328
+ *
329
+ * See the README for more details.
330
+ */
331
+ export declare type EndowmentSpecificationConstraint = PermissionSpecificationBase<PermissionType.Endowment> & {
332
+ /**
333
+ * The {@link EndowmentGetter} function for the permission. This function
334
+ * will be called by the {@link PermissionController} whenever the
335
+ * permission is invoked, after which the host can apply the endowments to
336
+ * the requesting subject in the intended manner.
337
+ */
338
+ endowmentGetter: EndowmentGetter<any>;
339
+ };
340
+ /**
341
+ * The constraint for permission specification objects. Every {@link Permission}
342
+ * supported by a {@link PermissionController} must have an associated
343
+ * specification, which is the source of truth for all permission-related types.
344
+ * All specifications must adhere to the {@link PermissionSpecificationBase}
345
+ * interface, but specifications may have different fields depending on the
346
+ * {@link PermissionType}.
347
+ *
348
+ * See the README for more details.
349
+ */
350
+ export declare type PermissionSpecificationConstraint = EndowmentSpecificationConstraint | RestrictedMethodSpecificationConstraint;
351
+ /**
352
+ * Options for {@link PermissionSpecificationBuilder} functions.
353
+ */
354
+ declare type PermissionSpecificationBuilderOptions<FactoryHooks extends Record<string, unknown>, MethodHooks extends Record<string, unknown>, ValidatorHooks extends Record<string, unknown>> = {
355
+ targetKey?: string;
356
+ allowedCaveats?: Readonly<NonEmptyArray<string>> | null;
357
+ factoryHooks?: FactoryHooks;
358
+ methodHooks?: MethodHooks;
359
+ validatorHooks?: ValidatorHooks;
360
+ };
361
+ /**
362
+ * A function that builds a permission specification. Modules that specify
363
+ * permissions for external consumption should make this their primary /
364
+ * default export so that host applications can use them to generate concrete
365
+ * specifications tailored to their requirements.
366
+ */
367
+ export declare type PermissionSpecificationBuilder<Type extends PermissionType, Options extends PermissionSpecificationBuilderOptions<any, any, any>, Specification extends PermissionSpecificationConstraint & {
368
+ permissionType: Type;
369
+ }> = (options: Options) => Specification;
370
+ /**
371
+ * A restricted method permission export object, containing the
372
+ * {@link PermissionSpecificationBuilder} function and "hook name" objects.
373
+ */
374
+ export declare type PermissionSpecificationBuilderExportConstraint = {
375
+ targetKey: string;
376
+ specificationBuilder: PermissionSpecificationBuilder<PermissionType, PermissionSpecificationBuilderOptions<any, any, any>, PermissionSpecificationConstraint>;
377
+ factoryHookNames?: Record<string, true>;
378
+ methodHookNames?: Record<string, true>;
379
+ validatorHookNames?: Record<string, true>;
380
+ };
381
+ declare type ValidRestrictedMethodSpecification<Specification extends RestrictedMethodSpecificationConstraint> = Specification['methodImplementation'] extends ValidRestrictedMethod<Specification['methodImplementation']> ? Specification : never;
382
+ /**
383
+ * Constraint for {@link PermissionSpecificationConstraint} objects that
384
+ * evaluates to `never` if the specification contains any invalid fields.
385
+ *
386
+ * @template Specification - The permission specification to validate.
387
+ */
388
+ export declare type ValidPermissionSpecification<Specification extends PermissionSpecificationConstraint> = Specification['targetKey'] extends ValidTargetKey<Specification['targetKey']> ? Specification['permissionType'] extends PermissionType.Endowment ? Specification : Specification['permissionType'] extends PermissionType.RestrictedMethod ? ValidRestrictedMethodSpecification<Extract<Specification, RestrictedMethodSpecificationConstraint>> : never : never;
389
+ /**
390
+ * Checks that the specification has the expected permission type.
391
+ *
392
+ * @param specification - The specification to check.
393
+ * @param expectedType - The expected permission type.
394
+ * @template Specification - The specification to check.
395
+ * @template Type - The expected permission type.
396
+ * @returns Whether or not the specification is of the expected type.
397
+ */
398
+ export declare function hasSpecificationType<Specification extends PermissionSpecificationConstraint, Type extends PermissionType>(specification: Specification, expectedType: Type): specification is Specification & {
399
+ permissionType: Type;
400
+ };
401
+ /**
402
+ * The specifications for all permissions supported by a particular
403
+ * {@link PermissionController}.
404
+ *
405
+ * @template Specifications - The union of all {@link PermissionSpecificationConstraint} types.
406
+ */
407
+ export declare type PermissionSpecificationMap<Specification extends PermissionSpecificationConstraint> = {
408
+ [TargetKey in Specification['targetKey']]: Specification extends {
409
+ targetKey: TargetKey;
410
+ } ? Specification : never;
411
+ };
412
+ /**
413
+ * Extracts a specific {@link PermissionSpecificationConstraint} from a union of
414
+ * permission specifications.
415
+ *
416
+ * @template Specification - The specification union type to extract from.
417
+ * @template TargetKey - The `targetKey` of the specification to extract.
418
+ */
419
+ export declare type ExtractPermissionSpecification<Specification extends PermissionSpecificationConstraint, TargetKey extends Specification['targetKey']> = Specification extends {
420
+ targetKey: TargetKey;
421
+ } ? Specification : never;
422
+ export {};
@@ -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":";;;AACA,mCAAgC;AAmMhC;;;;;;;;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;AAwID;;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;AA+KD;;;;;;;;GAQG;AACH,SAAgB,oBAAoB,CAIlC,aAA4B,EAC5B,YAAkB;IAIlB,OAAO,aAAa,CAAC,cAAc,KAAK,YAAY,CAAC;AACvD,CAAC;AAVD,oDAUC","sourcesContent":["import { Json } from '@metamask/types';\nimport { nanoid } from 'nanoid';\nimport { NonEmptyArray } from '@metamask/controller-utils';\nimport { CaveatConstraint } from './Caveat';\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { PermissionController } from './PermissionController';\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { Caveat } from './Caveat';\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 TargetKey - They key of the permission target that the permission\n * corresponds to.\n * @template AllowedCaveat - A union of the allowed {@link Caveat} types\n * for the permission.\n */\nexport type ValidPermission<\n TargetKey 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: ExtractPermissionTargetNames<TargetKey>;\n};\n\n/**\n * A utility type for ensuring that the given permission target name conforms to\n * our naming conventions.\n *\n * See the README for the distinction between target names and keys.\n */\ntype ValidTargetName<Name extends string> = Name extends `${string}*`\n ? never\n : Name extends `${string}_`\n ? never\n : Name;\n\n/**\n * A utility type for extracting permission target names from a union of target\n * keys.\n *\n * See the README for the distinction between target names and keys.\n *\n * @template Key - The target key type to extract target names from.\n */\nexport type ExtractPermissionTargetNames<Key extends string> = ValidTargetName<\n Key extends `${infer Base}_*` ? `${Base}_${string}` : Key\n>;\n\n/**\n * Extracts the permission key of a particular name from a union of keys.\n * An internal utility type used in {@link ExtractPermissionTargetKey}.\n *\n * @template Key - The target key type to extract from.\n * @template Name - The name whose key to extract.\n */\ntype KeyOfTargetName<\n Key extends string,\n Name extends string,\n> = Name extends ExtractPermissionTargetNames<Key> ? Key : never;\n\n/**\n * A utility type for finding the permission target key corresponding to a\n * target name. In a way, the inverse of {@link ExtractPermissionTargetNames}.\n *\n * See the README for the distinction between target names and keys.\n *\n * @template Key - The target key type to extract from.\n * @template Name - The name whose key to extract.\n */\nexport type ExtractPermissionTargetKey<\n Key extends string,\n Name extends string,\n> = Key extends Name ? Key : Extract<Key, KeyOfTargetName<Key, Name>>;\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 * A utility type for ensuring that the given permission target key conforms to\n * our naming conventions.\n *\n * See the README for the distinction between target names and keys.\n *\n * @template Key - The target key string to apply the constraint to.\n */\ntype ValidTargetKey<Key extends string> = Key extends `${string}_*`\n ? Key\n : Key extends `${string}_`\n ? never\n : Key extends `${string}*`\n ? never\n : Key;\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 target resource of the permission. The shape of this string depends on\n * the permission type. For example, a restricted method target key will\n * consist of either a complete method name or the prefix of a namespaced\n * method, e.g. `wallet_snap_*`.\n */\n targetKey: 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/**\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 targetKey?: 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 targetKey: 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['targetKey'] extends ValidTargetKey<\n Specification['targetKey']\n>\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 [TargetKey in Specification['targetKey']]: Specification extends {\n targetKey: TargetKey;\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 TargetKey - The `targetKey` of the specification to extract.\n */\nexport type ExtractPermissionSpecification<\n Specification extends PermissionSpecificationConstraint,\n TargetKey extends Specification['targetKey'],\n> = Specification extends {\n targetKey: TargetKey;\n}\n ? Specification\n : never;\n"]}