1- import type { Caip25CaveatValue } from '@metamask/chain-agnostic-permission' ;
21import {
32 Caip25CaveatType ,
43 Caip25EndowmentPermissionName ,
54 setEthAccounts ,
65} from '@metamask/chain-agnostic-permission' ;
76import type { RequestedPermissions } from '@metamask/permission-controller' ;
87import { 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
1123import { getSessionScopes } from './utils' ;
1224import { 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