Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/inconsistent state #589

Merged
merged 3 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/beacon-dapp/src/dapp-client/DAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export class DAppClient extends Client {
protected wcProjectId?: string
protected wcRelayUrl?: string

private isGetActiveAccountHandled: boolean = false
/**
* A map of requests that are currently "open", meaning we have sent them to a wallet and are still awaiting a response.
*/
Expand Down Expand Up @@ -577,12 +578,28 @@ export class DAppClient extends Client {
return this._activeAccount.promise
}

private async isInvalidState(account: AccountInfo) {
const activeAccount = await this._activeAccount.promise
return !activeAccount
? false
: activeAccount?.address !== account.address && !this.isGetActiveAccountHandled
}

/**
* Sets the active account
*
* @param account The account that will be set as the active account
*/
public async setActiveAccount(account?: AccountInfo): Promise<void> {
if (account && (await this.isInvalidState(account))) {
setTimeout(() => this.events.emit(BeaconEvent.HIDE_UI), 1000)
this.destroy()
this.setActiveAccount(undefined)
setTimeout(() => this.events.emit(BeaconEvent.INVALID_ACTIVE_ACCOUNT_STATE), 1000)

return
}

if (this._activeAccount.isSettled()) {
// If the promise has already been resolved we need to create a new one.
this._activeAccount = ExposedPromise.resolve<AccountInfo | undefined>(account)
Expand Down Expand Up @@ -742,6 +759,10 @@ export class DAppClient extends Client {
internalEvent: K,
eventCallback: BeaconEventHandlerFunction<BeaconEventType[K]>
): Promise<void> {
if (internalEvent === BeaconEvent.ACTIVE_ACCOUNT_SET) {
this.isGetActiveAccountHandled = true
}

await this.events.on(internalEvent, eventCallback)
}

Expand Down
16 changes: 15 additions & 1 deletion packages/beacon-dapp/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export enum BeaconEvent {

SHOW_PREPARE = 'SHOW_PREPARE',
HIDE_UI = 'HIDE_UI',

INVALID_ACTIVE_ACCOUNT_STATE = 'INVALID_ACTIVE_ACCOUNT_STATE',
PAIR_INIT = 'PAIR_INIT',
PAIR_SUCCESS = 'PAIR_SUCCESS',
CHANNEL_CLOSED = 'CHANNEL_CLOSED',
Expand Down Expand Up @@ -164,6 +164,7 @@ export interface BeaconEventType {
[BeaconEvent.NO_PERMISSIONS]: undefined
[BeaconEvent.ACTIVE_ACCOUNT_SET]: AccountInfo
[BeaconEvent.ACTIVE_TRANSPORT_SET]: Transport
[BeaconEvent.INVALID_ACTIVE_ACCOUNT_STATE]: undefined
[BeaconEvent.SHOW_PREPARE]: { walletInfo?: WalletInfo }
[BeaconEvent.HIDE_UI]: ('alert' | 'toast')[] | undefined
[BeaconEvent.PAIR_INIT]: {
Expand Down Expand Up @@ -287,6 +288,17 @@ const showNoPermissionAlert = async (): Promise<void> => {
})
}

/**
* Show a
*/
const showInvalidActiveAccountState = async (): Promise<void> => {
await openAlert({
title: 'Invalid state',
body: `A new active account has been received but no handler found
(INVALID STATE: no handler found for BeaconEvent.ACTIVE_ACCOUNT_SET)`
})
}

/**
* Show an error toast
*
Expand Down Expand Up @@ -645,6 +657,7 @@ export const defaultEventCallbacks: {
[BeaconEvent.NO_PERMISSIONS]: showNoPermissionAlert,
[BeaconEvent.ACTIVE_ACCOUNT_SET]: emptyHandler(),
[BeaconEvent.ACTIVE_TRANSPORT_SET]: emptyHandler(),
[BeaconEvent.INVALID_ACTIVE_ACCOUNT_STATE]: showInvalidActiveAccountState,
[BeaconEvent.SHOW_PREPARE]: showPrepare,
[BeaconEvent.HIDE_UI]: hideUI,
[BeaconEvent.PAIR_INIT]: showPairAlert,
Expand Down Expand Up @@ -684,6 +697,7 @@ export class BeaconEventHandler {
[BeaconEvent.NO_PERMISSIONS]: [defaultEventCallbacks.NO_PERMISSIONS],
[BeaconEvent.ACTIVE_ACCOUNT_SET]: [defaultEventCallbacks.ACTIVE_ACCOUNT_SET],
[BeaconEvent.ACTIVE_TRANSPORT_SET]: [defaultEventCallbacks.ACTIVE_TRANSPORT_SET],
[BeaconEvent.INVALID_ACTIVE_ACCOUNT_STATE]: [defaultEventCallbacks.INVALID_ACTIVE_ACCOUNT_STATE],
[BeaconEvent.SHOW_PREPARE]: [defaultEventCallbacks.SHOW_PREPARE],
[BeaconEvent.HIDE_UI]: [defaultEventCallbacks.HIDE_UI],
[BeaconEvent.PAIR_INIT]: [defaultEventCallbacks.PAIR_INIT],
Expand Down
Loading