Skip to content

Commit

Permalink
[pairing] update types
Browse files Browse the repository at this point in the history
  • Loading branch information
grod220 committed Aug 23, 2024
1 parent 8e03a5d commit 3f788eb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions apps/extension/src/entry/popup-root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions apps/extension/src/hooks/popup-ready.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -18,7 +18,7 @@ export const usePopupReady = (isReady: IsReady = undefined) => {
data: {
popupId,
},
} as PopupReadyResponse);
} as PopupResponse<Ready>);
}
}, [popupId, isReady]);
};
10 changes: 6 additions & 4 deletions apps/extension/src/message/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ export enum PopupType {
Ready = 'PopupReady',
}

export type PopupMessage = TxApproval | OriginApproval;
export type PopupMessage = TxApproval | OriginApproval | Ready;
export type PopupRequest<T extends PopupMessage = PopupMessage> = InternalRequest<T>;
export type PopupResponse<T extends PopupMessage = PopupMessage> = InternalResponse<T>;
export type PopupReadyResponse<T extends Ready = Ready> = InternalResponse<T>;

export type OriginApproval = InternalMessage<
PopupType.OriginApproval,
Expand Down Expand Up @@ -53,7 +52,10 @@ export const isPopupRequest = (req: unknown): req is PopupRequest =>
req.type in PopupType;

export const isOriginApprovalRequest = (req: unknown): req is InternalRequest<OriginApproval> =>
isPopupRequest(req) && req.type === PopupType.OriginApproval && 'origin' in req.request;
isPopupRequest(req) && req.type === PopupType.OriginApproval;

export const isTxApprovalRequest = (req: unknown): req is InternalRequest<TxApproval> =>
isPopupRequest(req) && req.type === PopupType.TxApproval && 'authorizeRequest' in req.request;
isPopupRequest(req) && req.type === PopupType.TxApproval;

export const isPopupReadyResponse = (req: unknown): req is InternalResponse<Ready> =>
isPopupRequest(req) && req.type === PopupType.Ready;
17 changes: 13 additions & 4 deletions apps/extension/src/popup.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -101,8 +106,12 @@ const popupReady = async (popupId: string): Promise<void> => {
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();
}
Expand Down

0 comments on commit 3f788eb

Please sign in to comment.