@polkadot/extension-dapp 0.42.5-3 → 0.42.5-4
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 +5 -12
- package/bundle.cjs +297 -0
- package/bundle.d.ts +13 -0
- package/bundle.js +222 -0
- package/index.cjs +9 -288
- package/index.d.ts +1 -13
- package/index.js +1 -220
- package/package.json +7 -2
- package/packageInfo.cjs +1 -1
- package/packageInfo.js +1 -1
|
@@ -3,18 +3,6 @@ const polkadotExtensionDapp = (function (exports, util, utilCrypto) {
|
|
|
3
3
|
|
|
4
4
|
const global = window;
|
|
5
5
|
|
|
6
|
-
const packageInfo$1 = {
|
|
7
|
-
name: '@polkadot/extension-inject',
|
|
8
|
-
version: '0.42.5-3'
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
const packageInfo = {
|
|
12
|
-
name: '@polkadot/extension-dapp',
|
|
13
|
-
version: '0.42.5-3'
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
util.detectPackage(packageInfo, typeof __dirname !== 'undefined' && __dirname, [packageInfo$1]);
|
|
17
|
-
|
|
18
6
|
function documentReadyPromise(creator) {
|
|
19
7
|
return new Promise(resolve => {
|
|
20
8
|
if (document.readyState === 'complete') {
|
|
@@ -25,6 +13,11 @@ const polkadotExtensionDapp = (function (exports, util, utilCrypto) {
|
|
|
25
13
|
});
|
|
26
14
|
}
|
|
27
15
|
|
|
16
|
+
const packageInfo = {
|
|
17
|
+
name: '@polkadot/extension-dapp',
|
|
18
|
+
version: '0.42.5-4'
|
|
19
|
+
};
|
|
20
|
+
|
|
28
21
|
const unwrapBytes = util.u8aUnwrapBytes;
|
|
29
22
|
const wrapBytes = util.u8aWrapBytes;
|
|
30
23
|
|
package/bundle.cjs
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.isWeb3Injected = void 0;
|
|
7
|
+
Object.defineProperty(exports, "packageInfo", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () {
|
|
10
|
+
return _packageInfo.packageInfo;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(exports, "unwrapBytes", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function () {
|
|
16
|
+
return _wrapBytes.unwrapBytes;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
exports.web3Accounts = web3Accounts;
|
|
20
|
+
exports.web3AccountsSubscribe = web3AccountsSubscribe;
|
|
21
|
+
exports.web3Enable = web3Enable;
|
|
22
|
+
exports.web3EnablePromise = void 0;
|
|
23
|
+
exports.web3FromAddress = web3FromAddress;
|
|
24
|
+
exports.web3FromSource = web3FromSource;
|
|
25
|
+
exports.web3ListRpcProviders = web3ListRpcProviders;
|
|
26
|
+
exports.web3UseRpcProvider = web3UseRpcProvider;
|
|
27
|
+
Object.defineProperty(exports, "wrapBytes", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function () {
|
|
30
|
+
return _wrapBytes.wrapBytes;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
var _util = require("@polkadot/util");
|
|
35
|
+
|
|
36
|
+
var _utilCrypto = require("@polkadot/util-crypto");
|
|
37
|
+
|
|
38
|
+
var _util2 = require("./util.cjs");
|
|
39
|
+
|
|
40
|
+
var _packageInfo = require("./packageInfo.cjs");
|
|
41
|
+
|
|
42
|
+
var _wrapBytes = require("./wrapBytes.cjs");
|
|
43
|
+
|
|
44
|
+
// Copyright 2019-2021 @polkadot/extension-dapp authors & contributors
|
|
45
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
46
|
+
// expose utility functions
|
|
47
|
+
// just a helper (otherwise we cast all-over, so shorter and more readable)
|
|
48
|
+
const win = window; // don't clobber the existing object, but ensure non-undefined
|
|
49
|
+
|
|
50
|
+
win.injectedWeb3 = win.injectedWeb3 || {}; // true when anything has been injected and is available
|
|
51
|
+
|
|
52
|
+
function web3IsInjected() {
|
|
53
|
+
return Object.keys(win.injectedWeb3).length !== 0;
|
|
54
|
+
} // helper to throw a consistent error when not enabled
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
function throwError(method) {
|
|
58
|
+
throw new Error(`${method}: web3Enable(originName) needs to be called before ${method}`);
|
|
59
|
+
} // internal helper to map from Array<InjectedAccount> -> Array<InjectedAccountWithMeta>
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
function mapAccounts(source, list, ss58Format) {
|
|
63
|
+
return list.map(_ref => {
|
|
64
|
+
let {
|
|
65
|
+
address,
|
|
66
|
+
genesisHash,
|
|
67
|
+
name,
|
|
68
|
+
type
|
|
69
|
+
} = _ref;
|
|
70
|
+
const encodedAddress = address.length === 42 ? address : (0, _utilCrypto.encodeAddress)((0, _utilCrypto.decodeAddress)(address), ss58Format);
|
|
71
|
+
return {
|
|
72
|
+
address: encodedAddress,
|
|
73
|
+
meta: {
|
|
74
|
+
genesisHash,
|
|
75
|
+
name,
|
|
76
|
+
source
|
|
77
|
+
},
|
|
78
|
+
type
|
|
79
|
+
};
|
|
80
|
+
});
|
|
81
|
+
} // have we found a properly constructed window.injectedWeb3
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
let isWeb3Injected = web3IsInjected(); // we keep the last promise created around (for queries)
|
|
85
|
+
|
|
86
|
+
exports.isWeb3Injected = isWeb3Injected;
|
|
87
|
+
let web3EnablePromise = null;
|
|
88
|
+
exports.web3EnablePromise = web3EnablePromise;
|
|
89
|
+
|
|
90
|
+
function getWindowExtensions(originName) {
|
|
91
|
+
return Promise.all(Object.entries(win.injectedWeb3).map(_ref2 => {
|
|
92
|
+
let [name, {
|
|
93
|
+
enable,
|
|
94
|
+
version
|
|
95
|
+
}] = _ref2;
|
|
96
|
+
return Promise.all([Promise.resolve({
|
|
97
|
+
name,
|
|
98
|
+
version
|
|
99
|
+
}), enable(originName).catch(error => {
|
|
100
|
+
console.error(`Error initializing ${name}: ${error.message}`);
|
|
101
|
+
})]);
|
|
102
|
+
}));
|
|
103
|
+
} // enables all the providers found on the injected window interface
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
function web3Enable(originName) {
|
|
107
|
+
let compatInits = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
108
|
+
|
|
109
|
+
if (!originName) {
|
|
110
|
+
throw new Error('You must pass a name for your app to the web3Enable function');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const initCompat = compatInits.length ? Promise.all(compatInits.map(c => c().catch(() => false))) : Promise.resolve([true]);
|
|
114
|
+
exports.web3EnablePromise = web3EnablePromise = (0, _util2.documentReadyPromise)(() => initCompat.then(() => getWindowExtensions(originName).then(values => values.filter(value => !!value[1]).map(_ref3 => {
|
|
115
|
+
let [info, ext] = _ref3;
|
|
116
|
+
|
|
117
|
+
// if we don't have an accounts subscriber, add a single-shot version
|
|
118
|
+
if (!ext.accounts.subscribe) {
|
|
119
|
+
ext.accounts.subscribe = cb => {
|
|
120
|
+
ext.accounts.get().then(cb).catch(console.error);
|
|
121
|
+
return () => {// no ubsubscribe needed, this is a single-shot
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return { ...info,
|
|
127
|
+
...ext
|
|
128
|
+
};
|
|
129
|
+
})).catch(() => []).then(values => {
|
|
130
|
+
const names = values.map(_ref4 => {
|
|
131
|
+
let {
|
|
132
|
+
name,
|
|
133
|
+
version
|
|
134
|
+
} = _ref4;
|
|
135
|
+
return `${name}/${version}`;
|
|
136
|
+
});
|
|
137
|
+
exports.isWeb3Injected = isWeb3Injected = web3IsInjected();
|
|
138
|
+
console.log(`web3Enable: Enabled ${values.length} extension${values.length !== 1 ? 's' : ''}: ${names.join(', ')}`);
|
|
139
|
+
return values;
|
|
140
|
+
})));
|
|
141
|
+
return web3EnablePromise;
|
|
142
|
+
} // retrieve all the accounts across all providers
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
async function web3Accounts() {
|
|
146
|
+
let {
|
|
147
|
+
accountType,
|
|
148
|
+
ss58Format
|
|
149
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
150
|
+
|
|
151
|
+
if (!web3EnablePromise) {
|
|
152
|
+
return throwError('web3Accounts');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const accounts = [];
|
|
156
|
+
const injected = await web3EnablePromise;
|
|
157
|
+
const retrieved = await Promise.all(injected.map(async _ref5 => {
|
|
158
|
+
let {
|
|
159
|
+
accounts,
|
|
160
|
+
name: source
|
|
161
|
+
} = _ref5;
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
const list = await accounts.get();
|
|
165
|
+
return mapAccounts(source, list.filter(_ref6 => {
|
|
166
|
+
let {
|
|
167
|
+
type
|
|
168
|
+
} = _ref6;
|
|
169
|
+
return type && accountType ? accountType.includes(type) : true;
|
|
170
|
+
}), ss58Format);
|
|
171
|
+
} catch (error) {
|
|
172
|
+
// cannot handle this one
|
|
173
|
+
return [];
|
|
174
|
+
}
|
|
175
|
+
}));
|
|
176
|
+
retrieved.forEach(result => {
|
|
177
|
+
accounts.push(...result);
|
|
178
|
+
});
|
|
179
|
+
const addresses = accounts.map(_ref7 => {
|
|
180
|
+
let {
|
|
181
|
+
address
|
|
182
|
+
} = _ref7;
|
|
183
|
+
return address;
|
|
184
|
+
});
|
|
185
|
+
console.log(`web3Accounts: Found ${accounts.length} address${accounts.length !== 1 ? 'es' : ''}: ${addresses.join(', ')}`);
|
|
186
|
+
return accounts;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async function web3AccountsSubscribe(cb) {
|
|
190
|
+
let {
|
|
191
|
+
ss58Format
|
|
192
|
+
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
193
|
+
|
|
194
|
+
if (!web3EnablePromise) {
|
|
195
|
+
return throwError('web3AccountsSubscribe');
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const accounts = {};
|
|
199
|
+
|
|
200
|
+
const triggerUpdate = () => cb(Object.entries(accounts).reduce((result, _ref8) => {
|
|
201
|
+
let [source, list] = _ref8;
|
|
202
|
+
result.push(...mapAccounts(source, list, ss58Format));
|
|
203
|
+
return result;
|
|
204
|
+
}, []));
|
|
205
|
+
|
|
206
|
+
const unsubs = (await web3EnablePromise).map(_ref9 => {
|
|
207
|
+
let {
|
|
208
|
+
accounts: {
|
|
209
|
+
subscribe
|
|
210
|
+
},
|
|
211
|
+
name: source
|
|
212
|
+
} = _ref9;
|
|
213
|
+
return subscribe(result => {
|
|
214
|
+
accounts[source] = result; // eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
215
|
+
|
|
216
|
+
triggerUpdate();
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
return () => {
|
|
220
|
+
unsubs.forEach(unsub => {
|
|
221
|
+
unsub();
|
|
222
|
+
});
|
|
223
|
+
};
|
|
224
|
+
} // find a specific provider based on the name
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
async function web3FromSource(source) {
|
|
228
|
+
if (!web3EnablePromise) {
|
|
229
|
+
return throwError('web3FromSource');
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const sources = await web3EnablePromise;
|
|
233
|
+
const found = source && sources.find(_ref10 => {
|
|
234
|
+
let {
|
|
235
|
+
name
|
|
236
|
+
} = _ref10;
|
|
237
|
+
return name === source;
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
if (!found) {
|
|
241
|
+
throw new Error(`web3FromSource: Unable to find an injected ${source}`);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return found;
|
|
245
|
+
} // find a specific provider based on an address
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
async function web3FromAddress(address) {
|
|
249
|
+
if (!web3EnablePromise) {
|
|
250
|
+
return throwError('web3FromAddress');
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const accounts = await web3Accounts();
|
|
254
|
+
let found;
|
|
255
|
+
|
|
256
|
+
if (address) {
|
|
257
|
+
const accountU8a = (0, _utilCrypto.decodeAddress)(address);
|
|
258
|
+
found = accounts.find(account => (0, _util.u8aEq)((0, _utilCrypto.decodeAddress)(account.address), accountU8a));
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (!found) {
|
|
262
|
+
throw new Error(`web3FromAddress: Unable to find injected ${address}`);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return web3FromSource(found.meta.source);
|
|
266
|
+
} // retrieve all providers exposed by one source
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
async function web3ListRpcProviders(source) {
|
|
270
|
+
const {
|
|
271
|
+
provider
|
|
272
|
+
} = await web3FromSource(source);
|
|
273
|
+
|
|
274
|
+
if (!provider) {
|
|
275
|
+
console.warn(`Extension ${source} does not expose any provider`);
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return provider.listProviders();
|
|
280
|
+
} // retrieve all providers exposed by one source
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
async function web3UseRpcProvider(source, key) {
|
|
284
|
+
const {
|
|
285
|
+
provider
|
|
286
|
+
} = await web3FromSource(source);
|
|
287
|
+
|
|
288
|
+
if (!provider) {
|
|
289
|
+
throw new Error(`Extension ${source} does not expose any provider`);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const meta = await provider.startProvider(key);
|
|
293
|
+
return {
|
|
294
|
+
meta,
|
|
295
|
+
provider
|
|
296
|
+
};
|
|
297
|
+
}
|
package/bundle.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { InjectedAccountWithMeta, InjectedExtension, InjectedProviderWithMeta, ProviderList, Unsubcall, Web3AccountsOptions } from '@polkadot/extension-inject/types';
|
|
2
|
+
export { packageInfo } from './packageInfo';
|
|
3
|
+
export { unwrapBytes, wrapBytes } from './wrapBytes';
|
|
4
|
+
declare let isWeb3Injected: boolean;
|
|
5
|
+
declare let web3EnablePromise: Promise<InjectedExtension[]> | null;
|
|
6
|
+
export { isWeb3Injected, web3EnablePromise };
|
|
7
|
+
export declare function web3Enable(originName: string, compatInits?: (() => Promise<boolean>)[]): Promise<InjectedExtension[]>;
|
|
8
|
+
export declare function web3Accounts({ accountType, ss58Format }?: Web3AccountsOptions): Promise<InjectedAccountWithMeta[]>;
|
|
9
|
+
export declare function web3AccountsSubscribe(cb: (accounts: InjectedAccountWithMeta[]) => void | Promise<void>, { ss58Format }?: Web3AccountsOptions): Promise<Unsubcall>;
|
|
10
|
+
export declare function web3FromSource(source: string): Promise<InjectedExtension>;
|
|
11
|
+
export declare function web3FromAddress(address: string): Promise<InjectedExtension>;
|
|
12
|
+
export declare function web3ListRpcProviders(source: string): Promise<ProviderList | null>;
|
|
13
|
+
export declare function web3UseRpcProvider(source: string, key: string): Promise<InjectedProviderWithMeta>;
|
package/bundle.js
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
// Copyright 2019-2021 @polkadot/extension-dapp authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { u8aEq } from '@polkadot/util';
|
|
4
|
+
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
|
|
5
|
+
import { documentReadyPromise } from "./util.js"; // expose utility functions
|
|
6
|
+
|
|
7
|
+
export { packageInfo } from "./packageInfo.js";
|
|
8
|
+
export { unwrapBytes, wrapBytes } from "./wrapBytes.js"; // just a helper (otherwise we cast all-over, so shorter and more readable)
|
|
9
|
+
|
|
10
|
+
const win = window; // don't clobber the existing object, but ensure non-undefined
|
|
11
|
+
|
|
12
|
+
win.injectedWeb3 = win.injectedWeb3 || {}; // true when anything has been injected and is available
|
|
13
|
+
|
|
14
|
+
function web3IsInjected() {
|
|
15
|
+
return Object.keys(win.injectedWeb3).length !== 0;
|
|
16
|
+
} // helper to throw a consistent error when not enabled
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
function throwError(method) {
|
|
20
|
+
throw new Error(`${method}: web3Enable(originName) needs to be called before ${method}`);
|
|
21
|
+
} // internal helper to map from Array<InjectedAccount> -> Array<InjectedAccountWithMeta>
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
function mapAccounts(source, list, ss58Format) {
|
|
25
|
+
return list.map(({
|
|
26
|
+
address,
|
|
27
|
+
genesisHash,
|
|
28
|
+
name,
|
|
29
|
+
type
|
|
30
|
+
}) => {
|
|
31
|
+
const encodedAddress = address.length === 42 ? address : encodeAddress(decodeAddress(address), ss58Format);
|
|
32
|
+
return {
|
|
33
|
+
address: encodedAddress,
|
|
34
|
+
meta: {
|
|
35
|
+
genesisHash,
|
|
36
|
+
name,
|
|
37
|
+
source
|
|
38
|
+
},
|
|
39
|
+
type
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
} // have we found a properly constructed window.injectedWeb3
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
let isWeb3Injected = web3IsInjected(); // we keep the last promise created around (for queries)
|
|
46
|
+
|
|
47
|
+
let web3EnablePromise = null;
|
|
48
|
+
export { isWeb3Injected, web3EnablePromise };
|
|
49
|
+
|
|
50
|
+
function getWindowExtensions(originName) {
|
|
51
|
+
return Promise.all(Object.entries(win.injectedWeb3).map(([name, {
|
|
52
|
+
enable,
|
|
53
|
+
version
|
|
54
|
+
}]) => Promise.all([Promise.resolve({
|
|
55
|
+
name,
|
|
56
|
+
version
|
|
57
|
+
}), enable(originName).catch(error => {
|
|
58
|
+
console.error(`Error initializing ${name}: ${error.message}`);
|
|
59
|
+
})])));
|
|
60
|
+
} // enables all the providers found on the injected window interface
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
export function web3Enable(originName, compatInits = []) {
|
|
64
|
+
if (!originName) {
|
|
65
|
+
throw new Error('You must pass a name for your app to the web3Enable function');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const initCompat = compatInits.length ? Promise.all(compatInits.map(c => c().catch(() => false))) : Promise.resolve([true]);
|
|
69
|
+
web3EnablePromise = documentReadyPromise(() => initCompat.then(() => getWindowExtensions(originName).then(values => values.filter(value => !!value[1]).map(([info, ext]) => {
|
|
70
|
+
// if we don't have an accounts subscriber, add a single-shot version
|
|
71
|
+
if (!ext.accounts.subscribe) {
|
|
72
|
+
ext.accounts.subscribe = cb => {
|
|
73
|
+
ext.accounts.get().then(cb).catch(console.error);
|
|
74
|
+
return () => {// no ubsubscribe needed, this is a single-shot
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return { ...info,
|
|
80
|
+
...ext
|
|
81
|
+
};
|
|
82
|
+
})).catch(() => []).then(values => {
|
|
83
|
+
const names = values.map(({
|
|
84
|
+
name,
|
|
85
|
+
version
|
|
86
|
+
}) => `${name}/${version}`);
|
|
87
|
+
isWeb3Injected = web3IsInjected();
|
|
88
|
+
console.log(`web3Enable: Enabled ${values.length} extension${values.length !== 1 ? 's' : ''}: ${names.join(', ')}`);
|
|
89
|
+
return values;
|
|
90
|
+
})));
|
|
91
|
+
return web3EnablePromise;
|
|
92
|
+
} // retrieve all the accounts across all providers
|
|
93
|
+
|
|
94
|
+
export async function web3Accounts({
|
|
95
|
+
accountType,
|
|
96
|
+
ss58Format
|
|
97
|
+
} = {}) {
|
|
98
|
+
if (!web3EnablePromise) {
|
|
99
|
+
return throwError('web3Accounts');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const accounts = [];
|
|
103
|
+
const injected = await web3EnablePromise;
|
|
104
|
+
const retrieved = await Promise.all(injected.map(async ({
|
|
105
|
+
accounts,
|
|
106
|
+
name: source
|
|
107
|
+
}) => {
|
|
108
|
+
try {
|
|
109
|
+
const list = await accounts.get();
|
|
110
|
+
return mapAccounts(source, list.filter(({
|
|
111
|
+
type
|
|
112
|
+
}) => type && accountType ? accountType.includes(type) : true), ss58Format);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
// cannot handle this one
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
}));
|
|
118
|
+
retrieved.forEach(result => {
|
|
119
|
+
accounts.push(...result);
|
|
120
|
+
});
|
|
121
|
+
const addresses = accounts.map(({
|
|
122
|
+
address
|
|
123
|
+
}) => address);
|
|
124
|
+
console.log(`web3Accounts: Found ${accounts.length} address${accounts.length !== 1 ? 'es' : ''}: ${addresses.join(', ')}`);
|
|
125
|
+
return accounts;
|
|
126
|
+
}
|
|
127
|
+
export async function web3AccountsSubscribe(cb, {
|
|
128
|
+
ss58Format
|
|
129
|
+
} = {}) {
|
|
130
|
+
if (!web3EnablePromise) {
|
|
131
|
+
return throwError('web3AccountsSubscribe');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const accounts = {};
|
|
135
|
+
|
|
136
|
+
const triggerUpdate = () => cb(Object.entries(accounts).reduce((result, [source, list]) => {
|
|
137
|
+
result.push(...mapAccounts(source, list, ss58Format));
|
|
138
|
+
return result;
|
|
139
|
+
}, []));
|
|
140
|
+
|
|
141
|
+
const unsubs = (await web3EnablePromise).map(({
|
|
142
|
+
accounts: {
|
|
143
|
+
subscribe
|
|
144
|
+
},
|
|
145
|
+
name: source
|
|
146
|
+
}) => subscribe(result => {
|
|
147
|
+
accounts[source] = result; // eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
148
|
+
|
|
149
|
+
triggerUpdate();
|
|
150
|
+
}));
|
|
151
|
+
return () => {
|
|
152
|
+
unsubs.forEach(unsub => {
|
|
153
|
+
unsub();
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
} // find a specific provider based on the name
|
|
157
|
+
|
|
158
|
+
export async function web3FromSource(source) {
|
|
159
|
+
if (!web3EnablePromise) {
|
|
160
|
+
return throwError('web3FromSource');
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const sources = await web3EnablePromise;
|
|
164
|
+
const found = source && sources.find(({
|
|
165
|
+
name
|
|
166
|
+
}) => name === source);
|
|
167
|
+
|
|
168
|
+
if (!found) {
|
|
169
|
+
throw new Error(`web3FromSource: Unable to find an injected ${source}`);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return found;
|
|
173
|
+
} // find a specific provider based on an address
|
|
174
|
+
|
|
175
|
+
export async function web3FromAddress(address) {
|
|
176
|
+
if (!web3EnablePromise) {
|
|
177
|
+
return throwError('web3FromAddress');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const accounts = await web3Accounts();
|
|
181
|
+
let found;
|
|
182
|
+
|
|
183
|
+
if (address) {
|
|
184
|
+
const accountU8a = decodeAddress(address);
|
|
185
|
+
found = accounts.find(account => u8aEq(decodeAddress(account.address), accountU8a));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (!found) {
|
|
189
|
+
throw new Error(`web3FromAddress: Unable to find injected ${address}`);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return web3FromSource(found.meta.source);
|
|
193
|
+
} // retrieve all providers exposed by one source
|
|
194
|
+
|
|
195
|
+
export async function web3ListRpcProviders(source) {
|
|
196
|
+
const {
|
|
197
|
+
provider
|
|
198
|
+
} = await web3FromSource(source);
|
|
199
|
+
|
|
200
|
+
if (!provider) {
|
|
201
|
+
console.warn(`Extension ${source} does not expose any provider`);
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return provider.listProviders();
|
|
206
|
+
} // retrieve all providers exposed by one source
|
|
207
|
+
|
|
208
|
+
export async function web3UseRpcProvider(source, key) {
|
|
209
|
+
const {
|
|
210
|
+
provider
|
|
211
|
+
} = await web3FromSource(source);
|
|
212
|
+
|
|
213
|
+
if (!provider) {
|
|
214
|
+
throw new Error(`Extension ${source} does not expose any provider`);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const meta = await provider.startProvider(key);
|
|
218
|
+
return {
|
|
219
|
+
meta,
|
|
220
|
+
provider
|
|
221
|
+
};
|
|
222
|
+
}
|
package/index.cjs
CHANGED
|
@@ -3,297 +3,18 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.isWeb3Injected = void 0;
|
|
7
|
-
Object.defineProperty(exports, "packageInfo", {
|
|
8
|
-
enumerable: true,
|
|
9
|
-
get: function () {
|
|
10
|
-
return _packageInfo.packageInfo;
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
Object.defineProperty(exports, "unwrapBytes", {
|
|
14
|
-
enumerable: true,
|
|
15
|
-
get: function () {
|
|
16
|
-
return _wrapBytes.unwrapBytes;
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
exports.web3Accounts = web3Accounts;
|
|
20
|
-
exports.web3AccountsSubscribe = web3AccountsSubscribe;
|
|
21
|
-
exports.web3Enable = web3Enable;
|
|
22
|
-
exports.web3EnablePromise = void 0;
|
|
23
|
-
exports.web3FromAddress = web3FromAddress;
|
|
24
|
-
exports.web3FromSource = web3FromSource;
|
|
25
|
-
exports.web3ListRpcProviders = web3ListRpcProviders;
|
|
26
|
-
exports.web3UseRpcProvider = web3UseRpcProvider;
|
|
27
|
-
Object.defineProperty(exports, "wrapBytes", {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
get: function () {
|
|
30
|
-
return _wrapBytes.wrapBytes;
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
6
|
|
|
34
7
|
require("./detectPackage.cjs");
|
|
35
8
|
|
|
36
|
-
var
|
|
37
|
-
|
|
38
|
-
var _utilCrypto = require("@polkadot/util-crypto");
|
|
39
|
-
|
|
40
|
-
var _util2 = require("./util.cjs");
|
|
41
|
-
|
|
42
|
-
var _packageInfo = require("./packageInfo.cjs");
|
|
43
|
-
|
|
44
|
-
var _wrapBytes = require("./wrapBytes.cjs");
|
|
45
|
-
|
|
46
|
-
// Copyright 2019-2021 @polkadot/extension-dapp authors & contributors
|
|
47
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
48
|
-
// expose utility functions
|
|
49
|
-
// just a helper (otherwise we cast all-over, so shorter and more readable)
|
|
50
|
-
const win = window; // don't clobber the existing object, but ensure non-undefined
|
|
51
|
-
|
|
52
|
-
win.injectedWeb3 = win.injectedWeb3 || {}; // true when anything has been injected and is available
|
|
53
|
-
|
|
54
|
-
function web3IsInjected() {
|
|
55
|
-
return Object.keys(win.injectedWeb3).length !== 0;
|
|
56
|
-
} // helper to throw a consistent error when not enabled
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
function throwError(method) {
|
|
60
|
-
throw new Error(`${method}: web3Enable(originName) needs to be called before ${method}`);
|
|
61
|
-
} // internal helper to map from Array<InjectedAccount> -> Array<InjectedAccountWithMeta>
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
function mapAccounts(source, list, ss58Format) {
|
|
65
|
-
return list.map(_ref => {
|
|
66
|
-
let {
|
|
67
|
-
address,
|
|
68
|
-
genesisHash,
|
|
69
|
-
name,
|
|
70
|
-
type
|
|
71
|
-
} = _ref;
|
|
72
|
-
const encodedAddress = address.length === 42 ? address : (0, _utilCrypto.encodeAddress)((0, _utilCrypto.decodeAddress)(address), ss58Format);
|
|
73
|
-
return {
|
|
74
|
-
address: encodedAddress,
|
|
75
|
-
meta: {
|
|
76
|
-
genesisHash,
|
|
77
|
-
name,
|
|
78
|
-
source
|
|
79
|
-
},
|
|
80
|
-
type
|
|
81
|
-
};
|
|
82
|
-
});
|
|
83
|
-
} // have we found a properly constructed window.injectedWeb3
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
let isWeb3Injected = web3IsInjected(); // we keep the last promise created around (for queries)
|
|
87
|
-
|
|
88
|
-
exports.isWeb3Injected = isWeb3Injected;
|
|
89
|
-
let web3EnablePromise = null;
|
|
90
|
-
exports.web3EnablePromise = web3EnablePromise;
|
|
91
|
-
|
|
92
|
-
function getWindowExtensions(originName) {
|
|
93
|
-
return Promise.all(Object.entries(win.injectedWeb3).map(_ref2 => {
|
|
94
|
-
let [name, {
|
|
95
|
-
enable,
|
|
96
|
-
version
|
|
97
|
-
}] = _ref2;
|
|
98
|
-
return Promise.all([Promise.resolve({
|
|
99
|
-
name,
|
|
100
|
-
version
|
|
101
|
-
}), enable(originName).catch(error => {
|
|
102
|
-
console.error(`Error initializing ${name}: ${error.message}`);
|
|
103
|
-
})]);
|
|
104
|
-
}));
|
|
105
|
-
} // enables all the providers found on the injected window interface
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
function web3Enable(originName) {
|
|
109
|
-
let compatInits = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
110
|
-
|
|
111
|
-
if (!originName) {
|
|
112
|
-
throw new Error('You must pass a name for your app to the web3Enable function');
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const initCompat = compatInits.length ? Promise.all(compatInits.map(c => c().catch(() => false))) : Promise.resolve([true]);
|
|
116
|
-
exports.web3EnablePromise = web3EnablePromise = (0, _util2.documentReadyPromise)(() => initCompat.then(() => getWindowExtensions(originName).then(values => values.filter(value => !!value[1]).map(_ref3 => {
|
|
117
|
-
let [info, ext] = _ref3;
|
|
118
|
-
|
|
119
|
-
// if we don't have an accounts subscriber, add a single-shot version
|
|
120
|
-
if (!ext.accounts.subscribe) {
|
|
121
|
-
ext.accounts.subscribe = cb => {
|
|
122
|
-
ext.accounts.get().then(cb).catch(console.error);
|
|
123
|
-
return () => {// no ubsubscribe needed, this is a single-shot
|
|
124
|
-
};
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return { ...info,
|
|
129
|
-
...ext
|
|
130
|
-
};
|
|
131
|
-
})).catch(() => []).then(values => {
|
|
132
|
-
const names = values.map(_ref4 => {
|
|
133
|
-
let {
|
|
134
|
-
name,
|
|
135
|
-
version
|
|
136
|
-
} = _ref4;
|
|
137
|
-
return `${name}/${version}`;
|
|
138
|
-
});
|
|
139
|
-
exports.isWeb3Injected = isWeb3Injected = web3IsInjected();
|
|
140
|
-
console.log(`web3Enable: Enabled ${values.length} extension${values.length !== 1 ? 's' : ''}: ${names.join(', ')}`);
|
|
141
|
-
return values;
|
|
142
|
-
})));
|
|
143
|
-
return web3EnablePromise;
|
|
144
|
-
} // retrieve all the accounts across all providers
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
async function web3Accounts() {
|
|
148
|
-
let {
|
|
149
|
-
accountType,
|
|
150
|
-
ss58Format
|
|
151
|
-
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
152
|
-
|
|
153
|
-
if (!web3EnablePromise) {
|
|
154
|
-
return throwError('web3Accounts');
|
|
155
|
-
}
|
|
9
|
+
var _bundle = require("./bundle.cjs");
|
|
156
10
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
try {
|
|
166
|
-
const list = await accounts.get();
|
|
167
|
-
return mapAccounts(source, list.filter(_ref6 => {
|
|
168
|
-
let {
|
|
169
|
-
type
|
|
170
|
-
} = _ref6;
|
|
171
|
-
return type && accountType ? accountType.includes(type) : true;
|
|
172
|
-
}), ss58Format);
|
|
173
|
-
} catch (error) {
|
|
174
|
-
// cannot handle this one
|
|
175
|
-
return [];
|
|
11
|
+
Object.keys(_bundle).forEach(function (key) {
|
|
12
|
+
if (key === "default" || key === "__esModule") return;
|
|
13
|
+
if (key in exports && exports[key] === _bundle[key]) return;
|
|
14
|
+
Object.defineProperty(exports, key, {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function () {
|
|
17
|
+
return _bundle[key];
|
|
176
18
|
}
|
|
177
|
-
}));
|
|
178
|
-
retrieved.forEach(result => {
|
|
179
|
-
accounts.push(...result);
|
|
180
|
-
});
|
|
181
|
-
const addresses = accounts.map(_ref7 => {
|
|
182
|
-
let {
|
|
183
|
-
address
|
|
184
|
-
} = _ref7;
|
|
185
|
-
return address;
|
|
186
19
|
});
|
|
187
|
-
|
|
188
|
-
return accounts;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
async function web3AccountsSubscribe(cb) {
|
|
192
|
-
let {
|
|
193
|
-
ss58Format
|
|
194
|
-
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
195
|
-
|
|
196
|
-
if (!web3EnablePromise) {
|
|
197
|
-
return throwError('web3AccountsSubscribe');
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const accounts = {};
|
|
201
|
-
|
|
202
|
-
const triggerUpdate = () => cb(Object.entries(accounts).reduce((result, _ref8) => {
|
|
203
|
-
let [source, list] = _ref8;
|
|
204
|
-
result.push(...mapAccounts(source, list, ss58Format));
|
|
205
|
-
return result;
|
|
206
|
-
}, []));
|
|
207
|
-
|
|
208
|
-
const unsubs = (await web3EnablePromise).map(_ref9 => {
|
|
209
|
-
let {
|
|
210
|
-
accounts: {
|
|
211
|
-
subscribe
|
|
212
|
-
},
|
|
213
|
-
name: source
|
|
214
|
-
} = _ref9;
|
|
215
|
-
return subscribe(result => {
|
|
216
|
-
accounts[source] = result; // eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
217
|
-
|
|
218
|
-
triggerUpdate();
|
|
219
|
-
});
|
|
220
|
-
});
|
|
221
|
-
return () => {
|
|
222
|
-
unsubs.forEach(unsub => {
|
|
223
|
-
unsub();
|
|
224
|
-
});
|
|
225
|
-
};
|
|
226
|
-
} // find a specific provider based on the name
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
async function web3FromSource(source) {
|
|
230
|
-
if (!web3EnablePromise) {
|
|
231
|
-
return throwError('web3FromSource');
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const sources = await web3EnablePromise;
|
|
235
|
-
const found = source && sources.find(_ref10 => {
|
|
236
|
-
let {
|
|
237
|
-
name
|
|
238
|
-
} = _ref10;
|
|
239
|
-
return name === source;
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
if (!found) {
|
|
243
|
-
throw new Error(`web3FromSource: Unable to find an injected ${source}`);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
return found;
|
|
247
|
-
} // find a specific provider based on an address
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
async function web3FromAddress(address) {
|
|
251
|
-
if (!web3EnablePromise) {
|
|
252
|
-
return throwError('web3FromAddress');
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
const accounts = await web3Accounts();
|
|
256
|
-
let found;
|
|
257
|
-
|
|
258
|
-
if (address) {
|
|
259
|
-
const accountU8a = (0, _utilCrypto.decodeAddress)(address);
|
|
260
|
-
found = accounts.find(account => (0, _util.u8aEq)((0, _utilCrypto.decodeAddress)(account.address), accountU8a));
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
if (!found) {
|
|
264
|
-
throw new Error(`web3FromAddress: Unable to find injected ${address}`);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
return web3FromSource(found.meta.source);
|
|
268
|
-
} // retrieve all providers exposed by one source
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
async function web3ListRpcProviders(source) {
|
|
272
|
-
const {
|
|
273
|
-
provider
|
|
274
|
-
} = await web3FromSource(source);
|
|
275
|
-
|
|
276
|
-
if (!provider) {
|
|
277
|
-
console.warn(`Extension ${source} does not expose any provider`);
|
|
278
|
-
return null;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
return provider.listProviders();
|
|
282
|
-
} // retrieve all providers exposed by one source
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
async function web3UseRpcProvider(source, key) {
|
|
286
|
-
const {
|
|
287
|
-
provider
|
|
288
|
-
} = await web3FromSource(source);
|
|
289
|
-
|
|
290
|
-
if (!provider) {
|
|
291
|
-
throw new Error(`Extension ${source} does not expose any provider`);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
const meta = await provider.startProvider(key);
|
|
295
|
-
return {
|
|
296
|
-
meta,
|
|
297
|
-
provider
|
|
298
|
-
};
|
|
299
|
-
}
|
|
20
|
+
});
|
package/index.d.ts
CHANGED
|
@@ -1,14 +1,2 @@
|
|
|
1
1
|
import './detectPackage';
|
|
2
|
-
|
|
3
|
-
export { packageInfo } from './packageInfo';
|
|
4
|
-
export { unwrapBytes, wrapBytes } from './wrapBytes';
|
|
5
|
-
declare let isWeb3Injected: boolean;
|
|
6
|
-
declare let web3EnablePromise: Promise<InjectedExtension[]> | null;
|
|
7
|
-
export { isWeb3Injected, web3EnablePromise };
|
|
8
|
-
export declare function web3Enable(originName: string, compatInits?: (() => Promise<boolean>)[]): Promise<InjectedExtension[]>;
|
|
9
|
-
export declare function web3Accounts({ accountType, ss58Format }?: Web3AccountsOptions): Promise<InjectedAccountWithMeta[]>;
|
|
10
|
-
export declare function web3AccountsSubscribe(cb: (accounts: InjectedAccountWithMeta[]) => void | Promise<void>, { ss58Format }?: Web3AccountsOptions): Promise<Unsubcall>;
|
|
11
|
-
export declare function web3FromSource(source: string): Promise<InjectedExtension>;
|
|
12
|
-
export declare function web3FromAddress(address: string): Promise<InjectedExtension>;
|
|
13
|
-
export declare function web3ListRpcProviders(source: string): Promise<ProviderList | null>;
|
|
14
|
-
export declare function web3UseRpcProvider(source: string, key: string): Promise<InjectedProviderWithMeta>;
|
|
2
|
+
export * from './bundle';
|
package/index.js
CHANGED
|
@@ -1,223 +1,4 @@
|
|
|
1
1
|
// Copyright 2019-2021 @polkadot/extension-dapp authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import "./detectPackage.js";
|
|
4
|
-
|
|
5
|
-
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
|
|
6
|
-
import { documentReadyPromise } from "./util.js"; // expose utility functions
|
|
7
|
-
|
|
8
|
-
export { packageInfo } from "./packageInfo.js";
|
|
9
|
-
export { unwrapBytes, wrapBytes } from "./wrapBytes.js"; // just a helper (otherwise we cast all-over, so shorter and more readable)
|
|
10
|
-
|
|
11
|
-
const win = window; // don't clobber the existing object, but ensure non-undefined
|
|
12
|
-
|
|
13
|
-
win.injectedWeb3 = win.injectedWeb3 || {}; // true when anything has been injected and is available
|
|
14
|
-
|
|
15
|
-
function web3IsInjected() {
|
|
16
|
-
return Object.keys(win.injectedWeb3).length !== 0;
|
|
17
|
-
} // helper to throw a consistent error when not enabled
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
function throwError(method) {
|
|
21
|
-
throw new Error(`${method}: web3Enable(originName) needs to be called before ${method}`);
|
|
22
|
-
} // internal helper to map from Array<InjectedAccount> -> Array<InjectedAccountWithMeta>
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
function mapAccounts(source, list, ss58Format) {
|
|
26
|
-
return list.map(({
|
|
27
|
-
address,
|
|
28
|
-
genesisHash,
|
|
29
|
-
name,
|
|
30
|
-
type
|
|
31
|
-
}) => {
|
|
32
|
-
const encodedAddress = address.length === 42 ? address : encodeAddress(decodeAddress(address), ss58Format);
|
|
33
|
-
return {
|
|
34
|
-
address: encodedAddress,
|
|
35
|
-
meta: {
|
|
36
|
-
genesisHash,
|
|
37
|
-
name,
|
|
38
|
-
source
|
|
39
|
-
},
|
|
40
|
-
type
|
|
41
|
-
};
|
|
42
|
-
});
|
|
43
|
-
} // have we found a properly constructed window.injectedWeb3
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
let isWeb3Injected = web3IsInjected(); // we keep the last promise created around (for queries)
|
|
47
|
-
|
|
48
|
-
let web3EnablePromise = null;
|
|
49
|
-
export { isWeb3Injected, web3EnablePromise };
|
|
50
|
-
|
|
51
|
-
function getWindowExtensions(originName) {
|
|
52
|
-
return Promise.all(Object.entries(win.injectedWeb3).map(([name, {
|
|
53
|
-
enable,
|
|
54
|
-
version
|
|
55
|
-
}]) => Promise.all([Promise.resolve({
|
|
56
|
-
name,
|
|
57
|
-
version
|
|
58
|
-
}), enable(originName).catch(error => {
|
|
59
|
-
console.error(`Error initializing ${name}: ${error.message}`);
|
|
60
|
-
})])));
|
|
61
|
-
} // enables all the providers found on the injected window interface
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
export function web3Enable(originName, compatInits = []) {
|
|
65
|
-
if (!originName) {
|
|
66
|
-
throw new Error('You must pass a name for your app to the web3Enable function');
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const initCompat = compatInits.length ? Promise.all(compatInits.map(c => c().catch(() => false))) : Promise.resolve([true]);
|
|
70
|
-
web3EnablePromise = documentReadyPromise(() => initCompat.then(() => getWindowExtensions(originName).then(values => values.filter(value => !!value[1]).map(([info, ext]) => {
|
|
71
|
-
// if we don't have an accounts subscriber, add a single-shot version
|
|
72
|
-
if (!ext.accounts.subscribe) {
|
|
73
|
-
ext.accounts.subscribe = cb => {
|
|
74
|
-
ext.accounts.get().then(cb).catch(console.error);
|
|
75
|
-
return () => {// no ubsubscribe needed, this is a single-shot
|
|
76
|
-
};
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return { ...info,
|
|
81
|
-
...ext
|
|
82
|
-
};
|
|
83
|
-
})).catch(() => []).then(values => {
|
|
84
|
-
const names = values.map(({
|
|
85
|
-
name,
|
|
86
|
-
version
|
|
87
|
-
}) => `${name}/${version}`);
|
|
88
|
-
isWeb3Injected = web3IsInjected();
|
|
89
|
-
console.log(`web3Enable: Enabled ${values.length} extension${values.length !== 1 ? 's' : ''}: ${names.join(', ')}`);
|
|
90
|
-
return values;
|
|
91
|
-
})));
|
|
92
|
-
return web3EnablePromise;
|
|
93
|
-
} // retrieve all the accounts across all providers
|
|
94
|
-
|
|
95
|
-
export async function web3Accounts({
|
|
96
|
-
accountType,
|
|
97
|
-
ss58Format
|
|
98
|
-
} = {}) {
|
|
99
|
-
if (!web3EnablePromise) {
|
|
100
|
-
return throwError('web3Accounts');
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
const accounts = [];
|
|
104
|
-
const injected = await web3EnablePromise;
|
|
105
|
-
const retrieved = await Promise.all(injected.map(async ({
|
|
106
|
-
accounts,
|
|
107
|
-
name: source
|
|
108
|
-
}) => {
|
|
109
|
-
try {
|
|
110
|
-
const list = await accounts.get();
|
|
111
|
-
return mapAccounts(source, list.filter(({
|
|
112
|
-
type
|
|
113
|
-
}) => type && accountType ? accountType.includes(type) : true), ss58Format);
|
|
114
|
-
} catch (error) {
|
|
115
|
-
// cannot handle this one
|
|
116
|
-
return [];
|
|
117
|
-
}
|
|
118
|
-
}));
|
|
119
|
-
retrieved.forEach(result => {
|
|
120
|
-
accounts.push(...result);
|
|
121
|
-
});
|
|
122
|
-
const addresses = accounts.map(({
|
|
123
|
-
address
|
|
124
|
-
}) => address);
|
|
125
|
-
console.log(`web3Accounts: Found ${accounts.length} address${accounts.length !== 1 ? 'es' : ''}: ${addresses.join(', ')}`);
|
|
126
|
-
return accounts;
|
|
127
|
-
}
|
|
128
|
-
export async function web3AccountsSubscribe(cb, {
|
|
129
|
-
ss58Format
|
|
130
|
-
} = {}) {
|
|
131
|
-
if (!web3EnablePromise) {
|
|
132
|
-
return throwError('web3AccountsSubscribe');
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const accounts = {};
|
|
136
|
-
|
|
137
|
-
const triggerUpdate = () => cb(Object.entries(accounts).reduce((result, [source, list]) => {
|
|
138
|
-
result.push(...mapAccounts(source, list, ss58Format));
|
|
139
|
-
return result;
|
|
140
|
-
}, []));
|
|
141
|
-
|
|
142
|
-
const unsubs = (await web3EnablePromise).map(({
|
|
143
|
-
accounts: {
|
|
144
|
-
subscribe
|
|
145
|
-
},
|
|
146
|
-
name: source
|
|
147
|
-
}) => subscribe(result => {
|
|
148
|
-
accounts[source] = result; // eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
149
|
-
|
|
150
|
-
triggerUpdate();
|
|
151
|
-
}));
|
|
152
|
-
return () => {
|
|
153
|
-
unsubs.forEach(unsub => {
|
|
154
|
-
unsub();
|
|
155
|
-
});
|
|
156
|
-
};
|
|
157
|
-
} // find a specific provider based on the name
|
|
158
|
-
|
|
159
|
-
export async function web3FromSource(source) {
|
|
160
|
-
if (!web3EnablePromise) {
|
|
161
|
-
return throwError('web3FromSource');
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const sources = await web3EnablePromise;
|
|
165
|
-
const found = source && sources.find(({
|
|
166
|
-
name
|
|
167
|
-
}) => name === source);
|
|
168
|
-
|
|
169
|
-
if (!found) {
|
|
170
|
-
throw new Error(`web3FromSource: Unable to find an injected ${source}`);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
return found;
|
|
174
|
-
} // find a specific provider based on an address
|
|
175
|
-
|
|
176
|
-
export async function web3FromAddress(address) {
|
|
177
|
-
if (!web3EnablePromise) {
|
|
178
|
-
return throwError('web3FromAddress');
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
const accounts = await web3Accounts();
|
|
182
|
-
let found;
|
|
183
|
-
|
|
184
|
-
if (address) {
|
|
185
|
-
const accountU8a = decodeAddress(address);
|
|
186
|
-
found = accounts.find(account => u8aEq(decodeAddress(account.address), accountU8a));
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (!found) {
|
|
190
|
-
throw new Error(`web3FromAddress: Unable to find injected ${address}`);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
return web3FromSource(found.meta.source);
|
|
194
|
-
} // retrieve all providers exposed by one source
|
|
195
|
-
|
|
196
|
-
export async function web3ListRpcProviders(source) {
|
|
197
|
-
const {
|
|
198
|
-
provider
|
|
199
|
-
} = await web3FromSource(source);
|
|
200
|
-
|
|
201
|
-
if (!provider) {
|
|
202
|
-
console.warn(`Extension ${source} does not expose any provider`);
|
|
203
|
-
return null;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return provider.listProviders();
|
|
207
|
-
} // retrieve all providers exposed by one source
|
|
208
|
-
|
|
209
|
-
export async function web3UseRpcProvider(source, key) {
|
|
210
|
-
const {
|
|
211
|
-
provider
|
|
212
|
-
} = await web3FromSource(source);
|
|
213
|
-
|
|
214
|
-
if (!provider) {
|
|
215
|
-
throw new Error(`Extension ${source} does not expose any provider`);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
const meta = await provider.startProvider(key);
|
|
219
|
-
return {
|
|
220
|
-
meta,
|
|
221
|
-
provider
|
|
222
|
-
};
|
|
223
|
-
}
|
|
4
|
+
export * from "./bundle.js";
|
package/package.json
CHANGED
|
@@ -17,11 +17,11 @@
|
|
|
17
17
|
"./detectPackage.cjs"
|
|
18
18
|
],
|
|
19
19
|
"type": "module",
|
|
20
|
-
"version": "0.42.5-
|
|
20
|
+
"version": "0.42.5-4",
|
|
21
21
|
"main": "index.js",
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@babel/runtime": "^7.16.5",
|
|
24
|
-
"@polkadot/extension-inject": "^0.42.5-
|
|
24
|
+
"@polkadot/extension-inject": "^0.42.5-4",
|
|
25
25
|
"@polkadot/util": "^8.2.2",
|
|
26
26
|
"@polkadot/util-crypto": "^8.2.2"
|
|
27
27
|
},
|
|
@@ -36,6 +36,11 @@
|
|
|
36
36
|
"require": "./index.cjs",
|
|
37
37
|
"default": "./index.js"
|
|
38
38
|
},
|
|
39
|
+
"./bundle": {
|
|
40
|
+
"types": "./bundle.d.ts",
|
|
41
|
+
"require": "./bundle.cjs",
|
|
42
|
+
"default": "./bundle.js"
|
|
43
|
+
},
|
|
39
44
|
"./detectPackage": {
|
|
40
45
|
"types": "./detectPackage.d.ts",
|
|
41
46
|
"require": "./detectPackage.cjs",
|
package/packageInfo.cjs
CHANGED
package/packageInfo.js
CHANGED