@polkadot/extension-base 0.46.1 → 0.46.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.
- package/background/handlers/Extension.js +31 -36
- package/background/handlers/State.js +62 -67
- package/background/handlers/Tabs.js +18 -22
- package/cjs/background/handlers/Extension.js +31 -35
- package/cjs/background/handlers/State.js +62 -66
- package/cjs/background/handlers/Tabs.js +18 -21
- package/cjs/packageInfo.js +1 -1
- package/cjs/page/PostMessageProvider.js +14 -18
- package/cjs/stores/Base.js +14 -18
- package/package.json +15 -15
- package/packageInfo.js +1 -1
- package/page/PostMessageProvider.js +14 -18
- package/stores/Base.d.ts +3 -3
- package/stores/Base.js +14 -18
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
var _Extension_cachedUnlocks, _Extension_state;
|
|
2
|
-
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
|
3
1
|
import { ALLOWED_PATH, PASSWORD_EXPIRY_MS } from '@polkadot/extension-base/defaults';
|
|
4
2
|
import { metadataExpand } from '@polkadot/extension-chains';
|
|
5
3
|
import { TypeRegistry } from '@polkadot/types';
|
|
@@ -22,15 +20,13 @@ function isJsonPayload(value) {
|
|
|
22
20
|
}
|
|
23
21
|
export default class Extension {
|
|
24
22
|
constructor(state) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
__classPrivateFieldSet(this, _Extension_cachedUnlocks, {}, "f");
|
|
28
|
-
__classPrivateFieldSet(this, _Extension_state, state, "f");
|
|
23
|
+
this.__internal__cachedUnlocks = {};
|
|
24
|
+
this.__internal__state = state;
|
|
29
25
|
}
|
|
30
26
|
transformAccounts(accounts) {
|
|
31
27
|
return Object.values(accounts).map(({ json: { address, meta }, type }) => ({
|
|
32
28
|
address,
|
|
33
|
-
isDefaultAuthSelected:
|
|
29
|
+
isDefaultAuthSelected: this.__internal__state.defaultAuthAccountSelection.includes(address),
|
|
34
30
|
...meta,
|
|
35
31
|
type
|
|
36
32
|
}));
|
|
@@ -79,25 +75,25 @@ export default class Extension {
|
|
|
79
75
|
accountsForget({ address }) {
|
|
80
76
|
const authorizedAccountsDiff = [];
|
|
81
77
|
// cycle through authUrls and prepare the array of diff
|
|
82
|
-
Object.entries(
|
|
78
|
+
Object.entries(this.__internal__state.authUrls).forEach(([url, urlInfo]) => {
|
|
83
79
|
if (!urlInfo.authorizedAccounts.includes(address)) {
|
|
84
80
|
return;
|
|
85
81
|
}
|
|
86
82
|
authorizedAccountsDiff.push([url, urlInfo.authorizedAccounts.filter((previousAddress) => previousAddress !== address)]);
|
|
87
83
|
});
|
|
88
|
-
|
|
84
|
+
this.__internal__state.updateAuthorizedAccounts(authorizedAccountsDiff);
|
|
89
85
|
// cycle through default account selection for auth and remove any occurence of the account
|
|
90
|
-
const newDefaultAuthAccounts =
|
|
91
|
-
|
|
86
|
+
const newDefaultAuthAccounts = this.__internal__state.defaultAuthAccountSelection.filter((defaultSelectionAddress) => defaultSelectionAddress !== address);
|
|
87
|
+
this.__internal__state.updateDefaultAuthAccounts(newDefaultAuthAccounts);
|
|
92
88
|
keyring.forgetAccount(address);
|
|
93
89
|
return true;
|
|
94
90
|
}
|
|
95
91
|
refreshAccountPasswordCache(pair) {
|
|
96
92
|
const { address } = pair;
|
|
97
|
-
const savedExpiry =
|
|
93
|
+
const savedExpiry = this.__internal__cachedUnlocks[address] || 0;
|
|
98
94
|
const remainingTime = savedExpiry - Date.now();
|
|
99
95
|
if (remainingTime < 0) {
|
|
100
|
-
|
|
96
|
+
this.__internal__cachedUnlocks[address] = 0;
|
|
101
97
|
pair.lock();
|
|
102
98
|
return 0;
|
|
103
99
|
}
|
|
@@ -134,22 +130,22 @@ export default class Extension {
|
|
|
134
130
|
return true;
|
|
135
131
|
}
|
|
136
132
|
authorizeApprove({ authorizedAccounts, id }) {
|
|
137
|
-
const queued =
|
|
133
|
+
const queued = this.__internal__state.getAuthRequest(id);
|
|
138
134
|
assert(queued, 'Unable to find request');
|
|
139
135
|
const { resolve } = queued;
|
|
140
136
|
resolve({ authorizedAccounts, result: true });
|
|
141
137
|
return true;
|
|
142
138
|
}
|
|
143
139
|
authorizeUpdate({ authorizedAccounts, url }) {
|
|
144
|
-
return
|
|
140
|
+
return this.__internal__state.updateAuthorizedAccounts([[url, authorizedAccounts]]);
|
|
145
141
|
}
|
|
146
142
|
getAuthList() {
|
|
147
|
-
return { list:
|
|
143
|
+
return { list: this.__internal__state.authUrls };
|
|
148
144
|
}
|
|
149
145
|
// FIXME This looks very much like what we have in accounts
|
|
150
146
|
authorizeSubscribe(id, port) {
|
|
151
147
|
const cb = createSubscription(id, port);
|
|
152
|
-
const subscription =
|
|
148
|
+
const subscription = this.__internal__state.authSubject.subscribe((requests) => cb(requests));
|
|
153
149
|
port.onDisconnect.addListener(() => {
|
|
154
150
|
unsubscribe(id);
|
|
155
151
|
subscription.unsubscribe();
|
|
@@ -157,21 +153,21 @@ export default class Extension {
|
|
|
157
153
|
return true;
|
|
158
154
|
}
|
|
159
155
|
metadataApprove({ id }) {
|
|
160
|
-
const queued =
|
|
156
|
+
const queued = this.__internal__state.getMetaRequest(id);
|
|
161
157
|
assert(queued, 'Unable to find request');
|
|
162
158
|
const { request, resolve } = queued;
|
|
163
|
-
|
|
159
|
+
this.__internal__state.saveMetadata(request);
|
|
164
160
|
resolve(true);
|
|
165
161
|
return true;
|
|
166
162
|
}
|
|
167
163
|
metadataGet(genesisHash) {
|
|
168
|
-
return
|
|
164
|
+
return this.__internal__state.knownMetadata.find((result) => result.genesisHash === genesisHash) || null;
|
|
169
165
|
}
|
|
170
166
|
metadataList() {
|
|
171
|
-
return
|
|
167
|
+
return this.__internal__state.knownMetadata;
|
|
172
168
|
}
|
|
173
169
|
metadataReject({ id }) {
|
|
174
|
-
const queued =
|
|
170
|
+
const queued = this.__internal__state.getMetaRequest(id);
|
|
175
171
|
assert(queued, 'Unable to find request');
|
|
176
172
|
const { reject } = queued;
|
|
177
173
|
reject(new Error('Rejected'));
|
|
@@ -179,7 +175,7 @@ export default class Extension {
|
|
|
179
175
|
}
|
|
180
176
|
metadataSubscribe(id, port) {
|
|
181
177
|
const cb = createSubscription(id, port);
|
|
182
|
-
const subscription =
|
|
178
|
+
const subscription = this.__internal__state.metaSubject.subscribe((requests) => cb(requests));
|
|
183
179
|
port.onDisconnect.addListener(() => {
|
|
184
180
|
unsubscribe(id);
|
|
185
181
|
subscription.unsubscribe();
|
|
@@ -240,7 +236,7 @@ export default class Extension {
|
|
|
240
236
|
};
|
|
241
237
|
}
|
|
242
238
|
signingApprovePassword({ id, password, savePass }) {
|
|
243
|
-
const queued =
|
|
239
|
+
const queued = this.__internal__state.getSignRequest(id);
|
|
244
240
|
assert(queued, 'Unable to find request');
|
|
245
241
|
const { reject, request, resolve } = queued;
|
|
246
242
|
const pair = keyring.getPair(queued.account.address);
|
|
@@ -261,7 +257,7 @@ export default class Extension {
|
|
|
261
257
|
const { payload } = request;
|
|
262
258
|
if (isJsonPayload(payload)) {
|
|
263
259
|
// Get the metadata for the genesisHash
|
|
264
|
-
const metadata =
|
|
260
|
+
const metadata = this.__internal__state.knownMetadata.find(({ genesisHash }) => genesisHash === payload.genesisHash);
|
|
265
261
|
if (metadata) {
|
|
266
262
|
// we have metadata, expand it and extract the info/registry
|
|
267
263
|
const expanded = metadataExpand(metadata, false);
|
|
@@ -283,7 +279,7 @@ export default class Extension {
|
|
|
283
279
|
// unlike queued.account.address the following
|
|
284
280
|
// address is encoded with the default prefix
|
|
285
281
|
// which what is used for password caching mapping
|
|
286
|
-
|
|
282
|
+
this.__internal__cachedUnlocks[pair.address] = Date.now() + PASSWORD_EXPIRY_MS;
|
|
287
283
|
}
|
|
288
284
|
else {
|
|
289
285
|
pair.lock();
|
|
@@ -295,21 +291,21 @@ export default class Extension {
|
|
|
295
291
|
return true;
|
|
296
292
|
}
|
|
297
293
|
signingApproveSignature({ id, signature }) {
|
|
298
|
-
const queued =
|
|
294
|
+
const queued = this.__internal__state.getSignRequest(id);
|
|
299
295
|
assert(queued, 'Unable to find request');
|
|
300
296
|
const { resolve } = queued;
|
|
301
297
|
resolve({ id, signature });
|
|
302
298
|
return true;
|
|
303
299
|
}
|
|
304
300
|
signingCancel({ id }) {
|
|
305
|
-
const queued =
|
|
301
|
+
const queued = this.__internal__state.getSignRequest(id);
|
|
306
302
|
assert(queued, 'Unable to find request');
|
|
307
303
|
const { reject } = queued;
|
|
308
304
|
reject(new Error('Cancelled'));
|
|
309
305
|
return true;
|
|
310
306
|
}
|
|
311
307
|
signingIsLocked({ id }) {
|
|
312
|
-
const queued =
|
|
308
|
+
const queued = this.__internal__state.getSignRequest(id);
|
|
313
309
|
assert(queued, 'Unable to find request');
|
|
314
310
|
const address = queued.request.payload.address;
|
|
315
311
|
const pair = keyring.getPair(address);
|
|
@@ -323,7 +319,7 @@ export default class Extension {
|
|
|
323
319
|
// FIXME This looks very much like what we have in authorization
|
|
324
320
|
signingSubscribe(id, port) {
|
|
325
321
|
const cb = createSubscription(id, port);
|
|
326
|
-
const subscription =
|
|
322
|
+
const subscription = this.__internal__state.signSubject.subscribe((requests) => cb(requests));
|
|
327
323
|
port.onDisconnect.addListener(() => {
|
|
328
324
|
unsubscribe(id);
|
|
329
325
|
subscription.unsubscribe();
|
|
@@ -372,16 +368,16 @@ export default class Extension {
|
|
|
372
368
|
return true;
|
|
373
369
|
}
|
|
374
370
|
removeAuthorization(url) {
|
|
375
|
-
return { list:
|
|
371
|
+
return { list: this.__internal__state.removeAuthorization(url) };
|
|
376
372
|
}
|
|
377
373
|
deleteAuthRequest(requestId) {
|
|
378
|
-
return
|
|
374
|
+
return this.__internal__state.deleteAuthRequest(requestId);
|
|
379
375
|
}
|
|
380
376
|
updateCurrentTabs({ urls }) {
|
|
381
|
-
|
|
377
|
+
this.__internal__state.udateCurrentTabsUrl(urls);
|
|
382
378
|
}
|
|
383
379
|
getConnectedTabsUrl() {
|
|
384
|
-
return
|
|
380
|
+
return this.__internal__state.getConnectedTabsUrl();
|
|
385
381
|
}
|
|
386
382
|
// Weird thought, the eslint override is not needed in Tabs
|
|
387
383
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
@@ -454,7 +450,7 @@ export default class Extension {
|
|
|
454
450
|
case 'pri(seed.validate)':
|
|
455
451
|
return this.seedValidate(request);
|
|
456
452
|
case 'pri(settings.notification)':
|
|
457
|
-
return
|
|
453
|
+
return this.__internal__state.setNotification(request);
|
|
458
454
|
case 'pri(signing.approve.password)':
|
|
459
455
|
return this.signingApprovePassword(request);
|
|
460
456
|
case 'pri(signing.approve.signature)':
|
|
@@ -472,4 +468,3 @@ export default class Extension {
|
|
|
472
468
|
}
|
|
473
469
|
}
|
|
474
470
|
}
|
|
475
|
-
_Extension_cachedUnlocks = new WeakMap(), _Extension_state = new WeakMap();
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
var _State_authUrls, _State_authRequests, _State_metaStore, _State_injectedProviders, _State_metaRequests, _State_notification, _State_providers, _State_signRequests, _State_windows, _State_connectedTabsUrl;
|
|
2
|
-
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
|
3
1
|
import { BehaviorSubject } from 'rxjs';
|
|
4
2
|
import { addMetadata, knownMetadata } from '@polkadot/extension-chains';
|
|
5
3
|
import { knownGenesis } from '@polkadot/networks/defaults';
|
|
@@ -64,26 +62,24 @@ function extractMetadata(store) {
|
|
|
64
62
|
}
|
|
65
63
|
export default class State {
|
|
66
64
|
constructor(providers = {}) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
this.__internal__authUrls = {};
|
|
66
|
+
this.__internal__authRequests = {};
|
|
67
|
+
this.__internal__metaStore = new MetadataStore();
|
|
70
68
|
// Map of providers currently injected in tabs
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
_State_windows.set(this, []);
|
|
78
|
-
_State_connectedTabsUrl.set(this, []);
|
|
69
|
+
this.__internal__injectedProviders = new Map();
|
|
70
|
+
this.__internal__metaRequests = {};
|
|
71
|
+
this.__internal__notification = settings.notification;
|
|
72
|
+
this.__internal__signRequests = {};
|
|
73
|
+
this.__internal__windows = [];
|
|
74
|
+
this.__internal__connectedTabsUrl = [];
|
|
79
75
|
this.authSubject = new BehaviorSubject([]);
|
|
80
76
|
this.metaSubject = new BehaviorSubject([]);
|
|
81
77
|
this.signSubject = new BehaviorSubject([]);
|
|
82
78
|
this.defaultAuthAccountSelection = [];
|
|
83
79
|
this.authComplete = (id, resolve, reject) => {
|
|
84
80
|
const complete = (authorizedAccounts = []) => {
|
|
85
|
-
const { idStr, request: { origin }, url } =
|
|
86
|
-
|
|
81
|
+
const { idStr, request: { origin }, url } = this.__internal__authRequests[id];
|
|
82
|
+
this.__internal__authUrls[this.stripUrl(url)] = {
|
|
87
83
|
authorizedAccounts,
|
|
88
84
|
count: 0,
|
|
89
85
|
id: idStr,
|
|
@@ -92,7 +88,7 @@ export default class State {
|
|
|
92
88
|
};
|
|
93
89
|
this.saveCurrentAuthList();
|
|
94
90
|
this.updateDefaultAuthAccounts(authorizedAccounts);
|
|
95
|
-
delete
|
|
91
|
+
delete this.__internal__authRequests[id];
|
|
96
92
|
this.updateIconAuth(true);
|
|
97
93
|
};
|
|
98
94
|
return {
|
|
@@ -108,7 +104,7 @@ export default class State {
|
|
|
108
104
|
};
|
|
109
105
|
this.metaComplete = (id, resolve, reject) => {
|
|
110
106
|
const complete = () => {
|
|
111
|
-
delete
|
|
107
|
+
delete this.__internal__metaRequests[id];
|
|
112
108
|
this.updateIconMeta(true);
|
|
113
109
|
};
|
|
114
110
|
return {
|
|
@@ -124,7 +120,7 @@ export default class State {
|
|
|
124
120
|
};
|
|
125
121
|
this.signComplete = (id, resolve, reject) => {
|
|
126
122
|
const complete = () => {
|
|
127
|
-
delete
|
|
123
|
+
delete this.__internal__signRequests[id];
|
|
128
124
|
this.updateIconSign(true);
|
|
129
125
|
};
|
|
130
126
|
return {
|
|
@@ -138,12 +134,12 @@ export default class State {
|
|
|
138
134
|
}
|
|
139
135
|
};
|
|
140
136
|
};
|
|
141
|
-
|
|
142
|
-
extractMetadata(
|
|
137
|
+
this.__internal__providers = providers;
|
|
138
|
+
extractMetadata(this.__internal__metaStore);
|
|
143
139
|
// retrieve previously set authorizations
|
|
144
140
|
const authString = localStorage.getItem(AUTH_URLS_KEY) || '{}';
|
|
145
141
|
const previousAuth = JSON.parse(authString);
|
|
146
|
-
|
|
142
|
+
this.__internal__authUrls = previousAuth;
|
|
147
143
|
// retrieve previously set default auth accounts
|
|
148
144
|
const defaultAuthString = localStorage.getItem(DEFAULT_AUTH_ACCOUNTS) || '[]';
|
|
149
145
|
const previousDefaultAuth = JSON.parse(defaultAuthString);
|
|
@@ -153,43 +149,43 @@ export default class State {
|
|
|
153
149
|
return knownMetadata();
|
|
154
150
|
}
|
|
155
151
|
get numAuthRequests() {
|
|
156
|
-
return Object.keys(
|
|
152
|
+
return Object.keys(this.__internal__authRequests).length;
|
|
157
153
|
}
|
|
158
154
|
get numMetaRequests() {
|
|
159
|
-
return Object.keys(
|
|
155
|
+
return Object.keys(this.__internal__metaRequests).length;
|
|
160
156
|
}
|
|
161
157
|
get numSignRequests() {
|
|
162
|
-
return Object.keys(
|
|
158
|
+
return Object.keys(this.__internal__signRequests).length;
|
|
163
159
|
}
|
|
164
160
|
get allAuthRequests() {
|
|
165
161
|
return Object
|
|
166
|
-
.values(
|
|
162
|
+
.values(this.__internal__authRequests)
|
|
167
163
|
.map(({ id, request, url }) => ({ id, request, url }));
|
|
168
164
|
}
|
|
169
165
|
get allMetaRequests() {
|
|
170
166
|
return Object
|
|
171
|
-
.values(
|
|
167
|
+
.values(this.__internal__metaRequests)
|
|
172
168
|
.map(({ id, request, url }) => ({ id, request, url }));
|
|
173
169
|
}
|
|
174
170
|
get allSignRequests() {
|
|
175
171
|
return Object
|
|
176
|
-
.values(
|
|
172
|
+
.values(this.__internal__signRequests)
|
|
177
173
|
.map(({ account, id, request, url }) => ({ account, id, request, url }));
|
|
178
174
|
}
|
|
179
175
|
get authUrls() {
|
|
180
|
-
return
|
|
176
|
+
return this.__internal__authUrls;
|
|
181
177
|
}
|
|
182
178
|
popupClose() {
|
|
183
|
-
|
|
184
|
-
|
|
179
|
+
this.__internal__windows.forEach((id) => withErrorLog(() => chrome.windows.remove(id)));
|
|
180
|
+
this.__internal__windows = [];
|
|
185
181
|
}
|
|
186
182
|
popupOpen() {
|
|
187
|
-
|
|
188
|
-
chrome.windows.create(
|
|
183
|
+
this.__internal__notification !== 'extension' &&
|
|
184
|
+
chrome.windows.create(this.__internal__notification === 'window'
|
|
189
185
|
? NORMAL_WINDOW_OPTS
|
|
190
186
|
: POPUP_WINDOW_OPTS, (window) => {
|
|
191
187
|
if (window) {
|
|
192
|
-
|
|
188
|
+
this.__internal__windows.push(window.id || 0);
|
|
193
189
|
}
|
|
194
190
|
});
|
|
195
191
|
}
|
|
@@ -209,17 +205,17 @@ export default class State {
|
|
|
209
205
|
: undefined;
|
|
210
206
|
})
|
|
211
207
|
.filter((value) => !!value);
|
|
212
|
-
|
|
208
|
+
this.__internal__connectedTabsUrl = connectedTabs;
|
|
213
209
|
}
|
|
214
210
|
getConnectedTabsUrl() {
|
|
215
|
-
return
|
|
211
|
+
return this.__internal__connectedTabsUrl;
|
|
216
212
|
}
|
|
217
213
|
deleteAuthRequest(requestId) {
|
|
218
|
-
delete
|
|
214
|
+
delete this.__internal__authRequests[requestId];
|
|
219
215
|
this.updateIconAuth(true);
|
|
220
216
|
}
|
|
221
217
|
saveCurrentAuthList() {
|
|
222
|
-
localStorage.setItem(AUTH_URLS_KEY, JSON.stringify(
|
|
218
|
+
localStorage.setItem(AUTH_URLS_KEY, JSON.stringify(this.__internal__authUrls));
|
|
223
219
|
}
|
|
224
220
|
saveDefaultAuthAccounts() {
|
|
225
221
|
localStorage.setItem(DEFAULT_AUTH_ACCOUNTS, JSON.stringify(this.defaultAuthAccountSelection));
|
|
@@ -248,11 +244,11 @@ export default class State {
|
|
|
248
244
|
}
|
|
249
245
|
}
|
|
250
246
|
removeAuthorization(url) {
|
|
251
|
-
const entry =
|
|
247
|
+
const entry = this.__internal__authUrls[url];
|
|
252
248
|
assert(entry, `The source ${url} is not known`);
|
|
253
|
-
delete
|
|
249
|
+
delete this.__internal__authUrls[url];
|
|
254
250
|
this.saveCurrentAuthList();
|
|
255
|
-
return
|
|
251
|
+
return this.__internal__authUrls;
|
|
256
252
|
}
|
|
257
253
|
updateIconAuth(shouldClose) {
|
|
258
254
|
this.authSubject.next(this.allAuthRequests);
|
|
@@ -268,7 +264,7 @@ export default class State {
|
|
|
268
264
|
}
|
|
269
265
|
updateAuthorizedAccounts(authorizedAccountDiff) {
|
|
270
266
|
authorizedAccountDiff.forEach(([url, authorizedAccountDiff]) => {
|
|
271
|
-
|
|
267
|
+
this.__internal__authUrls[url].authorizedAccounts = authorizedAccountDiff;
|
|
272
268
|
});
|
|
273
269
|
this.saveCurrentAuthList();
|
|
274
270
|
}
|
|
@@ -276,12 +272,12 @@ export default class State {
|
|
|
276
272
|
const idStr = this.stripUrl(url);
|
|
277
273
|
// Do not enqueue duplicate authorization requests.
|
|
278
274
|
const isDuplicate = Object
|
|
279
|
-
.values(
|
|
275
|
+
.values(this.__internal__authRequests)
|
|
280
276
|
.some((request) => request.idStr === idStr);
|
|
281
277
|
assert(!isDuplicate, `The source ${url} has a pending authorization request`);
|
|
282
|
-
if (
|
|
278
|
+
if (this.__internal__authUrls[idStr]) {
|
|
283
279
|
// this url was seen in the past
|
|
284
|
-
assert(
|
|
280
|
+
assert(this.__internal__authUrls[idStr].authorizedAccounts || this.__internal__authUrls[idStr].isAllowed, `The source ${url} is not allowed to interact with this extension`);
|
|
285
281
|
return {
|
|
286
282
|
authorizedAccounts: [],
|
|
287
283
|
result: false
|
|
@@ -289,7 +285,7 @@ export default class State {
|
|
|
289
285
|
}
|
|
290
286
|
return new Promise((resolve, reject) => {
|
|
291
287
|
const id = getId();
|
|
292
|
-
|
|
288
|
+
this.__internal__authRequests[id] = {
|
|
293
289
|
...this.authComplete(id, resolve, reject),
|
|
294
290
|
id,
|
|
295
291
|
idStr,
|
|
@@ -301,14 +297,14 @@ export default class State {
|
|
|
301
297
|
});
|
|
302
298
|
}
|
|
303
299
|
ensureUrlAuthorized(url) {
|
|
304
|
-
const entry =
|
|
300
|
+
const entry = this.__internal__authUrls[this.stripUrl(url)];
|
|
305
301
|
assert(entry, `The source ${url} has not been enabled yet`);
|
|
306
302
|
return true;
|
|
307
303
|
}
|
|
308
304
|
injectMetadata(url, request) {
|
|
309
305
|
return new Promise((resolve, reject) => {
|
|
310
306
|
const id = getId();
|
|
311
|
-
|
|
307
|
+
this.__internal__metaRequests[id] = {
|
|
312
308
|
...this.metaComplete(id, resolve, reject),
|
|
313
309
|
id,
|
|
314
310
|
request,
|
|
@@ -319,73 +315,73 @@ export default class State {
|
|
|
319
315
|
});
|
|
320
316
|
}
|
|
321
317
|
getAuthRequest(id) {
|
|
322
|
-
return
|
|
318
|
+
return this.__internal__authRequests[id];
|
|
323
319
|
}
|
|
324
320
|
getMetaRequest(id) {
|
|
325
|
-
return
|
|
321
|
+
return this.__internal__metaRequests[id];
|
|
326
322
|
}
|
|
327
323
|
getSignRequest(id) {
|
|
328
|
-
return
|
|
324
|
+
return this.__internal__signRequests[id];
|
|
329
325
|
}
|
|
330
326
|
// List all providers the extension is exposing
|
|
331
327
|
rpcListProviders() {
|
|
332
|
-
return Promise.resolve(Object.keys(
|
|
333
|
-
acc[key] =
|
|
328
|
+
return Promise.resolve(Object.keys(this.__internal__providers).reduce((acc, key) => {
|
|
329
|
+
acc[key] = this.__internal__providers[key].meta;
|
|
334
330
|
return acc;
|
|
335
331
|
}, {}));
|
|
336
332
|
}
|
|
337
333
|
rpcSend(request, port) {
|
|
338
|
-
const provider =
|
|
334
|
+
const provider = this.__internal__injectedProviders.get(port);
|
|
339
335
|
assert(provider, 'Cannot call pub(rpc.subscribe) before provider is set');
|
|
340
336
|
return provider.send(request.method, request.params);
|
|
341
337
|
}
|
|
342
338
|
// Start a provider, return its meta
|
|
343
339
|
rpcStartProvider(key, port) {
|
|
344
|
-
assert(Object.keys(
|
|
345
|
-
if (
|
|
346
|
-
return Promise.resolve(
|
|
340
|
+
assert(Object.keys(this.__internal__providers).includes(key), `Provider ${key} is not exposed by extension`);
|
|
341
|
+
if (this.__internal__injectedProviders.get(port)) {
|
|
342
|
+
return Promise.resolve(this.__internal__providers[key].meta);
|
|
347
343
|
}
|
|
348
344
|
// Instantiate the provider
|
|
349
|
-
|
|
345
|
+
this.__internal__injectedProviders.set(port, this.__internal__providers[key].start());
|
|
350
346
|
// Close provider connection when page is closed
|
|
351
347
|
port.onDisconnect.addListener(() => {
|
|
352
|
-
const provider =
|
|
348
|
+
const provider = this.__internal__injectedProviders.get(port);
|
|
353
349
|
if (provider) {
|
|
354
350
|
withErrorLog(() => provider.disconnect());
|
|
355
351
|
}
|
|
356
|
-
|
|
352
|
+
this.__internal__injectedProviders.delete(port);
|
|
357
353
|
});
|
|
358
|
-
return Promise.resolve(
|
|
354
|
+
return Promise.resolve(this.__internal__providers[key].meta);
|
|
359
355
|
}
|
|
360
356
|
rpcSubscribe({ method, params, type }, cb, port) {
|
|
361
|
-
const provider =
|
|
357
|
+
const provider = this.__internal__injectedProviders.get(port);
|
|
362
358
|
assert(provider, 'Cannot call pub(rpc.subscribe) before provider is set');
|
|
363
359
|
return provider.subscribe(type, method, params, cb);
|
|
364
360
|
}
|
|
365
361
|
rpcSubscribeConnected(_request, cb, port) {
|
|
366
|
-
const provider =
|
|
362
|
+
const provider = this.__internal__injectedProviders.get(port);
|
|
367
363
|
assert(provider, 'Cannot call pub(rpc.subscribeConnected) before provider is set');
|
|
368
364
|
cb(null, provider.isConnected); // Immediately send back current isConnected
|
|
369
365
|
provider.on('connected', () => cb(null, true));
|
|
370
366
|
provider.on('disconnected', () => cb(null, false));
|
|
371
367
|
}
|
|
372
368
|
rpcUnsubscribe(request, port) {
|
|
373
|
-
const provider =
|
|
369
|
+
const provider = this.__internal__injectedProviders.get(port);
|
|
374
370
|
assert(provider, 'Cannot call pub(rpc.unsubscribe) before provider is set');
|
|
375
371
|
return provider.unsubscribe(request.type, request.method, request.subscriptionId);
|
|
376
372
|
}
|
|
377
373
|
saveMetadata(meta) {
|
|
378
|
-
|
|
374
|
+
this.__internal__metaStore.set(meta.genesisHash, meta);
|
|
379
375
|
addMetadata(meta);
|
|
380
376
|
}
|
|
381
377
|
setNotification(notification) {
|
|
382
|
-
|
|
378
|
+
this.__internal__notification = notification;
|
|
383
379
|
return true;
|
|
384
380
|
}
|
|
385
381
|
sign(url, request, account) {
|
|
386
382
|
const id = getId();
|
|
387
383
|
return new Promise((resolve, reject) => {
|
|
388
|
-
|
|
384
|
+
this.__internal__signRequests[id] = {
|
|
389
385
|
...this.signComplete(id, resolve, reject),
|
|
390
386
|
account,
|
|
391
387
|
id,
|
|
@@ -397,4 +393,3 @@ export default class State {
|
|
|
397
393
|
});
|
|
398
394
|
}
|
|
399
395
|
}
|
|
400
|
-
_State_authUrls = new WeakMap(), _State_authRequests = new WeakMap(), _State_metaStore = new WeakMap(), _State_injectedProviders = new WeakMap(), _State_metaRequests = new WeakMap(), _State_notification = new WeakMap(), _State_providers = new WeakMap(), _State_signRequests = new WeakMap(), _State_windows = new WeakMap(), _State_connectedTabsUrl = new WeakMap();
|