@subwallet/extension-base 0.6.7-2wr → 0.6.8-0

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.
Files changed (67) hide show
  1. package/background/KoniTypes.d.ts +51 -67
  2. package/background/KoniTypes.js +13 -23
  3. package/background/RequestBytesSign.js +1 -2
  4. package/background/RequestExtrinsicSign.js +1 -2
  5. package/background/handlers/Extension.js +28 -122
  6. package/background/handlers/State.js +28 -71
  7. package/background/handlers/Tabs.js +9 -45
  8. package/background/handlers/helpers.js +1 -1
  9. package/background/handlers/index.js +4 -2
  10. package/background/handlers/subscriptions.js +5 -2
  11. package/bundle.js +1 -0
  12. package/cjs/background/KoniTypes.js +15 -24
  13. package/cjs/background/RequestBytesSign.js +1 -6
  14. package/cjs/background/RequestExtrinsicSign.js +1 -4
  15. package/cjs/background/handlers/Extension.js +27 -174
  16. package/cjs/background/handlers/State.js +28 -82
  17. package/cjs/background/handlers/Tabs.js +9 -60
  18. package/cjs/background/handlers/helpers.js +1 -2
  19. package/cjs/background/handlers/index.js +4 -10
  20. package/cjs/background/handlers/subscriptions.js +5 -4
  21. package/cjs/bundle.js +0 -1
  22. package/cjs/defaults.js +2 -3
  23. package/cjs/detectOther.js +0 -4
  24. package/cjs/detectPackage.js +2 -4
  25. package/cjs/errors/SubWalletProviderError.js +1 -3
  26. package/cjs/index.js +0 -2
  27. package/cjs/packageInfo.js +3 -1
  28. package/cjs/page/Accounts.js +3 -6
  29. package/cjs/page/Injected.js +1 -8
  30. package/cjs/page/Metadata.js +1 -5
  31. package/cjs/page/PostMessageProvider.js +22 -38
  32. package/cjs/page/Signer.js +11 -11
  33. package/cjs/page/index.js +6 -13
  34. package/cjs/signers/substrates/LedgerSigner.js +1 -7
  35. package/cjs/signers/substrates/QrSigner.js +1 -10
  36. package/cjs/signers/web3/QrSigner.js +1 -12
  37. package/cjs/stores/Accounts.js +1 -8
  38. package/cjs/stores/Base.js +1 -15
  39. package/cjs/stores/Metadata.js +1 -6
  40. package/cjs/stores/index.js +0 -3
  41. package/cjs/utils/canDerive.js +1 -1
  42. package/cjs/utils/getId.js +1 -3
  43. package/cjs/utils/index.js +0 -1
  44. package/defaults.d.ts +1 -2
  45. package/defaults.js +2 -2
  46. package/detectOther.js +1 -0
  47. package/detectPackage.js +2 -0
  48. package/errors/SubWalletProviderError.js +1 -1
  49. package/index.js +2 -0
  50. package/package.json +5 -5
  51. package/packageInfo.js +3 -1
  52. package/page/Accounts.js +3 -4
  53. package/page/Injected.js +1 -1
  54. package/page/Metadata.js +1 -3
  55. package/page/PostMessageProvider.js +22 -33
  56. package/page/Signer.js +11 -9
  57. package/page/index.js +8 -6
  58. package/signers/substrates/LedgerSigner.js +1 -3
  59. package/signers/substrates/QrSigner.js +1 -5
  60. package/signers/web3/QrSigner.js +1 -5
  61. package/stores/Accounts.js +1 -3
  62. package/stores/Base.js +1 -13
  63. package/stores/Metadata.js +1 -1
  64. package/stores/index.js +1 -0
  65. package/utils/canDerive.js +1 -0
  66. package/utils/getId.js +1 -0
  67. package/utils/index.js +1 -0
@@ -1,30 +1,21 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.default = exports.NotificationOptions = void 0;
9
-
10
8
  var _getId = require("@subwallet/extension-base/utils/getId");
11
-
12
9
  var _extensionChains = require("@subwallet/extension-chains");
13
-
14
10
  var _rxjs = require("rxjs");
15
-
16
11
  var _defaults = require("@polkadot/networks/defaults");
17
-
18
12
  var _uiSettings = _interopRequireDefault(require("@polkadot/ui-settings"));
19
-
20
13
  var _util = require("@polkadot/util");
21
-
22
14
  var _stores = require("../../stores");
23
-
24
15
  var _helpers = require("./helpers");
25
-
26
16
  // Copyright 2019-2022 @polkadot/extension-bg authors & contributors
27
17
  // SPDX-License-Identifier: Apache-2.0
18
+
28
19
  const NOTIFICATION_URL = chrome.extension.getURL('notification.html');
29
20
  const POPUP_WINDOW_OPTS = {
30
21
  focused: true,
@@ -40,15 +31,12 @@ const NORMAL_WINDOW_OPTS = {
40
31
  };
41
32
  let NotificationOptions;
42
33
  exports.NotificationOptions = NotificationOptions;
43
-
44
34
  (function (NotificationOptions) {
45
35
  NotificationOptions[NotificationOptions["None"] = 0] = "None";
46
36
  NotificationOptions[NotificationOptions["Normal"] = 1] = "Normal";
47
37
  NotificationOptions[NotificationOptions["PopUp"] = 2] = "PopUp";
48
38
  })(NotificationOptions || (exports.NotificationOptions = NotificationOptions = {}));
49
-
50
39
  const AUTH_URLS_KEY = 'authUrls';
51
-
52
40
  function extractMetadata(store) {
53
41
  store.allMap(map => {
54
42
  const knownEntries = Object.entries(_defaults.knownGenesis);
@@ -60,18 +48,17 @@ function extractMetadata(store) {
60
48
  let [, hashes] = _ref2;
61
49
  return hashes.includes(def.genesisHash);
62
50
  });
63
-
64
51
  if (entry) {
65
52
  const [name, hashes] = entry;
66
- const index = hashes.indexOf(def.genesisHash); // flatten the known metadata based on the genesis index
67
- // (lower is better/newer)
53
+ const index = hashes.indexOf(def.genesisHash);
68
54
 
55
+ // flatten the known metadata based on the genesis index
56
+ // (lower is better/newer)
69
57
  if (!defs[name] || defs[name].index > index) {
70
58
  if (defs[name]) {
71
59
  // remove the old version of the metadata
72
60
  removals.push(defs[name].key);
73
61
  }
74
-
75
62
  defs[name] = {
76
63
  def,
77
64
  index,
@@ -96,49 +83,45 @@ function extractMetadata(store) {
96
83
  });
97
84
  });
98
85
  }
99
-
100
86
  class State {
101
87
  #authUrls = {};
102
88
  #authRequests = {};
103
- #metaStore = new _stores.MetadataStore(); // Map of providers currently injected in tabs
89
+ #metaStore = new _stores.MetadataStore();
104
90
 
91
+ // Map of providers currently injected in tabs
105
92
  #injectedProviders = new Map();
106
93
  #metaRequests = {};
107
- #notification = _uiSettings.default.notification; // Map of all providers exposed by the extension, they are retrievable by key
94
+ #notification = _uiSettings.default.notification;
108
95
 
96
+ // Map of all providers exposed by the extension, they are retrievable by key
109
97
  #providers;
110
98
  #signRequests = {};
111
99
  #windows = [];
112
100
  authSubject = new _rxjs.BehaviorSubject([]);
113
101
  metaSubject = new _rxjs.BehaviorSubject([]);
114
102
  signSubject = new _rxjs.BehaviorSubject([]);
115
-
116
103
  constructor() {
117
104
  let providers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
118
105
  this.#providers = providers;
119
- extractMetadata(this.#metaStore); // retrieve previously set authorizations
106
+ extractMetadata(this.#metaStore);
120
107
 
108
+ // retrieve previously set authorizations
121
109
  const authString = localStorage.getItem(AUTH_URLS_KEY) || '{}';
122
110
  const previousAuth = JSON.parse(authString);
123
111
  this.#authUrls = previousAuth;
124
112
  }
125
-
126
113
  get knownMetadata() {
127
114
  return (0, _extensionChains.knownMetadata)();
128
115
  }
129
-
130
116
  get numAuthRequests() {
131
117
  return Object.keys(this.#authRequests).length;
132
118
  }
133
-
134
119
  get numMetaRequests() {
135
120
  return Object.keys(this.#metaRequests).length;
136
121
  }
137
-
138
122
  get numSignRequests() {
139
123
  return Object.keys(this.#signRequests).length;
140
124
  }
141
-
142
125
  get allAuthRequests() {
143
126
  return Object.values(this.#authRequests).map(_ref4 => {
144
127
  let {
@@ -153,7 +136,6 @@ class State {
153
136
  };
154
137
  });
155
138
  }
156
-
157
139
  get allMetaRequests() {
158
140
  return Object.values(this.#metaRequests).map(_ref5 => {
159
141
  let {
@@ -168,7 +150,6 @@ class State {
168
150
  };
169
151
  });
170
152
  }
171
-
172
153
  get allSignRequests() {
173
154
  return Object.values(this.#signRequests).map(_ref6 => {
174
155
  let {
@@ -185,20 +166,16 @@ class State {
185
166
  };
186
167
  });
187
168
  }
188
-
189
169
  get authUrls() {
190
170
  return this.#authUrls;
191
171
  }
192
-
193
172
  getPopup() {
194
173
  return this.#windows;
195
174
  }
196
-
197
175
  popupClose() {
198
176
  this.#windows.forEach(id => (0, _helpers.withErrorLog)(() => chrome.windows.remove(id)));
199
177
  this.#windows = [];
200
178
  }
201
-
202
179
  popupOpen() {
203
180
  if (this.#notification !== 'extension') {
204
181
  if (this.#notification === 'window') {
@@ -208,16 +185,14 @@ class State {
208
185
  }
209
186
  });
210
187
  }
211
-
212
188
  chrome.windows.getCurrent(win => {
213
- const popupOptions = { ...POPUP_WINDOW_OPTS
189
+ const popupOptions = {
190
+ ...POPUP_WINDOW_OPTS
214
191
  };
215
-
216
192
  if (win) {
217
193
  popupOptions.left = (win.left || 0) + (win.width || 0) - (POPUP_WINDOW_OPTS.width || 0) - 20;
218
194
  popupOptions.top = (win.top || 0) + 80;
219
195
  }
220
-
221
196
  chrome.windows.create(popupOptions, window => {
222
197
  if (window) {
223
198
  this.#windows.push(window.id || 0);
@@ -226,7 +201,6 @@ class State {
226
201
  });
227
202
  }
228
203
  }
229
-
230
204
  authComplete = (id, resolve, reject) => {
231
205
  const complete = result => {
232
206
  const isAllowed = result === true;
@@ -250,7 +224,6 @@ class State {
250
224
  delete this.#authRequests[id];
251
225
  this.updateIconAuth(true);
252
226
  };
253
-
254
227
  return {
255
228
  reject: error => {
256
229
  complete(error);
@@ -262,17 +235,14 @@ class State {
262
235
  }
263
236
  };
264
237
  };
265
-
266
238
  saveCurrentAuthList() {
267
239
  localStorage.setItem(AUTH_URLS_KEY, JSON.stringify(this.#authUrls));
268
240
  }
269
-
270
241
  metaComplete = (id, resolve, reject) => {
271
242
  const complete = () => {
272
243
  delete this.#metaRequests[id];
273
244
  this.updateIconMeta(true);
274
245
  };
275
-
276
246
  return {
277
247
  reject: error => {
278
248
  complete();
@@ -289,7 +259,6 @@ class State {
289
259
  delete this.#signRequests[id];
290
260
  this.updateIconSign(true);
291
261
  };
292
-
293
262
  return {
294
263
  reject: error => {
295
264
  complete();
@@ -301,13 +270,11 @@ class State {
301
270
  }
302
271
  };
303
272
  };
304
-
305
273
  stripUrl(url) {
306
274
  (0, _util.assert)(url && (url.startsWith('http:') || url.startsWith('https:') || url.startsWith('ipfs:') || url.startsWith('ipns:')), `Invalid url ${url}, expected to start with http: or https: or ipfs: or ipns:`);
307
275
  const parts = url.split('/');
308
276
  return parts[2];
309
277
  }
310
-
311
278
  updateIcon(shouldClose) {
312
279
  const authCount = this.numAuthRequests;
313
280
  const metaCount = this.numMetaRequests;
@@ -316,12 +283,10 @@ class State {
316
283
  (0, _helpers.withErrorLog)(() => chrome.browserAction.setBadgeText({
317
284
  text
318
285
  }));
319
-
320
286
  if (shouldClose && text === '') {
321
287
  this.popupClose();
322
288
  }
323
289
  }
324
-
325
290
  toggleAuthorization(url) {
326
291
  const entry = this.#authUrls[url];
327
292
  (0, _util.assert)(entry, `The source ${url} is not known`);
@@ -329,37 +294,33 @@ class State {
329
294
  this.saveCurrentAuthList();
330
295
  return this.#authUrls;
331
296
  }
332
-
333
297
  updateIconAuth(shouldClose) {
334
298
  this.authSubject.next(this.allAuthRequests);
335
299
  this.updateIcon(shouldClose);
336
300
  }
337
-
338
301
  updateIconMeta(shouldClose) {
339
302
  this.metaSubject.next(this.allMetaRequests);
340
303
  this.updateIcon(shouldClose);
341
304
  }
342
-
343
305
  updateIconSign(shouldClose) {
344
306
  this.signSubject.next(this.allSignRequests);
345
307
  this.updateIcon(shouldClose);
346
308
  }
347
-
348
309
  async authorizeUrl(url, request) {
349
- const idStr = this.stripUrl(url); // Do not enqueue duplicate authorization requests.
310
+ const idStr = this.stripUrl(url);
350
311
 
312
+ // Do not enqueue duplicate authorization requests.
351
313
  const isDuplicate = Object.values(this.#authRequests).some(request => request.idStr === idStr);
352
314
  (0, _util.assert)(!isDuplicate, `The source ${url} has a pending authorization request`);
353
-
354
315
  if (this.#authUrls[idStr]) {
355
316
  // this url was seen in the past
356
317
  (0, _util.assert)(this.#authUrls[idStr].isAllowed, `The source ${url} is not allowed to interact with this extension`);
357
318
  return false;
358
319
  }
359
-
360
320
  return new Promise((resolve, reject) => {
361
321
  const id = (0, _getId.getId)();
362
- this.#authRequests[id] = { ...this.authComplete(id, resolve, reject),
322
+ this.#authRequests[id] = {
323
+ ...this.authComplete(id, resolve, reject),
363
324
  id,
364
325
  idStr,
365
326
  request,
@@ -369,18 +330,17 @@ class State {
369
330
  this.popupOpen();
370
331
  });
371
332
  }
372
-
373
333
  ensureUrlAuthorized(url) {
374
334
  const entry = this.#authUrls[this.stripUrl(url)];
375
335
  (0, _util.assert)(entry, `The source ${url} has not been enabled yet`);
376
336
  (0, _util.assert)(entry.isAllowed, `The source ${url} is not allowed to interact with this extension`);
377
337
  return true;
378
338
  }
379
-
380
339
  injectMetadata(url, request) {
381
340
  return new Promise((resolve, reject) => {
382
341
  const id = (0, _getId.getId)();
383
- this.#metaRequests[id] = { ...this.metaComplete(id, resolve, reject),
342
+ this.#metaRequests[id] = {
343
+ ...this.metaComplete(id, resolve, reject),
384
344
  id,
385
345
  request,
386
346
  url
@@ -389,56 +349,49 @@ class State {
389
349
  this.popupOpen();
390
350
  });
391
351
  }
392
-
393
352
  getAuthRequest(id) {
394
353
  return this.#authRequests[id];
395
354
  }
396
-
397
355
  getMetaRequest(id) {
398
356
  return this.#metaRequests[id];
399
357
  }
400
-
401
358
  getSignRequest(id) {
402
359
  return this.#signRequests[id];
403
- } // List all providers the extension is exposing
404
-
360
+ }
405
361
 
362
+ // List all providers the extension is exposing
406
363
  rpcListProviders() {
407
364
  return Promise.resolve(Object.keys(this.#providers).reduce((acc, key) => {
408
365
  acc[key] = this.#providers[key].meta;
409
366
  return acc;
410
367
  }, {}));
411
368
  }
412
-
413
369
  rpcSend(request, port) {
414
370
  const provider = this.#injectedProviders.get(port);
415
371
  (0, _util.assert)(provider, 'Cannot call pub(rpc.subscribe) before provider is set');
416
372
  return provider.send(request.method, request.params);
417
- } // Start a provider, return its meta
418
-
373
+ }
419
374
 
375
+ // Start a provider, return its meta
420
376
  rpcStartProvider(key, port) {
421
377
  (0, _util.assert)(Object.keys(this.#providers).includes(key), `Provider ${key} is not exposed by extension`);
422
-
423
378
  if (this.#injectedProviders.get(port)) {
424
379
  return Promise.resolve(this.#providers[key].meta);
425
- } // Instantiate the provider
426
-
380
+ }
427
381
 
428
- this.#injectedProviders.set(port, this.#providers[key].start()); // Close provider connection when page is closed
382
+ // Instantiate the provider
383
+ this.#injectedProviders.set(port, this.#providers[key].start());
429
384
 
385
+ // Close provider connection when page is closed
430
386
  port.onDisconnect.addListener(() => {
431
387
  const provider = this.#injectedProviders.get(port);
432
-
433
388
  if (provider) {
434
389
  (0, _helpers.withErrorLog)(() => provider.disconnect());
435
390
  }
436
-
437
391
  this.#injectedProviders.delete(port);
438
392
  });
439
393
  return Promise.resolve(this.#providers[key].meta);
440
394
  }
441
-
442
395
  rpcSubscribe(_ref7, cb, port) {
443
396
  let {
444
397
  method,
@@ -449,36 +402,31 @@ class State {
449
402
  (0, _util.assert)(provider, 'Cannot call pub(rpc.subscribe) before provider is set');
450
403
  return provider.subscribe(type, method, params, cb);
451
404
  }
452
-
453
405
  rpcSubscribeConnected(_request, cb, port) {
454
406
  const provider = this.#injectedProviders.get(port);
455
407
  (0, _util.assert)(provider, 'Cannot call pub(rpc.subscribeConnected) before provider is set');
456
408
  cb(null, provider.isConnected); // Immediately send back current isConnected
457
-
458
409
  provider.on('connected', () => cb(null, true));
459
410
  provider.on('disconnected', () => cb(null, false));
460
411
  }
461
-
462
412
  rpcUnsubscribe(request, port) {
463
413
  const provider = this.#injectedProviders.get(port);
464
414
  (0, _util.assert)(provider, 'Cannot call pub(rpc.unsubscribe) before provider is set');
465
415
  return provider.unsubscribe(request.type, request.method, request.subscriptionId);
466
416
  }
467
-
468
417
  saveMetadata(meta) {
469
418
  this.#metaStore.set(meta.genesisHash, meta);
470
419
  (0, _extensionChains.addMetadata)(meta);
471
420
  }
472
-
473
421
  setNotification(notification) {
474
422
  this.#notification = notification;
475
423
  return true;
476
424
  }
477
-
478
425
  sign(url, request, account) {
479
426
  const id = (0, _getId.getId)();
480
427
  return new Promise((resolve, reject) => {
481
- this.#signRequests[id] = { ...this.signComplete(id, resolve, reject),
428
+ this.#signRequests[id] = {
429
+ ...this.signComplete(id, resolve, reject),
482
430
  account,
483
431
  id,
484
432
  request,
@@ -488,7 +436,5 @@ class State {
488
436
  this.popupOpen();
489
437
  });
490
438
  }
491
-
492
439
  }
493
-
494
440
  exports.default = State;
@@ -1,34 +1,23 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.default = void 0;
9
-
10
8
  var _defaults = require("@subwallet/extension-base/defaults");
11
-
12
9
  var _utils = require("@subwallet/extension-base/utils");
13
-
14
10
  var _phishing = require("@polkadot/phishing");
15
-
16
11
  var _uiKeyring = _interopRequireDefault(require("@polkadot/ui-keyring"));
17
-
18
12
  var _accounts = require("@polkadot/ui-keyring/observable/accounts");
19
-
20
13
  var _util = require("@polkadot/util");
21
-
22
14
  var _RequestBytesSign = _interopRequireDefault(require("../RequestBytesSign"));
23
-
24
15
  var _RequestExtrinsicSign = _interopRequireDefault(require("../RequestExtrinsicSign"));
25
-
26
16
  var _helpers = require("./helpers");
27
-
28
17
  var _subscriptions = require("./subscriptions");
29
-
30
18
  // Copyright 2019-2022 @polkadot/extension authors & contributors
31
19
  // SPDX-License-Identifier: Apache-2.0
20
+
32
21
  function transformAccounts(accounts) {
33
22
  let anyType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
34
23
  return Object.values(accounts).filter(_ref => {
@@ -70,46 +59,38 @@ function transformAccounts(accounts) {
70
59
  };
71
60
  });
72
61
  }
73
-
74
62
  class Tabs {
75
63
  #state;
76
-
77
64
  constructor(state) {
78
65
  this.#state = state;
79
66
  }
80
-
81
67
  authorize(url, request) {
82
68
  return this.#state.authorizeUrl(url, request);
83
- } // eslint-disable-next-line @typescript-eslint/no-unused-vars
84
-
69
+ }
85
70
 
71
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
86
72
  accountsList(url, _ref5) {
87
73
  let {
88
74
  anyType
89
75
  } = _ref5;
90
76
  return transformAccounts(_accounts.accounts.subject.getValue(), anyType);
91
- } // FIXME This looks very much like what we have in Extension
92
-
77
+ }
93
78
 
79
+ // FIXME This looks very much like what we have in Extension
94
80
  accountsSubscribe(url, id, port) {
95
81
  const cb = (0, _subscriptions.createSubscription)(id, port);
96
-
97
82
  const subscription = _accounts.accounts.subject.subscribe(accounts => cb(transformAccounts(accounts)));
98
-
99
83
  port.onDisconnect.addListener(() => {
100
84
  (0, _subscriptions.unsubscribe)(id);
101
85
  subscription.unsubscribe();
102
86
  });
103
87
  return true;
104
88
  }
105
-
106
89
  getSigningPair(address) {
107
90
  const pair = _uiKeyring.default.getPair(address);
108
-
109
91
  (0, _util.assert)(pair, 'Unable to find keypair');
110
92
  return pair;
111
93
  }
112
-
113
94
  bytesSign(url, request) {
114
95
  const address = request.address;
115
96
  const pair = this.getSigningPair(address);
@@ -118,7 +99,6 @@ class Tabs {
118
99
  ...pair.meta
119
100
  });
120
101
  }
121
-
122
102
  extrinsicSign(url, request) {
123
103
  const address = request.address;
124
104
  const pair = this.getSigningPair(address);
@@ -127,12 +107,11 @@ class Tabs {
127
107
  ...pair.meta
128
108
  });
129
109
  }
130
-
131
110
  metadataProvide(url, request) {
132
111
  return this.#state.injectMetadata(url, request);
133
- } // eslint-disable-next-line @typescript-eslint/no-unused-vars
134
-
112
+ }
135
113
 
114
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
136
115
  metadataList(url) {
137
116
  return this.#state.knownMetadata.map(_ref6 => {
138
117
  let {
@@ -145,50 +124,40 @@ class Tabs {
145
124
  };
146
125
  });
147
126
  }
148
-
149
127
  rpcListProviders() {
150
128
  return this.#state.rpcListProviders();
151
129
  }
152
-
153
130
  rpcSend(request, port) {
154
131
  return this.#state.rpcSend(request, port);
155
132
  }
156
-
157
133
  rpcStartProvider(key, port) {
158
134
  return this.#state.rpcStartProvider(key, port);
159
135
  }
160
-
161
136
  async rpcSubscribe(request, id, port) {
162
137
  const innerCb = (0, _subscriptions.createSubscription)(id, port);
163
-
164
138
  const cb = (_error, data) => innerCb(data);
165
-
166
139
  const subscriptionId = await this.#state.rpcSubscribe(request, cb, port);
167
140
  port.onDisconnect.addListener(() => {
168
141
  (0, _subscriptions.unsubscribe)(id);
169
- (0, _helpers.withErrorLog)(() => this.rpcUnsubscribe({ ...request,
142
+ (0, _helpers.withErrorLog)(() => this.rpcUnsubscribe({
143
+ ...request,
170
144
  subscriptionId
171
145
  }, port));
172
146
  });
173
147
  return true;
174
148
  }
175
-
176
149
  rpcSubscribeConnected(request, id, port) {
177
150
  const innerCb = (0, _subscriptions.createSubscription)(id, port);
178
-
179
151
  const cb = (_error, data) => innerCb(data);
180
-
181
152
  this.#state.rpcSubscribeConnected(request, cb, port);
182
153
  port.onDisconnect.addListener(() => {
183
154
  (0, _subscriptions.unsubscribe)(id);
184
155
  });
185
156
  return Promise.resolve(true);
186
157
  }
187
-
188
158
  async rpcUnsubscribe(request, port) {
189
159
  return this.#state.rpcUnsubscribe(request, port);
190
160
  }
191
-
192
161
  redirectPhishingLanding(phishingWebsite) {
193
162
  const nonFragment = phishingWebsite.split('#')[0];
194
163
  const encodedWebsite = encodeURIComponent(nonFragment);
@@ -206,68 +175,48 @@ class Tabs {
206
175
  })));
207
176
  });
208
177
  }
209
-
210
178
  async redirectIfPhishing(url) {
211
179
  const isInDenyList = await (0, _phishing.checkIfDenied)(url);
212
-
213
180
  if (isInDenyList) {
214
181
  this.redirectPhishingLanding(url);
215
182
  return true;
216
183
  }
217
-
218
184
  return false;
219
185
  }
220
-
221
186
  async handle(id, type, request, url, port) {
222
187
  if (type === 'pub(phishing.redirectIfDenied)') {
223
188
  return this.redirectIfPhishing(url);
224
189
  }
225
-
226
190
  switch (type) {
227
191
  case 'pub(authorize.tab)':
228
192
  return this.authorize(url, request);
229
-
230
193
  case 'pub(accounts.list)':
231
194
  return this.accountsList(url, request);
232
-
233
195
  case 'pub(accounts.subscribe)':
234
196
  return this.accountsSubscribe(url, id, port);
235
-
236
197
  case 'pub(bytes.sign)':
237
198
  return this.bytesSign(url, request);
238
-
239
199
  case 'pub(extrinsic.sign)':
240
200
  return this.extrinsicSign(url, request);
241
-
242
201
  case 'pub(metadata.list)':
243
202
  return this.metadataList(url);
244
-
245
203
  case 'pub(metadata.provide)':
246
204
  return this.metadataProvide(url, request);
247
-
248
205
  case 'pub(rpc.listProviders)':
249
206
  return this.rpcListProviders();
250
-
251
207
  case 'pub(rpc.send)':
252
208
  return this.rpcSend(request, port);
253
-
254
209
  case 'pub(rpc.startProvider)':
255
210
  return this.rpcStartProvider(request, port);
256
-
257
211
  case 'pub(rpc.subscribe)':
258
212
  return this.rpcSubscribe(request, id, port);
259
-
260
213
  case 'pub(rpc.subscribeConnected)':
261
214
  return this.rpcSubscribeConnected(request, id, port);
262
-
263
215
  case 'pub(rpc.unsubscribe)':
264
216
  return this.rpcUnsubscribe(request, port);
265
-
266
217
  default:
267
218
  throw new Error(`Unable to handle message of type ${type}`);
268
219
  }
269
220
  }
270
-
271
221
  }
272
-
273
222
  exports.default = Tabs;
@@ -4,13 +4,12 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.withErrorLog = withErrorLog;
7
-
8
7
  // Copyright 2019-2022 @polkadot/extension authors & contributors
9
8
  // SPDX-License-Identifier: Apache-2.0
9
+
10
10
  function withErrorLog(fn) {
11
11
  try {
12
12
  const p = fn();
13
-
14
13
  if (p && typeof p === 'object' && typeof p.catch === 'function') {
15
14
  p.catch(console.error);
16
15
  }