diff --git a/lnwallet/script_utils.go b/lnwallet/script_utils.go index 3eafcba1..114e9b3b 100644 --- a/lnwallet/script_utils.go +++ b/lnwallet/script_utils.go @@ -83,7 +83,7 @@ func GenFundingPkScript(aPub, bPub []byte, amt int64) ([]byte, *wire.TxOut, erro return redeemScript, wire.NewTxOut(amt, pkScript), nil } -// spendMultiSig generates the witness stack required to redeem the 2-of-2 p2wsh +// SpendMultiSig generates the witness stack required to redeem the 2-of-2 p2wsh // multi-sig output. func SpendMultiSig(redeemScript, pubA, sigA, pubB, sigB []byte) [][]byte { witness := make([][]byte, 4) @@ -592,18 +592,19 @@ func commitScriptUnencumbered(key *btcec.PublicKey) ([]byte, error) { return builder.Script() } -// commitSpendTimeout constructs a valid witness allowing the owner of a +// CommitSpendTimeout constructs a valid witness allowing the owner of a // particular commitment transaction to spend the output returning settled // funds back to themselves after an absolute block timeout. -func commitSpendTimeout(commitScript []byte, outputAmt btcutil.Amount, - blockTimeout uint32, selfKey *btcec.PrivateKey, - sweepTx *wire.MsgTx) (wire.TxWitness, error) { +func CommitSpendTimeout(signer Signer, signDesc *SignDescriptor, + blockTimeout uint32, sweepTx *wire.MsgTx) (wire.TxWitness, error) { + + inputIndex := signDesc.InputIndex // In order to properly spend the transaction, we need to set the // sequence number. We do this by convering the relative block delay // into a sequence number value able to be interpeted by // OP_CHECKSEQUENCEVERIFY. - sweepTx.TxIn[0].Sequence = lockTimeToSequence(false, blockTimeout) + sweepTx.TxIn[inputIndex].Sequence = lockTimeToSequence(false, blockTimeout) // Additionally, OP_CSV requires that the version of the transaction // spending a pkscript with OP_CSV within it *must* be >= 2. @@ -611,10 +612,7 @@ func commitSpendTimeout(commitScript []byte, outputAmt btcutil.Amount, // With the sequence number in place, we're now able to properly sign // off on the sweep transaction. - hashCache := txscript.NewTxSigHashes(sweepTx) - sweepSig, err := txscript.RawTxInWitnessSignature( - sweepTx, hashCache, 0, int64(outputAmt), commitScript, - txscript.SigHashAll, selfKey) + sweepSig, err := signer.SignOutputRaw(sweepTx, signDesc) if err != nil { return nil, err } @@ -622,9 +620,9 @@ func commitSpendTimeout(commitScript []byte, outputAmt btcutil.Amount, // Place a zero as the first item in the evaluated witness stack to // force script execution to the timeout spend clause. witnessStack := wire.TxWitness(make([][]byte, 3)) - witnessStack[0] = sweepSig + witnessStack[0] = append(sweepSig, byte(txscript.SigHashAll)) witnessStack[1] = []byte{0} - witnessStack[2] = commitScript + witnessStack[2] = signDesc.RedeemScript return witnessStack, nil } diff --git a/lnwallet/script_utils_test.go b/lnwallet/script_utils_test.go index 1952d174..665a1ff4 100644 --- a/lnwallet/script_utils_test.go +++ b/lnwallet/script_utils_test.go @@ -43,6 +43,8 @@ func TestCommitmentSpendValidation(t *testing.T) { revocationPreimage := testHdSeed[:] revokePubKey := DeriveRevocationPubkey(bobKeyPub, revocationPreimage) + aliceSelfOutputSigner := &mockSigner{aliceKeyPriv} + // With all the test data set up, we create the commitment transaction. // We only focus on a single party's transactions, as the scripts are // identical with the roles reversed. @@ -77,15 +79,24 @@ func TestCommitmentSpendValidation(t *testing.T) { if err != nil { t.Fatalf("unable to generate alice delay script: %v") } - aliceWitnessSpend, err := commitSpendTimeout(delayScript, channelBalance, - csvTimeout, aliceKeyPriv, sweepTx) + signDesc := &SignDescriptor{ + RedeemScript: delayScript, + SigHashes: txscript.NewTxSigHashes(sweepTx), + Output: &wire.TxOut{ + Value: int64(channelBalance), + }, + HashType: txscript.SigHashAll, + InputIndex: 0, + } + aliceWitnessSpend, err := CommitSpendTimeout(aliceSelfOutputSigner, + signDesc, csvTimeout, sweepTx) if err != nil { t.Fatalf("unable to generate delay commit spend witness :%v") } sweepTx.TxIn[0].Witness = aliceWitnessSpend vm, err := txscript.NewEngine(delayOutput.PkScript, sweepTx, 0, txscript.StandardVerifyFlags, nil, - nil, int64(channelBalance)) + signDesc.SigHashes, int64(channelBalance)) if err != nil { t.Fatalf("unable to create engine: %v", err) }