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

Merged
merged 31 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
16a30dc
Add `snap_getState`, `snap_setState`, `snap_clearState` methods
Mrtenz Nov 29, 2024
6000f18
Add missing dependency to manage state example
Mrtenz Nov 29, 2024
4a336c2
Fix example tests
Mrtenz Nov 29, 2024
d98f427
Throw for invalid keys
Mrtenz Dec 10, 2024
78658fc
Fix `@metamask/utils` version
Mrtenz Dec 10, 2024
4b1576a
Use `snap_manageState`'s target name instead of hardcoding permission…
Mrtenz Dec 10, 2024
978eeae
Move check for invalid state
Mrtenz Dec 10, 2024
9d71f21
Validate keys
Mrtenz Dec 10, 2024
b61509c
Add tests for new methods
Mrtenz Dec 10, 2024
f5b1c15
Update types in example
Mrtenz Dec 17, 2024
9144281
Add missing hook
Mrtenz Dec 17, 2024
549142a
Assume methods bound to origin
Mrtenz Dec 17, 2024
b5ac14e
Update packages/snaps-sdk/src/types/methods/get-state.ts
Mrtenz Dec 17, 2024
b324ad2
Update packages/snaps-sdk/src/types/methods/set-state.ts
Mrtenz Dec 17, 2024
6d1af36
Throw error for invalid branch
Mrtenz Dec 17, 2024
05ffe82
Use `isObject` for better performance
Mrtenz Dec 18, 2024
4b1f033
Use `isObject` for better performance in getState handler
Mrtenz Dec 18, 2024
9e3c1db
More small performance optimisations
Mrtenz Dec 18, 2024
ce07fbb
Update coverage
Mrtenz Dec 18, 2024
01c3b7e
Reuse some logic in test-snaps
Mrtenz Dec 18, 2024
bafff42
Add headings to test-snaps
Mrtenz Dec 18, 2024
aea7f62
Move forbidden keys to shared array
Mrtenz Dec 18, 2024
3ad1e63
Fix some minor issues
Mrtenz Dec 18, 2024
bf52606
Test calls to getSnapState hook
Mrtenz Dec 18, 2024
05929ba
Update packages/snaps-rpc-methods/src/permitted/getState.test.ts
Mrtenz Dec 18, 2024
b99cb20
Update packages/snaps-rpc-methods/src/permitted/setState.ts
Mrtenz Dec 18, 2024
d9c7cd8
Update packages/snaps-rpc-methods/src/permitted/setState.ts
Mrtenz Dec 18, 2024
0c8be2d
Don't allow overwriting non-objects
Mrtenz Dec 18, 2024
bc395c1
Remove unnecessary code
Mrtenz Dec 18, 2024
baa0090
Update packages/snaps-rpc-methods/src/permitted/setState.ts
Mrtenz Dec 18, 2024
65625fb
Coverage
Mrtenz Dec 18, 2024
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
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
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', () => {
FrederikBolding marked this conversation as resolved.
Show resolved Hide resolved
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.62,
},
},
});
Loading
Loading