Skip to content

Commit

Permalink
chore: refactor psbt software logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Nov 8, 2023
1 parent ef651c2 commit 3818345
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 37 deletions.
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"prettier.documentSelectors": ["src/**/*.{ts,tsx}", "*.{js,json}"],
"vitest.include": ["src/**/*.spec.ts"],
"githubPullRequests.ignoredPullRequestBranches": ["dev"],
"editor.folding": false
"githubPullRequests.ignoredPullRequestBranches": ["dev"]
}
25 changes: 11 additions & 14 deletions src/app/common/psbt/use-psbt-request-params.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { useMemo } from 'react';
import { useSearchParams } from 'react-router-dom';

import { ensureArray, undefinedIfLengthZero } from '@shared/utils';

import { useRejectIfLedgerWallet } from '@app/common/rpc-helpers';

import { useDefaultRequestParams } from '../hooks/use-default-request-search-params';
import { initialSearchParams } from '../initial-search-params';
import { getPsbtPayloadFromToken } from './requests';

export function usePsbtRequestSearchParams() {
const [searchParams] = useSearchParams();
const { origin, tabId } = useDefaultRequestParams();
const requestToken = searchParams.get('request');
const requestToken = initialSearchParams.get('request');

if (!requestToken) throw new Error('Cannot decode psbt without request token');

Expand All @@ -33,21 +30,21 @@ export function usePsbtRequestSearchParams() {
}

export function useRpcSignPsbtParams() {
const [searchParams] = useSearchParams();
const { origin, tabId } = useDefaultRequestParams();
const broadcast = searchParams.get('broadcast');
const psbtHex = searchParams.get('hex');
const requestId = searchParams.get('requestId');
const signAtIndex = searchParams.getAll('signAtIndex');
const broadcast = initialSearchParams.get('broadcast');
const psbtHex = initialSearchParams.get('hex');
const requestId = initialSearchParams.get('requestId');
const signAtIndex = initialSearchParams.getAll('signAtIndex');

return useMemo(() => {
return {
return useMemo(
() => ({
broadcast: broadcast === 'true',
origin,
psbtHex,
requestId,
signAtIndex: undefinedIfLengthZero(ensureArray(signAtIndex).map(h => Number(h))),
tabId: tabId ?? 0,
};
}, [broadcast, origin, psbtHex, requestId, signAtIndex, tabId]);
}),
[broadcast, origin, psbtHex, requestId, signAtIndex, tabId]
);
}
5 changes: 2 additions & 3 deletions src/app/features/psbt-signer/hooks/use-psbt-signer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { hexToBytes } from '@noble/hashes/utils';
import * as btc from '@scure/btc-signer';

import { logger } from '@shared/logger';
import { allSighashTypes } from '@shared/rpc/methods/sign-psbt';
import { isString, isUndefined } from '@shared/utils';
import { isString } from '@shared/utils';

import {
useAddTapInternalKeysIfMissing,
Expand All @@ -31,7 +30,7 @@ export function usePsbtSigner() {
return useMemo(
() => ({
async signPsbt({ indexesToSign, tx }: SignPsbtArgs) {
if (indexesToSign) addMissingTapInteralKeys(tx, indexesToSign);
addMissingTapInteralKeys(tx, indexesToSign);
return signBitcoinTx(tx.toPSBT());
// inputs.forEach((input, idx) => {
// const isSigning = isUndefined(indexesToSign) || indexesToSign.includes(idx);
Expand Down
21 changes: 13 additions & 8 deletions src/app/pages/rpc-sign-psbt/rpc-sign-psbt.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Outlet } from 'react-router-dom';

import { PsbtSigner } from '@app/features/psbt-signer/psbt-signer';

import { useRpcSignPsbt } from './use-rpc-sign-psbt';
Expand All @@ -6,13 +8,16 @@ export function RpcSignPsbt() {
const { indexesToSign, isBroadcasting, onSignPsbt, onCancel, origin, psbtHex } = useRpcSignPsbt();

return (
<PsbtSigner
indexesToSign={indexesToSign}
isBroadcasting={isBroadcasting}
origin={origin}
onSignPsbt={onSignPsbt}
onCancel={onCancel}
psbtHex={psbtHex}
/>
<>
<PsbtSigner
indexesToSign={indexesToSign}
isBroadcasting={isBroadcasting}
origin={origin}
onSignPsbt={onSignPsbt}
onCancel={onCancel}
psbtHex={psbtHex}
/>
<Outlet />
</>
);
}
7 changes: 4 additions & 3 deletions src/app/pages/rpc-sign-psbt/use-rpc-sign-psbt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function useRpcSignPsbt() {
const btcMarketData = useCryptoCurrencyMarketData('BTC');
const calculateBitcoinFiatValue = useCalculateBitcoinFiatValue();

if (!requestId || !psbtHex || !origin) throw new Error('Invalid params');
if (!requestId || !psbtHex || !origin) throw new Error('Invalid params in useRpcSignPsbt');

async function broadcastSignedPsbtTx({
addressNativeSegwitTotal,
Expand Down Expand Up @@ -86,12 +86,13 @@ export function useRpcSignPsbt() {
isBroadcasting,
origin,
psbtHex,
async onSignPsbt({ addressNativeSegwitTotal, addressTaprootTotal, fee, inputs }: SignPsbtArgs) {
async onSignPsbt({ addressNativeSegwitTotal, addressTaprootTotal, fee }: SignPsbtArgs) {
const tx = getPsbtAsTransaction(psbtHex);

try {
await signPsbt({ indexesToSign: signAtIndex, inputs, tx });
await signPsbt({ tx, indexesToSign: signAtIndex });
} catch (e) {
console.log(e);

Check failure on line 95 in src/app/pages/rpc-sign-psbt/use-rpc-sign-psbt.tsx

View workflow job for this annotation

GitHub Actions / lint-eslint

Unexpected console statement
return navigate(RouteUrls.RequestError, {
state: { message: e instanceof Error ? e.message : '', title: 'Failed to sign' },
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';

import { bytesToHex } from '@noble/hashes/utils';
import { Psbt } from 'bitcoinjs-lib';
import * as yup from 'yup';

Expand Down Expand Up @@ -95,6 +96,8 @@ export function useSendInscriptionForm() {
return;
}

console.log('unsigned sending psbt ', bytesToHex(resp.psbt));

Check failure on line 99 in src/app/pages/send/ordinal-inscription/hooks/use-send-inscription-form.tsx

View workflow job for this annotation

GitHub Actions / lint-eslint

Unexpected console statement

const signedTx = await sign(resp.psbt);

if (!signedTx) {
Expand Down
4 changes: 1 addition & 3 deletions src/app/routes/app-routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import {
import { RouteUrls } from '@shared/route-urls';

import { LoadingSpinner } from '@app/components/loading-spinner';
import { ActivityList } from '@app/features/activity-list/activity-list';
import { AddNetwork } from '@app/features/add-network/add-network';
import { AssetsList } from '@app/features/asset-list/asset-list';
import { Container } from '@app/features/container/container';
import { EditNonceDrawer } from '@app/features/edit-nonce-drawer/edit-nonce-drawer';
import { IncreaseBtcFeeDrawer } from '@app/features/increase-fee-drawer/increase-btc-fee-drawer';
Expand Down Expand Up @@ -81,7 +79,7 @@ function useAppRoutes() {
createRoutesFromElements(
<Route element={<Container />}>
<Route
path="/"
path="/*"
element={
<AccountGate>
<Home />
Expand Down
5 changes: 4 additions & 1 deletion src/app/routes/rpc-routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Route } from 'react-router-dom';

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

import { ledgerBitcoinTxSigningRoutes } from '@app/features/ledger/flows/bitcoin-tx-signing/ledger-bitcoin-sign-tx-container';
import { RpcGetAddresses } from '@app/pages/rpc-get-addresses/rpc-get-addresses';
import { rpcSendTransferRoutes } from '@app/pages/rpc-send-transfer/rpc-send-transfer.routes';
import { RpcSignPsbt } from '@app/pages/rpc-sign-psbt/rpc-sign-psbt';
Expand Down Expand Up @@ -35,7 +36,9 @@ export const rpcRequestRoutes = (
<RpcSignPsbt />
</AccountGate>
}
/>
>
{ledgerBitcoinTxSigningRoutes}
</Route>
<Route
path={RouteUrls.RpcSignPsbtSummary}
element={
Expand Down
6 changes: 3 additions & 3 deletions src/app/store/accounts/blockchain/bitcoin/bitcoin.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ export function useSignLedgerBitcoinTx() {

export function useAddTapInternalKeysIfMissing() {
const createTaprootSigner = useCurrentAccountTaprootSigner();
return (tx: btc.Transaction, inputIndexes: number[]) => {
return (tx: btc.Transaction, inputIndexes?: number[]) => {
const taprootSigner = createTaprootSigner?.(0);
if (!taprootSigner) throw new Error('Taproot signer not found');

inputIndexes.forEach(index => {
(inputIndexes ?? makeNumberRange(tx.inputsLength)).forEach(index => {
const input = tx.getInput(index);
const witnessOutputScript =
input.witnessUtxo?.script && btc.OutScript.decode(input.witnessUtxo.script);
Expand All @@ -201,7 +201,7 @@ export function useSignBitcoinTx() {
const signSoftwareTx = useSignBitcoinSoftwareTx();

// TODO: add support for signing specific inputs
return (psbt: Uint8Array, inputsToSign?: Record<number, string>) =>
return (psbt: Uint8Array, inputsToSign?: number[]) =>

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

View workflow job for this annotation

GitHub Actions / typecheck

'inputsToSign' is declared but its value is never read.
whenWallet({
async ledger(psbt: Uint8Array) {
ledgerNavigate.toConnectAndSignBitcoinTransactionStep(psbt);
Expand Down

0 comments on commit 3818345

Please sign in to comment.