Skip to content

Commit b297480

Browse files
authored
Merge pull request #211 from mintlayer/dev
Dev-20-05-2025
2 parents 73181fe + 1f7807e commit b297480

File tree

41 files changed

+5625
-370
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+5625
-370
lines changed

jsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"@Helpers": ["./src/utils/Helpers/index.js"],
1818
"@Constants": ["./src/utils/Constants/index.js"],
1919
"@TestData": ["./src/utils/TestData/index.js"],
20-
"@Storage": ["./src/services/Storage/index.js"]
20+
"@Storage": ["./src/services/Storage/index.js"],
21+
"@Version": ["./src/version/version.js"]
2122
}
2223
},
2324
"exclude": ["node_modules", "build"]

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "browser-extension",
3-
"version": "1.3.7",
3+
"version": "1.4.0",
44
"private": true,
55
"dependencies": {
66
"@bitcoin-js/tiny-secp256k1-asmjs": "^2.2.3",
@@ -43,7 +43,8 @@
4343
"pretty-quick": "pretty-quick",
4444
"pretty-quick-check": "pretty-quick --check",
4545
"pretty-quick-check-branch": "pretty-quick --check --branch main",
46-
"prepare": "husky install"
46+
"prepare": "husky install",
47+
"version-manifest": "node scripts/version-manifest.js"
4748
},
4849
"eslintConfig": {
4950
"ignorePatterns": [
@@ -116,7 +117,8 @@
116117
"@APIs": "<rootDir>/src/services/API/index.js",
117118
"d3": "<rootDir>/node_modules/d3/dist/d3.min.js",
118119
"@Storage": "<rootDir>/src/services/Storage/index.js",
119-
".*wasm_wrappers.js": "<rootDir>/src/tests/mock/wasmCrypro/wasmCrypto.js"
120+
".*wasm_wrappers.js": "<rootDir>/src/tests/mock/wasmCrypro/wasmCrypto.js",
121+
"@Version": "<rootDir>/src/version/version.js"
120122
},
121123
"collectCoverageFrom": [
122124
"!src/pages"

packing.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#!/bin/sh
22

3+
34
# cleaning previous builds
45
rm -f ext.zip
56
rm -f extFF.zip
67

78
# mode inside build dir
89
cd build
910

11+
# Run the version-manifest script
12+
node ../src/version/version-manifest.js
13+
1014
CSPHEADER="<meta http-equiv=\"Content-Security-Policy\" content=\"default-src *;img-src * 'self' data: https:;style-src-elem 'self' https:\/\/fonts.googleapis.com;font-src https:\/\/fonts.gstatic.com;\"\/>"
1115
sed -i "s/<meta name=\"CSP\"\/>/$CSPHEADER/g" index.html
1216

public/background.js

Lines changed: 148 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,183 @@
1-
/* eslint-disable max-depth */
1+
/* eslint-disable no-undef */
22
/* global chrome */
33

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
67

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)
1727

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') {
1937
if (connectWindowId === false) {
20-
popupWindowId = true
21-
chrome.windows.create(
38+
pendingResponses.set(message.requestId, sendResponse)
39+
api.windows.create(
2240
{
23-
url: chrome.runtime.getURL('popup.html'),
41+
url: api.runtime.getURL('popup.html'),
2442
type: 'popup',
2543
width: 800,
2644
height: 600,
2745
focused: true,
2846
},
29-
function (win) {
47+
(win) => {
3048
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,
3454
action: 'connect',
3555
},
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+
)
4166
},
4267
)
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' })
4772
}
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(
5479
{
55-
url: chrome.runtime.getURL('popup.html'),
80+
url: api.runtime.getURL('popup.html'),
5681
type: 'popup',
5782
width: 800,
5883
height: 600,
5984
focused: true,
6085
},
61-
function (win) {
86+
(win) => {
6287
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 || {},
6995
},
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+
)
72106
},
73107
)
108+
return true
74109
} 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' })
78112
}
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]
80118

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,
103123
},
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 })
109127
}
128+
129+
return true
130+
} else {
131+
sendResponse({ error: 'Unknown method' })
110132
}
111133
}
112-
}
113-
return true
114-
})
115134

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

Comments
 (0)