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 1 commit
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.3.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
16 changes: 8 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';

Check failure on line 4 in src/app/pages/rpc-get-addresses/use-get-addresses.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '@leather.io/rpc' or its corresponding type declarations.

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';

Check failure on line 7 in src/app/pages/rpc-sign-bip322-message/use-sign-bip322-message.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '@leather.io/rpc' or its corresponding type declarations.

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';

Check failure on line 6 in src/app/pages/rpc-sign-stacks-message/use-rpc-sign-stacks-message.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '@leather.io/rpc' or its corresponding type declarations.

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';

Check failure on line 6 in src/app/pages/rpc-sign-stacks-transaction/use-rpc-sign-stacks-transaction.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '@leather.io/rpc' or its corresponding type declarations.

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 @@
} from '@leather.io/bitcoin';
import { extractAddressIndexFromPath } from '@leather.io/crypto';
import { bitcoinNetworkToNetworkMode } from '@leather.io/models';
import { PaymentTypes } from '@leather.io/rpc';

Check failure on line 15 in src/app/store/accounts/blockchain/bitcoin/bitcoin.hooks.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '@leather.io/rpc' or its corresponding type declarations.
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';

Check failure on line 1 in src/background/messaging/rpc-message-handler.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '@leather.io/rpc' or its corresponding type declarations.

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 @@
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';

Check failure on line 1 in src/background/messaging/rpc-methods/get-addresses.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '@leather.io/rpc' or its corresponding type declarations.

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';

Check failure on line 1 in src/background/messaging/rpc-methods/open-swap.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '@leather.io/rpc' or its corresponding type declarations.

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';

Check failure on line 1 in src/background/messaging/rpc-methods/open.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '@leather.io/rpc' or its corresponding type declarations.

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';

Check failure on line 6 in src/background/messaging/rpc-methods/send-transfer.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '@leather.io/rpc' or its corresponding type declarations.
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 @@
} 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
4 changes: 3 additions & 1 deletion src/background/messaging/rpc-methods/supported-methods.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { supportedMethodsMethodName } from '@leather.io/rpc';

import { SupportedMethodsRequest } from '@shared/rpc/methods/supported-methods';
import { makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';

Expand All @@ -7,7 +9,7 @@ export function rpcSupportedMethods(message: SupportedMethodsRequest, port: chro
const { tabId } = makeSearchParamsWithDefaults(port);
chrome.tabs.sendMessage(
tabId,
makeRpcSuccessResponse('supportedMethods', {
makeRpcSuccessResponse(supportedMethodsMethodName, {
id: message.id,
result: {
documentation: 'https://leather.gitbook.io/developers/home/welcome',
Expand Down
15 changes: 8 additions & 7 deletions src/inpage/inpage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RpcRequest } from '@btckit/types';
import type { StacksProvider } from '@stacks/connect';

import { type LeatherRpcMethodMap, type RpcParameter, RpcRequest } from '@leather.io/rpc';

import { BRANCH, COMMIT_SHA } from '@shared/environment';
import {
AuthenticationRequestEventDetails,
Expand All @@ -20,7 +21,7 @@ import {
SignatureResponseMessage,
TransactionResponseMessage,
} from '@shared/message-types';
import type { WalletMethodMap, WalletMethodNames, WalletResponses } from '@shared/rpc/rpc-methods';
import type { WalletMethodNames, WalletResponses } from '@shared/rpc/rpc-methods';

import { addLeatherToProviders } from './add-leather-to-providers';

Expand Down Expand Up @@ -244,14 +245,14 @@ const provider: LeatherProviderOverrides = {

request<T extends WalletMethodNames>(
method: T,
params?: Record<string, any>
): Promise<WalletMethodMap[T]['response']> {
const id = crypto.randomUUID();
const rpcRequest: RpcRequest<T> = {
params?: RpcParameter
): Promise<LeatherRpcMethodMap[T]['response']> {
const id: string = crypto.randomUUID();
const rpcRequest = {
jsonrpc: '2.0',
id,
method,
params,
params: params ?? {},
};
document.dispatchEvent(new CustomEvent(DomEventName.request, { detail: rpcRequest }));
return new Promise((resolve, reject) => {
Expand Down
Loading
Loading