From d9465e1b991a89e572c0c2330fc5d21aa18fc1a9 Mon Sep 17 00:00:00 2001 From: Will Cameron Date: Wed, 11 Dec 2024 14:17:01 -0800 Subject: [PATCH 1/2] feat: create rpc method to link to swap/bridge --- src/background/messaging/messaging-utils.ts | 12 ++++++++- .../messaging/rpc-message-handler.ts | 5 ++++ src/background/messaging/rpc-methods/swap.ts | 26 +++++++++++++++++++ src/shared/rpc/methods/swap.ts | 12 +++++++++ src/shared/rpc/rpc-methods.ts | 2 ++ 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/background/messaging/rpc-methods/swap.ts create mode 100644 src/shared/rpc/methods/swap.ts diff --git a/src/background/messaging/messaging-utils.ts b/src/background/messaging/messaging-utils.ts index e9422d73f56..82b51989128 100644 --- a/src/background/messaging/messaging-utils.ts +++ b/src/background/messaging/messaging-utils.ts @@ -1,6 +1,9 @@ +import type { To } from 'react-router-dom'; + import { InternalMethods } from '@shared/message-types'; import { sendMessage } from '@shared/messages'; import { RouteUrls } from '@shared/route-urls'; +import type { SwapRequestParams } from '@shared/rpc/methods/swap'; import { popup } from '@background/popup'; @@ -16,7 +19,7 @@ function getOriginFromPort(port: chrome.runtime.Port) { // // Playwright does not currently support Chrome extension popup testing: // https://github.com/microsoft/playwright/issues/5593 -async function openRequestInFullPage(path: string, urlParams: URLSearchParams) { +async function openRequestInFullPage(path: string | To, urlParams: URLSearchParams) { return chrome.tabs.create({ url: chrome.runtime.getURL(`index.html#${path}?${urlParams.toString()}`), }); @@ -69,3 +72,10 @@ export async function triggerRequestWindowOpen(path: RouteUrls, urlParams: URLSe if (IS_TEST_ENV) return openRequestInFullPage(path, urlParams); return popup({ url: `/popup.html#${path}?${urlParams.toString()}` }); } + +export async function triggerSwapWindowOpen(path: To, urlParams: URLSearchParams) { + console.log('triggerSwapWindowOpen', path, urlParams); + + if (IS_TEST_ENV) return openRequestInFullPage(path, urlParams); + return popup({ url: `/index.html#${path}?${urlParams.toString()}` }); +} diff --git a/src/background/messaging/rpc-message-handler.ts b/src/background/messaging/rpc-message-handler.ts index b6ad1407ad0..375dcbf1aa8 100644 --- a/src/background/messaging/rpc-message-handler.ts +++ b/src/background/messaging/rpc-message-handler.ts @@ -4,6 +4,7 @@ import { WalletRequests, makeRpcErrorResponse } from '@shared/rpc/rpc-methods'; import { queueAnalyticsRequest } from '@background/background-analytics'; import { rpcSignStacksTransaction } from '@background/messaging/rpc-methods/sign-stacks-transaction'; +import { rpcSwap } from '@background/messaging/rpc-methods/swap'; import { getTabIdFromPort, listenForOriginTabClose } from './messaging-utils'; import { rpcGetAddresses } from './rpc-methods/get-addresses'; @@ -22,6 +23,10 @@ export async function rpcMessageHandler(message: WalletRequests, port: chrome.ru await rpcOpen(message, port); break; } + case 'swap': { + await rpcSwap(message, port); + break; + } case 'getAddresses': { await rpcGetAddresses(message, port); break; diff --git a/src/background/messaging/rpc-methods/swap.ts b/src/background/messaging/rpc-methods/swap.ts new file mode 100644 index 00000000000..9b59a4691ee --- /dev/null +++ b/src/background/messaging/rpc-methods/swap.ts @@ -0,0 +1,26 @@ +import { RouteUrls } from '@shared/route-urls'; +import type { SwapRequest } from '@shared/rpc/methods/swap'; +import { makeRpcSuccessResponse } from '@shared/rpc/rpc-methods'; + +import { makeSearchParamsWithDefaults, triggerSwapWindowOpen } from '../messaging-utils'; +import { trackRpcRequestSuccess } from '../rpc-message-handler'; + +export async function rpcSwap(message: SwapRequest, port: chrome.runtime.Port) { + const { urlParams, tabId } = makeSearchParamsWithDefaults(port, [['requestId', message.id]]); + const { from = 'STX', to } = message?.params || {}; + + await triggerSwapWindowOpen( + RouteUrls.Swap.replace(':base', from).replace(':quote', to ?? ''), + urlParams + ); + + void trackRpcRequestSuccess({ endpoint: message.method }); + + chrome.tabs.sendMessage( + tabId, + makeRpcSuccessResponse('swap', { + id: message.id, + result: { message: 'Success' }, + }) + ); +} diff --git a/src/shared/rpc/methods/swap.ts b/src/shared/rpc/methods/swap.ts new file mode 100644 index 00000000000..af416071637 --- /dev/null +++ b/src/shared/rpc/methods/swap.ts @@ -0,0 +1,12 @@ +import { DefineRpcMethod, RpcRequest, RpcResponse } from '@btckit/types'; + +export interface SwapRequestParams { + from?: string; + to?: string; +} + +export type SwapRequest = RpcRequest<'swap', SwapRequestParams>; + +type SwapResponse = RpcResponse<{ message: string }>; + +export type Swap = DefineRpcMethod; diff --git a/src/shared/rpc/rpc-methods.ts b/src/shared/rpc/rpc-methods.ts index 30f3db9acdb..2374abd2767 100644 --- a/src/shared/rpc/rpc-methods.ts +++ b/src/shared/rpc/rpc-methods.ts @@ -8,10 +8,12 @@ import { Open } from './methods/open'; import { SignPsbt } from './methods/sign-psbt'; import { SignStacksMessage } from './methods/sign-stacks-message'; import { SupportedMethods } from './methods/supported-methods'; +import { Swap } from './methods/swap'; // Supports BtcKit methods, as well as custom Leather methods export type WalletMethodMap = BtcKitMethodMap & Open & + Swap & SupportedMethods & SignPsbt & SignStacksTransaction & From ca5b30c1739594f6a8edc98e4b857aec447fdcf7 Mon Sep 17 00:00:00 2001 From: Will Cameron Date: Thu, 12 Dec 2024 10:49:55 -0800 Subject: [PATCH 2/2] chore: remove log --- src/background/messaging/messaging-utils.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/background/messaging/messaging-utils.ts b/src/background/messaging/messaging-utils.ts index 82b51989128..4e36aff090f 100644 --- a/src/background/messaging/messaging-utils.ts +++ b/src/background/messaging/messaging-utils.ts @@ -74,8 +74,6 @@ export async function triggerRequestWindowOpen(path: RouteUrls, urlParams: URLSe } export async function triggerSwapWindowOpen(path: To, urlParams: URLSearchParams) { - console.log('triggerSwapWindowOpen', path, urlParams); - if (IS_TEST_ENV) return openRequestInFullPage(path, urlParams); return popup({ url: `/index.html#${path}?${urlParams.toString()}` }); }