Skip to content

Commit

Permalink
Fix walletConnect persistance
Browse files Browse the repository at this point in the history
  • Loading branch information
mgavrila committed Dec 13, 2024
1 parent 296030a commit 070d6bf
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 26 deletions.
7 changes: 3 additions & 4 deletions src/core/providers-strategy/CrossWindowProviderStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,15 @@ export class CrossWindowProviderStrategy {
};

private buildProvider = () => {
const { address } = getAccount();

if (!this.provider) {
throw new Error(ProviderErrorsEnum.notInitialized);
}

const provider = this.provider as unknown as IProvider;

if (this.address) {
provider.setAccount({ address: this.address });
}

provider.setAccount({ address: this.address || address });
provider.signTransactions = this.signTransactions;
provider.signMessage = this.signMessage;

Expand Down
7 changes: 4 additions & 3 deletions src/core/providers-strategy/IFrameProviderStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IframeProvider } from '@multiversx/sdk-web-wallet-iframe-provider/out';
import { IframeLoginTypes } from '@multiversx/sdk-web-wallet-iframe-provider/out/constants';
import { getAccount } from 'core/methods/account/getAccount';
import { IProvider } from 'core/providers/types/providerFactory.types';
import { networkSelector } from 'store/selectors/networkSelectors';
import { getState } from 'store/store';
Expand Down Expand Up @@ -38,15 +39,15 @@ export class IFrameProviderStrategy {
};

private buildProvider = () => {
const { address } = getAccount();

if (!this.provider) {
throw new Error(ProviderErrorsEnum.notInitialized);
}

const provider = this.provider as unknown as IProvider;

if (this.address) {
provider.setAccount({ address: this.address });
}
provider.setAccount({ address: this.address || address });

return provider;
};
Expand Down
47 changes: 31 additions & 16 deletions src/core/providers-strategy/WalletConnectProviderStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from 'core/providers/types/providerFactory.types';
import { defineCustomElements, WalletConnectModal } from 'lib/sdkDappCoreUi';
import { logoutAction } from 'store/actions';
import { setWalletConnectConfig } from 'store/actions/config/configActions';
import { chainIdSelector, nativeAuthConfigSelector } from 'store/selectors';
import { getState } from 'store/store';
import { ProviderErrorsEnum } from 'types';
Expand Down Expand Up @@ -60,8 +61,12 @@ export class WalletConnectProviderStrategy {

const eventBus = await this.createEventBus();

const manager = WalletConnectStateManager.getInstance(eventBus);
this.manager = manager;
if (eventBus) {
const manager = WalletConnectStateManager.getInstance(eventBus);
this.manager = manager;

setWalletConnectConfig(this.config);
}

if (!this.provider) {
const { walletConnectProvider, dappMethods } =
Expand All @@ -74,21 +79,31 @@ export class WalletConnectProviderStrategy {
}

const onClose = () => {
manager.closeAndReset();
if (!this.manager) {
throw new Error('State manager is not initialized');
}

this.manager.closeAndReset();
};

this.unsubscribeEvents = () => {
if (!eventBus) {
throw new Error('Event bus is not initialized');
}

eventBus.unsubscribe(WalletConnectEventsEnum.CLOSE, onClose);
};

const { uri = '', approval } = await this.provider.connect({
methods: this.methods
});
if (eventBus && this.manager) {
const { uri = '', approval } = await this.provider.connect({
methods: this.methods
});

this.approval = approval;
manager.updateWcURI(uri);
this.approval = approval;
this.manager.updateWcURI(uri);

eventBus.subscribe(WalletConnectEventsEnum.CLOSE, onClose);
eventBus.subscribe(WalletConnectEventsEnum.CLOSE, onClose);
}

return this.buildProvider();
};
Expand Down Expand Up @@ -122,10 +137,10 @@ export class WalletConnectProviderStrategy {
await customElements.whenDefined('wallet-connect-modal');

eventBus = await walletConnectModalElement.getEventBus();
}

if (!eventBus) {
throw new Error('Event bus not provided for WalletConnect provider');
if (!eventBus) {
throw new Error('Event bus not provided for WalletConnect provider');
}
}

return eventBus;
Expand All @@ -150,11 +165,11 @@ export class WalletConnectProviderStrategy {
const handleOnLogin = () => {};

const handleOnLogout = async () => {
if (config.onLogout) {
await config.onLogout();
}

logoutAction();

if (config.logoutRoute) {
safeWindow.location.replace(config.logoutRoute);
}
};

const handleOnEvent = (_event: SessionEventTypes['event']) => {};
Expand Down
11 changes: 10 additions & 1 deletion src/core/providers/helpers/restoreProvider.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { getAddress } from 'core/methods/account/getAddress';
import { providerTypeSelector } from 'store/selectors';
import {
walletConnectConfigSelector,
providerTypeSelector
} from 'store/selectors';
import { getState } from 'store/store';
import { setAccountProvider } from '../accountProvider';
import { ProviderFactory } from '../ProviderFactory';
import { IProviderConfig } from '../types/providerFactory.types';

export async function restoreProvider() {
const type = providerTypeSelector(getState());
const walletConnectConfig = walletConnectConfigSelector(getState());

const address = getAddress();

if (!type) {
Expand All @@ -19,6 +24,10 @@ export async function restoreProvider() {
}
};

if (walletConnectConfig) {
config.walletConnect = walletConnectConfig;
}

const provider = await ProviderFactory.create({
type,
config
Expand Down
2 changes: 1 addition & 1 deletion src/core/providers/types/providerFactory.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface IProviderConfig {
walletConnectV2RelayAddress?: string;
walletConnectV2Options?: WalletConnectV2ProviderOptionsType;
customRequestMethods?: Array<string>;
onLogout?: () => Promise<void>;
logoutRoute?: string;
};
}

Expand Down
8 changes: 8 additions & 0 deletions src/store/actions/config/configActions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { IProviderConfig } from 'core/providers/types/providerFactory.types';
import { NativeAuthConfigType } from 'services/nativeAuth/nativeAuth.types';
import { getStore } from 'store/store';

export const setNativeAuthConfig = (config: NativeAuthConfigType) =>
getStore().setState(({ config: state }) => {
state.nativeAuthConfig = config;
});

export const setWalletConnectConfig = (
config: IProviderConfig['walletConnect']
) =>
getStore().setState(({ config: state }) => {
state.walletConnectConfig = config;
});
4 changes: 4 additions & 0 deletions src/store/selectors/configSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ export const configSelector = ({ config }: StoreType) => config;

export const nativeAuthConfigSelector = ({ config }: StoreType) =>
config.nativeAuthConfig;

export const walletConnectConfigSelector = ({ config }: StoreType) => {
return config.walletConnectConfig;
};
2 changes: 2 additions & 0 deletions src/store/slices/config/config.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { IProviderConfig } from 'core/providers/types/providerFactory.types';
import { NativeAuthConfigType } from 'services/nativeAuth/nativeAuth.types';

export interface ConfigSliceType {
nativeAuthConfig: NativeAuthConfigType | null;
walletConnectConfig: IProviderConfig['walletConnect'] | null;
}
3 changes: 2 additions & 1 deletion src/store/slices/config/configSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { ConfigSliceType } from './config.types';
// The config should be changed by using the `setNativeAuthConfig` action in some specific cases.
// Preferably, the config should be set at the dApp initialization and not changed during the dApp lifecycle. (e.g. when the user logs in/log out)
const initialState: ConfigSliceType = {
nativeAuthConfig: null
nativeAuthConfig: null,
walletConnectConfig: null
};

function getConfigSlice(): StateCreator<
Expand Down

0 comments on commit 070d6bf

Please sign in to comment.