diff --git a/src/background/messaging/rpc-message-handler.ts b/src/background/messaging/rpc-message-handler.ts index f863fbef2a8..0ab9862919c 100644 --- a/src/background/messaging/rpc-message-handler.ts +++ b/src/background/messaging/rpc-message-handler.ts @@ -7,6 +7,7 @@ import { rpcSignStacksTransaction } from '@background/messaging/rpc-methods/sign import { getTabIdFromPort } from './messaging-utils'; import { rpcGetAddresses } from './rpc-methods/get-addresses'; +import { rpcOpen } from './rpc-methods/open'; import { rpcSendTransfer } from './rpc-methods/send-transfer'; import { rpcSignMessage } from './rpc-methods/sign-message'; import { rpcSignPsbt } from './rpc-methods/sign-psbt'; @@ -15,6 +16,10 @@ import { rpcSupportedMethods } from './rpc-methods/supported-methods'; export async function rpcMessageHandler(message: WalletRequests, port: chrome.runtime.Port) { switch (message.method) { + case 'open': { + await rpcOpen(message, port); + break; + } case 'getAddresses': { await rpcGetAddresses(message, port); break; diff --git a/src/background/messaging/rpc-methods/open.ts b/src/background/messaging/rpc-methods/open.ts new file mode 100644 index 00000000000..fcd7cd3f864 --- /dev/null +++ b/src/background/messaging/rpc-methods/open.ts @@ -0,0 +1,30 @@ +import { RpcErrorCode } from '@btckit/types'; + +import { RouteUrls } from '@shared/route-urls'; +import { OpenRequest } from '@shared/rpc/methods/open'; +import { makeRpcErrorResponse } from '@shared/rpc/rpc-methods'; + +import { + listenForPopupClose, + makeSearchParamsWithDefaults, + triggerRequestWindowOpen, +} from '../messaging-utils'; +import { trackRpcRequestSuccess } from '../rpc-message-handler'; + +export async function rpcOpen(message: OpenRequest, port: chrome.runtime.Port) { + const { urlParams, tabId } = makeSearchParamsWithDefaults(port, [['requestId', message.id]]); + const { id } = await triggerRequestWindowOpen(RouteUrls.Home, urlParams); + void trackRpcRequestSuccess({ endpoint: message.method }); + + listenForPopupClose({ + tabId, + id, + response: makeRpcErrorResponse('open', { + id: message.id, + error: { + code: RpcErrorCode.USER_REJECTION, + message: 'User rejected request to open wallet', + }, + }), + }); +} diff --git a/src/background/messaging/rpc-methods/supported-methods.ts b/src/background/messaging/rpc-methods/supported-methods.ts index 52f3ff57bf0..36de24fad79 100644 --- a/src/background/messaging/rpc-methods/supported-methods.ts +++ b/src/background/messaging/rpc-methods/supported-methods.ts @@ -12,6 +12,10 @@ export function rpcSupportedMethods(message: SupportedMethodsRequest, port: chro result: { documentation: 'https://leather.gitbook.io/developers/home/welcome', methods: [ + { + name: 'open', + docsUrl: ['https://leather.gitbook.io/developers/bitcoin/connect-users/open-wallet'], + }, { name: 'getAddresses', docsUrl: [ diff --git a/src/shared/rpc/methods/open.ts b/src/shared/rpc/methods/open.ts new file mode 100644 index 00000000000..2ee6a16d6f4 --- /dev/null +++ b/src/shared/rpc/methods/open.ts @@ -0,0 +1,14 @@ +import { DefineRpcMethod, RpcRequest, RpcResponse } from '@btckit/types'; +import * as yup from 'yup'; + +const rpcOpenParamsSchema = yup.object().shape({ + url: yup.string(), +}); + +type OpenRequestParams = yup.InferType; + +export type OpenRequest = RpcRequest<'open', OpenRequestParams>; + +type OpenResponse = RpcResponse; + +export type Open = DefineRpcMethod; diff --git a/src/shared/rpc/rpc-methods.ts b/src/shared/rpc/rpc-methods.ts index 323ea8b1f54..30f3db9acdb 100644 --- a/src/shared/rpc/rpc-methods.ts +++ b/src/shared/rpc/rpc-methods.ts @@ -4,12 +4,14 @@ import type { ValueOf } from '@leather.io/models'; import { SignStacksTransaction } from '@shared/rpc/methods/sign-stacks-transaction'; +import { Open } from './methods/open'; import { SignPsbt } from './methods/sign-psbt'; import { SignStacksMessage } from './methods/sign-stacks-message'; import { SupportedMethods } from './methods/supported-methods'; // Supports BtcKit methods, as well as custom Leather methods export type WalletMethodMap = BtcKitMethodMap & + Open & SupportedMethods & SignPsbt & SignStacksTransaction &