@polkadot/extension-dapp 0.44.8 → 0.45.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/bundle-polkadot-extension-dapp.js +183 -213
- package/bundle.d.ts +48 -0
- package/bundle.js +204 -209
- package/cjs/bundle.js +219 -272
- package/cjs/detectOther.js +3 -10
- package/cjs/detectPackage.js +6 -11
- package/cjs/index.js +3 -15
- package/cjs/packageInfo.js +2 -16
- package/cjs/util.js +12 -16
- package/cjs/wrapBytes.js +8 -20
- package/detectOther.js +0 -3
- package/detectPackage.js +2 -7
- package/index.js +1 -7
- package/package.json +9 -6
- package/packageInfo.js +1 -11
- package/util.js +8 -10
- package/wrapBytes.js +0 -3
package/bundle.js
CHANGED
|
@@ -1,237 +1,232 @@
|
|
|
1
|
-
// Copyright 2019-2023 @polkadot/extension-dapp authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
1
|
import { isPromise, objectSpread, u8aEq } from '@polkadot/util';
|
|
5
2
|
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
|
|
6
|
-
import { documentReadyPromise } from
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export { packageInfo } from "./packageInfo.js";
|
|
10
|
-
export { unwrapBytes, wrapBytes } from "./wrapBytes.js";
|
|
11
|
-
|
|
12
|
-
// just a helper (otherwise we cast all-over, so shorter and more readable)
|
|
3
|
+
import { documentReadyPromise } from './util.js';
|
|
4
|
+
export { packageInfo } from './packageInfo.js';
|
|
5
|
+
export { unwrapBytes, wrapBytes } from './wrapBytes.js';
|
|
13
6
|
const win = window;
|
|
14
|
-
|
|
15
|
-
// don't clobber the existing object, but ensure non-undefined
|
|
16
7
|
win.injectedWeb3 = win.injectedWeb3 || {};
|
|
17
|
-
|
|
18
|
-
|
|
8
|
+
let isWeb3Injected = web3IsInjected();
|
|
9
|
+
let web3EnablePromise = null;
|
|
10
|
+
export { isWeb3Injected, web3EnablePromise };
|
|
11
|
+
/** @internal true when anything has been injected and is available */
|
|
19
12
|
function web3IsInjected() {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
13
|
+
return Object
|
|
14
|
+
.values(win.injectedWeb3)
|
|
15
|
+
.filter(({ connect, enable }) => !!(connect || enable))
|
|
16
|
+
.length !== 0;
|
|
24
17
|
}
|
|
25
|
-
|
|
26
|
-
// helper to throw a consistent error when not enabled
|
|
18
|
+
/** @internal throw a consistent error when not extensions have not been enabled */
|
|
27
19
|
function throwError(method) {
|
|
28
|
-
|
|
20
|
+
throw new Error(`${method}: web3Enable(originName) needs to be called before ${method}`);
|
|
29
21
|
}
|
|
30
|
-
|
|
31
|
-
// internal helper to map from Array<InjectedAccount> -> Array<InjectedAccountWithMeta>
|
|
22
|
+
/** @internal map from Array<InjectedAccount> to Array<InjectedAccountWithMeta> */
|
|
32
23
|
function mapAccounts(source, list, ss58Format) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
meta: {
|
|
41
|
-
genesisHash,
|
|
42
|
-
name,
|
|
43
|
-
source
|
|
44
|
-
},
|
|
45
|
-
type
|
|
46
|
-
}));
|
|
24
|
+
return list.map(({ address, genesisHash, name, type }) => ({
|
|
25
|
+
address: address.length === 42
|
|
26
|
+
? address
|
|
27
|
+
: encodeAddress(decodeAddress(address), ss58Format),
|
|
28
|
+
meta: { genesisHash, name, source },
|
|
29
|
+
type
|
|
30
|
+
}));
|
|
47
31
|
}
|
|
48
|
-
|
|
49
|
-
// internal helper to filter accounts
|
|
32
|
+
/** @internal filter accounts based on genesisHash and type of account */
|
|
50
33
|
function filterAccounts(list, genesisHash, type) {
|
|
51
|
-
|
|
34
|
+
return list.filter((a) => (!a.type || !type || type.includes(a.type)) &&
|
|
35
|
+
(!a.genesisHash || !genesisHash || a.genesisHash === genesisHash));
|
|
52
36
|
}
|
|
53
|
-
|
|
54
|
-
// have we found a properly constructed window.injectedWeb3
|
|
55
|
-
let isWeb3Injected = web3IsInjected();
|
|
56
|
-
|
|
57
|
-
// we keep the last promise created around (for queries)
|
|
58
|
-
let web3EnablePromise = null;
|
|
59
|
-
export { isWeb3Injected, web3EnablePromise };
|
|
37
|
+
/** @internal retrieves all the extensions available on the window */
|
|
60
38
|
function getWindowExtensions(originName) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
39
|
+
return Promise
|
|
40
|
+
.all(Object
|
|
41
|
+
.entries(win.injectedWeb3)
|
|
42
|
+
.map(([nameOrHash, { connect, enable, version }]) => Promise
|
|
43
|
+
.resolve()
|
|
44
|
+
.then(() => connect
|
|
45
|
+
// new style, returning all info
|
|
46
|
+
? connect(originName)
|
|
47
|
+
: enable
|
|
48
|
+
// previous interface, leakages on name/version
|
|
49
|
+
? enable(originName).then((e) => objectSpread({ name: nameOrHash, version: version || 'unknown' }, e))
|
|
50
|
+
: Promise.reject(new Error('No connect(..) or enable(...) hook found')))
|
|
51
|
+
.catch(({ message }) => {
|
|
52
|
+
console.error(`Error initializing ${nameOrHash}: ${message}`);
|
|
53
|
+
})))
|
|
54
|
+
.then((exts) => exts.filter((e) => !!e));
|
|
77
55
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
throw new Error('You must pass a name for your app to the web3Enable function');
|
|
83
|
-
}
|
|
84
|
-
const initCompat = compatInits.length ? Promise.all(compatInits.map(c => c().catch(() => false))) : Promise.resolve([true]);
|
|
85
|
-
web3EnablePromise = documentReadyPromise(() => initCompat.then(() => getWindowExtensions(originName).then(values => values.map(e => {
|
|
86
|
-
// if we don't have an accounts subscriber, add a single-shot version
|
|
87
|
-
if (!e.accounts.subscribe) {
|
|
88
|
-
e.accounts.subscribe = cb => {
|
|
89
|
-
e.accounts.get().then(cb).catch(console.error);
|
|
90
|
-
return () => {
|
|
91
|
-
// no ubsubscribe needed, this is a single-shot
|
|
92
|
-
};
|
|
93
|
-
};
|
|
56
|
+
/** @internal Ensure the enable promise is resolved and filter by extensions */
|
|
57
|
+
async function filterEnable(caller, extensions) {
|
|
58
|
+
if (!web3EnablePromise) {
|
|
59
|
+
return throwError(caller);
|
|
94
60
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
name,
|
|
99
|
-
version
|
|
100
|
-
}) => `${name}/${version}`);
|
|
101
|
-
isWeb3Injected = web3IsInjected();
|
|
102
|
-
console.info(`web3Enable: Enabled ${values.length} extension${values.length !== 1 ? 's' : ''}: ${names.join(', ')}`);
|
|
103
|
-
return values;
|
|
104
|
-
})));
|
|
105
|
-
return web3EnablePromise;
|
|
61
|
+
const sources = await web3EnablePromise;
|
|
62
|
+
return sources.filter(({ name }) => !extensions ||
|
|
63
|
+
extensions.includes(name));
|
|
106
64
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
return throwError('web3Accounts');
|
|
117
|
-
}
|
|
118
|
-
const accounts = [];
|
|
119
|
-
const sources = await web3EnablePromise;
|
|
120
|
-
const retrieved = await Promise.all(sources.filter(({
|
|
121
|
-
name: source
|
|
122
|
-
}) => !extensions || extensions.includes(source)).map(async ({
|
|
123
|
-
accounts,
|
|
124
|
-
name: source
|
|
125
|
-
}) => {
|
|
126
|
-
try {
|
|
127
|
-
const list = await accounts.get();
|
|
128
|
-
return mapAccounts(source, filterAccounts(list, genesisHash, accountType), ss58Format);
|
|
129
|
-
} catch (error) {
|
|
130
|
-
// cannot handle this one
|
|
131
|
-
return [];
|
|
65
|
+
/**
|
|
66
|
+
* @summary Enables all the providers found on the injected window interface
|
|
67
|
+
* @description
|
|
68
|
+
* Enables all injected extensions that has been found on the page. This
|
|
69
|
+
* should be called before making use of any other web3* functions.
|
|
70
|
+
*/
|
|
71
|
+
export function web3Enable(originName, compatInits = []) {
|
|
72
|
+
if (!originName) {
|
|
73
|
+
throw new Error('You must pass a name for your app to the web3Enable function');
|
|
132
74
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
75
|
+
const initCompat = compatInits.length
|
|
76
|
+
? Promise.all(compatInits.map((c) => c().catch(() => false)))
|
|
77
|
+
: Promise.resolve([true]);
|
|
78
|
+
web3EnablePromise = documentReadyPromise(() => initCompat.then(() => getWindowExtensions(originName)
|
|
79
|
+
.then((values) => values.map((e) => {
|
|
80
|
+
// if we don't have an accounts subscriber, add a single-shot version
|
|
81
|
+
if (!e.accounts.subscribe) {
|
|
82
|
+
e.accounts.subscribe = (cb) => {
|
|
83
|
+
e.accounts
|
|
84
|
+
.get()
|
|
85
|
+
.then(cb)
|
|
86
|
+
.catch(console.error);
|
|
87
|
+
return () => {
|
|
88
|
+
// no ubsubscribe needed, this is a single-shot
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
return e;
|
|
93
|
+
}))
|
|
94
|
+
.catch(() => [])
|
|
95
|
+
.then((values) => {
|
|
96
|
+
const names = values.map(({ name, version }) => `${name}/${version}`);
|
|
97
|
+
isWeb3Injected = web3IsInjected();
|
|
98
|
+
console.info(`web3Enable: Enabled ${values.length} extension${values.length !== 1 ? 's' : ''}: ${names.join(', ')}`);
|
|
99
|
+
return values;
|
|
100
|
+
})));
|
|
101
|
+
return web3EnablePromise;
|
|
139
102
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
accounts[source] = result;
|
|
164
|
-
try {
|
|
165
|
-
const result = triggerUpdate();
|
|
166
|
-
if (result && isPromise(result)) {
|
|
167
|
-
result.catch(console.error);
|
|
168
|
-
}
|
|
169
|
-
} catch (error) {
|
|
170
|
-
console.error(error);
|
|
171
|
-
}
|
|
172
|
-
}));
|
|
173
|
-
return () => {
|
|
174
|
-
unsubs.forEach(unsub => {
|
|
175
|
-
unsub();
|
|
103
|
+
/**
|
|
104
|
+
* @summary Retrieves all the accounts across all providers
|
|
105
|
+
* @description
|
|
106
|
+
* This returns the full list of account available (accross all extensions) to
|
|
107
|
+
* the page. Filtereing options are available of a per-extension, per type and
|
|
108
|
+
* per-genesisHash basis. Optionally the accounts can be encoded with the provided
|
|
109
|
+
* ss58Format
|
|
110
|
+
*/
|
|
111
|
+
export async function web3Accounts({ accountType, extensions, genesisHash, ss58Format } = {}) {
|
|
112
|
+
const accounts = [];
|
|
113
|
+
const sources = await filterEnable('web3Accounts', extensions);
|
|
114
|
+
const retrieved = await Promise.all(sources.map(async ({ accounts, name: source }) => {
|
|
115
|
+
try {
|
|
116
|
+
const list = await accounts.get();
|
|
117
|
+
return mapAccounts(source, filterAccounts(list, genesisHash, accountType), ss58Format);
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
// cannot handle this one
|
|
121
|
+
return [];
|
|
122
|
+
}
|
|
123
|
+
}));
|
|
124
|
+
retrieved.forEach((result) => {
|
|
125
|
+
accounts.push(...result);
|
|
176
126
|
});
|
|
177
|
-
|
|
127
|
+
console.info(`web3Accounts: Found ${accounts.length} address${accounts.length !== 1 ? 'es' : ''}`);
|
|
128
|
+
return accounts;
|
|
178
129
|
}
|
|
179
|
-
|
|
180
|
-
|
|
130
|
+
/**
|
|
131
|
+
* @summary Subscribes to all the accounts across all providers
|
|
132
|
+
* @description
|
|
133
|
+
* This is the subscription version of the web3Accounts interface with
|
|
134
|
+
* updates as to when new accounts do become available. The list of filtering
|
|
135
|
+
* options are the same as for the web3Accounts interface.
|
|
136
|
+
*/
|
|
137
|
+
export async function web3AccountsSubscribe(cb, { accountType, extensions, genesisHash, ss58Format } = {}) {
|
|
138
|
+
const sources = await filterEnable('web3AccountsSubscribe', extensions);
|
|
139
|
+
const accounts = {};
|
|
140
|
+
const triggerUpdate = () => cb(Object
|
|
141
|
+
.entries(accounts)
|
|
142
|
+
.reduce((result, [source, list]) => {
|
|
143
|
+
result.push(...mapAccounts(source, filterAccounts(list, genesisHash, accountType), ss58Format));
|
|
144
|
+
return result;
|
|
145
|
+
}, []));
|
|
146
|
+
const unsubs = sources.map(({ accounts: { subscribe }, name: source }) => subscribe((result) => {
|
|
147
|
+
accounts[source] = result;
|
|
148
|
+
try {
|
|
149
|
+
const result = triggerUpdate();
|
|
150
|
+
if (result && isPromise(result)) {
|
|
151
|
+
result.catch(console.error);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
console.error(error);
|
|
156
|
+
}
|
|
157
|
+
}));
|
|
158
|
+
return () => {
|
|
159
|
+
unsubs.forEach((unsub) => {
|
|
160
|
+
unsub();
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* @summary Finds a specific provider based on the name
|
|
166
|
+
* @description
|
|
167
|
+
* This retrieves a specific source (extension) based on the name. In most
|
|
168
|
+
* cases it should not be needed to call it directly (e.g. it is used internally
|
|
169
|
+
* by calls such as web3FromAddress) but would allow operation on a specific
|
|
170
|
+
* known extension.
|
|
171
|
+
*/
|
|
181
172
|
export async function web3FromSource(source) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
return found;
|
|
173
|
+
if (!web3EnablePromise) {
|
|
174
|
+
return throwError('web3FromSource');
|
|
175
|
+
}
|
|
176
|
+
const sources = await web3EnablePromise;
|
|
177
|
+
const found = source && sources.find(({ name }) => name === source);
|
|
178
|
+
if (!found) {
|
|
179
|
+
throw new Error(`web3FromSource: Unable to find an injected ${source}`);
|
|
180
|
+
}
|
|
181
|
+
return found;
|
|
193
182
|
}
|
|
194
|
-
|
|
195
|
-
|
|
183
|
+
/**
|
|
184
|
+
* @summary Find a specific provider that provides a specific address
|
|
185
|
+
* @description
|
|
186
|
+
* Based on an address, return the provider that has makes this address
|
|
187
|
+
* available to the page.
|
|
188
|
+
*/
|
|
196
189
|
export async function web3FromAddress(address) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
190
|
+
if (!web3EnablePromise) {
|
|
191
|
+
return throwError('web3FromAddress');
|
|
192
|
+
}
|
|
193
|
+
const accounts = await web3Accounts();
|
|
194
|
+
let found;
|
|
195
|
+
if (address) {
|
|
196
|
+
const accountU8a = decodeAddress(address);
|
|
197
|
+
found = accounts.find((account) => u8aEq(decodeAddress(account.address), accountU8a));
|
|
198
|
+
}
|
|
199
|
+
if (!found) {
|
|
200
|
+
throw new Error(`web3FromAddress: Unable to find injected ${address}`);
|
|
201
|
+
}
|
|
202
|
+
return web3FromSource(found.meta.source);
|
|
210
203
|
}
|
|
211
|
-
|
|
212
|
-
|
|
204
|
+
/**
|
|
205
|
+
* @summary List all providers exposed by one source
|
|
206
|
+
* @description
|
|
207
|
+
* For extensions that supply RPC providers, this call would return the list
|
|
208
|
+
* of RPC providers that any extension may supply.
|
|
209
|
+
*/
|
|
213
210
|
export async function web3ListRpcProviders(source) {
|
|
214
|
-
|
|
215
|
-
provider
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
return
|
|
220
|
-
}
|
|
221
|
-
return provider.listProviders();
|
|
211
|
+
const { provider } = await web3FromSource(source);
|
|
212
|
+
if (!provider) {
|
|
213
|
+
console.warn(`Extension ${source} does not expose any provider`);
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
return provider.listProviders();
|
|
222
217
|
}
|
|
223
|
-
|
|
224
|
-
|
|
218
|
+
/**
|
|
219
|
+
* @summary Start an RPC provider provider by a specific source
|
|
220
|
+
* @description
|
|
221
|
+
* For extensions that supply RPC providers, this call would return an
|
|
222
|
+
* enabled provider (initialized with the specific key) from the
|
|
223
|
+
* specified extension source.
|
|
224
|
+
*/
|
|
225
225
|
export async function web3UseRpcProvider(source, key) {
|
|
226
|
-
|
|
227
|
-
provider
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
const meta = await provider.startProvider(key);
|
|
233
|
-
return {
|
|
234
|
-
meta,
|
|
235
|
-
provider
|
|
236
|
-
};
|
|
226
|
+
const { provider } = await web3FromSource(source);
|
|
227
|
+
if (!provider) {
|
|
228
|
+
throw new Error(`Extension ${source} does not expose any provider`);
|
|
229
|
+
}
|
|
230
|
+
const meta = await provider.startProvider(key);
|
|
231
|
+
return { meta, provider };
|
|
237
232
|
}
|