|
1 | 1 | // Copyright 2019-2024 @polkadot/extension-dapp authors & contributors |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | | -import type { InjectedAccount, InjectedAccountWithMeta, InjectedExtension, InjectedProviderWithMeta, InjectedWindow, InjectedWindowProvider, ProviderList, Unsubcall, Web3AccountsOptions } from '@polkadot/extension-inject/types'; |
| 4 | +import type { InjectedAccount, InjectedAccountWithMeta, InjectedExtension, InjectedProviderWithMeta, InjectedWindow, ProviderList, Unsubcall, Web3AccountsOptions } from '@polkadot/extension-inject/types'; |
5 | 5 |
|
6 | 6 | import { isPromise, objectSpread, u8aEq } from '@polkadot/util'; |
7 | 7 | import { decodeAddress, encodeAddress } from '@polkadot/util-crypto'; |
@@ -63,18 +63,37 @@ function filterAccounts (list: InjectedAccount[], genesisHash?: string | null, t |
63 | 63 |
|
64 | 64 | /** @internal retrieves all the extensions available on the window */ |
65 | 65 | function getWindowExtensions (originName: string): Promise<InjectedExtension[]> { |
66 | | - let extensions: Record<string, InjectedWindowProvider>; |
67 | | - |
68 | | - /** If originName indicates enabling only snap */ |
69 | | - if (['onlysnap', 'only_snap', 'snaponly', 'snap_only'].includes(originName.toLowerCase())) { |
70 | | - extensions = { DEFAULT_SNAP_NAME : injectedMetamaskSnap }; // no other extensions |
71 | | - } else { |
72 | | - extensions = win.injectedWeb3; |
73 | | - } |
| 66 | + |
| 67 | + /** Since web3Enable enables all available extensions, which is the default behavior |
| 68 | + * for Polkadot JS apps, some dapps, like the Staking dashboard, provide an extension |
| 69 | + * list where users can choose specific extensions to enable. Therefore, we utilize |
| 70 | + * "Snap only" to inject snaps as an additional feature, suitable for such dapps. |
| 71 | + * */ |
| 72 | + const isSnapOnlyRequested = ['onlysnap', 'only_snap', 'snaponly', 'snap_only'].includes(originName.toLowerCase()); |
| 73 | + const extensions = isSnapOnlyRequested ? { DEFAULT_SNAP_NAME: injectedMetamaskSnap }: win.injectedWeb3; |
74 | 74 |
|
75 | 75 | /** inject snap into window */ |
76 | 76 | hasMetamask && (win.injectedWeb3[DEFAULT_SNAP_NAME] = injectedMetamaskSnap); |
77 | 77 |
|
| 78 | + if (isSnapOnlyRequested) { |
| 79 | + return Promise |
| 80 | + .all( |
| 81 | + Object |
| 82 | + .entries(extensions) |
| 83 | + .map(([nameOrHash, { version }]): Promise<(InjectedExtension | void)> => |
| 84 | + Promise |
| 85 | + .resolve() |
| 86 | + .then(() => |
| 87 | + objectSpread<InjectedExtension>({ name: nameOrHash, version: version || 'unknown' }) |
| 88 | + ) |
| 89 | + .catch(({ message }: Error): void => { |
| 90 | + console.error(`Error injecting ${nameOrHash}: ${message}`); |
| 91 | + }) |
| 92 | + ) |
| 93 | + ) |
| 94 | + .then((exts) => exts.filter((e): e is InjectedExtension => !!e)); |
| 95 | + } |
| 96 | + |
78 | 97 | return Promise |
79 | 98 | .all( |
80 | 99 | Object |
@@ -145,7 +164,7 @@ export function web3Enable (originName: string, compatInits: (() => Promise<bool |
145 | 164 | .catch(console.error); |
146 | 165 |
|
147 | 166 | return (): void => { |
148 | | - // no ubsubscribe needed, this is a single-shot |
| 167 | + // no unsubscribe needed, this is a single-shot |
149 | 168 | }; |
150 | 169 | }; |
151 | 170 | } |
|
0 commit comments