@subwallet/extension-base 0.3.6-0 → 0.3.6-3
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/{build/background → background}/KoniTypes.d.ts +0 -0
- package/{build/background → background}/KoniTypes.js +0 -0
- package/{build/background → background}/RequestBytesSign.d.ts +0 -0
- package/{build/background → background}/RequestBytesSign.js +0 -0
- package/{build/background → background}/RequestExtrinsicSign.d.ts +0 -0
- package/{build/background → background}/RequestExtrinsicSign.js +0 -0
- package/{build/background → background}/handlers/Extension.d.ts +1 -1
- package/{build/background → background}/handlers/Extension.js +2 -2
- package/{build/background → background}/handlers/State.d.ts +0 -0
- package/{build/background → background}/handlers/State.js +0 -0
- package/{build/background → background}/handlers/Tabs.d.ts +0 -0
- package/{build/background → background}/handlers/Tabs.js +0 -0
- package/{build/background → background}/handlers/helpers.d.ts +0 -0
- package/{build/background → background}/handlers/helpers.js +0 -0
- package/{build/background → background}/handlers/index.d.ts +0 -0
- package/{build/background → background}/handlers/index.js +0 -0
- package/{build/background → background}/handlers/subscriptions.d.ts +0 -0
- package/{build/background → background}/handlers/subscriptions.js +0 -0
- package/{build/background → background}/types.d.ts +0 -0
- package/{build/background → background}/types.js +0 -0
- package/{build/bundle.d.ts → bundle.d.ts} +0 -0
- package/{build/bundle.js → bundle.js} +0 -0
- package/{build-cjs → cjs}/background/KoniTypes.js +0 -0
- package/{build-cjs → cjs}/background/RequestBytesSign.js +0 -0
- package/{build-cjs → cjs}/background/RequestExtrinsicSign.js +0 -0
- package/{build/cjs → cjs}/background/handlers/Extension.js +4 -4
- package/{build-cjs → cjs}/background/handlers/State.js +0 -0
- package/{build-cjs → cjs}/background/handlers/Tabs.js +0 -0
- package/{build-cjs → cjs}/background/handlers/helpers.js +0 -0
- package/{build-cjs → cjs}/background/handlers/index.js +0 -0
- package/{build-cjs → cjs}/background/handlers/subscriptions.js +0 -0
- package/{build-cjs → cjs}/background/types.js +0 -0
- package/{build-cjs → cjs}/bundle.js +0 -0
- package/{build-cjs → cjs}/defaults.js +0 -0
- package/{build-cjs → cjs}/detectOther.js +0 -0
- package/{build-cjs → cjs}/detectPackage.js +0 -0
- package/{build-cjs → cjs}/index.js +0 -0
- package/{build/cjs → cjs}/package.json +0 -0
- package/{build-cjs → cjs}/packageInfo.js +1 -1
- package/{build-cjs → cjs}/page/Accounts.js +0 -0
- package/{build-cjs → cjs}/page/Injected.js +0 -0
- package/{build-cjs → cjs}/page/Metadata.js +0 -0
- package/{build-cjs → cjs}/page/PostMessageProvider.js +0 -0
- package/{build-cjs → cjs}/page/Signer.js +0 -0
- package/{build-cjs → cjs}/page/index.js +0 -0
- package/{build-cjs → cjs}/page/types.js +0 -0
- package/{build-cjs → cjs}/stores/Accounts.js +0 -0
- package/{build-cjs → cjs}/stores/Base.js +0 -0
- package/{build-cjs → cjs}/stores/Metadata.js +0 -0
- package/{build-cjs → cjs}/stores/index.js +0 -0
- package/{build-cjs → cjs}/types.js +0 -0
- package/{build-cjs → cjs}/utils/canDerive.js +0 -0
- package/{build-cjs → cjs}/utils/getId.js +0 -0
- package/{build-cjs → cjs}/utils/index.js +0 -0
- package/{build/defaults.d.ts → defaults.d.ts} +0 -0
- package/{build/defaults.js → defaults.js} +0 -0
- package/{build/detectOther.d.ts → detectOther.d.ts} +0 -0
- package/{build/detectOther.js → detectOther.js} +0 -0
- package/{build/detectPackage.d.ts → detectPackage.d.ts} +0 -0
- package/{build/detectPackage.js → detectPackage.js} +0 -0
- package/{build/index.d.ts → index.d.ts} +0 -0
- package/{build/index.js → index.js} +0 -0
- package/package.json +174 -10
- package/{build/packageInfo.d.ts → packageInfo.d.ts} +0 -0
- package/{build/packageInfo.js → packageInfo.js} +1 -1
- package/{build/page → page}/Accounts.d.ts +0 -0
- package/{build/page → page}/Accounts.js +0 -0
- package/{build/page → page}/Injected.d.ts +0 -0
- package/{build/page → page}/Injected.js +0 -0
- package/{build/page → page}/Metadata.d.ts +0 -0
- package/{build/page → page}/Metadata.js +0 -0
- package/{build/page → page}/PostMessageProvider.d.ts +0 -0
- package/{build/page → page}/PostMessageProvider.js +0 -0
- package/{build/page → page}/Signer.d.ts +0 -0
- package/{build/page → page}/Signer.js +0 -0
- package/{build/page → page}/index.d.ts +1 -1
- package/{build/page → page}/index.js +0 -0
- package/{build/page → page}/types.d.ts +0 -0
- package/{build/page → page}/types.js +0 -0
- package/{build/stores → stores}/Accounts.d.ts +0 -0
- package/{build/stores → stores}/Accounts.js +0 -0
- package/{build/stores → stores}/Base.d.ts +0 -0
- package/{build/stores → stores}/Base.js +0 -0
- package/{build/stores → stores}/Metadata.d.ts +0 -0
- package/{build/stores → stores}/Metadata.js +0 -0
- package/{build/stores → stores}/index.d.ts +0 -0
- package/{build/stores → stores}/index.js +0 -0
- package/{build/types.d.ts → types.d.ts} +0 -0
- package/{build/types.js → types.js} +0 -0
- package/{build/utils → utils}/canDerive.d.ts +0 -0
- package/{build/utils → utils}/canDerive.js +0 -0
- package/{build/utils → utils}/getId.d.ts +0 -0
- package/{build/utils → utils}/getId.js +0 -0
- package/{build/utils → utils}/index.d.ts +0 -0
- package/{build/utils → utils}/index.js +0 -0
- package/build/LICENSE +0 -201
- package/build/README.md +0 -10
- package/build/cjs/background/KoniTypes.js +0 -69
- package/build/cjs/background/RequestBytesSign.js +0 -27
- package/build/cjs/background/RequestExtrinsicSign.js +0 -23
- package/build/cjs/background/handlers/State.js +0 -472
- package/build/cjs/background/handlers/Tabs.js +0 -273
- package/build/cjs/background/handlers/helpers.js +0 -20
- package/build/cjs/background/handlers/index.js +0 -60
- package/build/cjs/background/handlers/subscriptions.js +0 -32
- package/build/cjs/background/types.js +0 -1
- package/build/cjs/bundle.js +0 -13
- package/build/cjs/defaults.js +0 -26
- package/build/cjs/detectOther.js +0 -17
- package/build/cjs/detectPackage.js +0 -14
- package/build/cjs/index.js +0 -18
- package/build/cjs/packageInfo.js +0 -16
- package/build/cjs/page/Accounts.js +0 -31
- package/build/cjs/page/Injected.js +0 -30
- package/build/cjs/page/Metadata.js +0 -27
- package/build/cjs/page/PostMessageProvider.js +0 -184
- package/build/cjs/page/Signer.js +0 -44
- package/build/cjs/page/index.js +0 -76
- package/build/cjs/page/types.js +0 -1
- package/build/cjs/stores/Accounts.js +0 -33
- package/build/cjs/stores/Base.js +0 -84
- package/build/cjs/stores/Metadata.js +0 -23
- package/build/cjs/stores/index.js +0 -23
- package/build/cjs/types.js +0 -1
- package/build/cjs/utils/canDerive.js +0 -12
- package/build/cjs/utils/getId.js +0 -16
- package/build/cjs/utils/index.js +0 -13
- package/build/package.json +0 -210
- package/build-cjs/background/handlers/Extension.js +0 -764
- package/src/background/KoniTypes.ts +0 -537
- package/src/background/RequestBytesSign.ts +0 -30
- package/src/background/RequestExtrinsicSign.ts +0 -23
- package/src/background/handlers/Extension.spec.ts +0 -456
- package/src/background/handlers/Extension.ts +0 -627
- package/src/background/handlers/State.ts +0 -513
- package/src/background/handlers/Tabs.ts +0 -223
- package/src/background/handlers/helpers.ts +0 -14
- package/src/background/handlers/index.ts +0 -49
- package/src/background/handlers/subscriptions.ts +0 -30
- package/src/background/types.ts +0 -437
- package/src/bundle.ts +0 -4
- package/src/defaults.ts +0 -24
- package/src/detectOther.ts +0 -8
- package/src/detectPackage.ts +0 -11
- package/src/index.ts +0 -7
- package/src/packageInfo.ts +0 -6
- package/src/page/Accounts.ts +0 -27
- package/src/page/Injected.ts +0 -27
- package/src/page/Metadata.ts +0 -22
- package/src/page/PostMessageProvider.ts +0 -178
- package/src/page/Signer.ts +0 -45
- package/src/page/index.ts +0 -87
- package/src/page/types.ts +0 -10
- package/src/stores/Accounts.ts +0 -24
- package/src/stores/Base.ts +0 -81
- package/src/stores/Metadata.ts +0 -13
- package/src/stores/index.ts +0 -5
- package/src/types.ts +0 -12
- package/src/utils/canDerive.ts +0 -8
- package/src/utils/getId.ts +0 -10
- package/src/utils/index.ts +0 -4
- package/tsconfig.build.json +0 -18
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.json +0 -14
|
@@ -1,513 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2022 @subwallet/extension-bg authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import type { MetadataDef, ProviderMeta } from '@subwallet/extension-inject/types';
|
|
5
|
-
import type { JsonRpcResponse, ProviderInterface, ProviderInterfaceCallback } from '@polkadot/rpc-provider/types';
|
|
6
|
-
import type { AccountJson, AuthorizeRequest, MetadataRequest, RequestAuthorizeTab, RequestRpcSend, RequestRpcSubscribe, RequestRpcUnsubscribe, RequestSign, ResponseRpcListProviders, ResponseSigning, SigningRequest } from '../types';
|
|
7
|
-
|
|
8
|
-
import { getId } from '@subwallet/extension-base/utils/getId';
|
|
9
|
-
import { addMetadata, knownMetadata } from '@subwallet/extension-chains';
|
|
10
|
-
import { BehaviorSubject } from 'rxjs';
|
|
11
|
-
|
|
12
|
-
import { knownGenesis } from '@polkadot/networks/defaults';
|
|
13
|
-
import settings from '@polkadot/ui-settings';
|
|
14
|
-
import { assert } from '@polkadot/util';
|
|
15
|
-
|
|
16
|
-
import { MetadataStore } from '../../stores';
|
|
17
|
-
import { withErrorLog } from './helpers';
|
|
18
|
-
|
|
19
|
-
export interface Resolver <T> {
|
|
20
|
-
reject: (error: Error) => void;
|
|
21
|
-
resolve: (result: T) => void;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface AuthRequest extends Resolver<boolean> {
|
|
25
|
-
id: string;
|
|
26
|
-
idStr: string;
|
|
27
|
-
request: RequestAuthorizeTab;
|
|
28
|
-
url: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export type AuthUrls = Record<string, AuthUrlInfo>;
|
|
32
|
-
|
|
33
|
-
export interface AuthUrlInfo {
|
|
34
|
-
count: number;
|
|
35
|
-
id: string;
|
|
36
|
-
isAllowed: boolean;
|
|
37
|
-
origin: string;
|
|
38
|
-
url: string;
|
|
39
|
-
isAllowedMap: Record<string, boolean>
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
interface MetaRequest extends Resolver<boolean> {
|
|
43
|
-
id: string;
|
|
44
|
-
request: MetadataDef;
|
|
45
|
-
url: string;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// List of providers passed into constructor. This is the list of providers
|
|
49
|
-
// exposed by the extension.
|
|
50
|
-
type Providers = Record<string, {
|
|
51
|
-
meta: ProviderMeta;
|
|
52
|
-
// The provider is not running at init, calling this will instantiate the
|
|
53
|
-
// provider.
|
|
54
|
-
start: () => ProviderInterface;
|
|
55
|
-
}>
|
|
56
|
-
|
|
57
|
-
interface SignRequest extends Resolver<ResponseSigning> {
|
|
58
|
-
account: AccountJson;
|
|
59
|
-
id: string;
|
|
60
|
-
request: RequestSign;
|
|
61
|
-
url: string;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const NOTIFICATION_URL = chrome.extension.getURL('notification.html');
|
|
65
|
-
|
|
66
|
-
const POPUP_WINDOW_OPTS: chrome.windows.CreateData = {
|
|
67
|
-
focused: true,
|
|
68
|
-
height: 621,
|
|
69
|
-
left: 150,
|
|
70
|
-
top: 150,
|
|
71
|
-
type: 'popup',
|
|
72
|
-
url: NOTIFICATION_URL,
|
|
73
|
-
width: 460
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
const NORMAL_WINDOW_OPTS: chrome.windows.CreateData = {
|
|
77
|
-
focused: true,
|
|
78
|
-
type: 'normal',
|
|
79
|
-
url: NOTIFICATION_URL
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
export enum NotificationOptions {
|
|
83
|
-
None,
|
|
84
|
-
Normal,
|
|
85
|
-
PopUp,
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const AUTH_URLS_KEY = 'authUrls';
|
|
89
|
-
|
|
90
|
-
function extractMetadata (store: MetadataStore): void {
|
|
91
|
-
store.allMap((map): void => {
|
|
92
|
-
const knownEntries = Object.entries(knownGenesis);
|
|
93
|
-
const defs: Record<string, { def: MetadataDef, index: number, key: string }> = {};
|
|
94
|
-
const removals: string[] = [];
|
|
95
|
-
|
|
96
|
-
Object
|
|
97
|
-
.entries(map)
|
|
98
|
-
.forEach(([key, def]): void => {
|
|
99
|
-
const entry = knownEntries.find(([, hashes]) => hashes.includes(def.genesisHash));
|
|
100
|
-
|
|
101
|
-
if (entry) {
|
|
102
|
-
const [name, hashes] = entry;
|
|
103
|
-
const index = hashes.indexOf(def.genesisHash);
|
|
104
|
-
|
|
105
|
-
// flatten the known metadata based on the genesis index
|
|
106
|
-
// (lower is better/newer)
|
|
107
|
-
if (!defs[name] || (defs[name].index > index)) {
|
|
108
|
-
if (defs[name]) {
|
|
109
|
-
// remove the old version of the metadata
|
|
110
|
-
removals.push(defs[name].key);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
defs[name] = { def, index, key };
|
|
114
|
-
}
|
|
115
|
-
} else {
|
|
116
|
-
// this is not a known entry, so we will just apply it
|
|
117
|
-
defs[key] = { def, index: 0, key };
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
removals.forEach((key) => store.remove(key));
|
|
122
|
-
Object.values(defs).forEach(({ def }) => addMetadata(def));
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export default class State {
|
|
127
|
-
readonly #authUrls: AuthUrls = {};
|
|
128
|
-
|
|
129
|
-
readonly #authRequests: Record<string, AuthRequest> = {};
|
|
130
|
-
|
|
131
|
-
readonly #metaStore = new MetadataStore();
|
|
132
|
-
|
|
133
|
-
// Map of providers currently injected in tabs
|
|
134
|
-
readonly #injectedProviders = new Map<chrome.runtime.Port, ProviderInterface>();
|
|
135
|
-
|
|
136
|
-
readonly #metaRequests: Record<string, MetaRequest> = {};
|
|
137
|
-
|
|
138
|
-
#notification = settings.notification;
|
|
139
|
-
|
|
140
|
-
// Map of all providers exposed by the extension, they are retrievable by key
|
|
141
|
-
readonly #providers: Providers;
|
|
142
|
-
|
|
143
|
-
readonly #signRequests: Record<string, SignRequest> = {};
|
|
144
|
-
|
|
145
|
-
#windows: number[] = [];
|
|
146
|
-
|
|
147
|
-
public readonly authSubject: BehaviorSubject<AuthorizeRequest[]> = new BehaviorSubject<AuthorizeRequest[]>([]);
|
|
148
|
-
|
|
149
|
-
public readonly metaSubject: BehaviorSubject<MetadataRequest[]> = new BehaviorSubject<MetadataRequest[]>([]);
|
|
150
|
-
|
|
151
|
-
public readonly signSubject: BehaviorSubject<SigningRequest[]> = new BehaviorSubject<SigningRequest[]>([]);
|
|
152
|
-
|
|
153
|
-
constructor (providers: Providers = {}) {
|
|
154
|
-
this.#providers = providers;
|
|
155
|
-
|
|
156
|
-
extractMetadata(this.#metaStore);
|
|
157
|
-
|
|
158
|
-
// retrieve previously set authorizations
|
|
159
|
-
const authString = localStorage.getItem(AUTH_URLS_KEY) || '{}';
|
|
160
|
-
const previousAuth = JSON.parse(authString) as AuthUrls;
|
|
161
|
-
|
|
162
|
-
this.#authUrls = previousAuth;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
public get knownMetadata (): MetadataDef[] {
|
|
166
|
-
return knownMetadata();
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
public get numAuthRequests (): number {
|
|
170
|
-
return Object.keys(this.#authRequests).length;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
public get numMetaRequests (): number {
|
|
174
|
-
return Object.keys(this.#metaRequests).length;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
public get numSignRequests (): number {
|
|
178
|
-
return Object.keys(this.#signRequests).length;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
public get allAuthRequests (): AuthorizeRequest[] {
|
|
182
|
-
return Object
|
|
183
|
-
.values(this.#authRequests)
|
|
184
|
-
.map(({ id, request, url }): AuthorizeRequest => ({ id, request, url }));
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
public get allMetaRequests (): MetadataRequest[] {
|
|
188
|
-
return Object
|
|
189
|
-
.values(this.#metaRequests)
|
|
190
|
-
.map(({ id, request, url }): MetadataRequest => ({ id, request, url }));
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
public get allSignRequests (): SigningRequest[] {
|
|
194
|
-
return Object
|
|
195
|
-
.values(this.#signRequests)
|
|
196
|
-
.map(({ account, id, request, url }): SigningRequest => ({ account, id, request, url }));
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
public get authUrls (): AuthUrls {
|
|
200
|
-
return this.#authUrls;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
protected popupClose (): void {
|
|
204
|
-
this.#windows.forEach((id: number) =>
|
|
205
|
-
withErrorLog(() => chrome.windows.remove(id))
|
|
206
|
-
);
|
|
207
|
-
this.#windows = [];
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
protected popupOpen (): void {
|
|
211
|
-
this.#notification !== 'extension' &&
|
|
212
|
-
chrome.windows.create(
|
|
213
|
-
this.#notification === 'window'
|
|
214
|
-
? NORMAL_WINDOW_OPTS
|
|
215
|
-
: POPUP_WINDOW_OPTS,
|
|
216
|
-
(window): void => {
|
|
217
|
-
if (window) {
|
|
218
|
-
this.#windows.push(window.id || 0);
|
|
219
|
-
}
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
private authComplete = (id: string, resolve: (result: boolean) => void, reject: (error: Error) => void): Resolver<boolean> => {
|
|
224
|
-
const complete = (result: boolean | Error) => {
|
|
225
|
-
const isAllowed = result === true;
|
|
226
|
-
const { idStr, request: { origin }, url } = this.#authRequests[id];
|
|
227
|
-
const isAllowedMap = {};
|
|
228
|
-
|
|
229
|
-
this.#authUrls[this.stripUrl(url)] = {
|
|
230
|
-
count: 0,
|
|
231
|
-
id: idStr,
|
|
232
|
-
isAllowed,
|
|
233
|
-
isAllowedMap,
|
|
234
|
-
origin,
|
|
235
|
-
url
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
this.saveCurrentAuthList();
|
|
239
|
-
delete this.#authRequests[id];
|
|
240
|
-
this.updateIconAuth(true);
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
return {
|
|
244
|
-
reject: (error: Error): void => {
|
|
245
|
-
complete(error);
|
|
246
|
-
reject(error);
|
|
247
|
-
},
|
|
248
|
-
resolve: (result: boolean): void => {
|
|
249
|
-
complete(result);
|
|
250
|
-
resolve(result);
|
|
251
|
-
}
|
|
252
|
-
};
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
private saveCurrentAuthList () {
|
|
256
|
-
localStorage.setItem(AUTH_URLS_KEY, JSON.stringify(this.#authUrls));
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
private metaComplete = (id: string, resolve: (result: boolean) => void, reject: (error: Error) => void): Resolver<boolean> => {
|
|
260
|
-
const complete = (): void => {
|
|
261
|
-
delete this.#metaRequests[id];
|
|
262
|
-
this.updateIconMeta(true);
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
return {
|
|
266
|
-
reject: (error: Error): void => {
|
|
267
|
-
complete();
|
|
268
|
-
reject(error);
|
|
269
|
-
},
|
|
270
|
-
resolve: (result: boolean): void => {
|
|
271
|
-
complete();
|
|
272
|
-
resolve(result);
|
|
273
|
-
}
|
|
274
|
-
};
|
|
275
|
-
};
|
|
276
|
-
|
|
277
|
-
private signComplete = (id: string, resolve: (result: ResponseSigning) => void, reject: (error: Error) => void): Resolver<ResponseSigning> => {
|
|
278
|
-
const complete = (): void => {
|
|
279
|
-
delete this.#signRequests[id];
|
|
280
|
-
this.updateIconSign(true);
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
return {
|
|
284
|
-
reject: (error: Error): void => {
|
|
285
|
-
complete();
|
|
286
|
-
reject(error);
|
|
287
|
-
},
|
|
288
|
-
resolve: (result: ResponseSigning): void => {
|
|
289
|
-
complete();
|
|
290
|
-
resolve(result);
|
|
291
|
-
}
|
|
292
|
-
};
|
|
293
|
-
};
|
|
294
|
-
|
|
295
|
-
public stripUrl (url: string): string {
|
|
296
|
-
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:`);
|
|
297
|
-
|
|
298
|
-
const parts = url.split('/');
|
|
299
|
-
|
|
300
|
-
return parts[2];
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
private updateIcon (shouldClose?: boolean): void {
|
|
304
|
-
const authCount = this.numAuthRequests;
|
|
305
|
-
const metaCount = this.numMetaRequests;
|
|
306
|
-
const signCount = this.numSignRequests;
|
|
307
|
-
const text = (
|
|
308
|
-
authCount
|
|
309
|
-
? 'Auth'
|
|
310
|
-
: metaCount
|
|
311
|
-
? 'Meta'
|
|
312
|
-
: (signCount ? `${signCount}` : '')
|
|
313
|
-
);
|
|
314
|
-
|
|
315
|
-
withErrorLog(() => chrome.browserAction.setBadgeText({ text }));
|
|
316
|
-
|
|
317
|
-
if (shouldClose && text === '') {
|
|
318
|
-
this.popupClose();
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
public toggleAuthorization (url: string): AuthUrls {
|
|
323
|
-
const entry = this.#authUrls[url];
|
|
324
|
-
|
|
325
|
-
assert(entry, `The source ${url} is not known`);
|
|
326
|
-
|
|
327
|
-
this.#authUrls[url].isAllowed = !entry.isAllowed;
|
|
328
|
-
this.saveCurrentAuthList();
|
|
329
|
-
|
|
330
|
-
return this.#authUrls;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
private updateIconAuth (shouldClose?: boolean): void {
|
|
334
|
-
this.authSubject.next(this.allAuthRequests);
|
|
335
|
-
this.updateIcon(shouldClose);
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
private updateIconMeta (shouldClose?: boolean): void {
|
|
339
|
-
this.metaSubject.next(this.allMetaRequests);
|
|
340
|
-
this.updateIcon(shouldClose);
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
private updateIconSign (shouldClose?: boolean): void {
|
|
344
|
-
this.signSubject.next(this.allSignRequests);
|
|
345
|
-
this.updateIcon(shouldClose);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
public async authorizeUrl (url: string, request: RequestAuthorizeTab): Promise<boolean> {
|
|
349
|
-
const idStr = this.stripUrl(url);
|
|
350
|
-
|
|
351
|
-
// Do not enqueue duplicate authorization requests.
|
|
352
|
-
const isDuplicate = Object.values(this.#authRequests)
|
|
353
|
-
.some((request) => request.idStr === idStr);
|
|
354
|
-
|
|
355
|
-
assert(!isDuplicate, `The source ${url} has a pending authorization request`);
|
|
356
|
-
|
|
357
|
-
if (this.#authUrls[idStr]) {
|
|
358
|
-
// this url was seen in the past
|
|
359
|
-
assert(this.#authUrls[idStr].isAllowed, `The source ${url} is not allowed to interact with this extension`);
|
|
360
|
-
|
|
361
|
-
return false;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
return new Promise((resolve, reject): void => {
|
|
365
|
-
const id = getId();
|
|
366
|
-
|
|
367
|
-
this.#authRequests[id] = {
|
|
368
|
-
...this.authComplete(id, resolve, reject),
|
|
369
|
-
id,
|
|
370
|
-
idStr,
|
|
371
|
-
request,
|
|
372
|
-
url
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
this.updateIconAuth();
|
|
376
|
-
this.popupOpen();
|
|
377
|
-
});
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
public ensureUrlAuthorized (url: string): boolean {
|
|
381
|
-
const entry = this.#authUrls[this.stripUrl(url)];
|
|
382
|
-
|
|
383
|
-
assert(entry, `The source ${url} has not been enabled yet`);
|
|
384
|
-
assert(entry.isAllowed, `The source ${url} is not allowed to interact with this extension`);
|
|
385
|
-
|
|
386
|
-
return true;
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
public injectMetadata (url: string, request: MetadataDef): Promise<boolean> {
|
|
390
|
-
return new Promise((resolve, reject): void => {
|
|
391
|
-
const id = getId();
|
|
392
|
-
|
|
393
|
-
this.#metaRequests[id] = {
|
|
394
|
-
...this.metaComplete(id, resolve, reject),
|
|
395
|
-
id,
|
|
396
|
-
request,
|
|
397
|
-
url
|
|
398
|
-
};
|
|
399
|
-
|
|
400
|
-
this.updateIconMeta();
|
|
401
|
-
this.popupOpen();
|
|
402
|
-
});
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
public getAuthRequest (id: string): AuthRequest {
|
|
406
|
-
return this.#authRequests[id];
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
public getMetaRequest (id: string): MetaRequest {
|
|
410
|
-
return this.#metaRequests[id];
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
public getSignRequest (id: string): SignRequest {
|
|
414
|
-
return this.#signRequests[id];
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
// List all providers the extension is exposing
|
|
418
|
-
public rpcListProviders (): Promise<ResponseRpcListProviders> {
|
|
419
|
-
return Promise.resolve(Object.keys(this.#providers).reduce((acc, key) => {
|
|
420
|
-
acc[key] = this.#providers[key].meta;
|
|
421
|
-
|
|
422
|
-
return acc;
|
|
423
|
-
}, {} as ResponseRpcListProviders));
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
public rpcSend (request: RequestRpcSend, port: chrome.runtime.Port): Promise<JsonRpcResponse> {
|
|
427
|
-
const provider = this.#injectedProviders.get(port);
|
|
428
|
-
|
|
429
|
-
assert(provider, 'Cannot call pub(rpc.subscribe) before provider is set');
|
|
430
|
-
|
|
431
|
-
return provider.send(request.method, request.params);
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
// Start a provider, return its meta
|
|
435
|
-
public rpcStartProvider (key: string, port: chrome.runtime.Port): Promise<ProviderMeta> {
|
|
436
|
-
assert(Object.keys(this.#providers).includes(key), `Provider ${key} is not exposed by extension`);
|
|
437
|
-
|
|
438
|
-
if (this.#injectedProviders.get(port)) {
|
|
439
|
-
return Promise.resolve(this.#providers[key].meta);
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
// Instantiate the provider
|
|
443
|
-
this.#injectedProviders.set(port, this.#providers[key].start());
|
|
444
|
-
|
|
445
|
-
// Close provider connection when page is closed
|
|
446
|
-
port.onDisconnect.addListener((): void => {
|
|
447
|
-
const provider = this.#injectedProviders.get(port);
|
|
448
|
-
|
|
449
|
-
if (provider) {
|
|
450
|
-
withErrorLog(() => provider.disconnect());
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
this.#injectedProviders.delete(port);
|
|
454
|
-
});
|
|
455
|
-
|
|
456
|
-
return Promise.resolve(this.#providers[key].meta);
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
public rpcSubscribe ({ method, params, type }: RequestRpcSubscribe, cb: ProviderInterfaceCallback, port: chrome.runtime.Port): Promise<number | string> {
|
|
460
|
-
const provider = this.#injectedProviders.get(port);
|
|
461
|
-
|
|
462
|
-
assert(provider, 'Cannot call pub(rpc.subscribe) before provider is set');
|
|
463
|
-
|
|
464
|
-
return provider.subscribe(type, method, params, cb);
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
public rpcSubscribeConnected (_request: null, cb: ProviderInterfaceCallback, port: chrome.runtime.Port): void {
|
|
468
|
-
const provider = this.#injectedProviders.get(port);
|
|
469
|
-
|
|
470
|
-
assert(provider, 'Cannot call pub(rpc.subscribeConnected) before provider is set');
|
|
471
|
-
|
|
472
|
-
cb(null, provider.isConnected); // Immediately send back current isConnected
|
|
473
|
-
provider.on('connected', () => cb(null, true));
|
|
474
|
-
provider.on('disconnected', () => cb(null, false));
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
public rpcUnsubscribe (request: RequestRpcUnsubscribe, port: chrome.runtime.Port): Promise<boolean> {
|
|
478
|
-
const provider = this.#injectedProviders.get(port);
|
|
479
|
-
|
|
480
|
-
assert(provider, 'Cannot call pub(rpc.unsubscribe) before provider is set');
|
|
481
|
-
|
|
482
|
-
return provider.unsubscribe(request.type, request.method, request.subscriptionId);
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
public saveMetadata (meta: MetadataDef): void {
|
|
486
|
-
this.#metaStore.set(meta.genesisHash, meta);
|
|
487
|
-
|
|
488
|
-
addMetadata(meta);
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
public setNotification (notification: string): boolean {
|
|
492
|
-
this.#notification = notification;
|
|
493
|
-
|
|
494
|
-
return true;
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
public sign (url: string, request: RequestSign, account: AccountJson): Promise<ResponseSigning> {
|
|
498
|
-
const id = getId();
|
|
499
|
-
|
|
500
|
-
return new Promise((resolve, reject): void => {
|
|
501
|
-
this.#signRequests[id] = {
|
|
502
|
-
...this.signComplete(id, resolve, reject),
|
|
503
|
-
account,
|
|
504
|
-
id,
|
|
505
|
-
request,
|
|
506
|
-
url
|
|
507
|
-
};
|
|
508
|
-
|
|
509
|
-
this.updateIconSign();
|
|
510
|
-
this.popupOpen();
|
|
511
|
-
});
|
|
512
|
-
}
|
|
513
|
-
}
|