-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathwallet-bridge-init.js
More file actions
58 lines (51 loc) · 2.01 KB
/
wallet-bridge-init.js
File metadata and controls
58 lines (51 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Launchpad — MCW Apps Wallet Bridge auto-connect
*
* Activates ONLY when:
* 1. URL contains ?walletBridge=swaponline
* 2. Page is opened inside an iframe (window.parent !== window)
*
* Loads bridge client from swaponline.github.io, waits for handshake,
* sets window.ethereum to the bridge provider so web3-react/web3.js pick it up automatically.
*/
(function () {
var params = new URLSearchParams(window.location.search);
if (params.get('walletBridge') !== 'swaponline' || window.parent === window) {
return;
}
window.__bridgeActive = true;
console.log('[Launchpad] Bridge mode: loading MCW bridge client');
var BRIDGE_CLIENT_URL = 'https://swaponline.github.io/wallet-apps-bridge-client.js';
function onBridgeLoaded() {
if (!window.ethereum || !window.ethereum.isSwapWalletAppsBridge) {
console.warn('[Launchpad Bridge] Provider not set after script load');
window.__bridgeActive = false;
return;
}
// Notify app when accounts change (connect / disconnect / switch)
window.ethereum.on('accountsChanged', function (accounts) {
document.dispatchEvent(new CustomEvent('wcAccountChanged', {
detail: {
address: accounts && accounts.length > 0 ? accounts[0] : null,
isConnected: !!(accounts && accounts.length > 0),
},
}));
});
// Race-condition fix: fire immediately if already connected
var preloaded = window.ethereum.selectedAddress;
if (preloaded) {
document.dispatchEvent(new CustomEvent('wcAccountChanged', {
detail: { address: preloaded, isConnected: true },
}));
}
console.log('[Launchpad Bridge] Bridge client ready, waiting for wallet handshake...');
}
var script = document.createElement('script');
script.src = BRIDGE_CLIENT_URL;
script.onload = onBridgeLoaded;
script.onerror = function () {
console.error('[Launchpad Bridge] Failed to load bridge client:', BRIDGE_CLIENT_URL);
window.__bridgeActive = false;
};
document.head.appendChild(script);
})();