chore: improve rpc message erroring

This commit is contained in:
kyranjamie
2023-06-06 10:44:36 +02:00
parent 6c9606370b
commit da511d6f13
4 changed files with 51 additions and 4 deletions

View File

@@ -21,7 +21,7 @@ jobs:
update-pull-request-body:
name: Add links to built extensions
# Don't run on forks
if: github.event.pull_request.head.repo.full_name == github.repository
if: github.repository == 'hirosystems/wallet'
runs-on: ubuntu-latest
needs:
- pre_run

View File

@@ -2,7 +2,11 @@ import { RpcErrorCode } from '@btckit/types';
import { isSupportedMessageSigningPaymentType } from '@shared/crypto/bitcoin/bip322/bip322-utils';
import { RouteUrls } from '@shared/route-urls';
import { WalletRequests, makeRpcErrorResponse } from '@shared/rpc/rpc-methods';
import {
WalletRequests,
makeRpcErrorResponse,
makeRpcSuccessResponse,
} from '@shared/rpc/rpc-methods';
import {
getTabIdFromPort,
@@ -118,6 +122,36 @@ export async function rpcMessageHandler(message: WalletRequests, port: chrome.ru
break;
}
case 'supportedMethods': {
const { tabId } = makeSearchParamsWithDefaults(port);
chrome.tabs.sendMessage(
tabId,
makeRpcSuccessResponse('supportedMethods', {
id: message.id,
result: {
documentation: 'https://hirowallet.gitbook.io/developers',
methods: [
{
name: 'getAddresses',
docsUrl: [
'https://hirowallet.gitbook.io/developers/bitcoin/connect-users/get-addresses',
'https://btckit.org/docs/requests/getaddresses',
],
},
{
name: 'signMessage',
docsUrl: 'https://hirowallet.gitbook.io/developers/bitcoin/sign-messages',
},
{
name: 'sendTransfer',
},
],
},
})
);
break;
}
default:
chrome.tabs.sendMessage(
getTabIdFromPort(port),
@@ -125,7 +159,7 @@ export async function rpcMessageHandler(message: WalletRequests, port: chrome.ru
id: message.id,
error: {
code: RpcErrorCode.METHOD_NOT_FOUND,
message: `${message.method} is not supported`,
message: `"${message.method}" is not supported. Try running \`.request('supportedMethods')\` to see what Hiro Wallet can do, or check out our developer documentation at https://hirowallet.gitbook.io/developers`,
},
})
);

View File

@@ -0,0 +1,11 @@
import { DefineRpcMethod, RpcRequest, RpcSuccessResponse } from '@btckit/types';
// Demo method used to serve as example while we only have a single method
type SupportedMethodsRequest = RpcRequest<'supportedMethods'>;
type SupportedMethodsResponse = RpcSuccessResponse<{
documentation: string;
methods: { name: string; docsUrl?: string | string[] }[];
}>;
export type SupportedMethods = DefineRpcMethod<SupportedMethodsRequest, SupportedMethodsResponse>;

View File

@@ -2,9 +2,11 @@ import { BtcKitMethodMap, ExtractErrorResponse, ExtractSuccessResponse } from '@
import { ValueOf } from '@shared/utils/type-utils';
import { SupportedMethods } from './methods/supported-methods';
import { Test } from './methods/test-method';
export type WalletMethodMap = BtcKitMethodMap & Test;
// Supports BtcKit methods, as well as custom Hiro Wallet methods
export type WalletMethodMap = BtcKitMethodMap & Test & SupportedMethods;
export type WalletRequests = ValueOf<WalletMethodMap>['request'];
export type WalletResponses = ValueOf<WalletMethodMap>['response'];