@@ -64,100 +64,138 @@ export function toProvider(options: ToEip1193ProviderOptions): EIP1193Provider {
6464      // should invoke the return fn from subscribe instead 
6565    } , 
6666    request : async  ( request )  =>  { 
67-       if  ( request . method  ===  "eth_sendTransaction" )  { 
68-         const  account  =  wallet . getAccount ( ) ; 
69-         if  ( ! account )  { 
70-           throw  new  Error ( "Account not connected" ) ; 
67+       switch  ( request . method )  { 
68+         case  "eth_sendTransaction" : { 
69+           const  account  =  wallet . getAccount ( ) ; 
70+           if  ( ! account )  { 
71+             throw  new  Error ( "Account not connected" ) ; 
72+           } 
73+           const  result  =  await  sendTransaction ( { 
74+             account : account , 
75+             transaction : prepareTransaction ( { 
76+               ...request . params [ 0 ] , 
77+               chain, 
78+               client, 
79+             } ) , 
80+           } ) ; 
81+           return  result . transactionHash ; 
7182        } 
72-         const  result  =  await  sendTransaction ( { 
73-           account : account , 
74-           transaction : prepareTransaction ( { 
75-             ...request . params [ 0 ] , 
76-             chain, 
77-             client, 
78-           } ) , 
79-         } ) ; 
80-         return  result . transactionHash ; 
81-       } 
82-       if  ( request . method  ===  "eth_estimateGas" )  { 
83-         const  account  =  wallet . getAccount ( ) ; 
84-         if  ( ! account )  { 
85-           throw  new  Error ( "Account not connected" ) ; 
83+         case  "eth_estimateGas" : { 
84+           const  account  =  wallet . getAccount ( ) ; 
85+           if  ( ! account )  { 
86+             throw  new  Error ( "Account not connected" ) ; 
87+           } 
88+           return  estimateGas ( { 
89+             account, 
90+             transaction : prepareTransaction ( { 
91+               ...request . params [ 0 ] , 
92+               chain, 
93+               client, 
94+             } ) , 
95+           } ) ; 
8696        } 
87-         return  estimateGas ( { 
88-           account, 
89-           transaction : prepareTransaction ( { 
90-             ...request . params [ 0 ] , 
91-             chain, 
92-             client, 
93-           } ) , 
94-         } ) ; 
95-       } 
96-       if  ( request . method  ===  "personal_sign" )  { 
97-         const  account  =  wallet . getAccount ( ) ; 
98-         if  ( ! account )  { 
99-           throw  new  Error ( "Account not connected" ) ; 
97+         case  "personal_sign" : { 
98+           const  account  =  wallet . getAccount ( ) ; 
99+           if  ( ! account )  { 
100+             throw  new  Error ( "Account not connected" ) ; 
101+           } 
102+           return  account . signMessage ( { 
103+             message : { 
104+               raw : request . params [ 0 ] , 
105+             } , 
106+           } ) ; 
100107        } 
101-         return  account . signMessage ( { 
102-           message : { 
103-             raw : request . params [ 0 ] , 
104-           } , 
105-         } ) ; 
106-       } 
107-       if  ( request . method  ===  "eth_signTypedData_v4" )  { 
108-         const  account  =  wallet . getAccount ( ) ; 
109-         if  ( ! account )  { 
110-           throw  new  Error ( "Account not connected" ) ; 
108+         case  "eth_signTypedData_v4" : { 
109+           const  account  =  wallet . getAccount ( ) ; 
110+           if  ( ! account )  { 
111+             throw  new  Error ( "Account not connected" ) ; 
112+           } 
113+           const  data  =  JSON . parse ( request . params [ 1 ] ) ; 
114+           return  account . signTypedData ( data ) ; 
111115        } 
112-         const  data  =  JSON . parse ( request . params [ 1 ] ) ; 
113-         return  account . signTypedData ( data ) ; 
114-       } 
115-       if  ( request . method  ===  "eth_accounts" )  { 
116-         const  account  =  wallet . getAccount ( ) ; 
117-         if  ( ! account )  { 
118-           return  [ ] ; 
116+         case  "eth_accounts" : { 
117+           const  account  =  wallet . getAccount ( ) ; 
118+           if  ( ! account )  { 
119+             return  [ ] ; 
120+           } 
121+           return  [ account . address ] ; 
119122        } 
120-         return  [ account . address ] ; 
121-       } 
122-       if  ( request . method  ===  "eth_requestAccounts" )  { 
123-         const  connectedAccount  =  wallet . getAccount ( ) ; 
124-         if  ( connectedAccount )  { 
125-           return  [ connectedAccount . address ] ; 
123+         case  "eth_requestAccounts" : { 
124+           const  connectedAccount  =  wallet . getAccount ( ) ; 
125+           if  ( connectedAccount )  { 
126+             return  [ connectedAccount . address ] ; 
127+           } 
128+           const  account  =  connectOverride 
129+             ? await  connectOverride ( wallet ) 
130+             : await  wallet 
131+                 . connect ( { 
132+                   client, 
133+                 } ) 
134+                 . catch ( ( e )  =>  { 
135+                   console . error ( "Error connecting wallet" ,  e ) ; 
136+                   return  null ; 
137+                 } ) ; 
138+           if  ( ! account )  { 
139+             throw  new  Error ( 
140+               "Unable to connect wallet - try passing a connectOverride function" , 
141+             ) ; 
142+           } 
143+           return  [ account . address ] ; 
126144        } 
127-         const  account  =  connectOverride 
128-           ? await  connectOverride ( wallet ) 
129-           : await  wallet 
130-               . connect ( { 
131-                 client, 
132-               } ) 
133-               . catch ( ( e )  =>  { 
134-                 console . error ( "Error connecting wallet" ,  e ) ; 
135-                 return  null ; 
136-               } ) ; 
137-         if  ( ! account )  { 
138-           throw  new  Error ( 
139-             "Unable to connect wallet - try passing a connectOverride function" , 
140-           ) ; 
145+         case  "wallet_switchEthereumChain" :
146+         case  "wallet_addEthereumChain" : { 
147+           const  data  =  request . params [ 0 ] ; 
148+           const  chainIdHex  =  data . chainId ; 
149+           if  ( ! chainIdHex )  { 
150+             throw  new  Error ( "Chain ID is required" ) ; 
151+           } 
152+           // chainId is hex most likely, convert to number 
153+           const  chainId  =  isHex ( chainIdHex ) 
154+             ? hexToNumber ( chainIdHex ) 
155+             : chainIdHex ; 
156+           const  chain  =  getCachedChain ( chainId ) ; 
157+           return  wallet . switchChain ( chain ) ; 
141158        } 
142-         return  [ account . address ] ; 
143-       } 
144-       if  ( 
145-         request . method  ===  "wallet_switchEthereumChain"  || 
146-         request . method  ===  "wallet_addEthereumChain" 
147-       )  { 
148-         const  data  =  request . params [ 0 ] ; 
149-         const  chainIdHex  =  data . chainId ; 
150-         if  ( ! chainIdHex )  { 
151-           throw  new  Error ( "Chain ID is required" ) ; 
159+         case  "wallet_getCapabilities" : { 
160+           const  account  =  wallet . getAccount ( ) ; 
161+           if  ( ! account )  { 
162+             throw  new  Error ( "Account not connected" ) ; 
163+           } 
164+           if  ( ! account . getCapabilities )  { 
165+             throw  new  Error ( "Wallet does not support EIP-5792" ) ; 
166+           } 
167+           return  account . getCapabilities ( {  chainId : chain . id  } ) ; 
168+         } 
169+         case  "wallet_sendCalls" : { 
170+           const  account  =  wallet . getAccount ( ) ; 
171+           if  ( ! account )  { 
172+             throw  new  Error ( "Account not connected" ) ; 
173+           } 
174+           if  ( ! account . sendCalls )  { 
175+             throw  new  Error ( "Wallet does not support EIP-5792" ) ; 
176+           } 
177+           return  account . sendCalls ( { 
178+             ...request . params [ 0 ] , 
179+             chain : chain , 
180+           } ) ; 
181+         } 
182+         case  "wallet_getCallsStatus" : { 
183+           const  account  =  wallet . getAccount ( ) ; 
184+           if  ( ! account )  { 
185+             throw  new  Error ( "Account not connected" ) ; 
186+           } 
187+           if  ( ! account . getCallsStatus )  { 
188+             throw  new  Error ( "Wallet does not support EIP-5792" ) ; 
189+           } 
190+           return  account . getCallsStatus ( { 
191+             id : request . params [ 0 ] , 
192+             chain : chain , 
193+             client : client , 
194+           } ) ; 
152195        } 
153-         // chainId is hex most likely, convert to number 
154-         const  chainId  =  isHex ( chainIdHex ) 
155-           ? hexToNumber ( chainIdHex ) 
156-           : chainIdHex ; 
157-         const  chain  =  getCachedChain ( chainId ) ; 
158-         return  wallet . switchChain ( chain ) ; 
196+         default :
197+           return  rpcClient ( request ) ; 
159198      } 
160-       return  rpcClient ( request ) ; 
161199    } , 
162200  } ; 
163201} 
0 commit comments