Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add snap_getState, snap_setState, snap_clearState methods #2916

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
40 changes: 37 additions & 3 deletions packages/examples/packages/manage-state/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type OnRpcRequestHandler,
} from '@metamask/snaps-sdk';

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

/**
Expand Down Expand Up @@ -34,21 +34,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': {
case 'legacy_getState': {
const params = request.params as BaseParams;
return await getState(params?.encrypted);
}

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

return true;
}

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

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

export type BaseParams = { 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 = BaseParams & Partial<State>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key isn't allowed in the legacy RPC method is it?

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.26,
lines: 97.87,
statements: 97.39,
branches: 93.3,
functions: 97.42,
lines: 98.01,
statements: 97.58,
},
},
});
Loading
Loading