From 3f788eb3d71e47b2b06b223bae1c44637dadf89e Mon Sep 17 00:00:00 2001 From: Gabe Rodriguez Date: Fri, 23 Aug 2024 15:18:23 +0200 Subject: [PATCH] [pairing] update types --- apps/extension/src/entry/popup-root.tsx | 1 + apps/extension/src/hooks/popup-ready.ts | 4 ++-- apps/extension/src/message/popup.ts | 10 ++++++---- apps/extension/src/popup.ts | 17 +++++++++++++---- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/apps/extension/src/entry/popup-root.tsx b/apps/extension/src/entry/popup-root.tsx index 4f42f0d1..5aab9d1a 100644 --- a/apps/extension/src/entry/popup-root.tsx +++ b/apps/extension/src/entry/popup-root.tsx @@ -21,6 +21,7 @@ chrome.runtime.onMessage.addListener( if (isTxApprovalRequest(req)) { void txApprovalSelector(useStore.getState()).acceptRequest(req, responder); } else if (isOriginApprovalRequest(req)) { + req.request.origin; originApprovalSelector(useStore.getState()).acceptRequest(req, responder); } else { throw new Error('Unknown popup request'); diff --git a/apps/extension/src/hooks/popup-ready.ts b/apps/extension/src/hooks/popup-ready.ts index 6fac64c4..f385f0c9 100644 --- a/apps/extension/src/hooks/popup-ready.ts +++ b/apps/extension/src/hooks/popup-ready.ts @@ -1,5 +1,5 @@ import { useEffect, useRef } from 'react'; -import { PopupType, PopupReadyResponse } from '../message/popup'; +import { PopupResponse, PopupType, Ready } from '../message/popup'; type IsReady = boolean | undefined; @@ -18,7 +18,7 @@ export const usePopupReady = (isReady: IsReady = undefined) => { data: { popupId, }, - } as PopupReadyResponse); + } as PopupResponse); } }, [popupId, isReady]); }; diff --git a/apps/extension/src/message/popup.ts b/apps/extension/src/message/popup.ts index a55e34c7..973ad583 100644 --- a/apps/extension/src/message/popup.ts +++ b/apps/extension/src/message/popup.ts @@ -14,10 +14,9 @@ export enum PopupType { Ready = 'PopupReady', } -export type PopupMessage = TxApproval | OriginApproval; +export type PopupMessage = TxApproval | OriginApproval | Ready; export type PopupRequest = InternalRequest; export type PopupResponse = InternalResponse; -export type PopupReadyResponse = InternalResponse; export type OriginApproval = InternalMessage< PopupType.OriginApproval, @@ -53,7 +52,10 @@ export const isPopupRequest = (req: unknown): req is PopupRequest => req.type in PopupType; export const isOriginApprovalRequest = (req: unknown): req is InternalRequest => - isPopupRequest(req) && req.type === PopupType.OriginApproval && 'origin' in req.request; + isPopupRequest(req) && req.type === PopupType.OriginApproval; export const isTxApprovalRequest = (req: unknown): req is InternalRequest => - isPopupRequest(req) && req.type === PopupType.TxApproval && 'authorizeRequest' in req.request; + isPopupRequest(req) && req.type === PopupType.TxApproval; + +export const isPopupReadyResponse = (req: unknown): req is InternalResponse => + isPopupRequest(req) && req.type === PopupType.Ready; diff --git a/apps/extension/src/popup.ts b/apps/extension/src/popup.ts index bbbc887c..8f540d90 100644 --- a/apps/extension/src/popup.ts +++ b/apps/extension/src/popup.ts @@ -1,6 +1,11 @@ import { sessionExtStorage } from './storage/session'; -import { PopupMessage, PopupRequest, PopupType, PopupReadyResponse } from './message/popup'; -// import { PopupMessage, PopupRequest, PopupType } from './message/popup'; +import { + isPopupReadyResponse, + PopupMessage, + PopupRequest, + PopupResponse, + PopupType, +} from './message/popup'; import { PopupPath } from './routes/popup/paths'; import type { InternalRequest, InternalResponse } from '@penumbra-zone/types/internal-msg/shared'; import { Code, ConnectError } from '@connectrpc/connect'; @@ -101,8 +106,12 @@ const popupReady = async (popupId: string): Promise => { reject(new Error('Popup ready timed out')); }, POPUP_READY_TIMEOUT); - const handlePopupReady = (res: PopupReadyResponse): void => { - if (res.type === PopupType.Ready && res.data.popupId === popupId) { + const handlePopupReady = (res: PopupResponse): void => { + if (!isPopupReadyResponse(res)) { + return; + } + + if ('data' in res && res.data.popupId === popupId) { chrome.runtime.onMessage.removeListener(handlePopupReady); resolve(); }