@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.
- package/CHANGELOG.md +18 -0
- package/LICENSE +20 -0
- package/README.md +19 -0
- package/dist/Caveat.d.ts +182 -0
- package/dist/Caveat.js +57 -0
- package/dist/Caveat.js.map +1 -0
- package/dist/Permission.d.ts +422 -0
- package/dist/Permission.js +66 -0
- package/dist/Permission.js.map +1 -0
- package/dist/PermissionController.d.ts +870 -0
- package/dist/PermissionController.js +1229 -0
- package/dist/PermissionController.js.map +1 -0
- package/dist/errors.d.ts +158 -0
- package/dist/errors.js +196 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/dist/permission-middleware.d.ts +32 -0
- package/dist/permission-middleware.js +64 -0
- package/dist/permission-middleware.js.map +1 -0
- package/dist/rpc-methods/getPermissions.d.ts +7 -0
- package/dist/rpc-methods/getPermissions.js +38 -0
- package/dist/rpc-methods/getPermissions.js.map +1 -0
- package/dist/rpc-methods/index.d.ts +4 -0
- package/dist/rpc-methods/index.js +7 -0
- package/dist/rpc-methods/index.js.map +1 -0
- package/dist/rpc-methods/requestPermissions.d.ts +16 -0
- package/dist/rpc-methods/requestPermissions.js +55 -0
- package/dist/rpc-methods/requestPermissions.js.map +1 -0
- package/dist/utils.d.ts +15 -0
- package/dist/utils.js +9 -0
- package/dist/utils.js.map +1 -0
- package/package.json +60 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
All notable changes to this project will be documented in this file.
|
|
3
|
+
|
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [1.0.0]
|
|
10
|
+
### Added
|
|
11
|
+
- Initial release
|
|
12
|
+
- As a result of converting our shared controllers repo into a monorepo ([#831](https://github.com/MetaMask/controllers/pull/831)), we've created this package from select parts of [`@metamask/controllers` v33.0.0](https://github.com/MetaMask/controllers/tree/v33.0.0), namely:
|
|
13
|
+
- Everything in `src/permissions`
|
|
14
|
+
|
|
15
|
+
All changes listed after this point were applied to this package following the monorepo conversion.
|
|
16
|
+
|
|
17
|
+
[Unreleased]: https://github.com/MetaMask/controllers/compare/@metamask/permission-controller@1.0.0...HEAD
|
|
18
|
+
[1.0.0]: https://github.com/MetaMask/controllers/releases/tag/@metamask/permission-controller@1.0.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 MetaMask
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# `@metamask/permission-controller`
|
|
2
|
+
|
|
3
|
+
Mediates access to JSON-RPC methods, used to interact with pieces of the MetaMask stack, via middleware for `json-rpc-engine`.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
`yarn add @metamask/permission-controller`
|
|
8
|
+
|
|
9
|
+
or
|
|
10
|
+
|
|
11
|
+
`npm install @metamask/permission-controller`
|
|
12
|
+
|
|
13
|
+
## Understanding
|
|
14
|
+
|
|
15
|
+
Please read the [Architecture][./architecture.md] document for more on how PermissionController works and how to use it.
|
|
16
|
+
|
|
17
|
+
## Contributing
|
|
18
|
+
|
|
19
|
+
This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/controllers#readme).
|
package/dist/Caveat.d.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { Json } from '@metamask/types';
|
|
2
|
+
import { AsyncRestrictedMethod, RestrictedMethod, PermissionConstraint, RestrictedMethodParameters } from './Permission';
|
|
3
|
+
export declare type CaveatConstraint = {
|
|
4
|
+
/**
|
|
5
|
+
* The type of the caveat. The type is presumed to be meaningful in the
|
|
6
|
+
* context of the capability it is associated with.
|
|
7
|
+
*
|
|
8
|
+
* In MetaMask, every permission can only have one caveat of each type.
|
|
9
|
+
*/
|
|
10
|
+
readonly type: string;
|
|
11
|
+
/**
|
|
12
|
+
* Any additional data necessary to enforce the caveat.
|
|
13
|
+
*/
|
|
14
|
+
readonly value: Json;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* A `ZCAP-LD`-like caveat object. A caveat is associated with a particular
|
|
18
|
+
* permission, and stored in its `caveats` array. Conceptually, a caveat is
|
|
19
|
+
* an arbitrary attenuation of the authority granted by its associated
|
|
20
|
+
* permission. It is the responsibility of the host to interpret and apply
|
|
21
|
+
* the restriction represented by a caveat.
|
|
22
|
+
*
|
|
23
|
+
* @template Type - The type of the caveat.
|
|
24
|
+
* @template Value - The value associated with the caveat.
|
|
25
|
+
*/
|
|
26
|
+
export declare type Caveat<Type extends string, Value extends Json> = {
|
|
27
|
+
/**
|
|
28
|
+
* The type of the caveat. The type is presumed to be meaningful in the
|
|
29
|
+
* context of the capability it is associated with.
|
|
30
|
+
*
|
|
31
|
+
* In MetaMask, every permission can only have one caveat of each type.
|
|
32
|
+
*/
|
|
33
|
+
readonly type: Type;
|
|
34
|
+
/**
|
|
35
|
+
* Any additional data necessary to enforce the caveat.
|
|
36
|
+
*/
|
|
37
|
+
readonly value: Value;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* A function for applying caveats to a restricted method request.
|
|
41
|
+
*
|
|
42
|
+
* @template ParentCaveat - The caveat type associated with this decorator.
|
|
43
|
+
* @param decorated - The restricted method implementation to be decorated.
|
|
44
|
+
* The method may have already been decorated with other caveats.
|
|
45
|
+
* @param caveat - The caveat object.
|
|
46
|
+
* @returns The decorated restricted method implementation.
|
|
47
|
+
*/
|
|
48
|
+
export declare type CaveatDecorator<ParentCaveat extends CaveatConstraint> = (decorated: AsyncRestrictedMethod<RestrictedMethodParameters, Json>, caveat: ParentCaveat) => AsyncRestrictedMethod<RestrictedMethodParameters, Json>;
|
|
49
|
+
/**
|
|
50
|
+
* Extracts a caveat value type from a caveat decorator.
|
|
51
|
+
*
|
|
52
|
+
* @template Decorator - The {@link CaveatDecorator} to extract a caveat value
|
|
53
|
+
* type from.
|
|
54
|
+
*/
|
|
55
|
+
declare type ExtractCaveatValueFromDecorator<Decorator extends CaveatDecorator<any>> = Decorator extends (decorated: any, caveat: infer ParentCaveat) => AsyncRestrictedMethod<any, any> ? ParentCaveat extends CaveatConstraint ? ParentCaveat['value'] : never : never;
|
|
56
|
+
/**
|
|
57
|
+
* A function for validating caveats of a particular type.
|
|
58
|
+
*
|
|
59
|
+
* @template ParentCaveat - The caveat type associated with this validator.
|
|
60
|
+
* @param caveat - The caveat object to validate.
|
|
61
|
+
* @param origin - The origin associated with the parent permission.
|
|
62
|
+
* @param target - The target of the parent permission.
|
|
63
|
+
*/
|
|
64
|
+
export declare type CaveatValidator<ParentCaveat extends CaveatConstraint> = (caveat: {
|
|
65
|
+
type: ParentCaveat['type'];
|
|
66
|
+
value: unknown;
|
|
67
|
+
}, origin?: string, target?: string) => void;
|
|
68
|
+
export declare type CaveatSpecificationBase = {
|
|
69
|
+
/**
|
|
70
|
+
* The string type of the caveat.
|
|
71
|
+
*/
|
|
72
|
+
type: string;
|
|
73
|
+
/**
|
|
74
|
+
* The validator function used to validate caveats of the associated type
|
|
75
|
+
* whenever they are instantiated. Caveat are instantiated whenever they are
|
|
76
|
+
* created or mutated.
|
|
77
|
+
*
|
|
78
|
+
* The validator should throw an appropriate JSON-RPC error if validation fails.
|
|
79
|
+
*
|
|
80
|
+
* If no validator is specified, no validation of caveat values will be
|
|
81
|
+
* performed. Although caveats can also be validated by permission validators,
|
|
82
|
+
* validating caveat values separately is strongly recommended.
|
|
83
|
+
*/
|
|
84
|
+
validator?: CaveatValidator<any>;
|
|
85
|
+
};
|
|
86
|
+
export declare type RestrictedMethodCaveatSpecificationConstraint = CaveatSpecificationBase & {
|
|
87
|
+
/**
|
|
88
|
+
* The decorator function used to apply the caveat to restricted method
|
|
89
|
+
* requests.
|
|
90
|
+
*/
|
|
91
|
+
decorator: CaveatDecorator<any>;
|
|
92
|
+
};
|
|
93
|
+
export declare type EndowmentCaveatSpecificationConstraint = CaveatSpecificationBase;
|
|
94
|
+
/**
|
|
95
|
+
* The constraint for caveat specification objects. Every {@link Caveat}
|
|
96
|
+
* supported by a {@link PermissionController} must have an associated
|
|
97
|
+
* specification, which is the source of truth for all caveat-related types.
|
|
98
|
+
* In addition, a caveat specification may include a decorator function used
|
|
99
|
+
* to apply the caveat's attenuation to a restricted method. It may also include
|
|
100
|
+
* a validator function specified by the consumer.
|
|
101
|
+
*
|
|
102
|
+
* See the README for more details.
|
|
103
|
+
*/
|
|
104
|
+
export declare type CaveatSpecificationConstraint = RestrictedMethodCaveatSpecificationConstraint | EndowmentCaveatSpecificationConstraint;
|
|
105
|
+
/**
|
|
106
|
+
* Options for {@link CaveatSpecificationBuilder} functions.
|
|
107
|
+
*/
|
|
108
|
+
declare type CaveatSpecificationBuilderOptions<DecoratorHooks extends Record<string, unknown>, ValidatorHooks extends Record<string, unknown>> = {
|
|
109
|
+
type?: string;
|
|
110
|
+
decoratorHooks?: DecoratorHooks;
|
|
111
|
+
validatorHooks?: ValidatorHooks;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* A function that builds caveat specifications. Modules that specify caveats
|
|
115
|
+
* for external consumption should make this their primary / default export so
|
|
116
|
+
* that host applications can use them to generate concrete specifications
|
|
117
|
+
* tailored to their requirements.
|
|
118
|
+
*/
|
|
119
|
+
export declare type CaveatSpecificationBuilder<Options extends CaveatSpecificationBuilderOptions<any, any>, Specification extends CaveatSpecificationConstraint> = (options: Options) => Specification;
|
|
120
|
+
/**
|
|
121
|
+
* A caveat specification export object, containing the
|
|
122
|
+
* {@link CaveatSpecificationBuilder} function and "hook name" objects.
|
|
123
|
+
*/
|
|
124
|
+
export declare type CaveatSpecificationBuilderExportConstraint = {
|
|
125
|
+
specificationBuilder: CaveatSpecificationBuilder<CaveatSpecificationBuilderOptions<any, any>, CaveatSpecificationConstraint>;
|
|
126
|
+
decoratorHookNames?: Record<string, true>;
|
|
127
|
+
validatorHookNames?: Record<string, true>;
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* The specifications for all caveats supported by a particular
|
|
131
|
+
* {@link PermissionController}.
|
|
132
|
+
*
|
|
133
|
+
* @template Specifications - The union of all {@link CaveatSpecificationConstraint} types.
|
|
134
|
+
*/
|
|
135
|
+
export declare type CaveatSpecificationMap<CaveatSpecification extends CaveatSpecificationConstraint> = Record<CaveatSpecification['type'], CaveatSpecification>;
|
|
136
|
+
/**
|
|
137
|
+
* Extracts the union of all caveat types specified by the given
|
|
138
|
+
* {@link CaveatSpecificationConstraint} type.
|
|
139
|
+
*
|
|
140
|
+
* @template CaveatSpecification - The {@link CaveatSpecificationConstraint} to extract a
|
|
141
|
+
* caveat type union from.
|
|
142
|
+
*/
|
|
143
|
+
export declare type ExtractCaveats<CaveatSpecification extends CaveatSpecificationConstraint> = CaveatSpecification extends any ? CaveatSpecification extends RestrictedMethodCaveatSpecificationConstraint ? Caveat<CaveatSpecification['type'], ExtractCaveatValueFromDecorator<RestrictedMethodCaveatSpecificationConstraint['decorator']>> : Caveat<CaveatSpecification['type'], Json> : never;
|
|
144
|
+
/**
|
|
145
|
+
* Extracts the type of a specific {@link Caveat} from a union of caveat
|
|
146
|
+
* specifications.
|
|
147
|
+
*
|
|
148
|
+
* @template CaveatSpecifications - The union of all caveat specifications.
|
|
149
|
+
* @template CaveatType - The type of the caveat to extract.
|
|
150
|
+
*/
|
|
151
|
+
export declare type ExtractCaveat<CaveatSpecifications extends CaveatSpecificationConstraint, CaveatType extends string> = Extract<ExtractCaveats<CaveatSpecifications>, {
|
|
152
|
+
type: CaveatType;
|
|
153
|
+
}>;
|
|
154
|
+
/**
|
|
155
|
+
* Extracts the value type of a specific {@link Caveat} from a union of caveat
|
|
156
|
+
* specifications.
|
|
157
|
+
*
|
|
158
|
+
* @template CaveatSpecifications - The union of all caveat specifications.
|
|
159
|
+
* @template CaveatType - The type of the caveat whose value to extract.
|
|
160
|
+
*/
|
|
161
|
+
export declare type ExtractCaveatValue<CaveatSpecifications extends CaveatSpecificationConstraint, CaveatType extends string> = ExtractCaveat<CaveatSpecifications, CaveatType>['value'];
|
|
162
|
+
/**
|
|
163
|
+
* Determines whether a caveat specification is a restricted method caveat specification.
|
|
164
|
+
*
|
|
165
|
+
* @param specification - The caveat specification.
|
|
166
|
+
* @returns True if the caveat specification is a restricted method caveat specification, otherwise false.
|
|
167
|
+
*/
|
|
168
|
+
export declare function isRestrictedMethodCaveatSpecification(specification: CaveatSpecificationConstraint): specification is RestrictedMethodCaveatSpecificationConstraint;
|
|
169
|
+
/**
|
|
170
|
+
* Decorate a restricted method implementation with its caveats.
|
|
171
|
+
*
|
|
172
|
+
* Note that all caveat functions (i.e. the argument and return value of the
|
|
173
|
+
* decorator) must be awaited.
|
|
174
|
+
*
|
|
175
|
+
* @param methodImplementation - The restricted method implementation
|
|
176
|
+
* @param permission - The origin's potential permission
|
|
177
|
+
* @param caveatSpecifications - All caveat implementations
|
|
178
|
+
* @returns The decorated method implementation
|
|
179
|
+
*/
|
|
180
|
+
export declare function decorateWithCaveats<CaveatSpecifications extends CaveatSpecificationConstraint>(methodImplementation: RestrictedMethod<RestrictedMethodParameters, Json>, permission: Readonly<PermissionConstraint>, // bound to the requesting origin
|
|
181
|
+
caveatSpecifications: CaveatSpecificationMap<CaveatSpecifications>): RestrictedMethod<RestrictedMethodParameters, Json>;
|
|
182
|
+
export {};
|
package/dist/Caveat.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.decorateWithCaveats = exports.isRestrictedMethodCaveatSpecification = void 0;
|
|
13
|
+
const controller_utils_1 = require("@metamask/controller-utils");
|
|
14
|
+
const errors_1 = require("./errors");
|
|
15
|
+
const Permission_1 = require("./Permission");
|
|
16
|
+
/**
|
|
17
|
+
* Determines whether a caveat specification is a restricted method caveat specification.
|
|
18
|
+
*
|
|
19
|
+
* @param specification - The caveat specification.
|
|
20
|
+
* @returns True if the caveat specification is a restricted method caveat specification, otherwise false.
|
|
21
|
+
*/
|
|
22
|
+
function isRestrictedMethodCaveatSpecification(specification) {
|
|
23
|
+
return (0, controller_utils_1.hasProperty)(specification, 'decorator');
|
|
24
|
+
}
|
|
25
|
+
exports.isRestrictedMethodCaveatSpecification = isRestrictedMethodCaveatSpecification;
|
|
26
|
+
/**
|
|
27
|
+
* Decorate a restricted method implementation with its caveats.
|
|
28
|
+
*
|
|
29
|
+
* Note that all caveat functions (i.e. the argument and return value of the
|
|
30
|
+
* decorator) must be awaited.
|
|
31
|
+
*
|
|
32
|
+
* @param methodImplementation - The restricted method implementation
|
|
33
|
+
* @param permission - The origin's potential permission
|
|
34
|
+
* @param caveatSpecifications - All caveat implementations
|
|
35
|
+
* @returns The decorated method implementation
|
|
36
|
+
*/
|
|
37
|
+
function decorateWithCaveats(methodImplementation, permission, // bound to the requesting origin
|
|
38
|
+
caveatSpecifications) {
|
|
39
|
+
const { caveats } = permission;
|
|
40
|
+
if (!caveats) {
|
|
41
|
+
return methodImplementation;
|
|
42
|
+
}
|
|
43
|
+
let decorated = (args) => __awaiter(this, void 0, void 0, function* () { return methodImplementation(args); });
|
|
44
|
+
for (const caveat of caveats) {
|
|
45
|
+
const specification = caveatSpecifications[caveat.type];
|
|
46
|
+
if (!specification) {
|
|
47
|
+
throw new errors_1.UnrecognizedCaveatTypeError(caveat.type);
|
|
48
|
+
}
|
|
49
|
+
if (!isRestrictedMethodCaveatSpecification(specification)) {
|
|
50
|
+
throw new errors_1.CaveatSpecificationMismatchError(specification, Permission_1.PermissionType.RestrictedMethod);
|
|
51
|
+
}
|
|
52
|
+
decorated = specification.decorator(decorated, caveat);
|
|
53
|
+
}
|
|
54
|
+
return decorated;
|
|
55
|
+
}
|
|
56
|
+
exports.decorateWithCaveats = decorateWithCaveats;
|
|
57
|
+
//# sourceMappingURL=Caveat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Caveat.js","sourceRoot":"","sources":["../src/Caveat.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,iEAAyD;AACzD,qCAGkB;AAClB,6CAMsB;AAsOtB;;;;;GAKG;AACH,SAAgB,qCAAqC,CACnD,aAA4C;IAE5C,OAAO,IAAA,8BAAW,EAAC,aAAa,EAAE,WAAW,CAAC,CAAC;AACjD,CAAC;AAJD,sFAIC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,mBAAmB,CAGjC,oBAAwE,EACxE,UAA0C,EAAE,iCAAiC;AAC7E,oBAAkE;IAElE,MAAM,EAAE,OAAO,EAAE,GAAG,UAAU,CAAC;IAC/B,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,oBAAoB,CAAC;KAC7B;IAED,IAAI,SAAS,GAAG,CACd,IAAuE,EACvE,EAAE,gDAAC,OAAA,oBAAoB,CAAC,IAAI,CAAC,CAAA,GAAA,CAAC;IAEhC,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE;QAC5B,MAAM,aAAa,GACjB,oBAAoB,CAAC,MAAM,CAAC,IAAoC,CAAC,CAAC;QACpE,IAAI,CAAC,aAAa,EAAE;YAClB,MAAM,IAAI,oCAA2B,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;SACpD;QAED,IAAI,CAAC,qCAAqC,CAAC,aAAa,CAAC,EAAE;YACzD,MAAM,IAAI,yCAAgC,CACxC,aAAa,EACb,2BAAc,CAAC,gBAAgB,CAChC,CAAC;SACH;QACD,SAAS,GAAG,aAAa,CAAC,SAAS,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;KACxD;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAjCD,kDAiCC","sourcesContent":["import { Json } from '@metamask/types';\nimport { hasProperty } from '@metamask/controller-utils';\nimport {\n CaveatSpecificationMismatchError,\n UnrecognizedCaveatTypeError,\n} from './errors';\nimport {\n AsyncRestrictedMethod,\n RestrictedMethod,\n PermissionConstraint,\n RestrictedMethodParameters,\n PermissionType,\n} from './Permission';\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { PermissionController } from './PermissionController';\n\nexport type CaveatConstraint = {\n /**\n * The type of the caveat. The type is presumed to be meaningful in the\n * context of the capability it is associated with.\n *\n * In MetaMask, every permission can only have one caveat of each type.\n */\n readonly type: string;\n\n // TODO:TS4.4 Make optional\n /**\n * Any additional data necessary to enforce the caveat.\n */\n readonly value: Json;\n};\n\n/**\n * A `ZCAP-LD`-like caveat object. A caveat is associated with a particular\n * permission, and stored in its `caveats` array. Conceptually, a caveat is\n * an arbitrary attenuation of the authority granted by its associated\n * permission. It is the responsibility of the host to interpret and apply\n * the restriction represented by a caveat.\n *\n * @template Type - The type of the caveat.\n * @template Value - The value associated with the caveat.\n */\nexport type Caveat<Type extends string, Value extends Json> = {\n /**\n * The type of the caveat. The type is presumed to be meaningful in the\n * context of the capability it is associated with.\n *\n * In MetaMask, every permission can only have one caveat of each type.\n */\n readonly type: Type;\n\n // TODO:TS4.4 Make optional\n /**\n * Any additional data necessary to enforce the caveat.\n */\n readonly value: Value;\n};\n\n// Next, we define types used for specifying caveats at the consumer layer,\n// and a function for applying caveats to a restricted method request. This is\n// Accomplished by decorating the restricted method implementation with the\n// the corresponding caveat functions.\n\n/**\n * A function for applying caveats to a restricted method request.\n *\n * @template ParentCaveat - The caveat type associated with this decorator.\n * @param decorated - The restricted method implementation to be decorated.\n * The method may have already been decorated with other caveats.\n * @param caveat - The caveat object.\n * @returns The decorated restricted method implementation.\n */\nexport type CaveatDecorator<ParentCaveat extends CaveatConstraint> = (\n decorated: AsyncRestrictedMethod<RestrictedMethodParameters, Json>,\n caveat: ParentCaveat,\n) => AsyncRestrictedMethod<RestrictedMethodParameters, Json>;\n\n/**\n * Extracts a caveat value type from a caveat decorator.\n *\n * @template Decorator - The {@link CaveatDecorator} to extract a caveat value\n * type from.\n */\ntype ExtractCaveatValueFromDecorator<Decorator extends CaveatDecorator<any>> =\n Decorator extends (\n decorated: any,\n caveat: infer ParentCaveat,\n ) => AsyncRestrictedMethod<any, any>\n ? ParentCaveat extends CaveatConstraint\n ? ParentCaveat['value']\n : never\n : never;\n\n/**\n * A function for validating caveats of a particular type.\n *\n * @template ParentCaveat - The caveat type associated with this validator.\n * @param caveat - The caveat object to validate.\n * @param origin - The origin associated with the parent permission.\n * @param target - The target of the parent permission.\n */\nexport type CaveatValidator<ParentCaveat extends CaveatConstraint> = (\n caveat: { type: ParentCaveat['type']; value: unknown },\n origin?: string,\n target?: string,\n) => void;\n\nexport type CaveatSpecificationBase = {\n /**\n * The string type of the caveat.\n */\n type: string;\n\n /**\n * The validator function used to validate caveats of the associated type\n * whenever they are instantiated. Caveat are instantiated whenever they are\n * created or mutated.\n *\n * The validator should throw an appropriate JSON-RPC error if validation fails.\n *\n * If no validator is specified, no validation of caveat values will be\n * performed. Although caveats can also be validated by permission validators,\n * validating caveat values separately is strongly recommended.\n */\n validator?: CaveatValidator<any>;\n};\n\nexport type RestrictedMethodCaveatSpecificationConstraint =\n CaveatSpecificationBase & {\n /**\n * The decorator function used to apply the caveat to restricted method\n * requests.\n */\n decorator: CaveatDecorator<any>;\n };\n\nexport type EndowmentCaveatSpecificationConstraint = CaveatSpecificationBase;\n\n/**\n * The constraint for caveat specification objects. Every {@link Caveat}\n * supported by a {@link PermissionController} must have an associated\n * specification, which is the source of truth for all caveat-related types.\n * In addition, a caveat specification may include a decorator function used\n * to apply the caveat's attenuation to a restricted method. It may also include\n * a validator function specified by the consumer.\n *\n * See the README for more details.\n */\nexport type CaveatSpecificationConstraint =\n | RestrictedMethodCaveatSpecificationConstraint\n | EndowmentCaveatSpecificationConstraint;\n\n/**\n * Options for {@link CaveatSpecificationBuilder} functions.\n */\ntype CaveatSpecificationBuilderOptions<\n DecoratorHooks extends Record<string, unknown>,\n ValidatorHooks extends Record<string, unknown>,\n> = {\n type?: string;\n decoratorHooks?: DecoratorHooks;\n validatorHooks?: ValidatorHooks;\n};\n\n/**\n * A function that builds caveat specifications. Modules that specify caveats\n * for external consumption should make this their primary / default export so\n * that host applications can use them to generate concrete specifications\n * tailored to their requirements.\n */\nexport type CaveatSpecificationBuilder<\n Options extends CaveatSpecificationBuilderOptions<any, any>,\n Specification extends CaveatSpecificationConstraint,\n> = (options: Options) => Specification;\n\n/**\n * A caveat specification export object, containing the\n * {@link CaveatSpecificationBuilder} function and \"hook name\" objects.\n */\nexport type CaveatSpecificationBuilderExportConstraint = {\n specificationBuilder: CaveatSpecificationBuilder<\n CaveatSpecificationBuilderOptions<any, any>,\n CaveatSpecificationConstraint\n >;\n decoratorHookNames?: Record<string, true>;\n validatorHookNames?: Record<string, true>;\n};\n\n/**\n * The specifications for all caveats supported by a particular\n * {@link PermissionController}.\n *\n * @template Specifications - The union of all {@link CaveatSpecificationConstraint} types.\n */\nexport type CaveatSpecificationMap<\n CaveatSpecification extends CaveatSpecificationConstraint,\n> = Record<CaveatSpecification['type'], CaveatSpecification>;\n\n/**\n * Extracts the union of all caveat types specified by the given\n * {@link CaveatSpecificationConstraint} type.\n *\n * @template CaveatSpecification - The {@link CaveatSpecificationConstraint} to extract a\n * caveat type union from.\n */\nexport type ExtractCaveats<\n CaveatSpecification extends CaveatSpecificationConstraint,\n> = CaveatSpecification extends any\n ? CaveatSpecification extends RestrictedMethodCaveatSpecificationConstraint\n ? Caveat<\n CaveatSpecification['type'],\n ExtractCaveatValueFromDecorator<\n RestrictedMethodCaveatSpecificationConstraint['decorator']\n >\n >\n : Caveat<CaveatSpecification['type'], Json>\n : never;\n\n/**\n * Extracts the type of a specific {@link Caveat} from a union of caveat\n * specifications.\n *\n * @template CaveatSpecifications - The union of all caveat specifications.\n * @template CaveatType - The type of the caveat to extract.\n */\nexport type ExtractCaveat<\n CaveatSpecifications extends CaveatSpecificationConstraint,\n CaveatType extends string,\n> = Extract<ExtractCaveats<CaveatSpecifications>, { type: CaveatType }>;\n\n/**\n * Extracts the value type of a specific {@link Caveat} from a union of caveat\n * specifications.\n *\n * @template CaveatSpecifications - The union of all caveat specifications.\n * @template CaveatType - The type of the caveat whose value to extract.\n */\nexport type ExtractCaveatValue<\n CaveatSpecifications extends CaveatSpecificationConstraint,\n CaveatType extends string,\n> = ExtractCaveat<CaveatSpecifications, CaveatType>['value'];\n\n/**\n * Determines whether a caveat specification is a restricted method caveat specification.\n *\n * @param specification - The caveat specification.\n * @returns True if the caveat specification is a restricted method caveat specification, otherwise false.\n */\nexport function isRestrictedMethodCaveatSpecification(\n specification: CaveatSpecificationConstraint,\n): specification is RestrictedMethodCaveatSpecificationConstraint {\n return hasProperty(specification, 'decorator');\n}\n\n/**\n * Decorate a restricted method implementation with its caveats.\n *\n * Note that all caveat functions (i.e. the argument and return value of the\n * decorator) must be awaited.\n *\n * @param methodImplementation - The restricted method implementation\n * @param permission - The origin's potential permission\n * @param caveatSpecifications - All caveat implementations\n * @returns The decorated method implementation\n */\nexport function decorateWithCaveats<\n CaveatSpecifications extends CaveatSpecificationConstraint,\n>(\n methodImplementation: RestrictedMethod<RestrictedMethodParameters, Json>,\n permission: Readonly<PermissionConstraint>, // bound to the requesting origin\n caveatSpecifications: CaveatSpecificationMap<CaveatSpecifications>, // all caveat implementations\n): RestrictedMethod<RestrictedMethodParameters, Json> {\n const { caveats } = permission;\n if (!caveats) {\n return methodImplementation;\n }\n\n let decorated = async (\n args: Parameters<RestrictedMethod<RestrictedMethodParameters, Json>>[0],\n ) => methodImplementation(args);\n\n for (const caveat of caveats) {\n const specification =\n caveatSpecifications[caveat.type as CaveatSpecifications['type']];\n if (!specification) {\n throw new UnrecognizedCaveatTypeError(caveat.type);\n }\n\n if (!isRestrictedMethodCaveatSpecification(specification)) {\n throw new CaveatSpecificationMismatchError(\n specification,\n PermissionType.RestrictedMethod,\n );\n }\n decorated = specification.decorator(decorated, caveat);\n }\n\n return decorated;\n}\n"]}
|