bitcoinjs-lib
bitcoinjs-lib copied to clipboard
feat(psbt): add taproot support for HD signing methods
Summary
signInputHD / signAllInputsHD (and their async variants) silently fail on taproot inputs because they only look at bip32Derivation, not tapBip32Derivation. This patch teaches the HD signing flow to recognize taproot inputs and derive the correct signers.
What changed
getSignersFromHDnow detects taproot inputs viaisTaprootInput()and delegates to a newgetTaprootSignersFromHDhelper- Key-path spend (
leafHashesis empty): derives the child key fromtapBip32Derivation, then tweaks it withtapTweakHash(internalKey, tapMerkleRoot)— the tweaked signer produces a validtapKeySig - Script-path spend (
leafHashesis non-empty): derives the child key untweaked and hands it off to the existing_signTaprootInputmachinery, which matches the pubkey againsttapLeafScriptentries sighashTypesparameter on all four HD methods changed from= [SIGHASH_ALL]tooptional— this removes a conflict where taproot'sSIGHASH_DEFAULT (0x00)was being rejected by a hardcodedSIGHASH_ALLfilter. Inner methods (_signInput/_signTaprootInput) already default correctly per input typeHDSigner/HDSignerAsyncinterfaces extended with optionalsignSchnorr?andtweak?— no breaking change for existing implementations
Why this approach
PR #2329 tackled the same problem. This takes a slightly different approach:
- Only modifies
ts_src/psbt.ts— compiledsrc/cjs/andsrc/esm/output is generated by the build, not hand-edited - Cleaner filtering —
.filter()instead of.map().filter()for fingerprint matching - Avoids redundant
checkForInputcall — passes the already-resolved input directly togetTaprootSignersFromHD - Uses
Uint8Arrayconsistently in tests (v7-compatible)
Test plan
- [x] Key-path signing with
signInputHD - [x] Key-path signing with merkle root present (script tree exists, but signing via key path)
- [x] Script-path signing with
signInputHD - [x] Mixed taproot + legacy (P2WPKH) inputs with
signAllInputsHD - [x] Error: missing
tapBip32Derivation - [x] Error: fingerprint mismatch
- [x] Error: pubkey / derivation path mismatch
- [x] All 2667 existing tests still pass
Closes #2132
@jasonandjay @junderw Would you check this PR?