Skip to content

Commit 1e16931

Browse files
authored
Add snap_getState, snap_setState, snap_clearState methods (#2916)
1 parent aa5f7a1 commit 1e16931

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2114
-64
lines changed

packages/examples/packages/browserify-plugin/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "REliam7FRJGwKCI4NaC2G3mBSD5iYR7DCEhrXLcBDqA=",
10+
"shasum": "82KbG3cf0wtxooJpWzHeM1g4FhO8O7zSYCAAGNPshfM=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/browserify/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "NItYOhAaWlS9q2r59/zlpoyVUyxojfsc5xMh65mLIwQ=",
10+
"shasum": "5LsB950haZGnl0q5K7M4XgSh5J2e0p5O1Ptl/e6kpSQ=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/manage-state/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"test:watch": "jest --watch"
4444
},
4545
"dependencies": {
46-
"@metamask/snaps-sdk": "workspace:^"
46+
"@metamask/snaps-sdk": "workspace:^",
47+
"@metamask/utils": "^10.0.0"
4748
},
4849
"devDependencies": {
4950
"@jest/globals": "^29.5.0",

packages/examples/packages/manage-state/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "3jbTBm2Gtm5+qWdWgNR2sgwEGwWmKsGK7QPeXN9yOpE=",
10+
"shasum": "fIXije73reQctVWFkOL9kdLWns7uDs7UWbPPL1J0f2o=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/manage-state/src/index.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ describe('onRpcRequest', () => {
2020
});
2121
});
2222

23-
describe('setState', () => {
23+
describe('legacy_setState', () => {
2424
it('sets the state to the params', async () => {
2525
const { request } = await installSnap();
2626

2727
expect(
2828
await request({
29-
method: 'setState',
29+
method: 'legacy_setState',
3030
params: {
3131
items: ['foo'],
3232
},
@@ -35,7 +35,7 @@ describe('onRpcRequest', () => {
3535

3636
expect(
3737
await request({
38-
method: 'getState',
38+
method: 'legacy_getState',
3939
}),
4040
).toRespondWith({
4141
items: ['foo'],
@@ -47,7 +47,7 @@ describe('onRpcRequest', () => {
4747

4848
expect(
4949
await request({
50-
method: 'setState',
50+
method: 'legacy_setState',
5151
params: {
5252
items: ['foo'],
5353
encrypted: false,
@@ -57,15 +57,15 @@ describe('onRpcRequest', () => {
5757

5858
expect(
5959
await request({
60-
method: 'getState',
60+
method: 'legacy_getState',
6161
}),
6262
).toRespondWith({
6363
items: [],
6464
});
6565

6666
expect(
6767
await request({
68-
method: 'getState',
68+
method: 'legacy_getState',
6969
params: {
7070
encrypted: false,
7171
},
@@ -76,12 +76,12 @@ describe('onRpcRequest', () => {
7676
});
7777
});
7878

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

8383
const response = await request({
84-
method: 'getState',
84+
method: 'legacy_getState',
8585
});
8686

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

9595
await request({
96-
method: 'setState',
96+
method: 'legacy_setState',
9797
params: {
9898
items: ['foo'],
9999
},
100100
});
101101

102102
const response = await request({
103-
method: 'getState',
103+
method: 'legacy_getState',
104104
});
105105

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

120120
const response = await request({
121-
method: 'getState',
121+
method: 'legacy_getState',
122122
params: {
123123
encrypted: false,
124124
},
@@ -130,26 +130,26 @@ describe('onRpcRequest', () => {
130130
});
131131
});
132132

133-
describe('clearState', () => {
133+
describe('legacy_clearState', () => {
134134
it('clears the state', async () => {
135135
const { request } = await installSnap();
136136

137137
await request({
138-
method: 'setState',
138+
method: 'legacy_setState',
139139
params: {
140140
items: ['foo'],
141141
},
142142
});
143143

144144
expect(
145145
await request({
146-
method: 'clearState',
146+
method: 'legacy_clearState',
147147
}),
148148
).toRespondWith(true);
149149

150150
expect(
151151
await request({
152-
method: 'getState',
152+
method: 'legacy_getState',
153153
}),
154154
).toRespondWith({
155155
items: [],
@@ -160,7 +160,7 @@ describe('onRpcRequest', () => {
160160
const { request } = await installSnap();
161161

162162
await request({
163-
method: 'setState',
163+
method: 'legacy_setState',
164164
params: {
165165
items: ['foo'],
166166
encrypted: false,
@@ -169,7 +169,7 @@ describe('onRpcRequest', () => {
169169

170170
expect(
171171
await request({
172-
method: 'clearState',
172+
method: 'legacy_clearState',
173173
params: {
174174
encrypted: false,
175175
},
@@ -178,7 +178,7 @@ describe('onRpcRequest', () => {
178178

179179
expect(
180180
await request({
181-
method: 'getState',
181+
method: 'legacy_getState',
182182
params: {
183183
encrypted: false,
184184
},

packages/examples/packages/manage-state/src/index.ts

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import {
33
type OnRpcRequestHandler,
44
} from '@metamask/snaps-sdk';
55

6-
import type { BaseParams, SetStateParams } from './types';
6+
import type {
7+
BaseParams,
8+
LegacyParams,
9+
LegacySetStateParams,
10+
SetStateParams,
11+
} from './types';
712
import { clearState, getState, setState } from './utils';
813

914
/**
@@ -34,21 +39,55 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
3439
switch (request.method) {
3540
case 'setState': {
3641
const params = request.params as SetStateParams;
42+
return await snap.request({
43+
method: 'snap_setState',
44+
params: {
45+
key: params?.key,
46+
value: params?.value,
47+
encrypted: params?.encrypted,
48+
},
49+
});
50+
}
51+
52+
case 'getState': {
53+
const params = request.params as BaseParams;
54+
return await snap.request({
55+
method: 'snap_getState',
56+
params: {
57+
key: params?.key,
58+
encrypted: params?.encrypted,
59+
},
60+
});
61+
}
3762

63+
case 'clearState': {
64+
const params = request.params as BaseParams;
65+
return await snap.request({
66+
method: 'snap_clearState',
67+
params: {
68+
encrypted: params?.encrypted,
69+
},
70+
});
71+
}
72+
73+
case 'legacy_setState': {
74+
const params = request.params as LegacySetStateParams;
3875
if (params.items) {
3976
await setState({ items: params.items }, params.encrypted);
4077
}
78+
4179
return true;
4280
}
4381

44-
case 'getState': {
45-
const params = request.params as BaseParams;
82+
case 'legacy_getState': {
83+
const params = request.params as LegacyParams;
4684
return await getState(params?.encrypted);
4785
}
4886

49-
case 'clearState': {
50-
const params = request.params as BaseParams;
87+
case 'legacy_clearState': {
88+
const params = request.params as LegacyParams;
5189
await clearState(params?.encrypted);
90+
5291
return true;
5392
}
5493

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
import type { Json } from '@metamask/utils';
2+
13
import type { State } from './utils';
24

3-
export type BaseParams = { encrypted?: boolean };
5+
export type LegacyParams = { encrypted?: boolean };
6+
7+
export type BaseParams = { key?: string; encrypted?: boolean };
48

59
/**
610
* The parameters for the `setState` JSON-RPC method.
7-
*
8-
* The current state will be merged with the new state.
911
*/
10-
export type SetStateParams = BaseParams & Partial<State>;
12+
export type SetStateParams = BaseParams & {
13+
value: Json;
14+
};
15+
16+
/**
17+
* The parameters for the `legacy_setState` JSON-RPC method.
18+
*/
19+
export type LegacySetStateParams = LegacyParams & Partial<State>;

packages/snaps-rpc-methods/jest.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ module.exports = deepmerge(baseConfig, {
1010
],
1111
coverageThreshold: {
1212
global: {
13-
branches: 92.94,
14-
functions: 97.29,
15-
lines: 97.88,
16-
statements: 97.41,
13+
branches: 93.33,
14+
functions: 97.46,
15+
lines: 98.03,
16+
statements: 97.61,
1717
},
1818
},
1919
});

0 commit comments

Comments
 (0)