Skip to content

Commit 4fe4a0e

Browse files
committed
prevent enabling extensions if a dapp has requested snap_only to just inject snaps
like what w3ux in Staking Dashboard needs
1 parent 71402c6 commit 4fe4a0e

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

packages/extension-dapp/src/bundle.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2019-2024 @polkadot/extension-dapp authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

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';
55

66
import { isPromise, objectSpread, u8aEq } from '@polkadot/util';
77
import { decodeAddress, encodeAddress } from '@polkadot/util-crypto';
@@ -63,18 +63,37 @@ function filterAccounts (list: InjectedAccount[], genesisHash?: string | null, t
6363

6464
/** @internal retrieves all the extensions available on the window */
6565
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;
7474

7575
/** inject snap into window */
7676
hasMetamask && (win.injectedWeb3[DEFAULT_SNAP_NAME] = injectedMetamaskSnap);
7777

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+
7897
return Promise
7998
.all(
8099
Object
@@ -145,7 +164,7 @@ export function web3Enable (originName: string, compatInits: (() => Promise<bool
145164
.catch(console.error);
146165

147166
return (): void => {
148-
// no ubsubscribe needed, this is a single-shot
167+
// no unsubscribe needed, this is a single-shot
149168
};
150169
};
151170
}

0 commit comments

Comments
 (0)