Skip to content

Commit 213c19a

Browse files
feat: Include versions in snap_getClientStatus (#3724)
Include `clientVersion` and `platformVersion` in the response for `snap_getClientStatus`. This is useful for ensuring backwards compatibility in Snaps or enabling new features in certain versions. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Add `clientVersion` and `platformVersion` to `snap_getClientStatus`, introduce `getVersion` hook, update types, simulation, and tests. > > - **RPC Methods**: > - Extend `snap_getClientStatus` to include `clientVersion` and `platformVersion` in `packages/snaps-rpc-methods/src/permitted/getClientStatus.ts`. > - Add `getVersion` to `GetClientStatusHooks` and use `getPlatformVersion()` in the implementation. > - **SDK Types**: > - Expand `GetClientStatusResult` to include `clientVersion` and `platformVersion` in `packages/snaps-sdk/src/types/methods/get-client-status.ts`. > - **Simulation**: > - Expose `getVersion` permitted hook (returns `"13.6.0-flask.0"`) in `packages/snaps-simulation/src/simulation.ts` and add corresponding test. > - **Examples/Tests**: > - Update example snap test to assert new fields in `packages/examples/packages/client-status/src/index.test.ts` and add `@metamask/snaps-utils` devDependency. > - Update `getClientStatus` tests to expect new fields. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit ed97239. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent ab34f97 commit 213c19a

File tree

8 files changed

+79
-4
lines changed

8 files changed

+79
-4
lines changed

packages/examples/packages/client-status/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"@metamask/auto-changelog": "^5.0.2",
5252
"@metamask/snaps-cli": "workspace:^",
5353
"@metamask/snaps-jest": "workspace:^",
54+
"@metamask/snaps-utils": "workspace:^",
5455
"@swc/core": "1.11.31",
5556
"@swc/jest": "^0.2.38",
5657
"@types/node": "18.14.2",

packages/examples/packages/client-status/src/index.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from '@jest/globals';
22
import { installSnap } from '@metamask/snaps-jest';
3+
import { getPlatformVersion } from '@metamask/snaps-utils';
34

45
describe('onRpcRequest', () => {
56
it('throws an error if the requested method does not exist', async () => {
@@ -31,6 +32,8 @@ describe('onRpcRequest', () => {
3132
expect(response).toRespondWith({
3233
locked: false,
3334
active: true,
35+
clientVersion: '13.6.0-flask.0',
36+
platformVersion: getPlatformVersion(),
3437
});
3538
});
3639
});

packages/snaps-rpc-methods/src/permitted/getClientStatus.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
22
import type { GetClientStatusResult } from '@metamask/snaps-sdk';
3+
import { getPlatformVersion } from '@metamask/snaps-utils';
34
import type { PendingJsonRpcResponse } from '@metamask/utils';
45

56
import { getClientStatusHandler } from './getClientStatus';
@@ -13,6 +14,7 @@ describe('snap_getClientStatus', () => {
1314
hookNames: {
1415
getIsLocked: true,
1516
getIsActive: true,
17+
getVersion: true,
1618
},
1719
});
1820
});
@@ -24,9 +26,11 @@ describe('snap_getClientStatus', () => {
2426

2527
const getIsLocked = jest.fn().mockReturnValue(true);
2628
const getIsActive = jest.fn().mockReturnValue(false);
29+
const getVersion = jest.fn().mockReturnValue('13.6.0-flask.0');
2730
const hooks = {
2831
getIsLocked,
2932
getIsActive,
33+
getVersion,
3034
};
3135

3236
const engine = new JsonRpcEngine();
@@ -51,7 +55,12 @@ describe('snap_getClientStatus', () => {
5155
expect(response).toStrictEqual({
5256
jsonrpc: '2.0',
5357
id: 1,
54-
result: { locked: true, active: false },
58+
result: {
59+
clientVersion: '13.6.0-flask.0',
60+
platformVersion: getPlatformVersion(),
61+
locked: true,
62+
active: false,
63+
},
5564
});
5665
});
5766
});

packages/snaps-rpc-methods/src/permitted/getClientStatus.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { JsonRpcEngineEndCallback } from '@metamask/json-rpc-engine';
22
import type { PermittedHandlerExport } from '@metamask/permission-controller';
33
import type { GetClientStatusResult } from '@metamask/snaps-sdk';
4+
import { getPlatformVersion } from '@metamask/snaps-utils';
45
import type {
56
JsonRpcParams,
67
JsonRpcRequest,
@@ -12,6 +13,7 @@ import type { MethodHooksObject } from '../utils';
1213
const hookNames: MethodHooksObject<GetClientStatusHooks> = {
1314
getIsLocked: true,
1415
getIsActive: true,
16+
getVersion: true,
1517
};
1618

1719
/**
@@ -37,6 +39,11 @@ export type GetClientStatusHooks = {
3739
* @returns Whether the client is active or not.
3840
*/
3941
getIsActive: () => boolean;
42+
43+
/**
44+
* @returns The version string for the client.
45+
*/
46+
getVersion: () => string;
4047
};
4148

4249
/**
@@ -51,15 +58,21 @@ export type GetClientStatusHooks = {
5158
* @param hooks - The RPC method hooks.
5259
* @param hooks.getIsLocked - A function that returns whether the client is locked or not.
5360
* @param hooks.getIsActive - A function that returns whether the client is opened or not.
61+
* @param hooks.getVersion - A function that returns the client version.
5462
* @returns Nothing.
5563
*/
5664
async function getClientStatusImplementation(
5765
_request: JsonRpcRequest,
5866
response: PendingJsonRpcResponse<GetClientStatusResult>,
5967
_next: unknown,
6068
end: JsonRpcEngineEndCallback,
61-
{ getIsLocked, getIsActive }: GetClientStatusHooks,
69+
{ getIsLocked, getIsActive, getVersion }: GetClientStatusHooks,
6270
): Promise<void> {
63-
response.result = { locked: getIsLocked(), active: getIsActive() };
71+
response.result = {
72+
locked: getIsLocked(),
73+
active: getIsActive(),
74+
clientVersion: getVersion(),
75+
platformVersion: getPlatformVersion(),
76+
};
6477
return end();
6578
}

packages/snaps-sdk/src/types/methods/get-client-status.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,21 @@ export type GetClientStatusParams = never;
1010
*
1111
* It returns an object containing useful information about the client.
1212
*/
13-
export type GetClientStatusResult = { locked: boolean; active: boolean };
13+
export type GetClientStatusResult = {
14+
/**
15+
* The semantic version of the client that the Snap is running in.
16+
*/
17+
clientVersion: string;
18+
/**
19+
* The Snaps Platform version that the client is running.
20+
*/
21+
platformVersion: string;
22+
/**
23+
* A boolean flag that indicates whether the client is locked or not.
24+
*/
25+
locked: boolean;
26+
/**
27+
* A boolean flag that indicates whether the client is active or not.
28+
*/
29+
active: boolean;
30+
};

packages/snaps-simulation/src/simulation.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,29 @@ describe('getPermittedHooks', () => {
334334
await close();
335335
});
336336

337+
it('returns the `getVersion` hook', async () => {
338+
const { snapId, close } = await getMockServer({
339+
manifest: getSnapManifest(),
340+
});
341+
342+
const location = detectSnapLocation(snapId, {
343+
allowLocal: true,
344+
});
345+
346+
const snapFiles = await fetchSnap(snapId, location);
347+
348+
const { getVersion } = getPermittedHooks(
349+
snapId,
350+
snapFiles,
351+
controllerMessenger,
352+
runSaga,
353+
);
354+
355+
expect(getVersion()).toBe('13.6.0-flask.0');
356+
357+
await close();
358+
});
359+
337360
it('returns the `getSnapFile` hook', async () => {
338361
const value = JSON.stringify({ bar: 'baz' });
339362
const { snapId, close } = await getMockServer({

packages/snaps-simulation/src/simulation.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ export type PermittedMiddlewareHooks = {
195195
*/
196196
getIsActive: () => boolean;
197197

198+
/**
199+
* A hook that returns the client version.
200+
*
201+
* @returns A string that corresponds to the client version.
202+
*/
203+
getVersion: () => string;
204+
198205
/**
199206
* A hook that returns the Snap's auxiliary file for the given path. This hook
200207
* is bound to the Snap ID.
@@ -486,6 +493,7 @@ export function getPermittedHooks(
486493
getUnlockPromise: asyncResolve(),
487494
getIsLocked: () => false,
488495
getIsActive: () => true,
496+
getVersion: () => '13.6.0-flask.0',
489497

490498
getSnapFile: async (path: string, encoding: AuxiliaryFileEncoding) =>
491499
await getSnapFile(snapFiles.auxiliaryFiles, path, encoding),

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,6 +2971,7 @@ __metadata:
29712971
"@metamask/snaps-cli": "workspace:^"
29722972
"@metamask/snaps-jest": "workspace:^"
29732973
"@metamask/snaps-sdk": "workspace:^"
2974+
"@metamask/snaps-utils": "workspace:^"
29742975
"@swc/core": "npm:1.11.31"
29752976
"@swc/jest": "npm:^0.2.38"
29762977
"@types/node": "npm:18.14.2"

0 commit comments

Comments
 (0)