@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.
@@ -1,233 +1,204 @@
1
1
  (function (global, factory) {
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));
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
- const global = typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : window;
7
+ const global = typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : window;
8
8
 
9
- function documentReadyPromise(creator) {
10
- return new Promise(resolve => {
11
- if (document.readyState === 'complete') {
12
- resolve(creator());
13
- } else {
14
- window.addEventListener('load', () => resolve(creator()));
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
- const packageInfo = {
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
- const unwrapBytes = util.u8aUnwrapBytes;
27
- const wrapBytes = util.u8aWrapBytes;
22
+ const unwrapBytes = util.u8aUnwrapBytes;
23
+ const wrapBytes = util.u8aWrapBytes;
28
24
 
29
- const win = window;
30
- win.injectedWeb3 = win.injectedWeb3 || {};
31
- exports.isWeb3Injected = web3IsInjected();
32
- exports.web3EnablePromise = null;
33
- function web3IsInjected() {
34
- return Object.values(win.injectedWeb3).filter(({
35
- connect,
36
- enable
37
- }) => !!(connect || enable)).length !== 0;
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
- const sources = await exports.web3EnablePromise;
82
- return sources.filter(({
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
- const initCompat = compatInits.length ? Promise.all(compatInits.map(c => c().catch(() => false))) : Promise.resolve([true]);
91
- exports.web3EnablePromise = documentReadyPromise(() => initCompat.then(() => getWindowExtensions(originName).then(values => values.map(e => {
92
- if (!e.accounts.subscribe) {
93
- e.accounts.subscribe = cb => {
94
- e.accounts.get().then(cb).catch(console.error);
95
- return () => {
96
- };
97
- };
98
- }
99
- return e;
100
- })).catch(() => []).then(values => {
101
- const names = values.map(({
102
- name,
103
- version
104
- }) => `${name}/${version}`);
105
- exports.isWeb3Injected = web3IsInjected();
106
- console.info(`web3Enable: Enabled ${values.length} extension${values.length !== 1 ? 's' : ''}: ${names.join(', ')}`);
107
- return values;
108
- })));
109
- return exports.web3EnablePromise;
110
- }
111
- async function web3Accounts({
112
- accountType,
113
- extensions,
114
- genesisHash,
115
- ss58Format
116
- } = {}) {
117
- const accounts = [];
118
- const sources = await filterEnable('web3Accounts', extensions);
119
- const retrieved = await Promise.all(sources.map(async ({
120
- accounts,
121
- name: source
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
- } catch (error) {
161
- console.error(error);
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
- const sources = await exports.web3EnablePromise;
175
- const found = source && sources.find(({
176
- name
177
- }) => name === source);
178
- if (!found) {
179
- throw new Error(`web3FromSource: Unable to find an injected ${source}`);
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
- return found;
182
- }
183
- async function web3FromAddress(address) {
184
- if (!exports.web3EnablePromise) {
185
- return throwError('web3FromAddress');
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
- const accounts = await web3Accounts();
188
- let found;
189
- if (address) {
190
- const accountU8a = utilCrypto.decodeAddress(address);
191
- found = accounts.find(account => util.u8aEq(utilCrypto.decodeAddress(account.address), accountU8a));
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
- if (!found) {
194
- throw new Error(`web3FromAddress: Unable to find injected ${address}`);
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
- return web3FromSource(found.meta.source);
197
- }
198
- async function web3ListRpcProviders(source) {
199
- const {
200
- provider
201
- } = await web3FromSource(source);
202
- if (!provider) {
203
- console.warn(`Extension ${source} does not expose any provider`);
204
- return null;
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
- return provider.listProviders();
207
- }
208
- async function web3UseRpcProvider(source, key) {
209
- const {
210
- provider
211
- } = await web3FromSource(source);
212
- if (!provider) {
213
- throw new Error(`Extension ${source} does not expose any provider`);
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
- exports.packageInfo = packageInfo;
223
- exports.unwrapBytes = unwrapBytes;
224
- exports.web3Accounts = web3Accounts;
225
- exports.web3AccountsSubscribe = web3AccountsSubscribe;
226
- exports.web3Enable = web3Enable;
227
- exports.web3FromAddress = web3FromAddress;
228
- exports.web3FromSource = web3FromSource;
229
- exports.web3ListRpcProviders = web3ListRpcProviders;
230
- exports.web3UseRpcProvider = web3UseRpcProvider;
231
- exports.wrapBytes = wrapBytes;
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 };