snap_setState
Description
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).
Parameters
key
string | nullThe key of the state to update. If not provided, the entire
state is updated. This may contain Lodash-style path syntax, e.g.,
a.b.c, with the exception of array syntax.
value
JsonrequiredThe value to set the state to.
encrypted
boolean | nullWhether to use the separate encrypted state, or the unencrypted state. Defaults to the encrypted state. Encrypted state can only be used if the client is unlocked, while unencrypted state can be used whether the client is locked or unlocked.
Example
- Manifest
- Usage
{
"initialPermissions": {
"snap_manageState": {}
}
}
// 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`
},
});