diff --git a/src/chrome/background/ledger-tab-watcher.ts b/src/chrome/background/ledger-tab-watcher.ts index 4a7e1722..d8eb54e4 100644 --- a/src/chrome/background/ledger-tab-watcher.ts +++ b/src/chrome/background/ledger-tab-watcher.ts @@ -9,10 +9,15 @@ export const LedgerTabWatcher = () => ({ sessionStore .getSingleItem('watchedTab') .mapErr(() => ({ reason: 'failedToGetWatchedTab' })), - setWatchedTab: (tabId: number, request: LedgerRequest) => + setWatchedTab: ( + tabId: number, + request: LedgerRequest, + walletPublicKey: string, + ) => sessionStore.setSingleItem('watchedTab', { tabId, request, + walletPublicKey, }), triggerTabRemoval: async (justRemovedTabId: number) => { const watchedTab = await sessionStore.getSingleItem('watchedTab') @@ -20,7 +25,7 @@ export const LedgerTabWatcher = () => ({ return } - const { tabId, request } = watchedTab.value + const { tabId, request, walletPublicKey } = watchedTab.value if (!tabId || justRemovedTabId !== tabId) { return @@ -29,6 +34,7 @@ export const LedgerTabWatcher = () => ({ sendMessage( createMessage.ledgerResponse( createLedgerErrorResponse(request, 'tabClosed'), + walletPublicKey, ), ) diff --git a/src/chrome/background/message-handler.ts b/src/chrome/background/message-handler.ts index 83a70c55..b0df0b28 100644 --- a/src/chrome/background/message-handler.ts +++ b/src/chrome/background/message-handler.ts @@ -7,6 +7,7 @@ import { Message, SendMessageWithConfirmation, MessageHandlerOutput, + messageSource, } from '../messages/_types' import { getConnections as getConnectionsFn, @@ -249,12 +250,12 @@ export const BackgroundMessageHandler = ensureTab(config.popup.pages.ledger) .andThen((tab) => ledgerTabWatcher - .setWatchedTab(tab.id!, message.data) + .setWatchedTab(tab.id!, message.data, message.walletPublicKey) .map(() => tab), ) .andThen((tab) => sendMessageWithConfirmation( - { ...message, source: 'background' }, + { ...message, source: messageSource.background }, tab.id, ), ) diff --git a/src/chrome/dapp/schemas.ts b/src/chrome/dapp/schemas.ts index 5584f1f9..c55d0cde 100644 --- a/src/chrome/dapp/schemas.ts +++ b/src/chrome/dapp/schemas.ts @@ -50,13 +50,15 @@ export type CancelWalletInteractionExtensionInteractionOptionalOrigin = Output< typeof CancelWalletInteractionExtensionInteractionOptionalOrigin > -export const CancelWalletInteractionExtensionInteractionOptionalOrigin = object({ - interactionId: string(), - discriminator: literal( - extensionInteractionDiscriminator.cancelWalletInteraction, - ), - metadata: MetadataWithOptionalOrigin, -}) +export const CancelWalletInteractionExtensionInteractionOptionalOrigin = object( + { + interactionId: string(), + discriminator: literal( + extensionInteractionDiscriminator.cancelWalletInteraction, + ), + metadata: MetadataWithOptionalOrigin, + }, +) export type ExtenstionInteractionOptionalOrigin = Output< typeof ExtenstionInteractionOptionalOrigin diff --git a/src/chrome/dev-tools/components/LedgerSimulator.tsx b/src/chrome/dev-tools/components/LedgerSimulator.tsx index d8eaba10..8d04acb4 100644 --- a/src/chrome/dev-tools/components/LedgerSimulator.tsx +++ b/src/chrome/dev-tools/components/LedgerSimulator.tsx @@ -144,7 +144,7 @@ export const LedgerSimulator = () => { } const response = await createResponse() if (response) { - sendMessage(createMessage.ledgerResponse(response as LedgerResponse)) + sendMessage(createMessage.ledgerResponse(response as LedgerResponse, '')) sendMessage(createMessage.confirmationSuccess('any', messageId!)) sendMessage(createMessage.closeLedgerTab()) setMessageId(undefined) diff --git a/src/chrome/messages/_types.ts b/src/chrome/messages/_types.ts index e5d00c90..9b625c4c 100644 --- a/src/chrome/messages/_types.ts +++ b/src/chrome/messages/_types.ts @@ -214,7 +214,7 @@ export type Messages = { > [messageDiscriminator.ledgerResponse]: MessageBuilder< MessageDiscriminator['ledgerResponse'], - { data: LedgerResponse } + { data: LedgerResponse; walletPublicKey: string } > [messageDiscriminator.walletToExtension]: MessageBuilder< MessageDiscriminator['walletToExtension'], @@ -225,7 +225,7 @@ export type Messages = { > [messageDiscriminator.walletToLedger]: MessageBuilder< MessageDiscriminator['walletToLedger'], - { data: LedgerRequest } + { data: LedgerRequest; walletPublicKey: string } > [messageDiscriminator.restartConnector]: MessageBuilder< MessageDiscriminator['restartConnector'], diff --git a/src/chrome/messages/create-message.ts b/src/chrome/messages/create-message.ts index 29ed938a..fff7558f 100644 --- a/src/chrome/messages/create-message.ts +++ b/src/chrome/messages/create-message.ts @@ -150,9 +150,11 @@ export const createMessage = { walletToLedger: ( source: MessageSource, message: LedgerRequest, + walletPublicKey: string, ): Messages['walletToLedger'] => ({ source, discriminator: messageDiscriminator.walletToLedger, + walletPublicKey, messageId: crypto.randomUUID(), data: message, }), @@ -167,9 +169,13 @@ export const createMessage = { walletPublicKey, data: message, }), - ledgerResponse: (message: LedgerResponse): Messages['ledgerResponse'] => ({ + ledgerResponse: ( + message: LedgerResponse, + walletPublicKey: string, + ): Messages['ledgerResponse'] => ({ source: 'ledger', discriminator: messageDiscriminator.ledgerResponse, + walletPublicKey, messageId: crypto.randomUUID(), data: message, }), diff --git a/src/chrome/offscreen/wallet-connection/message-handler.ts b/src/chrome/offscreen/wallet-connection/message-handler.ts index 8167f783..4ca6aa4b 100644 --- a/src/chrome/offscreen/wallet-connection/message-handler.ts +++ b/src/chrome/offscreen/wallet-connection/message-handler.ts @@ -61,10 +61,14 @@ export const WalletConnectionMessageHandler = (input: { logger.debug('🪪 -> 📒: walletToLedgerSubject', message.data) return sendMessageWithConfirmation( - createMessage.walletToLedger('offScreen', message.data), + createMessage.walletToLedger( + 'offScreen', + message.data, + walletPublicKey, + ), ).map(() => ({ sendConfirmation: false })) } else if (isExtensionMessage(message.data)) { - logger.debug('wallet to extension', message.data) + logger.debug('Wallet to extension', message.data) return sendMessageWithConfirmation( createMessage.walletToExtension( 'offScreen', @@ -212,6 +216,10 @@ export const WalletConnectionMessageHandler = (input: { }) case messageDiscriminator.ledgerResponse: + if (message.walletPublicKey !== walletPublicKey) { + return okAsync({ sendConfirmation: false }) + } + return extensionToWalletQueue .add(message.data, message.data.interactionId) .map(() => ({ sendConfirmation: false })) diff --git a/src/ledger/views/ledger.tsx b/src/ledger/views/ledger.tsx index 0cc33378..91fc89c3 100644 --- a/src/ledger/views/ledger.tsx +++ b/src/ledger/views/ledger.tsx @@ -95,9 +95,9 @@ export const Ledger = () => { useState() const [viewDefinition, setViewDefinition] = useState() - const respond = (response: LedgerResponse) => { - sendMessage(createMessage.ledgerResponse(response)).map(() => - window.close(), + const respond = (response: LedgerResponse, walletPublicKey: string) => { + sendMessage(createMessage.ledgerResponse(response, walletPublicKey)).map( + () => window.close(), ) } @@ -158,7 +158,10 @@ export const Ledger = () => { return } - respond(createLedgerErrorResponse(currentMessage.data, response.error)) + respond( + createLedgerErrorResponse(currentMessage.data, response.error), + currentMessage.walletPublicKey, + ) return } @@ -169,7 +172,7 @@ export const Ledger = () => { response.value, ) as LedgerSuccessResponse - respond(ledgerResponse) + respond(ledgerResponse, currentMessage.walletPublicKey) } } } @@ -182,6 +185,7 @@ export const Ledger = () => { if (event.device.productId === currentId && currentMessage) { respond( createLedgerErrorResponse(currentMessage.data, 'deviceDisconnected'), + currentMessage.walletPublicKey, ) } }