@metamask-previews/permission-log-controller 0.0.0-preview.08978bf
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 +10 -0
- package/LICENSE +18 -0
- package/README.md +15 -0
- package/dist/PermissionLogController.d.ts +81 -0
- package/dist/PermissionLogController.d.ts.map +1 -0
- package/dist/PermissionLogController.js +239 -0
- package/dist/PermissionLogController.js.map +1 -0
- package/dist/enums.d.ts +14 -0
- package/dist/enums.d.ts.map +1 -0
- package/dist/enums.js +21 -0
- package/dist/enums.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/package.json +58 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
[Unreleased]: https://github.com/MetaMask/core/
|
package/LICENSE
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Copyright ConsenSys Software Inc. 2022. All rights reserved.
|
|
2
|
+
|
|
3
|
+
You acknowledge and agree that ConsenSys Software Inc. (“ConsenSys”) (or ConsenSys’s licensors) own all legal right, title and interest in and to the work, software, application, source code, documentation and any other documents in this repository (collectively, the “Program”), including any intellectual property rights which subsist in the Program (whether those rights happen to be registered or not, and wherever in the world those rights may exist), whether in source code or any other form.
|
|
4
|
+
|
|
5
|
+
Subject to the limited license below, you may not (and you may not permit anyone else to) distribute, publish, copy, modify, merge, combine with another program, create derivative works of, reverse engineer, decompile or otherwise attempt to extract the source code of, the Program or any part thereof, except that you may contribute to this repository.
|
|
6
|
+
|
|
7
|
+
You are granted a non-exclusive, non-transferable, non-sublicensable license to distribute, publish, copy, modify, merge, combine with another program or create derivative works of the Program (such resulting program, collectively, the “Resulting Program”) solely for Non-Commercial Use as long as you:
|
|
8
|
+
1. give prominent notice (“Notice”) with each copy of the Resulting Program that the Program is used in the Resulting Program and that the Program is the copyright of ConsenSys; and
|
|
9
|
+
2. subject the Resulting Program and any distribution, publication, copy, modification, merger therewith, combination with another program or derivative works thereof to the same Notice requirement and Non-Commercial Use restriction set forth herein.
|
|
10
|
+
|
|
11
|
+
“Non-Commercial Use” means each use as described in clauses (1)-(3) below, as reasonably determined by ConsenSys in its sole discretion:
|
|
12
|
+
1. personal use for research, personal study, private entertainment, hobby projects or amateur pursuits, in each case without any anticipated commercial application;
|
|
13
|
+
2. use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization or government institution; or
|
|
14
|
+
3. the number of monthly active users of the Resulting Program across all versions thereof and platforms globally do not exceed 10,000 at any time.
|
|
15
|
+
|
|
16
|
+
You will not use any trade mark, service mark, trade name, logo of ConsenSys or any other company or organization in a way that is likely or intended to cause confusion about the owner or authorized user of such marks, names or logos.
|
|
17
|
+
|
|
18
|
+
If you have any questions, comments or interest in pursuing any other use cases, please reach out to us at communications@metamask.io.
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# `@metamask/permission-log-controller`
|
|
2
|
+
|
|
3
|
+
Controller with middleware for logging requests and responses to restricted and permissions-related methods.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
`yarn add @metamask/permission-log-controller`
|
|
8
|
+
|
|
9
|
+
or
|
|
10
|
+
|
|
11
|
+
`npm install @metamask/permission-log-controller`
|
|
12
|
+
|
|
13
|
+
## Contributing
|
|
14
|
+
|
|
15
|
+
This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { BaseController, type RestrictedControllerMessenger } from '@metamask/base-controller';
|
|
2
|
+
import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';
|
|
3
|
+
import { type Json, type JsonRpcRequest, type JsonRpcParams } from '@metamask/utils';
|
|
4
|
+
import { LOG_METHOD_TYPES } from './enums';
|
|
5
|
+
export declare type JsonRpcRequestWithOrigin<Params extends JsonRpcParams = JsonRpcParams> = JsonRpcRequest<Params> & {
|
|
6
|
+
origin?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare type Caveat = {
|
|
9
|
+
type: string;
|
|
10
|
+
value: string[];
|
|
11
|
+
};
|
|
12
|
+
export declare type Permission = {
|
|
13
|
+
parentCapability: string;
|
|
14
|
+
caveats?: Caveat[];
|
|
15
|
+
};
|
|
16
|
+
export declare type PermissionActivityLog = {
|
|
17
|
+
id: string | number | null;
|
|
18
|
+
method: string;
|
|
19
|
+
methodType: LOG_METHOD_TYPES;
|
|
20
|
+
origin?: string;
|
|
21
|
+
requestTime: number;
|
|
22
|
+
responseTime: number | null;
|
|
23
|
+
success: boolean | null;
|
|
24
|
+
};
|
|
25
|
+
export declare type PermissionLog = {
|
|
26
|
+
accounts?: Record<string, number>;
|
|
27
|
+
lastApproved?: number;
|
|
28
|
+
};
|
|
29
|
+
export declare type PermissionEntry = Record<string, PermissionLog>;
|
|
30
|
+
export declare type PermissionHistory = Record<string, PermissionEntry>;
|
|
31
|
+
/**
|
|
32
|
+
*
|
|
33
|
+
* Permission log controller state
|
|
34
|
+
* @property permissionHistory - permission history
|
|
35
|
+
* @property permissionActivityLog - permission activity logs
|
|
36
|
+
*/
|
|
37
|
+
export declare type PermissionLogControllerState = {
|
|
38
|
+
permissionHistory: PermissionHistory;
|
|
39
|
+
permissionActivityLog: PermissionActivityLog[];
|
|
40
|
+
};
|
|
41
|
+
export declare type PermissionLogControllerOptions = {
|
|
42
|
+
restrictedMethods: Set<string>;
|
|
43
|
+
state?: Partial<PermissionLogControllerState>;
|
|
44
|
+
messenger: PermissionLogControllerMessenger;
|
|
45
|
+
};
|
|
46
|
+
export declare type PermissionLogControllerMessenger = RestrictedControllerMessenger<typeof name, never, never, never, never>;
|
|
47
|
+
/**
|
|
48
|
+
* The name of the {@link PermissionController}.
|
|
49
|
+
*/
|
|
50
|
+
declare const name = "PermissionLogController";
|
|
51
|
+
/**
|
|
52
|
+
* Controller with middleware for logging requests and responses to restricted
|
|
53
|
+
* and permissions-related methods.
|
|
54
|
+
*/
|
|
55
|
+
export declare class PermissionLogController extends BaseController<typeof name, PermissionLogControllerState, PermissionLogControllerMessenger> {
|
|
56
|
+
#private;
|
|
57
|
+
constructor({ messenger, restrictedMethods, state, }: PermissionLogControllerOptions);
|
|
58
|
+
/**
|
|
59
|
+
* Updates the exposed account history for the given origin.
|
|
60
|
+
* Sets the 'last seen' time to Date.now() for the given accounts.
|
|
61
|
+
* Does **not** update the 'lastApproved' time for the permission itself.
|
|
62
|
+
* Returns if the accounts array is empty.
|
|
63
|
+
*
|
|
64
|
+
* @param origin - The origin that the accounts are exposed to.
|
|
65
|
+
* @param accounts - The accounts.
|
|
66
|
+
*/
|
|
67
|
+
updateAccountsHistory(origin: string, accounts: string[]): void;
|
|
68
|
+
/**
|
|
69
|
+
* Create a permissions log middleware. Records permissions activity and history:
|
|
70
|
+
*
|
|
71
|
+
* Activity: requests and responses for restricted and most wallet_ methods.
|
|
72
|
+
*
|
|
73
|
+
* History: for each origin, the last time a permission was granted, including
|
|
74
|
+
* which accounts were exposed, if any.
|
|
75
|
+
*
|
|
76
|
+
* @returns The permissions log middleware.
|
|
77
|
+
*/
|
|
78
|
+
createMiddleware(): JsonRpcMiddleware<JsonRpcParams, Json>;
|
|
79
|
+
}
|
|
80
|
+
export {};
|
|
81
|
+
//# sourceMappingURL=PermissionLogController.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PermissionLogController.d.ts","sourceRoot":"","sources":["../src/PermissionLogController.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAA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AA4B,GAAG;IACzC,iBAAiB,EAAE,iBAAiB,CAAC;IACrC,qBAAqB,EAAE,qBAAqB,EAAE,CAAC;CAChD,CAAC;AAEF,oBAAY,8BAA8B,GAAG;IAC3C,iBAAiB,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IAC/B,KAAK,CAAC,EAAE,OAAO,CAAC,4BAA4B,CAAC,CAAC;IAC9C,SAAS,EAAE,gCAAgC,CAAC;CAC7C,CAAC;AAEF,oBAAY,gCAAgC,GAAG,6BAA6B,CAC1E,OAAO,IAAI,EACX,KAAK,EACL,KAAK,EACL,KAAK,EACL,KAAK,CACN,CAAC;AAOF;;GAEG;AACH,QAAA,MAAM,IAAI,4BAA4B,CAAC;AAEvC;;;GAGG;AACH,qBAAa,uBAAwB,SAAQ,cAAc,CACzD,OAAO,IAAI,EACX,4BAA4B,EAC5B,gCAAgC,CACjC;;gBAGa,EACV,SAAS,EACT,iBAAiB,EACjB,KAAK,GACN,EAAE,8BAA8B;IAmBjC;;;;;;;;OAQG;IACH,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE;IAYxD;;;;;;;;;OASG;IACH,gBAAgB,IAAI,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC;CAoS3D"}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
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");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
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");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _PermissionLogController_instances, _PermissionLogController_restrictedMethods, _PermissionLogController_getAccountToTimeMap, _PermissionLogController_logRequest, _PermissionLogController_logResponse, _PermissionLogController_logPermissionsHistory, _PermissionLogController_commitNewHistory, _PermissionLogController_getRequestedMethods, _PermissionLogController_getAccountsFromPermission;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.PermissionLogController = void 0;
|
|
16
|
+
const base_controller_1 = require("@metamask/base-controller");
|
|
17
|
+
const utils_1 = require("@metamask/utils");
|
|
18
|
+
const enums_1 = require("./enums");
|
|
19
|
+
const defaultState = {
|
|
20
|
+
permissionHistory: {},
|
|
21
|
+
permissionActivityLog: [],
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* The name of the {@link PermissionController}.
|
|
25
|
+
*/
|
|
26
|
+
const name = 'PermissionLogController';
|
|
27
|
+
/**
|
|
28
|
+
* Controller with middleware for logging requests and responses to restricted
|
|
29
|
+
* and permissions-related methods.
|
|
30
|
+
*/
|
|
31
|
+
class PermissionLogController extends base_controller_1.BaseController {
|
|
32
|
+
constructor({ messenger, restrictedMethods, state, }) {
|
|
33
|
+
super({
|
|
34
|
+
messenger,
|
|
35
|
+
name,
|
|
36
|
+
metadata: {
|
|
37
|
+
permissionHistory: {
|
|
38
|
+
persist: true,
|
|
39
|
+
anonymous: false,
|
|
40
|
+
},
|
|
41
|
+
permissionActivityLog: {
|
|
42
|
+
persist: true,
|
|
43
|
+
anonymous: false,
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
state: Object.assign(Object.assign({}, defaultState), state),
|
|
47
|
+
});
|
|
48
|
+
_PermissionLogController_instances.add(this);
|
|
49
|
+
_PermissionLogController_restrictedMethods.set(this, void 0);
|
|
50
|
+
__classPrivateFieldSet(this, _PermissionLogController_restrictedMethods, restrictedMethods, "f");
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Updates the exposed account history for the given origin.
|
|
54
|
+
* Sets the 'last seen' time to Date.now() for the given accounts.
|
|
55
|
+
* Does **not** update the 'lastApproved' time for the permission itself.
|
|
56
|
+
* Returns if the accounts array is empty.
|
|
57
|
+
*
|
|
58
|
+
* @param origin - The origin that the accounts are exposed to.
|
|
59
|
+
* @param accounts - The accounts.
|
|
60
|
+
*/
|
|
61
|
+
updateAccountsHistory(origin, accounts) {
|
|
62
|
+
if (accounts.length === 0) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const newEntries = {
|
|
66
|
+
eth_accounts: {
|
|
67
|
+
accounts: __classPrivateFieldGet(this, _PermissionLogController_instances, "m", _PermissionLogController_getAccountToTimeMap).call(this, accounts, Date.now()),
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
__classPrivateFieldGet(this, _PermissionLogController_instances, "m", _PermissionLogController_commitNewHistory).call(this, origin, newEntries);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Create a permissions log middleware. Records permissions activity and history:
|
|
74
|
+
*
|
|
75
|
+
* Activity: requests and responses for restricted and most wallet_ methods.
|
|
76
|
+
*
|
|
77
|
+
* History: for each origin, the last time a permission was granted, including
|
|
78
|
+
* which accounts were exposed, if any.
|
|
79
|
+
*
|
|
80
|
+
* @returns The permissions log middleware.
|
|
81
|
+
*/
|
|
82
|
+
createMiddleware() {
|
|
83
|
+
return (req, res, next) => {
|
|
84
|
+
const { origin, method } = req;
|
|
85
|
+
const isInternal = method.startsWith(enums_1.WALLET_PREFIX);
|
|
86
|
+
const isEthRequestAccounts = method === 'eth_requestAccounts';
|
|
87
|
+
// Determine if the method should be logged
|
|
88
|
+
if ((!enums_1.LOG_IGNORE_METHODS.includes(method) &&
|
|
89
|
+
(isInternal || __classPrivateFieldGet(this, _PermissionLogController_restrictedMethods, "f").has(method))) ||
|
|
90
|
+
isEthRequestAccounts) {
|
|
91
|
+
const activityEntry = __classPrivateFieldGet(this, _PermissionLogController_instances, "m", _PermissionLogController_logRequest).call(this, req, isInternal);
|
|
92
|
+
const requestedMethods = __classPrivateFieldGet(this, _PermissionLogController_instances, "m", _PermissionLogController_getRequestedMethods).call(this, req);
|
|
93
|
+
// Call next with a return handler for capturing the response
|
|
94
|
+
next((cb) => {
|
|
95
|
+
const time = Date.now();
|
|
96
|
+
__classPrivateFieldGet(this, _PermissionLogController_instances, "m", _PermissionLogController_logResponse).call(this, activityEntry, res, time);
|
|
97
|
+
if (requestedMethods && !res.error && res.result && origin) {
|
|
98
|
+
__classPrivateFieldGet(this, _PermissionLogController_instances, "m", _PermissionLogController_logPermissionsHistory).call(this, requestedMethods, origin, res.result, time, isEthRequestAccounts);
|
|
99
|
+
}
|
|
100
|
+
cb();
|
|
101
|
+
});
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
next();
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exports.PermissionLogController = PermissionLogController;
|
|
109
|
+
_PermissionLogController_restrictedMethods = new WeakMap(), _PermissionLogController_instances = new WeakSet(), _PermissionLogController_getAccountToTimeMap = function _PermissionLogController_getAccountToTimeMap(accounts, time) {
|
|
110
|
+
return accounts.reduce((acc, account) => (Object.assign(Object.assign({}, acc), { [account]: time })), {});
|
|
111
|
+
}, _PermissionLogController_logRequest = function _PermissionLogController_logRequest(request, isInternal) {
|
|
112
|
+
const activityEntry = {
|
|
113
|
+
id: request.id,
|
|
114
|
+
method: request.method,
|
|
115
|
+
methodType: isInternal
|
|
116
|
+
? enums_1.LOG_METHOD_TYPES.internal
|
|
117
|
+
: enums_1.LOG_METHOD_TYPES.restricted,
|
|
118
|
+
origin: request.origin,
|
|
119
|
+
requestTime: Date.now(),
|
|
120
|
+
responseTime: null,
|
|
121
|
+
success: null,
|
|
122
|
+
};
|
|
123
|
+
this.update((state) => {
|
|
124
|
+
const newLogs = [...state.permissionActivityLog, activityEntry];
|
|
125
|
+
state.permissionActivityLog =
|
|
126
|
+
// remove oldest log if exceeding size limit
|
|
127
|
+
newLogs.length > enums_1.LOG_LIMIT ? newLogs.slice(1) : newLogs;
|
|
128
|
+
});
|
|
129
|
+
return activityEntry;
|
|
130
|
+
}, _PermissionLogController_logResponse = function _PermissionLogController_logResponse(entry, response, time) {
|
|
131
|
+
if (!entry || !response) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
// The JSON-RPC 2.0 specification defines "success" by the presence of
|
|
135
|
+
// either the "result" or "error" property. The specification forbids
|
|
136
|
+
// both properties from being present simultaneously, and our JSON-RPC
|
|
137
|
+
// stack is spec-compliant at the time of writing.
|
|
138
|
+
this.update((state) => {
|
|
139
|
+
state.permissionActivityLog = state.permissionActivityLog.map((log) => {
|
|
140
|
+
// Update the log entry that matches the given entry id
|
|
141
|
+
if (log.id === entry.id) {
|
|
142
|
+
return Object.assign(Object.assign({}, log), { success: (0, utils_1.hasProperty)(response, 'result'), responseTime: time });
|
|
143
|
+
}
|
|
144
|
+
return log;
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
}, _PermissionLogController_logPermissionsHistory = function _PermissionLogController_logPermissionsHistory(requestedMethods, origin, result, time, isEthRequestAccounts) {
|
|
148
|
+
let newEntries;
|
|
149
|
+
if (isEthRequestAccounts) {
|
|
150
|
+
// Type assertion: We are assuming that the response data contains
|
|
151
|
+
// a set of accounts if the RPC method is "eth_requestAccounts".
|
|
152
|
+
const accounts = result;
|
|
153
|
+
newEntries = {
|
|
154
|
+
eth_accounts: {
|
|
155
|
+
accounts: __classPrivateFieldGet(this, _PermissionLogController_instances, "m", _PermissionLogController_getAccountToTimeMap).call(this, accounts, time),
|
|
156
|
+
lastApproved: time,
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
// Records new "lastApproved" times for the granted permissions, if any.
|
|
162
|
+
// Special handling for eth_accounts, in order to record the time the
|
|
163
|
+
// accounts were last seen or approved by the origin.
|
|
164
|
+
// Type assertion: We are assuming that the response data contains
|
|
165
|
+
// a set of permissions if the RPC method is "eth_requestPermissions".
|
|
166
|
+
const permissions = result;
|
|
167
|
+
newEntries = permissions.reduce((acc, permission) => {
|
|
168
|
+
const method = permission.parentCapability;
|
|
169
|
+
if (!requestedMethods.includes(method)) {
|
|
170
|
+
return acc;
|
|
171
|
+
}
|
|
172
|
+
if (method === 'eth_accounts') {
|
|
173
|
+
const accounts = __classPrivateFieldGet(this, _PermissionLogController_instances, "m", _PermissionLogController_getAccountsFromPermission).call(this, permission);
|
|
174
|
+
return Object.assign(Object.assign({}, acc), { [method]: {
|
|
175
|
+
lastApproved: time,
|
|
176
|
+
accounts: __classPrivateFieldGet(this, _PermissionLogController_instances, "m", _PermissionLogController_getAccountToTimeMap).call(this, accounts, time),
|
|
177
|
+
} });
|
|
178
|
+
}
|
|
179
|
+
return Object.assign(Object.assign({}, acc), { [method]: {
|
|
180
|
+
lastApproved: time,
|
|
181
|
+
} });
|
|
182
|
+
}, {});
|
|
183
|
+
}
|
|
184
|
+
if (Object.keys(newEntries).length > 0) {
|
|
185
|
+
__classPrivateFieldGet(this, _PermissionLogController_instances, "m", _PermissionLogController_commitNewHistory).call(this, origin, newEntries);
|
|
186
|
+
}
|
|
187
|
+
}, _PermissionLogController_commitNewHistory = function _PermissionLogController_commitNewHistory(origin, newEntries) {
|
|
188
|
+
var _a, _b;
|
|
189
|
+
const { permissionHistory } = this.state;
|
|
190
|
+
// a simple merge updates most permissions
|
|
191
|
+
const oldOriginHistory = (_a = permissionHistory[origin]) !== null && _a !== void 0 ? _a : {};
|
|
192
|
+
const newOriginHistory = Object.assign(Object.assign({}, oldOriginHistory), newEntries);
|
|
193
|
+
// eth_accounts requires special handling, because of information
|
|
194
|
+
// we store about the accounts
|
|
195
|
+
const existingEthAccountsEntry = oldOriginHistory.eth_accounts;
|
|
196
|
+
const newEthAccountsEntry = newEntries.eth_accounts;
|
|
197
|
+
if (existingEthAccountsEntry && newEthAccountsEntry) {
|
|
198
|
+
// we may intend to update just the accounts, not the permission
|
|
199
|
+
// itself
|
|
200
|
+
const lastApproved = (_b = newEthAccountsEntry.lastApproved) !== null && _b !== void 0 ? _b : existingEthAccountsEntry.lastApproved;
|
|
201
|
+
// merge old and new eth_accounts history entries
|
|
202
|
+
newOriginHistory.eth_accounts = {
|
|
203
|
+
lastApproved,
|
|
204
|
+
accounts: Object.assign(Object.assign({}, existingEthAccountsEntry.accounts), newEthAccountsEntry.accounts),
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
this.update((state) => {
|
|
208
|
+
state.permissionHistory = Object.assign(Object.assign({}, permissionHistory), { [origin]: newOriginHistory });
|
|
209
|
+
});
|
|
210
|
+
}, _PermissionLogController_getRequestedMethods = function _PermissionLogController_getRequestedMethods(request) {
|
|
211
|
+
const { method, params } = request;
|
|
212
|
+
if (method === 'eth_requestAccounts') {
|
|
213
|
+
return ['eth_accounts'];
|
|
214
|
+
}
|
|
215
|
+
else if (method === `${enums_1.WALLET_PREFIX}requestPermissions` &&
|
|
216
|
+
params &&
|
|
217
|
+
Array.isArray(params) &&
|
|
218
|
+
params[0] &&
|
|
219
|
+
typeof params[0] === 'object' &&
|
|
220
|
+
!Array.isArray(params[0])) {
|
|
221
|
+
return Object.keys(params[0]);
|
|
222
|
+
}
|
|
223
|
+
return null;
|
|
224
|
+
}, _PermissionLogController_getAccountsFromPermission = function _PermissionLogController_getAccountsFromPermission(permission) {
|
|
225
|
+
if (permission.parentCapability !== 'eth_accounts' || !permission.caveats) {
|
|
226
|
+
return [];
|
|
227
|
+
}
|
|
228
|
+
const accounts = new Set();
|
|
229
|
+
for (const caveat of permission.caveats) {
|
|
230
|
+
if (caveat.type === enums_1.CAVEAT_TYPES.restrictReturnedAccounts &&
|
|
231
|
+
Array.isArray(caveat.value)) {
|
|
232
|
+
for (const value of caveat.value) {
|
|
233
|
+
accounts.add(value);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return [...accounts];
|
|
238
|
+
};
|
|
239
|
+
//# sourceMappingURL=PermissionLogController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PermissionLogController.js","sourceRoot":"","sources":["../src/PermissionLogController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+DAGmC;AAEnC,2CAMyB;AAEzB,mCAMiB;AA6DjB,MAAM,YAAY,GAAiC;IACjD,iBAAiB,EAAE,EAAE;IACrB,qBAAqB,EAAE,EAAE;CAC1B,CAAC;AAEF;;GAEG;AACH,MAAM,IAAI,GAAG,yBAAyB,CAAC;AAEvC;;;GAGG;AACH,MAAa,uBAAwB,SAAQ,gCAI5C;IAGC,YAAY,EACV,SAAS,EACT,iBAAiB,EACjB,KAAK,GAC0B;QAC/B,KAAK,CAAC;YACJ,SAAS;YACT,IAAI;YACJ,QAAQ,EAAE;gBACR,iBAAiB,EAAE;oBACjB,OAAO,EAAE,IAAI;oBACb,SAAS,EAAE,KAAK;iBACjB;gBACD,qBAAqB,EAAE;oBACrB,OAAO,EAAE,IAAI;oBACb,SAAS,EAAE,KAAK;iBACjB;aACF;YACD,KAAK,kCAAO,YAAY,GAAK,KAAK,CAAE;SACrC,CAAC,CAAC;;QArBL,6DAAgC;QAsB9B,uBAAA,IAAI,8CAAsB,iBAAiB,MAAA,CAAC;IAC9C,CAAC;IAED;;;;;;;;OAQG;IACH,qBAAqB,CAAC,MAAc,EAAE,QAAkB;QACtD,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE;YACzB,OAAO;SACR;QACD,MAAM,UAAU,GAAG;YACjB,YAAY,EAAE;gBACZ,QAAQ,EAAE,uBAAA,IAAI,wFAAqB,MAAzB,IAAI,EAA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BAAiB,mCAClB,iBAAiB,KACpB,CAAC,MAAM,CAAC,EAAE,gBAAgB,GAC3B,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,uGAQoB,OAAiC;IACpD,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IACnC,IAAI,MAAM,KAAK,qBAAqB,EAAE;QACpC,OAAO,CAAC,cAAc,CAAC,CAAC;KACzB;SAAM,IACL,MAAM,KAAK,GAAG,qBAAa,oBAAoB;QAC/C,MAAM;QACN,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;QACrB,MAAM,CAAC,CAAC,CAAC;QACT,OAAO,MAAM,CAAC,CAAC,CAAC,KAAK,QAAQ;QAC7B,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EACzB;QACA,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;KAC/B;IACD,OAAO,IAAI,CAAC;AACd,CAAC,mHAW0B,UAAsB;IAC/C,IAAI,UAAU,CAAC,gBAAgB,KAAK,cAAc,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE;QACzE,OAAO,EAAE,CAAC;KACX;IAED,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAU,CAAC;IACnC,KAAK,MAAM,MAAM,IAAI,UAAU,CAAC,OAAO,EAAE;QACvC,IACE,MAAM,CAAC,IAAI,KAAK,oBAAY,CAAC,wBAAwB;YACrD,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,EAC3B;YACA,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,KAAK,EAAE;gBAChC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;aACrB;SACF;KACF;IAED,OAAO,CAAC,GAAG,QAAQ,CAAC,CAAC;AACvB,CAAC","sourcesContent":["import {\n BaseController,\n type RestrictedControllerMessenger,\n} from '@metamask/base-controller';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport {\n type Json,\n type JsonRpcRequest,\n type JsonRpcParams,\n type PendingJsonRpcResponse,\n hasProperty,\n} from '@metamask/utils';\n\nimport {\n LOG_IGNORE_METHODS,\n LOG_LIMIT,\n LOG_METHOD_TYPES,\n WALLET_PREFIX,\n CAVEAT_TYPES,\n} from './enums';\n\nexport type JsonRpcRequestWithOrigin<\n Params extends JsonRpcParams = JsonRpcParams,\n> = JsonRpcRequest<Params> & {\n origin?: string;\n};\n\nexport type Caveat = {\n type: string;\n value: string[];\n};\n\nexport type Permission = {\n parentCapability: string;\n caveats?: Caveat[];\n};\n\nexport type PermissionActivityLog = {\n id: string | number | null;\n method: string;\n methodType: LOG_METHOD_TYPES;\n origin?: string;\n requestTime: number;\n responseTime: number | null;\n success: boolean | null;\n};\n\nexport type PermissionLog = {\n accounts?: Record<string, number>;\n lastApproved?: number;\n};\nexport type PermissionEntry = Record<string, PermissionLog>;\n\nexport type PermissionHistory = Record<string, PermissionEntry>;\n\n/**\n *\n * Permission log controller state\n * @property permissionHistory - permission history\n * @property permissionActivityLog - permission activity logs\n */\nexport type PermissionLogControllerState = {\n permissionHistory: PermissionHistory;\n permissionActivityLog: PermissionActivityLog[];\n};\n\nexport type PermissionLogControllerOptions = {\n restrictedMethods: Set<string>;\n state?: Partial<PermissionLogControllerState>;\n messenger: PermissionLogControllerMessenger;\n};\n\nexport type PermissionLogControllerMessenger = RestrictedControllerMessenger<\n typeof name,\n never,\n never,\n never,\n never\n>;\n\nconst defaultState: PermissionLogControllerState = {\n permissionHistory: {},\n permissionActivityLog: [],\n};\n\n/**\n * The name of the {@link PermissionController}.\n */\nconst name = 'PermissionLogController';\n\n/**\n * Controller with middleware for logging requests and responses to restricted\n * and permissions-related methods.\n */\nexport class PermissionLogController extends BaseController<\n typeof name,\n PermissionLogControllerState,\n PermissionLogControllerMessenger\n> {\n #restrictedMethods: Set<string>;\n\n constructor({\n messenger,\n restrictedMethods,\n state,\n }: PermissionLogControllerOptions) {\n super({\n messenger,\n name,\n metadata: {\n permissionHistory: {\n persist: true,\n anonymous: false,\n },\n permissionActivityLog: {\n persist: true,\n anonymous: false,\n },\n },\n state: { ...defaultState, ...state },\n });\n this.#restrictedMethods = restrictedMethods;\n }\n\n /**\n * Updates the exposed account history for the given origin.\n * Sets the 'last seen' time to Date.now() for the given accounts.\n * Does **not** update the 'lastApproved' time for the permission itself.\n * Returns if the accounts array is empty.\n *\n * @param origin - The origin that the accounts are exposed to.\n * @param accounts - The accounts.\n */\n updateAccountsHistory(origin: string, accounts: string[]) {\n if (accounts.length === 0) {\n return;\n }\n const newEntries = {\n eth_accounts: {\n accounts: this.#getAccountToTimeMap(accounts, Date.now()),\n },\n };\n this.#commitNewHistory(origin, newEntries);\n }\n\n /**\n * Create a permissions log middleware. Records permissions activity and history:\n *\n * Activity: requests and responses for restricted and most wallet_ methods.\n *\n * History: for each origin, the last time a permission was granted, including\n * which accounts were exposed, if any.\n *\n * @returns The permissions log middleware.\n */\n createMiddleware(): JsonRpcMiddleware<JsonRpcParams, Json> {\n return (req: JsonRpcRequestWithOrigin, res, next) => {\n const { origin, method } = req;\n const isInternal = method.startsWith(WALLET_PREFIX);\n const isEthRequestAccounts = method === 'eth_requestAccounts';\n\n // Determine if the method should be logged\n if (\n (!LOG_IGNORE_METHODS.includes(method) &&\n (isInternal || this.#restrictedMethods.has(method))) ||\n isEthRequestAccounts\n ) {\n const activityEntry = this.#logRequest(req, isInternal);\n\n const requestedMethods = this.#getRequestedMethods(req);\n\n // Call next with a return handler for capturing the response\n next((cb) => {\n const time = Date.now();\n this.#logResponse(activityEntry, res, time);\n\n if (requestedMethods && !res.error && res.result && origin) {\n this.#logPermissionsHistory(\n requestedMethods,\n origin,\n res.result,\n time,\n isEthRequestAccounts,\n );\n }\n cb();\n });\n return;\n }\n\n next();\n };\n }\n\n /**\n * Get a map from account addresses to the given time.\n *\n * @param accounts - An array of addresses.\n * @param time - A time, e.g. Date.now().\n * @returns A string:number map of addresses to time.\n */\n #getAccountToTimeMap(\n accounts: string[],\n time: number,\n ): Record<string, number> {\n return accounts.reduce(\n (acc, account) => ({\n ...acc,\n [account]: time,\n }),\n {},\n );\n }\n\n /**\n * Creates and commits an activity log entry, without response data.\n *\n * @param request - The request object.\n * @param isInternal - Whether the request is internal.\n * @returns new added activity entry\n */\n #logRequest(\n request: JsonRpcRequestWithOrigin,\n isInternal: boolean,\n ): PermissionActivityLog {\n const activityEntry: PermissionActivityLog = {\n id: request.id,\n method: request.method,\n methodType: isInternal\n ? LOG_METHOD_TYPES.internal\n : LOG_METHOD_TYPES.restricted,\n origin: request.origin,\n requestTime: Date.now(),\n responseTime: null,\n success: null,\n };\n this.update((state) => {\n const newLogs = [...state.permissionActivityLog, activityEntry];\n state.permissionActivityLog =\n // remove oldest log if exceeding size limit\n newLogs.length > LOG_LIMIT ? newLogs.slice(1) : newLogs;\n });\n return activityEntry;\n }\n\n /**\n * Adds response data to an existing activity log entry.\n * Entry assumed already committed (i.e., in the log).\n *\n * @param entry - The entry to add a response to.\n * @param response - The response object.\n * @param time - Output from Date.now()\n */\n #logResponse(\n entry: PermissionActivityLog,\n response: PendingJsonRpcResponse<Json>,\n time: number,\n ) {\n if (!entry || !response) {\n return;\n }\n\n // The JSON-RPC 2.0 specification defines \"success\" by the presence of\n // either the \"result\" or \"error\" property. The specification forbids\n // both properties from being present simultaneously, and our JSON-RPC\n // stack is spec-compliant at the time of writing.\n this.update((state) => {\n state.permissionActivityLog = state.permissionActivityLog.map((log) => {\n // Update the log entry that matches the given entry id\n if (log.id === entry.id) {\n return {\n ...log,\n success: hasProperty(response, 'result'),\n responseTime: time,\n };\n }\n return log;\n });\n });\n }\n\n /**\n * Create new permissions history log entries, if any, and commit them.\n *\n * @param requestedMethods - The method names corresponding to the requested permissions.\n * @param origin - The origin of the permissions request.\n * @param result - The permissions request response.result.\n * @param time - The time of the request, i.e. Date.now().\n * @param isEthRequestAccounts - Whether the permissions request was 'eth_requestAccounts'.\n */\n #logPermissionsHistory(\n requestedMethods: string[],\n origin: string,\n result: Json,\n time: number,\n isEthRequestAccounts: boolean,\n ) {\n let newEntries: PermissionEntry;\n\n if (isEthRequestAccounts) {\n // Type assertion: We are assuming that the response data contains\n // a set of accounts if the RPC method is \"eth_requestAccounts\".\n const accounts = result as string[];\n newEntries = {\n eth_accounts: {\n accounts: this.#getAccountToTimeMap(accounts, time),\n lastApproved: time,\n },\n };\n } else {\n // Records new \"lastApproved\" times for the granted permissions, if any.\n // Special handling for eth_accounts, in order to record the time the\n // accounts were last seen or approved by the origin.\n // Type assertion: We are assuming that the response data contains\n // a set of permissions if the RPC method is \"eth_requestPermissions\".\n const permissions = result as Permission[];\n newEntries = permissions.reduce((acc: PermissionEntry, permission) => {\n const method = permission.parentCapability;\n\n if (!requestedMethods.includes(method)) {\n return acc;\n }\n\n if (method === 'eth_accounts') {\n const accounts = this.#getAccountsFromPermission(permission);\n return {\n ...acc,\n [method]: {\n lastApproved: time,\n accounts: this.#getAccountToTimeMap(accounts, time),\n },\n };\n }\n\n return {\n ...acc,\n [method]: {\n lastApproved: time,\n },\n };\n }, {});\n }\n\n if (Object.keys(newEntries).length > 0) {\n this.#commitNewHistory(origin, newEntries);\n }\n }\n\n /**\n * Commit new entries to the permissions history log.\n * Merges the history for the given origin, overwriting existing entries\n * with the same key (permission name).\n *\n * @param origin - The requesting origin.\n * @param newEntries - The new entries to commit.\n */\n #commitNewHistory(origin: string, newEntries: PermissionEntry) {\n const { permissionHistory } = this.state;\n\n // a simple merge updates most permissions\n const oldOriginHistory = permissionHistory[origin] ?? {};\n const newOriginHistory = {\n ...oldOriginHistory,\n ...newEntries,\n };\n\n // eth_accounts requires special handling, because of information\n // we store about the accounts\n const existingEthAccountsEntry = oldOriginHistory.eth_accounts;\n const newEthAccountsEntry = newEntries.eth_accounts;\n\n if (existingEthAccountsEntry && newEthAccountsEntry) {\n // we may intend to update just the accounts, not the permission\n // itself\n const lastApproved =\n newEthAccountsEntry.lastApproved ??\n existingEthAccountsEntry.lastApproved;\n\n // merge old and new eth_accounts history entries\n newOriginHistory.eth_accounts = {\n lastApproved,\n accounts: {\n ...existingEthAccountsEntry.accounts,\n ...newEthAccountsEntry.accounts,\n },\n };\n }\n\n this.update((state) => {\n state.permissionHistory = {\n ...permissionHistory,\n [origin]: newOriginHistory,\n };\n });\n }\n\n /**\n * Get all requested methods from a permissions request.\n *\n * @param request - The request object.\n * @returns The names of the requested permissions.\n */\n #getRequestedMethods(request: JsonRpcRequestWithOrigin): string[] | null {\n const { method, params } = request;\n if (method === 'eth_requestAccounts') {\n return ['eth_accounts'];\n } else if (\n method === `${WALLET_PREFIX}requestPermissions` &&\n params &&\n Array.isArray(params) &&\n params[0] &&\n typeof params[0] === 'object' &&\n !Array.isArray(params[0])\n ) {\n return Object.keys(params[0]);\n }\n return null;\n }\n\n /**\n * Get the permitted accounts from an eth_accounts permissions object.\n * Returns an empty array if the permission is not eth_accounts.\n *\n * @param permission - The permissions object.\n * @param permission.parentCapability - The permissions parentCapability.\n * @param permission.caveats - The permissions caveats.\n * @returns The permitted accounts.\n */\n #getAccountsFromPermission(permission: Permission): string[] {\n if (permission.parentCapability !== 'eth_accounts' || !permission.caveats) {\n return [];\n }\n\n const accounts = new Set<string>();\n for (const caveat of permission.caveats) {\n if (\n caveat.type === CAVEAT_TYPES.restrictReturnedAccounts &&\n Array.isArray(caveat.value)\n ) {\n for (const value of caveat.value) {\n accounts.add(value);\n }\n }\n }\n\n return [...accounts];\n }\n}\n"]}
|
package/dist/enums.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const WALLET_PREFIX = "wallet_";
|
|
2
|
+
export declare const CAVEAT_TYPES: Readonly<{
|
|
3
|
+
restrictReturnedAccounts: "restrictReturnedAccounts";
|
|
4
|
+
}>;
|
|
5
|
+
export declare const LOG_IGNORE_METHODS: string[];
|
|
6
|
+
export declare enum LOG_METHOD_TYPES {
|
|
7
|
+
restricted = "restricted",
|
|
8
|
+
internal = "internal"
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The permission activity log size limit.
|
|
12
|
+
*/
|
|
13
|
+
export declare const LOG_LIMIT = 100;
|
|
14
|
+
//# sourceMappingURL=enums.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enums.d.ts","sourceRoot":"","sources":["../src/enums.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,aAAa,YAAY,CAAC;AAEvC,eAAO,MAAM,YAAY;;EAEvB,CAAC;AAEH,eAAO,MAAM,kBAAkB,UAG9B,CAAC;AAEF,oBAAY,gBAAgB;IAC1B,UAAU,eAAe;IACzB,QAAQ,aAAa;CACtB;AAED;;GAEG;AACH,eAAO,MAAM,SAAS,MAAM,CAAC"}
|
package/dist/enums.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LOG_LIMIT = exports.LOG_METHOD_TYPES = exports.LOG_IGNORE_METHODS = exports.CAVEAT_TYPES = exports.WALLET_PREFIX = void 0;
|
|
4
|
+
exports.WALLET_PREFIX = 'wallet_';
|
|
5
|
+
exports.CAVEAT_TYPES = Object.freeze({
|
|
6
|
+
restrictReturnedAccounts: 'restrictReturnedAccounts',
|
|
7
|
+
});
|
|
8
|
+
exports.LOG_IGNORE_METHODS = [
|
|
9
|
+
'wallet_registerOnboarding',
|
|
10
|
+
'wallet_watchAsset',
|
|
11
|
+
];
|
|
12
|
+
var LOG_METHOD_TYPES;
|
|
13
|
+
(function (LOG_METHOD_TYPES) {
|
|
14
|
+
LOG_METHOD_TYPES["restricted"] = "restricted";
|
|
15
|
+
LOG_METHOD_TYPES["internal"] = "internal";
|
|
16
|
+
})(LOG_METHOD_TYPES = exports.LOG_METHOD_TYPES || (exports.LOG_METHOD_TYPES = {}));
|
|
17
|
+
/**
|
|
18
|
+
* The permission activity log size limit.
|
|
19
|
+
*/
|
|
20
|
+
exports.LOG_LIMIT = 100;
|
|
21
|
+
//# sourceMappingURL=enums.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enums.js","sourceRoot":"","sources":["../src/enums.ts"],"names":[],"mappings":";;;AAAa,QAAA,aAAa,GAAG,SAAS,CAAC;AAE1B,QAAA,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC;IACxC,wBAAwB,EAAE,0BAAmC;CAC9D,CAAC,CAAC;AAEU,QAAA,kBAAkB,GAAG;IAChC,2BAA2B;IAC3B,mBAAmB;CACpB,CAAC;AAEF,IAAY,gBAGX;AAHD,WAAY,gBAAgB;IAC1B,6CAAyB,CAAA;IACzB,yCAAqB,CAAA;AACvB,CAAC,EAHW,gBAAgB,GAAhB,wBAAgB,KAAhB,wBAAgB,QAG3B;AAED;;GAEG;AACU,QAAA,SAAS,GAAG,GAAG,CAAC","sourcesContent":["export const WALLET_PREFIX = 'wallet_';\n\nexport const CAVEAT_TYPES = Object.freeze({\n restrictReturnedAccounts: 'restrictReturnedAccounts' as const,\n});\n\nexport const LOG_IGNORE_METHODS = [\n 'wallet_registerOnboarding',\n 'wallet_watchAsset',\n];\n\nexport enum LOG_METHOD_TYPES {\n restricted = 'restricted',\n internal = 'internal',\n}\n\n/**\n * The permission activity log size limit.\n */\nexport const LOG_LIMIT = 100;\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./PermissionLogController"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4DAA0C","sourcesContent":["export * from './PermissionLogController';\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@metamask-previews/permission-log-controller",
|
|
3
|
+
"version": "0.0.0-preview.08978bf",
|
|
4
|
+
"description": "Controller with middleware for logging requests and responses to restricted and permissions-related methods",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"MetaMask",
|
|
7
|
+
"Ethereum"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/MetaMask/core/tree/main/packages/permission-log-controller#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/MetaMask/core/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/MetaMask/core.git"
|
|
16
|
+
},
|
|
17
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
18
|
+
"main": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist/"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build:docs": "typedoc",
|
|
25
|
+
"changelog:update": "../../scripts/update-changelog.sh @metamask/permission-log-controller",
|
|
26
|
+
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/permission-log-controller",
|
|
27
|
+
"publish:preview": "yarn npm publish --tag preview",
|
|
28
|
+
"test": "jest --reporters=jest-silent-reporter",
|
|
29
|
+
"test:clean": "jest --clearCache",
|
|
30
|
+
"test:verbose": "jest --verbose",
|
|
31
|
+
"test:watch": "jest --watch"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@metamask/base-controller": "^4.1.1",
|
|
35
|
+
"@metamask/json-rpc-engine": "^7.3.2",
|
|
36
|
+
"@metamask/utils": "^8.3.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@metamask/auto-changelog": "^3.4.4",
|
|
40
|
+
"@types/deep-freeze-strict": "^1.1.0",
|
|
41
|
+
"@types/jest": "^27.4.1",
|
|
42
|
+
"deep-freeze-strict": "^1.1.1",
|
|
43
|
+
"deepmerge": "^4.2.2",
|
|
44
|
+
"jest": "^27.5.1",
|
|
45
|
+
"nanoid": "^3.1.31",
|
|
46
|
+
"ts-jest": "^27.1.4",
|
|
47
|
+
"typedoc": "^0.24.8",
|
|
48
|
+
"typedoc-plugin-missing-exports": "^2.0.0",
|
|
49
|
+
"typescript": "~4.8.4"
|
|
50
|
+
},
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=16.0.0"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public",
|
|
56
|
+
"registry": "https://registry.npmjs.org/"
|
|
57
|
+
}
|
|
58
|
+
}
|