diff --git a/package-lock.json b/package-lock.json index 63f4c5321..856416b04 100755 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@airgap/beacon-sdk", - "version": "2.3.3", + "version": "2.3.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@airgap/beacon-sdk", - "version": "2.3.1", + "version": "2.3.4", "license": "MIT", "dependencies": { "@types/chrome": "0.0.115", diff --git a/package.json b/package.json index 9a0cb0604..411f95ccb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@airgap/beacon-sdk", - "version": "2.3.3", + "version": "2.3.4", "description": "The beacon-sdk allows you to easily connect DApps with Wallets through P2P communication or a chrome extension.", "main": "dist/cjs/index.js", "module": "dist/esm/index.js", diff --git a/src/clients/dapp-client/DAppClient.ts b/src/clients/dapp-client/DAppClient.ts index 9ade917e0..f755d5372 100644 --- a/src/clients/dapp-client/DAppClient.ts +++ b/src/clients/dapp-client/DAppClient.ts @@ -78,6 +78,7 @@ import { desktopList, extensionList, iOSList, webList } from '../../ui/alert/wal import { Optional } from '../../utils/utils' import { DAppClientOptions } from './DAppClientOptions' import { App, DesktopApp, ExtensionApp, WebApp } from '../../ui/alert/Pairing' +import { closeToast } from '../../ui/toast/Toast' const logger = new Logger('DAppClient') @@ -428,8 +429,8 @@ export class DAppClient extends Client { await this.events.emit(BeaconEvent.SHOW_PREPARE, { walletInfo }) } - public async hideUI(): Promise { - await this.events.emit(BeaconEvent.HIDE_UI) + public async hideUI(elements?: ('alert' | 'toast')[]): Promise { + await this.events.emit(BeaconEvent.HIDE_UI, elements) } /** @@ -858,7 +859,7 @@ export class DAppClient extends Client { * @param errorMessage The error message to send. */ private async sendInternalError(errorMessage: string): Promise { - await this.events.emit(BeaconEvent.INTERNAL_ERROR, errorMessage) + await this.events.emit(BeaconEvent.INTERNAL_ERROR, { text: errorMessage }) throw new Error(errorMessage) } @@ -1158,7 +1159,25 @@ export class DAppClient extends Client { logger.log('makeRequest', 'sending message', request) console.timeLog(messageId, 'sending') - await (await this.transport).send(payload, peer) + try { + await (await this.transport).send(payload, peer) + } catch (sendError) { + this.events.emit(BeaconEvent.INTERNAL_ERROR, { + text: + 'Unable to send message. If this problem persists, please reset the connection and pair your wallet again.', + buttons: [ + { + text: 'Reset Connection', + actionCallback: async (): Promise => { + await closeToast() + this.disconnect() + } + } + ] + }) + console.timeLog(messageId, 'send error') + throw sendError + } console.timeLog(messageId, 'sent') this.events diff --git a/src/constants.ts b/src/constants.ts index 6484ae7cf..3e2708d5f 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,2 +1,2 @@ -export const SDK_VERSION: string = '2.3.3' +export const SDK_VERSION: string = '2.3.4' export const BEACON_VERSION: string = '2' diff --git a/src/events.ts b/src/events.ts index 14aa0aae6..7511967b8 100644 --- a/src/events.ts +++ b/src/events.ts @@ -147,7 +147,7 @@ export interface BeaconEventType { [BeaconEvent.ACTIVE_ACCOUNT_SET]: AccountInfo [BeaconEvent.ACTIVE_TRANSPORT_SET]: Transport [BeaconEvent.SHOW_PREPARE]: { walletInfo?: WalletInfo } - [BeaconEvent.HIDE_UI]: undefined + [BeaconEvent.HIDE_UI]: ('alert' | 'toast')[] | undefined [BeaconEvent.PAIR_INIT]: { p2pPeerInfo: () => Promise postmessagePeerInfo: () => Promise @@ -157,7 +157,7 @@ export interface BeaconEventType { } [BeaconEvent.PAIR_SUCCESS]: ExtendedPostMessagePairingResponse | ExtendedP2PPairingResponse [BeaconEvent.CHANNEL_CLOSED]: string - [BeaconEvent.INTERNAL_ERROR]: string + [BeaconEvent.INTERNAL_ERROR]: { text: string; buttons?: AlertButton[] } [BeaconEvent.UNKNOWN]: undefined } @@ -237,9 +237,17 @@ const showPrepare = async (data: { walletInfo?: WalletInfo }): Promise => }).catch((toastError) => console.error(toastError)) } -const hideUI = async (): Promise => { - closeToast() - closeAlerts() +const hideUI = async (elements?: ('alert' | 'toast')[]): Promise => { + if (elements) { + if (elements.includes('alert')) { + closeAlerts() + } + if (elements.includes('toast')) { + closeToast() + } + } else { + closeToast() + } } /** @@ -339,10 +347,14 @@ const showChannelClosedAlert = async (): Promise => { const showInternalErrorAlert = async ( data: BeaconEventType[BeaconEvent.INTERNAL_ERROR] ): Promise => { + const buttons: AlertButton[] = [...(data.buttons ?? [])] + + buttons.push({ text: 'Done', style: 'outline' }) + const alertConfig: AlertConfig = { title: 'Internal Error', - body: `${data}`, - buttons: [{ text: 'Done', style: 'outline' }] + body: data.text, + buttons } await openAlert(alertConfig) } diff --git a/test/clients/dapp-client.spec.ts b/test/clients/dapp-client.spec.ts index 372cc48d2..df1c3305d 100644 --- a/test/clients/dapp-client.spec.ts +++ b/test/clients/dapp-client.spec.ts @@ -485,7 +485,7 @@ describe(`DAppClient`, () => { expect(eventsStub.firstCall.args[0]).to.equal(BeaconEvent.ACTIVE_TRANSPORT_SET) // Called in the constructor expect(eventsStub.firstCall.args[1]).to.equal(undefined) expect(eventsStub.secondCall.args[0]).to.equal(BeaconEvent.INTERNAL_ERROR) - expect(eventsStub.secondCall.args[1]).to.equal('No active account set!') + expect(eventsStub.secondCall.args[1]).to.deep.equal({ text: 'No active account set!' }) expect(eventsStub.thirdCall.args[0]).to.equal(BeaconEvent.ACTIVE_ACCOUNT_SET) // Called in the constructor expect(eventsStub.thirdCall.args[1]).to.equal(undefined) expect(eventsStub.callCount).to.equal(3) @@ -861,7 +861,7 @@ describe(`DAppClient`, () => { } catch (e) { expect(eventsStub.callCount).to.equal(2) expect(eventsStub.firstCall.args[0]).to.equal(BeaconEvent.INTERNAL_ERROR) - expect(eventsStub.firstCall.args[1]).to.equal('some-message') + expect(eventsStub.firstCall.args[1]).to.deep.equal({ text: 'some-message' }) expect(eventsStub.secondCall.args[0]).to.equal(BeaconEvent.ACTIVE_TRANSPORT_SET) expect(eventsStub.secondCall.args[1]).to.equal(undefined) expect(e.message).to.equal('some-message')