UNPKG

@airgap/beacon-dapp

Version:

Use this package on your dApp to instanciate a DAppClient object and communicate to wallets.

719 lines • 32.1 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BeaconEventHandler = exports.defaultEventCallbacks = exports.BeaconEvent = void 0; const beacon_dapp_1 = require("@airgap/beacon-dapp"); const beacon_types_1 = require("@airgap/beacon-types"); const beacon_core_1 = require("@airgap/beacon-core"); const shorten_string_1 = require("./utils/shorten-string"); const beacon_ui_1 = require("@airgap/beacon-ui"); const logger = new beacon_core_1.Logger('BeaconEvents'); const SUCCESS_TIMER = 5 * 1000; /** * The different events that can be emitted by the beacon-sdk */ var BeaconEvent; (function (BeaconEvent) { BeaconEvent["PERMISSION_REQUEST_SENT"] = "PERMISSION_REQUEST_SENT"; BeaconEvent["PERMISSION_REQUEST_SUCCESS"] = "PERMISSION_REQUEST_SUCCESS"; BeaconEvent["PERMISSION_REQUEST_ERROR"] = "PERMISSION_REQUEST_ERROR"; BeaconEvent["PROOF_OF_EVENT_CHALLENGE_REQUEST_SENT"] = "PROOF_OF_EVENT_CHALLENGE_REQUEST_SENT"; BeaconEvent["PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS"] = "PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS"; BeaconEvent["PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR"] = "PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR"; BeaconEvent["SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SENT"] = "SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SENT"; BeaconEvent["SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS"] = "SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS"; BeaconEvent["SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR"] = "SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR"; BeaconEvent["OPERATION_REQUEST_SENT"] = "OPERATION_REQUEST_SENT"; BeaconEvent["OPERATION_REQUEST_SUCCESS"] = "OPERATION_REQUEST_SUCCESS"; BeaconEvent["OPERATION_REQUEST_ERROR"] = "OPERATION_REQUEST_ERROR"; BeaconEvent["SIGN_REQUEST_SENT"] = "SIGN_REQUEST_SENT"; BeaconEvent["SIGN_REQUEST_SUCCESS"] = "SIGN_REQUEST_SUCCESS"; BeaconEvent["SIGN_REQUEST_ERROR"] = "SIGN_REQUEST_ERROR"; BeaconEvent["BLOCKCHAIN_REQUEST_SUCCESS"] = "BLOCKCHAIN_REQUEST_SUCCESS"; // TODO: ENCRYPTION // ENCRYPT_REQUEST_SENT = 'ENCRYPT_REQUEST_SENT', // ENCRYPT_REQUEST_SUCCESS = 'ENCRYPT_REQUEST_SUCCESS', // ENCRYPT_REQUEST_ERROR = 'ENCRYPT_REQUEST_ERROR', BeaconEvent["BROADCAST_REQUEST_SENT"] = "BROADCAST_REQUEST_SENT"; BeaconEvent["BROADCAST_REQUEST_SUCCESS"] = "BROADCAST_REQUEST_SUCCESS"; BeaconEvent["BROADCAST_REQUEST_ERROR"] = "BROADCAST_REQUEST_ERROR"; BeaconEvent["ACKNOWLEDGE_RECEIVED"] = "ACKNOWLEDGE_RECEIVED"; BeaconEvent["LOCAL_RATE_LIMIT_REACHED"] = "LOCAL_RATE_LIMIT_REACHED"; BeaconEvent["NO_PERMISSIONS"] = "NO_PERMISSIONS"; BeaconEvent["ACTIVE_ACCOUNT_SET"] = "ACTIVE_ACCOUNT_SET"; BeaconEvent["ACTIVE_TRANSPORT_SET"] = "ACTIVE_TRANSPORT_SET"; BeaconEvent["SHOW_PREPARE"] = "SHOW_PREPARE"; BeaconEvent["HIDE_UI"] = "HIDE_UI"; BeaconEvent["INVALID_ACTIVE_ACCOUNT_STATE"] = "INVALID_ACTIVE_ACCOUNT_STATE"; BeaconEvent["INVALID_ACCOUNT_DEACTIVATED"] = "INVALID_ACCOUNT_DEACTIVATED"; BeaconEvent["PAIR_INIT"] = "PAIR_INIT"; BeaconEvent["PAIR_SUCCESS"] = "PAIR_SUCCESS"; BeaconEvent["CHANNEL_CLOSED"] = "CHANNEL_CLOSED"; BeaconEvent["GENERIC_ERROR"] = "GENERIC_ERROR"; BeaconEvent["OPEN_BUG_REPORT"] = "OPEN_BUG_REPORT"; BeaconEvent["RELAYER_ERROR"] = "RELAYER_ERROR"; BeaconEvent["INTERNAL_ERROR"] = "INTERNAL_ERROR"; BeaconEvent["UNKNOWN"] = "UNKNOWN"; })(BeaconEvent || (exports.BeaconEvent = BeaconEvent = {})); /** * Show a "Request sent" toast */ const showSentToast = (data) => __awaiter(void 0, void 0, void 0, function* () { let openWalletAction; const actions = []; if (data.walletInfo.deeplink) { if (data.walletInfo.type === 'web' || (data.walletInfo.type === 'mobile' && (0, beacon_ui_1.isMobile)(window)) || (data.walletInfo.type === 'desktop' && !(0, beacon_ui_1.isMobile)(window))) { const link = data.walletInfo.deeplink; openWalletAction = () => __awaiter(void 0, void 0, void 0, function* () { const a = document.createElement('a'); a.setAttribute('rel', 'noopener'); a.setAttribute('href', link); a.setAttribute('target', '_blank'); a.dispatchEvent(new MouseEvent('click', { view: window, bubbles: true, cancelable: true })); }); } } actions.push({ text: `No answer from your wallet received yet. Please make sure the wallet is open.`, isBold: true }); actions.push({ text: 'Wallet not receiving request?', actionText: 'Reset Connection', actionCallback: () => __awaiter(void 0, void 0, void 0, function* () { (0, beacon_dapp_1.closeToast)(); // eslint-disable-next-line @typescript-eslint/unbound-method const resetCallback = data.extraInfo.resetCallback; if (resetCallback) { logger.log('showSentToast', 'resetCallback invoked'); yield resetCallback(); } }) }); actions.push({ text: 'Do you wish to report a bug?', actionText: 'Open', actionCallback: () => __awaiter(void 0, void 0, void 0, function* () { (0, beacon_dapp_1.closeToast)(); (0, beacon_dapp_1.openBugReport)(); }) }); (0, beacon_dapp_1.openToast)({ body: `Request sent to\u00A0 {{wallet}}`, walletInfo: data.walletInfo, state: 'loading', timer: (0, beacon_ui_1.isMobileOS)(window) ? SUCCESS_TIMER : 0, actions, openWalletAction }); }); const showAcknowledgedToast = (data) => __awaiter(void 0, void 0, void 0, function* () { (0, beacon_dapp_1.openToast)({ body: 'Awaiting confirmation in\u00A0 {{wallet}}', state: 'acknowledge', walletInfo: data.walletInfo }); }); const showPrepare = (data) => __awaiter(void 0, void 0, void 0, function* () { const text = data.walletInfo ? `Preparing Request for\u00A0 {{wallet}}...` : 'Preparing Request...'; (0, beacon_dapp_1.openToast)({ body: text, state: 'prepare', walletInfo: data.walletInfo }); }); const hideUI = (elements) => { if (elements === null || elements === void 0 ? void 0 : elements.includes('alert')) { (0, beacon_dapp_1.closeAlert)(); } if ((elements === null || elements === void 0 ? void 0 : elements.includes('toast')) || !elements) { (0, beacon_dapp_1.closeToast)(); } }; /** * Show a "No Permission" alert */ const showNoPermissionAlert = () => __awaiter(void 0, void 0, void 0, function* () { (0, beacon_dapp_1.openAlert)({ title: 'No Permission', body: 'Please allow the wallet to handle this type of request.' }); }); /** * Show an "Invalid state" alert */ const showInvalidActiveAccountState = () => __awaiter(void 0, void 0, void 0, function* () { (0, beacon_dapp_1.openAlert)({ title: 'Invalid state', body: `An active account has been received, but no active subscription was found for BeaconEvent.ACTIVE_ACCOUNT_SET. For more information, visit: https://docs.walletbeacon.io/guides/migration-guide` }); }); /** * Show an "account deactivated" error alert */ const showInvalidAccountDeactivated = () => __awaiter(void 0, void 0, void 0, function* () { yield (0, beacon_dapp_1.openAlert)({ title: 'Error', body: `Your session has expired. Please pair with your wallet again.` }); }); /** * Show an "Invalid state" alert */ const showGenericErrorAlert = (errorMessage) => __awaiter(void 0, void 0, void 0, function* () { (0, beacon_dapp_1.openAlert)({ title: `${errorMessage}`, body: 'Please try again. If this problem persists please send us a bug report.', buttons: [ { label: 'Send Report', type: 'primary', onClick: () => (0, beacon_dapp_1.openBugReport)() } ] }); }); /** * Show an error toast * * @param beaconError The beacon error */ const showErrorToast = (response, buttons) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f; const error = response.errorResponse.errorType ? (0, beacon_core_1.getError)(response.errorResponse.errorType, response.errorResponse.errorData) : new beacon_core_1.UnknownBeaconError(); const actions = [ { text: error.title, isBold: true } ]; if (response.errorResponse.errorType === beacon_types_1.BeaconErrorType.TRANSACTION_INVALID_ERROR && response.errorResponse.errorData) { const err = response.errorResponse.errorData; const errorMessages = response.errorMessages; let hasHumandReadableError = false; if (((_a = err[0]) === null || _a === void 0 ? void 0 : _a.contract_handle) && errorMessages && (errorMessages === null || errorMessages === void 0 ? void 0 : errorMessages[err[0].contract_handle])) { const errCode = (_d = (_c = (_b = err[1]) === null || _b === void 0 ? void 0 : _b.with) === null || _c === void 0 ? void 0 : _c.int) !== null && _d !== void 0 ? _d : (_f = (_e = err[1]) === null || _e === void 0 ? void 0 : _e.with) === null || _f === void 0 ? void 0 : _f.string; const contractErrors = errorMessages === null || errorMessages === void 0 ? void 0 : errorMessages[err[0].contract_handle]; if (errCode && (contractErrors === null || contractErrors === void 0 ? void 0 : contractErrors[errCode])) { actions.push({ text: contractErrors === null || contractErrors === void 0 ? void 0 : contractErrors[errCode], isBold: true }); hasHumandReadableError = true; } } if (!hasHumandReadableError) { actions.push({ text: error.description }); } actions.push({ text: '', actionText: 'Show Details', actionCallback: () => __awaiter(void 0, void 0, void 0, function* () { (0, beacon_dapp_1.closeToast)(); (0, beacon_dapp_1.openAlert)({ title: error.title, // eslint-disable-next-line @typescript-eslint/unbound-method body: error.fullDescription.description, data: error.fullDescription.data, buttons }); }) }); } (0, beacon_dapp_1.openToast)({ body: `{{wallet}}\u00A0 has returned an error`, timer: response.errorResponse.errorType === beacon_types_1.BeaconErrorType.ABORTED_ERROR ? SUCCESS_TIMER : undefined, state: 'finished', walletInfo: response.walletInfo, actions }); }); /** * Show a rate limit reached toast */ const showRateLimitReached = () => __awaiter(void 0, void 0, void 0, function* () { (0, beacon_dapp_1.openAlert)({ title: 'Error', body: 'Rate limit reached. Please slow down', buttons: [{ text: 'Done', style: 'outline' }], timer: 3000 }); }); /** * Show a "connection successful" alert for 1.5 seconds */ const showExtensionConnectedAlert = () => { (0, beacon_dapp_1.closeAlert)(); }; const showBugReportForm = () => __awaiter(void 0, void 0, void 0, function* () { yield (0, beacon_dapp_1.openBugReport)(); }); const showRelayerErrorAlert = () => __awaiter(void 0, void 0, void 0, function* () { (0, beacon_dapp_1.openAlert)({ title: 'Error', body: `Failed to connect to the relayer. Please check that your system time is synchronized with the current time and reload the page.`, buttons: [{ text: 'Done', style: 'outline' }], timer: 1500 }); }); /** * Show a "channel closed" alert for 1.5 seconds */ const showChannelClosedAlert = () => __awaiter(void 0, void 0, void 0, function* () { // openAlert({ // title: 'Channel closed', // body: `Your peer has closed the connection.`, // buttons: [{ text: 'Done', style: 'outline' }], // timer: 1500 // }) }); const showInternalErrorAlert = (data) => __awaiter(void 0, void 0, void 0, function* () { var _a; const buttons = [...((_a = data.buttons) !== null && _a !== void 0 ? _a : [])]; buttons.push({ text: 'Done', style: 'outline' }); const alertConfig = { title: 'Internal Error', body: data.text, buttons }; (0, beacon_dapp_1.openAlert)(alertConfig); }); /** * Show a connect alert with QR code * * @param data The data that is emitted by the PAIR_INIT event */ const showPairAlert = (data) => __awaiter(void 0, void 0, void 0, function* () { logger.log('showPairAlert'); const alertConfig = { title: 'Choose your preferred wallet', body: `<p></p>`, pairingPayload: { p2pSyncCode: data.p2pPeerInfo, walletConnectSyncCode: data.walletConnectPeerInfo, postmessageSyncCode: data.postmessagePeerInfo, networkType: data.networkType }, // eslint-disable-next-line @typescript-eslint/unbound-method closeButtonCallback: data.abortedHandler, disclaimerText: data.disclaimerText, analytics: data.analytics, featuredWallets: data.featuredWallets, substratePairing: data.substratePairing }; (0, beacon_dapp_1.openAlert)(alertConfig); }); /** * Show a "Permission Granted" alert * * @param data The data that is emitted by the PERMISSION_REQUEST_SUCCESS event */ const showPermissionSuccessAlert = (data) => __awaiter(void 0, void 0, void 0, function* () { const { output } = data; const actions = [ { text: 'Address', actionText: (0, shorten_string_1.shortenString)(output.address), isBold: true } ]; if (output.network.type !== 'substrate') { actions.push({ text: 'Network', actionText: `${output.network.type}` }); actions.push({ text: 'Permissions', actionText: output.scopes.join(', ') }); } (0, beacon_dapp_1.openToast)({ body: `{{wallet}}\u00A0 has granted permission`, timer: SUCCESS_TIMER, walletInfo: data.walletInfo, state: 'finished', actions }); }); const showProofOfEventChallengeSuccessAlert = (data) => __awaiter(void 0, void 0, void 0, function* () { const { output } = data; (0, beacon_dapp_1.openToast)({ body: `{{wallet}}\u00A0 has ${output.isAccepted ? 'accepted' : 'refused'} the challenge`, timer: SUCCESS_TIMER, walletInfo: data.walletInfo, state: 'finished', actions: output.isAccepted ? [ { text: `Payload hash: ${output.payloadHash}`, actionText: 'Copy to clipboard', actionCallback: () => __awaiter(void 0, void 0, void 0, function* () { navigator.clipboard.writeText(output.payloadHash).then(() => { logger.log('showSignSuccessAlert', 'Copying to clipboard was successful!'); }, (err) => { logger.error('showSignSuccessAlert', 'Could not copy text to clipboard: ', err); }); (0, beacon_dapp_1.closeToast)(); }) } ] : [] }); }); const showSimulatedProofOfEventChallengeSuccessAlert = (data) => __awaiter(void 0, void 0, void 0, function* () { const { output } = data; (0, beacon_dapp_1.openToast)({ body: !output.errorMessage ? `{{wallet}}\u00A0 has returned the list of operation` : `{{wallet}}\u00A0 has returned an error`, timer: SUCCESS_TIMER, walletInfo: data.walletInfo, state: 'finished', actions: !output.errorMessage ? [ { text: 'Operation list', actionText: 'Copy to clipboard', actionCallback: () => __awaiter(void 0, void 0, void 0, function* () { navigator.clipboard.writeText(output.operationsList).then(() => { logger.log('showSignSuccessAlert', 'Copying to clipboard was successful!'); }, (err) => { logger.error('showSignSuccessAlert', 'Could not copy text to clipboard: ', err); }); (0, beacon_dapp_1.closeToast)(); }) } ] : [{ text: 'Error message', actionText: output.errorMessage }] }); }); /** * Show an "Operation Broadcasted" alert * * @param data The data that is emitted by the OPERATION_REQUEST_SUCCESS event */ const showOperationSuccessAlert = (data) => __awaiter(void 0, void 0, void 0, function* () { const { account, output, blockExplorer } = data; (0, beacon_dapp_1.openToast)({ body: `{{wallet}}\u00A0 successfully submitted operation`, timer: SUCCESS_TIMER, state: 'finished', walletInfo: data.walletInfo, actions: [ { text: (0, shorten_string_1.shortenString)(output.transactionHash), isBold: true, actionText: `Open Blockexplorer`, actionLogo: 'external', actionCallback: () => __awaiter(void 0, void 0, void 0, function* () { const link = yield blockExplorer.getTransactionLink(output.transactionHash, account.network); window.open(link, '_blank', 'noopener'); (0, beacon_dapp_1.closeToast)(); }) } ] }); }); /** * Show a "Transaction Signed" alert * * @param data The data that is emitted by the SIGN_REQUEST_SUCCESS event */ const showSignSuccessAlert = (data) => __awaiter(void 0, void 0, void 0, function* () { const output = data.output; (0, beacon_dapp_1.openToast)({ body: `{{wallet}}\u00A0 successfully signed payload`, timer: SUCCESS_TIMER, state: 'finished', walletInfo: data.walletInfo, actions: [ { text: `Signature: ${(0, shorten_string_1.shortenString)(output.signature)}`, actionText: 'Copy to clipboard', actionCallback: () => __awaiter(void 0, void 0, void 0, function* () { navigator.clipboard.writeText(output.signature).then(() => { logger.log('showSignSuccessAlert', 'Copying to clipboard was successful!'); }, (err) => { logger.error('showSignSuccessAlert', 'Could not copy text to clipboard: ', err); }); (0, beacon_dapp_1.closeToast)(); }) } ] }); }); /** * Show a "Transaction Signed" alert * * @param data The data that is emitted by the ENCRYPT_REQUEST_SUCCESS event */ // TODO: ENCRYPTION // const showEncryptSuccessAlert = async ( // data: BeaconEventType[BeaconEvent.ENCRYPT_REQUEST_SUCCESS] // ): Promise<void> => { // const output = data.output // openToast({ // body: `{{wallet}}\u00A0 successfully ${ // data.output.cryptoOperation === EncryptionOperation.ENCRYPT ? 'encrypted' : 'decrypted' // } payload`, // timer: SUCCESS_TIMER, // state: 'finished', // walletInfo: data.walletInfo, // actions: [ // { // text: `Payload: <strong>${shortenString(output.payload)}</strong>`, // actionText: 'Copy to clipboard', // actionCallback: async (): Promise<void> => { // navigator.clipboard.writeText(output.payload).then( // () => { // logger.log('showSignSuccessAlert', 'Copying to clipboard was successful!') // }, // (err) => { // logger.error('showSignSuccessAlert', 'Could not copy text to clipboard: ', err) // } // ) // closeToast() // } // } // ] // }) // } const showBlockchainRequestSuccessToast = (data) => __awaiter(void 0, void 0, void 0, function* () { (0, beacon_dapp_1.openToast)({ body: `{{wallet}}\u00A0 request successfully executed`, timer: SUCCESS_TIMER, state: 'finished', walletInfo: data.walletInfo }); }); /** * Show a "Broadcasted" alert * * @param data The data that is emitted by the BROADCAST_REQUEST_SUCCESS event */ const showBroadcastSuccessAlert = (data) => __awaiter(void 0, void 0, void 0, function* () { const { network, output, blockExplorer } = data; (0, beacon_dapp_1.openToast)({ body: `{{wallet}}\u00A0 successfully injected operation`, timer: SUCCESS_TIMER, state: 'finished', walletInfo: data.walletInfo, actions: [ { text: (0, shorten_string_1.shortenString)(output.transactionHash), isBold: true, actionText: `Open Blockexplorer`, actionLogo: 'external', actionCallback: () => __awaiter(void 0, void 0, void 0, function* () { const link = yield blockExplorer.getTransactionLink(output.transactionHash, network); window.open(link, '_blank', 'noopener'); (0, beacon_dapp_1.closeToast)(); }) } ] }); }); const emptyHandler = () => () => __awaiter(void 0, void 0, void 0, function* () { // }); /** * The default event handlers */ exports.defaultEventCallbacks = { [BeaconEvent.PERMISSION_REQUEST_SENT]: showSentToast, [BeaconEvent.PERMISSION_REQUEST_SUCCESS]: showPermissionSuccessAlert, [BeaconEvent.PERMISSION_REQUEST_ERROR]: showErrorToast, [BeaconEvent.PROOF_OF_EVENT_CHALLENGE_REQUEST_SENT]: showSentToast, [BeaconEvent.PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS]: showProofOfEventChallengeSuccessAlert, [BeaconEvent.PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR]: showErrorToast, [BeaconEvent.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SENT]: showSentToast, [BeaconEvent.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS]: showSimulatedProofOfEventChallengeSuccessAlert, [BeaconEvent.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR]: showErrorToast, [BeaconEvent.OPERATION_REQUEST_SENT]: showSentToast, [BeaconEvent.OPERATION_REQUEST_SUCCESS]: showOperationSuccessAlert, [BeaconEvent.OPERATION_REQUEST_ERROR]: showErrorToast, [BeaconEvent.SIGN_REQUEST_SENT]: showSentToast, [BeaconEvent.SIGN_REQUEST_SUCCESS]: showSignSuccessAlert, [BeaconEvent.SIGN_REQUEST_ERROR]: showErrorToast, // TODO: ENCRYPTION // [BeaconEvent.ENCRYPT_REQUEST_SENT]: showSentToast, // [BeaconEvent.ENCRYPT_REQUEST_SUCCESS]: showEncryptSuccessAlert, // [BeaconEvent.ENCRYPT_REQUEST_ERROR]: showErrorToast, [BeaconEvent.BROADCAST_REQUEST_SENT]: showSentToast, [BeaconEvent.BROADCAST_REQUEST_SUCCESS]: showBroadcastSuccessAlert, [BeaconEvent.BROADCAST_REQUEST_ERROR]: showErrorToast, [BeaconEvent.ACKNOWLEDGE_RECEIVED]: showAcknowledgedToast, [BeaconEvent.LOCAL_RATE_LIMIT_REACHED]: showRateLimitReached, [BeaconEvent.NO_PERMISSIONS]: showNoPermissionAlert, [BeaconEvent.ACTIVE_ACCOUNT_SET]: emptyHandler(), [BeaconEvent.ACTIVE_TRANSPORT_SET]: emptyHandler(), [BeaconEvent.INVALID_ACTIVE_ACCOUNT_STATE]: showInvalidActiveAccountState, [BeaconEvent.GENERIC_ERROR]: showGenericErrorAlert, [BeaconEvent.INVALID_ACCOUNT_DEACTIVATED]: showInvalidAccountDeactivated, [BeaconEvent.SHOW_PREPARE]: showPrepare, [BeaconEvent.HIDE_UI]: hideUI, [BeaconEvent.PAIR_INIT]: showPairAlert, [BeaconEvent.PAIR_SUCCESS]: showExtensionConnectedAlert, [BeaconEvent.OPEN_BUG_REPORT]: showBugReportForm, [BeaconEvent.RELAYER_ERROR]: showRelayerErrorAlert, [BeaconEvent.CHANNEL_CLOSED]: showChannelClosedAlert, [BeaconEvent.INTERNAL_ERROR]: showInternalErrorAlert, [BeaconEvent.BLOCKCHAIN_REQUEST_SUCCESS]: showBlockchainRequestSuccessToast, [BeaconEvent.UNKNOWN]: emptyHandler() }; /** * @internalapi * * Handles beacon events */ class BeaconEventHandler { constructor(eventsToOverride = {}, overrideAll) { this.callbackMap = { [BeaconEvent.PERMISSION_REQUEST_SENT]: [exports.defaultEventCallbacks.PERMISSION_REQUEST_SENT], [BeaconEvent.PERMISSION_REQUEST_SUCCESS]: [exports.defaultEventCallbacks.PERMISSION_REQUEST_SUCCESS], [BeaconEvent.PERMISSION_REQUEST_ERROR]: [exports.defaultEventCallbacks.PERMISSION_REQUEST_ERROR], [BeaconEvent.PROOF_OF_EVENT_CHALLENGE_REQUEST_SENT]: [ exports.defaultEventCallbacks.PERMISSION_REQUEST_SENT ], [BeaconEvent.PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS]: [ exports.defaultEventCallbacks.PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS ], [BeaconEvent.PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR]: [ exports.defaultEventCallbacks.PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR ], [BeaconEvent.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SENT]: [ exports.defaultEventCallbacks.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SENT ], [BeaconEvent.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS]: [ exports.defaultEventCallbacks.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_SUCCESS ], [BeaconEvent.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR]: [ exports.defaultEventCallbacks.SIMULATED_PROOF_OF_EVENT_CHALLENGE_REQUEST_ERROR ], [BeaconEvent.OPERATION_REQUEST_SENT]: [exports.defaultEventCallbacks.OPERATION_REQUEST_SENT], [BeaconEvent.OPERATION_REQUEST_SUCCESS]: [exports.defaultEventCallbacks.OPERATION_REQUEST_SUCCESS], [BeaconEvent.OPERATION_REQUEST_ERROR]: [exports.defaultEventCallbacks.OPERATION_REQUEST_ERROR], [BeaconEvent.SIGN_REQUEST_SENT]: [exports.defaultEventCallbacks.SIGN_REQUEST_SENT], [BeaconEvent.SIGN_REQUEST_SUCCESS]: [exports.defaultEventCallbacks.SIGN_REQUEST_SUCCESS], [BeaconEvent.SIGN_REQUEST_ERROR]: [exports.defaultEventCallbacks.SIGN_REQUEST_ERROR], // TODO: ENCRYPTION // [BeaconEvent.ENCRYPT_REQUEST_SENT]: [defaultEventCallbacks.ENCRYPT_REQUEST_SENT], // [BeaconEvent.ENCRYPT_REQUEST_SUCCESS]: [defaultEventCallbacks.ENCRYPT_REQUEST_SUCCESS], // [BeaconEvent.ENCRYPT_REQUEST_ERROR]: [defaultEventCallbacks.ENCRYPT_REQUEST_ERROR], [BeaconEvent.BROADCAST_REQUEST_SENT]: [exports.defaultEventCallbacks.BROADCAST_REQUEST_SENT], [BeaconEvent.BROADCAST_REQUEST_SUCCESS]: [exports.defaultEventCallbacks.BROADCAST_REQUEST_SUCCESS], [BeaconEvent.BROADCAST_REQUEST_ERROR]: [exports.defaultEventCallbacks.BROADCAST_REQUEST_ERROR], [BeaconEvent.BLOCKCHAIN_REQUEST_SUCCESS]: [exports.defaultEventCallbacks.BLOCKCHAIN_REQUEST_SUCCESS], [BeaconEvent.ACKNOWLEDGE_RECEIVED]: [exports.defaultEventCallbacks.ACKNOWLEDGE_RECEIVED], [BeaconEvent.LOCAL_RATE_LIMIT_REACHED]: [exports.defaultEventCallbacks.LOCAL_RATE_LIMIT_REACHED], [BeaconEvent.NO_PERMISSIONS]: [exports.defaultEventCallbacks.NO_PERMISSIONS], [BeaconEvent.ACTIVE_ACCOUNT_SET]: [exports.defaultEventCallbacks.ACTIVE_ACCOUNT_SET], [BeaconEvent.ACTIVE_TRANSPORT_SET]: [exports.defaultEventCallbacks.ACTIVE_TRANSPORT_SET], [BeaconEvent.INVALID_ACTIVE_ACCOUNT_STATE]: [ exports.defaultEventCallbacks.INVALID_ACTIVE_ACCOUNT_STATE ], [BeaconEvent.GENERIC_ERROR]: [showGenericErrorAlert], [BeaconEvent.INVALID_ACCOUNT_DEACTIVATED]: [exports.defaultEventCallbacks.INVALID_ACCOUNT_DEACTIVATED], [BeaconEvent.SHOW_PREPARE]: [exports.defaultEventCallbacks.SHOW_PREPARE], [BeaconEvent.HIDE_UI]: [exports.defaultEventCallbacks.HIDE_UI], [BeaconEvent.PAIR_INIT]: [exports.defaultEventCallbacks.PAIR_INIT], [BeaconEvent.PAIR_SUCCESS]: [exports.defaultEventCallbacks.PAIR_SUCCESS], [BeaconEvent.OPEN_BUG_REPORT]: [showBugReportForm], [BeaconEvent.CHANNEL_CLOSED]: [exports.defaultEventCallbacks.CHANNEL_CLOSED], [BeaconEvent.INTERNAL_ERROR]: [exports.defaultEventCallbacks.INTERNAL_ERROR], [BeaconEvent.RELAYER_ERROR]: [exports.defaultEventCallbacks.RELAYER_ERROR], [BeaconEvent.UNKNOWN]: [exports.defaultEventCallbacks.UNKNOWN] }; if (overrideAll) { this.setAllHandlers(); } this.overrideDefaults(eventsToOverride); } /** * A method to subscribe to a specific beacon event and register a callback * * @param event The event being emitted * @param eventCallback The callback that will be invoked */ on(event, eventCallback) { return __awaiter(this, void 0, void 0, function* () { const listeners = this.callbackMap[event] || []; listeners.push(eventCallback); this.callbackMap[event] = listeners; }); } /** * Emit a beacon event * * @param event The event being emitted * @param data The data to be emit */ emit(event, data, eventCallback) { return __awaiter(this, void 0, void 0, function* () { const listeners = this.callbackMap[event]; if (listeners && listeners.length > 0) { listeners.forEach((listener) => __awaiter(this, void 0, void 0, function* () { try { yield listener(data, eventCallback); } catch (listenerError) { logger.error(`error handling event ${event}`, listenerError); } })); } }); } /** * Override beacon event default callbacks. This can be used to disable default alert/toast behaviour * * @param eventsToOverride An object with the events to override */ overrideDefaults(eventsToOverride) { Object.keys(eventsToOverride).forEach((untypedEvent) => { const eventType = untypedEvent; const event = eventsToOverride[eventType]; if (event) { this.callbackMap[eventType] = [event.handler]; } }); } /** * Set all event callbacks to a specific handler. */ setAllHandlers(handler) { Object.keys(this.callbackMap).forEach((untypedEvent) => { const eventType = untypedEvent; this.callbackMap[eventType] = []; if (handler) { this.callbackMap[eventType].push(handler); } else { this.callbackMap[eventType].push((...data) => { logger.log(untypedEvent, ...data); }); } }); } } exports.BeaconEventHandler = BeaconEventHandler; //# sourceMappingURL=events.js.map