@polkadot/extension-dapp 0.44.9 → 0.45.2
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 +184 -213
- package/bundle.d.ts +2 -2
- package/bundle.js +146 -197
- package/cjs/bundle.js +161 -256
- 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.d.ts +1 -1
- 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
|
@@ -1,233 +1,204 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@polkadot/util'), require('@polkadot/util-crypto')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', '@polkadot/util', '@polkadot/util-crypto'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.polkadotExtensionDapp = {}, global.polkadotUtil, global.polkadotUtilCrypto));
|
|
5
5
|
})(this, (function (exports, util, utilCrypto) { 'use strict';
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
const global = typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : window;
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
function documentReadyPromise(creator) {
|
|
10
|
+
return new Promise((resolve) => {
|
|
11
|
+
if (document.readyState === 'complete') {
|
|
12
|
+
resolve(creator());
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
window.addEventListener('load', () => resolve(creator()));
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
name: '@polkadot/extension-dapp',
|
|
21
|
-
path: (({ url: (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('bundle-polkadot-extension-dapp.js', document.baseURI).href)) }) && (typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('bundle-polkadot-extension-dapp.js', document.baseURI).href))) ? new URL((typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('bundle-polkadot-extension-dapp.js', document.baseURI).href))).pathname.substring(0, new URL((typeof document === 'undefined' && typeof location === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('bundle-polkadot-extension-dapp.js', document.baseURI).href))).pathname.lastIndexOf('/') + 1) : 'auto',
|
|
22
|
-
type: 'esm',
|
|
23
|
-
version: '0.44.9'
|
|
24
|
-
};
|
|
20
|
+
const packageInfo = { name: '@polkadot/extension-dapp', path: (({ url: (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('bundle-polkadot-extension-dapp.js', document.baseURI).href)) }) && (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('bundle-polkadot-extension-dapp.js', document.baseURI).href))) ? new URL((typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('bundle-polkadot-extension-dapp.js', document.baseURI).href))).pathname.substring(0, new URL((typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('bundle-polkadot-extension-dapp.js', document.baseURI).href))).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '0.45.2' };
|
|
25
21
|
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
const unwrapBytes = util.u8aUnwrapBytes;
|
|
23
|
+
const wrapBytes = util.u8aWrapBytes;
|
|
28
24
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
function throwError(method) {
|
|
40
|
-
throw new Error(`${method}: web3Enable(originName) needs to be called before ${method}`);
|
|
41
|
-
}
|
|
42
|
-
function mapAccounts(source, list, ss58Format) {
|
|
43
|
-
return list.map(({
|
|
44
|
-
address,
|
|
45
|
-
genesisHash,
|
|
46
|
-
name,
|
|
47
|
-
type
|
|
48
|
-
}) => ({
|
|
49
|
-
address: address.length === 42 ? address : utilCrypto.encodeAddress(utilCrypto.decodeAddress(address), ss58Format),
|
|
50
|
-
meta: {
|
|
51
|
-
genesisHash,
|
|
52
|
-
name,
|
|
53
|
-
source
|
|
54
|
-
},
|
|
55
|
-
type
|
|
56
|
-
}));
|
|
57
|
-
}
|
|
58
|
-
function filterAccounts(list, genesisHash, type) {
|
|
59
|
-
return list.filter(a => (!a.type || !type || type.includes(a.type)) && (!a.genesisHash || !genesisHash || a.genesisHash === genesisHash));
|
|
60
|
-
}
|
|
61
|
-
function getWindowExtensions(originName) {
|
|
62
|
-
return Promise.all(Object.entries(win.injectedWeb3).map(([nameOrHash, {
|
|
63
|
-
connect,
|
|
64
|
-
enable,
|
|
65
|
-
version
|
|
66
|
-
}]) => Promise.resolve().then(() => connect
|
|
67
|
-
? connect(originName) : enable
|
|
68
|
-
? enable(originName).then(e => util.objectSpread({
|
|
69
|
-
name: nameOrHash,
|
|
70
|
-
version: version || 'unknown'
|
|
71
|
-
}, e)) : Promise.reject(new Error('No connect(..) or enable(...) hook found'))).catch(({
|
|
72
|
-
message
|
|
73
|
-
}) => {
|
|
74
|
-
console.error(`Error initializing ${nameOrHash}: ${message}`);
|
|
75
|
-
}))).then(exts => exts.filter(e => !!e));
|
|
76
|
-
}
|
|
77
|
-
async function filterEnable(caller, extensions) {
|
|
78
|
-
if (!exports.web3EnablePromise) {
|
|
79
|
-
return throwError(caller);
|
|
25
|
+
const win = window;
|
|
26
|
+
win.injectedWeb3 = win.injectedWeb3 || {};
|
|
27
|
+
exports.isWeb3Injected = web3IsInjected();
|
|
28
|
+
exports.web3EnablePromise = null;
|
|
29
|
+
function web3IsInjected() {
|
|
30
|
+
return Object
|
|
31
|
+
.values(win.injectedWeb3)
|
|
32
|
+
.filter(({ connect, enable }) => !!(connect || enable))
|
|
33
|
+
.length !== 0;
|
|
80
34
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
name
|
|
84
|
-
}) => !extensions || extensions.includes(name));
|
|
85
|
-
}
|
|
86
|
-
function web3Enable(originName, compatInits = []) {
|
|
87
|
-
if (!originName) {
|
|
88
|
-
throw new Error('You must pass a name for your app to the web3Enable function');
|
|
35
|
+
function throwError(method) {
|
|
36
|
+
throw new Error(`${method}: web3Enable(originName) needs to be called before ${method}`);
|
|
89
37
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}) => {
|
|
123
|
-
try {
|
|
124
|
-
const list = await accounts.get();
|
|
125
|
-
return mapAccounts(source, filterAccounts(list, genesisHash, accountType), ss58Format);
|
|
126
|
-
} catch (error) {
|
|
127
|
-
return [];
|
|
128
|
-
}
|
|
129
|
-
}));
|
|
130
|
-
retrieved.forEach(result => {
|
|
131
|
-
accounts.push(...result);
|
|
132
|
-
});
|
|
133
|
-
console.info(`web3Accounts: Found ${accounts.length} address${accounts.length !== 1 ? 'es' : ''}`);
|
|
134
|
-
return accounts;
|
|
135
|
-
}
|
|
136
|
-
async function web3AccountsSubscribe(cb, {
|
|
137
|
-
accountType,
|
|
138
|
-
extensions,
|
|
139
|
-
genesisHash,
|
|
140
|
-
ss58Format
|
|
141
|
-
} = {}) {
|
|
142
|
-
const sources = await filterEnable('web3AccountsSubscribe', extensions);
|
|
143
|
-
const accounts = {};
|
|
144
|
-
const triggerUpdate = () => cb(Object.entries(accounts).reduce((result, [source, list]) => {
|
|
145
|
-
result.push(...mapAccounts(source, filterAccounts(list, genesisHash, accountType), ss58Format));
|
|
146
|
-
return result;
|
|
147
|
-
}, []));
|
|
148
|
-
const unsubs = sources.map(({
|
|
149
|
-
accounts: {
|
|
150
|
-
subscribe
|
|
151
|
-
},
|
|
152
|
-
name: source
|
|
153
|
-
}) => subscribe(result => {
|
|
154
|
-
accounts[source] = result;
|
|
155
|
-
try {
|
|
156
|
-
const result = triggerUpdate();
|
|
157
|
-
if (result && util.isPromise(result)) {
|
|
158
|
-
result.catch(console.error);
|
|
38
|
+
function mapAccounts(source, list, ss58Format) {
|
|
39
|
+
return list.map(({ address, genesisHash, name, type }) => ({
|
|
40
|
+
address: address.length === 42
|
|
41
|
+
? address
|
|
42
|
+
: utilCrypto.encodeAddress(utilCrypto.decodeAddress(address), ss58Format),
|
|
43
|
+
meta: { genesisHash, name, source },
|
|
44
|
+
type
|
|
45
|
+
}));
|
|
46
|
+
}
|
|
47
|
+
function filterAccounts(list, genesisHash, type) {
|
|
48
|
+
return list.filter((a) => (!a.type || !type || type.includes(a.type)) &&
|
|
49
|
+
(!a.genesisHash || !genesisHash || a.genesisHash === genesisHash));
|
|
50
|
+
}
|
|
51
|
+
function getWindowExtensions(originName) {
|
|
52
|
+
return Promise
|
|
53
|
+
.all(Object
|
|
54
|
+
.entries(win.injectedWeb3)
|
|
55
|
+
.map(([nameOrHash, { connect, enable, version }]) => Promise
|
|
56
|
+
.resolve()
|
|
57
|
+
.then(() => connect
|
|
58
|
+
? connect(originName)
|
|
59
|
+
: enable
|
|
60
|
+
? enable(originName).then((e) => util.objectSpread({ name: nameOrHash, version: version || 'unknown' }, e))
|
|
61
|
+
: Promise.reject(new Error('No connect(..) or enable(...) hook found')))
|
|
62
|
+
.catch(({ message }) => {
|
|
63
|
+
console.error(`Error initializing ${nameOrHash}: ${message}`);
|
|
64
|
+
})))
|
|
65
|
+
.then((exts) => exts.filter((e) => !!e));
|
|
66
|
+
}
|
|
67
|
+
async function filterEnable(caller, extensions) {
|
|
68
|
+
if (!exports.web3EnablePromise) {
|
|
69
|
+
return throwError(caller);
|
|
159
70
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}));
|
|
164
|
-
return () => {
|
|
165
|
-
unsubs.forEach(unsub => {
|
|
166
|
-
unsub();
|
|
167
|
-
});
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
async function web3FromSource(source) {
|
|
171
|
-
if (!exports.web3EnablePromise) {
|
|
172
|
-
return throwError('web3FromSource');
|
|
71
|
+
const sources = await exports.web3EnablePromise;
|
|
72
|
+
return sources.filter(({ name }) => !extensions ||
|
|
73
|
+
extensions.includes(name));
|
|
173
74
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
75
|
+
function web3Enable(originName, compatInits = []) {
|
|
76
|
+
if (!originName) {
|
|
77
|
+
throw new Error('You must pass a name for your app to the web3Enable function');
|
|
78
|
+
}
|
|
79
|
+
const initCompat = compatInits.length
|
|
80
|
+
? Promise.all(compatInits.map((c) => c().catch(() => false)))
|
|
81
|
+
: Promise.resolve([true]);
|
|
82
|
+
exports.web3EnablePromise = documentReadyPromise(() => initCompat.then(() => getWindowExtensions(originName)
|
|
83
|
+
.then((values) => values.map((e) => {
|
|
84
|
+
if (!e.accounts.subscribe) {
|
|
85
|
+
e.accounts.subscribe = (cb) => {
|
|
86
|
+
e.accounts
|
|
87
|
+
.get()
|
|
88
|
+
.then(cb)
|
|
89
|
+
.catch(console.error);
|
|
90
|
+
return () => {
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return e;
|
|
95
|
+
}))
|
|
96
|
+
.catch(() => [])
|
|
97
|
+
.then((values) => {
|
|
98
|
+
const names = values.map(({ name, version }) => `${name}/${version}`);
|
|
99
|
+
exports.isWeb3Injected = web3IsInjected();
|
|
100
|
+
console.info(`web3Enable: Enabled ${values.length} extension${values.length !== 1 ? 's' : ''}: ${names.join(', ')}`);
|
|
101
|
+
return values;
|
|
102
|
+
})));
|
|
103
|
+
return exports.web3EnablePromise;
|
|
180
104
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
105
|
+
async function web3Accounts({ accountType, extensions, genesisHash, ss58Format } = {}) {
|
|
106
|
+
const accounts = [];
|
|
107
|
+
const sources = await filterEnable('web3Accounts', extensions);
|
|
108
|
+
const retrieved = await Promise.all(sources.map(async ({ accounts, name: source }) => {
|
|
109
|
+
try {
|
|
110
|
+
const list = await accounts.get();
|
|
111
|
+
return mapAccounts(source, filterAccounts(list, genesisHash, accountType), ss58Format);
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
return [];
|
|
115
|
+
}
|
|
116
|
+
}));
|
|
117
|
+
retrieved.forEach((result) => {
|
|
118
|
+
accounts.push(...result);
|
|
119
|
+
});
|
|
120
|
+
console.info(`web3Accounts: Found ${accounts.length} address${accounts.length !== 1 ? 'es' : ''}`);
|
|
121
|
+
return accounts;
|
|
186
122
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
123
|
+
async function web3AccountsSubscribe(cb, { accountType, extensions, genesisHash, ss58Format } = {}) {
|
|
124
|
+
const sources = await filterEnable('web3AccountsSubscribe', extensions);
|
|
125
|
+
const accounts = {};
|
|
126
|
+
const triggerUpdate = () => cb(Object
|
|
127
|
+
.entries(accounts)
|
|
128
|
+
.reduce((result, [source, list]) => {
|
|
129
|
+
result.push(...mapAccounts(source, filterAccounts(list, genesisHash, accountType), ss58Format));
|
|
130
|
+
return result;
|
|
131
|
+
}, []));
|
|
132
|
+
const unsubs = sources.map(({ accounts: { subscribe }, name: source }) => subscribe((result) => {
|
|
133
|
+
accounts[source] = result;
|
|
134
|
+
try {
|
|
135
|
+
const result = triggerUpdate();
|
|
136
|
+
if (result && util.isPromise(result)) {
|
|
137
|
+
result.catch(console.error);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
console.error(error);
|
|
142
|
+
}
|
|
143
|
+
}));
|
|
144
|
+
return () => {
|
|
145
|
+
unsubs.forEach((unsub) => {
|
|
146
|
+
unsub();
|
|
147
|
+
});
|
|
148
|
+
};
|
|
192
149
|
}
|
|
193
|
-
|
|
194
|
-
|
|
150
|
+
async function web3FromSource(source) {
|
|
151
|
+
if (!exports.web3EnablePromise) {
|
|
152
|
+
return throwError('web3FromSource');
|
|
153
|
+
}
|
|
154
|
+
const sources = await exports.web3EnablePromise;
|
|
155
|
+
const found = source && sources.find(({ name }) => name === source);
|
|
156
|
+
if (!found) {
|
|
157
|
+
throw new Error(`web3FromSource: Unable to find an injected ${source}`);
|
|
158
|
+
}
|
|
159
|
+
return found;
|
|
195
160
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
161
|
+
async function web3FromAddress(address) {
|
|
162
|
+
if (!exports.web3EnablePromise) {
|
|
163
|
+
return throwError('web3FromAddress');
|
|
164
|
+
}
|
|
165
|
+
const accounts = await web3Accounts();
|
|
166
|
+
let found;
|
|
167
|
+
if (address) {
|
|
168
|
+
const accountU8a = utilCrypto.decodeAddress(address);
|
|
169
|
+
found = accounts.find((account) => util.u8aEq(utilCrypto.decodeAddress(account.address), accountU8a));
|
|
170
|
+
}
|
|
171
|
+
if (!found) {
|
|
172
|
+
throw new Error(`web3FromAddress: Unable to find injected ${address}`);
|
|
173
|
+
}
|
|
174
|
+
return web3FromSource(found.meta.source);
|
|
205
175
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
176
|
+
async function web3ListRpcProviders(source) {
|
|
177
|
+
const { provider } = await web3FromSource(source);
|
|
178
|
+
if (!provider) {
|
|
179
|
+
console.warn(`Extension ${source} does not expose any provider`);
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
return provider.listProviders();
|
|
183
|
+
}
|
|
184
|
+
async function web3UseRpcProvider(source, key) {
|
|
185
|
+
const { provider } = await web3FromSource(source);
|
|
186
|
+
if (!provider) {
|
|
187
|
+
throw new Error(`Extension ${source} does not expose any provider`);
|
|
188
|
+
}
|
|
189
|
+
const meta = await provider.startProvider(key);
|
|
190
|
+
return { meta, provider };
|
|
214
191
|
}
|
|
215
|
-
const meta = await provider.startProvider(key);
|
|
216
|
-
return {
|
|
217
|
-
meta,
|
|
218
|
-
provider
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
192
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
193
|
+
exports.packageInfo = packageInfo;
|
|
194
|
+
exports.unwrapBytes = unwrapBytes;
|
|
195
|
+
exports.web3Accounts = web3Accounts;
|
|
196
|
+
exports.web3AccountsSubscribe = web3AccountsSubscribe;
|
|
197
|
+
exports.web3Enable = web3Enable;
|
|
198
|
+
exports.web3FromAddress = web3FromAddress;
|
|
199
|
+
exports.web3FromSource = web3FromSource;
|
|
200
|
+
exports.web3ListRpcProviders = web3ListRpcProviders;
|
|
201
|
+
exports.web3UseRpcProvider = web3UseRpcProvider;
|
|
202
|
+
exports.wrapBytes = wrapBytes;
|
|
232
203
|
|
|
233
204
|
}));
|
package/bundle.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
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';
|
|
2
|
+
export { packageInfo } from './packageInfo.js';
|
|
3
|
+
export { unwrapBytes, wrapBytes } from './wrapBytes.js';
|
|
4
4
|
declare let isWeb3Injected: boolean;
|
|
5
5
|
declare let web3EnablePromise: Promise<InjectedExtension[]> | null;
|
|
6
6
|
export { isWeb3Injected, web3EnablePromise };
|