Skip to content

Commit

Permalink
fix: revert signing logic to try both keys, closes #4645
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Dec 8, 2023
1 parent d2edb18 commit 8b1be50
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"prettier.documentSelectors": ["src/**/*.{ts,tsx}", "*.{js,json}"],
"vitest.include": ["src/**/*.spec.ts"],
"githubPullRequests.ignoredPullRequestBranches": ["dev"]
"githubPullRequests.ignoredPullRequestBranches": ["dev"],
"typescript.preferences.preferTypeOnlyAutoImports": true
}
38 changes: 18 additions & 20 deletions src/app/store/accounts/blockchain/bitcoin/bitcoin.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import {
BitcoinInputSigningConfig,
getAssumedZeroIndexSigningConfig,
} from '@shared/crypto/bitcoin/signer-config';
import { logger } from '@shared/logger';
import { allSighashTypes } from '@shared/rpc/methods/sign-psbt';
import { isNumber, makeNumberRange } from '@shared/utils';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { useWalletType } from '@app/common/use-wallet-type';
import { listenForBitcoinTxLedgerSigning } from '@app/features/ledger/flows/bitcoin-tx-signing/bitcoin-tx-signing-event-listeners';
import { useLedgerNavigate } from '@app/features/ledger/hooks/use-ledger-navigate';
Expand Down Expand Up @@ -72,7 +72,6 @@ export function useZeroIndexTaprootAddress(accIndex?: number) {
function useSignBitcoinSoftwareTx() {
const createNativeSegwitSigner = useCurrentAccountNativeSegwitSigner();
const createTaprootSigner = useCurrentAccountTaprootSigner();
const network = useCurrentNetwork();

return async (psbt: Uint8Array, inputSigningConfig: BitcoinInputSigningConfig[]) => {
const tx = btc.Transaction.fromPSBT(psbt);
Expand All @@ -85,20 +84,16 @@ function useSignBitcoinSoftwareTx() {

if (!nativeSegwitSigner || !taprootSigner) throw new Error('Signers not available');

const input = tx.getInput(index);
const addressType = getInputPaymentType(index, input, network.chain.bitcoin.bitcoinNetwork);

switch (addressType) {
case 'p2tr': {
// See #4628.
// Our API doesn't support users specifying which key they want to sign
// with. Until we support this, we sign with both, as in some cases, e.g.
// Asigna, the Native Segwit key is used to sign a multisig taproot input
try {
nativeSegwitSigner.signIndex(tx, index, allSighashTypes);
} catch (e) {
try {
taprootSigner.signIndex(tx, index, allSighashTypes);
break;
}
case 'p2wpkh': {
nativeSegwitSigner.signIndex(tx, index, allSighashTypes);
break;
}
default:
logger.warn('Cannot sign input of address type ' + addressType);
} catch (er) {}
}
});

Expand Down Expand Up @@ -205,18 +200,21 @@ export function useSignLedgerBitcoinTx() {

export function useAddTapInternalKeysIfMissing() {
const createTaprootSigner = useCurrentAccountTaprootSigner();
return (tx: btc.Transaction, inputIndexes: BitcoinInputSigningConfig[]) => {
const analytics = useAnalytics();
return (tx: btc.Transaction, inputIndexes: BitcoinInputSigningConfig[]) =>
inputIndexes.forEach(({ index, derivationPath }) => {
const taprootSigner = createTaprootSigner?.(extractAddressIndexFromPath(derivationPath));
if (!taprootSigner) throw new Error('Taproot signer not found');
const input = tx.getInput(index);

const witnessOutputScript =
input.witnessUtxo?.script && btc.OutScript.decode(input.witnessUtxo.script);

if (taprootSigner && witnessOutputScript?.type === 'tr' && !input.tapInternalKey)
if (taprootSigner && witnessOutputScript?.type === 'tr' && !input.tapInternalKey) {
void analytics.track('psbt_sign_request_p2tr_missing_taproot_internal_key');
tx.updateInput(index, { ...input, tapInternalKey: taprootSigner.payment.tapInternalKey });
}
});
};
}

export function useGetAssumedZeroIndexSigningConfig() {
Expand All @@ -238,8 +236,8 @@ export function useSignBitcoinTx() {
const getDefaultSigningConfig = useGetAssumedZeroIndexSigningConfig();

/**
* Don't forget to finalize the tx once it's returned. You can broadcast with
* the hex value from `tx.hex`.
* Bitcoin signing function. Don't forget to finalize the tx once it's
* returned. You can broadcast with the hex value from `tx.hex`.
*/
return (psbt: Uint8Array, inputsToSign?: BitcoinInputSigningConfig[] | number[]) => {
function getSigningConfig(inputsToSign?: BitcoinInputSigningConfig[] | number[]) {
Expand Down

0 comments on commit 8b1be50

Please sign in to comment.