@subwallet/extension-base 0.3.6-0 → 0.3.6-1
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 +0 -0
- package/{build/background → background}/handlers/Extension.js +0 -0
- 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 +0 -0
- 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 +0 -0
- 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 +171 -7
- package/{build/packageInfo.d.ts → packageInfo.d.ts} +0 -0
- package/{build/packageInfo.js → packageInfo.js} +0 -0
- 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 +0 -0
- 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/Extension.js +0 -764
- 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/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,178 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import type { InjectedProvider, ProviderList, ProviderMeta } from '@subwallet/extension-inject/types';
|
|
5
|
-
import type { ProviderInterfaceEmitCb, ProviderInterfaceEmitted } from '@polkadot/rpc-provider/types';
|
|
6
|
-
import type { AnyFunction } from '@polkadot/types/types';
|
|
7
|
-
import type { SendRequest } from './types';
|
|
8
|
-
|
|
9
|
-
import EventEmitter from 'eventemitter3';
|
|
10
|
-
|
|
11
|
-
import { isUndefined, logger } from '@polkadot/util';
|
|
12
|
-
|
|
13
|
-
const l = logger('PostMessageProvider');
|
|
14
|
-
|
|
15
|
-
type CallbackHandler = (error?: null | Error, value?: unknown) => void;
|
|
16
|
-
|
|
17
|
-
// Same as https://github.com/polkadot-js/api/blob/57ca9a9c3204339e1e1f693fcacc33039868dc27/packages/rpc-provider/src/ws/Provider.ts#L17
|
|
18
|
-
interface SubscriptionHandler {
|
|
19
|
-
callback: CallbackHandler;
|
|
20
|
-
type: string;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// External to class, this.# is not private enough (yet)
|
|
24
|
-
let sendRequest: SendRequest;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* @name PostMessageProvider
|
|
28
|
-
*
|
|
29
|
-
* @description Extension provider to be used by dapps
|
|
30
|
-
*/
|
|
31
|
-
export default class PostMessageProvider implements InjectedProvider {
|
|
32
|
-
readonly #eventemitter: EventEmitter;
|
|
33
|
-
|
|
34
|
-
// Whether or not the actual extension background provider is connected
|
|
35
|
-
#isConnected = false;
|
|
36
|
-
|
|
37
|
-
// Subscription IDs are (historically) not guaranteed to be globally unique;
|
|
38
|
-
// only unique for a given subscription method; which is why we identify
|
|
39
|
-
// the subscriptions based on subscription id + type
|
|
40
|
-
readonly #subscriptions: Record<string, AnyFunction> = {}; // {[(type,subscriptionId)]: callback}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* @param {function} sendRequest The function to be called to send requests to the node
|
|
44
|
-
* @param {function} subscriptionNotificationHandler Channel for receiving subscription messages
|
|
45
|
-
*/
|
|
46
|
-
public constructor (_sendRequest: SendRequest) {
|
|
47
|
-
this.#eventemitter = new EventEmitter();
|
|
48
|
-
|
|
49
|
-
sendRequest = _sendRequest;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* @description Returns a clone of the object
|
|
54
|
-
*/
|
|
55
|
-
public clone (): PostMessageProvider {
|
|
56
|
-
return new PostMessageProvider(sendRequest);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* @description Manually disconnect from the connection, clearing autoconnect logic
|
|
61
|
-
*/
|
|
62
|
-
// eslint-disable-next-line @typescript-eslint/require-await
|
|
63
|
-
public async connect (): Promise<void> {
|
|
64
|
-
// FIXME This should see if the extension's state's provider can disconnect
|
|
65
|
-
console.error('PostMessageProvider.disconnect() is not implemented.');
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* @description Manually disconnect from the connection, clearing autoconnect logic
|
|
70
|
-
*/
|
|
71
|
-
// eslint-disable-next-line @typescript-eslint/require-await
|
|
72
|
-
public async disconnect (): Promise<void> {
|
|
73
|
-
// FIXME This should see if the extension's state's provider can disconnect
|
|
74
|
-
console.error('PostMessageProvider.disconnect() is not implemented.');
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* @summary `true` when this provider supports subscriptions
|
|
79
|
-
*/
|
|
80
|
-
public get hasSubscriptions (): boolean {
|
|
81
|
-
// FIXME This should see if the extension's state's provider has subscriptions
|
|
82
|
-
return true;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* @summary Whether the node is connected or not.
|
|
87
|
-
* @return {boolean} true if connected
|
|
88
|
-
*/
|
|
89
|
-
public get isConnected (): boolean {
|
|
90
|
-
return this.#isConnected;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
public listProviders (): Promise<ProviderList> {
|
|
94
|
-
return sendRequest('pub(rpc.listProviders)', undefined);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* @summary Listens on events after having subscribed using the [[subscribe]] function.
|
|
99
|
-
* @param {ProviderInterfaceEmitted} type Event
|
|
100
|
-
* @param {ProviderInterfaceEmitCb} sub Callback
|
|
101
|
-
* @return unsubscribe function
|
|
102
|
-
*/
|
|
103
|
-
public on (type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void {
|
|
104
|
-
this.#eventemitter.on(type, sub);
|
|
105
|
-
|
|
106
|
-
return (): void => {
|
|
107
|
-
this.#eventemitter.removeListener(type, sub);
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
112
|
-
public async send (method: string, params: unknown[], _?: boolean, subscription?: SubscriptionHandler): Promise<any> {
|
|
113
|
-
if (subscription) {
|
|
114
|
-
const { callback, type } = subscription;
|
|
115
|
-
|
|
116
|
-
const id = await sendRequest('pub(rpc.subscribe)', { method, params, type }, (res): void => {
|
|
117
|
-
subscription.callback(null, res);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
this.#subscriptions[`${type}::${id}`] = callback;
|
|
121
|
-
|
|
122
|
-
return id;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return sendRequest('pub(rpc.send)', { method, params });
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* @summary Spawn a provider on the extension background.
|
|
130
|
-
*/
|
|
131
|
-
public async startProvider (key: string): Promise<ProviderMeta> {
|
|
132
|
-
// Disconnect from the previous provider
|
|
133
|
-
this.#isConnected = false;
|
|
134
|
-
this.#eventemitter.emit('disconnected');
|
|
135
|
-
|
|
136
|
-
const meta = await sendRequest('pub(rpc.startProvider)', key);
|
|
137
|
-
|
|
138
|
-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
139
|
-
sendRequest('pub(rpc.subscribeConnected)', null, (connected) => {
|
|
140
|
-
this.#isConnected = connected;
|
|
141
|
-
|
|
142
|
-
if (connected) {
|
|
143
|
-
this.#eventemitter.emit('connected');
|
|
144
|
-
} else {
|
|
145
|
-
this.#eventemitter.emit('disconnected');
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return true;
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
return meta;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
public subscribe (type: string, method: string, params: unknown[], callback: AnyFunction): Promise<number> {
|
|
155
|
-
return this.send(method, params, false, { callback, type }) as Promise<number>;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* @summary Allows unsubscribing to subscriptions made with [[subscribe]].
|
|
160
|
-
*/
|
|
161
|
-
public async unsubscribe (type: string, method: string, id: number): Promise<boolean> {
|
|
162
|
-
const subscription = `${type}::${id}`;
|
|
163
|
-
|
|
164
|
-
// FIXME This now could happen with re-subscriptions. The issue is that with a re-sub
|
|
165
|
-
// the assigned id now does not match what the API user originally received. It has
|
|
166
|
-
// a slight complication in solving - since we cannot rely on the send id, but rather
|
|
167
|
-
// need to find the actual subscription id to map it
|
|
168
|
-
if (isUndefined(this.#subscriptions[subscription])) {
|
|
169
|
-
l.debug((): string => `Unable to find active subscription=${subscription}`);
|
|
170
|
-
|
|
171
|
-
return false;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
delete this.#subscriptions[subscription];
|
|
175
|
-
|
|
176
|
-
return this.send(method, [id]) as Promise<boolean>;
|
|
177
|
-
}
|
|
178
|
-
}
|
package/src/page/Signer.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import type { Signer as SignerInterface, SignerResult } from '@polkadot/api/types';
|
|
5
|
-
import type { SignerPayloadJSON, SignerPayloadRaw } from '@polkadot/types/types';
|
|
6
|
-
import type { SendRequest } from './types';
|
|
7
|
-
|
|
8
|
-
// External to class, this.# is not private enough (yet)
|
|
9
|
-
let sendRequest: SendRequest;
|
|
10
|
-
let nextId = 0;
|
|
11
|
-
|
|
12
|
-
export default class Signer implements SignerInterface {
|
|
13
|
-
constructor (_sendRequest: SendRequest) {
|
|
14
|
-
sendRequest = _sendRequest;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public async signPayload (payload: SignerPayloadJSON): Promise<SignerResult> {
|
|
18
|
-
const id = ++nextId;
|
|
19
|
-
const result = await sendRequest('pub(extrinsic.sign)', payload);
|
|
20
|
-
|
|
21
|
-
// we add an internal id (number) - should have a mapping from the
|
|
22
|
-
// extension id (string) -> internal id (number) if we wish to provide
|
|
23
|
-
// updated via the update functionality (noop at this point)
|
|
24
|
-
return {
|
|
25
|
-
...result,
|
|
26
|
-
id
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public async signRaw (payload: SignerPayloadRaw): Promise<SignerResult> {
|
|
31
|
-
const id = ++nextId;
|
|
32
|
-
const result = await sendRequest('pub(bytes.sign)', payload);
|
|
33
|
-
|
|
34
|
-
return {
|
|
35
|
-
...result,
|
|
36
|
-
id
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// NOTE We don't listen to updates at all, if we do we can interpret the
|
|
41
|
-
// resuklt as provided by the API here
|
|
42
|
-
// public update (id: number, status: Hash | SubmittableResult): void {
|
|
43
|
-
// // ignore
|
|
44
|
-
// }
|
|
45
|
-
}
|
package/src/page/index.ts
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2022 @subwallet/extension authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import type { MessageTypes, MessageTypesWithNoSubscriptions, MessageTypesWithNullRequest, MessageTypesWithSubscriptions, RequestTypes, ResponseTypes, SubscriptionMessageTypes, TransportRequestMessage, TransportResponseMessage } from '../background/types';
|
|
5
|
-
|
|
6
|
-
import { MESSAGE_ORIGIN_PAGE } from '../defaults';
|
|
7
|
-
import { getId } from '../utils/getId';
|
|
8
|
-
import Injected from './Injected';
|
|
9
|
-
|
|
10
|
-
// when sending a message from the injector to the extension, we
|
|
11
|
-
// - create an event - this we send to the loader
|
|
12
|
-
// - the loader takes this event and uses port.postMessage to background
|
|
13
|
-
// - on response, the loader creates a reponse event
|
|
14
|
-
// - this injector, listens on the events, maps it to the original
|
|
15
|
-
// - resolves/rejects the promise with the result (or sub data)
|
|
16
|
-
|
|
17
|
-
export interface Handler {
|
|
18
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
-
resolve: (data?: any) => void;
|
|
20
|
-
reject: (error: Error) => void;
|
|
21
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
-
subscriber?: (data: any) => void;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export type Handlers = Record<string, Handler>;
|
|
26
|
-
|
|
27
|
-
const handlers: Handlers = {};
|
|
28
|
-
|
|
29
|
-
// a generic message sender that creates an event, returning a promise that will
|
|
30
|
-
// resolve once the event is resolved (by the response listener just below this)
|
|
31
|
-
export function sendMessage<TMessageType extends MessageTypesWithNullRequest>(message: TMessageType): Promise<ResponseTypes[TMessageType]>;
|
|
32
|
-
export function sendMessage<TMessageType extends MessageTypesWithNoSubscriptions>(message: TMessageType, request: RequestTypes[TMessageType]): Promise<ResponseTypes[TMessageType]>;
|
|
33
|
-
export function sendMessage<TMessageType extends MessageTypesWithSubscriptions>(message: TMessageType, request: RequestTypes[TMessageType], subscriber: (data: SubscriptionMessageTypes[TMessageType]) => void): Promise<ResponseTypes[TMessageType]>;
|
|
34
|
-
|
|
35
|
-
export function sendMessage<TMessageType extends MessageTypes> (message: TMessageType, request?: RequestTypes[TMessageType], subscriber?: (data: unknown) => void): Promise<ResponseTypes[TMessageType]> {
|
|
36
|
-
return new Promise((resolve, reject): void => {
|
|
37
|
-
const id = getId();
|
|
38
|
-
|
|
39
|
-
handlers[id] = { reject, resolve, subscriber };
|
|
40
|
-
|
|
41
|
-
const transportRequestMessage: TransportRequestMessage<TMessageType> = {
|
|
42
|
-
id,
|
|
43
|
-
message,
|
|
44
|
-
origin: MESSAGE_ORIGIN_PAGE,
|
|
45
|
-
request: request || null as RequestTypes[TMessageType]
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
window.postMessage(transportRequestMessage, '*');
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// the enable function, called by the dapp to allow access
|
|
53
|
-
export async function enable (origin: string): Promise<Injected> {
|
|
54
|
-
await sendMessage('pub(authorize.tabV2)', { origin });
|
|
55
|
-
|
|
56
|
-
return new Injected(sendMessage);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// redirect users if this page is considered as phishing, otherwise return false
|
|
60
|
-
export async function redirectIfPhishing (): Promise<boolean> {
|
|
61
|
-
const res = await sendMessage('pub(phishing.redirectIfDenied)');
|
|
62
|
-
|
|
63
|
-
return res;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function handleResponse<TMessageType extends MessageTypes> (data: TransportResponseMessage<TMessageType> & { subscription?: string }): void {
|
|
67
|
-
const handler = handlers[data.id];
|
|
68
|
-
|
|
69
|
-
if (!handler) {
|
|
70
|
-
console.error(`Unknown response: ${JSON.stringify(data)}`);
|
|
71
|
-
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (!handler.subscriber) {
|
|
76
|
-
delete handlers[data.id];
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (data.subscription) {
|
|
80
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
81
|
-
(handler.subscriber as Function)(data.subscription);
|
|
82
|
-
} else if (data.error) {
|
|
83
|
-
handler.reject(new Error(data.error));
|
|
84
|
-
} else {
|
|
85
|
-
handler.resolve(data.response);
|
|
86
|
-
}
|
|
87
|
-
}
|
package/src/page/types.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import type { MessageTypesWithNoSubscriptions, MessageTypesWithNullRequest, MessageTypesWithSubscriptions, RequestTypes, ResponseTypes, SubscriptionMessageTypes } from '../background/types';
|
|
5
|
-
|
|
6
|
-
export interface SendRequest {
|
|
7
|
-
<TMessageType extends MessageTypesWithNullRequest>(message: TMessageType): Promise<ResponseTypes[TMessageType]>;
|
|
8
|
-
<TMessageType extends MessageTypesWithNoSubscriptions>(message: TMessageType, request: RequestTypes[TMessageType]): Promise<ResponseTypes[TMessageType]>;
|
|
9
|
-
<TMessageType extends MessageTypesWithSubscriptions>(message: TMessageType, request: RequestTypes[TMessageType], subscriber: (data: SubscriptionMessageTypes[TMessageType]) => void): Promise<ResponseTypes[TMessageType]>;
|
|
10
|
-
}
|
package/src/stores/Accounts.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import type { KeyringJson, KeyringStore } from '@polkadot/ui-keyring/types';
|
|
5
|
-
|
|
6
|
-
import { EXTENSION_PREFIX } from '../defaults';
|
|
7
|
-
import BaseStore from './Base';
|
|
8
|
-
|
|
9
|
-
export default class AccountsStore extends BaseStore<KeyringJson> implements KeyringStore {
|
|
10
|
-
constructor () {
|
|
11
|
-
super(EXTENSION_PREFIX ? `${EXTENSION_PREFIX}accounts` : null);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public override set (key: string, value: KeyringJson, update?: () => void): void {
|
|
15
|
-
// shortcut, don't save testing accounts in extension storage
|
|
16
|
-
if (key.startsWith('account:') && value.meta && value.meta.isTesting) {
|
|
17
|
-
update && update();
|
|
18
|
-
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
super.set(key, value, update);
|
|
23
|
-
}
|
|
24
|
-
}
|
package/src/stores/Base.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
type StoreValue = Record<string, unknown>;
|
|
5
|
-
|
|
6
|
-
const lastError = (type: string): void => {
|
|
7
|
-
const error = chrome.runtime.lastError;
|
|
8
|
-
|
|
9
|
-
if (error) {
|
|
10
|
-
console.error(`BaseStore.${type}:: runtime.lastError:`, error);
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export default abstract class BaseStore <T> {
|
|
15
|
-
#prefix: string;
|
|
16
|
-
|
|
17
|
-
constructor (prefix: string | null) {
|
|
18
|
-
this.#prefix = prefix ? `${prefix}:` : '';
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public getPrefix (): string {
|
|
22
|
-
return this.#prefix;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public all (update: (key: string, value: T) => void): void {
|
|
26
|
-
this.allMap((map): void => {
|
|
27
|
-
Object.entries(map).forEach(([key, value]): void => {
|
|
28
|
-
update(key, value);
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
public allMap (update: (value: Record<string, T>) => void): void {
|
|
34
|
-
chrome.storage.local.get(null, (result: StoreValue): void => {
|
|
35
|
-
lastError('all');
|
|
36
|
-
|
|
37
|
-
const entries = Object.entries(result);
|
|
38
|
-
const map: Record<string, T> = {};
|
|
39
|
-
|
|
40
|
-
for (let i = 0; i < entries.length; i++) {
|
|
41
|
-
const [key, value] = entries[i];
|
|
42
|
-
|
|
43
|
-
if (key.startsWith(this.#prefix)) {
|
|
44
|
-
map[key.replace(this.#prefix, '')] = value as T;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
update(map);
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
public get (_key: string, update: (value: T) => void): void {
|
|
53
|
-
const key = `${this.#prefix}${_key}`;
|
|
54
|
-
|
|
55
|
-
chrome.storage.local.get([key], (result: StoreValue): void => {
|
|
56
|
-
lastError('get');
|
|
57
|
-
|
|
58
|
-
update(result[key] as T);
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
public remove (_key: string, update?: () => void): void {
|
|
63
|
-
const key = `${this.#prefix}${_key}`;
|
|
64
|
-
|
|
65
|
-
chrome.storage.local.remove(key, (): void => {
|
|
66
|
-
lastError('remove');
|
|
67
|
-
|
|
68
|
-
update && update();
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
public set (_key: string, value: T, update?: () => void): void {
|
|
73
|
-
const key = `${this.#prefix}${_key}`;
|
|
74
|
-
|
|
75
|
-
chrome.storage.local.set({ [key]: value }, (): void => {
|
|
76
|
-
lastError('set');
|
|
77
|
-
|
|
78
|
-
update && update();
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
package/src/stores/Metadata.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2022 @subwallet/extension-base authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import type { MetadataDef } from '@subwallet/extension-inject/types';
|
|
5
|
-
|
|
6
|
-
import { EXTENSION_PREFIX } from '../defaults';
|
|
7
|
-
import BaseStore from './Base';
|
|
8
|
-
|
|
9
|
-
export default class MetadataStore extends BaseStore<MetadataDef> {
|
|
10
|
-
constructor () {
|
|
11
|
-
super(`${EXTENSION_PREFIX}metadata`);
|
|
12
|
-
}
|
|
13
|
-
}
|
package/src/stores/index.ts
DELETED
package/src/types.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2022 @subwallet/extension authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
export interface Message extends MessageEvent {
|
|
5
|
-
data: {
|
|
6
|
-
error?: string;
|
|
7
|
-
id: string;
|
|
8
|
-
origin: string;
|
|
9
|
-
response?: string;
|
|
10
|
-
subscription?: string;
|
|
11
|
-
}
|
|
12
|
-
}
|
package/src/utils/canDerive.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2022 @subwallet/extension authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import type { KeypairType } from '@polkadot/util-crypto/types';
|
|
5
|
-
|
|
6
|
-
export function canDerive (type?: KeypairType): boolean {
|
|
7
|
-
return !!type && ['ed25519', 'sr25519', 'ecdsa', 'ethereum'].includes(type);
|
|
8
|
-
}
|
package/src/utils/getId.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
// Copyright 2019-2022 @subwallet/extension authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import { EXTENSION_PREFIX } from '../defaults';
|
|
5
|
-
|
|
6
|
-
let counter = 0;
|
|
7
|
-
|
|
8
|
-
export function getId (): string {
|
|
9
|
-
return `${EXTENSION_PREFIX}.${Date.now()}.${++counter}`;
|
|
10
|
-
}
|
package/src/utils/index.ts
DELETED
package/tsconfig.build.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.base.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"baseUrl": "..",
|
|
5
|
-
"outDir": "./build",
|
|
6
|
-
"rootDir": "./src"
|
|
7
|
-
},
|
|
8
|
-
"references": [
|
|
9
|
-
{ "path": "../extension-chains/tsconfig.build.json"
|
|
10
|
-
},
|
|
11
|
-
{ "path": "../extension-dapp/tsconfig.build.json"
|
|
12
|
-
},
|
|
13
|
-
{ "path": "../extension-inject/tsconfig.build.json"
|
|
14
|
-
},
|
|
15
|
-
{ "path": "../extension-mocks/tsconfig.build.json"
|
|
16
|
-
}
|
|
17
|
-
]
|
|
18
|
-
}
|