@@ -16,6 +16,7 @@ import (
1616 "github.com/btcsuite/btcd/integration/rpctest"
1717 "github.com/btcsuite/btcd/txscript"
1818 "github.com/btcsuite/btcd/wire"
19+ "github.com/lightningnetwork/lnd/input"
1920 "github.com/lightningnetwork/lnd/lntest/unittest"
2021 "github.com/stretchr/testify/require"
2122)
@@ -36,7 +37,7 @@ func randPubKeyHashScript() ([]byte, *btcec.PrivateKey, error) {
3637 }
3738
3839 pubKeyHash := btcutil .Hash160 (privKey .PubKey ().SerializeCompressed ())
39- addrScript , err := btcutil .NewAddressPubKeyHash (
40+ addrScript , err := btcutil .NewAddressWitnessPubKeyHash (
4041 pubKeyHash , unittest .NetParams ,
4142 )
4243 if err != nil {
@@ -139,16 +140,26 @@ func CreateSpendTx(t *testing.T, prevOutPoint *wire.OutPoint,
139140
140141 t .Helper ()
141142
142- spendingTx := wire .NewMsgTx (1 )
143- spendingTx .AddTxIn (& wire.TxIn {PreviousOutPoint : * prevOutPoint })
144- spendingTx .AddTxOut (& wire.TxOut {Value : 1e8 , PkScript : prevOutput .PkScript })
145-
146- sigScript , err := txscript .SignatureScript (
147- spendingTx , 0 , prevOutput .PkScript , txscript .SigHashAll ,
148- privKey , true ,
143+ // Create a new output.
144+ outputAmt := int64 (1e8 )
145+ witnessProgram , _ , err := randPubKeyHashScript ()
146+ require .NoError (t , err , "unable to generate pkScript" )
147+ output := wire .NewTxOut (outputAmt , witnessProgram )
148+
149+ // Create a new tx.
150+ tx := wire .NewMsgTx (2 )
151+ tx .AddTxIn (wire .NewTxIn (prevOutPoint , nil , nil ))
152+ tx .AddTxOut (output )
153+
154+ // Generate the witness.
155+ sigHashes := input .NewTxSigHashesV0Only (tx )
156+ witnessScript , err := txscript .WitnessSignature (
157+ tx , sigHashes , 0 , prevOutput .Value , prevOutput .PkScript ,
158+ txscript .SigHashAll , privKey , true ,
149159 )
150160 require .NoError (t , err , "unable to sign tx" )
151- spendingTx .TxIn [0 ].SignatureScript = sigScript
152161
153- return spendingTx
162+ tx .TxIn [0 ].Witness = witnessScript
163+
164+ return tx
154165}
0 commit comments