@pezkuwi/extension-base 0.62.14 → 0.62.18
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/package.json +16 -470
- package/src/background/RequestBytesSign.ts +28 -0
- package/src/background/RequestExtrinsicSign.ts +22 -0
- package/src/background/handlers/Extension.spec.ts +478 -0
- package/src/background/handlers/Extension.ts +690 -0
- package/src/background/handlers/State.ts +664 -0
- package/src/background/handlers/Tabs.ts +289 -0
- package/src/background/handlers/helpers.ts +14 -0
- package/src/background/handlers/index.ts +60 -0
- package/src/background/handlers/subscriptions.ts +32 -0
- package/src/background/index.ts +4 -0
- package/src/background/types.ts +432 -0
- package/src/bundle.ts +4 -0
- package/{defaults.js → src/defaults.ts} +15 -2
- package/src/index.ts +7 -0
- package/{packageDetect.js → src/packageDetect.ts} +8 -0
- package/src/packageInfo.ts +6 -0
- package/src/page/Accounts.ts +33 -0
- package/src/page/Injected.ts +33 -0
- package/src/page/Metadata.ts +22 -0
- package/src/page/PostMessageProvider.ts +182 -0
- package/src/page/Signer.ts +45 -0
- package/src/page/index.ts +89 -0
- package/src/page/types.ts +10 -0
- package/src/stores/Accounts.ts +28 -0
- package/src/stores/Base.ts +93 -0
- package/src/stores/Metadata.ts +17 -0
- package/{stores/index.js → src/stores/index.ts} +3 -0
- package/src/types.ts +12 -0
- package/src/utils/canDerive.ts +8 -0
- package/src/utils/getId.ts +10 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/portUtils.ts +65 -0
- package/tsconfig.build.json +16 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.spec.json +18 -0
- package/tsconfig.spec.tsbuildinfo +1 -0
- package/LICENSE +0 -201
- package/background/RequestBytesSign.d.ts +0 -12
- package/background/RequestBytesSign.js +0 -12
- package/background/RequestExtrinsicSign.d.ts +0 -12
- package/background/RequestExtrinsicSign.js +0 -11
- package/background/handlers/Extension.d.ts +0 -49
- package/background/handlers/Extension.js +0 -489
- package/background/handlers/State.js +0 -478
- package/background/handlers/Tabs.d.ts +0 -25
- package/background/handlers/Tabs.js +0 -195
- package/background/handlers/helpers.js +0 -11
- package/background/handlers/index.d.ts +0 -3
- package/background/handlers/index.js +0 -40
- package/background/handlers/subscriptions.d.ts +0 -3
- package/background/handlers/subscriptions.js +0 -18
- package/background/index.d.ts +0 -1
- package/background/index.js +0 -1
- package/background/types.js +0 -1
- package/bundle.d.ts +0 -1
- package/bundle.js +0 -1
- package/cjs/background/RequestBytesSign.d.ts +0 -12
- package/cjs/background/RequestBytesSign.js +0 -15
- package/cjs/background/RequestExtrinsicSign.d.ts +0 -12
- package/cjs/background/RequestExtrinsicSign.js +0 -14
- package/cjs/background/handlers/Extension.d.ts +0 -49
- package/cjs/background/handlers/Extension.js +0 -492
- package/cjs/background/handlers/State.d.ts +0 -96
- package/cjs/background/handlers/State.js +0 -482
- package/cjs/background/handlers/Tabs.d.ts +0 -25
- package/cjs/background/handlers/Tabs.js +0 -199
- package/cjs/background/handlers/helpers.d.ts +0 -1
- package/cjs/background/handlers/helpers.js +0 -14
- package/cjs/background/handlers/index.d.ts +0 -3
- package/cjs/background/handlers/index.js +0 -46
- package/cjs/background/handlers/subscriptions.d.ts +0 -3
- package/cjs/background/handlers/subscriptions.js +0 -22
- package/cjs/background/index.d.ts +0 -1
- package/cjs/background/index.js +0 -7
- package/cjs/background/types.d.ts +0 -343
- package/cjs/background/types.js +0 -2
- package/cjs/bundle.d.ts +0 -1
- package/cjs/bundle.js +0 -5
- package/cjs/defaults.js +0 -16
- package/cjs/index.d.ts +0 -1
- package/cjs/index.js +0 -4
- package/cjs/package.json +0 -3
- package/cjs/packageDetect.d.ts +0 -1
- package/cjs/packageDetect.js +0 -8
- package/cjs/packageInfo.d.ts +0 -6
- package/cjs/packageInfo.js +0 -4
- package/cjs/page/Accounts.d.ts +0 -7
- package/cjs/page/Accounts.js +0 -24
- package/cjs/page/Injected.d.ts +0 -13
- package/cjs/page/Injected.js +0 -25
- package/cjs/page/Metadata.d.ts +0 -7
- package/cjs/page/Metadata.js +0 -15
- package/cjs/page/PostMessageProvider.d.ts +0 -63
- package/cjs/page/PostMessageProvider.js +0 -135
- package/cjs/page/Signer.d.ts +0 -8
- package/cjs/page/Signer.js +0 -29
- package/cjs/page/index.d.ts +0 -16
- package/cjs/page/index.js +0 -52
- package/cjs/page/types.d.ts +0 -6
- package/cjs/page/types.js +0 -2
- package/cjs/stores/Accounts.js +0 -21
- package/cjs/stores/Base.js +0 -70
- package/cjs/stores/Metadata.js +0 -13
- package/cjs/stores/index.js +0 -8
- package/cjs/types.js +0 -2
- package/cjs/utils/canDerive.d.ts +0 -2
- package/cjs/utils/canDerive.js +0 -6
- package/cjs/utils/getId.js +0 -8
- package/cjs/utils/index.d.ts +0 -1
- package/cjs/utils/index.js +0 -5
- package/cjs/utils/portUtils.d.ts +0 -13
- package/cjs/utils/portUtils.js +0 -49
- package/defaults.d.ts +0 -9
- package/index.d.ts +0 -1
- package/index.js +0 -1
- package/packageDetect.d.ts +0 -1
- package/packageInfo.d.ts +0 -6
- package/packageInfo.js +0 -1
- package/page/Accounts.d.ts +0 -7
- package/page/Accounts.js +0 -21
- package/page/Injected.d.ts +0 -13
- package/page/Injected.js +0 -21
- package/page/Metadata.d.ts +0 -7
- package/page/Metadata.js +0 -12
- package/page/PostMessageProvider.d.ts +0 -63
- package/page/PostMessageProvider.js +0 -132
- package/page/Signer.d.ts +0 -8
- package/page/Signer.js +0 -26
- package/page/index.d.ts +0 -16
- package/page/index.js +0 -45
- package/page/types.d.ts +0 -6
- package/page/types.js +0 -1
- package/stores/Accounts.d.ts +0 -6
- package/stores/Accounts.js +0 -17
- package/stores/Base.d.ts +0 -9
- package/stores/Base.js +0 -67
- package/stores/Metadata.d.ts +0 -5
- package/stores/Metadata.js +0 -9
- package/stores/index.d.ts +0 -2
- package/types.d.ts +0 -9
- package/types.js +0 -1
- package/utils/canDerive.d.ts +0 -2
- package/utils/canDerive.js +0 -3
- package/utils/getId.d.ts +0 -1
- package/utils/getId.js +0 -5
- package/utils/index.d.ts +0 -1
- package/utils/index.js +0 -1
- package/utils/portUtils.d.ts +0 -13
- package/utils/portUtils.js +0 -43
- /package/{background → src/background}/handlers/State.d.ts +0 -0
- /package/{background → src/background}/handlers/helpers.d.ts +0 -0
- /package/{background → src/background}/types.d.ts +0 -0
- /package/{cjs → src}/defaults.d.ts +0 -0
- /package/{cjs → src}/stores/Accounts.d.ts +0 -0
- /package/{cjs → src}/stores/Base.d.ts +0 -0
- /package/{cjs → src}/stores/Metadata.d.ts +0 -0
- /package/{cjs → src}/stores/index.d.ts +0 -0
- /package/{cjs → src}/types.d.ts +0 -0
- /package/{cjs → src}/utils/getId.d.ts +0 -0
|
@@ -1,482 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NotificationOptions = void 0;
|
|
4
|
-
const rxjs_1 = require("rxjs");
|
|
5
|
-
const extension_chains_1 = require("@pezkuwi/extension-chains");
|
|
6
|
-
const defaults_1 = require("@pezkuwi/networks/defaults");
|
|
7
|
-
const ui_settings_1 = require("@pezkuwi/ui-settings");
|
|
8
|
-
const util_1 = require("@pezkuwi/util");
|
|
9
|
-
const index_js_1 = require("../../stores/index.js");
|
|
10
|
-
const getId_js_1 = require("../../utils/getId.js");
|
|
11
|
-
const helpers_js_1 = require("./helpers.js");
|
|
12
|
-
const NOTIFICATION_URL = chrome.runtime.getURL('notification.html');
|
|
13
|
-
const POPUP_WINDOW_OPTS = {
|
|
14
|
-
focused: true,
|
|
15
|
-
height: 621,
|
|
16
|
-
left: 150,
|
|
17
|
-
top: 150,
|
|
18
|
-
type: 'popup',
|
|
19
|
-
url: NOTIFICATION_URL,
|
|
20
|
-
width: 560
|
|
21
|
-
};
|
|
22
|
-
const NORMAL_WINDOW_OPTS = {
|
|
23
|
-
focused: true,
|
|
24
|
-
type: 'normal',
|
|
25
|
-
url: NOTIFICATION_URL
|
|
26
|
-
};
|
|
27
|
-
var NotificationOptions;
|
|
28
|
-
(function (NotificationOptions) {
|
|
29
|
-
NotificationOptions[NotificationOptions["None"] = 0] = "None";
|
|
30
|
-
NotificationOptions[NotificationOptions["Normal"] = 1] = "Normal";
|
|
31
|
-
NotificationOptions[NotificationOptions["PopUp"] = 2] = "PopUp";
|
|
32
|
-
})(NotificationOptions || (exports.NotificationOptions = NotificationOptions = {}));
|
|
33
|
-
const AUTH_URLS_KEY = 'authUrls';
|
|
34
|
-
const DEFAULT_AUTH_ACCOUNTS = 'defaultAuthAccounts';
|
|
35
|
-
async function extractMetadata(store) {
|
|
36
|
-
await store.allMap(async (map) => {
|
|
37
|
-
const knownEntries = Object.entries(defaults_1.knownGenesis);
|
|
38
|
-
const defs = {};
|
|
39
|
-
const removals = [];
|
|
40
|
-
Object
|
|
41
|
-
.entries(map)
|
|
42
|
-
.forEach(([key, def]) => {
|
|
43
|
-
const entry = knownEntries.find(([, hashes]) => hashes.includes(def.genesisHash));
|
|
44
|
-
if (entry) {
|
|
45
|
-
const [name, hashes] = entry;
|
|
46
|
-
const index = hashes.indexOf(def.genesisHash);
|
|
47
|
-
// flatten the known metadata based on the genesis index
|
|
48
|
-
// (lower is better/newer)
|
|
49
|
-
if (!defs[name] || (defs[name].index > index)) {
|
|
50
|
-
if (defs[name]) {
|
|
51
|
-
// remove the old version of the metadata
|
|
52
|
-
removals.push(defs[name].key);
|
|
53
|
-
}
|
|
54
|
-
defs[name] = { def, index, key };
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
// this is not a known entry, so we will just apply it
|
|
59
|
-
defs[key] = { def, index: 0, key };
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
for (const key of removals) {
|
|
63
|
-
await store.remove(key);
|
|
64
|
-
}
|
|
65
|
-
Object.values(defs).forEach(({ def }) => (0, extension_chains_1.addMetadata)(def));
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
class State {
|
|
69
|
-
#authUrls = new Map();
|
|
70
|
-
#lastRequestTimestamps = new Map();
|
|
71
|
-
#maxEntries = 10;
|
|
72
|
-
#rateLimitInterval = 3000; // 3 seconds
|
|
73
|
-
#authRequests = {};
|
|
74
|
-
#metaStore = new index_js_1.MetadataStore();
|
|
75
|
-
// Map of providers currently injected in tabs
|
|
76
|
-
#injectedProviders = new Map();
|
|
77
|
-
#metaRequests = {};
|
|
78
|
-
#notification = ui_settings_1.settings.notification;
|
|
79
|
-
// Map of all providers exposed by the extension, they are retrievable by key
|
|
80
|
-
#providers;
|
|
81
|
-
#signRequests = {};
|
|
82
|
-
#windows = [];
|
|
83
|
-
#connectedTabsUrl = [];
|
|
84
|
-
authSubject = new rxjs_1.BehaviorSubject([]);
|
|
85
|
-
metaSubject = new rxjs_1.BehaviorSubject([]);
|
|
86
|
-
signSubject = new rxjs_1.BehaviorSubject([]);
|
|
87
|
-
authUrlSubjects = {};
|
|
88
|
-
defaultAuthAccountSelection = [];
|
|
89
|
-
constructor(providers = {}, rateLimitInterval = 3000) {
|
|
90
|
-
(0, util_1.assert)(rateLimitInterval >= 0, 'Expects non-negative number for rateLimitInterval');
|
|
91
|
-
this.#providers = providers;
|
|
92
|
-
this.#rateLimitInterval = rateLimitInterval;
|
|
93
|
-
}
|
|
94
|
-
async init() {
|
|
95
|
-
await extractMetadata(this.#metaStore);
|
|
96
|
-
// retrieve previously set authorizations
|
|
97
|
-
const storageAuthUrls = await chrome.storage.local.get(AUTH_URLS_KEY);
|
|
98
|
-
const authString = storageAuthUrls?.[AUTH_URLS_KEY] || '{}';
|
|
99
|
-
const previousAuth = JSON.parse(authString);
|
|
100
|
-
this.#authUrls = new Map(Object.entries(previousAuth));
|
|
101
|
-
// Initialize authUrlSubjects for each URL
|
|
102
|
-
this.#authUrls.forEach((authInfo, url) => {
|
|
103
|
-
this.authUrlSubjects[url] = new rxjs_1.BehaviorSubject(authInfo);
|
|
104
|
-
});
|
|
105
|
-
// retrieve previously set default auth accounts
|
|
106
|
-
const storageDefaultAuthAccounts = await chrome.storage.local.get(DEFAULT_AUTH_ACCOUNTS);
|
|
107
|
-
const defaultAuthString = storageDefaultAuthAccounts?.[DEFAULT_AUTH_ACCOUNTS] || '[]';
|
|
108
|
-
const previousDefaultAuth = JSON.parse(defaultAuthString);
|
|
109
|
-
this.defaultAuthAccountSelection = previousDefaultAuth;
|
|
110
|
-
}
|
|
111
|
-
get knownMetadata() {
|
|
112
|
-
return (0, extension_chains_1.knownMetadata)();
|
|
113
|
-
}
|
|
114
|
-
get numAuthRequests() {
|
|
115
|
-
return Object.keys(this.#authRequests).length;
|
|
116
|
-
}
|
|
117
|
-
get numMetaRequests() {
|
|
118
|
-
return Object.keys(this.#metaRequests).length;
|
|
119
|
-
}
|
|
120
|
-
get numSignRequests() {
|
|
121
|
-
return Object.keys(this.#signRequests).length;
|
|
122
|
-
}
|
|
123
|
-
get allAuthRequests() {
|
|
124
|
-
return Object
|
|
125
|
-
.values(this.#authRequests)
|
|
126
|
-
.map(({ id, request, url }) => ({ id, request, url }));
|
|
127
|
-
}
|
|
128
|
-
get allMetaRequests() {
|
|
129
|
-
return Object
|
|
130
|
-
.values(this.#metaRequests)
|
|
131
|
-
.map(({ id, request, url }) => ({ id, request, url }));
|
|
132
|
-
}
|
|
133
|
-
get allSignRequests() {
|
|
134
|
-
return Object
|
|
135
|
-
.values(this.#signRequests)
|
|
136
|
-
.map(({ account, id, request, url }) => ({ account, id, request, url }));
|
|
137
|
-
}
|
|
138
|
-
get authUrls() {
|
|
139
|
-
return Object.fromEntries(this.#authUrls);
|
|
140
|
-
}
|
|
141
|
-
popupClose() {
|
|
142
|
-
this.#windows.forEach((id) => (0, helpers_js_1.withErrorLog)(() => chrome.windows.remove(id)));
|
|
143
|
-
this.#windows = [];
|
|
144
|
-
}
|
|
145
|
-
popupOpen() {
|
|
146
|
-
this.#notification !== 'extension' &&
|
|
147
|
-
chrome.windows.create(this.#notification === 'window'
|
|
148
|
-
? NORMAL_WINDOW_OPTS
|
|
149
|
-
: POPUP_WINDOW_OPTS, (window) => {
|
|
150
|
-
if (window) {
|
|
151
|
-
this.#windows.push(window.id || 0);
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
authComplete = (id, resolve, reject) => {
|
|
156
|
-
const complete = async (authorizedAccounts = []) => {
|
|
157
|
-
const { idStr, request: { origin }, url } = this.#authRequests[id];
|
|
158
|
-
const strippedUrl = this.stripUrl(url);
|
|
159
|
-
const authInfo = {
|
|
160
|
-
authorizedAccounts,
|
|
161
|
-
count: 0,
|
|
162
|
-
id: idStr,
|
|
163
|
-
origin,
|
|
164
|
-
url
|
|
165
|
-
};
|
|
166
|
-
this.#authUrls.set(strippedUrl, authInfo);
|
|
167
|
-
if (!this.authUrlSubjects[strippedUrl]) {
|
|
168
|
-
this.authUrlSubjects[strippedUrl] = new rxjs_1.BehaviorSubject(authInfo);
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
this.authUrlSubjects[strippedUrl].next(authInfo);
|
|
172
|
-
}
|
|
173
|
-
await this.saveCurrentAuthList();
|
|
174
|
-
await this.updateDefaultAuthAccounts(authorizedAccounts);
|
|
175
|
-
delete this.#authRequests[id];
|
|
176
|
-
this.updateIconAuth(true);
|
|
177
|
-
};
|
|
178
|
-
return {
|
|
179
|
-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
180
|
-
reject: async (error) => {
|
|
181
|
-
if (error.message === 'Cancelled') {
|
|
182
|
-
delete this.#authRequests[id];
|
|
183
|
-
this.updateIconAuth(true);
|
|
184
|
-
reject(new Error('Connection request was cancelled by the user.'));
|
|
185
|
-
}
|
|
186
|
-
else {
|
|
187
|
-
await complete();
|
|
188
|
-
reject(new Error('Connection request was rejected by the user.'));
|
|
189
|
-
}
|
|
190
|
-
},
|
|
191
|
-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
192
|
-
resolve: async ({ authorizedAccounts, result }) => {
|
|
193
|
-
await complete(authorizedAccounts);
|
|
194
|
-
resolve({ authorizedAccounts, result });
|
|
195
|
-
}
|
|
196
|
-
};
|
|
197
|
-
};
|
|
198
|
-
/**
|
|
199
|
-
* @deprecated This method is deprecated in favor of {@link updateCurrentTabs} and will be removed in a future release.
|
|
200
|
-
*/
|
|
201
|
-
udateCurrentTabsUrl(urls) {
|
|
202
|
-
this.updateCurrentTabsUrl(urls);
|
|
203
|
-
}
|
|
204
|
-
updateCurrentTabsUrl(urls) {
|
|
205
|
-
const connectedTabs = urls.map((url) => {
|
|
206
|
-
let strippedUrl = '';
|
|
207
|
-
// the assert in stripUrl may throw for new tabs with "chrome://newtab/"
|
|
208
|
-
try {
|
|
209
|
-
strippedUrl = this.stripUrl(url);
|
|
210
|
-
}
|
|
211
|
-
catch (e) {
|
|
212
|
-
console.error(e);
|
|
213
|
-
}
|
|
214
|
-
// return the stripped url only if this website is known
|
|
215
|
-
return !!strippedUrl && this.authUrls[strippedUrl]
|
|
216
|
-
? strippedUrl
|
|
217
|
-
: undefined;
|
|
218
|
-
})
|
|
219
|
-
.filter((value) => !!value);
|
|
220
|
-
this.#connectedTabsUrl = connectedTabs;
|
|
221
|
-
}
|
|
222
|
-
getConnectedTabsUrl() {
|
|
223
|
-
return this.#connectedTabsUrl;
|
|
224
|
-
}
|
|
225
|
-
deleteAuthRequest(requestId) {
|
|
226
|
-
delete this.#authRequests[requestId];
|
|
227
|
-
this.updateIconAuth(true);
|
|
228
|
-
}
|
|
229
|
-
async saveCurrentAuthList() {
|
|
230
|
-
await chrome.storage.local.set({ [AUTH_URLS_KEY]: JSON.stringify(Object.fromEntries(this.#authUrls)) });
|
|
231
|
-
}
|
|
232
|
-
async saveDefaultAuthAccounts() {
|
|
233
|
-
await chrome.storage.local.set({ [DEFAULT_AUTH_ACCOUNTS]: JSON.stringify(this.defaultAuthAccountSelection) });
|
|
234
|
-
}
|
|
235
|
-
async updateDefaultAuthAccounts(newList) {
|
|
236
|
-
this.defaultAuthAccountSelection = newList;
|
|
237
|
-
await this.saveDefaultAuthAccounts();
|
|
238
|
-
}
|
|
239
|
-
metaComplete = (id, resolve, reject) => {
|
|
240
|
-
const complete = () => {
|
|
241
|
-
delete this.#metaRequests[id];
|
|
242
|
-
this.updateIconMeta(true);
|
|
243
|
-
};
|
|
244
|
-
return {
|
|
245
|
-
reject: (error) => {
|
|
246
|
-
complete();
|
|
247
|
-
reject(error);
|
|
248
|
-
},
|
|
249
|
-
resolve: (result) => {
|
|
250
|
-
complete();
|
|
251
|
-
resolve(result);
|
|
252
|
-
}
|
|
253
|
-
};
|
|
254
|
-
};
|
|
255
|
-
signComplete = (id, resolve, reject) => {
|
|
256
|
-
const complete = () => {
|
|
257
|
-
delete this.#signRequests[id];
|
|
258
|
-
this.updateIconSign(true);
|
|
259
|
-
};
|
|
260
|
-
return {
|
|
261
|
-
reject: (error) => {
|
|
262
|
-
complete();
|
|
263
|
-
reject(error);
|
|
264
|
-
},
|
|
265
|
-
resolve: (result) => {
|
|
266
|
-
complete();
|
|
267
|
-
resolve(result);
|
|
268
|
-
}
|
|
269
|
-
};
|
|
270
|
-
};
|
|
271
|
-
stripUrl(url) {
|
|
272
|
-
try {
|
|
273
|
-
const parsedUrl = new URL(url);
|
|
274
|
-
if (!['http:', 'https:', 'ipfs:', 'ipns:'].includes(parsedUrl.protocol)) {
|
|
275
|
-
throw new Error(`Invalid protocol ${parsedUrl.protocol}`);
|
|
276
|
-
}
|
|
277
|
-
// For ipfs/ipns which don't have a standard origin, we handle it differently.
|
|
278
|
-
if (parsedUrl.protocol === 'ipfs:' || parsedUrl.protocol === 'ipns:') {
|
|
279
|
-
// ipfs://<hash> | ipns://<hash>
|
|
280
|
-
return `${parsedUrl.protocol}//${parsedUrl.hostname}`;
|
|
281
|
-
}
|
|
282
|
-
return parsedUrl.origin;
|
|
283
|
-
}
|
|
284
|
-
catch (e) {
|
|
285
|
-
console.error(e);
|
|
286
|
-
throw new Error('Invalid URL');
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
updateIcon(shouldClose) {
|
|
290
|
-
const authCount = this.numAuthRequests;
|
|
291
|
-
const metaCount = this.numMetaRequests;
|
|
292
|
-
const signCount = this.numSignRequests;
|
|
293
|
-
const text = (authCount
|
|
294
|
-
? 'Auth'
|
|
295
|
-
: metaCount
|
|
296
|
-
? 'Meta'
|
|
297
|
-
: (signCount ? `${signCount}` : ''));
|
|
298
|
-
(0, helpers_js_1.withErrorLog)(() => chrome.action.setBadgeText({ text }));
|
|
299
|
-
if (shouldClose && text === '') {
|
|
300
|
-
this.popupClose();
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
async removeAuthorization(url) {
|
|
304
|
-
const entry = this.#authUrls.get(url);
|
|
305
|
-
(0, util_1.assert)(entry, `The source ${url} is not known`);
|
|
306
|
-
this.#authUrls.delete(url);
|
|
307
|
-
await this.saveCurrentAuthList();
|
|
308
|
-
if (this.authUrlSubjects[url]) {
|
|
309
|
-
entry.authorizedAccounts = [];
|
|
310
|
-
this.authUrlSubjects[url].next(entry);
|
|
311
|
-
}
|
|
312
|
-
return this.authUrls;
|
|
313
|
-
}
|
|
314
|
-
updateIconAuth(shouldClose) {
|
|
315
|
-
this.authSubject.next(this.allAuthRequests);
|
|
316
|
-
this.updateIcon(shouldClose);
|
|
317
|
-
}
|
|
318
|
-
updateIconMeta(shouldClose) {
|
|
319
|
-
this.metaSubject.next(this.allMetaRequests);
|
|
320
|
-
this.updateIcon(shouldClose);
|
|
321
|
-
}
|
|
322
|
-
updateIconSign(shouldClose) {
|
|
323
|
-
this.signSubject.next(this.allSignRequests);
|
|
324
|
-
this.updateIcon(shouldClose);
|
|
325
|
-
}
|
|
326
|
-
async updateAuthorizedAccounts(authorizedAccountsDiff) {
|
|
327
|
-
authorizedAccountsDiff.forEach(([url, authorizedAccountDiff]) => {
|
|
328
|
-
const authInfo = this.#authUrls.get(url);
|
|
329
|
-
if (authInfo) {
|
|
330
|
-
authInfo.authorizedAccounts = authorizedAccountDiff;
|
|
331
|
-
this.#authUrls.set(url, authInfo);
|
|
332
|
-
this.authUrlSubjects[url].next(authInfo);
|
|
333
|
-
}
|
|
334
|
-
});
|
|
335
|
-
await this.saveCurrentAuthList();
|
|
336
|
-
}
|
|
337
|
-
async authorizeUrl(url, request) {
|
|
338
|
-
const idStr = this.stripUrl(url);
|
|
339
|
-
// Do not enqueue duplicate authorization requests.
|
|
340
|
-
const isDuplicate = Object
|
|
341
|
-
.values(this.#authRequests)
|
|
342
|
-
.some((request) => request.idStr === idStr);
|
|
343
|
-
(0, util_1.assert)(!isDuplicate, `The source ${url} has a pending authorization request`);
|
|
344
|
-
if (this.#authUrls.has(idStr)) {
|
|
345
|
-
// this url was seen in the past
|
|
346
|
-
const authInfo = this.#authUrls.get(idStr);
|
|
347
|
-
(0, util_1.assert)(authInfo?.authorizedAccounts || authInfo?.isAllowed, `The source ${url} is not allowed to interact with this extension`);
|
|
348
|
-
return {
|
|
349
|
-
authorizedAccounts: [],
|
|
350
|
-
result: false
|
|
351
|
-
};
|
|
352
|
-
}
|
|
353
|
-
return new Promise((resolve, reject) => {
|
|
354
|
-
const id = (0, getId_js_1.getId)();
|
|
355
|
-
this.#authRequests[id] = {
|
|
356
|
-
...this.authComplete(id, resolve, reject),
|
|
357
|
-
id,
|
|
358
|
-
idStr,
|
|
359
|
-
request,
|
|
360
|
-
url
|
|
361
|
-
};
|
|
362
|
-
this.updateIconAuth();
|
|
363
|
-
this.popupOpen();
|
|
364
|
-
});
|
|
365
|
-
}
|
|
366
|
-
ensureUrlAuthorized(url) {
|
|
367
|
-
const entry = this.#authUrls.get(this.stripUrl(url));
|
|
368
|
-
(0, util_1.assert)(entry, `The source ${url} has not been enabled yet`);
|
|
369
|
-
return true;
|
|
370
|
-
}
|
|
371
|
-
injectMetadata(url, request) {
|
|
372
|
-
return new Promise((resolve, reject) => {
|
|
373
|
-
const id = (0, getId_js_1.getId)();
|
|
374
|
-
this.#metaRequests[id] = {
|
|
375
|
-
...this.metaComplete(id, resolve, reject),
|
|
376
|
-
id,
|
|
377
|
-
request,
|
|
378
|
-
url
|
|
379
|
-
};
|
|
380
|
-
this.updateIconMeta();
|
|
381
|
-
this.popupOpen();
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
getAuthRequest(id) {
|
|
385
|
-
return this.#authRequests[id];
|
|
386
|
-
}
|
|
387
|
-
getMetaRequest(id) {
|
|
388
|
-
return this.#metaRequests[id];
|
|
389
|
-
}
|
|
390
|
-
getSignRequest(id) {
|
|
391
|
-
return this.#signRequests[id];
|
|
392
|
-
}
|
|
393
|
-
// List all providers the extension is exposing
|
|
394
|
-
rpcListProviders() {
|
|
395
|
-
return Promise.resolve(Object.keys(this.#providers).reduce((acc, key) => {
|
|
396
|
-
acc[key] = this.#providers[key].meta;
|
|
397
|
-
return acc;
|
|
398
|
-
}, {}));
|
|
399
|
-
}
|
|
400
|
-
rpcSend(request, port) {
|
|
401
|
-
const provider = this.#injectedProviders.get(port);
|
|
402
|
-
(0, util_1.assert)(provider, 'Cannot call pub(rpc.subscribe) before provider is set');
|
|
403
|
-
return provider.send(request.method, request.params);
|
|
404
|
-
}
|
|
405
|
-
// Start a provider, return its meta
|
|
406
|
-
rpcStartProvider(key, port) {
|
|
407
|
-
(0, util_1.assert)(Object.keys(this.#providers).includes(key), `Provider ${key} is not exposed by extension`);
|
|
408
|
-
if (this.#injectedProviders.get(port)) {
|
|
409
|
-
return Promise.resolve(this.#providers[key].meta);
|
|
410
|
-
}
|
|
411
|
-
// Instantiate the provider
|
|
412
|
-
this.#injectedProviders.set(port, this.#providers[key].start());
|
|
413
|
-
// Close provider connection when page is closed
|
|
414
|
-
port.onDisconnect.addListener(() => {
|
|
415
|
-
const provider = this.#injectedProviders.get(port);
|
|
416
|
-
if (provider) {
|
|
417
|
-
(0, helpers_js_1.withErrorLog)(() => provider.disconnect());
|
|
418
|
-
}
|
|
419
|
-
this.#injectedProviders.delete(port);
|
|
420
|
-
});
|
|
421
|
-
return Promise.resolve(this.#providers[key].meta);
|
|
422
|
-
}
|
|
423
|
-
rpcSubscribe({ method, params, type }, cb, port) {
|
|
424
|
-
const provider = this.#injectedProviders.get(port);
|
|
425
|
-
(0, util_1.assert)(provider, 'Cannot call pub(rpc.subscribe) before provider is set');
|
|
426
|
-
return provider.subscribe(type, method, params, cb);
|
|
427
|
-
}
|
|
428
|
-
rpcSubscribeConnected(_request, cb, port) {
|
|
429
|
-
const provider = this.#injectedProviders.get(port);
|
|
430
|
-
(0, util_1.assert)(provider, 'Cannot call pub(rpc.subscribeConnected) before provider is set');
|
|
431
|
-
cb(null, provider.isConnected); // Immediately send back current isConnected
|
|
432
|
-
provider.on('connected', () => cb(null, true));
|
|
433
|
-
provider.on('disconnected', () => cb(null, false));
|
|
434
|
-
}
|
|
435
|
-
rpcUnsubscribe(request, port) {
|
|
436
|
-
const provider = this.#injectedProviders.get(port);
|
|
437
|
-
(0, util_1.assert)(provider, 'Cannot call pub(rpc.unsubscribe) before provider is set');
|
|
438
|
-
return provider.unsubscribe(request.type, request.method, request.subscriptionId);
|
|
439
|
-
}
|
|
440
|
-
async saveMetadata(meta) {
|
|
441
|
-
await this.#metaStore.set(meta.genesisHash, meta);
|
|
442
|
-
(0, extension_chains_1.addMetadata)(meta);
|
|
443
|
-
}
|
|
444
|
-
setNotification(notification) {
|
|
445
|
-
this.#notification = notification;
|
|
446
|
-
return true;
|
|
447
|
-
}
|
|
448
|
-
handleSignRequest(origin) {
|
|
449
|
-
const now = Date.now();
|
|
450
|
-
const lastTime = this.#lastRequestTimestamps.get(origin) || 0;
|
|
451
|
-
if (now - lastTime < this.#rateLimitInterval) {
|
|
452
|
-
throw new Error('Rate limit exceeded. Try again later.');
|
|
453
|
-
}
|
|
454
|
-
// If we're about to exceed max entries, evict the oldest
|
|
455
|
-
if (!this.#lastRequestTimestamps.has(origin) && this.#lastRequestTimestamps.size >= this.#maxEntries) {
|
|
456
|
-
const oldestKey = this.#lastRequestTimestamps.keys().next().value;
|
|
457
|
-
oldestKey && this.#lastRequestTimestamps.delete(oldestKey);
|
|
458
|
-
}
|
|
459
|
-
this.#lastRequestTimestamps.set(origin, now);
|
|
460
|
-
}
|
|
461
|
-
sign(url, request, account) {
|
|
462
|
-
const id = (0, getId_js_1.getId)();
|
|
463
|
-
try {
|
|
464
|
-
this.handleSignRequest(url);
|
|
465
|
-
}
|
|
466
|
-
catch (error) {
|
|
467
|
-
return Promise.reject(error);
|
|
468
|
-
}
|
|
469
|
-
return new Promise((resolve, reject) => {
|
|
470
|
-
this.#signRequests[id] = {
|
|
471
|
-
...this.signComplete(id, resolve, reject),
|
|
472
|
-
account,
|
|
473
|
-
id,
|
|
474
|
-
request,
|
|
475
|
-
url
|
|
476
|
-
};
|
|
477
|
-
this.updateIconSign();
|
|
478
|
-
this.popupOpen();
|
|
479
|
-
});
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
exports.default = State;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { MessageTypes, RequestTypes, ResponseTypes } from '../types.js';
|
|
2
|
-
import type State from './State.js';
|
|
3
|
-
export default class Tabs {
|
|
4
|
-
#private;
|
|
5
|
-
constructor(state: State);
|
|
6
|
-
private filterForAuthorizedAccounts;
|
|
7
|
-
private authorize;
|
|
8
|
-
private accountsListAuthorized;
|
|
9
|
-
private accountsSubscribeAuthorized;
|
|
10
|
-
private accountsUnsubscribe;
|
|
11
|
-
private getSigningPair;
|
|
12
|
-
private bytesSign;
|
|
13
|
-
private extrinsicSign;
|
|
14
|
-
private metadataProvide;
|
|
15
|
-
private metadataList;
|
|
16
|
-
private rpcListProviders;
|
|
17
|
-
private rpcSend;
|
|
18
|
-
private rpcStartProvider;
|
|
19
|
-
private rpcSubscribe;
|
|
20
|
-
private rpcSubscribeConnected;
|
|
21
|
-
private rpcUnsubscribe;
|
|
22
|
-
private redirectPhishingLanding;
|
|
23
|
-
private redirectIfPhishing;
|
|
24
|
-
handle<TMessageType extends MessageTypes>(id: string, type: TMessageType, request: RequestTypes[TMessageType], url: string, port?: chrome.runtime.Port): Promise<ResponseTypes[keyof ResponseTypes]>;
|
|
25
|
-
}
|