From f972378140c3b1b3cbfa7736b3f271be63ecdc67 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sat, 10 Sep 2016 13:48:36 -0700 Subject: [PATCH] lnwallet: modify CommitSpendTimeout to expect proper input sequence num and tx version --- lnwallet/script_utils.go | 25 ++++++++++++------------- lnwallet/script_utils_test.go | 6 ++++-- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lnwallet/script_utils.go b/lnwallet/script_utils.go index 114e9b3b..e6e12e0f 100644 --- a/lnwallet/script_utils.go +++ b/lnwallet/script_utils.go @@ -594,21 +594,20 @@ func commitScriptUnencumbered(key *btcec.PublicKey) ([]byte, error) { // 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. +// funds back to themselves after a relative block timeout. In order to +// properly spend the transaction, the target input's sequence number should be +// set accordingly based off of the target relative block timeout within the +// redeem script. Additionally, OP_CSV requires that the version of the +// transaction spending a pkscript with OP_CSV within it *must* be >= 2. func CommitSpendTimeout(signer Signer, signDesc *SignDescriptor, - blockTimeout uint32, sweepTx *wire.MsgTx) (wire.TxWitness, error) { + 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[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. - sweepTx.Version = 2 + // Ensure the transaction version supports the validation of sequence + // locks and CSV semantics. + if sweepTx.Version < 2 { + return nil, fmt.Errorf("version of passed transaction MUST "+ + "be >= 2, not %v", sweepTx.Version) + } // With the sequence number in place, we're now able to properly sign // off on the sweep transaction. diff --git a/lnwallet/script_utils_test.go b/lnwallet/script_utils_test.go index 665a1ff4..3c6b6449 100644 --- a/lnwallet/script_utils_test.go +++ b/lnwallet/script_utils_test.go @@ -68,6 +68,7 @@ func TestCommitmentSpendValidation(t *testing.T) { t.Fatalf("unable to create target output: %v") } sweepTx := wire.NewMsgTx() + sweepTx.Version = 2 sweepTx.AddTxIn(wire.NewTxIn(&wire.OutPoint{commitmentTx.TxSha(), 0}, nil, nil)) sweepTx.AddTxOut(&wire.TxOut{ PkScript: targetOutput, @@ -79,6 +80,7 @@ func TestCommitmentSpendValidation(t *testing.T) { if err != nil { t.Fatalf("unable to generate alice delay script: %v") } + sweepTx.TxIn[0].Sequence = lockTimeToSequence(false, csvTimeout) signDesc := &SignDescriptor{ RedeemScript: delayScript, SigHashes: txscript.NewTxSigHashes(sweepTx), @@ -89,14 +91,14 @@ func TestCommitmentSpendValidation(t *testing.T) { InputIndex: 0, } aliceWitnessSpend, err := CommitSpendTimeout(aliceSelfOutputSigner, - signDesc, csvTimeout, sweepTx) + signDesc, 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, - signDesc.SigHashes, int64(channelBalance)) + nil, int64(channelBalance)) if err != nil { t.Fatalf("unable to create engine: %v", err) }