@polkadot/extension-base 0.44.9 → 0.45.2

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 (79) hide show
  1. package/background/RequestBytesSign.d.ts +1 -1
  2. package/background/RequestBytesSign.js +8 -11
  3. package/background/RequestExtrinsicSign.d.ts +1 -1
  4. package/background/RequestExtrinsicSign.js +8 -11
  5. package/background/handlers/Extension.d.ts +2 -2
  6. package/background/handlers/Extension.js +460 -612
  7. package/background/handlers/State.d.ts +1 -1
  8. package/background/handlers/State.js +380 -424
  9. package/background/handlers/Tabs.d.ts +2 -2
  10. package/background/handlers/Tabs.js +178 -212
  11. package/background/handlers/helpers.js +8 -10
  12. package/background/handlers/index.d.ts +1 -1
  13. package/background/handlers/index.js +29 -39
  14. package/background/handlers/subscriptions.d.ts +1 -1
  15. package/background/handlers/subscriptions.js +13 -22
  16. package/background/types.d.ts +2 -2
  17. package/bundle.d.ts +1 -1
  18. package/bundle.js +1 -4
  19. package/cjs/background/RequestBytesSign.js +11 -18
  20. package/cjs/background/RequestExtrinsicSign.js +9 -16
  21. package/cjs/background/handlers/Extension.js +468 -658
  22. package/cjs/background/handlers/State.js +389 -460
  23. package/cjs/background/handlers/Tabs.js +185 -242
  24. package/cjs/background/handlers/helpers.js +12 -16
  25. package/cjs/background/handlers/index.js +37 -52
  26. package/cjs/background/handlers/subscriptions.js +17 -28
  27. package/cjs/background/types.js +2 -1
  28. package/cjs/bundle.js +4 -11
  29. package/cjs/defaults.js +3 -12
  30. package/cjs/detectOther.js +5 -12
  31. package/cjs/detectPackage.js +6 -11
  32. package/cjs/index.js +3 -15
  33. package/cjs/packageInfo.js +2 -16
  34. package/cjs/page/Accounts.js +19 -28
  35. package/cjs/page/Injected.js +19 -26
  36. package/cjs/page/Metadata.js +10 -18
  37. package/cjs/page/PostMessageProvider.js +127 -160
  38. package/cjs/page/Signer.js +23 -38
  39. package/cjs/page/index.js +44 -61
  40. package/cjs/page/types.js +2 -1
  41. package/cjs/stores/Accounts.js +17 -22
  42. package/cjs/stores/Base.js +56 -63
  43. package/cjs/stores/Metadata.js +10 -15
  44. package/cjs/stores/index.js +9 -19
  45. package/cjs/types.js +2 -1
  46. package/cjs/utils/canDerive.js +5 -10
  47. package/cjs/utils/getId.js +6 -11
  48. package/cjs/utils/index.js +4 -11
  49. package/defaults.js +1 -8
  50. package/detectOther.js +0 -3
  51. package/detectPackage.js +2 -7
  52. package/index.d.ts +1 -1
  53. package/index.js +1 -7
  54. package/package.json +20 -17
  55. package/packageInfo.js +1 -11
  56. package/page/Accounts.d.ts +1 -1
  57. package/page/Accounts.js +18 -23
  58. package/page/Injected.d.ts +5 -5
  59. package/page/Injected.js +15 -18
  60. package/page/Metadata.d.ts +1 -1
  61. package/page/Metadata.js +9 -13
  62. package/page/PostMessageProvider.d.ts +1 -1
  63. package/page/PostMessageProvider.js +124 -152
  64. package/page/Signer.d.ts +1 -1
  65. package/page/Signer.js +22 -33
  66. package/page/index.d.ts +2 -2
  67. package/page/index.js +36 -59
  68. package/page/types.d.ts +1 -1
  69. package/stores/Accounts.d.ts +1 -1
  70. package/stores/Accounts.js +14 -15
  71. package/stores/Base.js +55 -57
  72. package/stores/Metadata.d.ts +1 -1
  73. package/stores/Metadata.js +7 -8
  74. package/stores/index.d.ts +2 -2
  75. package/stores/index.js +2 -5
  76. package/utils/canDerive.js +1 -4
  77. package/utils/getId.js +2 -5
  78. package/utils/index.d.ts +1 -1
  79. package/utils/index.js +1 -4
@@ -1,6 +1,6 @@
1
1
  /// <reference types="chrome" />
2
- import type { MessageTypes, RequestTypes, ResponseTypes } from '../types';
3
- import State from './State';
2
+ import type { MessageTypes, RequestTypes, ResponseTypes } from '../types.js';
3
+ import State from './State.js';
4
4
  export default class Tabs {
5
5
  #private;
6
6
  constructor(state: State);
@@ -1,223 +1,189 @@
1
- // Copyright 2019-2023 @polkadot/extension authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
1
+ var _Tabs_accountSubs, _Tabs_state;
2
+ import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
4
3
  import { PHISHING_PAGE_REDIRECT } from '@polkadot/extension-base/defaults';
5
4
  import { canDerive } from '@polkadot/extension-base/utils';
6
5
  import { checkIfDenied } from '@polkadot/phishing';
7
6
  import keyring from '@polkadot/ui-keyring';
8
7
  import { accounts as accountsObservable } from '@polkadot/ui-keyring/observable/accounts';
9
8
  import { assert, isNumber } from '@polkadot/util';
10
- import RequestBytesSign from "../RequestBytesSign.js";
11
- import RequestExtrinsicSign from "../RequestExtrinsicSign.js";
12
- import { withErrorLog } from "./helpers.js";
13
- import { createSubscription, unsubscribe } from "./subscriptions.js";
9
+ import RequestBytesSign from '../RequestBytesSign.js';
10
+ import RequestExtrinsicSign from '../RequestExtrinsicSign.js';
11
+ import { withErrorLog } from './helpers.js';
12
+ import { createSubscription, unsubscribe } from './subscriptions.js';
14
13
  function transformAccounts(accounts, anyType = false) {
15
- return Object.values(accounts).filter(({
16
- json: {
17
- meta: {
18
- isHidden
19
- }
20
- }
21
- }) => !isHidden).filter(({
22
- type
23
- }) => anyType ? true : canDerive(type)).sort((a, b) => (a.json.meta.whenCreated || 0) - (b.json.meta.whenCreated || 0)).map(({
24
- json: {
25
- address,
26
- meta: {
14
+ return Object
15
+ .values(accounts)
16
+ .filter(({ json: { meta: { isHidden } } }) => !isHidden)
17
+ .filter(({ type }) => anyType ? true : canDerive(type))
18
+ .sort((a, b) => (a.json.meta.whenCreated || 0) - (b.json.meta.whenCreated || 0))
19
+ .map(({ json: { address, meta: { genesisHash, name } }, type }) => ({
20
+ address,
27
21
  genesisHash,
28
- name
29
- }
30
- },
31
- type
32
- }) => ({
33
- address,
34
- genesisHash,
35
- name,
36
- type
37
- }));
22
+ name,
23
+ type
24
+ }));
38
25
  }
39
26
  export default class Tabs {
40
- #accountSubs = {};
41
- #state;
42
- constructor(state) {
43
- this.#state = state;
44
- }
45
- filterForAuthorizedAccounts(accounts, url) {
46
- const auth = this.#state.authUrls[this.#state.stripUrl(url)];
47
- return accounts.filter(allAcc => auth.authorizedAccounts
48
- // we have a list, use it
49
- ? auth.authorizedAccounts.includes(allAcc.address)
50
- // if no authorizedAccounts and isAllowed return all - these are old converted urls
51
- : auth.isAllowed);
52
- }
53
- authorize(url, request) {
54
- return this.#state.authorizeUrl(url, request);
55
- }
56
- accountsListAuthorized(url, {
57
- anyType
58
- }) {
59
- const transformedAccounts = transformAccounts(accountsObservable.subject.getValue(), anyType);
60
- return this.filterForAuthorizedAccounts(transformedAccounts, url);
61
- }
62
- accountsSubscribeAuthorized(url, id, port) {
63
- const cb = createSubscription(id, port);
64
- this.#accountSubs[id] = {
65
- subscription: accountsObservable.subject.subscribe(accounts => {
66
- const transformedAccounts = transformAccounts(accounts);
67
- cb(this.filterForAuthorizedAccounts(transformedAccounts, url));
68
- }),
69
- url
70
- };
71
- port.onDisconnect.addListener(() => {
72
- this.accountsUnsubscribe(url, {
73
- id
74
- });
75
- });
76
- return id;
77
- }
78
- accountsUnsubscribe(url, {
79
- id
80
- }) {
81
- const sub = this.#accountSubs[id];
82
- if (!sub || sub.url !== url) {
83
- return false;
84
- }
85
- delete this.#accountSubs[id];
86
- unsubscribe(id);
87
- sub.subscription.unsubscribe();
88
- return true;
89
- }
90
- getSigningPair(address) {
91
- const pair = keyring.getPair(address);
92
- assert(pair, 'Unable to find keypair');
93
- return pair;
94
- }
95
- bytesSign(url, request) {
96
- const address = request.address;
97
- const pair = this.getSigningPair(address);
98
- return this.#state.sign(url, new RequestBytesSign(request), {
99
- address,
100
- ...pair.meta
101
- });
102
- }
103
- extrinsicSign(url, request) {
104
- const address = request.address;
105
- const pair = this.getSigningPair(address);
106
- return this.#state.sign(url, new RequestExtrinsicSign(request), {
107
- address,
108
- ...pair.meta
109
- });
110
- }
111
- metadataProvide(url, request) {
112
- return this.#state.injectMetadata(url, request);
113
- }
114
-
115
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
116
- metadataList(url) {
117
- return this.#state.knownMetadata.map(({
118
- genesisHash,
119
- specVersion
120
- }) => ({
121
- genesisHash,
122
- specVersion
123
- }));
124
- }
125
- rpcListProviders() {
126
- return this.#state.rpcListProviders();
127
- }
128
- rpcSend(request, port) {
129
- return this.#state.rpcSend(request, port);
130
- }
131
- rpcStartProvider(key, port) {
132
- return this.#state.rpcStartProvider(key, port);
133
- }
134
- async rpcSubscribe(request, id, port) {
135
- const innerCb = createSubscription(id, port);
136
- const cb = (_error, data) => innerCb(data);
137
- const subscriptionId = await this.#state.rpcSubscribe(request, cb, port);
138
- port.onDisconnect.addListener(() => {
139
- unsubscribe(id);
140
- withErrorLog(() => this.rpcUnsubscribe({
141
- ...request,
142
- subscriptionId
143
- }, port));
144
- });
145
- return true;
146
- }
147
- rpcSubscribeConnected(request, id, port) {
148
- const innerCb = createSubscription(id, port);
149
- const cb = (_error, data) => innerCb(data);
150
- this.#state.rpcSubscribeConnected(request, cb, port);
151
- port.onDisconnect.addListener(() => {
152
- unsubscribe(id);
153
- });
154
- return Promise.resolve(true);
155
- }
156
- async rpcUnsubscribe(request, port) {
157
- return this.#state.rpcUnsubscribe(request, port);
158
- }
159
- redirectPhishingLanding(phishingWebsite) {
160
- const nonFragment = phishingWebsite.split('#')[0];
161
- const encodedWebsite = encodeURIComponent(nonFragment);
162
- const url = `${chrome.extension.getURL('index.html')}#${PHISHING_PAGE_REDIRECT}/${encodedWebsite}`;
163
- chrome.tabs.query({
164
- url: nonFragment
165
- }, tabs => {
166
- tabs.map(({
167
- id
168
- }) => id).filter(id => isNumber(id)).forEach(id => withErrorLog(() => chrome.tabs.update(id, {
169
- url
170
- })));
171
- });
172
- }
173
- async redirectIfPhishing(url) {
174
- const isInDenyList = await checkIfDenied(url);
175
- if (isInDenyList) {
176
- this.redirectPhishingLanding(url);
177
- return true;
178
- }
179
- return false;
180
- }
181
- async handle(id, type, request, url, port) {
182
- if (type === 'pub(phishing.redirectIfDenied)') {
183
- return this.redirectIfPhishing(url);
184
- }
185
- if (type !== 'pub(authorize.tab)') {
186
- this.#state.ensureUrlAuthorized(url);
187
- }
188
- switch (type) {
189
- case 'pub(authorize.tab)':
190
- return this.authorize(url, request);
191
- case 'pub(accounts.list)':
192
- return this.accountsListAuthorized(url, request);
193
- case 'pub(accounts.subscribe)':
194
- return port && this.accountsSubscribeAuthorized(url, id, port);
195
- case 'pub(accounts.unsubscribe)':
196
- return this.accountsUnsubscribe(url, request);
197
- case 'pub(bytes.sign)':
198
- return this.bytesSign(url, request);
199
- case 'pub(extrinsic.sign)':
200
- return this.extrinsicSign(url, request);
201
- case 'pub(metadata.list)':
202
- return this.metadataList(url);
203
- case 'pub(metadata.provide)':
204
- return this.metadataProvide(url, request);
205
- case 'pub(ping)':
27
+ constructor(state) {
28
+ _Tabs_accountSubs.set(this, {});
29
+ _Tabs_state.set(this, void 0);
30
+ __classPrivateFieldSet(this, _Tabs_state, state, "f");
31
+ }
32
+ filterForAuthorizedAccounts(accounts, url) {
33
+ const auth = __classPrivateFieldGet(this, _Tabs_state, "f").authUrls[__classPrivateFieldGet(this, _Tabs_state, "f").stripUrl(url)];
34
+ return accounts.filter((allAcc) => auth.authorizedAccounts
35
+ // we have a list, use it
36
+ ? auth.authorizedAccounts.includes(allAcc.address)
37
+ // if no authorizedAccounts and isAllowed return all - these are old converted urls
38
+ : auth.isAllowed);
39
+ }
40
+ authorize(url, request) {
41
+ return __classPrivateFieldGet(this, _Tabs_state, "f").authorizeUrl(url, request);
42
+ }
43
+ accountsListAuthorized(url, { anyType }) {
44
+ const transformedAccounts = transformAccounts(accountsObservable.subject.getValue(), anyType);
45
+ return this.filterForAuthorizedAccounts(transformedAccounts, url);
46
+ }
47
+ accountsSubscribeAuthorized(url, id, port) {
48
+ const cb = createSubscription(id, port);
49
+ __classPrivateFieldGet(this, _Tabs_accountSubs, "f")[id] = {
50
+ subscription: accountsObservable.subject.subscribe((accounts) => {
51
+ const transformedAccounts = transformAccounts(accounts);
52
+ cb(this.filterForAuthorizedAccounts(transformedAccounts, url));
53
+ }),
54
+ url
55
+ };
56
+ port.onDisconnect.addListener(() => {
57
+ this.accountsUnsubscribe(url, { id });
58
+ });
59
+ return id;
60
+ }
61
+ accountsUnsubscribe(url, { id }) {
62
+ const sub = __classPrivateFieldGet(this, _Tabs_accountSubs, "f")[id];
63
+ if (!sub || sub.url !== url) {
64
+ return false;
65
+ }
66
+ delete __classPrivateFieldGet(this, _Tabs_accountSubs, "f")[id];
67
+ unsubscribe(id);
68
+ sub.subscription.unsubscribe();
69
+ return true;
70
+ }
71
+ getSigningPair(address) {
72
+ const pair = keyring.getPair(address);
73
+ assert(pair, 'Unable to find keypair');
74
+ return pair;
75
+ }
76
+ bytesSign(url, request) {
77
+ const address = request.address;
78
+ const pair = this.getSigningPair(address);
79
+ return __classPrivateFieldGet(this, _Tabs_state, "f").sign(url, new RequestBytesSign(request), { address, ...pair.meta });
80
+ }
81
+ extrinsicSign(url, request) {
82
+ const address = request.address;
83
+ const pair = this.getSigningPair(address);
84
+ return __classPrivateFieldGet(this, _Tabs_state, "f").sign(url, new RequestExtrinsicSign(request), { address, ...pair.meta });
85
+ }
86
+ metadataProvide(url, request) {
87
+ return __classPrivateFieldGet(this, _Tabs_state, "f").injectMetadata(url, request);
88
+ }
89
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
90
+ metadataList(url) {
91
+ return __classPrivateFieldGet(this, _Tabs_state, "f").knownMetadata.map(({ genesisHash, specVersion }) => ({
92
+ genesisHash,
93
+ specVersion
94
+ }));
95
+ }
96
+ rpcListProviders() {
97
+ return __classPrivateFieldGet(this, _Tabs_state, "f").rpcListProviders();
98
+ }
99
+ rpcSend(request, port) {
100
+ return __classPrivateFieldGet(this, _Tabs_state, "f").rpcSend(request, port);
101
+ }
102
+ rpcStartProvider(key, port) {
103
+ return __classPrivateFieldGet(this, _Tabs_state, "f").rpcStartProvider(key, port);
104
+ }
105
+ async rpcSubscribe(request, id, port) {
106
+ const innerCb = createSubscription(id, port);
107
+ const cb = (_error, data) => innerCb(data);
108
+ const subscriptionId = await __classPrivateFieldGet(this, _Tabs_state, "f").rpcSubscribe(request, cb, port);
109
+ port.onDisconnect.addListener(() => {
110
+ unsubscribe(id);
111
+ withErrorLog(() => this.rpcUnsubscribe({ ...request, subscriptionId }, port));
112
+ });
113
+ return true;
114
+ }
115
+ rpcSubscribeConnected(request, id, port) {
116
+ const innerCb = createSubscription(id, port);
117
+ const cb = (_error, data) => innerCb(data);
118
+ __classPrivateFieldGet(this, _Tabs_state, "f").rpcSubscribeConnected(request, cb, port);
119
+ port.onDisconnect.addListener(() => {
120
+ unsubscribe(id);
121
+ });
206
122
  return Promise.resolve(true);
207
- case 'pub(rpc.listProviders)':
208
- return this.rpcListProviders();
209
- case 'pub(rpc.send)':
210
- return port && this.rpcSend(request, port);
211
- case 'pub(rpc.startProvider)':
212
- return port && this.rpcStartProvider(request, port);
213
- case 'pub(rpc.subscribe)':
214
- return port && this.rpcSubscribe(request, id, port);
215
- case 'pub(rpc.subscribeConnected)':
216
- return port && this.rpcSubscribeConnected(request, id, port);
217
- case 'pub(rpc.unsubscribe)':
218
- return port && this.rpcUnsubscribe(request, port);
219
- default:
220
- throw new Error(`Unable to handle message of type ${type}`);
221
- }
222
- }
223
- }
123
+ }
124
+ async rpcUnsubscribe(request, port) {
125
+ return __classPrivateFieldGet(this, _Tabs_state, "f").rpcUnsubscribe(request, port);
126
+ }
127
+ redirectPhishingLanding(phishingWebsite) {
128
+ const nonFragment = phishingWebsite.split('#')[0];
129
+ const encodedWebsite = encodeURIComponent(nonFragment);
130
+ const url = `${chrome.extension.getURL('index.html')}#${PHISHING_PAGE_REDIRECT}/${encodedWebsite}`;
131
+ chrome.tabs.query({ url: nonFragment }, (tabs) => {
132
+ tabs
133
+ .map(({ id }) => id)
134
+ .filter((id) => isNumber(id))
135
+ .forEach((id) => withErrorLog(() => chrome.tabs.update(id, { url })));
136
+ });
137
+ }
138
+ async redirectIfPhishing(url) {
139
+ const isInDenyList = await checkIfDenied(url);
140
+ if (isInDenyList) {
141
+ this.redirectPhishingLanding(url);
142
+ return true;
143
+ }
144
+ return false;
145
+ }
146
+ async handle(id, type, request, url, port) {
147
+ if (type === 'pub(phishing.redirectIfDenied)') {
148
+ return this.redirectIfPhishing(url);
149
+ }
150
+ if (type !== 'pub(authorize.tab)') {
151
+ __classPrivateFieldGet(this, _Tabs_state, "f").ensureUrlAuthorized(url);
152
+ }
153
+ switch (type) {
154
+ case 'pub(authorize.tab)':
155
+ return this.authorize(url, request);
156
+ case 'pub(accounts.list)':
157
+ return this.accountsListAuthorized(url, request);
158
+ case 'pub(accounts.subscribe)':
159
+ return port && this.accountsSubscribeAuthorized(url, id, port);
160
+ case 'pub(accounts.unsubscribe)':
161
+ return this.accountsUnsubscribe(url, request);
162
+ case 'pub(bytes.sign)':
163
+ return this.bytesSign(url, request);
164
+ case 'pub(extrinsic.sign)':
165
+ return this.extrinsicSign(url, request);
166
+ case 'pub(metadata.list)':
167
+ return this.metadataList(url);
168
+ case 'pub(metadata.provide)':
169
+ return this.metadataProvide(url, request);
170
+ case 'pub(ping)':
171
+ return Promise.resolve(true);
172
+ case 'pub(rpc.listProviders)':
173
+ return this.rpcListProviders();
174
+ case 'pub(rpc.send)':
175
+ return port && this.rpcSend(request, port);
176
+ case 'pub(rpc.startProvider)':
177
+ return port && this.rpcStartProvider(request, port);
178
+ case 'pub(rpc.subscribe)':
179
+ return port && this.rpcSubscribe(request, id, port);
180
+ case 'pub(rpc.subscribeConnected)':
181
+ return port && this.rpcSubscribeConnected(request, id, port);
182
+ case 'pub(rpc.unsubscribe)':
183
+ return port && this.rpcUnsubscribe(request, port);
184
+ default:
185
+ throw new Error(`Unable to handle message of type ${type}`);
186
+ }
187
+ }
188
+ }
189
+ _Tabs_accountSubs = new WeakMap(), _Tabs_state = new WeakMap();
@@ -1,13 +1,11 @@
1
- // Copyright 2019-2023 @polkadot/extension authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
1
  export function withErrorLog(fn) {
5
- try {
6
- const p = fn();
7
- if (p && typeof p === 'object' && typeof p.catch === 'function') {
8
- p.catch(console.error);
2
+ try {
3
+ const p = fn();
4
+ if (p && typeof p === 'object' && typeof p.catch === 'function') {
5
+ p.catch(console.error);
6
+ }
7
+ }
8
+ catch (e) {
9
+ console.error(e);
9
10
  }
10
- } catch (e) {
11
- console.error(e);
12
- }
13
11
  }
@@ -1,3 +1,3 @@
1
1
  /// <reference types="chrome" />
2
- import type { MessageTypes, TransportRequestMessage } from '../types';
2
+ import type { MessageTypes, TransportRequestMessage } from '../types.js';
3
3
  export default function handler<TMessageType extends MessageTypes>({ id, message, request }: TransportRequestMessage<TMessageType>, port?: chrome.runtime.Port, extensionPortName?: string): void;
@@ -1,45 +1,35 @@
1
- // Copyright 2019-2023 @polkadot/extension authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
1
  import { assert } from '@polkadot/util';
5
- import { PORT_EXTENSION } from "../../defaults.js";
6
- import Extension from "./Extension.js";
7
- import State from "./State.js";
8
- import Tabs from "./Tabs.js";
2
+ import { PORT_EXTENSION } from '../../defaults.js';
3
+ import Extension from './Extension.js';
4
+ import State from './State.js';
5
+ import Tabs from './Tabs.js';
9
6
  const state = new State();
10
7
  const extension = new Extension(state);
11
8
  const tabs = new Tabs(state);
12
- export default function handler({
13
- id,
14
- message,
15
- request
16
- }, port, extensionPortName = PORT_EXTENSION) {
17
- const isExtension = !port || (port == null ? void 0 : port.name) === extensionPortName;
18
- const sender = port == null ? void 0 : port.sender;
19
- const from = isExtension ? 'extension' : sender.tab && sender.tab.url || sender.url || '<unknown>';
20
- const source = `${from}: ${id}: ${message}`;
21
- console.log(` [in] ${source}`); // :: ${JSON.stringify(request)}`);
22
-
23
- const promise = isExtension ? extension.handle(id, message, request, port) : tabs.handle(id, message, request, from, port);
24
- promise.then(response => {
25
- console.log(`[out] ${source}`); // :: ${JSON.stringify(response)}`);
26
-
27
- // between the start and the end of the promise, the user may have closed
28
- // the tab, in which case port will be undefined
29
- assert(port, 'Port has been disconnected');
30
- port.postMessage({
31
- id,
32
- response
9
+ export default function handler({ id, message, request }, port, extensionPortName = PORT_EXTENSION) {
10
+ const isExtension = !port || port?.name === extensionPortName;
11
+ const sender = port?.sender;
12
+ const from = isExtension
13
+ ? 'extension'
14
+ : (sender.tab && sender.tab.url) || sender.url || '<unknown>';
15
+ const source = `${from}: ${id}: ${message}`;
16
+ console.log(` [in] ${source}`); // :: ${JSON.stringify(request)}`);
17
+ const promise = isExtension
18
+ ? extension.handle(id, message, request, port)
19
+ : tabs.handle(id, message, request, from, port);
20
+ promise
21
+ .then((response) => {
22
+ console.log(`[out] ${source}`); // :: ${JSON.stringify(response)}`);
23
+ // between the start and the end of the promise, the user may have closed
24
+ // the tab, in which case port will be undefined
25
+ assert(port, 'Port has been disconnected');
26
+ port.postMessage({ id, response });
27
+ })
28
+ .catch((error) => {
29
+ console.log(`[err] ${source}:: ${error.message}`);
30
+ // only send message back to port if it's still connected
31
+ if (port) {
32
+ port.postMessage({ error: error.message, id });
33
+ }
33
34
  });
34
- }).catch(error => {
35
- console.log(`[err] ${source}:: ${error.message}`);
36
-
37
- // only send message back to port if it's still connected
38
- if (port) {
39
- port.postMessage({
40
- error: error.message,
41
- id
42
- });
43
- }
44
- });
45
35
  }
@@ -1,4 +1,4 @@
1
1
  /// <reference types="chrome" />
2
- import type { MessageTypesWithSubscriptions, SubscriptionMessageTypes } from '../types';
2
+ import type { MessageTypesWithSubscriptions, SubscriptionMessageTypes } from '../types.js';
3
3
  export declare function createSubscription<TMessageType extends MessageTypesWithSubscriptions>(id: string, port: chrome.runtime.Port): (data: SubscriptionMessageTypes[TMessageType]) => void;
4
4
  export declare function unsubscribe(id: string): void;
@@ -1,27 +1,18 @@
1
- // Copyright 2019-2023 @polkadot/extension authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
1
  const subscriptions = {};
5
-
6
- // return a subscription callback, that will send the data to the caller via the port
7
2
  export function createSubscription(id, port) {
8
- subscriptions[id] = port;
9
- return subscription => {
10
- if (subscriptions[id]) {
11
- port.postMessage({
12
- id,
13
- subscription
14
- });
15
- }
16
- };
3
+ subscriptions[id] = port;
4
+ return (subscription) => {
5
+ if (subscriptions[id]) {
6
+ port.postMessage({ id, subscription });
7
+ }
8
+ };
17
9
  }
18
-
19
- // clear a previous subscriber
20
10
  export function unsubscribe(id) {
21
- if (subscriptions[id]) {
22
- console.log(`Unsubscribing from ${id}`);
23
- delete subscriptions[id];
24
- } else {
25
- console.error(`Unable to unsubscribe from ${id}`);
26
- }
11
+ if (subscriptions[id]) {
12
+ console.log(`Unsubscribing from ${id}`);
13
+ delete subscriptions[id];
14
+ }
15
+ else {
16
+ console.error(`Unable to unsubscribe from ${id}`);
17
+ }
27
18
  }
@@ -5,8 +5,8 @@ import type { Registry, SignerPayloadJSON, SignerPayloadRaw } from '@polkadot/ty
5
5
  import type { KeyringPairs$Json } from '@polkadot/ui-keyring/types';
6
6
  import type { HexString } from '@polkadot/util/types';
7
7
  import type { KeypairType } from '@polkadot/util-crypto/types';
8
- import { ALLOWED_PATH } from '../defaults';
9
- import { AuthResponse, AuthUrls } from './handlers/State';
8
+ import { ALLOWED_PATH } from '../defaults.js';
9
+ import { AuthResponse, AuthUrls } from './handlers/State.js';
10
10
  type KeysWithDefinedValues<T> = {
11
11
  [K in keyof T]: T[K] extends undefined ? never : K;
12
12
  }[keyof T];
package/bundle.d.ts CHANGED
@@ -1 +1 @@
1
- export { packageInfo } from './packageInfo';
1
+ export { packageInfo } from './packageInfo.js';
package/bundle.js CHANGED
@@ -1,4 +1 @@
1
- // Copyright 2019-2023 @polkadot/extension-base authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- export { packageInfo } from "./packageInfo.js";
1
+ export { packageInfo } from './packageInfo.js';
@@ -1,22 +1,15 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _wrapBytes = require("@polkadot/extension-dapp/wrapBytes");
8
- var _util = require("@polkadot/util");
9
- // Copyright 2019-2023 @polkadot/extension authors & contributors
10
- // SPDX-License-Identifier: Apache-2.0
11
-
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const wrapBytes_1 = require("@polkadot/extension-dapp/wrapBytes");
4
+ const util_1 = require("@polkadot/util");
12
5
  class RequestBytesSign {
13
- constructor(payload) {
14
- this.payload = payload;
15
- }
16
- sign(_registry, pair) {
17
- return {
18
- signature: (0, _util.u8aToHex)(pair.sign((0, _wrapBytes.wrapBytes)(this.payload.data)))
19
- };
20
- }
6
+ constructor(payload) {
7
+ this.payload = payload;
8
+ }
9
+ sign(_registry, pair) {
10
+ return {
11
+ signature: (0, util_1.u8aToHex)(pair.sign((0, wrapBytes_1.wrapBytes)(this.payload.data)))
12
+ };
13
+ }
21
14
  }
22
15
  exports.default = RequestBytesSign;