From ef82ddfe485e47bc4e48e2958fc162cc2db45ac5 Mon Sep 17 00:00:00 2001 From: Tudor Morar Date: Thu, 5 Dec 2024 11:27:52 +0200 Subject: [PATCH 1/8] Fix config 1 --- src/core/providers/helpers/getConfig.ts | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/core/providers/helpers/getConfig.ts b/src/core/providers/helpers/getConfig.ts index 70a2b92..20f561e 100644 --- a/src/core/providers/helpers/getConfig.ts +++ b/src/core/providers/helpers/getConfig.ts @@ -2,13 +2,26 @@ import type { LedgerConnectModal } from '@multiversx/sdk-dapp-core-ui/dist/compo import { defineCustomElements } from '@multiversx/sdk-dapp-core-ui/loader'; import { safeWindow } from 'constants/index'; import { + IEventBus, IProviderConfig, + IProviderConfigUI, ProviderTypeEnum } from '../types/providerFactory.types'; -export const getConfig = async (config: IProviderConfig = {}) => { +const UI: IProviderConfigUI = { + [ProviderTypeEnum.ledger]: { + eventBus: {} as IEventBus, + mount: () => { + throw new Error('mount not implemented'); + } + } +}; + +const defaultConfig = { UI }; + +export const getConfig = async (config: IProviderConfig = defaultConfig) => { if (!safeWindow.document) { - return config; + return { ...defaultConfig, ...config }; } defineCustomElements(safeWindow); @@ -19,7 +32,7 @@ export const getConfig = async (config: IProviderConfig = {}) => { await customElements.whenDefined('ledger-connect-modal'); const eventBus = await ledgerModalElement.getEventBus(); - const ui = { + const UI = { [ProviderTypeEnum.ledger]: { eventBus, mount: () => { @@ -30,9 +43,9 @@ export const getConfig = async (config: IProviderConfig = {}) => { return { ...config, - ui: { - ...ui, - ...config.ui + UI: { + ...UI, + ...config.UI } }; }; From da7adaaf45c403919ad23cb1c29befac47069d30 Mon Sep 17 00:00:00 2001 From: Tudor Morar Date: Thu, 5 Dec 2024 11:28:19 +0200 Subject: [PATCH 2/8] Fix config 2 --- .../providers/types/providerFactory.types.ts | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/core/providers/types/providerFactory.types.ts b/src/core/providers/types/providerFactory.types.ts index e95466c..a51b0bc 100644 --- a/src/core/providers/types/providerFactory.types.ts +++ b/src/core/providers/types/providerFactory.types.ts @@ -1,7 +1,8 @@ import type { IDAppProviderBase } from '@multiversx/sdk-dapp-utils'; // @ts-ignore -export interface IProvider extends IDAppProviderBase { +export interface IProvider + extends IDAppProviderBase { init: () => Promise; login: (options?: { callbackUrl?: string; token?: string }) => Promise<{ address: string; @@ -12,7 +13,7 @@ export interface IProvider extends IDAppProviderBase { }>; logout: () => Promise; setShouldShowConsentPopup?: (shouldShow: boolean) => void; - getType: () => ProviderTypeEnum; + getType: () => T; getAddress(): Promise; // TODO will be removed as soon as the new login method is implemented in the same way for all providers getTokenLoginSignature(): string | undefined; @@ -26,16 +27,18 @@ export interface IEventBus { unsubscribe(event: string, callback: Function): void; } +export interface IProviderConfigUI { + ledger: { + eventBus: IEventBus; + mount: () => void; + }; +} + export interface IProviderConfig { account?: { address: string; }; - ui?: { - ledger: { - eventBus: IEventBus; - mount: () => void; - }; - }; + UI?: IProviderConfigUI; } export enum ProviderTypeEnum { @@ -47,13 +50,6 @@ export enum ProviderTypeEnum { opera = 'opera', metamask = 'metamask', passkey = 'passkey', - webhook = 'webhook', custom = 'custom', none = '' } - -export interface IProviderFactory { - type: ProviderTypeEnum; - config?: IProviderConfig; - customProvider?: IProvider; -} From 4d53f813fce4382ed5b47431e50b48973ea82c60 Mon Sep 17 00:00:00 2001 From: Tudor Morar Date: Thu, 5 Dec 2024 11:29:17 +0200 Subject: [PATCH 3/8] Fix config 3 --- src/core/providers/ProviderFactory.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/core/providers/ProviderFactory.ts b/src/core/providers/ProviderFactory.ts index d7427be..72397c6 100644 --- a/src/core/providers/ProviderFactory.ts +++ b/src/core/providers/ProviderFactory.ts @@ -10,11 +10,7 @@ import { createExtensionProvider } from './helpers/extension/createExtensionProv import { getConfig } from './helpers/getConfig'; import { createIframeProvider } from './helpers/iframe/createIframeProvider'; import { createLedgerProvider } from './helpers/ledger/createLedgerProvider'; -import { - IProvider, - IProviderFactory, - ProviderTypeEnum -} from './types/providerFactory.types'; +import { IProvider, ProviderTypeEnum } from './types/providerFactory.types'; export class ProviderFactory { public async create({ @@ -23,7 +19,7 @@ export class ProviderFactory { customProvider }: IProviderFactory): Promise { let createdProvider: IProvider | null = null; - const { account, ui } = await getConfig(userConfig); + const { account, UI } = await getConfig(userConfig); switch (type) { case ProviderTypeEnum.extension: { @@ -48,8 +44,8 @@ export class ProviderFactory { case ProviderTypeEnum.ledger: { const ledgerProvider = await createLedgerProvider( - ui.ledger.eventBus, - ui.ledger.mount + UI.ledger.eventBus, + UI.ledger.mount ); if (!ledgerProvider) { From 13eaebdf3240aceb5ee050d85140ad36b1604abf Mon Sep 17 00:00:00 2001 From: Tudor Morar Date: Thu, 5 Dec 2024 12:22:37 +0200 Subject: [PATCH 4/8] Work on custom providers --- src/core/methods/initApp/initApp.ts | 6 ++- src/core/methods/initApp/initApp.types.ts | 6 +++ src/core/providers/ProviderFactory.ts | 45 ++++++++++++------- src/core/providers/helpers/getConfig.ts | 25 +++++------ .../helpers/ledger/createLedgerProvider.ts | 14 +++--- src/core/providers/helpers/restoreProvider.ts | 4 +- src/core/providers/helpers/utils.ts | 3 -- .../providers/types/providerFactory.types.ts | 4 +- 8 files changed, 60 insertions(+), 47 deletions(-) diff --git a/src/core/methods/initApp/initApp.ts b/src/core/methods/initApp/initApp.ts index c83d67e..6829f98 100644 --- a/src/core/methods/initApp/initApp.ts +++ b/src/core/methods/initApp/initApp.ts @@ -1,4 +1,5 @@ import { restoreProvider } from 'core/providers/helpers/restoreProvider'; +import { ProviderFactory } from 'core/providers/ProviderFactory'; import { getDefaultNativeAuthConfig } from 'services/nativeAuth/methods/getDefaultNativeAuthConfig'; import { NativeAuthConfigType } from 'services/nativeAuth/nativeAuth.types'; import { initializeNetwork } from 'store/actions'; @@ -30,7 +31,8 @@ const defaultInitAppProps = { * */ export async function initApp({ storage = defaultInitAppProps.storage, - dAppConfig + dAppConfig, + customProviders }: InitAppType) { initStore(storage.getStorageCallback); @@ -57,6 +59,8 @@ export async function initApp({ const isLoggedIn = getIsLoggedIn(); + ProviderFactory.customProviders(customProviders || []); + if (isLoggedIn) { await restoreProvider(); await registerWebsocketListener(); diff --git a/src/core/methods/initApp/initApp.types.ts b/src/core/methods/initApp/initApp.types.ts index d53d8e3..1ef6bab 100644 --- a/src/core/methods/initApp/initApp.types.ts +++ b/src/core/methods/initApp/initApp.types.ts @@ -1,3 +1,4 @@ +import { IProvider } from 'core/providers/types/providerFactory.types'; import { NativeAuthConfigType } from 'services/nativeAuth/nativeAuth.types'; import { StorageCallback } from 'store/storage'; import { EnvironmentsEnum } from 'types/enums.types'; @@ -49,4 +50,9 @@ export type InitAppType = { getStorageCallback: StorageCallback; }; dAppConfig: DappConfigType; + customProviders?: Array<{ + name: string; + icon: string; + class: IProvider; + }>; }; diff --git a/src/core/providers/ProviderFactory.ts b/src/core/providers/ProviderFactory.ts index 72397c6..7eef767 100644 --- a/src/core/providers/ProviderFactory.ts +++ b/src/core/providers/ProviderFactory.ts @@ -13,11 +13,28 @@ import { createLedgerProvider } from './helpers/ledger/createLedgerProvider'; import { IProvider, ProviderTypeEnum } from './types/providerFactory.types'; export class ProviderFactory { - public async create({ + private static _customProviders: Array<{ + name: string; + icon: string; + class: IProvider; + }> = []; + + public static customProviders( + providers: { + name: string; + icon: string; + class: IProvider; + }[] + ) { + this._customProviders = providers; + } + + public static async create({ type, - config: userConfig, - customProvider - }: IProviderFactory): Promise { + config: userConfig + }: any): Promise { + // IProviderFactory + let createdProvider: IProvider | null = null; const { account, UI } = await getConfig(userConfig); @@ -43,10 +60,7 @@ export class ProviderFactory { } case ProviderTypeEnum.ledger: { - const ledgerProvider = await createLedgerProvider( - UI.ledger.eventBus, - UI.ledger.mount - ); + const ledgerProvider = await createLedgerProvider(UI.ledger.mount); if (!ledgerProvider) { throw new Error('Unable to create ledger provider'); @@ -102,16 +116,15 @@ export class ProviderFactory { break; } - case ProviderTypeEnum.custom: { - if (!customProvider) { - throw new Error('Unable to create custom provider provider'); - } - createdProvider = customProvider; + default: { + this._customProviders.forEach((customProvider) => { + if (customProvider.name === type) { + createdProvider = customProvider.class; + createdProvider.getType = () => type; + } + }); break; } - - default: - break; } if (!createdProvider) { diff --git a/src/core/providers/helpers/getConfig.ts b/src/core/providers/helpers/getConfig.ts index 20f561e..e937da4 100644 --- a/src/core/providers/helpers/getConfig.ts +++ b/src/core/providers/helpers/getConfig.ts @@ -2,7 +2,6 @@ import type { LedgerConnectModal } from '@multiversx/sdk-dapp-core-ui/dist/compo import { defineCustomElements } from '@multiversx/sdk-dapp-core-ui/loader'; import { safeWindow } from 'constants/index'; import { - IEventBus, IProviderConfig, IProviderConfigUI, ProviderTypeEnum @@ -10,7 +9,6 @@ import { const UI: IProviderConfigUI = { [ProviderTypeEnum.ledger]: { - eventBus: {} as IEventBus, mount: () => { throw new Error('mount not implemented'); } @@ -24,19 +22,18 @@ export const getConfig = async (config: IProviderConfig = defaultConfig) => { return { ...defaultConfig, ...config }; } - defineCustomElements(safeWindow); - const ledgerModalElement = document.createElement( - 'ledger-connect-modal' - ) as LedgerConnectModal; - document.body.appendChild(ledgerModalElement); - await customElements.whenDefined('ledger-connect-modal'); - const eventBus = await ledgerModalElement.getEventBus(); - const UI = { [ProviderTypeEnum.ledger]: { - eventBus, - mount: () => { + mount: async () => { + defineCustomElements(safeWindow); + const ledgerModalElement = document.createElement( + 'ledger-connect-modal' + ) as LedgerConnectModal; + document.body.appendChild(ledgerModalElement); + + const eventBus = await ledgerModalElement.getEventBus(); + return eventBus; } } }; @@ -44,8 +41,8 @@ export const getConfig = async (config: IProviderConfig = defaultConfig) => { return { ...config, UI: { - ...UI, - ...config.UI + ...defaultConfig.UI, + ...UI } }; }; diff --git a/src/core/providers/helpers/ledger/createLedgerProvider.ts b/src/core/providers/helpers/ledger/createLedgerProvider.ts index c200e7a..d2e476c 100644 --- a/src/core/providers/helpers/ledger/createLedgerProvider.ts +++ b/src/core/providers/helpers/ledger/createLedgerProvider.ts @@ -18,17 +18,17 @@ import { ILedgerAccount } from './ledger.types'; const failInitializeErrorText = 'Check if the MultiversX App is open on Ledger'; export async function createLedgerProvider( - eventBus: IEventBus, - mount: () => void + mount: () => Promise ): Promise { - if (!eventBus) { - throw new Error('Event bus not provided for Ledger provider'); - } - const shouldInitiateLogin = !getIsLoggedIn(); + let eventBus: IEventBus | undefined; if (shouldInitiateLogin) { - mount?.(); + eventBus = await mount?.(); + } + + if (!eventBus) { + throw new Error('Event bus not provided for Ledger provider'); } const manager = LedgerConnectStateManager.getInstance(eventBus); diff --git a/src/core/providers/helpers/restoreProvider.ts b/src/core/providers/helpers/restoreProvider.ts index 3e352ef..9a4ff0c 100644 --- a/src/core/providers/helpers/restoreProvider.ts +++ b/src/core/providers/helpers/restoreProvider.ts @@ -19,9 +19,7 @@ export async function restoreProvider() { } }; - const factory = new ProviderFactory(); - - const provider = await factory.create({ + const provider = await ProviderFactory.create({ type, config }); diff --git a/src/core/providers/helpers/utils.ts b/src/core/providers/helpers/utils.ts index 89e50f3..2935c6a 100644 --- a/src/core/providers/helpers/utils.ts +++ b/src/core/providers/helpers/utils.ts @@ -2,7 +2,6 @@ import { ExtensionProvider } from '@multiversx/sdk-extension-provider'; import { HWProvider } from '@multiversx/sdk-hw-provider'; import { MetamaskProvider } from '@multiversx/sdk-metamask-provider/out/metamaskProvider'; import { OperaProvider } from '@multiversx/sdk-opera-provider'; -import { WalletProvider } from '@multiversx/sdk-web-wallet-provider'; import { ProviderTypeEnum } from 'core/providers/types/providerFactory.types'; import { CrossWindowProvider } from 'lib/sdkWebWalletCrossWindowProvider'; import { WalletConnectV2Provider } from 'utils/walletconnect/__sdkWalletconnectProvider'; @@ -12,8 +11,6 @@ export function getProviderType( provider?: TProvider | null ): ProviderTypeEnum { switch (provider?.constructor) { - case WalletProvider: - return ProviderTypeEnum.webhook; // TODO: remove? case WalletConnectV2Provider: return ProviderTypeEnum.walletConnect; case HWProvider: diff --git a/src/core/providers/types/providerFactory.types.ts b/src/core/providers/types/providerFactory.types.ts index a51b0bc..9484268 100644 --- a/src/core/providers/types/providerFactory.types.ts +++ b/src/core/providers/types/providerFactory.types.ts @@ -29,8 +29,7 @@ export interface IEventBus { export interface IProviderConfigUI { ledger: { - eventBus: IEventBus; - mount: () => void; + mount: () => Promise; }; } @@ -50,6 +49,5 @@ export enum ProviderTypeEnum { opera = 'opera', metamask = 'metamask', passkey = 'passkey', - custom = 'custom', none = '' } From bcfa723464ee355834248a4776960235870b1c09 Mon Sep 17 00:00:00 2001 From: Tudor Morar Date: Thu, 5 Dec 2024 13:40:51 +0200 Subject: [PATCH 5/8] Code compiling --- src/core/methods/initApp/initApp.ts | 8 +++++++- src/core/providers/ProviderFactory.ts | 10 ++++++---- src/core/providers/types/providerFactory.types.ts | 5 +++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/core/methods/initApp/initApp.ts b/src/core/methods/initApp/initApp.ts index 6829f98..83ae383 100644 --- a/src/core/methods/initApp/initApp.ts +++ b/src/core/methods/initApp/initApp.ts @@ -1,3 +1,4 @@ +import { safeWindow } from 'constants/index'; import { restoreProvider } from 'core/providers/helpers/restoreProvider'; import { ProviderFactory } from 'core/providers/ProviderFactory'; import { getDefaultNativeAuthConfig } from 'services/nativeAuth/methods/getDefaultNativeAuthConfig'; @@ -59,7 +60,12 @@ export async function initApp({ const isLoggedIn = getIsLoggedIn(); - ProviderFactory.customProviders(customProviders || []); + const usedProviders = [ + ...((safeWindow as any)?.multiversx?.providers || []), + ...(customProviders || []) + ]; + + ProviderFactory.customProviders(usedProviders || []); if (isLoggedIn) { await restoreProvider(); diff --git a/src/core/providers/ProviderFactory.ts b/src/core/providers/ProviderFactory.ts index 7eef767..30f4230 100644 --- a/src/core/providers/ProviderFactory.ts +++ b/src/core/providers/ProviderFactory.ts @@ -10,7 +10,11 @@ import { createExtensionProvider } from './helpers/extension/createExtensionProv import { getConfig } from './helpers/getConfig'; import { createIframeProvider } from './helpers/iframe/createIframeProvider'; import { createLedgerProvider } from './helpers/ledger/createLedgerProvider'; -import { IProvider, ProviderTypeEnum } from './types/providerFactory.types'; +import { + IProvider, + IProviderFactory, + ProviderTypeEnum +} from './types/providerFactory.types'; export class ProviderFactory { private static _customProviders: Array<{ @@ -32,9 +36,7 @@ export class ProviderFactory { public static async create({ type, config: userConfig - }: any): Promise { - // IProviderFactory - + }: IProviderFactory): Promise { let createdProvider: IProvider | null = null; const { account, UI } = await getConfig(userConfig); diff --git a/src/core/providers/types/providerFactory.types.ts b/src/core/providers/types/providerFactory.types.ts index 9484268..eee3d8d 100644 --- a/src/core/providers/types/providerFactory.types.ts +++ b/src/core/providers/types/providerFactory.types.ts @@ -51,3 +51,8 @@ export enum ProviderTypeEnum { passkey = 'passkey', none = '' } + +export interface IProviderFactory { + type: ProviderTypeEnum; + config?: IProviderConfig; +} From d1b1d938ef0f09cfc207b7320e490fee0ce1e02b Mon Sep 17 00:00:00 2001 From: Tudor Morar Date: Thu, 5 Dec 2024 13:44:51 +0200 Subject: [PATCH 6/8] Improve custom providers --- src/core/methods/initApp/initApp.types.ts | 8 ++------ src/core/providers/ProviderFactory.ts | 17 ++++------------- .../providers/types/providerFactory.types.ts | 6 ++++++ 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/core/methods/initApp/initApp.types.ts b/src/core/methods/initApp/initApp.types.ts index 1ef6bab..69dbc89 100644 --- a/src/core/methods/initApp/initApp.types.ts +++ b/src/core/methods/initApp/initApp.types.ts @@ -1,4 +1,4 @@ -import { IProvider } from 'core/providers/types/providerFactory.types'; +import { ICustomProvider } from 'core/providers/types/providerFactory.types'; import { NativeAuthConfigType } from 'services/nativeAuth/nativeAuth.types'; import { StorageCallback } from 'store/storage'; import { EnvironmentsEnum } from 'types/enums.types'; @@ -50,9 +50,5 @@ export type InitAppType = { getStorageCallback: StorageCallback; }; dAppConfig: DappConfigType; - customProviders?: Array<{ - name: string; - icon: string; - class: IProvider; - }>; + customProviders?: ICustomProvider[]; }; diff --git a/src/core/providers/ProviderFactory.ts b/src/core/providers/ProviderFactory.ts index 30f4230..3214974 100644 --- a/src/core/providers/ProviderFactory.ts +++ b/src/core/providers/ProviderFactory.ts @@ -11,25 +11,16 @@ import { getConfig } from './helpers/getConfig'; import { createIframeProvider } from './helpers/iframe/createIframeProvider'; import { createLedgerProvider } from './helpers/ledger/createLedgerProvider'; import { + ICustomProvider, IProvider, IProviderFactory, ProviderTypeEnum } from './types/providerFactory.types'; export class ProviderFactory { - private static _customProviders: Array<{ - name: string; - icon: string; - class: IProvider; - }> = []; - - public static customProviders( - providers: { - name: string; - icon: string; - class: IProvider; - }[] - ) { + private static _customProviders: ICustomProvider[] = []; + + public static customProviders(providers: ICustomProvider[]) { this._customProviders = providers; } diff --git a/src/core/providers/types/providerFactory.types.ts b/src/core/providers/types/providerFactory.types.ts index eee3d8d..0d0e73d 100644 --- a/src/core/providers/types/providerFactory.types.ts +++ b/src/core/providers/types/providerFactory.types.ts @@ -56,3 +56,9 @@ export interface IProviderFactory { type: ProviderTypeEnum; config?: IProviderConfig; } + +export interface ICustomProvider { + name: string; + icon: string; + class: IProvider; +} From 1966b1b67e977d2522675d6e6427985418b59f0a Mon Sep 17 00:00:00 2001 From: Tudor Morar Date: Thu, 5 Dec 2024 14:18:49 +0200 Subject: [PATCH 7/8] Call with config --- .../DappProvider/helpers/logout/logout.ts | 4 +-- .../helpers/signMessage/signMessage.ts | 4 +-- src/core/providers/ProviderFactory.ts | 9 +++--- src/core/providers/helpers/utils.ts | 30 ------------------- .../providers/types/providerFactory.types.ts | 7 +++-- 5 files changed, 12 insertions(+), 42 deletions(-) delete mode 100644 src/core/providers/helpers/utils.ts diff --git a/src/core/providers/DappProvider/helpers/logout/logout.ts b/src/core/providers/DappProvider/helpers/logout/logout.ts index 2e6476c..73f3cc9 100644 --- a/src/core/providers/DappProvider/helpers/logout/logout.ts +++ b/src/core/providers/DappProvider/helpers/logout/logout.ts @@ -1,5 +1,4 @@ import { getAddress } from 'core/methods/account/getAddress'; -import { getProviderType } from 'core/providers/helpers/utils'; import { IProvider, ProviderTypeEnum @@ -48,7 +47,6 @@ export async function logout({ } }: IProviderLogout) { let address = getAddress(); - const providerType = getProviderType(provider); if (options.shouldBroadcastLogoutAcrossTabs) { broadcastLogoutAcrossTabs(address); @@ -59,7 +57,7 @@ export async function logout({ if ( options.hasConsentPopup && - providerType === ProviderTypeEnum.crossWindow + provider.getType() === ProviderTypeEnum.crossWindow ) { (provider as unknown as CrossWindowProvider).setShouldShowConsentPopup( true diff --git a/src/core/providers/DappProvider/helpers/signMessage/signMessage.ts b/src/core/providers/DappProvider/helpers/signMessage/signMessage.ts index 7cf3e8e..ed21f05 100644 --- a/src/core/providers/DappProvider/helpers/signMessage/signMessage.ts +++ b/src/core/providers/DappProvider/helpers/signMessage/signMessage.ts @@ -1,6 +1,5 @@ import { Message, Address } from '@multiversx/sdk-core'; import { getAddress } from 'core/methods/account/getAddress'; -import { getProviderType } from 'core/providers/helpers/utils'; import { IProvider, ProviderTypeEnum @@ -22,7 +21,6 @@ export async function signMessage({ options }: SignMessageType): Promise> { const address = getAddress(); - const providerType = getProviderType(provider); const messageToSign = new Message({ address: new Address(address), @@ -31,7 +29,7 @@ export async function signMessage({ if ( options?.hasConsentPopup && - providerType === ProviderTypeEnum.crossWindow + provider.getType() === ProviderTypeEnum.crossWindow ) { (provider as unknown as CrossWindowProvider).setShouldShowConsentPopup( true diff --git a/src/core/providers/ProviderFactory.ts b/src/core/providers/ProviderFactory.ts index 3214974..9c3c2a5 100644 --- a/src/core/providers/ProviderFactory.ts +++ b/src/core/providers/ProviderFactory.ts @@ -29,7 +29,8 @@ export class ProviderFactory { config: userConfig }: IProviderFactory): Promise { let createdProvider: IProvider | null = null; - const { account, UI } = await getConfig(userConfig); + const config = await getConfig(userConfig); + const { account, UI } = config; switch (type) { case ProviderTypeEnum.extension: { @@ -110,9 +111,9 @@ export class ProviderFactory { } default: { - this._customProviders.forEach((customProvider) => { - if (customProvider.name === type) { - createdProvider = customProvider.class; + this._customProviders.forEach(async (customProvider) => { + if (customProvider.name !== type) { + createdProvider = await customProvider.constructor(config); createdProvider.getType = () => type; } }); diff --git a/src/core/providers/helpers/utils.ts b/src/core/providers/helpers/utils.ts deleted file mode 100644 index 2935c6a..0000000 --- a/src/core/providers/helpers/utils.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { ExtensionProvider } from '@multiversx/sdk-extension-provider'; -import { HWProvider } from '@multiversx/sdk-hw-provider'; -import { MetamaskProvider } from '@multiversx/sdk-metamask-provider/out/metamaskProvider'; -import { OperaProvider } from '@multiversx/sdk-opera-provider'; -import { ProviderTypeEnum } from 'core/providers/types/providerFactory.types'; -import { CrossWindowProvider } from 'lib/sdkWebWalletCrossWindowProvider'; -import { WalletConnectV2Provider } from 'utils/walletconnect/__sdkWalletconnectProvider'; -import { EmptyProvider } from './emptyProvider'; - -export function getProviderType( - provider?: TProvider | null -): ProviderTypeEnum { - switch (provider?.constructor) { - case WalletConnectV2Provider: - return ProviderTypeEnum.walletConnect; - case HWProvider: - return ProviderTypeEnum.ledger; - case ExtensionProvider: - return ProviderTypeEnum.extension; - case MetamaskProvider: - return ProviderTypeEnum.metamask; - case OperaProvider: - return ProviderTypeEnum.opera; - case CrossWindowProvider: - return ProviderTypeEnum.crossWindow; - case EmptyProvider: - default: - return ProviderTypeEnum.none; - } -} diff --git a/src/core/providers/types/providerFactory.types.ts b/src/core/providers/types/providerFactory.types.ts index 0d0e73d..8b91ba3 100644 --- a/src/core/providers/types/providerFactory.types.ts +++ b/src/core/providers/types/providerFactory.types.ts @@ -57,8 +57,11 @@ export interface IProviderFactory { config?: IProviderConfig; } -export interface ICustomProvider { +export interface ICustomProvider< + T extends ProviderTypeEnum = ProviderTypeEnum +> { name: string; + type: T[keyof T]; icon: string; - class: IProvider; + constructor: (config: IProviderConfig) => Promise; } From ed1ea422b621ebf716b32d9f63e3318273498547 Mon Sep 17 00:00:00 2001 From: Tudor Morar Date: Thu, 5 Dec 2024 14:59:28 +0200 Subject: [PATCH 8/8] Project building --- src/core/providers/ProviderFactory.ts | 4 ++-- src/core/providers/types/providerFactory.types.ts | 8 +++++--- src/store/actions/loginInfo/loginInfoActions.ts | 4 +++- src/store/actions/sharedActions/sharedActions.ts | 6 ++++-- src/store/slices/loginInfo/loginInfo.types.ts | 6 ++++-- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/core/providers/ProviderFactory.ts b/src/core/providers/ProviderFactory.ts index 9c3c2a5..aa37f76 100644 --- a/src/core/providers/ProviderFactory.ts +++ b/src/core/providers/ProviderFactory.ts @@ -112,7 +112,7 @@ export class ProviderFactory { default: { this._customProviders.forEach(async (customProvider) => { - if (customProvider.name !== type) { + if (customProvider.type === type) { createdProvider = await customProvider.constructor(config); createdProvider.getType = () => type; } @@ -128,7 +128,7 @@ export class ProviderFactory { const dappProvider = new DappProvider(createdProvider); setAccountProvider(dappProvider); - setProviderType(type); + setProviderType(type as ProviderTypeEnum); return dappProvider; } diff --git a/src/core/providers/types/providerFactory.types.ts b/src/core/providers/types/providerFactory.types.ts index 8b91ba3..2063592 100644 --- a/src/core/providers/types/providerFactory.types.ts +++ b/src/core/providers/types/providerFactory.types.ts @@ -13,7 +13,7 @@ export interface IProvider }>; logout: () => Promise; setShouldShowConsentPopup?: (shouldShow: boolean) => void; - getType: () => T; + getType: () => T[keyof T] | string; getAddress(): Promise; // TODO will be removed as soon as the new login method is implemented in the same way for all providers getTokenLoginSignature(): string | undefined; @@ -52,8 +52,10 @@ export enum ProviderTypeEnum { none = '' } -export interface IProviderFactory { - type: ProviderTypeEnum; +export interface IProviderFactory< + T extends ProviderTypeEnum = ProviderTypeEnum +> { + type: T[keyof T]; config?: IProviderConfig; } diff --git a/src/store/actions/loginInfo/loginInfoActions.ts b/src/store/actions/loginInfo/loginInfoActions.ts index e9ae8ff..c8a610f 100644 --- a/src/store/actions/loginInfo/loginInfoActions.ts +++ b/src/store/actions/loginInfo/loginInfoActions.ts @@ -7,7 +7,9 @@ import { import { getStore } from 'store/store'; import { TokenLoginType } from 'types/login.types'; -export const setProviderType = (providerType: ProviderTypeEnum) => +export const setProviderType = ( + providerType: T +) => getStore().setState(({ loginInfo: state }) => { state.providerType = providerType; }); diff --git a/src/store/actions/sharedActions/sharedActions.ts b/src/store/actions/sharedActions/sharedActions.ts index 530925f..fcda2aa 100644 --- a/src/store/actions/sharedActions/sharedActions.ts +++ b/src/store/actions/sharedActions/sharedActions.ts @@ -4,9 +4,11 @@ import { resetStore } from 'store/middleware/logoutMiddleware'; import { getStore } from '../../store'; export const logoutAction = () => getStore().setState(resetStore); -export interface LoginActionPayloadType { +export interface LoginActionPayloadType< + T extends ProviderTypeEnum = ProviderTypeEnum +> { address: string; - providerType: ProviderTypeEnum; + providerType: T[keyof T]; } export const loginAction = ({ diff --git a/src/store/slices/loginInfo/loginInfo.types.ts b/src/store/slices/loginInfo/loginInfo.types.ts index 118b475..4560b12 100644 --- a/src/store/slices/loginInfo/loginInfo.types.ts +++ b/src/store/slices/loginInfo/loginInfo.types.ts @@ -17,8 +17,10 @@ export interface LoginInfoType { expires: number; } -export interface LoginInfoSliceType { - providerType: ProviderTypeEnum | null; +export interface LoginInfoSliceType< + T extends ProviderTypeEnum = ProviderTypeEnum +> { + providerType: T[keyof T] | null; walletConnectLogin: WalletConnectLoginType | null; ledgerLogin: LedgerLoginType | null; tokenLogin: TokenLoginType | null;