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

@ -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.

@ -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)
}