Skip to content

Commit

Permalink
chore: add open API, ref leather-io/issues#5800
Browse files Browse the repository at this point in the history
  • Loading branch information
pete-watters committed Aug 28, 2024
1 parent 523d3ca commit 3df62f7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/background/messaging/rpc-message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions src/background/messaging/rpc-methods/open.ts
Original file line number Diff line number Diff line change
@@ -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',
},
}),
});
}
4 changes: 4 additions & 0 deletions src/background/messaging/rpc-methods/supported-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
14 changes: 14 additions & 0 deletions src/shared/rpc/methods/open.ts
Original file line number Diff line number Diff line change
@@ -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<typeof rpcOpenParamsSchema>;

export type OpenRequest = RpcRequest<'open', OpenRequestParams>;

type OpenResponse = RpcResponse<Response>;

export type Open = DefineRpcMethod<OpenRequest, OpenResponse>;
2 changes: 2 additions & 0 deletions src/shared/rpc/rpc-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &
Expand Down

0 comments on commit 3df62f7

Please sign in to comment.