From 5ff5c05985cb5d3a5c796cb1fd4ad80746f0818b Mon Sep 17 00:00:00 2001 From: Alex Stelea Date: Tue, 1 Oct 2024 16:40:23 +0200 Subject: [PATCH 1/4] feat: fallback to origin if tabId is not found --- src/chrome/helpers/get-tabs-by-origin.ts | 8 ++++++++ src/chrome/messages/send-message.ts | 19 ++++++++++++++++--- .../wallet-connection/message-handler.ts | 16 ---------------- 3 files changed, 24 insertions(+), 19 deletions(-) create mode 100644 src/chrome/helpers/get-tabs-by-origin.ts diff --git a/src/chrome/helpers/get-tabs-by-origin.ts b/src/chrome/helpers/get-tabs-by-origin.ts new file mode 100644 index 00000000..e1970243 --- /dev/null +++ b/src/chrome/helpers/get-tabs-by-origin.ts @@ -0,0 +1,8 @@ +import { ResultAsync } from 'neverthrow' + +export const getTabsByOrigin = (origin: string) => + ResultAsync.fromPromise( + // url pattern match requires a trailing slash + chrome.tabs.query({ url: `${origin}/` }), + (err) => err as Error, + ) diff --git a/src/chrome/messages/send-message.ts b/src/chrome/messages/send-message.ts index 81a6ea73..1fe7f472 100644 --- a/src/chrome/messages/send-message.ts +++ b/src/chrome/messages/send-message.ts @@ -1,9 +1,10 @@ -import { ResultAsync } from 'neverthrow' +import { err, ok, ResultAsync } from 'neverthrow' import { ConfirmationMessageError, Message } from './_types' import { getTabById } from 'chrome/helpers/get-tab-by-id' import { sendMessage as chromeSendMessage } from 'chrome/helpers/send-message' import { sendMessageToTab as chromeSendMessageToTab } from 'chrome/helpers/send-message-to-tab' +import { getTabsByOrigin } from 'chrome/helpers/get-tabs-by-origin' export type SendMessage = typeof sendMessage export const sendMessage = ( @@ -19,8 +20,20 @@ export const sendMessage = ( message: 'could not find tab, user may have closed it', jsError: error, })) - .andThen(() => - chromeSendMessageToTab(tabId, message).mapErr((error) => ({ + .orElse((error) => { + if (message.discriminator === 'walletResponse') { + const maybeOrigin: string | undefined = message.data?.metadata.origin + if (maybeOrigin) { + return getTabsByOrigin(maybeOrigin) + .andThen((tabs) => (tabs[0] ? ok(tabs[0]) : err(error))) + .orElse(() => err(error)) + } + } + + return err(error) + }) + .andThen((tab) => + chromeSendMessageToTab(tab.id!, message).mapErr((error) => ({ reason: 'couldNotSendMessageToTab', jsError: error, })), diff --git a/src/chrome/offscreen/wallet-connection/message-handler.ts b/src/chrome/offscreen/wallet-connection/message-handler.ts index 8b98235a..de7f093b 100644 --- a/src/chrome/offscreen/wallet-connection/message-handler.ts +++ b/src/chrome/offscreen/wallet-connection/message-handler.ts @@ -222,22 +222,6 @@ export const WalletConnectionMessageHandler = (input: { .add(message.data, message.data.interactionId) .map(() => ({ sendConfirmation: false })) - case messageDiscriminator.closeDappTab: { - const { tabId } = message - return messagesRouter - .getAndRemoveByTabId(tabId) - .mapErr(() => ({ reason: 'tabIdNotFound' })) - .map((interactionIds) => { - for (const interactionId of interactionIds) { - ResultAsync.combine([ - dAppRequestQueue.cancel(interactionId), - incomingWalletMessageQueue.cancel(interactionId), - ]) - } - }) - .map(() => ({ sendConfirmation: false })) - } - default: return errAsync({ reason: 'unhandledMessageDiscriminator', From 6f86dfd2783b80180ee5ee84a48ebc096d04456a Mon Sep 17 00:00:00 2001 From: Alex Stelea Date: Tue, 1 Oct 2024 16:46:11 +0200 Subject: [PATCH 2/4] code: remove closeDappTab message --- src/chrome/background/background.ts | 3 --- src/chrome/messages/_types.ts | 5 ----- src/chrome/messages/create-message.ts | 9 --------- 3 files changed, 17 deletions(-) diff --git a/src/chrome/background/background.ts b/src/chrome/background/background.ts index 846da8c0..95c469dc 100644 --- a/src/chrome/background/background.ts +++ b/src/chrome/background/background.ts @@ -107,9 +107,6 @@ const handleNotificationClick = (notificationId: string) => { const tabRemovedListener = (tabId: number) => { ledgerTabWatcher.triggerTabRemoval(tabId) - messageHandler.sendMessageAndWaitForConfirmation( - createMessage.closeDappTab('background', tabId), - ) } chrome.tabs.onUpdated.addListener((tabId, changeInfo) => { diff --git a/src/chrome/messages/_types.ts b/src/chrome/messages/_types.ts index 9b625c4c..6d2ed653 100644 --- a/src/chrome/messages/_types.ts +++ b/src/chrome/messages/_types.ts @@ -24,7 +24,6 @@ export const messageDiscriminator = { cancelWalletInteraction: 'cancelWalletInteraction', closeLedgerTab: 'closeLedgerTab', focusLedgerTab: 'focusLedgerTab', - closeDappTab: 'closeDappTab', extensionStatus: 'extensionStatus', ledgerResponse: 'ledgerResponse', walletToLedger: 'walletToLedger', @@ -192,10 +191,6 @@ export type Messages = { MessageDiscriminator['focusLedgerTab'], {} > - [messageDiscriminator.closeDappTab]: MessageBuilder< - MessageDiscriminator['closeDappTab'], - { tabId: number } - > [messageDiscriminator.incomingDappMessage]: MessageBuilder< MessageDiscriminator['incomingDappMessage'], { data: Record } diff --git a/src/chrome/messages/create-message.ts b/src/chrome/messages/create-message.ts index fff7558f..5ab8b61f 100644 --- a/src/chrome/messages/create-message.ts +++ b/src/chrome/messages/create-message.ts @@ -138,15 +138,6 @@ export const createMessage = { discriminator: 'closeLedgerTab', messageId: crypto.randomUUID(), }), - closeDappTab: ( - source: MessageSource, - tabId: number, - ): Messages['closeDappTab'] => ({ - source, - tabId, - discriminator: 'closeDappTab', - messageId: crypto.randomUUID(), - }), walletToLedger: ( source: MessageSource, message: LedgerRequest, From 3060d4b08d8bb7d4f296d6b7b76c7185b1ea3968 Mon Sep 17 00:00:00 2001 From: Alex Stelea Date: Wed, 2 Oct 2024 09:23:59 +0200 Subject: [PATCH 3/4] chore: add code type to semantic release config --- release.config.cjs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/release.config.cjs b/release.config.cjs index b6a6d93e..58b14359 100644 --- a/release.config.cjs +++ b/release.config.cjs @@ -55,6 +55,10 @@ module.exports = { type: 'chore', release: 'patch', }, + { + type: 'code', + release: 'patch', + }, { type: 'no-release', release: false, @@ -142,19 +146,17 @@ module.exports = { }, ], [ - "semantic-release-replace-plugin", + 'semantic-release-replace-plugin', { - "replacements": [ + replacements: [ { - "files": [ - "src/version.ts" - ], - "from": "export const __VERSION__ = '1.0.0'", - "to": "export const __VERSION__ = '${nextRelease.version}'", - "countMatches": true - } - ] - } + files: ['src/version.ts'], + from: "export const __VERSION__ = '1.0.0'", + to: "export const __VERSION__ = '${nextRelease.version}'", + countMatches: true, + }, + ], + }, ], [ '@semantic-release/exec', From de74a9f3bff253c5a9e9bd50c69767d503577352 Mon Sep 17 00:00:00 2001 From: Alex Stelea Date: Wed, 2 Oct 2024 09:24:26 +0200 Subject: [PATCH 4/4] code: use wildcard in url pattern match --- src/chrome/helpers/get-tabs-by-origin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chrome/helpers/get-tabs-by-origin.ts b/src/chrome/helpers/get-tabs-by-origin.ts index e1970243..706b6681 100644 --- a/src/chrome/helpers/get-tabs-by-origin.ts +++ b/src/chrome/helpers/get-tabs-by-origin.ts @@ -3,6 +3,6 @@ import { ResultAsync } from 'neverthrow' export const getTabsByOrigin = (origin: string) => ResultAsync.fromPromise( // url pattern match requires a trailing slash - chrome.tabs.query({ url: `${origin}/` }), + chrome.tabs.query({ url: `${origin}/*` }), (err) => err as Error, )