@@ -6,11 +6,10 @@ import type {
66} from '@metamask/approval-controller' ;
77import { ControllerMessenger } from '@metamask/base-controller' ;
88import { isPlainObject } from '@metamask/controller-utils' ;
9- import type { Json } from '@metamask/utils' ;
9+ import { JsonRpcEngine } from '@metamask/json-rpc-engine' ;
10+ import type { Json , PendingJsonRpcResponse } from '@metamask/utils' ;
1011import { hasProperty } from '@metamask/utils' ;
1112import assert from 'assert' ;
12- import { JsonRpcEngine } from 'json-rpc-engine' ;
13- import type { PendingJsonRpcResponse } from 'json-rpc-engine' ;
1413
1514import type {
1615 AsyncRestrictedMethod ,
@@ -251,7 +250,7 @@ function getDefaultPermissionSpecifications() {
251250 CaveatTypes . filterArrayResponse ,
252251 CaveatTypes . reverseArrayResponse ,
253252 ] ,
254- methodImplementation : ( _args : RestrictedMethodOptions < void > ) => {
253+ methodImplementation : ( _args : RestrictedMethodOptions < Json [ ] > ) => {
255254 return [ 'a' , 'b' , 'c' ] ;
256255 } ,
257256 } ,
@@ -262,7 +261,9 @@ function getDefaultPermissionSpecifications() {
262261 CaveatTypes . filterObjectResponse ,
263262 CaveatTypes . noopCaveat ,
264263 ] ,
265- methodImplementation : ( _args : RestrictedMethodOptions < void > ) => {
264+ methodImplementation : (
265+ _args : RestrictedMethodOptions < Record < string , Json > > ,
266+ ) => {
266267 return { a : 'x' , b : 'y' , c : 'z' } ;
267268 } ,
268269 validator : ( permission : PermissionConstraint ) => {
@@ -292,15 +293,15 @@ function getDefaultPermissionSpecifications() {
292293 permissionType : PermissionType . RestrictedMethod ,
293294 targetName : PermissionKeys . wallet_noop ,
294295 allowedCaveats : null ,
295- methodImplementation : ( _args : RestrictedMethodOptions < void > ) => {
296+ methodImplementation : ( _args : RestrictedMethodOptions < null > ) => {
296297 return null ;
297298 } ,
298299 } ,
299300 [ PermissionKeys . wallet_noopWithPermittedAndFailureSideEffects ] : {
300301 permissionType : PermissionType . RestrictedMethod ,
301302 targetName : PermissionKeys . wallet_noopWithPermittedAndFailureSideEffects ,
302303 allowedCaveats : null ,
303- methodImplementation : ( _args : RestrictedMethodOptions < void > ) => {
304+ methodImplementation : ( _args : RestrictedMethodOptions < null > ) => {
304305 return null ;
305306 } ,
306307 sideEffect : {
@@ -312,7 +313,7 @@ function getDefaultPermissionSpecifications() {
312313 permissionType : PermissionType . RestrictedMethod ,
313314 targetName : PermissionKeys . wallet_noopWithPermittedAndFailureSideEffects2 ,
314315 allowedCaveats : null ,
315- methodImplementation : ( _args : RestrictedMethodOptions < void > ) => {
316+ methodImplementation : ( _args : RestrictedMethodOptions < null > ) => {
316317 return null ;
317318 } ,
318319 sideEffect : {
@@ -324,7 +325,7 @@ function getDefaultPermissionSpecifications() {
324325 permissionType : PermissionType . RestrictedMethod ,
325326 targetName : PermissionKeys . wallet_noopWithPermittedSideEffects ,
326327 allowedCaveats : null ,
327- methodImplementation : ( _args : RestrictedMethodOptions < void > ) => {
328+ methodImplementation : ( _args : RestrictedMethodOptions < null > ) => {
328329 return null ;
329330 } ,
330331 sideEffect : {
@@ -335,7 +336,7 @@ function getDefaultPermissionSpecifications() {
335336 [ PermissionKeys . wallet_noopWithValidator ] : {
336337 permissionType : PermissionType . RestrictedMethod ,
337338 targetName : PermissionKeys . wallet_noopWithValidator ,
338- methodImplementation : ( _args : RestrictedMethodOptions < void > ) => {
339+ methodImplementation : ( _args : RestrictedMethodOptions < null > ) => {
339340 return null ;
340341 } ,
341342 allowedCaveats : [ CaveatTypes . noopCaveat , CaveatTypes . filterArrayResponse ] ,
@@ -352,7 +353,7 @@ function getDefaultPermissionSpecifications() {
352353 [ PermissionKeys . wallet_noopWithRequiredCaveat ] : {
353354 permissionType : PermissionType . RestrictedMethod ,
354355 targetName : PermissionKeys . wallet_noopWithRequiredCaveat ,
355- methodImplementation : ( _args : RestrictedMethodOptions < void > ) => {
356+ methodImplementation : ( _args : RestrictedMethodOptions < null > ) => {
356357 return null ;
357358 } ,
358359 allowedCaveats : [ CaveatTypes . noopCaveat ] ,
@@ -388,7 +389,7 @@ function getDefaultPermissionSpecifications() {
388389 [ PermissionKeys . wallet_noopWithFactory ] : {
389390 permissionType : PermissionType . RestrictedMethod ,
390391 targetName : PermissionKeys . wallet_noopWithFactory ,
391- methodImplementation : ( _args : RestrictedMethodOptions < void > ) => {
392+ methodImplementation : ( _args : RestrictedMethodOptions < null > ) => {
392393 return null ;
393394 } ,
394395 allowedCaveats : [ CaveatTypes . filterArrayResponse ] ,
@@ -415,7 +416,7 @@ function getDefaultPermissionSpecifications() {
415416 permissionType : PermissionType . RestrictedMethod ,
416417 targetName : PermissionKeys . snap_foo ,
417418 allowedCaveats : null ,
418- methodImplementation : ( _args : RestrictedMethodOptions < void > ) => {
419+ methodImplementation : ( _args : RestrictedMethodOptions < null > ) => {
419420 return null ;
420421 } ,
421422 subjectTypes : [ SubjectType . Snap ] ,
@@ -1850,6 +1851,7 @@ describe('PermissionController', () => {
18501851 expect ( ( ) =>
18511852 controller . removeCaveat (
18521853 origin ,
1854+ // @ts -expect-error - Testing invalid permission name.
18531855 PermissionNames . wallet_noopWithRequiredCaveat ,
18541856 CaveatTypes . noopCaveat ,
18551857 ) ,
@@ -5270,7 +5272,13 @@ describe('PermissionController', () => {
52705272 } ;
52715273
52725274 const expectedError = errors . unauthorized ( {
5273- data : { origin, method : PermissionNames . wallet_getSecretArray } ,
5275+ data : {
5276+ origin,
5277+ method : PermissionNames . wallet_getSecretArray ,
5278+ cause : null ,
5279+ } ,
5280+ message :
5281+ 'Unauthorized to perform action. Try requesting the required permission(s) first. For more information, see: https://docs.metamask.io/guide/rpc-api.html#permissions' ,
52745282 } ) ;
52755283
52765284 const { error } : any = await engine . handle ( request ) ;
@@ -5293,6 +5301,11 @@ describe('PermissionController', () => {
52935301 const expectedError = errors . methodNotFound ( 'wallet_foo' , { origin } ) ;
52945302
52955303 const { error } : any = await engine . handle ( request ) ;
5304+
5305+ expect ( error . message ) . toStrictEqual ( expectedError . message ) ;
5306+ expect ( error . data . cause ) . toBeNull ( ) ;
5307+ delete error . message ;
5308+ delete error . data . cause ;
52965309 expect ( error ) . toMatchObject ( expect . objectContaining ( expectedError ) ) ;
52975310 } ) ;
52985311
@@ -5334,6 +5347,10 @@ describe('PermissionController', () => {
53345347 ) ;
53355348
53365349 const { error } : any = await engine . handle ( request ) ;
5350+ expect ( error . message ) . toStrictEqual ( expectedError . message ) ;
5351+ expect ( error . data . cause ) . toBeNull ( ) ;
5352+ delete error . message ;
5353+ delete error . data . cause ;
53375354 expect ( error ) . toMatchObject ( expect . objectContaining ( expectedError ) ) ;
53385355 } ) ;
53395356 } ) ;
0 commit comments