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