UNPKG

@metamask/snaps-rpc-methods

Version:
73 lines 2.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.clearStateHandler = void 0; const rpc_errors_1 = require("@metamask/rpc-errors"); const superstruct_1 = require("@metamask/superstruct"); const manageState_1 = require("../restricted/manageState.cjs"); const hookNames = { clearSnapState: true, hasPermission: true, }; /** * `snap_clearState` clears the state of the Snap. */ exports.clearStateHandler = { methodNames: ['snap_clearState'], implementation: clearStateImplementation, hookNames, }; const ClearStateParametersStruct = (0, superstruct_1.object)({ encrypted: (0, superstruct_1.optional)((0, superstruct_1.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(manageState_1.manageStateBuilder.targetName)) { return end(rpc_errors_1.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 (0, superstruct_1.create)(params, ClearStateParametersStruct); } catch (error) { if (error instanceof superstruct_1.StructError) { throw rpc_errors_1.rpcErrors.invalidParams({ message: `Invalid params: ${error.message}.`, }); } /* istanbul ignore next */ throw rpc_errors_1.rpcErrors.internal(); } } //# sourceMappingURL=clearState.cjs.map