Skip to content

Commit ef6f3be

Browse files
Add struct for wallet_createSession
1 parent 8b57c30 commit ef6f3be

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

packages/snaps-simulation/src/middleware/multichain/create-session.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ describe('createSessionHandler', () => {
112112
jsonrpc: '2.0',
113113
id: 1,
114114
method: 'wallet_createSession',
115-
params: [],
115+
params: { optionalScopes: { foo: {} } },
116116
},
117117
{ grantPermissions, getMnemonic },
118118
),

packages/snaps-simulation/src/middleware/multichain/create-session.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
import type { Caip25CaveatValue } from '@metamask/chain-agnostic-permission';
21
import {
32
Caip25CaveatType,
43
Caip25EndowmentPermissionName,
54
setEthAccounts,
65
} from '@metamask/chain-agnostic-permission';
76
import type { RequestedPermissions } from '@metamask/permission-controller';
87
import { rpcErrors } from '@metamask/rpc-errors';
9-
import { isObject, type JsonRpcRequest } from '@metamask/utils';
8+
import {
9+
array,
10+
is,
11+
object,
12+
string,
13+
optional,
14+
record,
15+
} from '@metamask/superstruct';
16+
import {
17+
CaipAccountIdStruct,
18+
CaipChainIdStruct,
19+
JsonStruct,
20+
type JsonRpcRequest,
21+
} from '@metamask/utils';
1022

1123
import { getSessionScopes } from './utils';
1224
import { getSimulationAccount } from '../internal-methods/accounts';
@@ -16,6 +28,21 @@ export type CreateSessionHandlerHooks = {
1628
getMnemonic: () => Promise<Uint8Array>;
1729
};
1830

31+
const ScopesStruct = record(
32+
CaipChainIdStruct,
33+
object({
34+
methods: array(string()),
35+
accounts: array(CaipAccountIdStruct),
36+
notifications: array(string()),
37+
}),
38+
);
39+
40+
const CreateSessionParamsStruct = object({
41+
requiredScopes: optional(ScopesStruct),
42+
optionalScopes: optional(ScopesStruct),
43+
sessionProperties: optional(record(string(), JsonStruct)),
44+
});
45+
1946
/**
2047
* A handler that implements a simplified version of `wallet_createSession`.
2148
*
@@ -27,16 +54,16 @@ export async function createSessionHandler(
2754
request: JsonRpcRequest,
2855
hooks: CreateSessionHandlerHooks,
2956
) {
30-
if (!isObject(request.params)) {
57+
if (!is(request.params, CreateSessionParamsStruct)) {
3158
throw rpcErrors.invalidParams({ data: { request } });
3259
}
3360

3461
const caveat = {
3562
requiredScopes: request.params.requiredScopes ?? {},
3663
optionalScopes: request.params.optionalScopes ?? {},
37-
sessionProperties: {},
64+
sessionProperties: request.params.sessionProperties ?? {},
3865
isMultichainOrigin: true,
39-
} as Caip25CaveatValue;
66+
};
4067

4168
const mnemonic = await hooks.getMnemonic();
4269
const ethereumAccounts = [await getSimulationAccount(mnemonic)];

0 commit comments

Comments
 (0)