Skip to content

Commit

Permalink
Remove RPC response source filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
aryzing committed Jul 29, 2024
1 parent 798dd4b commit 204527d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
6 changes: 2 additions & 4 deletions src/content-scripts/content-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ function sendMessageToBackground(

// Receives message from background script to execute in browser
chrome.runtime.onMessage.addListener((message: LegacyMessageToContentScript) => {
if (message.source === MESSAGE_SOURCE) {
// Forward to web app (browser)
window.postMessage(message, window.location.origin);
}
// Forward to web app (browser)
window.postMessage(message, window.location.origin);
});

interface ForwardDomEventToBackgroundArgs {
Expand Down
50 changes: 33 additions & 17 deletions src/inpage/sats.inpage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@ import {
type SignMessageResponseMessage,
type SignPsbtResponseMessage,
} from '@common/types/message-types';
import type {
BitcoinProvider,
CreateInscriptionResponse,
CreateRepeatInscriptionsResponse,
GetAddressResponse,
Params,
Requests,
RpcRequest,
RpcResponse,
SignMultipleTransactionsResponse,
SignTransactionResponse,
import {
rpcResponseMessageSchema,
type BitcoinProvider,
type CreateInscriptionResponse,
type CreateRepeatInscriptionsResponse,
type GetAddressResponse,
type Params,
type Requests,
type RpcRequest,
type RpcResponse,
type SignMultipleTransactionsResponse,
type SignTransactionResponse,
} from '@sats-connect/core';
import { nanoid } from 'nanoid';
import { isValidLegacyEvent, isValidRpcEvent } from './utils';
import * as v from 'valibot';
import { isValidLegacyEvent } from './utils';

const SatsMethodsProvider: BitcoinProvider = {
connect: async (btcAddressRequest): Promise<GetAddressResponse> => {
Expand Down Expand Up @@ -218,14 +220,28 @@ const SatsMethodsProvider: BitcoinProvider = {
const rpcRequestEvent = new CustomEvent(DomEventName.rpcRequest, { detail: rpcRequest });
document.dispatchEvent(rpcRequestEvent);
return new Promise((resolve) => {
function handleRpcResponseEvent(eventMessage: MessageEvent<any>) {
if (!isValidRpcEvent(eventMessage)) return;
const response = eventMessage.data;
if (response.id !== id) {
function handleRpcResponseEvent(message: MessageEvent<unknown>) {
const parseResult = v.safeParse(rpcResponseMessageSchema, message.data);

if (!parseResult.success) {
// Ignore message if it's not an RPC message.
return;
}

const rpcResponseMessage = parseResult.output;

if (rpcResponseMessage.id !== id) {
// Ignore message if it's not a response to the current request.
return;
}

window.removeEventListener('message', handleRpcResponseEvent);
return resolve(response);

// NOTE: Ideally the response would be runtime type-checked before the
// promise is resolved since the message crosses a type assertion
// boundary. For now, since all the responses are typed, it's relatively
// safe to assume that the message will conform to the expected type.
return resolve(rpcResponseMessage as RpcResponse<Method>);
}
window.addEventListener('message', handleRpcResponseEvent);
});
Expand Down
5 changes: 0 additions & 5 deletions src/inpage/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ export const isValidLegacyEvent = (
return correctSource && correctMethod && !!data.payload;
};

export const isValidRpcEvent = (event: MessageEvent) => {
const { data } = event;
return data.source === MESSAGE_SOURCE;
};

export const callAndReceive = async (
methodName: CallableMethods | 'getURL',
opts: any = {},
Expand Down

0 comments on commit 204527d

Please sign in to comment.