UNPKG

@metamask/snaps-rpc-methods

Version:
78 lines 2.57 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"); /** * Clear the entire state of the Snap. * * @example * ```ts * await snap.request({ * method: 'snap_clearState', * params: { * encrypted: true, // Optional, defaults to true * }, * }); * ``` */ exports.clearStateHandler = { implementation: clearStateImplementation, actionNames: [ 'PermissionController:hasPermission', 'SnapController:clearSnapState', ], }; 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. Not used by this function. * @param messenger - The messenger used to call controller actions. * @returns Nothing. */ async function clearStateImplementation(request, response, _next, end, _hooks, messenger) { const { params, origin } = request; if (!messenger.call('PermissionController:hasPermission', origin, manageState_1.manageStateBuilder.targetName)) { return end(rpc_errors_1.providerErrors.unauthorized()); } try { const validatedParams = getValidatedParams(params); const { encrypted = true } = validatedParams; messenger.call('SnapController:clearSnapState', origin, 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