| 261 | * Handle RPC methods called by dapps |
| 262 | */ |
| 263 | export const getRpcMethodMiddleware = ({ |
| 264 | hostname, |
| 265 | channelId, |
| 266 | getProviderState, |
| 267 | navigation, |
| 268 | // Website info |
| 269 | url, |
| 270 | title, |
| 271 | icon, |
| 272 | // Bookmarks |
| 273 | isHomepage, |
| 274 | // Show autocomplete |
| 275 | fromHomepage, |
| 276 | toggleUrlModal, |
| 277 | // Wizard |
| 278 | wizardScrollAdjusted, |
| 279 | // For the browser |
| 280 | tabId, |
| 281 | // For WalletConnect |
| 282 | isWalletConnect, |
| 283 | // For MM SDK |
| 284 | isMMSDK, |
| 285 | injectHomePageScripts, |
| 286 | // For analytics |
| 287 | analytics, |
| 288 | }: RPCMethodsMiddleParameters) => { |
| 289 | // Make sure to always have the correct origin |
| 290 | hostname = hostname.replace(AppConstants.MM_SDK.SDK_REMOTE_ORIGIN, ''); |
| 291 | DevLogger.log( |
| 292 | `getRpcMethodMiddleware hostname=${hostname} channelId=${channelId}`, |
| 293 | ); |
| 294 | // all user facing RPC calls not implemented by the provider |
| 295 | return createAsyncMiddleware(async (req: any, res: any, next: any) => { |
| 296 | // Used by eth_accounts and eth_coinbase RPCs. |
| 297 | const getEthAccounts = async () => { |
| 298 | const accounts: string[] = |
| 299 | (await getPermittedAccounts(channelId ?? hostname)) ?? []; |
| 300 | res.result = accounts; |
| 301 | }; |
| 302 | |
| 303 | const checkTabActive = () => { |
| 304 | if (!tabId) return true; |
| 305 | const { browser } = store.getState(); |
| 306 | if (tabId !== browser.activeTab) |
| 307 | throw providerErrors.userRejectedRequest(); |
| 308 | }; |
| 309 | |
| 310 | const getSource = () => { |
| 311 | if (analytics?.isRemoteConn) |
| 312 | return AppConstants.REQUEST_SOURCES.SDK_REMOTE_CONN; |
| 313 | if (isWalletConnect) return AppConstants.REQUEST_SOURCES.WC; |
| 314 | return AppConstants.REQUEST_SOURCES.IN_APP_BROWSER; |
| 315 | }; |
| 316 | |
| 317 | const startApprovalFlow = (opts: StartFlowOptions) => { |
| 318 | checkTabActive(); |
| 319 | Engine.context.ApprovalController.clear( |
| 320 | providerErrors.userRejectedRequest(), |