Skip to content

Commit

Permalink
Add snap_getState, snap_setState, snap_clearState methods (#2916)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz authored and PatrykLucka committed Jan 13, 2025
1 parent 0668ba5 commit d503d91
Show file tree
Hide file tree
Showing 41 changed files with 2,114 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "REliam7FRJGwKCI4NaC2G3mBSD5iYR7DCEhrXLcBDqA=",
"shasum": "82KbG3cf0wtxooJpWzHeM1g4FhO8O7zSYCAAGNPshfM=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/browserify/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "NItYOhAaWlS9q2r59/zlpoyVUyxojfsc5xMh65mLIwQ=",
"shasum": "5LsB950haZGnl0q5K7M4XgSh5J2e0p5O1Ptl/e6kpSQ=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
3 changes: 2 additions & 1 deletion packages/examples/packages/manage-state/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@metamask/snaps-sdk": "workspace:^"
"@metamask/snaps-sdk": "workspace:^",
"@metamask/utils": "^10.0.0"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/manage-state/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "3jbTBm2Gtm5+qWdWgNR2sgwEGwWmKsGK7QPeXN9yOpE=",
"shasum": "fIXije73reQctVWFkOL9kdLWns7uDs7UWbPPL1J0f2o=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
36 changes: 18 additions & 18 deletions packages/examples/packages/manage-state/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ describe('onRpcRequest', () => {
});
});

describe('setState', () => {
describe('legacy_setState', () => {
it('sets the state to the params', async () => {
const { request } = await installSnap();

expect(
await request({
method: 'setState',
method: 'legacy_setState',
params: {
items: ['foo'],
},
Expand All @@ -35,7 +35,7 @@ describe('onRpcRequest', () => {

expect(
await request({
method: 'getState',
method: 'legacy_getState',
}),
).toRespondWith({
items: ['foo'],
Expand All @@ -47,7 +47,7 @@ describe('onRpcRequest', () => {

expect(
await request({
method: 'setState',
method: 'legacy_setState',
params: {
items: ['foo'],
encrypted: false,
Expand All @@ -57,15 +57,15 @@ describe('onRpcRequest', () => {

expect(
await request({
method: 'getState',
method: 'legacy_getState',
}),
).toRespondWith({
items: [],
});

expect(
await request({
method: 'getState',
method: 'legacy_getState',
params: {
encrypted: false,
},
Expand All @@ -76,12 +76,12 @@ describe('onRpcRequest', () => {
});
});

describe('getState', () => {
describe('legacy_getState', () => {
it('returns the state if no state has been set', async () => {
const { request } = await installSnap();

const response = await request({
method: 'getState',
method: 'legacy_getState',
});

expect(response).toRespondWith({
Expand All @@ -93,14 +93,14 @@ describe('onRpcRequest', () => {
const { request } = await installSnap();

await request({
method: 'setState',
method: 'legacy_setState',
params: {
items: ['foo'],
},
});

const response = await request({
method: 'getState',
method: 'legacy_getState',
});

expect(response).toRespondWith({
Expand All @@ -118,7 +118,7 @@ describe('onRpcRequest', () => {
});

const response = await request({
method: 'getState',
method: 'legacy_getState',
params: {
encrypted: false,
},
Expand All @@ -130,26 +130,26 @@ describe('onRpcRequest', () => {
});
});

describe('clearState', () => {
describe('legacy_clearState', () => {
it('clears the state', async () => {
const { request } = await installSnap();

await request({
method: 'setState',
method: 'legacy_setState',
params: {
items: ['foo'],
},
});

expect(
await request({
method: 'clearState',
method: 'legacy_clearState',
}),
).toRespondWith(true);

expect(
await request({
method: 'getState',
method: 'legacy_getState',
}),
).toRespondWith({
items: [],
Expand All @@ -160,7 +160,7 @@ describe('onRpcRequest', () => {
const { request } = await installSnap();

await request({
method: 'setState',
method: 'legacy_setState',
params: {
items: ['foo'],
encrypted: false,
Expand All @@ -169,7 +169,7 @@ describe('onRpcRequest', () => {

expect(
await request({
method: 'clearState',
method: 'legacy_clearState',
params: {
encrypted: false,
},
Expand All @@ -178,7 +178,7 @@ describe('onRpcRequest', () => {

expect(
await request({
method: 'getState',
method: 'legacy_getState',
params: {
encrypted: false,
},
Expand Down
49 changes: 44 additions & 5 deletions packages/examples/packages/manage-state/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import {
type OnRpcRequestHandler,
} from '@metamask/snaps-sdk';

import type { BaseParams, SetStateParams } from './types';
import type {
BaseParams,
LegacyParams,
LegacySetStateParams,
SetStateParams,
} from './types';
import { clearState, getState, setState } from './utils';

/**
Expand Down Expand Up @@ -34,21 +39,55 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
switch (request.method) {
case 'setState': {
const params = request.params as SetStateParams;
return await snap.request({
method: 'snap_setState',
params: {
key: params?.key,
value: params?.value,
encrypted: params?.encrypted,
},
});
}

case 'getState': {
const params = request.params as BaseParams;
return await snap.request({
method: 'snap_getState',
params: {
key: params?.key,
encrypted: params?.encrypted,
},
});
}

case 'clearState': {
const params = request.params as BaseParams;
return await snap.request({
method: 'snap_clearState',
params: {
encrypted: params?.encrypted,
},
});
}

case 'legacy_setState': {
const params = request.params as LegacySetStateParams;
if (params.items) {
await setState({ items: params.items }, params.encrypted);
}

return true;
}

case 'getState': {
const params = request.params as BaseParams;
case 'legacy_getState': {
const params = request.params as LegacyParams;
return await getState(params?.encrypted);
}

case 'clearState': {
const params = request.params as BaseParams;
case 'legacy_clearState': {
const params = request.params as LegacyParams;
await clearState(params?.encrypted);

return true;
}

Expand Down
17 changes: 13 additions & 4 deletions packages/examples/packages/manage-state/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import type { Json } from '@metamask/utils';

import type { State } from './utils';

export type BaseParams = { encrypted?: boolean };
export type LegacyParams = { encrypted?: boolean };

export type BaseParams = { key?: string; encrypted?: boolean };

/**
* The parameters for the `setState` JSON-RPC method.
*
* The current state will be merged with the new state.
*/
export type SetStateParams = BaseParams & Partial<State>;
export type SetStateParams = BaseParams & {
value: Json;
};

/**
* The parameters for the `legacy_setState` JSON-RPC method.
*/
export type LegacySetStateParams = LegacyParams & Partial<State>;
8 changes: 4 additions & 4 deletions packages/snaps-rpc-methods/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ module.exports = deepmerge(baseConfig, {
],
coverageThreshold: {
global: {
branches: 92.94,
functions: 97.29,
lines: 97.88,
statements: 97.41,
branches: 93.33,
functions: 97.46,
lines: 98.03,
statements: 97.61,
},
},
});
Loading

0 comments on commit d503d91

Please sign in to comment.