Skip to content

Commit

Permalink
Restore provider (#19)
Browse files Browse the repository at this point in the history
* Restore provider
  • Loading branch information
arhtudormorar authored Sep 6, 2024
1 parent 69bb844 commit 333db0b
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/core/methods/initApp/initApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -43,4 +45,10 @@ export const initApp = async ({
customNetworkConfig: dAppConfig.network,
environment: dAppConfig.environment
});

const isLoggedIn = getIsLoggedIn();

if (isLoggedIn) {
await restoreProvider();
}
};
11 changes: 8 additions & 3 deletions src/core/providers/ProviderFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -72,12 +73,16 @@ export class ProviderFactory {
}

private async getCrossWindowProvider({
address,
walletAddress
}: Partial<IProviderConfig['network']>) {
// 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);
Expand Down
35 changes: 35 additions & 0 deletions src/core/providers/helpers/restoreProvider.ts
Original file line number Diff line number Diff line change
@@ -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);
};
4 changes: 3 additions & 1 deletion src/core/providers/types/providerFactory.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions src/store/actions/network/initializeNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const initializeNetwork = async ({
environment
}: InitializeNetworkPropsType) => {
const fetchConfigFromServer = !customNetworkConfig?.skipFetchFromServer;

const customNetworkApiAddress = customNetworkConfig?.apiAddress;

const isFoundEnv =
Expand Down Expand Up @@ -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 = {
Expand Down
3 changes: 3 additions & 0 deletions src/store/selectors/loginInfoSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ export const tokenLoginSelector = ({ loginInfo }: StoreType) =>

export const walletConnectLoginSelector = ({ loginInfo }: StoreType) =>
loginInfo.walletConnectLogin;

export const providerTypeSelector = ({ loginInfo }: StoreType) =>
loginInfo.providerType;
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 333db0b

Please sign in to comment.