UNPKG

@metamask/snaps-rpc-methods

Version:
120 lines 4.2 kB
import { providerErrors, rpcErrors } from "@metamask/rpc-errors"; import { boolean, create, object, optional, StructError } from "@metamask/superstruct"; import { hasProperty, isObject } from "@metamask/utils"; import { manageStateBuilder } from "../restricted/manageState.mjs"; import { FORBIDDEN_KEYS, StateKeyStruct } from "../utils.mjs"; const hookNames = { hasPermission: true, getSnapState: true, getUnlockPromise: true, }; /** * `snap_getState` gets the state of the Snap. */ export const getStateHandler = { methodNames: ['snap_getState'], implementation: getStateImplementation, hookNames, }; const GetStateParametersStruct = object({ key: optional(StateKeyStruct), encrypted: optional(boolean()), }); /** * The `snap_getState` 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.hasPermission - Check whether a given origin has a given * permission. * @param hooks.getSnapState - Get the state of the requesting Snap. * @param hooks.getUnlockPromise - Wait for the extension to be unlocked. * @returns Nothing. */ async function getStateImplementation(request, // `GetStateResult` is an alias for `Json` (which is the default type argument // for `PendingJsonRpcResponse`), but that may not be the case in the future. // We use `GetStateResult` here to make it clear that this is the expected // type of the result. // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments response, _next, end, { hasPermission, getSnapState, getUnlockPromise }) { const { params } = request; if (!hasPermission(manageStateBuilder.targetName)) { return end(providerErrors.unauthorized()); } try { const validatedParams = getValidatedParams(params); const { key, encrypted = true } = validatedParams; if (encrypted) { await getUnlockPromise(true); } const state = await getSnapState(encrypted); response.result = get(state, key); } catch (error) { return end(error); } return end(); } /** * Validate the parameters of the `snap_getState` method. * * @param params - The parameters to validate. * @returns The validated parameters. */ function getValidatedParams(params) { try { return create(params, GetStateParametersStruct); } catch (error) { if (error instanceof StructError) { throw rpcErrors.invalidParams({ message: `Invalid params: ${error.message}.`, }); } /* istanbul ignore next */ throw rpcErrors.internal(); } } /** * Get the value of a key in an object. The key may contain Lodash-style path * syntax, e.g., `a.b.c` (with the exception of array syntax). If the key does * not exist, `null` is returned. * * This is a simplified version of Lodash's `get` function, but Lodash doesn't * seem to be maintained anymore, so we're using our own implementation. * * @param value - The object to get the key from. * @param key - The key to get. * @returns The value of the key in the object, or `null` if the key does not * exist. */ export function get(value, key) { if (key === undefined) { return value; } const keys = key.split('.'); let result = value; // Intentionally using a classic for loop here for performance reasons. // eslint-disable-next-line @typescript-eslint/prefer-for-of for (let i = 0; i < keys.length; i++) { const currentKey = keys[i]; if (FORBIDDEN_KEYS.includes(currentKey)) { throw rpcErrors.invalidParams('Invalid params: Key contains forbidden characters.'); } if (isObject(result)) { if (!hasProperty(result, currentKey)) { return null; } result = result[currentKey]; continue; } return null; } return result; } //# sourceMappingURL=getState.mjs.map