UNPKG

@metamask/snaps-rpc-methods

Version:
70 lines 2.23 kB
import { providerErrors, rpcErrors } from "@metamask/rpc-errors"; import { boolean, create, object, optional, StructError } from "@metamask/superstruct"; import { manageStateBuilder } from "../restricted/manageState.mjs"; const hookNames = { clearSnapState: true, hasPermission: true, }; /** * `snap_clearState` clears the state of the Snap. */ export const clearStateHandler = { methodNames: ['snap_clearState'], implementation: clearStateImplementation, hookNames, }; const ClearStateParametersStruct = object({ encrypted: optional(boolean()), }); /** * The `snap_clearState` method implementation. * * @param request - The JSON-RPC request object. * @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.clearSnapState - A function that clears the state of the * requesting Snap. * @param hooks.hasPermission - Check whether a given origin has a given * permission. * @returns Nothing. */ async function clearStateImplementation(request, response, _next, end, { clearSnapState, hasPermission }) { const { params } = request; if (!hasPermission(manageStateBuilder.targetName)) { return end(providerErrors.unauthorized()); } try { const validatedParams = getValidatedParams(params); const { encrypted = true } = validatedParams; clearSnapState(encrypted); response.result = null; } catch (error) { return end(error); } return end(); } /** * Validate the parameters of the `snap_clearState` method. * * @param params - The parameters to validate. * @returns The validated parameters. */ function getValidatedParams(params) { try { return create(params, ClearStateParametersStruct); } catch (error) { if (error instanceof StructError) { throw rpcErrors.invalidParams({ message: `Invalid params: ${error.message}.`, }); } /* istanbul ignore next */ throw rpcErrors.internal(); } } //# sourceMappingURL=clearState.mjs.map