lnwallet: modify CommitSpendTimeout to expect proper input sequence num and tx version

This commit is contained in:
Olaoluwa Osuntokun 2016-09-10 13:48:36 -07:00
parent 0873c4da76
commit f972378140
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
2 changed files with 16 additions and 15 deletions

View File

@ -594,21 +594,20 @@ func commitScriptUnencumbered(key *btcec.PublicKey) ([]byte, error) {
// 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 // 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, func CommitSpendTimeout(signer Signer, signDesc *SignDescriptor,
blockTimeout uint32, sweepTx *wire.MsgTx) (wire.TxWitness, error) { sweepTx *wire.MsgTx) (wire.TxWitness, error) {
inputIndex := signDesc.InputIndex // Ensure the transaction version supports the validation of sequence
// locks and CSV semantics.
// In order to properly spend the transaction, we need to set the if sweepTx.Version < 2 {
// sequence number. We do this by convering the relative block delay return nil, fmt.Errorf("version of passed transaction MUST "+
// into a sequence number value able to be interpeted by "be >= 2, not %v", sweepTx.Version)
// 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
// With the sequence number in place, we're now able to properly sign // With the sequence number in place, we're now able to properly sign
// off on the sweep transaction. // off on the sweep transaction.

View File

@ -68,6 +68,7 @@ func TestCommitmentSpendValidation(t *testing.T) {
t.Fatalf("unable to create target output: %v") t.Fatalf("unable to create target output: %v")
} }
sweepTx := wire.NewMsgTx() sweepTx := wire.NewMsgTx()
sweepTx.Version = 2
sweepTx.AddTxIn(wire.NewTxIn(&wire.OutPoint{commitmentTx.TxSha(), 0}, nil, nil)) sweepTx.AddTxIn(wire.NewTxIn(&wire.OutPoint{commitmentTx.TxSha(), 0}, nil, nil))
sweepTx.AddTxOut(&wire.TxOut{ sweepTx.AddTxOut(&wire.TxOut{
PkScript: targetOutput, PkScript: targetOutput,
@ -79,6 +80,7 @@ func TestCommitmentSpendValidation(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unable to generate alice delay script: %v") t.Fatalf("unable to generate alice delay script: %v")
} }
sweepTx.TxIn[0].Sequence = lockTimeToSequence(false, csvTimeout)
signDesc := &SignDescriptor{ signDesc := &SignDescriptor{
RedeemScript: delayScript, RedeemScript: delayScript,
SigHashes: txscript.NewTxSigHashes(sweepTx), SigHashes: txscript.NewTxSigHashes(sweepTx),
@ -89,14 +91,14 @@ func TestCommitmentSpendValidation(t *testing.T) {
InputIndex: 0, InputIndex: 0,
} }
aliceWitnessSpend, err := CommitSpendTimeout(aliceSelfOutputSigner, aliceWitnessSpend, err := CommitSpendTimeout(aliceSelfOutputSigner,
signDesc, csvTimeout, sweepTx) signDesc, sweepTx)
if err != nil { if err != nil {
t.Fatalf("unable to generate delay commit spend witness :%v") t.Fatalf("unable to generate delay commit spend witness :%v")
} }
sweepTx.TxIn[0].Witness = aliceWitnessSpend sweepTx.TxIn[0].Witness = aliceWitnessSpend
vm, err := txscript.NewEngine(delayOutput.PkScript, vm, err := txscript.NewEngine(delayOutput.PkScript,
sweepTx, 0, txscript.StandardVerifyFlags, nil, sweepTx, 0, txscript.StandardVerifyFlags, nil,
signDesc.SigHashes, int64(channelBalance)) nil, int64(channelBalance))
if err != nil { if err != nil {
t.Fatalf("unable to create engine: %v", err) t.Fatalf("unable to create engine: %v", err)
} }