Skip to main content

SnapController Actions

26 actions registered.

NameDeprecated
clearSnapState
clearState
disableSnap
disconnectOrigin
enableSnap
getAllSnaps
getPermittedSnaps
getRunnableSnaps
getSnap
getSnapFile
getSnapState
getState
handleRequest
hasSnap
init
installSnaps
isMinimumPlatformVersion
isSnapRunning
removeSnap
removeSnaps
revokeDynamicSnapPermissions
setClientActive
stopAllSnaps
stopSnap
updateRegistry
updateSnapState

SnapController:clearSnapState​

Package: @metamask/snaps-controllers

Clears the state of the snap with the given id. This is distinct from the state MetaMask uses to manage snaps.

Parameters:

NameDescription
snapIdThe id of the Snap whose state should be cleared.
encryptedA flag to indicate whether to use encrypted storage or not.

Handler signature:

(snapId: SnapId, encrypted: boolean) => void

SnapController:clearState​

Package: @metamask/snaps-controllers

Completely clear the controller's state: delete all associated data, handlers, event listeners, and permissions; tear down all snap providers. Also re-initializes the controller after clearing the state.

Handler signature:

() => Promise<void>

SnapController:disableSnap​

Package: @metamask/snaps-controllers

Disables the given snap. A snap can only be started if it is enabled.

Parameters:

NameDescription
snapIdThe id of the Snap to disable.

Returns: A promise that resolves once the snap has been disabled.

Handler signature:

(snapId: SnapId) => Promise<void>

SnapController:disconnectOrigin​

Package: @metamask/snaps-controllers

Disconnect the Snap from the given origin, meaning the origin can no longer interact with the Snap until it is reconnected.

Parameters:

NameDescription
originThe origin from which to remove the Snap.
snapIdThe id of the snap to remove.

Handler signature:

(origin: string, snapId: SnapId) => void

SnapController:enableSnap​

Package: @metamask/snaps-controllers

Enables the given snap. A snap can only be started if it is enabled. A snap can only be enabled if it isn't blocked.

Parameters:

NameDescription
snapIdThe id of the Snap to enable.

Handler signature:

(snapId: SnapId) => void

SnapController:getAllSnaps​

Package: @metamask/snaps-controllers

Gets all snaps in their truncated format.

Returns: All installed snaps in their truncated format.

Handler signature:

() => TruncatedSnap[]

SnapController:getPermittedSnaps​

Package: @metamask/snaps-controllers

Gets the serialized permitted snaps of the given origin, if any.

Parameters:

NameDescription
originThe origin whose permitted snaps to retrieve.

Returns: The serialized permitted snaps for the origin.

Handler signature:

(origin: string) => RequestSnapsResult

SnapController:getRunnableSnaps​

Package: @metamask/snaps-controllers

Gets all runnable snaps.

Returns: All runnable snaps.

Handler signature:

() => TruncatedSnap[]

SnapController:getSnap​

Package: @metamask/snaps-controllers

Gets the snap with the given id if it exists, including all data. This should not be used if the snap is to be serializable, as e.g. the snap sourceCode may be quite large.

Parameters:

NameDescription
snapIdThe id of the Snap to get.

Returns: The entire snap object from the controller state.

Handler signature:

(snapId: string) => Snap | null

SnapController:getSnapFile​

Package: @metamask/snaps-controllers

Gets a static auxiliary snap file in a chosen file encoding.

Parameters:

NameDescription
snapIdThe id of the Snap whose state to get.
pathThe path to the requested file.
encodingAn optional requested file encoding.

Returns: The file requested in the chosen file encoding or null if the file is not found.

Handler signature:

(snapId: SnapId, path: string, encoding?: AuxiliaryFileEncoding) => Promise<string | null>

SnapController:getSnapState​

Package: @metamask/snaps-controllers

Gets the own state of the snap with the given id. This is distinct from the state MetaMask uses to manage snaps.

Parameters:

NameDescription
snapIdThe id of the Snap whose state to get.
encryptedA flag to indicate whether to use encrypted storage or not.

Returns: The requested snap state or null if no state exists.

Handler signature:

(snapId: SnapId, encrypted: boolean) => Promise<Json>

SnapController:getState​

Package: @metamask/snaps-controllers

Handler signature:

() => SnapControllerState

SnapController:handleRequest​

Package: @metamask/snaps-controllers

Passes a JSON-RPC request object to the RPC handler function of a snap.

Parameters:

NameDescription
optionsA bag of options.
options.snapIdThe ID of the recipient snap.
options.originThe origin of the RPC request.
options.originMetadataOptional additional origin metadata for the RPC request.
options.handlerThe handler to trigger on the snap for the request.
options.requestThe JSON-RPC request object.

Returns: The result of the JSON-RPC request.

Handler signature:

({ snapId, origin, originMetadata, handler: handlerType, request: rawRequest, }: SnapRpcHookArgs & {
snapId: SnapId;
}) => Promise<unknown>

SnapController:hasSnap​

Package: @metamask/snaps-controllers

Returns whether the given snap has been added to state.

Parameters:

NameDescription
snapIdThe id of the Snap to check for.

Returns: true if the snap exists in the controller state, otherwise false.

Handler signature:

(snapId: SnapId) => boolean

SnapController:init​

Package: @metamask/snaps-controllers

Initialise the SnapController.

Currently this method sets up the controller and calls the onStart lifecycle hook for all runnable Snaps.

Parameters:

NameDescription
waitForPlatformWhether to wait for the platform to be ready before returning.

Handler signature:

(waitForPlatform?: boolean) => Promise<void>

SnapController:installSnaps​

Package: @metamask/snaps-controllers

Installs the snaps requested by the given origin, returning the snap object if the origin is permitted to install it, and an authorization error otherwise.

Parameters:

NameDescription
originThe origin that requested to install the snaps.
requestedSnapsThe snaps to install.

Returns: An object of snap ids and snap objects, or errors if a snap couldn't be installed.

Handler signature:

(origin: string, requestedSnaps: RequestSnapsParams) => Promise<RequestSnapsResult>

SnapController:isMinimumPlatformVersion​

Package: @metamask/snaps-controllers

Determine if a given Snap ID supports a given minimum version of the Snaps platform by inspecting the platformVersion in the Snap manifest.

Parameters:

NameDescription
snapIdThe Snap ID.
versionThe version.

Returns: True if the platform version is equal or greater to the passed version, false otherwise.

Handler signature:

(snapId: SnapId, version: SemVerVersion) => boolean

SnapController:isSnapRunning​

Package: @metamask/snaps-controllers

Returns whether the given snap is running. Throws an error if the snap doesn't exist.

Parameters:

NameDescription
snapIdThe id of the Snap to check.

Returns: true if the snap is running, otherwise false.

Handler signature:

(snapId: SnapId) => boolean

SnapController:removeSnap​

Package: @metamask/snaps-controllers

Removes the given snap from state, and clears all associated handlers and listeners.

Parameters:

NameDescription
snapIdThe id of the Snap.

Returns: A promise that resolves once the snap has been removed.

Handler signature:

(snapId: SnapId) => Promise<void>

SnapController:removeSnaps​

Package: @metamask/snaps-controllers

Stops the given snaps, removes them from state, and clears all associated permissions, handlers, and listeners.

Parameters:

NameDescription
snapIdsThe ids of the Snaps.

Handler signature:

(snapIds: SnapId[]) => Promise<void>

SnapController:revokeDynamicSnapPermissions​

Package: @metamask/snaps-controllers

Checks if a list of permissions are dynamic and allowed to be revoked, if they are they will all be revoked.

Parameters:

NameDescription
snapIdThe snap ID.
permissionNamesThe names of the permissions.

Handler signature:

(snapId: string, permissionNames: NonEmptyArray<string>) => void

SnapController:setClientActive​

Package: @metamask/snaps-controllers

Set the active state of the client. This will trigger the onActive or onInactive lifecycle hooks for all Snaps.

Parameters:

NameDescription
activeA boolean indicating whether the client is active or not.

Handler signature:

(active: boolean) => void

SnapController:stopAllSnaps​

Package: @metamask/snaps-controllers

Stops all running snaps, removes all hooks, closes all connections, and terminates their workers.

Parameters:

NameDescription
statusEventThe Snap status event that caused the snap to be stopped.

Handler signature:

(statusEvent?: SnapStatusEvents.Stop | SnapStatusEvents.Crash) => Promise<void>

SnapController:stopSnap​

Package: @metamask/snaps-controllers

Stops the given snap, removes all hooks, closes all connections, and terminates its worker.

Parameters:

NameDescription
snapIdThe id of the Snap to stop.
statusEventThe Snap status event that caused the snap to be stopped.

Handler signature:

(snapId: SnapId, statusEvent?: SnapStatusEvents.Stop | SnapStatusEvents.Crash) => Promise<void>

SnapController:updateRegistry​

Package: @metamask/snaps-controllers

Trigger an update of the registry.

As a side-effect, this will always check if preinstalled Snaps can be updated and whether any Snaps need to be blocked/unblocked.

Handler signature:

() => Promise<void>

SnapController:updateSnapState​

Package: @metamask/snaps-controllers

Updates the own state of the snap with the given id. This is distinct from the state MetaMask uses to manage snaps.

Parameters:

NameDescription
snapIdThe id of the Snap whose state should be updated.
newSnapStateThe new state of the snap.
encryptedA flag to indicate whether to use encrypted storage or not.

Handler signature:

(snapId: SnapId, newSnapState: Record<string, Json>, encrypted: boolean) => Promise<void>