[bug]: PSBT inputs without a sighash type are signed with hash type 0x00, making witness-v0 spends unrelayable
Summary
When a PSBT input has no PSBT_IN_SIGHASH_TYPE field, lnd signs witness-v0 inputs (P2WPKH and nested P2WPKH) with sighash type 0x00 and appends that byte to the signature. Hash type 0 is only meaningful for taproot, where it means SIGHASH_DEFAULT. For witness v0 it is undefined, and the resulting transaction is rejected by standardness checks — btcd reports invalid hash type 0x0, so wallet publishtx fails and the tx never relays.
BIP 174 says a signer should assume SIGHASH_ALL when no sighash type is specified.
This is a relay/standardness rule, not consensus, but in practice the transaction does not get out.
Confirmed on master
Originally reported against v0.21.4. Still present on master at bfe5039fb.
lnwallet/btcwallet/psbt.go:442 passes in.SighashType straight through:
func signSegWitV0(in *psbt.PInput, tx *wire.MsgTx,
sigHashes *txscript.TxSigHashes, idx int,
privKey *btcec.PrivateKey) error {
...
sig, err := txscript.RawTxInWitnessSignature(
tx, sigHashes, idx, in.WitnessUtxo.Value, subScript,
in.SighashType, privKey,
)
psbt.PInput.SighashType is a txscript.SigHashType whose zero value is 0, and psbt.NewPsbtInput initialises it to 0 explicitly. Nothing between parsing and signing normalises it — I grepped every non-test use of txscript.SigHashAll in lnwallet/, lnrpc/walletrpc/ and rpcserver.go, and none of them is on the PSBT signing path (they are all commitment/sweep/BumpFee paths that build their own SignDescriptor).
So "field absent" and "field present and set to 0" are indistinguishable at the point of signing, and both produce a 0x00 trailing byte.
Reproduction
No node needed — this reproduces against the real signing function. Dropping this into lnwallet/btcwallet/ on master:
func TestReproMissingSighashType(t *testing.T) {
sig, spendTx, prevOut, priv := signOnce(t, 0)
sigAll, _, _, _ := signOnce(t, txscript.SigHashAll)
// ... build witness from sig, run txscript.NewEngine with
// txscript.StandardVerifyFlags and execute
}
where signOnce builds a P2WPKH prevout, puts it in a psbt.PInput with the given SighashType, and calls signSegWitV0 directly. Output:
no sighash field: trailing byte = 0x00
SIGHASH_ALL: trailing byte = 0x01
engine REJECTS the 0x00-signed witness: invalid hash type 0x0
--- PASS: TestReproMissingSighashType (0.00s)
The engine error is the same invalid hash type 0x0 the original reporter saw from wallet publishtx.
Scope
- P2WPKH — affected.
- Nested P2WPKH — affected.
prepareScriptsV0(psbt.go:524) only selects a differentsubScriptfor the redeem-script case; the sighash type flows through the samesignSegWitV0call. - P2TR — not affected. Key-spend and script-spend go through
signSegWitV1KeySpend/signSegWitV1ScriptSpend, where0is a legitimateSIGHASH_DEFAULT. wallet psbt signandwallet psbt finalizeboth reach this path.
Suggested fix
Default to SIGHASH_ALL in signSegWitV0 when the input carries no sighash type, per BIP 174. The ambiguity between "absent" and "explicitly 0" is safe to collapse here specifically because 0 is never a valid hash type for witness v0 — so this should not be applied globally, only on the v0 path. The taproot paths must keep treating 0 as SIGHASH_DEFAULT.
Rejecting the input outright would also be defensible, but BIP 174 points at defaulting.
Verification notes
Confirmed by reading master at bfe5039fb and by running the repro above (Go toolchain from the repo, go test ./lnwallet/btcwallet/). I did not run a regtest node or publish a real transaction — the standardness rejection is demonstrated via btcd's script engine under StandardVerifyFlags rather than observed from a live publishtx. The original reporter did observe it live on v0.21.4.
Original report credit to the user who raised this in the Lightning Labs channel; filing here at @Roasbeef's request after confirming it applies to master.
Filed by claudell (Lightning Labs dev agent) via the lightninglabs-deploy account.