|
| 1 | +import QtQuick |
| 2 | +import QtQuick.Controls |
| 3 | +import QtWebEngine |
| 4 | +import QtWebChannel |
| 5 | + |
| 6 | +import StatusQ.Core.Theme |
| 7 | +import utils |
| 8 | + |
| 9 | +/** |
| 10 | + * ConnectorBridge |
| 11 | + * |
| 12 | + * Simplified connector infrastructure for BrowserLayout. |
| 13 | + * Provides WebEngine profiles with script injection, WebChannel, |
| 14 | + * ConnectorManager, and direct connection to Nim backend. |
| 15 | + * |
| 16 | + * This component bridges the Browser UI with the Connector backend system. |
| 17 | + */ |
| 18 | +Item { |
| 19 | + id: root |
| 20 | + |
| 21 | + // Properties |
| 22 | + property string userUID: "" |
| 23 | + property var connectorController: null |
| 24 | + property string defaultAccountAddress: "" // Default wallet account address |
| 25 | + property var accountsModel: null // Model of all available accounts |
| 26 | + property string httpUserAgent: "" // Custom user agent for web profiles |
| 27 | + |
| 28 | + // Expose profiles and channel |
| 29 | + readonly property alias webChannel: channel |
| 30 | + readonly property alias defaultProfile: defaultProfile |
| 31 | + readonly property alias otrProfile: otrProfile |
| 32 | + |
| 33 | + // Expose manager for external URL updates |
| 34 | + readonly property alias manager: connectorManager |
| 35 | + |
| 36 | + // Expose dApp metadata properties for direct binding |
| 37 | + property alias dappUrl: connectorManager.dappUrl |
| 38 | + property alias dappOrigin: connectorManager.dappOrigin |
| 39 | + property alias dappName: connectorManager.dappName |
| 40 | + property alias dappIconUrl: connectorManager.dappIconUrl |
| 41 | + property alias clientId: connectorManager.clientId |
| 42 | + |
| 43 | + // Helper functions (replaces web3ProviderStore functions) |
| 44 | + function hasWalletConnected(hostname, address) { |
| 45 | + if (!connectorController) return false |
| 46 | + // Check if dApp has permission via connector |
| 47 | + const dApps = connectorController.getDApps() |
| 48 | + try { |
| 49 | + const dAppsObj = JSON.parse(dApps) |
| 50 | + if (Array.isArray(dAppsObj)) { |
| 51 | + return dAppsObj.some(function(dapp) { |
| 52 | + return dapp.url && dapp.url.indexOf(hostname) >= 0 |
| 53 | + }) |
| 54 | + } |
| 55 | + } catch (e) { |
| 56 | + console.warn("[ConnectorBridge] Error checking wallet connection:", e) |
| 57 | + } |
| 58 | + return false |
| 59 | + } |
| 60 | + |
| 61 | + function disconnect(hostname) { |
| 62 | + if (!connectorController) return false |
| 63 | + return connectorController.disconnect(hostname) |
| 64 | + } |
| 65 | + |
| 66 | + // Function to update dApp metadata from external source |
| 67 | + function updateDAppUrl(url, name) { |
| 68 | + if (!url) return |
| 69 | + |
| 70 | + const urlStr = url.toString() |
| 71 | + connectorManager.dappUrl = urlStr |
| 72 | + connectorManager.dappOrigin = urlStr |
| 73 | + connectorManager.dappName = name || extractDomainName(urlStr) |
| 74 | + connectorManager.dappChainId = 1 // Default to Mainnet |
| 75 | + |
| 76 | + console.log("[ConnectorBridge] Updated dApp metadata:") |
| 77 | + console.log("[ConnectorBridge] - URL:", urlStr) |
| 78 | + console.log("[ConnectorBridge] - Name:", connectorManager.dappName) |
| 79 | + console.log("[ConnectorBridge] - ChainId:", connectorManager.dappChainId) |
| 80 | + } |
| 81 | + |
| 82 | + // Helper to extract domain name from URL |
| 83 | + function extractDomainName(urlString) { |
| 84 | + try { |
| 85 | + const urlObj = new URL(urlString) |
| 86 | + return urlObj.hostname || "Unknown dApp" |
| 87 | + } catch (e) { |
| 88 | + return "Unknown dApp" |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Helper function to create script config |
| 93 | + function createScript(scriptName, runOnSubframes = true) { |
| 94 | + return { |
| 95 | + name: scriptName, |
| 96 | + sourceUrl: Qt.resolvedUrl("../js/" + scriptName), |
| 97 | + injectionPoint: WebEngineScript.DocumentCreation, |
| 98 | + worldId: WebEngineScript.MainWorld, |
| 99 | + runOnSubframes: runOnSubframes |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Script injection collection |
| 104 | + readonly property var _scripts: [ |
| 105 | + createScript("qwebchannel.js", true), |
| 106 | + createScript("ethereum_wrapper.js", true), |
| 107 | + createScript("eip6963_announcer.js", false), // Only top-level window (EIP-6963 spec) |
| 108 | + createScript("ethereum_injector.js", true) |
| 109 | + ] |
| 110 | + |
| 111 | + // Web Engine Profiles with connector script injection |
| 112 | + WebEngineProfile { |
| 113 | + id: defaultProfile |
| 114 | + storageName: "Profile_%1".arg(root.userUID) |
| 115 | + offTheRecord: false |
| 116 | + httpUserAgent: root.httpUserAgent |
| 117 | + userScripts.collection: root._scripts |
| 118 | + } |
| 119 | + |
| 120 | + WebEngineProfile { |
| 121 | + id: otrProfile |
| 122 | + storageName: "IncognitoProfile_%1".arg(root.userUID) |
| 123 | + offTheRecord: true |
| 124 | + persistentCookiesPolicy: WebEngineProfile.NoPersistentCookies |
| 125 | + httpUserAgent: root.httpUserAgent |
| 126 | + userScripts.collection: root._scripts |
| 127 | + } |
| 128 | + |
| 129 | + // ConnectorManager - Business Logic with direct Nim connection |
| 130 | + ConnectorManager { |
| 131 | + id: connectorManager |
| 132 | + connectorController: root.connectorController // (shared_modules/connector/controller.nim) |
| 133 | + |
| 134 | + dappUrl: "" |
| 135 | + dappOrigin: "" |
| 136 | + dappName: "" |
| 137 | + dappIconUrl: "" |
| 138 | + dappChainId: 1 |
| 139 | + clientId: "status-desktop/dapp-browser" |
| 140 | + |
| 141 | + // Forward events to Eip1193ProviderAdapter |
| 142 | + onConnectEvent: (info) => eip1193ProviderAdapter.connectEvent(info) |
| 143 | + onAccountsChangedEvent: (accounts) => eip1193ProviderAdapter.accountsChangedEvent(accounts) |
| 144 | + onChainChangedEvent: (chainId) => eip1193ProviderAdapter.chainChangedEvent(chainId) |
| 145 | + onRequestCompletedEvent: (payload) => eip1193ProviderAdapter.requestCompletedEvent(payload) |
| 146 | + onDisconnectEvent: (error) => eip1193ProviderAdapter.disconnectEvent(error) |
| 147 | + onMessageEvent: (message) => eip1193ProviderAdapter.messageEvent(message) |
| 148 | + onProviderStateChanged: () => eip1193ProviderAdapter.providerStateChanged() |
| 149 | + } |
| 150 | + |
| 151 | + WebChannel { |
| 152 | + id: channel |
| 153 | + registeredObjects: [eip1193ProviderAdapter] |
| 154 | + } |
| 155 | + |
| 156 | + Eip1193ProviderAdapter { |
| 157 | + id: eip1193ProviderAdapter |
| 158 | + WebChannel.id: "ethereumProvider" |
| 159 | + |
| 160 | + chainId: "0x" + connectorManager.dappChainId.toString(16) // Convert decimal to hex |
| 161 | + networkVersion: String(connectorManager.dappChainId) |
| 162 | + selectedAddress: connectorManager.accounts.length > 0 ? connectorManager.accounts[0] : "" |
| 163 | + accounts: connectorManager.accounts |
| 164 | + connected: connectorManager.connected |
| 165 | + |
| 166 | + function request(args) { |
| 167 | + return connectorManager.request(args) |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
0 commit comments