From 10b6574ede520ee09dacd2be45fe9ee77389d6bb Mon Sep 17 00:00:00 2001 From: Dawid Sowa <119939994+dawidsowardx@users.noreply.github.com> Date: Tue, 9 Apr 2024 14:10:16 +0200 Subject: [PATCH] fix: Session id updates (#247) * fix: remove `removeSessionId` command * fix: stop requiring signature * fix: change pairing flow --- src/chrome/background/message-handler.ts | 11 ----------- src/chrome/content-script/content-script.ts | 6 ------ src/chrome/dapp/dapp-client.ts | 2 +- src/chrome/messages/_types.ts | 1 - src/chrome/messages/create-message.ts | 4 ---- src/pairing/pairing.tsx | 10 +++++----- src/pairing/state/connections.ts | 13 +++++++++---- 7 files changed, 15 insertions(+), 32 deletions(-) diff --git a/src/chrome/background/message-handler.ts b/src/chrome/background/message-handler.ts index de4f3cbe..b7be897b 100644 --- a/src/chrome/background/message-handler.ts +++ b/src/chrome/background/message-handler.ts @@ -130,17 +130,6 @@ export const BackgroundMessageHandler = .mapErr(() => ({ reason: 'failedToFocusLedgerTab' })) } - case messageDiscriminator.removeSessionId: { - chromeLocalStore.getSingleItem('sessionRouter').andThen((data) => { - if (!data) { - return okAsync(undefined) - } - - const { [message.sessionId]: _, ...rest } = data - return chromeLocalStore.setSingleItem('sessionRouter', rest) - }) - } - case messageDiscriminator.walletResponse: { const sessionId = message.data?.metadata?.sessionId const walletPublicKey = message.data?.metadata?.walletPublicKey diff --git a/src/chrome/content-script/content-script.ts b/src/chrome/content-script/content-script.ts index a7e3f145..b356cace 100644 --- a/src/chrome/content-script/content-script.ts +++ b/src/chrome/content-script/content-script.ts @@ -62,12 +62,6 @@ const handleExtensionInteraction = async ( extensionInteraction: ExtensionInteraction, ) => { switch (extensionInteraction.discriminator) { - case 'removeSessionId': - await sendMessage( - createMessage.removeSessionId(extensionInteraction.sessionId), - ) - break - case 'openPopup': await sendMessage(createMessage.openParingPopup()) break diff --git a/src/chrome/dapp/dapp-client.ts b/src/chrome/dapp/dapp-client.ts index f50359d4..ebf34cdf 100644 --- a/src/chrome/dapp/dapp-client.ts +++ b/src/chrome/dapp/dapp-client.ts @@ -64,7 +64,7 @@ export const ChromeDAppClient = (logger: AppLogger) => { if (message.interactionId) { logger.warn({ reason: 'UnrecognizedDappRequest', message }) return onDappRequest(addOriginToWalletInteraction(message)) - } + } }) } diff --git a/src/chrome/messages/_types.ts b/src/chrome/messages/_types.ts index 7037a8f6..55c4d39e 100644 --- a/src/chrome/messages/_types.ts +++ b/src/chrome/messages/_types.ts @@ -13,7 +13,6 @@ export const messageDiscriminator = { getExtensionOptions: 'getExtensionOptions', getSessionRouterData: 'getSessionRouterData', setSessionRouterData: 'setSessionRouterData', - removeSessionId: 'removeSessionId', dAppRequest: 'dAppRequest', closeLedgerTab: 'closeLedgerTab', focusLedgerTab: 'focusLedgerTab', diff --git a/src/chrome/messages/create-message.ts b/src/chrome/messages/create-message.ts index 139030aa..13bcdd44 100644 --- a/src/chrome/messages/create-message.ts +++ b/src/chrome/messages/create-message.ts @@ -26,10 +26,6 @@ export const createMessage = { discriminator: messageDiscriminator.setSessionRouterData, data, }), - removeSessionId: (sessionId: string) => ({ - discriminator: messageDiscriminator.removeSessionId, - sessionId, - }), extensionStatus: (isWalletLinked: boolean) => ({ eventType: 'extensionStatus', isExtensionAvailable: true, diff --git a/src/pairing/pairing.tsx b/src/pairing/pairing.tsx index 5564153d..e6bc2cba 100644 --- a/src/pairing/pairing.tsx +++ b/src/pairing/pairing.tsx @@ -5,9 +5,8 @@ import { logger } from 'utils/logger' import { config, radixConnectConfig } from 'config' import { useConnectionsClient } from './state/connections' import { useConnectorOptions } from './state/options' -import { Subscription, filter, map, tap, withLatestFrom } from 'rxjs' +import { Subscription, combineLatest, filter, map, switchMap, tap } from 'rxjs' import { useNavigate } from 'react-router-dom' -import { blakeHashBufferToHex } from 'crypto/blake2b' import { ed25519 } from '@noble/curves/ed25519' import { getLinkingSignatureMessage } from 'crypto/get-linking-message' @@ -70,10 +69,11 @@ export const Pairing = () => { connectorClient.connected$ .pipe( filter(Boolean), - withLatestFrom(hexConnectionPassword$), - withLatestFrom(linkClientInteraction$), + switchMap(() => + combineLatest([hexConnectionPassword$, linkClientInteraction$]), + ), ) - .subscribe(([[, password], interaction]) => { + .subscribe(([password, interaction]) => { connectionsClient .addOrUpdate(password, interaction) .map(() => connectorClient.disconnect()) diff --git a/src/pairing/state/connections.ts b/src/pairing/state/connections.ts index 72f00187..f75710bb 100644 --- a/src/pairing/state/connections.ts +++ b/src/pairing/state/connections.ts @@ -5,6 +5,7 @@ import { Message } from 'chrome/messages/_types' import { chrome } from 'jest-chrome' import { err, errAsync } from 'neverthrow' import { useEffect, useState } from 'react' +import { logger } from 'utils/logger' export type Connection = { walletName: string @@ -84,12 +85,16 @@ const ConnectionsClient = (connections?: Connections | null) => { const addOrUpdate = (password: string, interaction: Message) => { const walletPublickey = interaction.publicKey const signature = interaction.signature - const message = getLinkingSignatureMessage(Buffer.from(password, 'hex')) - const validSignature = ed25519.verify(signature, message, walletPublickey) + if (signature) { + const message = getLinkingSignatureMessage(Buffer.from(password, 'hex')) + const validSignature = ed25519.verify(signature, message, walletPublickey) - if (!validSignature) { - return errAsync({ cause: 'Invalid Signature' } as Error) + if (!validSignature) { + logger.warn('Invalid Signature') + // return errAsync({ cause: 'Invalid Signature' } as Error) + } } + if (connections && connections[walletPublickey]) { connections[walletPublickey] = { ...connections[walletPublickey],