@metamask/snaps-rpc-methods
Version:
MetaMask Snaps JSON-RPC method implementations
112 lines • 4.83 kB
text/typescript
import type { JsonRpcEngineEndCallback } from "@metamask/json-rpc-engine";
import type { Messenger } from "@metamask/messenger";
import type { PermissionControllerHasPermissionAction } from "@metamask/permission-controller";
import type { SetStateParams, SetStateResult } from "@metamask/snaps-sdk";
import type { JsonObject } from "@metamask/snaps-sdk/jsx";
import { type InferMatching } from "@metamask/snaps-utils";
import type { PendingJsonRpcResponse, Json } from "@metamask/utils";
import type { JsonRpcRequestWithOrigin, SnapControllerGetSnapAction, SnapControllerGetSnapStateAction, SnapControllerUpdateSnapStateAction } from "../types.mjs";
import type { MethodHooksObject } from "../utils.mjs";
export type SetStateMethodHooks = {
/**
* Wait for the extension to be unlocked.
*
* @returns A promise that resolves once the extension is unlocked.
*/
getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;
};
export type SetStateMethodActions = PermissionControllerHasPermissionAction | SnapControllerGetSnapStateAction | SnapControllerUpdateSnapStateAction | SnapControllerGetSnapAction;
/**
* Allow the Snap to persist up to 64 MB of data to disk and retrieve it at
* will. By default, the data is automatically encrypted using a Snap-specific
* key and automatically decrypted when retrieved. You can set `encrypted` to
* `false` to use unencrypted storage (available when the client is locked).
*
* If the key is `undefined`, the value is expected to be an object. In this
* case, the value is set as the new root state.
*
* If the key is not `undefined`, the value is set in the state at the key. If
* the key does not exist, it is created (and any missing intermediate keys are
* created as well).
*
* @example
* ```json name="Manifest"
* {
* "initialPermissions": {
* "snap_manageState": {}
* }
* }
* ```
* ```ts name="Usage"
* // Set the entire state:
* await snap.request({
* method: 'snap_setState',
* params: {
* value: {
* some: {
* nested: {
* value: 'Hello, world!',
* },
* },
* },
* encrypted: true, // Optional, defaults to `true`
* },
* });
*
* // Set a specific value within the state:
* await snap.request({
* method: 'snap_setState',
* params: {
* key: 'some.nested.value',
* value: 'Hello, world!',
* encrypted: true, // Optional, defaults to `true`
* },
* });
* ```
*/
export declare const setStateHandler: {
implementation: typeof setStateImplementation;
hookNames: MethodHooksObject<SetStateMethodHooks>;
actionNames: ("SnapController:getSnap" | "SnapController:getSnapState" | "SnapController:updateSnapState" | "PermissionController:hasPermission")[];
};
declare const SetStateParametersStruct: import("@metamask/superstruct").Struct<{
value: Json;
encrypted?: boolean | undefined;
key?: string | undefined;
}, {
key: import("@metamask/superstruct").Struct<string | undefined, null>;
value: import("@metamask/superstruct").Struct<Json, unknown>;
encrypted: import("@metamask/superstruct").Struct<boolean | undefined, null>;
}>;
export type SetStateParameters = InferMatching<typeof SetStateParametersStruct, SetStateParams>;
/**
* The `snap_setState` 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.getUnlockPromise - Wait for the extension to be unlocked.
* @param messenger - The messenger used to call controller actions.
* @returns Nothing.
*/
declare function setStateImplementation(request: JsonRpcRequestWithOrigin<SetStateParameters>, response: PendingJsonRpcResponse<SetStateResult>, _next: unknown, end: JsonRpcEngineEndCallback, { getUnlockPromise }: SetStateMethodHooks, messenger: Messenger<string, SetStateMethodActions>): Promise<void>;
/**
* Set 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, it is created (and any missing intermediate keys are created as
* well).
*
* This is a simplified version of Lodash's `set` function, but Lodash doesn't
* seem to be maintained anymore, so we're using our own implementation.
*
* @param object - The object to get the key from.
* @param key - The key to set.
* @param value - The value to set the key to.
* @returns The new object with the key set to the value.
*/
export declare function set(object: Record<string, Json> | null, key: string, value: Json): JsonObject;
export {};
//# sourceMappingURL=setState.d.mts.map