@@ -4631,7 +4631,7 @@ describe('SnapController', () => {
4631
4631
snapController . destroy ( ) ;
4632
4632
} ) ;
4633
4633
4634
- it ( 'grants permitted chains permission to Snaps with `endowment:ethereum-provider`' , async ( ) => {
4634
+ it ( 'grants the `endowment:permitted- chains` permission to a Snap with `endowment:ethereum-provider`' , async ( ) => {
4635
4635
const rootMessenger = getControllerMessenger ( ) ;
4636
4636
const messenger = getSnapControllerMessenger ( rootMessenger ) ;
4637
4637
@@ -4676,15 +4676,171 @@ describe('SnapController', () => {
4676
4676
} ) ;
4677
4677
4678
4678
const approvedPermissions = {
4679
- 'endowment:ethereum-provider' : {
4680
- caveats : [ ] ,
4679
+ 'endowment:page-home' : {
4680
+ caveats : null ,
4681
+ } ,
4682
+ 'endowment:ethereum-provider' : { } ,
4683
+ [ PERMITTED_CHAINS_ENDOWMENT ] : {
4684
+ caveats : [
4685
+ {
4686
+ type : 'restrictNetworkSwitching' ,
4687
+ value : [ '0x1' ] ,
4688
+ } ,
4689
+ ] ,
4690
+ invoker : MOCK_SNAP_ID ,
4691
+ parentCapability : PERMITTED_CHAINS_ENDOWMENT ,
4692
+ } ,
4693
+ } ;
4694
+
4695
+ expect ( messenger . call ) . toHaveBeenCalledWith (
4696
+ 'PermissionController:grantPermissions' ,
4697
+ {
4698
+ approvedPermissions,
4699
+ subject : { origin : MOCK_SNAP_ID } ,
4700
+ requestData : expect . any ( Object ) ,
4701
+ } ,
4702
+ ) ;
4703
+
4704
+ snapController . destroy ( ) ;
4705
+ } ) ;
4706
+
4707
+ it ( 'overrides the `endowment:permitted-chains` permission if the Snap specifies it in its manifest' , async ( ) => {
4708
+ const rootMessenger = getControllerMessenger ( ) ;
4709
+ const messenger = getSnapControllerMessenger ( rootMessenger ) ;
4710
+
4711
+ rootMessenger . registerActionHandler (
4712
+ 'PermissionController:getPermissions' ,
4713
+ ( ) => ( { } ) ,
4714
+ ) ;
4715
+
4716
+ rootMessenger . registerActionHandler (
4717
+ 'SelectedNetworkController:getNetworkClientIdForDomain' ,
4718
+ ( ) => 'mainnet' ,
4719
+ ) ;
4720
+
4721
+ rootMessenger . registerActionHandler (
4722
+ 'NetworkController:getNetworkClientById' ,
4723
+ ( ) => ( {
4724
+ // @ts -expect-error - Partial network client.
4725
+ configuration : {
4726
+ chainId : '0x1' ,
4727
+ } ,
4728
+ } ) ,
4729
+ ) ;
4730
+
4731
+ const { manifest } = await getMockSnapFilesWithUpdatedChecksum ( {
4732
+ manifest : getSnapManifest ( {
4733
+ initialPermissions : {
4734
+ 'endowment:page-home' : { } ,
4735
+ 'endowment:ethereum-provider' : { } ,
4736
+ // @ts -expect-error - There is no type definition for this.
4737
+ 'endowment:permitted-chains' : {
4738
+ caveats : [
4739
+ {
4740
+ type : 'restrictNetworkSwitching' ,
4741
+ value : [ '0x5' ] ,
4742
+ } ,
4743
+ ] ,
4744
+ } ,
4745
+ } ,
4746
+ } ) ,
4747
+ } ) ;
4748
+
4749
+ const snapController = getSnapController (
4750
+ getSnapControllerOptions ( {
4751
+ messenger,
4752
+ detectSnapLocation : loopbackDetect ( { manifest } ) ,
4753
+ } ) ,
4754
+ ) ;
4755
+
4756
+ await snapController . installSnaps ( MOCK_ORIGIN , {
4757
+ [ MOCK_SNAP_ID ] : { } ,
4758
+ } ) ;
4759
+
4760
+ const approvedPermissions = {
4761
+ 'endowment:page-home' : {
4762
+ caveats : null ,
4763
+ } ,
4764
+ 'endowment:ethereum-provider' : { } ,
4765
+ [ PERMITTED_CHAINS_ENDOWMENT ] : {
4766
+ caveats : [
4767
+ {
4768
+ type : 'restrictNetworkSwitching' ,
4769
+ value : [ '0x1' ] ,
4770
+ } ,
4771
+ ] ,
4772
+ invoker : MOCK_SNAP_ID ,
4773
+ parentCapability : PERMITTED_CHAINS_ENDOWMENT ,
4774
+ } ,
4775
+ } ;
4776
+
4777
+ expect ( messenger . call ) . toHaveBeenCalledWith (
4778
+ 'PermissionController:grantPermissions' ,
4779
+ {
4780
+ approvedPermissions,
4781
+ subject : { origin : MOCK_SNAP_ID } ,
4782
+ requestData : expect . any ( Object ) ,
4783
+ } ,
4784
+ ) ;
4785
+
4786
+ snapController . destroy ( ) ;
4787
+ } ) ;
4788
+
4789
+ it ( 'does not grant the `endowment:permitted-chains` permission if the Snap does not have the `endowment:ethereum-provider` permission' , async ( ) => {
4790
+ const rootMessenger = getControllerMessenger ( ) ;
4791
+ const messenger = getSnapControllerMessenger ( rootMessenger ) ;
4792
+
4793
+ rootMessenger . registerActionHandler (
4794
+ 'PermissionController:getPermissions' ,
4795
+ ( ) => ( { } ) ,
4796
+ ) ;
4797
+
4798
+ rootMessenger . registerActionHandler (
4799
+ 'SelectedNetworkController:getNetworkClientIdForDomain' ,
4800
+ ( ) => {
4801
+ throw new Error ( 'This should not be called.' ) ;
4802
+ } ,
4803
+ ) ;
4804
+
4805
+ rootMessenger . registerActionHandler (
4806
+ 'NetworkController:getNetworkClientById' ,
4807
+ ( ) => {
4808
+ throw new Error ( 'This should not be called.' ) ;
4809
+ } ,
4810
+ ) ;
4811
+
4812
+ const { manifest } = await getMockSnapFilesWithUpdatedChecksum ( {
4813
+ manifest : getSnapManifest ( {
4814
+ initialPermissions : {
4815
+ 'endowment:page-home' : { } ,
4816
+ } ,
4817
+ } ) ,
4818
+ } ) ;
4819
+
4820
+ const snapController = getSnapController (
4821
+ getSnapControllerOptions ( {
4822
+ messenger,
4823
+ detectSnapLocation : loopbackDetect ( { manifest } ) ,
4824
+ } ) ,
4825
+ ) ;
4826
+
4827
+ await snapController . installSnaps ( MOCK_ORIGIN , {
4828
+ [ MOCK_SNAP_ID ] : { } ,
4829
+ } ) ;
4830
+
4831
+ const approvedPermissions = {
4832
+ 'endowment:page-home' : {
4833
+ caveats : null ,
4681
4834
} ,
4682
- [ PERMITTED_CHAINS_ENDOWMENT ] : { } ,
4683
4835
} ;
4684
4836
4685
4837
expect ( messenger . call ) . toHaveBeenCalledWith (
4686
4838
'PermissionController:grantPermissions' ,
4687
- { approvedPermissions, subject : { origin : MOCK_SNAP_ID } } ,
4839
+ {
4840
+ approvedPermissions,
4841
+ subject : { origin : MOCK_SNAP_ID } ,
4842
+ requestData : expect . any ( Object ) ,
4843
+ } ,
4688
4844
) ;
4689
4845
4690
4846
snapController . destroy ( ) ;
0 commit comments