Skip to content

Commit

Permalink
fix: walletInfo from localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
isordo committed Oct 19, 2023
1 parent dbb7919 commit 950177e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
31 changes: 22 additions & 9 deletions packages/beacon-dapp/src/dapp-client/DAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export class DAppClient extends Client {
)
}

private async wcToastHandler(isWaiting: boolean) {
private async wcToastHandler() {
const walletInfo = await (async (): Promise<WalletInfo> => {
try {
return await this.getWalletInfo()
Expand All @@ -435,9 +435,13 @@ export class DAppClient extends Client {
}
})()

await (isWaiting
? this.events.emit(BeaconEvent.WC_ACKNOWLEDGE_PENDING, { walletInfo })
: this.events.emit(BeaconEvent.WC_ACKNOWLEDGE_RECEIVED, { walletInfo }))
console.log('walletInfo: ', walletInfo)

if (walletInfo.name === '' || walletInfo.name === 'wallet') {
setTimeout(async () => await this.events.emit(BeaconEvent.HIDE_UI, ['alert']))
}

await this.events.emit(BeaconEvent.WC_ACKNOWLEDGE_PENDING, { walletInfo })
}

private async channelClosedHandler() {
Expand Down Expand Up @@ -615,10 +619,10 @@ export class DAppClient extends Client {
*/
public async setActiveAccount(account?: AccountInfo): Promise<void> {
if (account && this._activeAccount.isSettled() && (await this.isInvalidState(account))) {
setTimeout(() => this.events.emit(BeaconEvent.HIDE_UI), 1000)
setTimeout(() => this.events.emit(BeaconEvent.HIDE_UI))
this.destroy()
this.setActiveAccount(undefined)
setTimeout(() => this.events.emit(BeaconEvent.INVALID_ACTIVE_ACCOUNT_STATE), 1000)
setTimeout(() => this.events.emit(BeaconEvent.INVALID_ACTIVE_ACCOUNT_STATE))

return
}
Expand Down Expand Up @@ -716,7 +720,7 @@ export class DAppClient extends Client {
this._initPromise = undefined
// by dispatching two opposite events (one closes the alert the other one opens it)
// it triggers some sort of race condition in the UI render cycle
setTimeout(async () => await this.events.emit(BeaconEvent.NO_PERMISSIONS), 1000)
setTimeout(async () => await this.events.emit(BeaconEvent.NO_PERMISSIONS))
}
}

Expand Down Expand Up @@ -1487,6 +1491,10 @@ export class DAppClient extends Client {
.catch((emitError) => console.warn(emitError))
}

private async getWalletInfoFromStorage() {
return (await this.storage.get(StorageKey.LAST_SELECTED_WALLET))?.split('_')[0]
}

private async getWalletInfo(peer?: PeerInfo, account?: AccountInfo): Promise<WalletInfo> {
const selectedAccount = account ? account : await this.getActiveAccount()

Expand All @@ -1499,7 +1507,7 @@ export class DAppClient extends Client {

if (!walletInfo) {
walletInfo = {
name: selectedPeer?.name ?? '',
name: selectedPeer?.name ?? (await this.getWalletInfoFromStorage()) ?? '',
icon: selectedPeer?.icon
}
}
Expand All @@ -1512,14 +1520,19 @@ export class DAppClient extends Client {
return false
}

const getOrgName = (name: string) => name.split(/[_\s]+/)[0]

let selectedApp: AppBase | undefined
let type: 'extension' | 'mobile' | 'web' | 'desktop' | undefined
const apps: AppBase[] = [
...getiOSList(),
...getWebList(),
...getDesktopList(),
...getExtensionList()
].filter((app: AppBase) => lowerCaseCompare(app.name, walletInfo?.name))
].filter((app: AppBase) =>
lowerCaseCompare(getOrgName(app.key), getOrgName(walletInfo?.name ?? 'wallet'))
)

// TODO: Remove once all wallets send the icon?
const mobile = (apps as App[]).find((app) => app.universalLink)
const browser = (apps as WebApp[]).find((app) => app.links)
Expand Down
14 changes: 0 additions & 14 deletions packages/beacon-dapp/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export enum BeaconEvent {
BROADCAST_REQUEST_SUCCESS = 'BROADCAST_REQUEST_SUCCESS',
BROADCAST_REQUEST_ERROR = 'BROADCAST_REQUEST_ERROR',
WC_ACKNOWLEDGE_PENDING = 'WC_ACKNOWLEDGE_PENDING',
WC_ACKNOWLEDGE_RECEIVED = 'WC_ACKNOWLEDGE_RECEIVED',
ACKNOWLEDGE_RECEIVED = 'ACKNOWLEDGE_RECEIVED',

LOCAL_RATE_LIMIT_REACHED = 'LOCAL_RATE_LIMIT_REACHED',
Expand Down Expand Up @@ -159,9 +158,6 @@ export interface BeaconEventType {
[BeaconEvent.WC_ACKNOWLEDGE_PENDING]: {
walletInfo: WalletInfo
}
[BeaconEvent.WC_ACKNOWLEDGE_RECEIVED]: {
walletInfo: WalletInfo
}
[BeaconEvent.ACKNOWLEDGE_RECEIVED]: {
message: AcknowledgeResponse
extraInfo: ExtraInfo
Expand Down Expand Up @@ -629,14 +625,6 @@ const showWCPendingAck = async (data: { walletInfo: WalletInfo }): Promise<void>
}).catch((toastError) => console.error(toastError))
}

const showWCReceivedAck = async (data: { walletInfo: WalletInfo }): Promise<void> => {
openToast({
body: 'Acknowledgment received from\u00A0 {{wallet}}',
state: 'acknowledge',
walletInfo: data.walletInfo
}).catch((toastError) => console.error(toastError))
}

const emptyHandler = (): BeaconEventHandlerFunction => async (): Promise<void> => {
//
}
Expand Down Expand Up @@ -669,7 +657,6 @@ export const defaultEventCallbacks: {
[BeaconEvent.BROADCAST_REQUEST_SUCCESS]: showBroadcastSuccessAlert,
[BeaconEvent.BROADCAST_REQUEST_ERROR]: showErrorToast,
[BeaconEvent.WC_ACKNOWLEDGE_PENDING]: showWCPendingAck,
[BeaconEvent.WC_ACKNOWLEDGE_RECEIVED]: showWCReceivedAck,
[BeaconEvent.ACKNOWLEDGE_RECEIVED]: showAcknowledgedToast,
[BeaconEvent.LOCAL_RATE_LIMIT_REACHED]: showRateLimitReached,
[BeaconEvent.NO_PERMISSIONS]: showNoPermissionAlert,
Expand Down Expand Up @@ -704,7 +691,6 @@ export class BeaconEventHandler {
[BeaconEvent.SIGN_REQUEST_SUCCESS]: [defaultEventCallbacks.SIGN_REQUEST_SUCCESS],
[BeaconEvent.SIGN_REQUEST_ERROR]: [defaultEventCallbacks.SIGN_REQUEST_ERROR],
[BeaconEvent.WC_ACKNOWLEDGE_PENDING]: [defaultEventCallbacks.WC_ACKNOWLEDGE_PENDING],
[BeaconEvent.WC_ACKNOWLEDGE_RECEIVED]: [defaultEventCallbacks.WC_ACKNOWLEDGE_RECEIVED],
// TODO: ENCRYPTION
// [BeaconEvent.ENCRYPT_REQUEST_SENT]: [defaultEventCallbacks.ENCRYPT_REQUEST_SENT],
// [BeaconEvent.ENCRYPT_REQUEST_SUCCESS]: [defaultEventCallbacks.ENCRYPT_REQUEST_SUCCESS],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,15 @@ export class WalletConnectCommunicationClient extends CommunicationClient {

this.setDefaultAccountAndNetwork()

const session = this.getSession()
let session = this.getSession()
let publicKey: string | undefined

if (!session.sessionProperties) {
const fun = this.eventHandlers.get(ClientEvents.WC_ACK_NOTIFICATION)
fun && fun(true)
fun && fun()
this.requestAccountNamespacePromise = new ExposedPromise()
await this.requestAccountNamespacePromise?.promise
session = this.getSession()
}

if (
Expand Down Expand Up @@ -393,13 +394,14 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
// therefore we must immediately open a session
// to get data required in the pairing response
try {
const session = await this.openSession(topic)
let session = await this.openSession(topic)

if (!session.sessionProperties) {
const fun = this.eventHandlers.get(ClientEvents.WC_ACK_NOTIFICATION)
fun && fun(true)
fun && fun()
this.requestAccountNamespacePromise = new ExposedPromise()
await this.requestAccountNamespacePromise?.promise
session = this.getSession()
}

const pairingResponse: ExtendedWalletConnectPairingResponse =
Expand Down Expand Up @@ -444,8 +446,6 @@ export class WalletConnectCommunicationClient extends CommunicationClient {
this.session = signClient.session.get(event.topic)

if (this.requestAccountNamespacePromise) {
const fun = this.eventHandlers.get(ClientEvents.WC_ACK_NOTIFICATION)
fun && fun(false)
this.requestAccountNamespacePromise?.resolve(true)
this.requestAccountNamespacePromise = undefined
} else {
Expand Down

0 comments on commit 950177e

Please sign in to comment.