@polkadot/extension-base 0.49.3 → 0.51.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/background/handlers/Extension.js +3 -3
- package/background/handlers/State.d.ts +6 -2
- package/background/handlers/State.js +27 -6
- package/background/handlers/Tabs.js +11 -1
- package/background/types.d.ts +2 -0
- package/cjs/background/handlers/Extension.js +3 -3
- package/cjs/background/handlers/State.js +27 -6
- package/cjs/background/handlers/Tabs.js +11 -1
- package/cjs/packageInfo.js +1 -1
- package/cjs/utils/portUtils.js +49 -0
- package/package.json +16 -10
- package/packageInfo.js +1 -1
- package/utils/portUtils.d.ts +14 -0
- package/utils/portUtils.js +43 -0
|
@@ -293,11 +293,11 @@ export default class Extension {
|
|
|
293
293
|
});
|
|
294
294
|
return true;
|
|
295
295
|
}
|
|
296
|
-
signingApproveSignature({ id, signature }) {
|
|
296
|
+
signingApproveSignature({ id, signature, signedTransaction }) {
|
|
297
297
|
const queued = this.__internal__state.getSignRequest(id);
|
|
298
298
|
assert(queued, 'Unable to find request');
|
|
299
299
|
const { resolve } = queued;
|
|
300
|
-
resolve({ id, signature });
|
|
300
|
+
resolve({ id, signature, signedTransaction });
|
|
301
301
|
return true;
|
|
302
302
|
}
|
|
303
303
|
signingCancel({ id }) {
|
|
@@ -378,7 +378,7 @@ export default class Extension {
|
|
|
378
378
|
return this.__internal__state.deleteAuthRequest(requestId);
|
|
379
379
|
}
|
|
380
380
|
updateCurrentTabs({ urls }) {
|
|
381
|
-
this.__internal__state.
|
|
381
|
+
this.__internal__state.updateCurrentTabsUrl(urls);
|
|
382
382
|
}
|
|
383
383
|
getConnectedTabsUrl() {
|
|
384
384
|
return this.__internal__state.getConnectedTabsUrl();
|
|
@@ -43,6 +43,7 @@ export default class State {
|
|
|
43
43
|
readonly authSubject: BehaviorSubject<AuthorizeRequest[]>;
|
|
44
44
|
readonly metaSubject: BehaviorSubject<MetadataRequest[]>;
|
|
45
45
|
readonly signSubject: BehaviorSubject<SigningRequest[]>;
|
|
46
|
+
readonly authUrlSubjects: Record<string, BehaviorSubject<AuthUrlInfo>>;
|
|
46
47
|
defaultAuthAccountSelection: string[];
|
|
47
48
|
constructor(providers?: Providers);
|
|
48
49
|
init(): Promise<void>;
|
|
@@ -54,11 +55,14 @@ export default class State {
|
|
|
54
55
|
get allMetaRequests(): MetadataRequest[];
|
|
55
56
|
get allSignRequests(): SigningRequest[];
|
|
56
57
|
get authUrls(): AuthUrls;
|
|
57
|
-
private set authUrls(value);
|
|
58
58
|
private popupClose;
|
|
59
59
|
private popupOpen;
|
|
60
60
|
private authComplete;
|
|
61
|
+
/**
|
|
62
|
+
* @deprecated This method is deprecated in favor of {@link updateCurrentTabs} and will be removed in a future release.
|
|
63
|
+
*/
|
|
61
64
|
udateCurrentTabsUrl(urls: string[]): void;
|
|
65
|
+
updateCurrentTabsUrl(urls: string[]): void;
|
|
62
66
|
getConnectedTabsUrl(): string[];
|
|
63
67
|
deleteAuthRequest(requestId: string): void;
|
|
64
68
|
private saveCurrentAuthList;
|
|
@@ -72,7 +76,7 @@ export default class State {
|
|
|
72
76
|
private updateIconAuth;
|
|
73
77
|
private updateIconMeta;
|
|
74
78
|
private updateIconSign;
|
|
75
|
-
updateAuthorizedAccounts(
|
|
79
|
+
updateAuthorizedAccounts(authorizedAccountsDiff: AuthorizedAccountsDiff): Promise<void>;
|
|
76
80
|
authorizeUrl(url: string, request: RequestAuthorizeTab): Promise<AuthResponse>;
|
|
77
81
|
ensureUrlAuthorized(url: string): boolean;
|
|
78
82
|
injectMetadata(url: string, request: MetadataDef): Promise<boolean>;
|
|
@@ -78,6 +78,7 @@ export default class State {
|
|
|
78
78
|
authSubject = new BehaviorSubject([]);
|
|
79
79
|
metaSubject = new BehaviorSubject([]);
|
|
80
80
|
signSubject = new BehaviorSubject([]);
|
|
81
|
+
authUrlSubjects = {};
|
|
81
82
|
defaultAuthAccountSelection = [];
|
|
82
83
|
constructor(providers = {}) {
|
|
83
84
|
this.__internal__providers = providers;
|
|
@@ -89,6 +90,10 @@ export default class State {
|
|
|
89
90
|
const authString = storageAuthUrls?.[AUTH_URLS_KEY] || '{}';
|
|
90
91
|
const previousAuth = JSON.parse(authString);
|
|
91
92
|
this.__internal__authUrls = previousAuth;
|
|
93
|
+
// Initialize authUrlSubjects for each URL
|
|
94
|
+
Object.entries(previousAuth).forEach(([url, authInfo]) => {
|
|
95
|
+
this.authUrlSubjects[url] = new BehaviorSubject(authInfo);
|
|
96
|
+
});
|
|
92
97
|
// retrieve previously set default auth accounts
|
|
93
98
|
const storageDefaultAuthAccounts = await chrome.storage.local.get(DEFAULT_AUTH_ACCOUNTS);
|
|
94
99
|
const defaultAuthString = storageDefaultAuthAccounts?.[DEFAULT_AUTH_ACCOUNTS] || '[]';
|
|
@@ -125,9 +130,6 @@ export default class State {
|
|
|
125
130
|
get authUrls() {
|
|
126
131
|
return this.__internal__authUrls;
|
|
127
132
|
}
|
|
128
|
-
set authUrls(urls) {
|
|
129
|
-
this.__internal__authUrls = urls;
|
|
130
|
-
}
|
|
131
133
|
popupClose() {
|
|
132
134
|
this.__internal__windows.forEach((id) => withErrorLog(() => chrome.windows.remove(id)));
|
|
133
135
|
this.__internal__windows = [];
|
|
@@ -145,13 +147,21 @@ export default class State {
|
|
|
145
147
|
authComplete = (id, resolve, reject) => {
|
|
146
148
|
const complete = async (authorizedAccounts = []) => {
|
|
147
149
|
const { idStr, request: { origin }, url } = this.__internal__authRequests[id];
|
|
148
|
-
this.
|
|
150
|
+
const strippedUrl = this.stripUrl(url);
|
|
151
|
+
const authInfo = {
|
|
149
152
|
authorizedAccounts,
|
|
150
153
|
count: 0,
|
|
151
154
|
id: idStr,
|
|
152
155
|
origin,
|
|
153
156
|
url
|
|
154
157
|
};
|
|
158
|
+
this.__internal__authUrls[strippedUrl] = authInfo;
|
|
159
|
+
if (!this.authUrlSubjects[strippedUrl]) {
|
|
160
|
+
this.authUrlSubjects[strippedUrl] = new BehaviorSubject(authInfo);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
this.authUrlSubjects[strippedUrl].next(authInfo);
|
|
164
|
+
}
|
|
155
165
|
await this.saveCurrentAuthList();
|
|
156
166
|
await this.updateDefaultAuthAccounts(authorizedAccounts);
|
|
157
167
|
delete this.__internal__authRequests[id];
|
|
@@ -170,7 +180,13 @@ export default class State {
|
|
|
170
180
|
}
|
|
171
181
|
};
|
|
172
182
|
};
|
|
183
|
+
/**
|
|
184
|
+
* @deprecated This method is deprecated in favor of {@link updateCurrentTabs} and will be removed in a future release.
|
|
185
|
+
*/
|
|
173
186
|
udateCurrentTabsUrl(urls) {
|
|
187
|
+
this.updateCurrentTabsUrl(urls);
|
|
188
|
+
}
|
|
189
|
+
updateCurrentTabsUrl(urls) {
|
|
174
190
|
const connectedTabs = urls.map((url) => {
|
|
175
191
|
let strippedUrl = '';
|
|
176
192
|
// the assert in stripUrl may throw for new tabs with "chrome://newtab/"
|
|
@@ -261,6 +277,10 @@ export default class State {
|
|
|
261
277
|
assert(entry, `The source ${url} is not known`);
|
|
262
278
|
delete this.__internal__authUrls[url];
|
|
263
279
|
await this.saveCurrentAuthList();
|
|
280
|
+
if (this.authUrlSubjects[url]) {
|
|
281
|
+
entry.authorizedAccounts = [];
|
|
282
|
+
this.authUrlSubjects[url].next(entry);
|
|
283
|
+
}
|
|
264
284
|
return this.__internal__authUrls;
|
|
265
285
|
}
|
|
266
286
|
updateIconAuth(shouldClose) {
|
|
@@ -275,9 +295,10 @@ export default class State {
|
|
|
275
295
|
this.signSubject.next(this.allSignRequests);
|
|
276
296
|
this.updateIcon(shouldClose);
|
|
277
297
|
}
|
|
278
|
-
async updateAuthorizedAccounts(
|
|
279
|
-
|
|
298
|
+
async updateAuthorizedAccounts(authorizedAccountsDiff) {
|
|
299
|
+
authorizedAccountsDiff.forEach(([url, authorizedAccountDiff]) => {
|
|
280
300
|
this.__internal__authUrls[url].authorizedAccounts = authorizedAccountDiff;
|
|
301
|
+
this.authUrlSubjects[url].next(this.__internal__authUrls[url]);
|
|
281
302
|
});
|
|
282
303
|
await this.saveCurrentAuthList();
|
|
283
304
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { combineLatest } from 'rxjs';
|
|
1
2
|
import { checkIfDenied } from '@polkadot/phishing';
|
|
2
3
|
import { keyring } from '@polkadot/ui-keyring';
|
|
3
4
|
import { accounts as accountsObservable } from '@polkadot/ui-keyring/observable/accounts';
|
|
@@ -29,6 +30,9 @@ export default class Tabs {
|
|
|
29
30
|
}
|
|
30
31
|
filterForAuthorizedAccounts(accounts, url) {
|
|
31
32
|
const auth = this.__internal__state.authUrls[this.__internal__state.stripUrl(url)];
|
|
33
|
+
if (!auth) {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
32
36
|
return accounts.filter((allAcc) => auth.authorizedAccounts
|
|
33
37
|
// we have a list, use it
|
|
34
38
|
? auth.authorizedAccounts.includes(allAcc.address)
|
|
@@ -44,8 +48,14 @@ export default class Tabs {
|
|
|
44
48
|
}
|
|
45
49
|
accountsSubscribeAuthorized(url, id, port) {
|
|
46
50
|
const cb = createSubscription(id, port);
|
|
51
|
+
const strippedUrl = this.__internal__state.stripUrl(url);
|
|
52
|
+
const authUrlObservable = this.__internal__state.authUrlSubjects[strippedUrl]?.asObservable();
|
|
53
|
+
if (!authUrlObservable) {
|
|
54
|
+
console.error(`No authUrlSubject found for ${strippedUrl}`);
|
|
55
|
+
return id;
|
|
56
|
+
}
|
|
47
57
|
this.__internal__accountSubs[id] = {
|
|
48
|
-
subscription: accountsObservable.subject.subscribe((accounts) => {
|
|
58
|
+
subscription: combineLatest([accountsObservable.subject, authUrlObservable]).subscribe(([accounts, _authUrlInfo]) => {
|
|
49
59
|
const transformedAccounts = transformAccounts(accounts);
|
|
50
60
|
cb(this.filterForAuthorizedAccounts(transformedAccounts, url));
|
|
51
61
|
}),
|
package/background/types.d.ts
CHANGED
|
@@ -244,6 +244,7 @@ export interface RequestSigningApprovePassword {
|
|
|
244
244
|
export interface RequestSigningApproveSignature {
|
|
245
245
|
id: string;
|
|
246
246
|
signature: HexString;
|
|
247
|
+
signedTransaction?: HexString;
|
|
247
248
|
}
|
|
248
249
|
export interface RequestSigningCancel {
|
|
249
250
|
id: string;
|
|
@@ -284,6 +285,7 @@ export type TransportResponseMessage<TMessageType extends MessageTypes> = TMessa
|
|
|
284
285
|
export interface ResponseSigning {
|
|
285
286
|
id: string;
|
|
286
287
|
signature: HexString;
|
|
288
|
+
signedTransaction?: HexString;
|
|
287
289
|
}
|
|
288
290
|
export interface ResponseDeriveValidate {
|
|
289
291
|
address: string;
|
|
@@ -295,11 +295,11 @@ class Extension {
|
|
|
295
295
|
});
|
|
296
296
|
return true;
|
|
297
297
|
}
|
|
298
|
-
signingApproveSignature({ id, signature }) {
|
|
298
|
+
signingApproveSignature({ id, signature, signedTransaction }) {
|
|
299
299
|
const queued = this.__internal__state.getSignRequest(id);
|
|
300
300
|
(0, util_1.assert)(queued, 'Unable to find request');
|
|
301
301
|
const { resolve } = queued;
|
|
302
|
-
resolve({ id, signature });
|
|
302
|
+
resolve({ id, signature, signedTransaction });
|
|
303
303
|
return true;
|
|
304
304
|
}
|
|
305
305
|
signingCancel({ id }) {
|
|
@@ -380,7 +380,7 @@ class Extension {
|
|
|
380
380
|
return this.__internal__state.deleteAuthRequest(requestId);
|
|
381
381
|
}
|
|
382
382
|
updateCurrentTabs({ urls }) {
|
|
383
|
-
this.__internal__state.
|
|
383
|
+
this.__internal__state.updateCurrentTabsUrl(urls);
|
|
384
384
|
}
|
|
385
385
|
getConnectedTabsUrl() {
|
|
386
386
|
return this.__internal__state.getConnectedTabsUrl();
|
|
@@ -81,6 +81,7 @@ class State {
|
|
|
81
81
|
authSubject = new rxjs_1.BehaviorSubject([]);
|
|
82
82
|
metaSubject = new rxjs_1.BehaviorSubject([]);
|
|
83
83
|
signSubject = new rxjs_1.BehaviorSubject([]);
|
|
84
|
+
authUrlSubjects = {};
|
|
84
85
|
defaultAuthAccountSelection = [];
|
|
85
86
|
constructor(providers = {}) {
|
|
86
87
|
this.__internal__providers = providers;
|
|
@@ -92,6 +93,10 @@ class State {
|
|
|
92
93
|
const authString = storageAuthUrls?.[AUTH_URLS_KEY] || '{}';
|
|
93
94
|
const previousAuth = JSON.parse(authString);
|
|
94
95
|
this.__internal__authUrls = previousAuth;
|
|
96
|
+
// Initialize authUrlSubjects for each URL
|
|
97
|
+
Object.entries(previousAuth).forEach(([url, authInfo]) => {
|
|
98
|
+
this.authUrlSubjects[url] = new rxjs_1.BehaviorSubject(authInfo);
|
|
99
|
+
});
|
|
95
100
|
// retrieve previously set default auth accounts
|
|
96
101
|
const storageDefaultAuthAccounts = await chrome.storage.local.get(DEFAULT_AUTH_ACCOUNTS);
|
|
97
102
|
const defaultAuthString = storageDefaultAuthAccounts?.[DEFAULT_AUTH_ACCOUNTS] || '[]';
|
|
@@ -128,9 +133,6 @@ class State {
|
|
|
128
133
|
get authUrls() {
|
|
129
134
|
return this.__internal__authUrls;
|
|
130
135
|
}
|
|
131
|
-
set authUrls(urls) {
|
|
132
|
-
this.__internal__authUrls = urls;
|
|
133
|
-
}
|
|
134
136
|
popupClose() {
|
|
135
137
|
this.__internal__windows.forEach((id) => (0, helpers_js_1.withErrorLog)(() => chrome.windows.remove(id)));
|
|
136
138
|
this.__internal__windows = [];
|
|
@@ -148,13 +150,21 @@ class State {
|
|
|
148
150
|
authComplete = (id, resolve, reject) => {
|
|
149
151
|
const complete = async (authorizedAccounts = []) => {
|
|
150
152
|
const { idStr, request: { origin }, url } = this.__internal__authRequests[id];
|
|
151
|
-
this.
|
|
153
|
+
const strippedUrl = this.stripUrl(url);
|
|
154
|
+
const authInfo = {
|
|
152
155
|
authorizedAccounts,
|
|
153
156
|
count: 0,
|
|
154
157
|
id: idStr,
|
|
155
158
|
origin,
|
|
156
159
|
url
|
|
157
160
|
};
|
|
161
|
+
this.__internal__authUrls[strippedUrl] = authInfo;
|
|
162
|
+
if (!this.authUrlSubjects[strippedUrl]) {
|
|
163
|
+
this.authUrlSubjects[strippedUrl] = new rxjs_1.BehaviorSubject(authInfo);
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
this.authUrlSubjects[strippedUrl].next(authInfo);
|
|
167
|
+
}
|
|
158
168
|
await this.saveCurrentAuthList();
|
|
159
169
|
await this.updateDefaultAuthAccounts(authorizedAccounts);
|
|
160
170
|
delete this.__internal__authRequests[id];
|
|
@@ -173,7 +183,13 @@ class State {
|
|
|
173
183
|
}
|
|
174
184
|
};
|
|
175
185
|
};
|
|
186
|
+
/**
|
|
187
|
+
* @deprecated This method is deprecated in favor of {@link updateCurrentTabs} and will be removed in a future release.
|
|
188
|
+
*/
|
|
176
189
|
udateCurrentTabsUrl(urls) {
|
|
190
|
+
this.updateCurrentTabsUrl(urls);
|
|
191
|
+
}
|
|
192
|
+
updateCurrentTabsUrl(urls) {
|
|
177
193
|
const connectedTabs = urls.map((url) => {
|
|
178
194
|
let strippedUrl = '';
|
|
179
195
|
// the assert in stripUrl may throw for new tabs with "chrome://newtab/"
|
|
@@ -264,6 +280,10 @@ class State {
|
|
|
264
280
|
(0, util_1.assert)(entry, `The source ${url} is not known`);
|
|
265
281
|
delete this.__internal__authUrls[url];
|
|
266
282
|
await this.saveCurrentAuthList();
|
|
283
|
+
if (this.authUrlSubjects[url]) {
|
|
284
|
+
entry.authorizedAccounts = [];
|
|
285
|
+
this.authUrlSubjects[url].next(entry);
|
|
286
|
+
}
|
|
267
287
|
return this.__internal__authUrls;
|
|
268
288
|
}
|
|
269
289
|
updateIconAuth(shouldClose) {
|
|
@@ -278,9 +298,10 @@ class State {
|
|
|
278
298
|
this.signSubject.next(this.allSignRequests);
|
|
279
299
|
this.updateIcon(shouldClose);
|
|
280
300
|
}
|
|
281
|
-
async updateAuthorizedAccounts(
|
|
282
|
-
|
|
301
|
+
async updateAuthorizedAccounts(authorizedAccountsDiff) {
|
|
302
|
+
authorizedAccountsDiff.forEach(([url, authorizedAccountDiff]) => {
|
|
283
303
|
this.__internal__authUrls[url].authorizedAccounts = authorizedAccountDiff;
|
|
304
|
+
this.authUrlSubjects[url].next(this.__internal__authUrls[url]);
|
|
284
305
|
});
|
|
285
306
|
await this.saveCurrentAuthList();
|
|
286
307
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
|
+
const rxjs_1 = require("rxjs");
|
|
4
5
|
const phishing_1 = require("@polkadot/phishing");
|
|
5
6
|
const ui_keyring_1 = require("@polkadot/ui-keyring");
|
|
6
7
|
const accounts_1 = require("@polkadot/ui-keyring/observable/accounts");
|
|
@@ -32,6 +33,9 @@ class Tabs {
|
|
|
32
33
|
}
|
|
33
34
|
filterForAuthorizedAccounts(accounts, url) {
|
|
34
35
|
const auth = this.__internal__state.authUrls[this.__internal__state.stripUrl(url)];
|
|
36
|
+
if (!auth) {
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
35
39
|
return accounts.filter((allAcc) => auth.authorizedAccounts
|
|
36
40
|
// we have a list, use it
|
|
37
41
|
? auth.authorizedAccounts.includes(allAcc.address)
|
|
@@ -47,8 +51,14 @@ class Tabs {
|
|
|
47
51
|
}
|
|
48
52
|
accountsSubscribeAuthorized(url, id, port) {
|
|
49
53
|
const cb = (0, subscriptions_js_1.createSubscription)(id, port);
|
|
54
|
+
const strippedUrl = this.__internal__state.stripUrl(url);
|
|
55
|
+
const authUrlObservable = this.__internal__state.authUrlSubjects[strippedUrl]?.asObservable();
|
|
56
|
+
if (!authUrlObservable) {
|
|
57
|
+
console.error(`No authUrlSubject found for ${strippedUrl}`);
|
|
58
|
+
return id;
|
|
59
|
+
}
|
|
50
60
|
this.__internal__accountSubs[id] = {
|
|
51
|
-
subscription: accounts_1.accounts.subject.subscribe((accounts) => {
|
|
61
|
+
subscription: (0, rxjs_1.combineLatest)([accounts_1.accounts.subject, authUrlObservable]).subscribe(([accounts, _authUrlInfo]) => {
|
|
52
62
|
const transformedAccounts = transformAccounts(accounts);
|
|
53
63
|
cb(this.filterForAuthorizedAccounts(transformedAccounts, url));
|
|
54
64
|
}),
|
package/cjs/packageInfo.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.packageInfo = void 0;
|
|
4
|
-
exports.packageInfo = { name: '@polkadot/extension-base', path: typeof __dirname === 'string' ? __dirname : 'auto', type: 'cjs', version: '0.
|
|
4
|
+
exports.packageInfo = { name: '@polkadot/extension-base', path: typeof __dirname === 'string' ? __dirname : 'auto', type: 'cjs', version: '0.51.1' };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ensurePortConnection = exports.wakeUpServiceWorkerWrapper = exports.wakeUpServiceWorker = exports.setupPort = void 0;
|
|
4
|
+
const chrome_1 = require("@polkadot/extension-inject/chrome");
|
|
5
|
+
function setupPort(portName, onMessageHandler, onDisconnectHandler) {
|
|
6
|
+
const port = chrome_1.chrome.runtime.connect({ name: portName });
|
|
7
|
+
port.onMessage.addListener(onMessageHandler);
|
|
8
|
+
port.onDisconnect.addListener(() => {
|
|
9
|
+
console.log(`Disconnected from ${portName}`);
|
|
10
|
+
onDisconnectHandler();
|
|
11
|
+
});
|
|
12
|
+
return port;
|
|
13
|
+
}
|
|
14
|
+
exports.setupPort = setupPort;
|
|
15
|
+
async function wakeUpServiceWorker() {
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
chrome_1.chrome.runtime.sendMessage({ type: 'wakeup' }, (response) => {
|
|
18
|
+
if (chrome_1.chrome.runtime.lastError) {
|
|
19
|
+
reject(new Error(chrome_1.chrome.runtime.lastError.message));
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
resolve(response);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
exports.wakeUpServiceWorker = wakeUpServiceWorker;
|
|
28
|
+
exports.wakeUpServiceWorkerWrapper = { wakeUpServiceWorker };
|
|
29
|
+
async function ensurePortConnection(portRef, portConfig) {
|
|
30
|
+
const maxAttempts = 5;
|
|
31
|
+
const delayMs = 1000;
|
|
32
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
33
|
+
try {
|
|
34
|
+
const response = await exports.wakeUpServiceWorkerWrapper.wakeUpServiceWorker();
|
|
35
|
+
if (response?.status === 'awake') {
|
|
36
|
+
if (!portRef) {
|
|
37
|
+
return setupPort(portConfig.portName, portConfig.onPortMessageHandler, portConfig.onPortDisconnectHandler);
|
|
38
|
+
}
|
|
39
|
+
return portRef;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
console.error(`Attempt ${attempt + 1} failed: ${error.message}`);
|
|
44
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
throw new Error('Failed to wake up the service worker and setup the port after multiple attempts');
|
|
48
|
+
}
|
|
49
|
+
exports.ensurePortConnection = ensurePortConnection;
|
package/package.json
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"./cjs/packageDetect.js"
|
|
19
19
|
],
|
|
20
20
|
"type": "module",
|
|
21
|
-
"version": "0.
|
|
21
|
+
"version": "0.51.1",
|
|
22
22
|
"main": "./cjs/index.js",
|
|
23
23
|
"module": "./index.js",
|
|
24
24
|
"types": "./index.d.ts",
|
|
@@ -214,20 +214,26 @@
|
|
|
214
214
|
"module": "./utils/getId.js",
|
|
215
215
|
"require": "./cjs/utils/getId.js",
|
|
216
216
|
"default": "./utils/getId.js"
|
|
217
|
+
},
|
|
218
|
+
"./utils/portUtils": {
|
|
219
|
+
"types": "./utils/portUtils.d.ts",
|
|
220
|
+
"module": "./utils/portUtils.js",
|
|
221
|
+
"require": "./cjs/utils/portUtils.js",
|
|
222
|
+
"default": "./utils/portUtils.js"
|
|
217
223
|
}
|
|
218
224
|
},
|
|
219
225
|
"dependencies": {
|
|
220
|
-
"@polkadot/api": "^12.
|
|
221
|
-
"@polkadot/extension-chains": "0.
|
|
222
|
-
"@polkadot/extension-dapp": "0.
|
|
223
|
-
"@polkadot/extension-inject": "0.
|
|
226
|
+
"@polkadot/api": "^12.3.1",
|
|
227
|
+
"@polkadot/extension-chains": "0.51.1",
|
|
228
|
+
"@polkadot/extension-dapp": "0.51.1",
|
|
229
|
+
"@polkadot/extension-inject": "0.51.1",
|
|
224
230
|
"@polkadot/keyring": "^13.0.2",
|
|
225
231
|
"@polkadot/networks": "^13.0.2",
|
|
226
|
-
"@polkadot/phishing": "^0.23.
|
|
227
|
-
"@polkadot/rpc-provider": "^12.
|
|
228
|
-
"@polkadot/types": "^12.
|
|
229
|
-
"@polkadot/ui-keyring": "^3.
|
|
230
|
-
"@polkadot/ui-settings": "^3.
|
|
232
|
+
"@polkadot/phishing": "^0.23.3",
|
|
233
|
+
"@polkadot/rpc-provider": "^12.3.1",
|
|
234
|
+
"@polkadot/types": "^12.3.1",
|
|
235
|
+
"@polkadot/ui-keyring": "^3.8.3",
|
|
236
|
+
"@polkadot/ui-settings": "^3.8.3",
|
|
231
237
|
"@polkadot/util": "^13.0.2",
|
|
232
238
|
"@polkadot/util-crypto": "^13.0.2",
|
|
233
239
|
"eventemitter3": "^5.0.1",
|
package/packageInfo.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const packageInfo = { name: '@polkadot/extension-base', path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '0.
|
|
1
|
+
export const packageInfo = { name: '@polkadot/extension-base', path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '0.51.1' };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="chrome" />
|
|
2
|
+
import type { Message } from '@polkadot/extension-base/types';
|
|
3
|
+
export declare function setupPort(portName: string, onMessageHandler: (data: Message['data']) => void, onDisconnectHandler: () => void): chrome.runtime.Port;
|
|
4
|
+
export declare function wakeUpServiceWorker(): Promise<{
|
|
5
|
+
status: string;
|
|
6
|
+
}>;
|
|
7
|
+
export declare const wakeUpServiceWorkerWrapper: {
|
|
8
|
+
wakeUpServiceWorker: typeof wakeUpServiceWorker;
|
|
9
|
+
};
|
|
10
|
+
export declare function ensurePortConnection(portRef: chrome.runtime.Port | undefined, portConfig: {
|
|
11
|
+
portName: string;
|
|
12
|
+
onPortMessageHandler: (data: Message['data']) => void;
|
|
13
|
+
onPortDisconnectHandler: () => void;
|
|
14
|
+
}): Promise<chrome.runtime.Port>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { chrome } from '@polkadot/extension-inject/chrome';
|
|
2
|
+
export function setupPort(portName, onMessageHandler, onDisconnectHandler) {
|
|
3
|
+
const port = chrome.runtime.connect({ name: portName });
|
|
4
|
+
port.onMessage.addListener(onMessageHandler);
|
|
5
|
+
port.onDisconnect.addListener(() => {
|
|
6
|
+
console.log(`Disconnected from ${portName}`);
|
|
7
|
+
onDisconnectHandler();
|
|
8
|
+
});
|
|
9
|
+
return port;
|
|
10
|
+
}
|
|
11
|
+
export async function wakeUpServiceWorker() {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
chrome.runtime.sendMessage({ type: 'wakeup' }, (response) => {
|
|
14
|
+
if (chrome.runtime.lastError) {
|
|
15
|
+
reject(new Error(chrome.runtime.lastError.message));
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
resolve(response);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export const wakeUpServiceWorkerWrapper = { wakeUpServiceWorker };
|
|
24
|
+
export async function ensurePortConnection(portRef, portConfig) {
|
|
25
|
+
const maxAttempts = 5;
|
|
26
|
+
const delayMs = 1000;
|
|
27
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
28
|
+
try {
|
|
29
|
+
const response = await wakeUpServiceWorkerWrapper.wakeUpServiceWorker();
|
|
30
|
+
if (response?.status === 'awake') {
|
|
31
|
+
if (!portRef) {
|
|
32
|
+
return setupPort(portConfig.portName, portConfig.onPortMessageHandler, portConfig.onPortDisconnectHandler);
|
|
33
|
+
}
|
|
34
|
+
return portRef;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
console.error(`Attempt ${attempt + 1} failed: ${error.message}`);
|
|
39
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
throw new Error('Failed to wake up the service worker and setup the port after multiple attempts');
|
|
43
|
+
}
|