Skip to content

Commit

Permalink
feat: add extension status event & messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidsowardx committed Aug 2, 2023
1 parent 5356fd5 commit 23a95f6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/chrome/content-script/content-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
)
},
)
17 changes: 17 additions & 0 deletions src/chrome/content-script/message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions src/chrome/dapp/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const messageLifeCycleEvent = {
receivedByWallet: 'receivedByWallet',
requestCancelSuccess: 'requestCancelSuccess',
requestCancelFail: 'requestCancelFail',
extensionStatus: 'extensionStatus',
} as const

export type MessageLifeCycleEvent = keyof typeof messageLifeCycleEvent
9 changes: 9 additions & 0 deletions src/chrome/messages/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const messageDiscriminator = {
closeLedgerTab: 'closeLedgerTab',
focusLedgerTab: 'focusLedgerTab',
closeDappTab: 'closeDappTab',
extensionStatus: 'extensionStatus',
ledgerResponse: 'ledgerResponse',
walletToLedger: 'walletToLedger',
walletResponse: 'walletResponse',
Expand Down Expand Up @@ -67,6 +68,14 @@ export type SendMessageWithConfirmation<T = any> = (
>

export type Messages = {
[messageDiscriminator.extensionStatus]: MessageBuilder<
MessageDiscriminator['extensionStatus'],
{
eventType: MessageDiscriminator['extensionStatus']
isWalletLinked: boolean
isExtensionAvailable: true
}
>
[messageDiscriminator.confirmation]:
| ConfirmationMessageSuccess
| ConfirmationMessageError
Expand Down
5 changes: 5 additions & 0 deletions src/chrome/messages/create-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 23a95f6

Please sign in to comment.