From 0ff970be3407d4beec02a59e62bf56833ebfaa59 Mon Sep 17 00:00:00 2001 From: fbwoolf Date: Wed, 30 Aug 2023 18:48:57 -0500 Subject: [PATCH] refactor: pass all sighashTypes to signer --- .../common/psbt/use-psbt-request-params.ts | 10 +-------- .../psbt-signer/hooks/use-parsed-inputs.tsx | 15 +++---------- .../psbt-signer/hooks/use-parsed-psbt.tsx | 11 +--------- .../psbt-signer/hooks/use-psbt-signer.tsx | 9 ++++---- src/app/features/psbt-signer/psbt-signer.tsx | 14 +----------- src/app/pages/psbt-request/psbt-request.tsx | 13 ++--------- .../pages/psbt-request/use-psbt-request.tsx | 6 ++--- src/app/pages/rpc-sign-psbt/rpc-sign-psbt.tsx | 4 +--- .../pages/rpc-sign-psbt/use-rpc-sign-psbt.tsx | 6 ++--- .../messaging/rpc-methods/sign-psbt.ts | 5 ----- src/shared/rpc/methods/sign-psbt.ts | 22 ++++++++++--------- 11 files changed, 29 insertions(+), 86 deletions(-) diff --git a/src/app/common/psbt/use-psbt-request-params.ts b/src/app/common/psbt/use-psbt-request-params.ts index c4b07238e26..b2c70671c4e 100644 --- a/src/app/common/psbt/use-psbt-request-params.ts +++ b/src/app/common/psbt/use-psbt-request-params.ts @@ -1,7 +1,6 @@ import { useMemo } from 'react'; import { useSearchParams } from 'react-router-dom'; -import { AllowedSighashTypes } from '@shared/rpc/methods/sign-psbt'; import { ensureArray, undefinedIfLengthZero } from '@shared/utils'; import { useRejectIfLedgerWallet } from '@app/common/rpc-helpers'; @@ -21,9 +20,6 @@ export function usePsbtRequestSearchParams() { return useMemo( () => ({ appName: payload?.appDetails?.name, - allowedSighash: payload?.allowedSighash - ? undefinedIfLengthZero(payload.allowedSighash.map(h => Number(h)) as AllowedSighashTypes[]) - : undefined, origin, payload, requestToken, @@ -41,7 +37,6 @@ export function useRpcSignPsbtParams() { const [searchParams] = useSearchParams(); const { origin, tabId } = useDefaultRequestParams(); - const allowedSighash = searchParams.getAll('allowedSighash'); const broadcast = searchParams.get('broadcast'); const psbtHex = searchParams.get('hex'); const requestId = searchParams.get('requestId'); @@ -49,9 +44,6 @@ export function useRpcSignPsbtParams() { return useMemo(() => { return { - allowedSighash: undefinedIfLengthZero( - allowedSighash.map(h => Number(h)) as AllowedSighashTypes[] - ), broadcast: broadcast === 'true', origin, psbtHex, @@ -59,5 +51,5 @@ export function useRpcSignPsbtParams() { signAtIndex: undefinedIfLengthZero(ensureArray(signAtIndex).map(h => Number(h))), tabId: tabId ?? 0, }; - }, [allowedSighash, broadcast, origin, psbtHex, requestId, signAtIndex, tabId]); + }, [broadcast, origin, psbtHex, requestId, signAtIndex, tabId]); } diff --git a/src/app/features/psbt-signer/hooks/use-parsed-inputs.tsx b/src/app/features/psbt-signer/hooks/use-parsed-inputs.tsx index 46c37ed20c8..f173a5ebb98 100644 --- a/src/app/features/psbt-signer/hooks/use-parsed-inputs.tsx +++ b/src/app/features/psbt-signer/hooks/use-parsed-inputs.tsx @@ -8,8 +8,7 @@ import { getBtcSignerLibNetworkConfigByMode, } from '@shared/crypto/bitcoin/bitcoin.network'; import { getAddressFromOutScript } from '@shared/crypto/bitcoin/bitcoin.utils'; -import { AllowedSighashTypes } from '@shared/rpc/methods/sign-psbt'; -import { ensureArray, isDefined, isUndefined } from '@shared/utils'; +import { isDefined, isUndefined } from '@shared/utils'; import { useOrdinalsAwareUtxoQueries } from '@app/query/bitcoin/ordinals/ordinals-aware-utxo.query'; import { useCurrentAccountNativeSegwitIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks'; @@ -45,11 +44,10 @@ function getInputValue(index: number, input: btc.TransactionInput) { } interface UseParsedInputsArgs { - allowedSighash?: AllowedSighashTypes[]; inputs: btc.TransactionInput[]; indexesToSign?: number[]; } -export function useParsedInputs({ allowedSighash, inputs, indexesToSign }: UseParsedInputsArgs) { +export function useParsedInputs({ inputs, indexesToSign }: UseParsedInputsArgs) { const network = useCurrentNetwork(); const bitcoinNetwork = getBtcSignerLibNetworkConfigByMode(network.chain.bitcoin.network); const bitcoinAddressNativeSegwit = useCurrentAccountNativeSegwitIndexZeroSigner().address; @@ -69,12 +67,6 @@ export function useParsedInputs({ allowedSighash, inputs, indexesToSign }: UsePa const canChange = isCurrentAddress && !(!input.sighashType || input.sighashType === 0 || input.sighashType === 1); - // Checks if the sighashType is allowed by the PSBT - const isAllowedToSign = - isUndefined(allowedSighash) || - ensureArray(allowedSighash).some( - type => !input.sighashType || type === input.sighashType - ); // Should we check the sighashType here before it gets to the signing lib? const toSignAll = isCurrentAddress && signAll; const toSignIndex = isCurrentAddress && !signAll && indexesToSign.includes(i); @@ -84,13 +76,12 @@ export function useParsedInputs({ allowedSighash, inputs, indexesToSign }: UsePa index: input.index, inscription: utxosWithInscriptions[i]?.inscriptions, isMutable: canChange, - toSign: isAllowedToSign && (toSignAll || toSignIndex), + toSign: toSignAll || toSignIndex, txid: input.txid ? bytesToHex(input.txid) : '', value: isDefined(input.index) ? getInputValue(input.index, input) : 0, }; }), [ - allowedSighash, bitcoinAddressNativeSegwit, bitcoinAddressTaproot, bitcoinNetwork, diff --git a/src/app/features/psbt-signer/hooks/use-parsed-psbt.tsx b/src/app/features/psbt-signer/hooks/use-parsed-psbt.tsx index 7a54a7434a0..eaaa02c03ea 100644 --- a/src/app/features/psbt-signer/hooks/use-parsed-psbt.tsx +++ b/src/app/features/psbt-signer/hooks/use-parsed-psbt.tsx @@ -2,8 +2,6 @@ import { useCallback } from 'react'; import * as btc from '@scure/btc-signer'; -import { AllowedSighashTypes } from '@shared/rpc/methods/sign-psbt'; - import { subtractMoney } from '@app/common/money/calculate-money'; import { useCurrentAccountNativeSegwitIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks'; import { useCurrentAccountTaprootIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/taproot-account.hooks'; @@ -15,22 +13,15 @@ import { usePsbtInscriptions } from './use-psbt-inscriptions'; import { usePsbtTotals } from './use-psbt-totals'; interface UseParsedPsbtArgs { - allowedSighash?: AllowedSighashTypes[]; inputs: btc.TransactionInput[]; indexesToSign?: number[]; outputs: btc.TransactionOutput[]; } -export function useParsedPsbt({ - allowedSighash, - inputs, - indexesToSign, - outputs, -}: UseParsedPsbtArgs) { +export function useParsedPsbt({ inputs, indexesToSign, outputs }: UseParsedPsbtArgs) { const network = useCurrentNetwork(); const bitcoinAddressNativeSegwit = useCurrentAccountNativeSegwitIndexZeroSigner().address; const { address: bitcoinAddressTaproot } = useCurrentAccountTaprootIndexZeroSigner(); const { isPsbtMutable, parsedInputs } = useParsedInputs({ - allowedSighash, inputs, indexesToSign, }); diff --git a/src/app/features/psbt-signer/hooks/use-psbt-signer.tsx b/src/app/features/psbt-signer/hooks/use-psbt-signer.tsx index c0c2f43dbf5..cea022001bb 100644 --- a/src/app/features/psbt-signer/hooks/use-psbt-signer.tsx +++ b/src/app/features/psbt-signer/hooks/use-psbt-signer.tsx @@ -4,7 +4,7 @@ import { hexToBytes } from '@noble/hashes/utils'; import * as btc from '@scure/btc-signer'; import { logger } from '@shared/logger'; -import { AllowedSighashTypes } from '@shared/rpc/methods/sign-psbt'; +import { allSighashTypes } from '@shared/rpc/methods/sign-psbt'; import { isString, isUndefined } from '@shared/utils'; import { useCurrentAccountNativeSegwitSigner } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks'; @@ -13,7 +13,6 @@ import { useCurrentAccountTaprootSigner } from '@app/store/accounts/blockchain/b export type RawPsbt = ReturnType; interface SignPsbtArgs { - allowedSighash?: AllowedSighashTypes[]; inputs: btc.TransactionInput[]; indexesToSign?: number[]; tx: btc.Transaction; @@ -28,7 +27,7 @@ export function usePsbtSigner() { return useMemo( () => ({ - signPsbt({ allowedSighash, inputs, indexesToSign, tx }: SignPsbtArgs) { + signPsbt({ inputs, indexesToSign, tx }: SignPsbtArgs) { inputs.forEach((input, idx) => { const isSigning = isUndefined(indexesToSign) || indexesToSign.includes(idx); @@ -44,10 +43,10 @@ export function usePsbtSigner() { } try { - nativeSegwitSigner?.signIndex(tx, idx, allowedSighash); + nativeSegwitSigner?.signIndex(tx, idx, allSighashTypes); } catch (e1) { try { - taprootSigner?.signIndex(tx, idx, allowedSighash); + taprootSigner?.signIndex(tx, idx, allSighashTypes); } catch (e2) { throw new Error(`Unable to sign PSBT at index, ${e1 ?? e2}`); } diff --git a/src/app/features/psbt-signer/psbt-signer.tsx b/src/app/features/psbt-signer/psbt-signer.tsx index 4d94f20c0ef..ba3b0479047 100644 --- a/src/app/features/psbt-signer/psbt-signer.tsx +++ b/src/app/features/psbt-signer/psbt-signer.tsx @@ -4,7 +4,6 @@ import { useNavigate } from 'react-router-dom'; import * as btc from '@scure/btc-signer'; import { RouteUrls } from '@shared/route-urls'; -import { AllowedSighashTypes } from '@shared/rpc/methods/sign-psbt'; import { useRouteHeader } from '@app/common/hooks/use-route-header'; import { SignPsbtArgs } from '@app/common/psbt/requests'; @@ -48,7 +47,6 @@ function getPsbtTxOutputs(psbtTx: btc.Transaction) { } interface PsbtSignerProps { - allowedSighash?: AllowedSighashTypes[]; indexesToSign?: number[]; isBroadcasting?: boolean; name?: string; @@ -58,16 +56,7 @@ interface PsbtSignerProps { psbtHex: string; } export function PsbtSigner(props: PsbtSignerProps) { - const { - allowedSighash, - indexesToSign, - isBroadcasting, - name, - origin, - onCancel, - onSignPsbt, - psbtHex, - } = props; + const { indexesToSign, isBroadcasting, name, origin, onCancel, onSignPsbt, psbtHex } = props; const navigate = useNavigate(); const { address: addressNativeSegwit } = useCurrentAccountNativeSegwitIndexZeroSigner(); const { address: addressTaproot } = useCurrentAccountTaprootIndexZeroSigner(); @@ -103,7 +92,6 @@ export function PsbtSigner(props: PsbtSignerProps) { psbtOutputs, shouldDefaultToAdvancedView, } = useParsedPsbt({ - allowedSighash, inputs: psbtTxInputs, indexesToSign, outputs: psbtTxOutputs, diff --git a/src/app/pages/psbt-request/psbt-request.tsx b/src/app/pages/psbt-request/psbt-request.tsx index 3253417b53f..7404a0e21a3 100644 --- a/src/app/pages/psbt-request/psbt-request.tsx +++ b/src/app/pages/psbt-request/psbt-request.tsx @@ -4,22 +4,13 @@ import { PsbtSigner } from '@app/features/psbt-signer/psbt-signer'; import { usePsbtRequest } from './use-psbt-request'; export function PsbtRequest() { - const { - allowedSighash, - appName, - indexesToSign, - isLoading, - onSignPsbt, - onDenyPsbtSigning, - origin, - psbtHex, - } = usePsbtRequest(); + const { appName, indexesToSign, isLoading, onSignPsbt, onDenyPsbtSigning, origin, psbtHex } = + usePsbtRequest(); if (isLoading) return ; return ( { return { appName, - allowedSighash, indexesToSign: signAtIndex, isLoading, getRawPsbt, @@ -43,7 +42,7 @@ export function usePsbtRequest() { const tx = getPsbtAsTransaction(payload.hex); try { - signPsbt({ allowedSighash, indexesToSign: signAtIndex, inputs, tx }); + signPsbt({ indexesToSign: signAtIndex, inputs, tx }); } catch (e) { return navigate(RouteUrls.RequestError, { state: { message: e instanceof Error ? e.message : '', title: 'Failed to sign' }, @@ -63,7 +62,6 @@ export function usePsbtRequest() { }; }, [ appName, - allowedSighash, signAtIndex, isLoading, getRawPsbt, diff --git a/src/app/pages/rpc-sign-psbt/rpc-sign-psbt.tsx b/src/app/pages/rpc-sign-psbt/rpc-sign-psbt.tsx index 3aac36a1058..dcd3cf81d88 100644 --- a/src/app/pages/rpc-sign-psbt/rpc-sign-psbt.tsx +++ b/src/app/pages/rpc-sign-psbt/rpc-sign-psbt.tsx @@ -3,12 +3,10 @@ import { PsbtSigner } from '@app/features/psbt-signer/psbt-signer'; import { useRpcSignPsbt } from './use-rpc-sign-psbt'; export function RpcSignPsbt() { - const { allowedSighash, indexesToSign, isBroadcasting, onSignPsbt, onCancel, origin, psbtHex } = - useRpcSignPsbt(); + const { indexesToSign, isBroadcasting, onSignPsbt, onCancel, origin, psbtHex } = useRpcSignPsbt(); return ( - requestParams.push(['allowedSighash', hash.toString()]) - ); - if (isDefined(message.params.broadcast)) { requestParams.push(['broadcast', message.params.broadcast.toString()]); } diff --git a/src/shared/rpc/methods/sign-psbt.ts b/src/shared/rpc/methods/sign-psbt.ts index 0af6e751b1e..745c2f4dc15 100644 --- a/src/shared/rpc/methods/sign-psbt.ts +++ b/src/shared/rpc/methods/sign-psbt.ts @@ -14,19 +14,21 @@ import { // TODO: Revisit allowedSighash type if/when fixed in btc-signer export type AllowedSighashTypes = SignatureHash | btc.SignatureHash; +// Pass all sighashTypes through as allowed to btc-signer +export const allSighashTypes = [ + btc.SignatureHash.DEFAULT, + SignatureHash.ALL, + SignatureHash.NONE, + SignatureHash.SINGLE, + btc.SignatureHash.ANYONECANPAY, + SignatureHash.ALL_ANYONECANPAY, + SignatureHash.NONE_ANYONECANPAY, + SignatureHash.SINGLE_ANYONECANPAY, +]; const rpcSignPsbtParamsSchema = yup.object().shape({ account: accountSchema, - allowedSighash: yup - .array() - .of( - yup - .mixed() - .oneOf([ - ...Object.values(SignatureHash).filter(Number.isInteger), - ...Object.values(btc.SignatureHash).filter(Number.isInteger), - ]) - ), + allowedSighash: yup.array(), broadcast: yup.boolean(), hex: yup.string().required(), network: yup.string().oneOf(Object.values(WalletDefaultNetworkConfigurationIds)),