Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convert rpc-methods to @leather.io/rpc types #6006

Merged
merged 6 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
"@leather.io/eslint-config": "0.7.0",
"@leather.io/panda-preset": "0.5.2",
"@leather.io/prettier-config": "0.6.0",
"@leather.io/rpc": "2.1.18",
"@leather.io/rpc": "2.4.0",
"@ls-lint/ls-lint": "2.2.3",
"@mdx-js/loader": "3.0.0",
"@pandacss/dev": "0.46.1",
Expand All @@ -297,7 +297,7 @@
"@svgr/webpack": "8.1.0",
"@types/argon2-browser": "1.18.4",
"@types/bn.js": "5.1.6",
"@types/chrome": "0.0.273",
"@types/chrome": "0.0.287",
"@types/dompurify": "3.0.5",
"@types/download": "8.0.5",
"@types/html-webpack-plugin": "3.2.9",
Expand Down
36 changes: 28 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/app/pages/rpc-get-addresses/use-get-addresses.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BtcAddress } from '@btckit/types';
import { bytesToHex } from '@stacks/common';

import { ecdsaPublicKeyToSchnorr } from '@leather.io/bitcoin';
import type { BtcAddress } from '@leather.io/rpc';

import { logger } from '@shared/logger';
import { makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useMemo, useState } from 'react';

import { PaymentTypes, RpcErrorCode } from '@btckit/types';
import * as btc from '@scure/btc-signer';
import * as bitcoin from 'bitcoinjs-lib';

import { signBip322MessageSimple } from '@leather.io/bitcoin';
import { PaymentTypes, RpcErrorCode } from '@leather.io/rpc';

import { logger } from '@shared/logger';
import { makeRpcErrorResponse, makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useMemo } from 'react';

import { RpcErrorCode } from '@btckit/types';
import { StacksNetwork } from '@stacks/network';
import { deserializeCV } from '@stacks/transactions';

import { RpcErrorCode } from '@leather.io/rpc';

import { makeRpcErrorResponse, makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';
import {
isSignableMessageType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useMemo } from 'react';

import { RpcErrorCode } from '@btckit/types';
import { bytesToHex } from '@stacks/common';
import { MultiSigSpendingCondition, deserializeTransaction } from '@stacks/transactions';

import { RpcErrorCode } from '@leather.io/rpc';

import { makeRpcErrorResponse, makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';
import { closeWindow } from '@shared/utils';

Expand Down
2 changes: 1 addition & 1 deletion src/app/store/accounts/blockchain/bitcoin/bitcoin.hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { PaymentTypes } from '@btckit/types';
import { bytesToHex } from '@noble/hashes/utils';
import * as btc from '@scure/btc-signer';
import { Psbt } from 'bitcoinjs-lib';
Expand All @@ -13,6 +12,7 @@ import {
} from '@leather.io/bitcoin';
import { extractAddressIndexFromPath } from '@leather.io/crypto';
import { bitcoinNetworkToNetworkMode } from '@leather.io/models';
import { PaymentTypes } from '@leather.io/rpc';
import { isNumber, isUndefined } from '@leather.io/utils';

import {
Expand Down
9 changes: 8 additions & 1 deletion src/background/messaging/messaging-utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { To } from 'react-router-dom';

import { InternalMethods } from '@shared/message-types';
import { sendMessage } from '@shared/messages';
import { RouteUrls } from '@shared/route-urls';
Expand All @@ -16,7 +18,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()}`),
});
Expand Down Expand Up @@ -69,3 +71,8 @@ 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) {
if (IS_TEST_ENV) return openRequestInFullPage(path, urlParams);
return popup({ url: `/swap.html#${path}?${urlParams.toString()}` });
}
7 changes: 6 additions & 1 deletion src/background/messaging/rpc-message-handler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { RpcErrorCode } from '@btckit/types';
import { RpcErrorCode } from '@leather.io/rpc';

import { WalletRequests, makeRpcErrorResponse } from '@shared/rpc/rpc-methods';

import { queueAnalyticsRequest } from '@background/background-analytics';
import { rpcSwap } from '@background/messaging/rpc-methods/open-swap';
import { rpcSignStacksTransaction } from '@background/messaging/rpc-methods/sign-stacks-transaction';

import { getTabIdFromPort, listenForOriginTabClose } from './messaging-utils';
Expand All @@ -22,6 +23,10 @@ export async function rpcMessageHandler(message: WalletRequests, port: chrome.ru
await rpcOpen(message, port);
break;
}
case 'openSwap': {
await rpcSwap(message, port);
break;
}
case 'getAddresses': {
await rpcGetAddresses(message, port);
break;
Expand Down
2 changes: 1 addition & 1 deletion src/background/messaging/rpc-methods/get-addresses.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GetAddressesRequest, RpcErrorCode } from '@btckit/types';
import { type GetAddressesRequest, RpcErrorCode } from '@leather.io/rpc';

import { RouteUrls } from '@shared/route-urls';
import { makeRpcErrorResponse } from '@shared/rpc/rpc-methods';
Expand Down
27 changes: 27 additions & 0 deletions src/background/messaging/rpc-methods/open-swap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { OpenSwapRequest } from '@leather.io/rpc';

import { RouteUrls } from '@shared/route-urls';
import { makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';

import { makeSearchParamsWithDefaults, triggerSwapWindowOpen } from '../messaging-utils';
import { trackRpcRequestSuccess } from '../rpc-message-handler';

export async function rpcSwap(message: OpenSwapRequest, port: chrome.runtime.Port) {
const { urlParams, tabId } = makeSearchParamsWithDefaults(port, [['requestId', message.id]]);
const { base = 'STX', quote } = message?.params || {};

await triggerSwapWindowOpen(
RouteUrls.Swap.replace(':base', base).replace(':quote', quote ?? ''),
urlParams
);

void trackRpcRequestSuccess({ endpoint: message.method });

chrome.tabs.sendMessage(
tabId,
makeRpcSuccessResponse('openSwap', {
id: message.id,
result: { message: 'Success' },
})
);
}
3 changes: 2 additions & 1 deletion src/background/messaging/rpc-methods/open.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { OpenRequest } from '@leather.io/rpc';

import { RouteUrls } from '@shared/route-urls';
import { OpenRequest } from '@shared/rpc/methods/open';
import { makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';

import { makeSearchParamsWithDefaults, triggerRequestWindowOpen } from '../messaging-utils';
Expand Down
15 changes: 7 additions & 8 deletions src/background/messaging/rpc-methods/send-transfer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { RpcErrorCode, type RpcRequest, type SendTransferRequestParams } from '@btckit/types';

import {
RpcErrorCode,
type RpcSendTransferParams,
type RpcSendTransferParamsLegacy,
type SendTransferRequest,
} from '@leather.io/rpc';
import { isUndefined } from '@leather.io/utils';

import { RouteUrls } from '@shared/route-urls';
import {
type RpcSendTransferParams,
type RpcSendTransferParamsLegacy,
convertRpcSendTransferLegacyParamsToNew,
defaultRpcSendTransferNetwork,
getRpcSendTransferParamErrors,
Expand All @@ -23,10 +25,7 @@ import {
} from '../messaging-utils';
import { trackRpcRequestError, trackRpcRequestSuccess } from '../rpc-message-handler';

export async function rpcSendTransfer(
message: RpcRequest<'sendTransfer', RpcSendTransferParams | SendTransferRequestParams>,
port: chrome.runtime.Port
) {
export async function rpcSendTransfer(message: SendTransferRequest, port: chrome.runtime.Port) {
if (isUndefined(message.params)) {
void trackRpcRequestError({ endpoint: 'sendTransfer', error: 'Undefined parameters' });

Expand Down
4 changes: 1 addition & 3 deletions src/background/messaging/rpc-methods/sign-message.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { PaymentTypes, RpcErrorCode } from '@btckit/types';
import { SignMessageRequest } from '@btckit/types/dist/types/methods/sign-message';

import { isSupportedMessageSigningPaymentType } from '@leather.io/bitcoin';
import { type PaymentTypes, RpcErrorCode, type SignMessageRequest } from '@leather.io/rpc';
import { isDefined, isUndefined } from '@leather.io/utils';

import { RouteUrls } from '@shared/route-urls';
Expand Down
2 changes: 1 addition & 1 deletion src/background/messaging/rpc-methods/sign-psbt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RpcErrorCode } from '@btckit/types';
import * as btc from '@scure/btc-signer';
import { hexToBytes } from '@stacks/common';

import { RpcErrorCode } from '@leather.io/rpc';
import { ensureArray, isDefined, isUndefined } from '@leather.io/utils';

import { RouteUrls } from '@shared/route-urls';
Expand Down
15 changes: 10 additions & 5 deletions src/background/messaging/rpc-methods/sign-stacks-message.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { RpcErrorCode } from '@btckit/types';

import {
RpcErrorCode,
type StxSignMessageRequest,
type StxSignMessageRequestParamsStructured,
} from '@leather.io/rpc';
import { isDefined, isUndefined } from '@leather.io/utils';

import { RouteUrls } from '@shared/route-urls';
import {
SignStacksMessageRequest,
getRpcSignStacksMessageParamErrors,
validateRpcSignStacksMessageParams,
} from '@shared/rpc/methods/sign-stacks-message';
Expand All @@ -20,7 +22,7 @@ import {
import { trackRpcRequestError, trackRpcRequestSuccess } from '../rpc-message-handler';

export async function rpcSignStacksMessage(
message: SignStacksMessageRequest,
message: StxSignMessageRequest,
port: chrome.runtime.Port
) {
if (isUndefined(message.params)) {
Expand Down Expand Up @@ -63,7 +65,10 @@ export async function rpcSignStacksMessage(
}

if (isDefined(message.params.domain)) {
requestParams.push(['domain', message.params.domain.toString()]);
requestParams.push([
'domain',
(message.params as StxSignMessageRequestParamsStructured).domain.toString(),
]);
}

const { urlParams, tabId } = makeSearchParamsWithDefaults(port, requestParams);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { RpcErrorCode } from '@btckit/types';
import { bytesToHex } from '@stacks/common';
import { TransactionTypes } from '@stacks/connect';
import {
Expand All @@ -17,11 +16,11 @@ import {
} from '@stacks/transactions';
import { createUnsecuredToken } from 'jsontokens';

import { RpcErrorCode, type StxSignTransactionRequest } from '@leather.io/rpc';
import { isDefined, isUndefined } from '@leather.io/utils';

import { RouteUrls } from '@shared/route-urls';
import {
SignStacksTransactionRequest,
getRpcSignStacksTransactionParamErrors,
validateRpcSignStacksTransactionParams,
} from '@shared/rpc/methods/sign-stacks-transaction';
Expand Down Expand Up @@ -106,7 +105,7 @@ function validateStacksTransaction(txHex: string) {
}

export async function rpcSignStacksTransaction(
message: SignStacksTransactionRequest,
message: StxSignTransactionRequest,
port: chrome.runtime.Port
) {
if (isUndefined(message.params)) {
Expand Down Expand Up @@ -137,7 +136,7 @@ export async function rpcSignStacksTransaction(
return;
}

if (!validateStacksTransaction(message.params.txHex!)) {
if (!validateStacksTransaction(message.params.txHex)) {
void trackRpcRequestError({ endpoint: message.method, error: 'Invalid Stacks transaction' });

chrome.tabs.sendMessage(
Expand All @@ -150,7 +149,7 @@ export async function rpcSignStacksTransaction(
return;
}

const stacksTransaction = deserializeTransaction(message.params.txHex!);
const stacksTransaction = deserializeTransaction(message.params.txHex);
const request = transactionPayloadToTransactionRequest(
stacksTransaction,
message.params.stxAddress,
Expand Down
Loading
Loading