@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
1 lines • 4.08 kB
Source Map (JSON)
{"version":3,"file":"getClientStatus.mjs","sourceRoot":"","sources":["../../src/permitted/getClientStatus.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,kBAAkB,EAAE,8BAA8B;AAU3D,MAAM,SAAS,GAAkD;IAC/D,WAAW,EAAE,IAAI;IACjB,UAAU,EAAE,IAAI;CACjB,CAAC;AAgBF;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG;IACpC,cAAc,EAAE,6BAA6B;IAC7C,SAAS;IACT,WAAW,EAAE,CAAC,4BAA4B,CAAC;CAM5C,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,KAAK,UAAU,6BAA6B,CAC1C,QAAwB,EACxB,QAAuD,EACvD,KAAc,EACd,GAA6B,EAC7B,EAAE,WAAW,EAAE,UAAU,EAA8B,EACvD,SAA0D;IAE1D,MAAM,EAAE,UAAU,EAAE,GAAG,SAAS,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAAC;IAEpE,QAAQ,CAAC,MAAM,GAAG;QAChB,MAAM,EAAE,CAAC,UAAU;QACnB,MAAM,EAAE,WAAW,EAAE;QACrB,aAAa,EAAE,UAAU,EAAE;QAC3B,eAAe,EAAE,kBAAkB,EAAE;KACtC,CAAC;IACF,OAAO,GAAG,EAAE,CAAC;AACf,CAAC","sourcesContent":["import type {\n JsonRpcEngineEndCallback,\n MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport type { Messenger } from '@metamask/messenger';\nimport type { GetClientStatusResult } from '@metamask/snaps-sdk';\nimport { getPlatformVersion } from '@metamask/snaps-utils';\nimport type {\n JsonRpcParams,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\n\nimport type { KeyringControllerGetStateAction } from '../types';\nimport type { MethodHooksObject } from '../utils';\n\nconst hookNames: MethodHooksObject<GetClientStatusMethodHooks> = {\n getIsActive: true,\n getVersion: true,\n};\n\nexport type GetClientStatusMethodHooks = {\n /**\n * @returns Whether the client is active or not.\n */\n getIsActive: () => boolean;\n\n /**\n * @returns The version string for the client.\n */\n getVersion: () => string;\n};\n\nexport type GetClientStatusMethodActions = KeyringControllerGetStateAction;\n\n/**\n * Get the status of the client running the Snap.\n *\n * @example\n * ```ts\n * import type { OnCronjobHandler } from '@metamask/snaps-sdk'\n * import { MethodNotFoundError } from '@metamask/snaps-sdk'\n *\n * export const onCronjob: OnCronjobHandler = async ({ request }) => {\n * switch (request.method) {\n * case 'execute':\n * // Find out if MetaMask is locked.\n * const { locked } = await snap.request({\n * method: 'snap_getClientStatus',\n * })\n *\n * if (!locked) {\n * // Do something that requires MetaMask to be unlocked, such as\n * // accessing the encrypted state.\n * }\n *\n * default:\n * throw new MethodNotFoundError()\n * }\n * }\n * ```\n */\nexport const getClientStatusHandler = {\n implementation: getClientStatusImplementation,\n hookNames,\n actionNames: ['KeyringController:getState'],\n} satisfies MethodHandler<\n GetClientStatusMethodHooks,\n GetClientStatusMethodActions,\n JsonRpcParams,\n GetClientStatusResult\n>;\n\n/**\n * The `snap_getClientStatus` method implementation.\n * Returns useful information about the client running the snap.\n *\n * @param _request - The JSON-RPC request object. Not used by this function.\n * @param response - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this\n * function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param hooks - The RPC method hooks.\n * @param hooks.getIsActive - A function that returns whether the client is opened or not.\n * @param hooks.getVersion - A function that returns the client version.\n * @param messenger - The messenger used to call controller actions.\n * @returns Nothing.\n */\nasync function getClientStatusImplementation(\n _request: JsonRpcRequest,\n response: PendingJsonRpcResponse<GetClientStatusResult>,\n _next: unknown,\n end: JsonRpcEngineEndCallback,\n { getIsActive, getVersion }: GetClientStatusMethodHooks,\n messenger: Messenger<string, GetClientStatusMethodActions>,\n): Promise<void> {\n const { isUnlocked } = messenger.call('KeyringController:getState');\n\n response.result = {\n locked: !isUnlocked,\n active: getIsActive(),\n clientVersion: getVersion(),\n platformVersion: getPlatformVersion(),\n };\n return end();\n}\n"]}