diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b3c8d2..49b8669 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] + +- [Added restore provider after page reload](https://github.com/multiversx/mx-sdk-dapp-core/pull/19) - [Added signMessage](https://github.com/multiversx/mx-sdk-dapp-core/pull/18) ## [[0.0.0-alpha.9]](https://github.com/multiversx/mx-sdk-dapp-core)] - 2024-08-29 diff --git a/src/core/methods/initApp/initApp.ts b/src/core/methods/initApp/initApp.ts index c8a74ce..e3863db 100644 --- a/src/core/methods/initApp/initApp.ts +++ b/src/core/methods/initApp/initApp.ts @@ -5,6 +5,8 @@ import { initializeNetwork } from 'store/actions'; import { NativeAuthConfigType } from 'services/nativeAuth/nativeAuth.types'; import { getDefaultNativeAuthConfig } from 'services/nativeAuth/methods/getDefaultNativeAuthConfig'; import { InitAppType } from './initApp.types'; +import { getIsLoggedIn } from '../account/getIsLoggedIn'; +import { restoreProvider } from 'core/providers/helpers/restoreProvider'; const defaultInitAppProps = { storage: { @@ -43,4 +45,10 @@ export const initApp = async ({ customNetworkConfig: dAppConfig.network, environment: dAppConfig.environment }); + + const isLoggedIn = getIsLoggedIn(); + + if (isLoggedIn) { + await restoreProvider(); + } }; diff --git a/src/core/providers/ProviderFactory.ts b/src/core/providers/ProviderFactory.ts index 620b6ae..37cc079 100644 --- a/src/core/providers/ProviderFactory.ts +++ b/src/core/providers/ProviderFactory.ts @@ -48,7 +48,8 @@ export class ProviderFactory { const { walletAddress } = config.network; const provider = await this.getCrossWindowProvider({ - walletAddress + walletAddress, + address: config.account?.address || '' }); createdProvider = provider as unknown as IProvider; @@ -72,12 +73,16 @@ export class ProviderFactory { } private async getCrossWindowProvider({ + address, walletAddress - }: Partial) { - // CrossWindowProvider.getInstance().clearInstance(); + }: { + address: string; + walletAddress: string; + }) { const provider = CrossWindowProvider.getInstance(); await provider.init(); provider.setWalletUrl(String(walletAddress)); + provider.setAddress(address); if (isBrowserWithPopupConfirmation) { provider.setShouldShowConsentPopup(true); diff --git a/src/core/providers/helpers/restoreProvider.ts b/src/core/providers/helpers/restoreProvider.ts new file mode 100644 index 0000000..967eadc --- /dev/null +++ b/src/core/providers/helpers/restoreProvider.ts @@ -0,0 +1,35 @@ +import { networkSelector, providerTypeSelector } from 'store/selectors'; +import { ProviderFactory } from '../ProviderFactory'; +import { IProviderConfig } from '../types/providerFactory.types'; +import { getState } from 'store/store'; +import { setAccountProvider } from '../accountProvider'; +import { getAddress } from 'core/methods/account/getAddress'; + +export const restoreProvider = async () => { + const type = providerTypeSelector(getState()); + const address = getAddress(); + + if (!type) { + return; + } + + const config: IProviderConfig = { + network: networkSelector(getState()), + account: { + address + } + }; + + const factory = new ProviderFactory(); + + const provider = await factory.create({ + type, + config + }); + + if (!provider) { + throw new Error('Provider not found'); + } + + setAccountProvider(provider); +}; diff --git a/src/core/providers/types/providerFactory.types.ts b/src/core/providers/types/providerFactory.types.ts index ee6a022..f50f1d3 100644 --- a/src/core/providers/types/providerFactory.types.ts +++ b/src/core/providers/types/providerFactory.types.ts @@ -20,10 +20,12 @@ export interface IProvider extends IDAppProviderBase { } export interface IProviderConfig { - // TODO check if we have to pass the network object as argument here or it should be read from the state network: { walletAddress: string; }; + account?: { + address: string; + }; } export enum ProviderTypeEnum { diff --git a/src/store/actions/network/initializeNetwork.ts b/src/store/actions/network/initializeNetwork.ts index 117dba3..fc7e032 100644 --- a/src/store/actions/network/initializeNetwork.ts +++ b/src/store/actions/network/initializeNetwork.ts @@ -15,7 +15,6 @@ export const initializeNetwork = async ({ environment }: InitializeNetworkPropsType) => { const fetchConfigFromServer = !customNetworkConfig?.skipFetchFromServer; - const customNetworkApiAddress = customNetworkConfig?.apiAddress; const isFoundEnv = @@ -44,9 +43,9 @@ export const initializeNetwork = async ({ const fallbackApiAddress = fallbackConfig?.apiAddress; if (fetchConfigFromServer) { - const serverConfigAddress = customNetworkApiAddress || fallbackApiAddress; - - const serverConfig = await getServerConfiguration(serverConfigAddress); + const serverConfig = await getServerConfiguration( + customNetworkApiAddress || fallbackApiAddress + ); if (serverConfig != null) { const apiConfig: NetworkType = { diff --git a/src/store/selectors/loginInfoSelectors.ts b/src/store/selectors/loginInfoSelectors.ts index fc8519f..5527945 100644 --- a/src/store/selectors/loginInfoSelectors.ts +++ b/src/store/selectors/loginInfoSelectors.ts @@ -7,3 +7,6 @@ export const tokenLoginSelector = ({ loginInfo }: StoreType) => export const walletConnectLoginSelector = ({ loginInfo }: StoreType) => loginInfo.walletConnectLogin; + +export const providerTypeSelector = ({ loginInfo }: StoreType) => + loginInfo.providerType; diff --git a/yarn.lock b/yarn.lock index 3d5eac8..1f51752 100644 --- a/yarn.lock +++ b/yarn.lock @@ -924,9 +924,9 @@ uuid "^9.0.1" "@metamask/utils@^9.0.0": - version "9.2.1" - resolved "https://registry.yarnpkg.com/@metamask/utils/-/utils-9.2.1.tgz#d9f84706ff97e0c8d1bde5778549365b14269e81" - integrity sha512-/u663aUaB6+Xe75i3Mt/1cCljm41HDYIsna5oBrwGvgkY2zH7/9k9Zjd706cxoAbxN7QgLSVAReUiGnuxCuXrQ== + version "9.2.0" + resolved "https://registry.yarnpkg.com/@metamask/utils/-/utils-9.2.0.tgz#62c59a180fd7c3a08752009429ef2bdda8619a04" + integrity sha512-01riKC6oc6YJVJBL6g6TgCBwcmzwuVSIIMhnwvWanPWs3ZpUabJzy3GR9PlsBPa2pHd1p0Q6WMVRExWWEHzsQA== dependencies: "@ethereumjs/tx" "^4.2.0" "@metamask/superstruct" "^3.1.0"