@subwallet/extension-base 0.3.6-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +10 -0
- package/build/LICENSE +201 -0
- package/build/README.md +10 -0
- package/build/background/KoniTypes.d.ts +462 -0
- package/build/background/KoniTypes.js +57 -0
- package/build/background/RequestBytesSign.d.ts +12 -0
- package/build/background/RequestBytesSign.js +16 -0
- package/build/background/RequestExtrinsicSign.d.ts +12 -0
- package/build/background/RequestExtrinsicSign.js +14 -0
- package/build/background/handlers/Extension.d.ts +49 -0
- package/build/background/handlers/Extension.js +676 -0
- package/build/background/handlers/State.d.ts +87 -0
- package/build/background/handlers/State.js +434 -0
- package/build/background/handlers/Tabs.d.ts +24 -0
- package/build/background/handlers/Tabs.js +230 -0
- package/build/background/handlers/helpers.d.ts +1 -0
- package/build/background/handlers/helpers.js +13 -0
- package/build/background/handlers/index.d.ts +3 -0
- package/build/background/handlers/index.js +43 -0
- package/build/background/handlers/subscriptions.d.ts +4 -0
- package/build/background/handlers/subscriptions.js +24 -0
- package/build/background/types.d.ts +345 -0
- package/build/background/types.js +1 -0
- package/build/bundle.d.ts +1 -0
- package/build/bundle.js +3 -0
- package/build/cjs/background/KoniTypes.js +69 -0
- package/build/cjs/background/RequestBytesSign.js +27 -0
- package/build/cjs/background/RequestExtrinsicSign.js +23 -0
- package/build/cjs/background/handlers/Extension.js +764 -0
- package/build/cjs/background/handlers/State.js +472 -0
- package/build/cjs/background/handlers/Tabs.js +273 -0
- package/build/cjs/background/handlers/helpers.js +20 -0
- package/build/cjs/background/handlers/index.js +60 -0
- package/build/cjs/background/handlers/subscriptions.js +32 -0
- package/build/cjs/background/types.js +1 -0
- package/build/cjs/bundle.js +13 -0
- package/build/cjs/defaults.js +26 -0
- package/build/cjs/detectOther.js +17 -0
- package/build/cjs/detectPackage.js +14 -0
- package/build/cjs/index.js +18 -0
- package/build/cjs/package.json +3 -0
- package/build/cjs/packageInfo.js +16 -0
- package/build/cjs/page/Accounts.js +31 -0
- package/build/cjs/page/Injected.js +30 -0
- package/build/cjs/page/Metadata.js +27 -0
- package/build/cjs/page/PostMessageProvider.js +184 -0
- package/build/cjs/page/Signer.js +44 -0
- package/build/cjs/page/index.js +76 -0
- package/build/cjs/page/types.js +1 -0
- package/build/cjs/stores/Accounts.js +33 -0
- package/build/cjs/stores/Base.js +84 -0
- package/build/cjs/stores/Metadata.js +23 -0
- package/build/cjs/stores/index.js +23 -0
- package/build/cjs/types.js +1 -0
- package/build/cjs/utils/canDerive.js +12 -0
- package/build/cjs/utils/getId.js +16 -0
- package/build/cjs/utils/index.js +13 -0
- package/build/defaults.d.ts +10 -0
- package/build/defaults.js +12 -0
- package/build/detectOther.d.ts +7 -0
- package/build/detectOther.js +6 -0
- package/build/detectPackage.d.ts +1 -0
- package/build/detectPackage.js +7 -0
- package/build/index.d.ts +1 -0
- package/build/index.js +5 -0
- package/build/package.json +210 -0
- package/build/packageInfo.d.ts +6 -0
- package/build/packageInfo.js +9 -0
- package/build/page/Accounts.d.ts +7 -0
- package/build/page/Accounts.js +22 -0
- package/build/page/Injected.d.ts +13 -0
- package/build/page/Injected.js +15 -0
- package/build/page/Metadata.d.ts +7 -0
- package/build/page/Metadata.js +18 -0
- package/build/page/PostMessageProvider.d.ts +62 -0
- package/build/page/PostMessageProvider.js +171 -0
- package/build/page/Signer.d.ts +8 -0
- package/build/page/Signer.js +35 -0
- package/build/page/index.d.ts +16 -0
- package/build/page/index.js +64 -0
- package/build/page/types.d.ts +6 -0
- package/build/page/types.js +1 -0
- package/build/stores/Accounts.d.ts +6 -0
- package/build/stores/Accounts.js +20 -0
- package/build/stores/Base.d.ts +10 -0
- package/build/stores/Base.js +74 -0
- package/build/stores/Metadata.d.ts +5 -0
- package/build/stores/Metadata.js +10 -0
- package/build/stores/index.d.ts +2 -0
- package/build/stores/index.js +4 -0
- package/build/types.d.ts +9 -0
- package/build/types.js +1 -0
- package/build/utils/canDerive.d.ts +2 -0
- package/build/utils/canDerive.js +5 -0
- package/build/utils/getId.d.ts +1 -0
- package/build/utils/getId.js +7 -0
- package/build/utils/index.d.ts +1 -0
- package/build/utils/index.js +3 -0
- package/build-cjs/background/KoniTypes.js +69 -0
- package/build-cjs/background/RequestBytesSign.js +27 -0
- package/build-cjs/background/RequestExtrinsicSign.js +23 -0
- package/build-cjs/background/handlers/Extension.js +764 -0
- package/build-cjs/background/handlers/State.js +472 -0
- package/build-cjs/background/handlers/Tabs.js +273 -0
- package/build-cjs/background/handlers/helpers.js +20 -0
- package/build-cjs/background/handlers/index.js +60 -0
- package/build-cjs/background/handlers/subscriptions.js +32 -0
- package/build-cjs/background/types.js +1 -0
- package/build-cjs/bundle.js +13 -0
- package/build-cjs/defaults.js +26 -0
- package/build-cjs/detectOther.js +17 -0
- package/build-cjs/detectPackage.js +14 -0
- package/build-cjs/index.js +18 -0
- package/build-cjs/packageInfo.js +16 -0
- package/build-cjs/page/Accounts.js +31 -0
- package/build-cjs/page/Injected.js +30 -0
- package/build-cjs/page/Metadata.js +27 -0
- package/build-cjs/page/PostMessageProvider.js +184 -0
- package/build-cjs/page/Signer.js +44 -0
- package/build-cjs/page/index.js +76 -0
- package/build-cjs/page/types.js +1 -0
- package/build-cjs/stores/Accounts.js +33 -0
- package/build-cjs/stores/Base.js +84 -0
- package/build-cjs/stores/Metadata.js +23 -0
- package/build-cjs/stores/index.js +23 -0
- package/build-cjs/types.js +1 -0
- package/build-cjs/utils/canDerive.js +12 -0
- package/build-cjs/utils/getId.js +16 -0
- package/build-cjs/utils/index.js +13 -0
- package/package.json +46 -0
- package/src/background/KoniTypes.ts +537 -0
- package/src/background/RequestBytesSign.ts +30 -0
- package/src/background/RequestExtrinsicSign.ts +23 -0
- package/src/background/handlers/Extension.spec.ts +456 -0
- package/src/background/handlers/Extension.ts +627 -0
- package/src/background/handlers/State.ts +513 -0
- package/src/background/handlers/Tabs.ts +223 -0
- package/src/background/handlers/helpers.ts +14 -0
- package/src/background/handlers/index.ts +49 -0
- package/src/background/handlers/subscriptions.ts +30 -0
- package/src/background/types.ts +437 -0
- package/src/bundle.ts +4 -0
- package/src/defaults.ts +24 -0
- package/src/detectOther.ts +8 -0
- package/src/detectPackage.ts +11 -0
- package/src/index.ts +7 -0
- package/src/packageInfo.ts +6 -0
- package/src/page/Accounts.ts +27 -0
- package/src/page/Injected.ts +27 -0
- package/src/page/Metadata.ts +22 -0
- package/src/page/PostMessageProvider.ts +178 -0
- package/src/page/Signer.ts +45 -0
- package/src/page/index.ts +87 -0
- package/src/page/types.ts +10 -0
- package/src/stores/Accounts.ts +24 -0
- package/src/stores/Base.ts +81 -0
- package/src/stores/Metadata.ts +13 -0
- package/src/stores/index.ts +5 -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/tsconfig.build.json +18 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.json +14 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
// Copyright 2019-2022 @subwallet/extension authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { PHISHING_PAGE_REDIRECT } from '@subwallet/extension-base/defaults';
|
|
4
|
+
import { canDerive } from '@subwallet/extension-base/utils';
|
|
5
|
+
import { checkIfDenied } from '@polkadot/phishing';
|
|
6
|
+
import keyring from '@polkadot/ui-keyring';
|
|
7
|
+
import { accounts as accountsObservable } from '@polkadot/ui-keyring/observable/accounts';
|
|
8
|
+
import { assert, isNumber } from '@polkadot/util';
|
|
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";
|
|
13
|
+
|
|
14
|
+
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)).filter(({
|
|
24
|
+
type
|
|
25
|
+
}) => type !== 'ethereum') // Quick fix DApp not allow EVM
|
|
26
|
+
.sort((a, b) => (a.json.meta.whenCreated || 0) - (b.json.meta.whenCreated || 0)).map(({
|
|
27
|
+
json: {
|
|
28
|
+
address,
|
|
29
|
+
meta: {
|
|
30
|
+
genesisHash,
|
|
31
|
+
name
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
type
|
|
35
|
+
}) => ({
|
|
36
|
+
address,
|
|
37
|
+
genesisHash,
|
|
38
|
+
name,
|
|
39
|
+
type
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export default class Tabs {
|
|
44
|
+
#state;
|
|
45
|
+
|
|
46
|
+
constructor(state) {
|
|
47
|
+
this.#state = state;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
authorize(url, request) {
|
|
51
|
+
return this.#state.authorizeUrl(url, request);
|
|
52
|
+
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
accountsList(url, {
|
|
56
|
+
anyType
|
|
57
|
+
}) {
|
|
58
|
+
return transformAccounts(accountsObservable.subject.getValue(), anyType);
|
|
59
|
+
} // FIXME This looks very much like what we have in Extension
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
accountsSubscribe(url, id, port) {
|
|
63
|
+
const cb = createSubscription(id, port);
|
|
64
|
+
const subscription = accountsObservable.subject.subscribe(accounts => cb(transformAccounts(accounts)));
|
|
65
|
+
port.onDisconnect.addListener(() => {
|
|
66
|
+
unsubscribe(id);
|
|
67
|
+
subscription.unsubscribe();
|
|
68
|
+
});
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
getSigningPair(address) {
|
|
73
|
+
const pair = keyring.getPair(address);
|
|
74
|
+
assert(pair, 'Unable to find keypair');
|
|
75
|
+
return pair;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
bytesSign(url, request) {
|
|
79
|
+
const address = request.address;
|
|
80
|
+
const pair = this.getSigningPair(address);
|
|
81
|
+
return this.#state.sign(url, new RequestBytesSign(request), {
|
|
82
|
+
address,
|
|
83
|
+
...pair.meta
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
extrinsicSign(url, request) {
|
|
88
|
+
const address = request.address;
|
|
89
|
+
const pair = this.getSigningPair(address);
|
|
90
|
+
return this.#state.sign(url, new RequestExtrinsicSign(request), {
|
|
91
|
+
address,
|
|
92
|
+
...pair.meta
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
metadataProvide(url, request) {
|
|
97
|
+
return this.#state.injectMetadata(url, request);
|
|
98
|
+
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
metadataList(url) {
|
|
102
|
+
return this.#state.knownMetadata.map(({
|
|
103
|
+
genesisHash,
|
|
104
|
+
specVersion
|
|
105
|
+
}) => ({
|
|
106
|
+
genesisHash,
|
|
107
|
+
specVersion
|
|
108
|
+
}));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
rpcListProviders() {
|
|
112
|
+
return this.#state.rpcListProviders();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
rpcSend(request, port) {
|
|
116
|
+
return this.#state.rpcSend(request, port);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
rpcStartProvider(key, port) {
|
|
120
|
+
return this.#state.rpcStartProvider(key, port);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async rpcSubscribe(request, id, port) {
|
|
124
|
+
const innerCb = createSubscription(id, port);
|
|
125
|
+
|
|
126
|
+
const cb = (_error, data) => innerCb(data);
|
|
127
|
+
|
|
128
|
+
const subscriptionId = await this.#state.rpcSubscribe(request, cb, port);
|
|
129
|
+
port.onDisconnect.addListener(() => {
|
|
130
|
+
unsubscribe(id);
|
|
131
|
+
withErrorLog(() => this.rpcUnsubscribe({ ...request,
|
|
132
|
+
subscriptionId
|
|
133
|
+
}, port));
|
|
134
|
+
});
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
rpcSubscribeConnected(request, id, port) {
|
|
139
|
+
const innerCb = createSubscription(id, port);
|
|
140
|
+
|
|
141
|
+
const cb = (_error, data) => innerCb(data);
|
|
142
|
+
|
|
143
|
+
this.#state.rpcSubscribeConnected(request, cb, port);
|
|
144
|
+
port.onDisconnect.addListener(() => {
|
|
145
|
+
unsubscribe(id);
|
|
146
|
+
});
|
|
147
|
+
return Promise.resolve(true);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async rpcUnsubscribe(request, port) {
|
|
151
|
+
return this.#state.rpcUnsubscribe(request, port);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
redirectPhishingLanding(phishingWebsite) {
|
|
155
|
+
const nonFragment = phishingWebsite.split('#')[0];
|
|
156
|
+
const encodedWebsite = encodeURIComponent(nonFragment);
|
|
157
|
+
const url = `${chrome.extension.getURL('index.html')}#${PHISHING_PAGE_REDIRECT}/${encodedWebsite}`;
|
|
158
|
+
chrome.tabs.query({
|
|
159
|
+
url: nonFragment
|
|
160
|
+
}, tabs => {
|
|
161
|
+
tabs.map(({
|
|
162
|
+
id
|
|
163
|
+
}) => id).filter(id => isNumber(id)).forEach(id => withErrorLog(() => chrome.tabs.update(id, {
|
|
164
|
+
url
|
|
165
|
+
})));
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async redirectIfPhishing(url) {
|
|
170
|
+
const isInDenyList = await checkIfDenied(url);
|
|
171
|
+
|
|
172
|
+
if (isInDenyList) {
|
|
173
|
+
this.redirectPhishingLanding(url);
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async handle(id, type, request, url, port) {
|
|
181
|
+
if (type === 'pub(phishing.redirectIfDenied)') {
|
|
182
|
+
return this.redirectIfPhishing(url);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
switch (type) {
|
|
186
|
+
case 'pub(authorize.tab)':
|
|
187
|
+
return this.authorize(url, request);
|
|
188
|
+
|
|
189
|
+
case 'pub(accounts.list)':
|
|
190
|
+
return this.accountsList(url, request);
|
|
191
|
+
|
|
192
|
+
case 'pub(accounts.subscribe)':
|
|
193
|
+
return this.accountsSubscribe(url, id, port);
|
|
194
|
+
|
|
195
|
+
case 'pub(bytes.sign)':
|
|
196
|
+
return this.bytesSign(url, request);
|
|
197
|
+
|
|
198
|
+
case 'pub(extrinsic.sign)':
|
|
199
|
+
return this.extrinsicSign(url, request);
|
|
200
|
+
|
|
201
|
+
case 'pub(metadata.list)':
|
|
202
|
+
return this.metadataList(url);
|
|
203
|
+
|
|
204
|
+
case 'pub(metadata.provide)':
|
|
205
|
+
return this.metadataProvide(url, request);
|
|
206
|
+
|
|
207
|
+
case 'pub(rpc.listProviders)':
|
|
208
|
+
return this.rpcListProviders();
|
|
209
|
+
|
|
210
|
+
case 'pub(rpc.send)':
|
|
211
|
+
return this.rpcSend(request, port);
|
|
212
|
+
|
|
213
|
+
case 'pub(rpc.startProvider)':
|
|
214
|
+
return this.rpcStartProvider(request, port);
|
|
215
|
+
|
|
216
|
+
case 'pub(rpc.subscribe)':
|
|
217
|
+
return this.rpcSubscribe(request, id, port);
|
|
218
|
+
|
|
219
|
+
case 'pub(rpc.subscribeConnected)':
|
|
220
|
+
return this.rpcSubscribeConnected(request, id, port);
|
|
221
|
+
|
|
222
|
+
case 'pub(rpc.unsubscribe)':
|
|
223
|
+
return this.rpcUnsubscribe(request, port);
|
|
224
|
+
|
|
225
|
+
default:
|
|
226
|
+
throw new Error(`Unable to handle message of type ${type}`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function withErrorLog(fn: () => unknown): void;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Copyright 2019-2022 @subwallet/extension authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
export function withErrorLog(fn) {
|
|
4
|
+
try {
|
|
5
|
+
const p = fn();
|
|
6
|
+
|
|
7
|
+
if (p && typeof p === 'object' && typeof p.catch === 'function') {
|
|
8
|
+
p.catch(console.error);
|
|
9
|
+
}
|
|
10
|
+
} catch (e) {
|
|
11
|
+
console.error(e);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/// <reference types="chrome" />
|
|
2
|
+
import type { MessageTypes, TransportRequestMessage } from '../types';
|
|
3
|
+
export default function handler<TMessageType extends MessageTypes>({ id, message, request }: TransportRequestMessage<TMessageType>, port: chrome.runtime.Port, extensionPortName?: string): void;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Copyright 2019-2022 @subwallet/extension authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { assert } from '@polkadot/util';
|
|
4
|
+
import { PORT_EXTENSION } from "../../defaults.js";
|
|
5
|
+
import Extension from "./Extension.js";
|
|
6
|
+
import State from "./State.js";
|
|
7
|
+
import Tabs from "./Tabs.js";
|
|
8
|
+
const state = new State();
|
|
9
|
+
const extension = new Extension(state);
|
|
10
|
+
const tabs = new Tabs(state);
|
|
11
|
+
export default function handler({
|
|
12
|
+
id,
|
|
13
|
+
message,
|
|
14
|
+
request
|
|
15
|
+
}, port, extensionPortName = PORT_EXTENSION) {
|
|
16
|
+
const isExtension = port.name === extensionPortName;
|
|
17
|
+
const sender = port.sender;
|
|
18
|
+
const from = isExtension ? 'extension' : sender.tab && sender.tab.url || sender.url || '<unknown>';
|
|
19
|
+
const source = `${from}: ${id}: ${message}`;
|
|
20
|
+
console.log(` [in] ${source}`); // :: ${JSON.stringify(request)}`);
|
|
21
|
+
|
|
22
|
+
const promise = isExtension ? extension.handle(id, message, request, port) : tabs.handle(id, message, request, from, port);
|
|
23
|
+
promise.then(response => {
|
|
24
|
+
console.log(`[out] ${source}`); // :: ${JSON.stringify(response)}`);
|
|
25
|
+
// between the start and the end of the promise, the user may have closed
|
|
26
|
+
// the tab, in which case port will be undefined
|
|
27
|
+
|
|
28
|
+
assert(port, 'Port has been disconnected');
|
|
29
|
+
port.postMessage({
|
|
30
|
+
id,
|
|
31
|
+
response
|
|
32
|
+
});
|
|
33
|
+
}).catch(error => {
|
|
34
|
+
console.log(`[err] ${source}:: ${error.message}`); // only send message back to port if it's still connected
|
|
35
|
+
|
|
36
|
+
if (port) {
|
|
37
|
+
port.postMessage({
|
|
38
|
+
error: error.message,
|
|
39
|
+
id
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/// <reference types="chrome" />
|
|
2
|
+
import type { MessageTypesWithSubscriptions, SubscriptionMessageTypes } from '../types';
|
|
3
|
+
export declare function createSubscription<TMessageType extends MessageTypesWithSubscriptions>(id: string, port: chrome.runtime.Port): (data: SubscriptionMessageTypes[TMessageType]) => void;
|
|
4
|
+
export declare function unsubscribe(id: string): void;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Copyright 2019-2022 @subwallet/extension authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
const subscriptions = {}; // return a subscription callback, that will send the data to the caller via the port
|
|
4
|
+
|
|
5
|
+
export function createSubscription(id, port) {
|
|
6
|
+
subscriptions[id] = port;
|
|
7
|
+
return subscription => {
|
|
8
|
+
if (subscriptions[id]) {
|
|
9
|
+
port.postMessage({
|
|
10
|
+
id,
|
|
11
|
+
subscription
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
} // clear a previous subscriber
|
|
16
|
+
|
|
17
|
+
export function unsubscribe(id) {
|
|
18
|
+
if (subscriptions[id]) {
|
|
19
|
+
console.log(`Unsubscribing from ${id}`);
|
|
20
|
+
delete subscriptions[id];
|
|
21
|
+
} else {
|
|
22
|
+
console.error(`Unable to unsubscribe from ${id}`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import type { InjectedAccount, InjectedMetadataKnown, MetadataDef, ProviderList, ProviderMeta } from '@subwallet/extension-inject/types';
|
|
2
|
+
import type { KeyringPair, KeyringPair$Json, KeyringPair$Meta } from '@polkadot/keyring/types';
|
|
3
|
+
import type { JsonRpcResponse } from '@polkadot/rpc-provider/types';
|
|
4
|
+
import type { SignerPayloadJSON, SignerPayloadRaw } from '@polkadot/types/types';
|
|
5
|
+
import type { KeyringPairs$Json } from '@polkadot/ui-keyring/types';
|
|
6
|
+
import type { HexString } from '@polkadot/util/types';
|
|
7
|
+
import type { KeypairType } from '@polkadot/util-crypto/types';
|
|
8
|
+
import { CurrentNetworkInfo, KoniRequestSignatures } from '@subwallet/extension-base/background/KoniTypes';
|
|
9
|
+
import { TypeRegistry } from '@polkadot/types';
|
|
10
|
+
import { ALLOWED_PATH } from '../defaults';
|
|
11
|
+
import { AuthUrls } from './handlers/State';
|
|
12
|
+
declare type KeysWithDefinedValues<T> = {
|
|
13
|
+
[K in keyof T]: T[K] extends undefined ? never : K;
|
|
14
|
+
}[keyof T];
|
|
15
|
+
declare type NoUndefinedValues<T> = {
|
|
16
|
+
[K in KeysWithDefinedValues<T>]: T[K];
|
|
17
|
+
};
|
|
18
|
+
declare type IsNull<T, K extends keyof T> = {
|
|
19
|
+
[K1 in Exclude<keyof T, K>]: T[K1];
|
|
20
|
+
} & T[K] extends null ? K : never;
|
|
21
|
+
declare type NullKeys<T> = {
|
|
22
|
+
[K in keyof T]: IsNull<T, K>;
|
|
23
|
+
}[keyof T];
|
|
24
|
+
export declare type SeedLengths = 12 | 24;
|
|
25
|
+
export interface AccountJson extends KeyringPair$Meta {
|
|
26
|
+
address: string;
|
|
27
|
+
genesisHash?: string | null;
|
|
28
|
+
isExternal?: boolean;
|
|
29
|
+
isHardware?: boolean;
|
|
30
|
+
isHidden?: boolean;
|
|
31
|
+
name?: string;
|
|
32
|
+
parentAddress?: string;
|
|
33
|
+
suri?: string;
|
|
34
|
+
type?: KeypairType;
|
|
35
|
+
whenCreated?: number;
|
|
36
|
+
}
|
|
37
|
+
export interface AccountsWithCurrentAddress {
|
|
38
|
+
accounts: AccountJson[];
|
|
39
|
+
currentAddress?: string;
|
|
40
|
+
}
|
|
41
|
+
export interface CurrentAccountInfo {
|
|
42
|
+
address: string;
|
|
43
|
+
}
|
|
44
|
+
export declare type AccountWithChildren = AccountJson & {
|
|
45
|
+
children?: AccountWithChildren[];
|
|
46
|
+
};
|
|
47
|
+
export declare type AccountsContext = {
|
|
48
|
+
accounts: AccountJson[];
|
|
49
|
+
hierarchy: AccountWithChildren[];
|
|
50
|
+
master?: AccountJson;
|
|
51
|
+
};
|
|
52
|
+
export declare type CurrentAccContext = {
|
|
53
|
+
currentAccount: AccountJson | null;
|
|
54
|
+
setCurrentAccount: (account: AccountJson | null) => void;
|
|
55
|
+
};
|
|
56
|
+
export declare type AccNetworkContext = {
|
|
57
|
+
network: CurrentNetworkInfo;
|
|
58
|
+
setNetwork: (network: CurrentNetworkInfo) => void;
|
|
59
|
+
};
|
|
60
|
+
export interface AuthorizeRequest {
|
|
61
|
+
id: string;
|
|
62
|
+
request: RequestAuthorizeTab;
|
|
63
|
+
url: string;
|
|
64
|
+
}
|
|
65
|
+
export interface MetadataRequest {
|
|
66
|
+
id: string;
|
|
67
|
+
request: MetadataDef;
|
|
68
|
+
url: string;
|
|
69
|
+
}
|
|
70
|
+
export interface SigningRequest {
|
|
71
|
+
account: AccountJson;
|
|
72
|
+
id: string;
|
|
73
|
+
request: RequestSign;
|
|
74
|
+
url: string;
|
|
75
|
+
}
|
|
76
|
+
export interface RequestSignatures extends KoniRequestSignatures {
|
|
77
|
+
'pri(accounts.create.external)': [RequestAccountCreateExternal, boolean];
|
|
78
|
+
'pri(accounts.create.hardware)': [RequestAccountCreateHardware, boolean];
|
|
79
|
+
'pri(accounts.create.suri)': [RequestAccountCreateSuri, boolean];
|
|
80
|
+
'pri(accounts.edit)': [RequestAccountEdit, boolean];
|
|
81
|
+
'pri(accounts.export)': [RequestAccountExport, ResponseAccountExport];
|
|
82
|
+
'pri(accounts.batchExport)': [RequestAccountBatchExport, ResponseAccountsExport];
|
|
83
|
+
'pri(accounts.forget)': [RequestAccountForget, boolean];
|
|
84
|
+
'pri(accounts.show)': [RequestAccountShow, boolean];
|
|
85
|
+
'pri(accounts.tie)': [RequestAccountTie, boolean];
|
|
86
|
+
'pri(accounts.subscribe)': [RequestAccountSubscribe, boolean, AccountJson[]];
|
|
87
|
+
'pri(accounts.validate)': [RequestAccountValidate, boolean];
|
|
88
|
+
'pri(accounts.changePassword)': [RequestAccountChangePassword, boolean];
|
|
89
|
+
'pri(authorize.approve)': [RequestAuthorizeApprove, boolean];
|
|
90
|
+
'pri(authorize.list)': [null, ResponseAuthorizeList];
|
|
91
|
+
'pri(authorize.reject)': [RequestAuthorizeReject, boolean];
|
|
92
|
+
'pri(authorize.requests)': [RequestAuthorizeSubscribe, boolean, AuthorizeRequest[]];
|
|
93
|
+
'pri(authorize.toggle)': [string, ResponseAuthorizeList];
|
|
94
|
+
'pri(derivation.create)': [RequestDeriveCreate, boolean];
|
|
95
|
+
'pri(derivation.validate)': [RequestDeriveValidate, ResponseDeriveValidate];
|
|
96
|
+
'pri(json.restore)': [RequestJsonRestore, void];
|
|
97
|
+
'pri(json.batchRestore)': [RequestBatchRestore, void];
|
|
98
|
+
'pri(json.validate.password)': [];
|
|
99
|
+
'pri(json.account.info)': [KeyringPair$Json, ResponseJsonGetAccountInfo];
|
|
100
|
+
'pri(metadata.approve)': [RequestMetadataApprove, boolean];
|
|
101
|
+
'pri(metadata.get)': [string | null, MetadataDef | null];
|
|
102
|
+
'pri(metadata.reject)': [RequestMetadataReject, boolean];
|
|
103
|
+
'pri(metadata.requests)': [RequestMetadataSubscribe, boolean, MetadataRequest[]];
|
|
104
|
+
'pri(metadata.list)': [null, MetadataDef[]];
|
|
105
|
+
'pri(seed.create)': [RequestSeedCreate, ResponseSeedCreate];
|
|
106
|
+
'pri(seed.validate)': [RequestSeedValidate, ResponseSeedValidate];
|
|
107
|
+
'pri(settings.notification)': [string, boolean];
|
|
108
|
+
'pri(signing.approve.password)': [RequestSigningApprovePassword, boolean];
|
|
109
|
+
'pri(signing.approve.signature)': [RequestSigningApproveSignature, boolean];
|
|
110
|
+
'pri(signing.cancel)': [RequestSigningCancel, boolean];
|
|
111
|
+
'pri(signing.isLocked)': [RequestSigningIsLocked, ResponseSigningIsLocked];
|
|
112
|
+
'pri(signing.requests)': [RequestSigningSubscribe, boolean, SigningRequest[]];
|
|
113
|
+
'pri(window.open)': [AllowedPath, boolean];
|
|
114
|
+
'pub(accounts.list)': [RequestAccountList, InjectedAccount[]];
|
|
115
|
+
'pub(accounts.subscribe)': [RequestAccountSubscribe, boolean, InjectedAccount[]];
|
|
116
|
+
'pub(authorize.tab)': [RequestAuthorizeTab, null];
|
|
117
|
+
'pub(authorize.tabV2)': [RequestAuthorizeTab, null];
|
|
118
|
+
'pub(bytes.sign)': [SignerPayloadRaw, ResponseSigning];
|
|
119
|
+
'pub(extrinsic.sign)': [SignerPayloadJSON, ResponseSigning];
|
|
120
|
+
'pub(metadata.list)': [null, InjectedMetadataKnown[]];
|
|
121
|
+
'pub(metadata.provide)': [MetadataDef, boolean];
|
|
122
|
+
'pub(phishing.redirectIfDenied)': [null, boolean];
|
|
123
|
+
'pub(rpc.listProviders)': [void, ResponseRpcListProviders];
|
|
124
|
+
'pub(rpc.send)': [RequestRpcSend, JsonRpcResponse];
|
|
125
|
+
'pub(rpc.startProvider)': [string, ProviderMeta];
|
|
126
|
+
'pub(rpc.subscribe)': [RequestRpcSubscribe, number, JsonRpcResponse];
|
|
127
|
+
'pub(rpc.subscribeConnected)': [null, boolean, boolean];
|
|
128
|
+
'pub(rpc.unsubscribe)': [RequestRpcUnsubscribe, boolean];
|
|
129
|
+
}
|
|
130
|
+
export declare type MessageTypes = keyof RequestSignatures;
|
|
131
|
+
export declare type RequestTypes = {
|
|
132
|
+
[MessageType in keyof RequestSignatures]: RequestSignatures[MessageType][0];
|
|
133
|
+
};
|
|
134
|
+
export declare type MessageTypesWithNullRequest = NullKeys<RequestTypes>;
|
|
135
|
+
export interface TransportRequestMessage<TMessageType extends MessageTypes> {
|
|
136
|
+
id: string;
|
|
137
|
+
message: TMessageType;
|
|
138
|
+
origin: 'page' | 'extension' | string;
|
|
139
|
+
request: RequestTypes[TMessageType];
|
|
140
|
+
}
|
|
141
|
+
export interface RequestAuthorizeTab {
|
|
142
|
+
origin: string;
|
|
143
|
+
}
|
|
144
|
+
export interface RequestAuthorizeApprove {
|
|
145
|
+
id: string;
|
|
146
|
+
}
|
|
147
|
+
export interface RequestAuthorizeReject {
|
|
148
|
+
id: string;
|
|
149
|
+
}
|
|
150
|
+
export declare type RequestAuthorizeSubscribe = null;
|
|
151
|
+
export interface RequestMetadataApprove {
|
|
152
|
+
id: string;
|
|
153
|
+
}
|
|
154
|
+
export interface RequestCurrentAccountAddress {
|
|
155
|
+
address: string;
|
|
156
|
+
}
|
|
157
|
+
export interface RequestMetadataReject {
|
|
158
|
+
id: string;
|
|
159
|
+
}
|
|
160
|
+
export declare type RequestMetadataSubscribe = null;
|
|
161
|
+
export interface RequestAccountCreateExternal {
|
|
162
|
+
address: string;
|
|
163
|
+
genesisHash?: string | null;
|
|
164
|
+
name: string;
|
|
165
|
+
}
|
|
166
|
+
export interface RequestAccountCreateSuri {
|
|
167
|
+
name: string;
|
|
168
|
+
genesisHash?: string | null;
|
|
169
|
+
password: string;
|
|
170
|
+
suri: string;
|
|
171
|
+
type?: KeypairType;
|
|
172
|
+
}
|
|
173
|
+
export interface RequestAccountCreateHardware {
|
|
174
|
+
accountIndex: number;
|
|
175
|
+
address: string;
|
|
176
|
+
addressOffset: number;
|
|
177
|
+
genesisHash: string;
|
|
178
|
+
hardwareType: string;
|
|
179
|
+
name: string;
|
|
180
|
+
}
|
|
181
|
+
export interface RequestAccountChangePassword {
|
|
182
|
+
address: string;
|
|
183
|
+
oldPass: string;
|
|
184
|
+
newPass: string;
|
|
185
|
+
}
|
|
186
|
+
export interface RequestAccountEdit {
|
|
187
|
+
address: string;
|
|
188
|
+
genesisHash?: string | null;
|
|
189
|
+
name: string;
|
|
190
|
+
}
|
|
191
|
+
export interface RequestAccountForget {
|
|
192
|
+
address: string;
|
|
193
|
+
}
|
|
194
|
+
export interface RequestAccountShow {
|
|
195
|
+
address: string;
|
|
196
|
+
isShowing: boolean;
|
|
197
|
+
}
|
|
198
|
+
export interface RequestAccountTie {
|
|
199
|
+
address: string;
|
|
200
|
+
genesisHash: string | null;
|
|
201
|
+
}
|
|
202
|
+
export interface RequestAccountValidate {
|
|
203
|
+
address: string;
|
|
204
|
+
password: string;
|
|
205
|
+
}
|
|
206
|
+
export interface RequestDeriveCreate {
|
|
207
|
+
name: string;
|
|
208
|
+
genesisHash?: string | null;
|
|
209
|
+
suri: string;
|
|
210
|
+
parentAddress: string;
|
|
211
|
+
parentPassword: string;
|
|
212
|
+
password: string;
|
|
213
|
+
}
|
|
214
|
+
export interface RequestDeriveValidate {
|
|
215
|
+
suri: string;
|
|
216
|
+
parentAddress: string;
|
|
217
|
+
parentPassword: string;
|
|
218
|
+
}
|
|
219
|
+
export interface RequestAccountExport {
|
|
220
|
+
address: string;
|
|
221
|
+
password: string;
|
|
222
|
+
}
|
|
223
|
+
export interface RequestAccountBatchExport {
|
|
224
|
+
addresses: string[];
|
|
225
|
+
password: string;
|
|
226
|
+
}
|
|
227
|
+
export interface RequestAccountList {
|
|
228
|
+
anyType?: boolean;
|
|
229
|
+
}
|
|
230
|
+
export declare type RequestAccountSubscribe = null;
|
|
231
|
+
export interface RequestRpcSend {
|
|
232
|
+
method: string;
|
|
233
|
+
params: unknown[];
|
|
234
|
+
}
|
|
235
|
+
export interface RequestRpcSubscribe extends RequestRpcSend {
|
|
236
|
+
type: string;
|
|
237
|
+
}
|
|
238
|
+
export interface RequestRpcUnsubscribe {
|
|
239
|
+
method: string;
|
|
240
|
+
subscriptionId: number | string;
|
|
241
|
+
type: string;
|
|
242
|
+
}
|
|
243
|
+
export interface RequestSigningApprovePassword {
|
|
244
|
+
id: string;
|
|
245
|
+
password?: string;
|
|
246
|
+
savePass: boolean;
|
|
247
|
+
}
|
|
248
|
+
export interface RequestSigningApproveSignature {
|
|
249
|
+
id: string;
|
|
250
|
+
signature: HexString;
|
|
251
|
+
}
|
|
252
|
+
export interface RequestSigningCancel {
|
|
253
|
+
id: string;
|
|
254
|
+
}
|
|
255
|
+
export interface RequestSigningIsLocked {
|
|
256
|
+
id: string;
|
|
257
|
+
}
|
|
258
|
+
export interface ResponseSigningIsLocked {
|
|
259
|
+
isLocked: boolean;
|
|
260
|
+
remainingTime: number;
|
|
261
|
+
}
|
|
262
|
+
export declare type RequestSigningSubscribe = null;
|
|
263
|
+
export interface RequestSeedCreate {
|
|
264
|
+
length?: SeedLengths;
|
|
265
|
+
seed?: string;
|
|
266
|
+
type?: KeypairType;
|
|
267
|
+
}
|
|
268
|
+
export interface RequestSeedValidate {
|
|
269
|
+
suri: string;
|
|
270
|
+
type?: KeypairType;
|
|
271
|
+
}
|
|
272
|
+
export declare type ResponseTypes = {
|
|
273
|
+
[MessageType in keyof RequestSignatures]: RequestSignatures[MessageType][1];
|
|
274
|
+
};
|
|
275
|
+
export declare type ResponseType<TMessageType extends keyof RequestSignatures> = RequestSignatures[TMessageType][1];
|
|
276
|
+
interface TransportResponseMessageSub<TMessageType extends MessageTypesWithSubscriptions> {
|
|
277
|
+
error?: string;
|
|
278
|
+
id: string;
|
|
279
|
+
response?: ResponseTypes[TMessageType];
|
|
280
|
+
subscription?: SubscriptionMessageTypes[TMessageType];
|
|
281
|
+
}
|
|
282
|
+
interface TransportResponseMessageNoSub<TMessageType extends MessageTypesWithNoSubscriptions> {
|
|
283
|
+
error?: string;
|
|
284
|
+
id: string;
|
|
285
|
+
response?: ResponseTypes[TMessageType];
|
|
286
|
+
}
|
|
287
|
+
export declare type TransportResponseMessage<TMessageType extends MessageTypes> = TMessageType extends MessageTypesWithNoSubscriptions ? TransportResponseMessageNoSub<TMessageType> : TMessageType extends MessageTypesWithSubscriptions ? TransportResponseMessageSub<TMessageType> : never;
|
|
288
|
+
export interface ResponseSigning {
|
|
289
|
+
id: string;
|
|
290
|
+
signature: HexString;
|
|
291
|
+
}
|
|
292
|
+
export interface ResponseDeriveValidate {
|
|
293
|
+
address: string;
|
|
294
|
+
suri: string;
|
|
295
|
+
}
|
|
296
|
+
export interface ResponseSeedCreate {
|
|
297
|
+
address: string;
|
|
298
|
+
seed: string;
|
|
299
|
+
}
|
|
300
|
+
export interface ResponseSeedValidate {
|
|
301
|
+
address: string;
|
|
302
|
+
suri: string;
|
|
303
|
+
}
|
|
304
|
+
export interface ResponseAccountExport {
|
|
305
|
+
exportedJson: KeyringPair$Json;
|
|
306
|
+
}
|
|
307
|
+
export interface ResponseAccountsExport {
|
|
308
|
+
exportedJson: KeyringPairs$Json;
|
|
309
|
+
}
|
|
310
|
+
export declare type ResponseRpcListProviders = ProviderList;
|
|
311
|
+
export declare type SubscriptionMessageTypes = NoUndefinedValues<{
|
|
312
|
+
[MessageType in keyof RequestSignatures]: RequestSignatures[MessageType][2];
|
|
313
|
+
}>;
|
|
314
|
+
export declare type MessageTypesWithSubscriptions = keyof SubscriptionMessageTypes;
|
|
315
|
+
export declare type MessageTypesWithNoSubscriptions = Exclude<MessageTypes, keyof SubscriptionMessageTypes>;
|
|
316
|
+
export interface RequestSign {
|
|
317
|
+
readonly payload: SignerPayloadJSON | SignerPayloadRaw;
|
|
318
|
+
sign(registry: TypeRegistry, pair: KeyringPair): {
|
|
319
|
+
signature: HexString;
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
export interface RequestJsonRestore {
|
|
323
|
+
file: KeyringPair$Json;
|
|
324
|
+
password: string;
|
|
325
|
+
address: string;
|
|
326
|
+
}
|
|
327
|
+
export interface RequestBatchRestore {
|
|
328
|
+
file: KeyringPairs$Json;
|
|
329
|
+
password: string;
|
|
330
|
+
address: string;
|
|
331
|
+
}
|
|
332
|
+
export interface ResponseJsonRestore {
|
|
333
|
+
error: string | null;
|
|
334
|
+
}
|
|
335
|
+
export declare type AllowedPath = typeof ALLOWED_PATH[number];
|
|
336
|
+
export interface ResponseJsonGetAccountInfo {
|
|
337
|
+
address: string;
|
|
338
|
+
name: string;
|
|
339
|
+
genesisHash: string;
|
|
340
|
+
type: KeypairType;
|
|
341
|
+
}
|
|
342
|
+
export interface ResponseAuthorizeList {
|
|
343
|
+
list: AuthUrls;
|
|
344
|
+
}
|
|
345
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { packageInfo } from './packageInfo';
|
package/build/bundle.js
ADDED