|
1 |
| -/* eslint-disable max-depth */ |
| 1 | +/* eslint-disable no-undef */ |
2 | 2 | /* global chrome */
|
3 | 3 |
|
4 |
| -var popupWindowId = false |
5 |
| -var connectWindowId = false |
| 4 | +;(function () { |
| 5 | + // Detect browser API (Chrome or Firefox) |
| 6 | + const api = typeof browser !== 'undefined' ? browser : chrome |
6 | 7 |
|
7 |
| -chrome.runtime.onMessageExternal.addListener(function ( |
8 |
| - request, |
9 |
| - sender, |
10 |
| - sendResponse, |
11 |
| -) { |
12 |
| - if (request) { |
13 |
| - if (request.message) { |
14 |
| - if (request.message === 'version') { |
15 |
| - sendResponse({ version: chrome.runtime.getManifest().version }) |
16 |
| - } |
| 8 | + // Track popup window IDs |
| 9 | + let popupWindowId = false |
| 10 | + let connectWindowId = false |
| 11 | + let connectedSites = {} |
| 12 | + const pendingResponses = new Map() |
| 13 | + |
| 14 | + // Load connected sites from storage |
| 15 | + api.storage.local.get(['connectedSites'], (data) => { |
| 16 | + if (api.runtime.lastError) { |
| 17 | + console.error('[Mintlayer] Storage get error:', api.runtime.lastError) |
| 18 | + return |
| 19 | + } |
| 20 | + connectedSites = data.connectedSites || {} |
| 21 | + console.log('[Mintlayer] Connected sites loaded:', connectedSites) |
| 22 | + }) |
| 23 | + |
| 24 | + // Single listener for all messages |
| 25 | + api.runtime.onMessage.addListener((message, sender, sendResponse) => { |
| 26 | + console.log('[Mintlayer] Received message:', message, 'from', sender) |
17 | 27 |
|
18 |
| - if (request.message === 'connect') { |
| 28 | + const origin = sender.origin || 'unknown' |
| 29 | + |
| 30 | + // Handle requests from content.js |
| 31 | + if (message.method) { |
| 32 | + if (message.method === 'checkConnection') { |
| 33 | + sendResponse({ |
| 34 | + result: { isConnected: !!connectedSites[origin] }, |
| 35 | + }) |
| 36 | + } else if (message.method === 'connect') { |
19 | 37 | if (connectWindowId === false) {
|
20 |
| - popupWindowId = true |
21 |
| - chrome.windows.create( |
| 38 | + pendingResponses.set(message.requestId, sendResponse) |
| 39 | + api.windows.create( |
22 | 40 | {
|
23 |
| - url: chrome.runtime.getURL('popup.html'), |
| 41 | + url: api.runtime.getURL('popup.html'), |
24 | 42 | type: 'popup',
|
25 | 43 | width: 800,
|
26 | 44 | height: 600,
|
27 | 45 | focused: true,
|
28 | 46 | },
|
29 |
| - function (win) { |
| 47 | + (win) => { |
30 | 48 | connectWindowId = win.id
|
31 |
| - setTimeout(function () { |
32 |
| - chrome.runtime.sendMessage( |
33 |
| - { |
| 49 | + api.storage.local.set( |
| 50 | + { |
| 51 | + pendingRequest: { |
| 52 | + origin, |
| 53 | + requestId: message.requestId, |
34 | 54 | action: 'connect',
|
35 | 55 | },
|
36 |
| - function (response) { |
37 |
| - sendResponse(response) |
38 |
| - }, |
39 |
| - ) |
40 |
| - }, 1000) |
| 56 | + }, |
| 57 | + () => { |
| 58 | + if (api.runtime.lastError) { |
| 59 | + console.error( |
| 60 | + '[Mintlayer] Storage set error:', |
| 61 | + api.runtime.lastError, |
| 62 | + ) |
| 63 | + } |
| 64 | + }, |
| 65 | + ) |
41 | 66 | },
|
42 | 67 | )
|
43 |
| - } else if (typeof popupWindowId === 'number') { |
44 |
| - //The window is open, and the user clicked the button. |
45 |
| - // Focus the window. |
46 |
| - chrome.windows.update(popupWindowId, { focused: true }) |
| 68 | + return true // Keep channel open |
| 69 | + } else if (typeof connectWindowId === 'number') { |
| 70 | + api.windows.update(connectWindowId, { focused: true }) |
| 71 | + sendResponse({ error: 'Connection window already open' }) |
47 | 72 | }
|
48 |
| - } |
49 |
| - |
50 |
| - if (request.message === 'delegate') { |
51 |
| - if (popupWindowId === false) { |
52 |
| - popupWindowId = true |
53 |
| - chrome.windows.create( |
| 73 | + } else if (message.method === 'signTransaction') { |
| 74 | + if (!connectedSites[origin]) { |
| 75 | + sendResponse({ error: 'Not connected. Call connect first.' }) |
| 76 | + } else if (popupWindowId === false) { |
| 77 | + pendingResponses.set(message.requestId, sendResponse) |
| 78 | + api.windows.create( |
54 | 79 | {
|
55 |
| - url: chrome.runtime.getURL('popup.html'), |
| 80 | + url: api.runtime.getURL('popup.html'), |
56 | 81 | type: 'popup',
|
57 | 82 | width: 800,
|
58 | 83 | height: 600,
|
59 | 84 | focused: true,
|
60 | 85 | },
|
61 |
| - function (win) { |
| 86 | + (win) => { |
62 | 87 | popupWindowId = win.id
|
63 |
| - setTimeout(function () { |
64 |
| - chrome.runtime.sendMessage({ |
65 |
| - action: 'createDelegate', |
66 |
| - data: { |
67 |
| - pool_id: request.pool_id, |
68 |
| - referral_code: request.referral_code || '', |
| 88 | + api.storage.local.set( |
| 89 | + { |
| 90 | + pendingRequest: { |
| 91 | + origin, |
| 92 | + requestId: message.requestId, |
| 93 | + action: 'signTransaction', |
| 94 | + data: message.params || {}, |
69 | 95 | },
|
70 |
| - }) |
71 |
| - }, 1000) |
| 96 | + }, |
| 97 | + () => { |
| 98 | + if (api.runtime.lastError) { |
| 99 | + console.error( |
| 100 | + '[Mintlayer] Storage set error:', |
| 101 | + api.runtime.lastError, |
| 102 | + ) |
| 103 | + } |
| 104 | + }, |
| 105 | + ) |
72 | 106 | },
|
73 | 107 | )
|
| 108 | + return true |
74 | 109 | } else if (typeof popupWindowId === 'number') {
|
75 |
| - //The window is open, and the user clicked the button. |
76 |
| - // Focus the window. |
77 |
| - chrome.windows.update(popupWindowId, { focused: true }) |
| 110 | + api.windows.update(popupWindowId, { focused: true }) |
| 111 | + sendResponse({ error: 'Transaction signing window already open' }) |
78 | 112 | }
|
79 |
| - } |
| 113 | + } else if (message.method === 'version') { |
| 114 | + sendResponse({ result: api.runtime.getManifest().version }) |
| 115 | + } else if (message.method === 'getSession') { |
| 116 | + const sessionOrigin = message.origin || sender.origin |
| 117 | + const session = connectedSites[sessionOrigin] |
80 | 118 |
|
81 |
| - if (request.message === 'stake') { |
82 |
| - if (popupWindowId === false) { |
83 |
| - popupWindowId = true |
84 |
| - chrome.windows.create( |
85 |
| - { |
86 |
| - url: chrome.runtime.getURL('popup.html'), |
87 |
| - type: 'popup', |
88 |
| - width: 800, |
89 |
| - height: 630, |
90 |
| - focused: true, |
91 |
| - }, |
92 |
| - function (win) { |
93 |
| - popupWindowId = win.id |
94 |
| - setTimeout(function () { |
95 |
| - chrome.runtime.sendMessage({ |
96 |
| - action: 'addStake', |
97 |
| - data: { |
98 |
| - delegation_id: request.delegation_id, |
99 |
| - amount: request.amount, |
100 |
| - }, |
101 |
| - }) |
102 |
| - }, 1000) |
| 119 | + if (session && session.address) { |
| 120 | + sendResponse({ |
| 121 | + result: { |
| 122 | + address: session.address, |
103 | 123 | },
|
104 |
| - ) |
105 |
| - } else if (typeof popupWindowId === 'number') { |
106 |
| - //The window is open, and the user clicked the button. |
107 |
| - // Focus the window. |
108 |
| - chrome.windows.update(popupWindowId, { focused: true }) |
| 124 | + }) |
| 125 | + } else { |
| 126 | + sendResponse({ result: null }) |
109 | 127 | }
|
| 128 | + |
| 129 | + return true |
| 130 | + } else { |
| 131 | + sendResponse({ error: 'Unknown method' }) |
110 | 132 | }
|
111 | 133 | }
|
112 |
| - } |
113 |
| - return true |
114 |
| -}) |
115 | 134 |
|
116 |
| -chrome.windows.onRemoved.addListener(function (winId) { |
117 |
| - if (popupWindowId === winId) { |
118 |
| - popupWindowId = false |
119 |
| - } |
120 |
| - if (connectWindowId === winId) { |
121 |
| - connectWindowId = false |
122 |
| - } |
123 |
| -}) |
| 135 | + // Handle popup responses |
| 136 | + if (message.action === 'popupResponse') { |
| 137 | + const { requestId, origin, result, error } = message |
| 138 | + const storedSendResponse = pendingResponses.get(requestId) |
| 139 | + if (result && message.method === 'connect') { |
| 140 | + connectedSites[origin] = { |
| 141 | + address: result.address, |
| 142 | + timestamp: Date.now(), |
| 143 | + } |
| 144 | + api.storage.local.set({ connectedSites }, () => { |
| 145 | + if (api.runtime.lastError) { |
| 146 | + console.error( |
| 147 | + '[Mintlayer] Storage set error:', |
| 148 | + api.runtime.lastError, |
| 149 | + ) |
| 150 | + } |
| 151 | + |
| 152 | + if (storedSendResponse) { |
| 153 | + storedSendResponse({ result, error }) |
| 154 | + pendingResponses.delete(requestId) |
| 155 | + } |
| 156 | + }) |
| 157 | + } else if (result && message.method === 'signTransaction_approve') { |
| 158 | + storedSendResponse({ result, error }) |
| 159 | + pendingResponses.delete(requestId) |
| 160 | + } else if (result && message.method === 'signTransaction_reject') { |
| 161 | + storedSendResponse({ result, error }) |
| 162 | + pendingResponses.delete(requestId) |
| 163 | + } else { |
| 164 | + api.runtime.sendMessage({ requestId, result, error }, (response) => { |
| 165 | + if (api.runtime.lastError) { |
| 166 | + console.error( |
| 167 | + '[Mintlayer] Send response error:', |
| 168 | + api.runtime.lastError, |
| 169 | + ) |
| 170 | + } |
| 171 | + }) |
| 172 | + } |
| 173 | + } |
| 174 | + }) |
| 175 | + |
| 176 | + // Clean up window IDs |
| 177 | + api.windows.onRemoved.addListener((winId) => { |
| 178 | + if (popupWindowId === winId) popupWindowId = false |
| 179 | + if (connectWindowId === winId) connectWindowId = false |
| 180 | + }) |
| 181 | + |
| 182 | + console.log('[Mintlayer Extension] Background script loaded') |
| 183 | +})() |
0 commit comments