@metamask-previews/gator-permissions-controller 1.1.2-preview-27e39dd44 → 1.1.2-preview-d7935cb09
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 +7 -1
- package/README.md +15 -10
- package/dist/GatorPermissionsController.cjs +123 -176
- package/dist/GatorPermissionsController.cjs.map +1 -1
- package/dist/GatorPermissionsController.d.cts +56 -74
- package/dist/GatorPermissionsController.d.cts.map +1 -1
- package/dist/GatorPermissionsController.d.mts +56 -74
- package/dist/GatorPermissionsController.d.mts.map +1 -1
- package/dist/GatorPermissionsController.mjs +125 -176
- package/dist/GatorPermissionsController.mjs.map +1 -1
- package/dist/errors.cjs +1 -22
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.cts +0 -11
- package/dist/errors.d.cts.map +1 -1
- package/dist/errors.d.mts +0 -11
- package/dist/errors.d.mts.map +1 -1
- package/dist/errors.mjs +0 -19
- package/dist/errors.mjs.map +1 -1
- package/dist/index.cjs +1 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +2 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +0 -1
- package/dist/index.mjs.map +1 -1
- package/dist/test/mocks.cjs +28 -49
- package/dist/test/mocks.cjs.map +1 -1
- package/dist/test/mocks.d.cts +32 -16
- package/dist/test/mocks.d.cts.map +1 -1
- package/dist/test/mocks.d.mts +32 -16
- package/dist/test/mocks.d.mts.map +1 -1
- package/dist/test/mocks.mjs +27 -47
- package/dist/test/mocks.mjs.map +1 -1
- package/dist/types.cjs +0 -2
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +31 -74
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +31 -74
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs +0 -2
- package/dist/types.mjs.map +1 -1
- package/dist/utils.cjs +25 -30
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +18 -12
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +18 -12
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +24 -28
- package/dist/utils.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,7 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Changed
|
|
11
11
|
|
|
12
|
-
-
|
|
12
|
+
- **BREAKING:** Refactor `GatorPermissionsController`: simplified config, permission storage, and public API ([#7847](https://github.com/MetaMask/core/pull/7847))
|
|
13
|
+
- Constructor now requires `config`, internal configuration is removed from controller state
|
|
14
|
+
- New `initialize()` function performs a syncronisation process if required when the controller is first initialized
|
|
15
|
+
- Replaces `gatorPermissionsMapSerialized` with `grantedPermissions` property in internal state, replaces related types, and utility functions
|
|
16
|
+
- `fetchAndUpdateGatorPermissions()` no longer accepts parameters and resolves to `void`
|
|
17
|
+
- `getPendingRevocations` / `pendingRevocations` getter replaced by `isPendingRevocation(permissionContext)`; list on `state.pendingRevocations`
|
|
18
|
+
- Bump `@metamask/transaction-controller` from `^62.11.0` to `^62.17.0` ([#7775](https://github.com/MetaMask/core/pull/7775), [#7802](https://github.com/MetaMask/core/pull/7802), [#7832](https://github.com/MetaMask/core/pull/7832), [#7854](https://github.com/MetaMask/core/pull/7854), [#7872](https://github.com/MetaMask/core/pull/7872)), ([#7897](https://github.com/MetaMask/core/pull/7897))>>>>>>> Stashed changes
|
|
13
19
|
|
|
14
20
|
## [1.1.2]
|
|
15
21
|
|
package/README.md
CHANGED
|
@@ -17,13 +17,21 @@ or
|
|
|
17
17
|
```typescript
|
|
18
18
|
import { GatorPermissionsController } from '@metamask/gator-permissions-controller';
|
|
19
19
|
|
|
20
|
-
// Create the controller
|
|
20
|
+
// Create the controller with required config
|
|
21
21
|
const gatorPermissionsController = new GatorPermissionsController({
|
|
22
22
|
messenger: yourMessenger,
|
|
23
|
+
config: {
|
|
24
|
+
supportedPermissionTypes: [
|
|
25
|
+
'native-token-stream',
|
|
26
|
+
'native-token-periodic',
|
|
27
|
+
'erc20-token-stream',
|
|
28
|
+
'erc20-token-periodic',
|
|
29
|
+
'erc20-token-revocation',
|
|
30
|
+
],
|
|
31
|
+
// Optional: override the default gator permissions provider Snap id
|
|
32
|
+
// gatorPermissionsProviderSnapId: 'npm:@metamask/gator-permissions-snap',
|
|
33
|
+
},
|
|
23
34
|
});
|
|
24
|
-
|
|
25
|
-
// Enable the feature (requires authentication)
|
|
26
|
-
gatorPermissionsController.enableGatorPermissions();
|
|
27
35
|
```
|
|
28
36
|
|
|
29
37
|
### Fetch from Profile Sync
|
|
@@ -33,12 +41,9 @@ gatorPermissionsController.enableGatorPermissions();
|
|
|
33
41
|
const permissions =
|
|
34
42
|
await gatorPermissionsController.fetchAndUpdateGatorPermissions();
|
|
35
43
|
|
|
36
|
-
// Fetch permissions
|
|
37
|
-
const
|
|
38
|
-
await gatorPermissionsController.fetchAndUpdateGatorPermissions(
|
|
39
|
-
origin: 'https://example.com',
|
|
40
|
-
chainId: '0x1',
|
|
41
|
-
});
|
|
44
|
+
// Fetch permissions and update internal state
|
|
45
|
+
const permissions =
|
|
46
|
+
await gatorPermissionsController.fetchAndUpdateGatorPermissions();
|
|
42
47
|
```
|
|
43
48
|
|
|
44
49
|
## Contributing
|
|
@@ -4,9 +4,14 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
4
4
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
5
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
6
|
};
|
|
7
|
-
var
|
|
7
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
8
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
11
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
|
+
};
|
|
13
|
+
var _GatorPermissionsController_instances, _GatorPermissionsController_supportedPermissionTypes, _GatorPermissionsController_gatorPermissionsProviderSnapId, _GatorPermissionsController_maxSyncIntervalMs, _GatorPermissionsController_fetchAndUpdateGatorPermissionsPromise, _GatorPermissionsController_setIsFetchingGatorPermissions, _GatorPermissionsController_addPendingRevocationToState, _GatorPermissionsController_removePendingRevocationFromStateByTxId, _GatorPermissionsController_removePendingRevocationFromStateByPermissionContext, _GatorPermissionsController_registerMessageHandlers, _GatorPermissionsController_storedPermissionToPermissionInfo, _GatorPermissionsController_storedPermissionsToPermissionInfoWithMetadata;
|
|
8
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.getDefaultGatorPermissionsControllerState = void 0;
|
|
10
15
|
const base_controller_1 = require("@metamask/base-controller");
|
|
11
16
|
const delegation_deployments_1 = require("@metamask/delegation-deployments");
|
|
12
17
|
const snaps_utils_1 = require("@metamask/snaps-utils");
|
|
@@ -22,16 +27,7 @@ const utils_1 = require("./utils.cjs");
|
|
|
22
27
|
const controllerName = 'GatorPermissionsController';
|
|
23
28
|
// Default value for the gator permissions provider snap id
|
|
24
29
|
const defaultGatorPermissionsProviderSnapId = 'npm:@metamask/gator-permissions-snap';
|
|
25
|
-
const
|
|
26
|
-
return {
|
|
27
|
-
'erc20-token-revocation': {},
|
|
28
|
-
'native-token-stream': {},
|
|
29
|
-
'native-token-periodic': {},
|
|
30
|
-
'erc20-token-stream': {},
|
|
31
|
-
'erc20-token-periodic': {},
|
|
32
|
-
other: {},
|
|
33
|
-
};
|
|
34
|
-
};
|
|
30
|
+
const DEFAULT_MAX_SYNC_INTERVAL_MS = 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds
|
|
35
31
|
/**
|
|
36
32
|
* Timeout duration for pending revocations (2 hours in milliseconds).
|
|
37
33
|
* After this time, event listeners will be cleaned up to prevent memory leaks.
|
|
@@ -39,13 +35,7 @@ const createEmptyGatorPermissionsMap = () => {
|
|
|
39
35
|
const PENDING_REVOCATION_TIMEOUT = 2 * 60 * 60 * 1000;
|
|
40
36
|
const contractsByChainId = delegation_deployments_1.DELEGATOR_CONTRACTS[constants_1.DELEGATION_FRAMEWORK_VERSION];
|
|
41
37
|
const gatorPermissionsControllerMetadata = {
|
|
42
|
-
|
|
43
|
-
includeInStateLogs: true,
|
|
44
|
-
persist: true,
|
|
45
|
-
includeInDebugSnapshot: false,
|
|
46
|
-
usedInUi: false,
|
|
47
|
-
},
|
|
48
|
-
gatorPermissionsMapSerialized: {
|
|
38
|
+
grantedPermissions: {
|
|
49
39
|
includeInStateLogs: true,
|
|
50
40
|
persist: true,
|
|
51
41
|
includeInDebugSnapshot: false,
|
|
@@ -55,134 +45,149 @@ const gatorPermissionsControllerMetadata = {
|
|
|
55
45
|
includeInStateLogs: true,
|
|
56
46
|
persist: false,
|
|
57
47
|
includeInDebugSnapshot: false,
|
|
58
|
-
usedInUi:
|
|
48
|
+
usedInUi: true,
|
|
59
49
|
},
|
|
60
|
-
|
|
50
|
+
pendingRevocations: {
|
|
61
51
|
includeInStateLogs: true,
|
|
62
52
|
persist: false,
|
|
63
53
|
includeInDebugSnapshot: false,
|
|
64
|
-
usedInUi:
|
|
54
|
+
usedInUi: true,
|
|
65
55
|
},
|
|
66
|
-
|
|
56
|
+
lastSyncedTimestamp: {
|
|
67
57
|
includeInStateLogs: true,
|
|
68
|
-
persist:
|
|
58
|
+
persist: true,
|
|
69
59
|
includeInDebugSnapshot: false,
|
|
70
|
-
usedInUi:
|
|
60
|
+
usedInUi: false,
|
|
71
61
|
},
|
|
72
62
|
};
|
|
73
63
|
/**
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
* and also helps in constructing complete state objects for this controller in
|
|
77
|
-
* tests.
|
|
64
|
+
* Creates initial controller state, merging defaults with optional partial state.
|
|
65
|
+
* Internal use only (e.g. constructor, tests).
|
|
78
66
|
*
|
|
79
|
-
* @
|
|
67
|
+
* @param state - Optional partial state to merge with defaults.
|
|
68
|
+
* @returns Complete {@link GatorPermissionsController} state.
|
|
80
69
|
*/
|
|
81
|
-
function
|
|
70
|
+
function createGatorPermissionsControllerState(state) {
|
|
82
71
|
return {
|
|
83
|
-
|
|
84
|
-
gatorPermissionsMapSerialized: (0, utils_1.serializeGatorPermissionsMap)(createEmptyGatorPermissionsMap()),
|
|
85
|
-
isFetchingGatorPermissions: false,
|
|
86
|
-
gatorPermissionsProviderSnapId: defaultGatorPermissionsProviderSnapId,
|
|
72
|
+
grantedPermissions: [],
|
|
87
73
|
pendingRevocations: [],
|
|
74
|
+
lastSyncedTimestamp: -1,
|
|
75
|
+
...state,
|
|
76
|
+
// isFetchingGatorPermissions is _always_ false when the controller is created
|
|
77
|
+
isFetchingGatorPermissions: false,
|
|
88
78
|
};
|
|
89
79
|
}
|
|
90
|
-
exports.getDefaultGatorPermissionsControllerState = getDefaultGatorPermissionsControllerState;
|
|
91
80
|
/**
|
|
92
|
-
* Controller that manages gator permissions by reading from
|
|
81
|
+
* Controller that manages gator permissions by reading from the gator permissions provider Snap.
|
|
93
82
|
*/
|
|
94
83
|
class GatorPermissionsController extends base_controller_1.BaseController {
|
|
84
|
+
/**
|
|
85
|
+
* The Snap ID of the gator permissions provider.
|
|
86
|
+
*
|
|
87
|
+
* @returns The Snap ID of the gator permissions provider.
|
|
88
|
+
*/
|
|
89
|
+
get gatorPermissionsProviderSnapId() {
|
|
90
|
+
return __classPrivateFieldGet(this, _GatorPermissionsController_gatorPermissionsProviderSnapId, "f");
|
|
91
|
+
}
|
|
95
92
|
/**
|
|
96
93
|
* Creates a GatorPermissionsController instance.
|
|
97
94
|
*
|
|
98
95
|
* @param args - The arguments to this function.
|
|
99
|
-
* @param args.messenger - Messenger used to communicate with
|
|
100
|
-
* @param args.
|
|
96
|
+
* @param args.messenger - Messenger used to communicate with other controllers.
|
|
97
|
+
* @param args.config - Configuration (supported permission types and optional Snap id).
|
|
98
|
+
* @param args.state - Optional partial state to merge with defaults.
|
|
101
99
|
*/
|
|
102
|
-
constructor({ messenger, state, }) {
|
|
100
|
+
constructor({ messenger, config, state, }) {
|
|
101
|
+
const initialState = createGatorPermissionsControllerState(state);
|
|
103
102
|
super({
|
|
104
103
|
name: controllerName,
|
|
105
104
|
metadata: gatorPermissionsControllerMetadata,
|
|
106
105
|
messenger,
|
|
107
|
-
state:
|
|
108
|
-
...getDefaultGatorPermissionsControllerState(),
|
|
109
|
-
...state,
|
|
110
|
-
isFetchingGatorPermissions: false,
|
|
111
|
-
},
|
|
106
|
+
state: initialState,
|
|
112
107
|
});
|
|
113
108
|
_GatorPermissionsController_instances.add(this);
|
|
109
|
+
_GatorPermissionsController_supportedPermissionTypes.set(this, void 0);
|
|
110
|
+
_GatorPermissionsController_gatorPermissionsProviderSnapId.set(this, void 0);
|
|
111
|
+
_GatorPermissionsController_maxSyncIntervalMs.set(this, void 0);
|
|
112
|
+
/**
|
|
113
|
+
* When a sync is in progress, holds the promise for that sync so concurrent
|
|
114
|
+
* callers receive the same promise. Cleared when the sync completes.
|
|
115
|
+
*/
|
|
116
|
+
_GatorPermissionsController_fetchAndUpdateGatorPermissionsPromise.set(this, null);
|
|
117
|
+
__classPrivateFieldSet(this, _GatorPermissionsController_supportedPermissionTypes, config.supportedPermissionTypes, "f");
|
|
118
|
+
__classPrivateFieldSet(this, _GatorPermissionsController_gatorPermissionsProviderSnapId, config.gatorPermissionsProviderSnapId ??
|
|
119
|
+
defaultGatorPermissionsProviderSnapId, "f");
|
|
120
|
+
__classPrivateFieldSet(this, _GatorPermissionsController_maxSyncIntervalMs, config.maxSyncIntervalMs ?? DEFAULT_MAX_SYNC_INTERVAL_MS, "f");
|
|
114
121
|
__classPrivateFieldGet(this, _GatorPermissionsController_instances, "m", _GatorPermissionsController_registerMessageHandlers).call(this);
|
|
115
122
|
}
|
|
116
123
|
/**
|
|
117
|
-
*
|
|
124
|
+
* Supported permission types this controller was configured with.
|
|
118
125
|
*
|
|
119
|
-
* @returns The
|
|
126
|
+
* @returns The supported permission types.
|
|
120
127
|
*/
|
|
121
|
-
get
|
|
122
|
-
return (
|
|
128
|
+
get supportedPermissionTypes() {
|
|
129
|
+
return __classPrivateFieldGet(this, _GatorPermissionsController_supportedPermissionTypes, "f");
|
|
123
130
|
}
|
|
124
131
|
/**
|
|
125
|
-
*
|
|
132
|
+
* Fetches granted permissions from the gator permissions provider Snap and updates state.
|
|
133
|
+
* If a sync is already in progress, returns the same promise. After the sync completes,
|
|
134
|
+
* the next call will perform a new sync.
|
|
126
135
|
*
|
|
127
|
-
* @returns
|
|
128
|
-
|
|
129
|
-
get permissionsProviderSnapId() {
|
|
130
|
-
return this.state.gatorPermissionsProviderSnapId;
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Enables gator permissions for the user.
|
|
134
|
-
*/
|
|
135
|
-
async enableGatorPermissions() {
|
|
136
|
-
__classPrivateFieldGet(this, _GatorPermissionsController_instances, "m", _GatorPermissionsController_setIsGatorPermissionsEnabled).call(this, true);
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Clears the gator permissions map and disables the feature.
|
|
140
|
-
*/
|
|
141
|
-
async disableGatorPermissions() {
|
|
142
|
-
this.update((state) => {
|
|
143
|
-
state.isGatorPermissionsEnabled = false;
|
|
144
|
-
state.gatorPermissionsMapSerialized = (0, utils_1.serializeGatorPermissionsMap)(createEmptyGatorPermissionsMap());
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Gets the pending revocations list.
|
|
149
|
-
*
|
|
150
|
-
* @returns The pending revocations list.
|
|
136
|
+
* @returns A promise that resolves when the sync completes. All data is available via the controller's state.
|
|
137
|
+
* @throws {GatorPermissionsFetchError} If the gator permissions fetch fails.
|
|
151
138
|
*/
|
|
152
|
-
|
|
153
|
-
|
|
139
|
+
fetchAndUpdateGatorPermissions() {
|
|
140
|
+
if (__classPrivateFieldGet(this, _GatorPermissionsController_fetchAndUpdateGatorPermissionsPromise, "f") !== null) {
|
|
141
|
+
return __classPrivateFieldGet(this, _GatorPermissionsController_fetchAndUpdateGatorPermissionsPromise, "f");
|
|
142
|
+
}
|
|
143
|
+
const performFetchAndUpdate = async () => {
|
|
144
|
+
try {
|
|
145
|
+
__classPrivateFieldGet(this, _GatorPermissionsController_instances, "m", _GatorPermissionsController_setIsFetchingGatorPermissions).call(this, true);
|
|
146
|
+
// Only ever fetch non-revoked permissions. Revoked permissions may be
|
|
147
|
+
// left in storage by the gator permissions snap, but we don't need to
|
|
148
|
+
// fetch them.
|
|
149
|
+
const params = { isRevoked: false };
|
|
150
|
+
const permissionsData = await (0, utils_1.executeSnapRpc)({
|
|
151
|
+
messenger: this.messenger,
|
|
152
|
+
snapId: __classPrivateFieldGet(this, _GatorPermissionsController_gatorPermissionsProviderSnapId, "f"),
|
|
153
|
+
method: types_1.GatorPermissionsSnapRpcMethod.PermissionProviderGetGrantedPermissions,
|
|
154
|
+
params,
|
|
155
|
+
});
|
|
156
|
+
const grantedPermissions = __classPrivateFieldGet(this, _GatorPermissionsController_instances, "m", _GatorPermissionsController_storedPermissionsToPermissionInfoWithMetadata).call(this, permissionsData);
|
|
157
|
+
this.update((state) => {
|
|
158
|
+
state.grantedPermissions = grantedPermissions;
|
|
159
|
+
state.lastSyncedTimestamp = Date.now();
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
(0, logger_1.controllerLog)('Failed to fetch gator permissions', error);
|
|
164
|
+
throw new errors_1.GatorPermissionsFetchError({
|
|
165
|
+
message: 'Failed to fetch gator permissions',
|
|
166
|
+
cause: error,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
finally {
|
|
170
|
+
__classPrivateFieldGet(this, _GatorPermissionsController_instances, "m", _GatorPermissionsController_setIsFetchingGatorPermissions).call(this, false);
|
|
171
|
+
__classPrivateFieldSet(this, _GatorPermissionsController_fetchAndUpdateGatorPermissionsPromise, null, "f");
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
__classPrivateFieldSet(this, _GatorPermissionsController_fetchAndUpdateGatorPermissionsPromise, performFetchAndUpdate(), "f");
|
|
175
|
+
return __classPrivateFieldGet(this, _GatorPermissionsController_fetchAndUpdateGatorPermissionsPromise, "f");
|
|
154
176
|
}
|
|
155
177
|
/**
|
|
156
|
-
*
|
|
178
|
+
* Initializes the controller. Call once after construction to ensure the
|
|
179
|
+
* controller is ready for use.
|
|
157
180
|
*
|
|
158
|
-
* @
|
|
159
|
-
* @returns A promise that resolves to the gator permissions map.
|
|
160
|
-
* @throws {GatorPermissionsFetchError} If the gator permissions fetch fails.
|
|
181
|
+
* @returns A promise that resolves when initialization is complete.
|
|
161
182
|
*/
|
|
162
|
-
async
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
const gatorPermissionsMap = __classPrivateFieldGet(this, _GatorPermissionsController_instances, "m", _GatorPermissionsController_categorizePermissionsDataByTypeAndChainId).call(this, permissionsData);
|
|
171
|
-
this.update((state) => {
|
|
172
|
-
state.gatorPermissionsMapSerialized =
|
|
173
|
-
(0, utils_1.serializeGatorPermissionsMap)(gatorPermissionsMap);
|
|
174
|
-
});
|
|
175
|
-
return gatorPermissionsMap;
|
|
176
|
-
}
|
|
177
|
-
catch (error) {
|
|
178
|
-
(0, logger_1.controllerLog)('Failed to fetch gator permissions', error);
|
|
179
|
-
throw new errors_1.GatorPermissionsFetchError({
|
|
180
|
-
message: 'Failed to fetch gator permissions',
|
|
181
|
-
cause: error,
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
finally {
|
|
185
|
-
__classPrivateFieldGet(this, _GatorPermissionsController_instances, "m", _GatorPermissionsController_setIsFetchingGatorPermissions).call(this, false);
|
|
183
|
+
async initialize() {
|
|
184
|
+
const currentTime = Date.now();
|
|
185
|
+
const millisecondsSinceLastSync = currentTime - this.state.lastSyncedTimestamp;
|
|
186
|
+
// Sync only when we have no data or data is stale, to avoid excessive startup
|
|
187
|
+
// queries while still avoiding showing stale data while a refresh runs.
|
|
188
|
+
if (this.state.lastSyncedTimestamp === -1 ||
|
|
189
|
+
millisecondsSinceLastSync > __classPrivateFieldGet(this, _GatorPermissionsController_maxSyncIntervalMs, "f")) {
|
|
190
|
+
await this.fetchAndUpdateGatorPermissions();
|
|
186
191
|
}
|
|
187
192
|
}
|
|
188
193
|
/**
|
|
@@ -206,7 +211,7 @@ class GatorPermissionsController extends base_controller_1.BaseController {
|
|
|
206
211
|
* or the enforcers/terms do not match a supported permission type.
|
|
207
212
|
*/
|
|
208
213
|
decodePermissionFromPermissionContextForOrigin({ origin, chainId, delegation: { caveats, delegator, delegate, authority }, metadata: { justification, origin: specifiedOrigin }, }) {
|
|
209
|
-
if (origin !== this
|
|
214
|
+
if (origin !== __classPrivateFieldGet(this, _GatorPermissionsController_gatorPermissionsProviderSnapId, "f")) {
|
|
210
215
|
throw new errors_1.OriginNotAllowedError({ origin });
|
|
211
216
|
}
|
|
212
217
|
const contracts = contractsByChainId[chainId];
|
|
@@ -248,16 +253,14 @@ class GatorPermissionsController extends base_controller_1.BaseController {
|
|
|
248
253
|
*
|
|
249
254
|
* @param revocationParams - The revocation parameters containing the permission context.
|
|
250
255
|
* @returns A promise that resolves when the revocation is submitted successfully.
|
|
251
|
-
* @throws {GatorPermissionsNotEnabledError} If the gator permissions are not enabled.
|
|
252
256
|
* @throws {GatorPermissionsProviderError} If the snap request fails.
|
|
253
257
|
*/
|
|
254
258
|
async submitRevocation(revocationParams) {
|
|
255
259
|
(0, logger_1.controllerLog)('submitRevocation method called', {
|
|
256
260
|
permissionContext: revocationParams.permissionContext,
|
|
257
261
|
});
|
|
258
|
-
__classPrivateFieldGet(this, _GatorPermissionsController_instances, "m", _GatorPermissionsController_assertGatorPermissionsEnabled).call(this);
|
|
259
262
|
const snapRequest = {
|
|
260
|
-
snapId: this
|
|
263
|
+
snapId: __classPrivateFieldGet(this, _GatorPermissionsController_gatorPermissionsProviderSnapId, "f"),
|
|
261
264
|
origin: 'metamask',
|
|
262
265
|
handler: snaps_utils_1.HandlerType.OnRpcRequest,
|
|
263
266
|
request: {
|
|
@@ -269,7 +272,7 @@ class GatorPermissionsController extends base_controller_1.BaseController {
|
|
|
269
272
|
try {
|
|
270
273
|
const result = await this.messenger.call('SnapController:handleRequest', snapRequest);
|
|
271
274
|
// Refresh list first (permission removed from list)
|
|
272
|
-
await this.fetchAndUpdateGatorPermissions(
|
|
275
|
+
await this.fetchAndUpdateGatorPermissions();
|
|
273
276
|
(0, logger_1.controllerLog)('Successfully submitted revocation', {
|
|
274
277
|
permissionContext: revocationParams.permissionContext,
|
|
275
278
|
result,
|
|
@@ -325,7 +328,6 @@ class GatorPermissionsController extends base_controller_1.BaseController {
|
|
|
325
328
|
txId,
|
|
326
329
|
permissionContext,
|
|
327
330
|
});
|
|
328
|
-
__classPrivateFieldGet(this, _GatorPermissionsController_instances, "m", _GatorPermissionsController_assertGatorPermissionsEnabled).call(this);
|
|
329
331
|
// Track handlers and timeout for cleanup
|
|
330
332
|
const handlers = {
|
|
331
333
|
approved: undefined,
|
|
@@ -337,7 +339,7 @@ class GatorPermissionsController extends base_controller_1.BaseController {
|
|
|
337
339
|
};
|
|
338
340
|
// Helper to refresh permissions after transaction state change
|
|
339
341
|
const refreshPermissions = (context) => {
|
|
340
|
-
this.fetchAndUpdateGatorPermissions(
|
|
342
|
+
this.fetchAndUpdateGatorPermissions().catch((error) => {
|
|
341
343
|
(0, logger_1.controllerLog)(`Failed to refresh permissions after ${context}`, {
|
|
342
344
|
txId,
|
|
343
345
|
permissionContext,
|
|
@@ -488,11 +490,9 @@ class GatorPermissionsController extends base_controller_1.BaseController {
|
|
|
488
490
|
*
|
|
489
491
|
* @param params - The revocation parameters containing the permission context.
|
|
490
492
|
* @returns A promise that resolves when the revocation is submitted successfully.
|
|
491
|
-
* @throws {GatorPermissionsNotEnabledError} If the gator permissions are not enabled.
|
|
492
493
|
* @throws {GatorPermissionsProviderError} If the snap request fails.
|
|
493
494
|
*/
|
|
494
495
|
async submitDirectRevocation(params) {
|
|
495
|
-
__classPrivateFieldGet(this, _GatorPermissionsController_instances, "m", _GatorPermissionsController_assertGatorPermissionsEnabled).call(this);
|
|
496
496
|
// Use a placeholder txId that doesn't conflict with real transaction IDs
|
|
497
497
|
const placeholderTxId = `no-tx-${params.permissionContext}`;
|
|
498
498
|
// Add to pending revocations state first (disables UI button immediately)
|
|
@@ -507,18 +507,15 @@ class GatorPermissionsController extends base_controller_1.BaseController {
|
|
|
507
507
|
* @returns `true` if the permission context is pending revocation, `false` otherwise.
|
|
508
508
|
*/
|
|
509
509
|
isPendingRevocation(permissionContext) {
|
|
510
|
+
const requestedPermissionContextLowercase = permissionContext.toLowerCase();
|
|
510
511
|
return this.state.pendingRevocations.some((pendingRevocation) => pendingRevocation.permissionContext.toLowerCase() ===
|
|
511
|
-
|
|
512
|
+
requestedPermissionContextLowercase);
|
|
512
513
|
}
|
|
513
514
|
}
|
|
514
|
-
_GatorPermissionsController_instances = new WeakSet(), _GatorPermissionsController_setIsFetchingGatorPermissions = function _GatorPermissionsController_setIsFetchingGatorPermissions(isFetchingGatorPermissions) {
|
|
515
|
+
_GatorPermissionsController_supportedPermissionTypes = new WeakMap(), _GatorPermissionsController_gatorPermissionsProviderSnapId = new WeakMap(), _GatorPermissionsController_maxSyncIntervalMs = new WeakMap(), _GatorPermissionsController_fetchAndUpdateGatorPermissionsPromise = new WeakMap(), _GatorPermissionsController_instances = new WeakSet(), _GatorPermissionsController_setIsFetchingGatorPermissions = function _GatorPermissionsController_setIsFetchingGatorPermissions(isFetchingGatorPermissions) {
|
|
515
516
|
this.update((state) => {
|
|
516
517
|
state.isFetchingGatorPermissions = isFetchingGatorPermissions;
|
|
517
518
|
});
|
|
518
|
-
}, _GatorPermissionsController_setIsGatorPermissionsEnabled = function _GatorPermissionsController_setIsGatorPermissionsEnabled(isGatorPermissionsEnabled) {
|
|
519
|
-
this.update((state) => {
|
|
520
|
-
state.isGatorPermissionsEnabled = isGatorPermissionsEnabled;
|
|
521
|
-
});
|
|
522
519
|
}, _GatorPermissionsController_addPendingRevocationToState = function _GatorPermissionsController_addPendingRevocationToState(txId, permissionContext) {
|
|
523
520
|
this.update((state) => {
|
|
524
521
|
state.pendingRevocations = [
|
|
@@ -537,74 +534,24 @@ _GatorPermissionsController_instances = new WeakSet(), _GatorPermissionsControll
|
|
|
537
534
|
});
|
|
538
535
|
}, _GatorPermissionsController_registerMessageHandlers = function _GatorPermissionsController_registerMessageHandlers() {
|
|
539
536
|
this.messenger.registerActionHandler(`${controllerName}:fetchAndUpdateGatorPermissions`, this.fetchAndUpdateGatorPermissions.bind(this));
|
|
540
|
-
this.messenger.registerActionHandler(`${controllerName}:enableGatorPermissions`, this.enableGatorPermissions.bind(this));
|
|
541
|
-
this.messenger.registerActionHandler(`${controllerName}:disableGatorPermissions`, this.disableGatorPermissions.bind(this));
|
|
542
537
|
this.messenger.registerActionHandler(`${controllerName}:decodePermissionFromPermissionContextForOrigin`, this.decodePermissionFromPermissionContextForOrigin.bind(this));
|
|
543
538
|
const submitRevocationAction = `${controllerName}:submitRevocation`;
|
|
544
539
|
this.messenger.registerActionHandler(submitRevocationAction, this.submitRevocation.bind(this));
|
|
545
540
|
this.messenger.registerActionHandler(`${controllerName}:addPendingRevocation`, this.addPendingRevocation.bind(this));
|
|
546
541
|
this.messenger.registerActionHandler(`${controllerName}:submitDirectRevocation`, this.submitDirectRevocation.bind(this));
|
|
547
542
|
this.messenger.registerActionHandler(`${controllerName}:isPendingRevocation`, this.isPendingRevocation.bind(this));
|
|
548
|
-
},
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
}
|
|
552
|
-
}, _GatorPermissionsController_handleSnapRequestToGatorPermissionsProvider =
|
|
553
|
-
/**
|
|
554
|
-
* Forwards a Snap request to the SnapController.
|
|
555
|
-
*
|
|
556
|
-
* @param args - The request parameters.
|
|
557
|
-
* @param args.snapId - The ID of the Snap of the gator permissions provider snap.
|
|
558
|
-
* @param args.params - Optional parameters to pass to the snap method.
|
|
559
|
-
* @returns A promise that resolves with the gator permissions.
|
|
560
|
-
*/
|
|
561
|
-
async function _GatorPermissionsController_handleSnapRequestToGatorPermissionsProvider({ snapId, params, }) {
|
|
562
|
-
try {
|
|
563
|
-
const response = (await this.messenger.call('SnapController:handleRequest', {
|
|
564
|
-
snapId,
|
|
565
|
-
origin: 'metamask',
|
|
566
|
-
handler: snaps_utils_1.HandlerType.OnRpcRequest,
|
|
567
|
-
request: {
|
|
568
|
-
jsonrpc: '2.0',
|
|
569
|
-
method: types_1.GatorPermissionsSnapRpcMethod.PermissionProviderGetGrantedPermissions,
|
|
570
|
-
...(params !== undefined && { params }),
|
|
571
|
-
},
|
|
572
|
-
}));
|
|
573
|
-
return response;
|
|
574
|
-
}
|
|
575
|
-
catch (error) {
|
|
576
|
-
(0, logger_1.controllerLog)('Failed to handle snap request to gator permissions provider', error);
|
|
577
|
-
throw new errors_1.GatorPermissionsProviderError({
|
|
578
|
-
method: types_1.GatorPermissionsSnapRpcMethod.PermissionProviderGetGrantedPermissions,
|
|
579
|
-
cause: error,
|
|
580
|
-
});
|
|
581
|
-
}
|
|
582
|
-
}, _GatorPermissionsController_sanitizeStoredGatorPermission = function _GatorPermissionsController_sanitizeStoredGatorPermission(storedGatorPermission) {
|
|
583
|
-
const { permissionResponse } = storedGatorPermission;
|
|
584
|
-
const { dependencies, to, ...rest } = permissionResponse;
|
|
543
|
+
}, _GatorPermissionsController_storedPermissionToPermissionInfo = function _GatorPermissionsController_storedPermissionToPermissionInfo(storedGatorPermission) {
|
|
544
|
+
const { permissionResponse: fullPermissionResponse } = storedGatorPermission;
|
|
545
|
+
const { dependencies: _dependencies, to: _to, ...permissionResponse } = fullPermissionResponse;
|
|
585
546
|
return {
|
|
586
547
|
...storedGatorPermission,
|
|
587
|
-
permissionResponse
|
|
588
|
-
...rest,
|
|
589
|
-
},
|
|
548
|
+
permissionResponse,
|
|
590
549
|
};
|
|
591
|
-
},
|
|
592
|
-
const gatorPermissionsMap = createEmptyGatorPermissionsMap();
|
|
550
|
+
}, _GatorPermissionsController_storedPermissionsToPermissionInfoWithMetadata = function _GatorPermissionsController_storedPermissionsToPermissionInfoWithMetadata(storedGatorPermissions) {
|
|
593
551
|
if (!storedGatorPermissions) {
|
|
594
|
-
return
|
|
595
|
-
}
|
|
596
|
-
for (const storedGatorPermission of storedGatorPermissions) {
|
|
597
|
-
const { permissionResponse: { permission: { type: permissionType }, chainId, }, } = storedGatorPermission;
|
|
598
|
-
const isPermissionTypeKnown = Object.prototype.hasOwnProperty.call(gatorPermissionsMap, permissionType);
|
|
599
|
-
const permissionTypeKey = isPermissionTypeKnown
|
|
600
|
-
? permissionType
|
|
601
|
-
: 'other';
|
|
602
|
-
gatorPermissionsMap[permissionTypeKey][chainId] = [
|
|
603
|
-
...(gatorPermissionsMap[permissionTypeKey][chainId] || []),
|
|
604
|
-
__classPrivateFieldGet(this, _GatorPermissionsController_instances, "m", _GatorPermissionsController_sanitizeStoredGatorPermission).call(this, storedGatorPermission),
|
|
605
|
-
];
|
|
552
|
+
return [];
|
|
606
553
|
}
|
|
607
|
-
return
|
|
554
|
+
return storedGatorPermissions.map((storedPermission) => __classPrivateFieldGet(this, _GatorPermissionsController_instances, "m", _GatorPermissionsController_storedPermissionToPermissionInfo).call(this, storedPermission));
|
|
608
555
|
};
|
|
609
556
|
exports.default = GatorPermissionsController;
|
|
610
557
|
//# sourceMappingURL=GatorPermissionsController.cjs.map
|