@@ -10,6 +10,7 @@ import (
1010	"github.com/btcsuite/btcd/btcutil" 
1111	"github.com/btcsuite/btcd/btcutil/hdkeychain" 
1212	"github.com/btcsuite/btcd/btcutil/psbt" 
13+ 	"github.com/btcsuite/btcd/txscript" 
1314	"github.com/lightninglabs/chantools/lnd" 
1415	"github.com/spf13/cobra" 
1516)
@@ -153,17 +154,25 @@ func signPsbt(rootKey *hdkeychain.ExtendedKey,
153154		return  fmt .Errorf ("could not derive local key: %w" , err )
154155	}
155156
156- 	if  len (packet .Inputs [inputIndex ].WitnessScript ) ==  0  {
157- 		return  fmt .Errorf ("invalid PSBT, input %d is missing witness " + 
158- 			"script" , inputIndex )
159- 	}
160- 	witnessScript  :=  packet .Inputs [inputIndex ].WitnessScript 
161157	if  packet .Inputs [inputIndex ].WitnessUtxo  ==  nil  {
162158		return  fmt .Errorf ("invalid PSBT, input %d is missing witness " + 
163159			"UTXO" , inputIndex )
164160	}
165161	utxo  :=  packet .Inputs [inputIndex ].WitnessUtxo 
166162
163+ 	// The signing is a bit different for P2WPKH, we need to specify the 
164+ 	// pk script as the witness script. 
165+ 	var  witnessScript  []byte 
166+ 	if  txscript .IsPayToWitnessPubKeyHash (utxo .PkScript ) {
167+ 		witnessScript  =  utxo .PkScript 
168+ 	} else  {
169+ 		if  len (packet .Inputs [inputIndex ].WitnessScript ) ==  0  {
170+ 			return  fmt .Errorf ("invalid PSBT, input %d is missing " + 
171+ 				"witness script" , inputIndex )
172+ 		}
173+ 		witnessScript  =  packet .Inputs [inputIndex ].WitnessScript 
174+ 	}
175+ 
167176	localPrivateKey , err  :=  localKey .ECPrivKey ()
168177	if  err  !=  nil  {
169178		return  fmt .Errorf ("error getting private key: %w" , err )
0 commit comments