Skip to content

Commit

Permalink
refactor: rpc package usage
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Feb 14, 2025
1 parent 463604c commit 52b0bdf
Show file tree
Hide file tree
Showing 35 changed files with 459 additions and 443 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,11 @@
"@leather.io/crypto": "1.6.35",
"@leather.io/models": "0.25.1",
"@leather.io/query": "2.27.0",
"@leather.io/rpc": "2.5.12",
"@leather.io/stacks": "1.5.20",
"@leather.io/tokens": "0.12.11",
"@leather.io/ui": "1.48.3",
"@leather.io/utils": "0.26.0",
"@leather.io/utils": "0.27.0",
"@ledgerhq/hw-transport-webusb": "6.27.19",
"@noble/hashes": "1.5.0",
"@noble/secp256k1": "2.1.0",
Expand Down Expand Up @@ -268,7 +269,6 @@
"@leather.io/eslint-config": "0.7.0",
"@leather.io/panda-preset": "0.8.10",
"@leather.io/prettier-config": "0.6.0",
"@leather.io/rpc": "2.5.7",
"@ls-lint/ls-lint": "2.2.3",
"@mdx-js/loader": "3.0.0",
"@pandacss/dev": "0.46.1",
Expand Down
455 changes: 249 additions & 206 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions src/app/pages/rpc-get-addresses/use-get-addresses.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { bytesToHex } from '@stacks/common';

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

import { logger } from '@shared/logger';
import { makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';
import { analytics } from '@shared/utils/analytics';

import { focusTabAndWindow } from '@app/common/focus-tab';
Expand Down Expand Up @@ -81,7 +80,7 @@ export function useGetAddresses() {

chrome.tabs.sendMessage(
tabId,
makeRpcSuccessResponse('getAddresses', {
createRpcSuccessResponse('getAddresses', {
id: requestId,
result: { addresses: keysToIncludeInResponse as any },
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { useNavigate } from 'react-router-dom';

import type { Money } from '@leather.io/models';
import { type UtxoResponseItem, useBitcoinBroadcastTransaction } from '@leather.io/query';
import { createRpcSuccessResponse } from '@leather.io/rpc';

import { logger } from '@shared/logger';
import type { TransferRecipient } from '@shared/models/form.model';
import { RouteUrls } from '@shared/route-urls';
import { makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';
import { closeWindow } from '@shared/utils';
import { analytics } from '@shared/utils/analytics';

Expand Down Expand Up @@ -89,7 +89,7 @@ export function useSendTransferApproveActions({

chrome.tabs.sendMessage(
tabId ?? 0,
makeRpcSuccessResponse('sendTransfer', {
createRpcSuccessResponse('sendTransfer', {
id: requestId,
result: { txid },
})
Expand Down
12 changes: 8 additions & 4 deletions src/app/pages/rpc-sign-bip322-message/use-sign-bip322-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ 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 {
PaymentTypes,
RpcErrorCode,
createRpcErrorResponse,
createRpcSuccessResponse,
} from '@leather.io/rpc';

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

Expand Down Expand Up @@ -66,7 +70,7 @@ function useSignBip322MessageFactory({ address, signPsbt }: SignBip322MessageFac
if (!tabId) return;
chrome.tabs.sendMessage(
tabId,
makeRpcErrorResponse('signMessage', {
createRpcErrorResponse('signMessage', {
id: requestId,
error: {
code: RpcErrorCode.USER_REJECTION,
Expand Down Expand Up @@ -96,7 +100,7 @@ function useSignBip322MessageFactory({ address, signPsbt }: SignBip322MessageFac

chrome.tabs.sendMessage(
tabId,
makeRpcSuccessResponse('signMessage', {
createRpcSuccessResponse('signMessage', {
id: requestId,
result: { signature, address, message },
})
Expand Down
14 changes: 8 additions & 6 deletions src/app/pages/rpc-sign-psbt/use-rpc-sign-psbt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
useCalculateBitcoinFiatValue,
useCryptoCurrencyMarketDataMeanAverage,
} from '@leather.io/query';
import { RpcErrorCode } from '@leather.io/rpc';
import { RpcErrorCode, createRpcErrorResponse, createRpcSuccessResponse } from '@leather.io/rpc';
import {
formatMoney,
formatMoneyPadded,
Expand All @@ -19,7 +19,6 @@ import {
} from '@leather.io/utils';

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

Expand Down Expand Up @@ -70,7 +69,7 @@ export function useRpcSignPsbt() {

chrome.tabs.sendMessage(
tabId,
makeRpcSuccessResponse('signPsbt', {
createRpcSuccessResponse('signPsbt', {
id: requestId,
result: { hex: psbt, txid },
})
Expand Down Expand Up @@ -99,7 +98,7 @@ export function useRpcSignPsbt() {

chrome.tabs.sendMessage(
tabId,
makeRpcErrorResponse('signPsbt', {
createRpcErrorResponse('signPsbt', {
id: requestId,
error: { code: 4002, message: 'Failed to broadcast transaction' },
})
Expand Down Expand Up @@ -130,7 +129,10 @@ export function useRpcSignPsbt() {
if (!broadcast) {
chrome.tabs.sendMessage(
tabId,
makeRpcSuccessResponse('signPsbt', { id: requestId, result: { hex: bytesToHex(psbt) } })
createRpcSuccessResponse('signPsbt', {
id: requestId,
result: { hex: bytesToHex(psbt) },
})
);
closeWindow();
return;
Expand Down Expand Up @@ -169,7 +171,7 @@ export function useRpcSignPsbt() {
onCancel() {
chrome.tabs.sendMessage(
tabId,
makeRpcErrorResponse('signPsbt', {
createRpcErrorResponse('signPsbt', {
id: requestId,
error: {
code: RpcErrorCode.USER_REJECTION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { useMemo } from 'react';
import { networkFromName } from '@stacks/network';
import { deserializeCV } from '@stacks/transactions';

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

import { makeRpcErrorResponse, makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';
import {
isSignableMessageType,
isStructuredMessageType,
Expand Down Expand Up @@ -95,7 +94,7 @@ export function useRpcSignStacksMessage() {
onSignMessageCompleted(messageSignature) {
chrome.tabs.sendMessage(
tabId,
makeRpcSuccessResponse('stx_signMessage', {
createRpcSuccessResponse('stx_signMessage', {
id: requestId,
result: {
signature: messageSignature.signature,
Expand All @@ -113,7 +112,7 @@ export function useRpcSignStacksMessage() {
void analytics.track('request_signature_cancel');
chrome.tabs.sendMessage(
tabId,
makeRpcErrorResponse('stx_signMessage', {
createRpcErrorResponse('stx_signMessage', {
id: requestId,
error: {
message: 'User denied signing',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { useMemo } from 'react';

import { MultiSigSpendingCondition, deserializeTransaction } from '@stacks/transactions';

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

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

import { useDefaultRequestParams } from '@app/common/hooks/use-default-request-search-params';
Expand Down Expand Up @@ -58,7 +57,7 @@ export function useRpcSignStacksTransaction() {

chrome.tabs.sendMessage(
tabId,
makeRpcSuccessResponse('stx_signTransaction', {
createRpcSuccessResponse('stx_signTransaction', {
id: requestId,
result: {
txHex: signedTransaction.serialize(),
Expand All @@ -71,7 +70,7 @@ export function useRpcSignStacksTransaction() {
onCancel() {
chrome.tabs.sendMessage(
tabId,
makeRpcErrorResponse('stx_signTransaction', {
createRpcErrorResponse('stx_signTransaction', {
id: requestId,
error: {
message: 'User denied signing stacks transaction',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { useMemo } from 'react';
import { useAsync } from 'react-async-hook';

import { useNextNonce } from '@leather.io/query';
import { RpcErrorCode } from '@leather.io/rpc';
import { RpcErrorCode, createRpcErrorResponse, createRpcSuccessResponse } from '@leather.io/rpc';
import { isUndefined } from '@leather.io/utils';

import { logger } from '@shared/logger';
import { makeRpcErrorResponse, makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';
import { closeWindow } from '@shared/utils';
import {
type TransactionPayload,
Expand Down Expand Up @@ -91,7 +90,7 @@ export function useRpcStxCallContract() {

chrome.tabs.sendMessage(
tabId,
makeRpcSuccessResponse('stx_callContract', {
createRpcSuccessResponse('stx_callContract', {
id: requestId,
result: {
txid: result.txid,
Expand All @@ -104,7 +103,7 @@ export function useRpcStxCallContract() {
onCancel() {
chrome.tabs.sendMessage(
tabId,
makeRpcErrorResponse('stx_callContract', {
createRpcErrorResponse('stx_callContract', {
id: requestId,
error: {
message: 'User denied signing stacks transaction',
Expand Down
24 changes: 22 additions & 2 deletions src/app/store/accounts/blockchain/bitcoin/bitcoin-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useCallback } from 'react';

import { HDKey, Versions } from '@scure/bip32';
import * as btc from '@scure/btc-signer';
import { SigHash } from '@scure/btc-signer';

import {
BitcoinAccount,
Expand All @@ -12,10 +13,29 @@ import {
} from '@leather.io/bitcoin';
import type { BitcoinNetworkModes } from '@leather.io/models';

import { AllowedSighashTypes } from '@shared/rpc/methods/sign-psbt';

import { useBitcoinExtendedPublicKeyVersions } from './bitcoin-keychain';

enum SignatureHash {
DEFAULT = 0x00,
ALL = 0x01,
NONE = 0x02,
SINGLE = 0x03,
ALL_ANYONECANPAY = 0x81,
NONE_ANYONECANPAY = 0x82,
SINGLE_ANYONECANPAY = 0x83,
}
export const allSighashTypes = [
SigHash.DEFAULT,
SignatureHash.ALL,
SignatureHash.NONE,
SignatureHash.SINGLE,
SigHash.ALL_ANYONECANPAY,
SignatureHash.ALL_ANYONECANPAY,
SignatureHash.NONE_ANYONECANPAY,
SignatureHash.SINGLE_ANYONECANPAY,
];
type AllowedSighashTypes = SignatureHash | SigHash;

export interface Signer<Payment> {
network: BitcoinNetworkModes;
payment: Payment;
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
Expand Up @@ -21,7 +21,6 @@ import {
BitcoinInputSigningConfig,
getAssumedZeroIndexSigningConfig,
} from '@shared/crypto/bitcoin/signer-config';
import { allSighashTypes } from '@shared/rpc/methods/sign-psbt';
import { analytics } from '@shared/utils/analytics';

import { useWalletType } from '@app/common/use-wallet-type';
Expand All @@ -40,6 +39,7 @@ import {
import { useCurrentNetwork } from '@app/store/networks/networks.selectors';

import { useCurrentAccountIndex } from '../../account';
import { allSighashTypes } from './bitcoin-signer';
import {
useCurrentAccountNativeSegwitSigner,
useCurrentNativeSegwitAccount,
Expand Down
5 changes: 3 additions & 2 deletions src/background/background.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//
// This file is the entrypoint to the extension's background script
// https://developer.chrome.com/docs/extensions/mv3/architecture-overview/#background_script
import type { RpcRequests } from '@leather.io/rpc';

import { logger } from '@shared/logger';
import { CONTENT_SCRIPT_PORT, type LegacyMessageFromContentScript } from '@shared/message-types';
import { WalletRequests } from '@shared/rpc/rpc-methods';
import { warnUsersAboutDevToolsDangers } from '@shared/utils/dev-tools-warning-log';

import { initContextMenuActions } from './init-context-menus';
Expand All @@ -29,7 +30,7 @@ chrome.runtime.onInstalled.addListener(async details => {
chrome.runtime.onConnect.addListener(port => {
if (port.name !== CONTENT_SCRIPT_PORT) return;

port.onMessage.addListener((message: LegacyMessageFromContentScript | WalletRequests, port) => {
port.onMessage.addListener((message: LegacyMessageFromContentScript | RpcRequests, port) => {
if (!port.sender?.tab?.id)
return logger.error('Message reached background script without a corresponding tab');

Expand Down
4 changes: 1 addition & 3 deletions src/background/messaging/messaging-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ export function getStxDefaultMessageParamsToTransactionRequest(
jsonTxRequest.nonce = params.nonce;
}
if ('postConditions' in params && isDefined(params.postConditions)) {
jsonTxRequest.postConditions = encodePostConditions(
params.postConditions as PostConditionWire[]
);
jsonTxRequest.postConditions = params.postConditions;
}
if ('postConiditionMode' in params && isDefined(params.postConditionMode)) {
jsonTxRequest.postConditionMode = params.postConditionMode;
Expand Down
Loading

0 comments on commit 52b0bdf

Please sign in to comment.