Skip to content

Commit

Permalink
fix: fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
RyukTheCoder committed Dec 31, 2024
1 parent 6a8df7b commit 025c837
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
4 changes: 4 additions & 0 deletions wallets/react/src/hub/autoConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ export async function autoConnect(deps: {
// Run `.connect` if `.canEagerConnect` returns `true`.
walletIds.forEach((providerName) => {
if (wallets && !wallets.includes(providerName)) {
console.warn(
'Trying to run auto connect for a wallet which is not included in config. Desired wallet:',
providerName
);
walletsToRemoveFromPersistance.push(providerName);
return;
}
Expand Down
44 changes: 22 additions & 22 deletions widget/embedded/src/utils/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export function matchAndGenerateProviders(
const all = allProviders(envs);

if (providers) {
/*
* If `wallets` is included in widget config,
* allProviders should be filtered based on wallets list
*/
const selectedProviders: VersionedProviders[] = [];

providers.forEach((requestedProvider) => {
Expand All @@ -58,11 +62,17 @@ export function matchAndGenerateProviders(
*/
if (typeof requestedProvider === 'string') {
const result = all.find((provider) => {
const versionedProvider =
pickSingleProviderVersionWithFallbackToLegacy(
provider,
options?.experimentalWallet
);
/*
* To find a provider in allProviders,
* a version of each provider should be picked
* and validated based on that version scheme.
* If the corresponding provider to a wallet was found in allProvider,
* it will be add to selected providers.
*/
const versionedProvider = pickProviderVersionWithFallbackToLegacy(
provider,
options
);
if (versionedProvider instanceof Provider) {
return versionedProvider.id === requestedProvider;
}
Expand All @@ -73,6 +83,7 @@ export function matchAndGenerateProviders(
selectedProviders.push(result);
}
console.warn(
// A provider name is included in config but was not found in allProviders
`Couldn't find ${requestedProvider} provider. Please make sure you are passing the correct name.`
);
} else {
Expand All @@ -95,23 +106,13 @@ export function matchAndGenerateProviders(
return all;
}

// TODO: this is a duplication with what we do in core.
function pickVersionWithFallbackToLegacy(
providers: VersionedProviders[],
options?: ProvidersOptions
): BothProvidersInterface[] {
const { experimentalWallet = 'enabled' } = options || {};

return providers.map((provider) =>
pickSingleProviderVersionWithFallbackToLegacy(provider, experimentalWallet)
);
}

function pickSingleProviderVersionWithFallbackToLegacy(
function pickProviderVersionWithFallbackToLegacy(
provider: VersionedProviders,
experimentalWallet?: 'enabled' | 'disabled'
options?: ProvidersOptions
): BothProvidersInterface {
const { experimentalWallet = 'enabled' } = options || {};
const version = experimentalWallet == 'disabled' ? '0.0.0' : '1.0.0';

try {
return pickVersion(provider, version)[1];
} catch {
Expand All @@ -124,9 +125,8 @@ export function configWalletsToWalletName(
config: WidgetConfig['wallets'],
options?: ProvidersOptions
): string[] {
const providers = pickVersionWithFallbackToLegacy(
matchAndGenerateProviders(config, options),
options
const providers = matchAndGenerateProviders(config, options).map((provider) =>
pickProviderVersionWithFallbackToLegacy(provider, options)
);
const names = providers.map((provider) => {
if (provider instanceof Provider) {
Expand Down

0 comments on commit 025c837

Please sign in to comment.