From ed8a1e2228351b766326c3c093c1119ebdf66aec Mon Sep 17 00:00:00 2001 From: Dawid Sowa Date: Tue, 1 Aug 2023 17:02:26 +0200 Subject: [PATCH] feat: add extension status event & messages --- src/chrome/content-script/content-script.ts | 11 +++++++++++ src/chrome/content-script/message-handler.ts | 17 +++++++++++++++++ src/chrome/dapp/_types.ts | 1 + src/chrome/messages/_types.ts | 9 +++++++++ src/chrome/messages/create-message.ts | 5 +++++ 5 files changed, 43 insertions(+) diff --git a/src/chrome/content-script/content-script.ts b/src/chrome/content-script/content-script.ts index 5a4b5350..f47c534d 100644 --- a/src/chrome/content-script/content-script.ts +++ b/src/chrome/content-script/content-script.ts @@ -46,3 +46,14 @@ chromeDAppClient.messageListener((message) => { chrome.runtime.onMessage.addListener((message: Message) => { messageHandler.onMessage(message) }) + +chrome.storage.onChanged.addListener( + (changes: { [key: string]: chrome.storage.StorageChange }) => { + if (changes['connectionPassword']) + sendMessageToDapp( + createMessage.extensionStatus( + !!changes['connectionPassword']?.newValue, + ), + ) + }, +) diff --git a/src/chrome/content-script/message-handler.ts b/src/chrome/content-script/message-handler.ts index 1527d9a0..d183af32 100644 --- a/src/chrome/content-script/message-handler.ts +++ b/src/chrome/content-script/message-handler.ts @@ -11,6 +11,7 @@ import { } from '../messages/_types' import { AppLogger } from 'utils/logger' import { MessageLifeCycleEvent } from 'chrome/dapp/_types' +import { getConnectionPassword } from 'chrome/helpers/get-connection-password' export type ContentScriptMessageHandlerOptions = { logger?: AppLogger @@ -51,6 +52,22 @@ export const ContentScriptMessageHandler = } case messageDiscriminator.incomingDappMessage: { + if ( + message.data.discriminator === messageDiscriminator.extensionStatus + ) { + return getConnectionPassword() + .andThen((connectionPassword) => + sendMessageToDapp( + createMessage.extensionStatus(!!connectionPassword), + ).map(() => ({ sendConfirmation: false })), + ) + .mapErr((error) => { + return { + reason: 'unableToGetConnectionPassword', + } + }) + } + return sendMessageEventToDapp( message.data.interactionId, 'receivedByExtension', diff --git a/src/chrome/dapp/_types.ts b/src/chrome/dapp/_types.ts index 09e3aab3..a3f8c0d0 100644 --- a/src/chrome/dapp/_types.ts +++ b/src/chrome/dapp/_types.ts @@ -10,6 +10,7 @@ export const messageLifeCycleEvent = { receivedByWallet: 'receivedByWallet', requestCancelSuccess: 'requestCancelSuccess', requestCancelFail: 'requestCancelFail', + extensionStatus: 'extensionStatus', } as const export type MessageLifeCycleEvent = keyof typeof messageLifeCycleEvent diff --git a/src/chrome/messages/_types.ts b/src/chrome/messages/_types.ts index 0e63159d..55aedf44 100644 --- a/src/chrome/messages/_types.ts +++ b/src/chrome/messages/_types.ts @@ -9,6 +9,7 @@ export const messageDiscriminator = { closeLedgerTab: 'closeLedgerTab', focusLedgerTab: 'focusLedgerTab', closeDappTab: 'closeDappTab', + extensionStatus: 'extensionStatus', ledgerResponse: 'ledgerResponse', walletToLedger: 'walletToLedger', walletResponse: 'walletResponse', @@ -67,6 +68,14 @@ export type SendMessageWithConfirmation = ( > export type Messages = { + [messageDiscriminator.extensionStatus]: MessageBuilder< + MessageDiscriminator['extensionStatus'], + { + eventType: MessageDiscriminator['extensionStatus'] + isWalletLinked: boolean + isExtensionAvailable: true + } + > [messageDiscriminator.confirmation]: | ConfirmationMessageSuccess | ConfirmationMessageError diff --git a/src/chrome/messages/create-message.ts b/src/chrome/messages/create-message.ts index 715a2379..0bcd5115 100644 --- a/src/chrome/messages/create-message.ts +++ b/src/chrome/messages/create-message.ts @@ -10,6 +10,11 @@ import { import { MessageLifeCycleEvent } from 'chrome/dapp/_types' export const createMessage = { + extensionStatus: (isWalletLinked: boolean) => ({ + eventType: 'extensionStatus', + isExtensionAvailable: true, + isWalletLinked, + }), offscreenLog: (log: any): Messages['offscreenLog'] => ({ source: 'any', discriminator: 'offscreenLog',