@metamask-previews/permission-log-controller 5.0.0-preview-ba016213 → 5.0.0-preview-29587976

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.
@@ -64,8 +64,6 @@ class PermissionLogController extends base_controller_1.BaseController {
64
64
  return;
65
65
  }
66
66
  const newEntries = {
67
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
68
- // eslint-disable-next-line @typescript-eslint/naming-convention
69
67
  eth_accounts: {
70
68
  accounts: __classPrivateFieldGet(this, _PermissionLogController_instances, "m", _PermissionLogController_getAccountToTimeMap).call(this, accounts, Date.now()),
71
69
  },
@@ -161,8 +159,6 @@ _PermissionLogController_restrictedMethods = new WeakMap(), _PermissionLogContro
161
159
  // a set of accounts if the RPC method is "eth_requestAccounts".
162
160
  const accounts = result;
163
161
  newEntries = {
164
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
165
- // eslint-disable-next-line @typescript-eslint/naming-convention
166
162
  eth_accounts: {
167
163
  accounts: __classPrivateFieldGet(this, _PermissionLogController_instances, "m", _PermissionLogController_getAccountToTimeMap).call(this, accounts, time),
168
164
  lastApproved: time,
@@ -1 +1 @@
1
- {"version":3,"file":"PermissionLogController.cjs","sourceRoot":"","sources":["../src/PermissionLogController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+DAImC;AAGnC,2CAMyB;AAEzB,uCAMiB;AAyEjB,MAAM,YAAY,GAAiC;IACjD,iBAAiB,EAAE,EAAE;IACrB,qBAAqB,EAAE,EAAE;CAC1B,CAAC;AAEF,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,kBAAkB,EAAE,IAAI;oBACxB,O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AC/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,CAAC;QACD,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IAChC,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC,mHAW0B,UAAsB;IAC/C,IAAI,UAAU,CAAC,gBAAgB,KAAK,cAAc,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;QAC1E,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAU,CAAC;IACnC,KAAK,MAAM,MAAM,IAAI,UAAU,CAAC,OAAO,EAAE,CAAC;QACxC,IACE,MAAM,CAAC,IAAI,KAAK,oBAAY,CAAC,wBAAwB;YACrD,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,EAC3B,CAAC;YACD,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;gBACjC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YACtB,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,CAAC,GAAG,QAAQ,CAAC,CAAC;AACvB,CAAC","sourcesContent":["import {\n BaseController,\n type ControllerGetStateAction,\n type ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport type { Messenger } from '@metamask/messenger';\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 PermissionLogControllerGetStateAction = ControllerGetStateAction<\n typeof name,\n PermissionLogControllerState\n>;\n\nexport type PermissionLogControllerActions =\n PermissionLogControllerGetStateAction;\n\nexport type PermissionLogControllerStateChangeEvent =\n ControllerStateChangeEvent<typeof name, PermissionLogControllerState>;\n\nexport type PermissionLogControllerEvents =\n PermissionLogControllerStateChangeEvent;\n\nexport type PermissionLogControllerMessenger = Messenger<\n typeof name,\n PermissionLogControllerActions,\n PermissionLogControllerEvents\n>;\n\nconst defaultState: PermissionLogControllerState = {\n permissionHistory: {},\n permissionActivityLog: [],\n};\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 includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n permissionActivityLog: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: 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 // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\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,\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 // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\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"]}
1
+ {"version":3,"file":"PermissionLogController.cjs","sourceRoot":"","sources":["../src/PermissionLogController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+DAIm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Content":["import {\n BaseController,\n type ControllerGetStateAction,\n type ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport type { Messenger } from '@metamask/messenger';\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 *\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 PermissionLogControllerGetStateAction = ControllerGetStateAction<\n typeof name,\n PermissionLogControllerState\n>;\n\nexport type PermissionLogControllerActions =\n PermissionLogControllerGetStateAction;\n\nexport type PermissionLogControllerStateChangeEvent =\n ControllerStateChangeEvent<typeof name, PermissionLogControllerState>;\n\nexport type PermissionLogControllerEvents =\n PermissionLogControllerStateChangeEvent;\n\nexport type PermissionLogControllerMessenger = Messenger<\n typeof name,\n PermissionLogControllerActions,\n PermissionLogControllerEvents\n>;\n\nconst defaultState: PermissionLogControllerState = {\n permissionHistory: {},\n permissionActivityLog: [],\n};\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 readonly #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 includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n permissionActivityLog: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: 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,\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"]}
@@ -32,6 +32,7 @@ export type PermissionHistory = Record<string, PermissionEntry>;
32
32
  /**
33
33
  *
34
34
  * Permission log controller state
35
+ *
35
36
  * @property permissionHistory - permission history
36
37
  * @property permissionActivityLog - permission activity logs
37
38
  */
@@ -1 +1 @@
1
- {"version":3,"file":"PermissionLogController.d.cts","sourceRoot":"","sources":["../src/PermissionLogController.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,KAAK,wBAAwB,EAC7B,KAAK,0BAA0B,EAChC,kCAAkC;AACnC,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AACnE,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,EACL,KAAK,IAAI,EACT,KAAK,cAAc,EACnB,KAAK,aAAa,EAGnB,wBAAwB;AAEzB,OAAO,EAGL,gBAAgB,EAGjB,oBAAgB;AAEjB,MAAM,MAAM,wBAAwB,CAClC,MAAM,SAAS,aAAa,GAAG,aAAa,IAC1C,cAAc,CAAC,MAAM,CAAC,GAAG;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,gBAAgB,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,gBAAgB,CAAC;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY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EACT,iBAAiB,EACjB,KAAK,GACN,EAAE,8BAA8B;IAuBjC;;;;;;;;OAQG;IACH,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE;IAcxD;;;;;;;;;OASG;IACH,gBAAgB,IAAI,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC;CAsS3D"}
1
+ {"version":3,"file":"PermissionLogController.d.cts","sourceRoot":"","sources":["../src/PermissionLogController.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,KAAK,wBAAwB,EAC7B,KAAK,0BAA0B,EAChC,kCAAkC;AACnC,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AACnE,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,EACL,KAAK,IAAI,EACT,KAAK,cAAc,EACnB,KAAK,aAAa,EAGnB,wBAAwB;AAEzB,OAAO,EAGL,gBAAgB,EAGjB,oBAAgB;AAEjB,MAAM,MAAM,wBAAwB,CAClC,MAAM,SAAS,aAAa,GAAG,aAAa,IAC1C,cAAc,CAAC,MAAM,CAAC,GAAG;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,gBAAgB,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,gBAAgB,CAAC;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;I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AAE,8BAA8B;IAuBjC;;;;;;;;OAQG;IACH,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE;IAYxD;;;;;;;;;OASG;IACH,gBAAgB,IAAI,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC;CAoS3D"}
@@ -32,6 +32,7 @@ export type PermissionHistory = Record<string, PermissionEntry>;
32
32
  /**
33
33
  *
34
34
  * Permission log controller state
35
+ *
35
36
  * @property permissionHistory - permission history
36
37
  * @property permissionActivityLog - permission activity logs
37
38
  */
@@ -1 +1 @@
1
- {"version":3,"file":"PermissionLogController.d.mts","sourceRoot":"","sources":["../src/PermissionLogController.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,KAAK,wBAAwB,EAC7B,KAAK,0BAA0B,EAChC,kCAAkC;AACnC,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AACnE,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,EACL,KAAK,IAAI,EACT,KAAK,cAAc,EACnB,KAAK,aAAa,EAGnB,wBAAwB;AAEzB,OAAO,EAGL,gBAAgB,EAGjB,oBAAgB;AAEjB,MAAM,MAAM,wBAAwB,CAClC,MAAM,SAAS,aAAa,GAAG,aAAa,IAC1C,cAAc,CAAC,MAAM,CAAC,GAAG;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,gBAAgB,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,UAA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qBAAa,uBAAwB,SAAQ,cAAc,CACzD,OAAO,IAAI,EACX,4BAA4B,EAC5B,gCAAgC,CACjC;;gBAGa,EACV,SAAS,EACT,iBAAiB,EACjB,KAAK,GACN,EAAE,8BAA8B;IAuBjC;;;;;;;;OAQG;IACH,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE;IAcxD;;;;;;;;;OASG;IACH,gBAAgB,IAAI,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC;CAsS3D"}
1
+ {"version":3,"file":"PermissionLogController.d.mts","sourceRoot":"","sources":["../src/PermissionLogController.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,KAAK,wBAAwB,EAC7B,KAAK,0BAA0B,EAChC,kCAAkC;AACnC,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AACnE,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,EACL,KAAK,IAAI,EACT,KAAK,cAAc,EACnB,KAAK,aAAa,EAGnB,wBAAwB;AAEzB,OAAO,EAGL,gBAAgB,EAGjB,oBAAgB;AAEjB,MAAM,MAAM,wBAAwB,CAClC,MAAM,SAAS,aAAa,GAAG,aAAa,IAC1C,cAAc,CAAC,MAAM,CAAC,GAAG;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,gBAAgB,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,gBAAgB,CAAC;IAC7B,MA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zD,OAAO,IAAI,EACX,4BAA4B,EAC5B,gCAAgC,CACjC;;gBAGa,EACV,SAAS,EACT,iBAAiB,EACjB,KAAK,GACN,EAAE,8BAA8B;IAuBjC;;;;;;;;OAQG;IACH,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE;IAYxD;;;;;;;;;OASG;IACH,gBAAgB,IAAI,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC;CAoS3D"}
@@ -61,8 +61,6 @@ export class PermissionLogController extends BaseController {
61
61
  return;
62
62
  }
63
63
  const newEntries = {
64
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
65
- // eslint-disable-next-line @typescript-eslint/naming-convention
66
64
  eth_accounts: {
67
65
  accounts: __classPrivateFieldGet(this, _PermissionLogController_instances, "m", _PermissionLogController_getAccountToTimeMap).call(this, accounts, Date.now()),
68
66
  },
@@ -157,8 +155,6 @@ _PermissionLogController_restrictedMethods = new WeakMap(), _PermissionLogContro
157
155
  // a set of accounts if the RPC method is "eth_requestAccounts".
158
156
  const accounts = result;
159
157
  newEntries = {
160
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
161
- // eslint-disable-next-line @typescript-eslint/naming-convention
162
158
  eth_accounts: {
163
159
  accounts: __classPrivateFieldGet(this, _PermissionLogController_instances, "m", _PermissionLogController_getAccountToTimeMap).call(this, accounts, time),
164
160
  lastApproved: time,
@@ -1 +1 @@
1
- {"version":3,"file":"PermissionLogController.mjs","sourceRoot":"","sources":["../src/PermissionLogController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EACL,cAAc,EAGf,kCAAkC;AAGnC,OAAO,EAKL,WAAW,EACZ,wBAAwB;AAEzB,OAAO,EACL,kBAAkB,EAClB,SAAS,EACT,gBAAgB,EAChB,aAAa,EACb,YAAY,EACb,oBAAgB;AAyEjB,MAAM,YAAY,GAAiC;IACjD,iBAAiB,EAAE,EAAE;IACrB,qBAAqB,EAAE,EAAE;CAC1B,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J,CAAC,CAAC,CAAC;AACL,CAAC,uGAQoB,OAAiC;IACpD,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IACnC,IAAI,MAAM,KAAK,qBAAqB,EAAE,CAAC;QACrC,OAAO,CAAC,cAAc,CAAC,CAAC;IAC1B,CAAC;SAAM,IACL,MAAM,KAAK,GAAG,aAAa,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,CAAC;QACD,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IAChC,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC,mHAW0B,UAAsB;IAC/C,IAAI,UAAU,CAAC,gBAAgB,KAAK,cAAc,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;QAC1E,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAU,CAAC;IACnC,KAAK,MAAM,MAAM,IAAI,UAAU,CAAC,OAAO,EAAE,CAAC;QACxC,IACE,MAAM,CAAC,IAAI,KAAK,YAAY,CAAC,wBAAwB;YACrD,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,EAC3B,CAAC;YACD,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;gBACjC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YACtB,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,CAAC,GAAG,QAAQ,CAAC,CAAC;AACvB,CAAC","sourcesContent":["import {\n BaseController,\n type ControllerGetStateAction,\n type ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport type { Messenger } from '@metamask/messenger';\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 PermissionLogControllerGetStateAction = ControllerGetStateAction<\n typeof name,\n PermissionLogControllerState\n>;\n\nexport type PermissionLogControllerActions =\n PermissionLogControllerGetStateAction;\n\nexport type PermissionLogControllerStateChangeEvent =\n ControllerStateChangeEvent<typeof name, PermissionLogControllerState>;\n\nexport type PermissionLogControllerEvents =\n PermissionLogControllerStateChangeEvent;\n\nexport type PermissionLogControllerMessenger = Messenger<\n typeof name,\n PermissionLogControllerActions,\n PermissionLogControllerEvents\n>;\n\nconst defaultState: PermissionLogControllerState = {\n permissionHistory: {},\n permissionActivityLog: [],\n};\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 includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n permissionActivityLog: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: 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 // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\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,\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 // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\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"]}
1
+ {"version":3,"file":"PermissionLogController.mjs","sourceRoot":"","sources":["../src/PermissionLogController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EACL,cAAc,EAGf,kCAAkC;AAGnC,OAAO,EAKL,WAAW,EACZ,wBAAwB;AAEzB,OAAO,EACL,kBAAkB,EAClB,SAAS,EACT,gBAAgB,EAChB,aAAa,EACb,YAAY,EACb,oBAAgB;AA0EjB,MAAM,YAAY,GAAiC;IACjD,iBAAiB,EAAE,EAAE;IACrB,qBAAqB,EAAE,EAAE;CAC1B,CAAC;AAEF,MAAM,IAAI,GAAG,yBAAyB,CAAC;AAEvC;;;GAGG;AACH,MAAM,OAAO,uBAAwB,SAAQ,cAI5C;IAGC,YAAY,EACV,SAAS,EACT,iBAAiB,EACjB,KAAK,GAC0B;QAC/B,KAAK,CAAC;YACJ,SAAS;YACT,IAAI;YACJ,QAAQ,EAAE;gBACR,iBAAiB,EAAE;oBACjB,kBAAkB,EAAE,IAAI;oBACxB,OAAO,EAAE,IAAI;oBACb,sBAAsB,EAAE,KAAK;oBAC7B,QAAQ,EAAE,IAAI;iBACf;gBACD,qBAAqB,EAAE;oBACrB,kBAAkB,EAAE,IAAI;oBACxB,OAAO,EAAE,KAAK;oBACd,sBAAsB,EAAE,KAAK;oBAC7B,QAAQ,EAAE,KAAK;iBAChB;aACF;YACD,KAAK,EAAE,EAAE,GAAG,YAAY,EAAE,GAAG,KAAK,EAAE;SACrC,CAAC,CAAC;;QAzBI,6DAAgC;QA0BvC,uBAAA,IAAI,8CAAsB,iBAAiB,MAAA,CAAC;IAC9C,CAAC;IAED;;;;;;;;OAQG;IACH,qBAAqB,CAAC,MAAc,EAAE,QAAkB;QACtD,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,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AO,EAAE,CAAC;QACxC,IACE,MAAM,CAAC,IAAI,KAAK,YAAY,CAAC,wBAAwB;YACrD,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,EAC3B,CAAC;YACD,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;gBACjC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YACtB,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,CAAC,GAAG,QAAQ,CAAC,CAAC;AACvB,CAAC","sourcesContent":["import {\n BaseController,\n type ControllerGetStateAction,\n type ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport type { Messenger } from '@metamask/messenger';\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 *\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 PermissionLogControllerGetStateAction = ControllerGetStateAction<\n typeof name,\n PermissionLogControllerState\n>;\n\nexport type PermissionLogControllerActions =\n PermissionLogControllerGetStateAction;\n\nexport type PermissionLogControllerStateChangeEvent =\n ControllerStateChangeEvent<typeof name, PermissionLogControllerState>;\n\nexport type PermissionLogControllerEvents =\n PermissionLogControllerStateChangeEvent;\n\nexport type PermissionLogControllerMessenger = Messenger<\n typeof name,\n PermissionLogControllerActions,\n PermissionLogControllerEvents\n>;\n\nconst defaultState: PermissionLogControllerState = {\n permissionHistory: {},\n permissionActivityLog: [],\n};\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 readonly #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 includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n permissionActivityLog: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: 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,\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.cjs CHANGED
@@ -9,15 +9,9 @@ exports.LOG_IGNORE_METHODS = [
9
9
  'wallet_registerOnboarding',
10
10
  'wallet_watchAsset',
11
11
  ];
12
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
13
- // eslint-disable-next-line @typescript-eslint/naming-convention
14
12
  var LOG_METHOD_TYPES;
15
13
  (function (LOG_METHOD_TYPES) {
16
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
17
- // eslint-disable-next-line @typescript-eslint/naming-convention
18
14
  LOG_METHOD_TYPES["restricted"] = "restricted";
19
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
20
- // eslint-disable-next-line @typescript-eslint/naming-convention
21
15
  LOG_METHOD_TYPES["internal"] = "internal";
22
16
  })(LOG_METHOD_TYPES || (exports.LOG_METHOD_TYPES = LOG_METHOD_TYPES = {}));
23
17
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"enums.cjs","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,gFAAgF;AAChF,gEAAgE;AAChE,IAAY,gBAOX;AAPD,WAAY,gBAAgB;IAC1B,gFAAgF;IAChF,gEAAgE;IAChE,6CAAyB,CAAA;IACzB,gFAAgF;IAChF,gEAAgE;IAChE,yCAAqB,CAAA;AACvB,CAAC,EAPW,gBAAgB,gCAAhB,gBAAgB,QAO3B;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\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport enum LOG_METHOD_TYPES {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n restricted = 'restricted',\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n internal = 'internal',\n}\n\n/**\n * The permission activity log size limit.\n */\nexport const LOG_LIMIT = 100;\n"]}
1
+ {"version":3,"file":"enums.cjs","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,gCAAhB,gBAAgB,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"]}
@@ -1 +1 @@
1
- {"version":3,"file":"enums.d.cts","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;AAIF,oBAAY,gBAAgB;IAG1B,UAAU,eAAe;IAGzB,QAAQ,aAAa;CACtB;AAED;;GAEG;AACH,eAAO,MAAM,SAAS,MAAM,CAAC"}
1
+ {"version":3,"file":"enums.d.cts","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"}
@@ -1 +1 @@
1
- {"version":3,"file":"enums.d.mts","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;AAIF,oBAAY,gBAAgB;IAG1B,UAAU,eAAe;IAGzB,QAAQ,aAAa;CACtB;AAED;;GAEG;AACH,eAAO,MAAM,SAAS,MAAM,CAAC"}
1
+ {"version":3,"file":"enums.d.mts","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.mjs CHANGED
@@ -6,15 +6,9 @@ export const LOG_IGNORE_METHODS = [
6
6
  'wallet_registerOnboarding',
7
7
  'wallet_watchAsset',
8
8
  ];
9
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
10
- // eslint-disable-next-line @typescript-eslint/naming-convention
11
9
  export var LOG_METHOD_TYPES;
12
10
  (function (LOG_METHOD_TYPES) {
13
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
14
- // eslint-disable-next-line @typescript-eslint/naming-convention
15
11
  LOG_METHOD_TYPES["restricted"] = "restricted";
16
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
17
- // eslint-disable-next-line @typescript-eslint/naming-convention
18
12
  LOG_METHOD_TYPES["internal"] = "internal";
19
13
  })(LOG_METHOD_TYPES || (LOG_METHOD_TYPES = {}));
20
14
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"enums.mjs","sourceRoot":"","sources":["../src/enums.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,aAAa,GAAG,SAAS,CAAC;AAEvC,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC;IACxC,wBAAwB,EAAE,0BAAmC;CAC9D,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAChC,2BAA2B;IAC3B,mBAAmB;CACpB,CAAC;AAEF,gFAAgF;AAChF,gEAAgE;AAChE,MAAM,CAAN,IAAY,gBAOX;AAPD,WAAY,gBAAgB;IAC1B,gFAAgF;IAChF,gEAAgE;IAChE,6CAAyB,CAAA;IACzB,gFAAgF;IAChF,gEAAgE;IAChE,yCAAqB,CAAA;AACvB,CAAC,EAPW,gBAAgB,KAAhB,gBAAgB,QAO3B;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,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\n// TODO: Either fix this lint violation or explain why it's necessary to ignore.\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport enum LOG_METHOD_TYPES {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n restricted = 'restricted',\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n internal = 'internal',\n}\n\n/**\n * The permission activity log size limit.\n */\nexport const LOG_LIMIT = 100;\n"]}
1
+ {"version":3,"file":"enums.mjs","sourceRoot":"","sources":["../src/enums.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,aAAa,GAAG,SAAS,CAAC;AAEvC,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC;IACxC,wBAAwB,EAAE,0BAAmC;CAC9D,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAChC,2BAA2B;IAC3B,mBAAmB;CACpB,CAAC;AAEF,MAAM,CAAN,IAAY,gBAGX;AAHD,WAAY,gBAAgB;IAC1B,6CAAyB,CAAA;IACzB,yCAAqB,CAAA;AACvB,CAAC,EAHW,gBAAgB,KAAhB,gBAAgB,QAG3B;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/permission-log-controller",
3
- "version": "5.0.0-preview-ba016213",
3
+ "version": "5.0.0-preview-29587976",
4
4
  "description": "Controller with middleware for logging requests and responses to restricted and permissions-related methods",
5
5
  "keywords": [
6
6
  "MetaMask",