@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
66 lines • 2.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClientStatusHandler = void 0;
const snaps_utils_1 = require("@metamask/snaps-utils");
const hookNames = {
getIsActive: true,
getVersion: true,
};
/**
* Get the status of the client running the Snap.
*
* @example
* ```ts
* import type { OnCronjobHandler } from '@metamask/snaps-sdk'
* import { MethodNotFoundError } from '@metamask/snaps-sdk'
*
* export const onCronjob: OnCronjobHandler = async ({ request }) => {
* switch (request.method) {
* case 'execute':
* // Find out if MetaMask is locked.
* const { locked } = await snap.request({
* method: 'snap_getClientStatus',
* })
*
* if (!locked) {
* // Do something that requires MetaMask to be unlocked, such as
* // accessing the encrypted state.
* }
*
* default:
* throw new MethodNotFoundError()
* }
* }
* ```
*/
exports.getClientStatusHandler = {
implementation: getClientStatusImplementation,
hookNames,
actionNames: ['KeyringController:getState'],
};
/**
* The `snap_getClientStatus` method implementation.
* Returns useful information about the client running the snap.
*
* @param _request - The JSON-RPC request object. Not used by this function.
* @param response - The JSON-RPC response object.
* @param _next - The `json-rpc-engine` "next" callback. Not used by this
* function.
* @param end - The `json-rpc-engine` "end" callback.
* @param hooks - The RPC method hooks.
* @param hooks.getIsActive - A function that returns whether the client is opened or not.
* @param hooks.getVersion - A function that returns the client version.
* @param messenger - The messenger used to call controller actions.
* @returns Nothing.
*/
async function getClientStatusImplementation(_request, response, _next, end, { getIsActive, getVersion }, messenger) {
const { isUnlocked } = messenger.call('KeyringController:getState');
response.result = {
locked: !isUnlocked,
active: getIsActive(),
clientVersion: getVersion(),
platformVersion: (0, snaps_utils_1.getPlatformVersion)(),
};
return end();
}
//# sourceMappingURL=getClientStatus.cjs.map