@polkadot/extension-dapp 0.44.6 → 0.44.8

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