lnwallet: convert CommitSpendTimeout to user the Signer interface

This commit is contained in:
Olaoluwa Osuntokun 2016-09-08 17:04:08 -07:00
parent e72c52288d
commit e858bb5ca2
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
2 changed files with 24 additions and 15 deletions

View File

@ -83,7 +83,7 @@ func GenFundingPkScript(aPub, bPub []byte, amt int64) ([]byte, *wire.TxOut, erro
return redeemScript, wire.NewTxOut(amt, pkScript), nil 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. // multi-sig output.
func SpendMultiSig(redeemScript, pubA, sigA, pubB, sigB []byte) [][]byte { func SpendMultiSig(redeemScript, pubA, sigA, pubB, sigB []byte) [][]byte {
witness := make([][]byte, 4) witness := make([][]byte, 4)
@ -592,18 +592,19 @@ func commitScriptUnencumbered(key *btcec.PublicKey) ([]byte, error) {
return builder.Script() 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 // particular commitment transaction to spend the output returning settled
// funds back to themselves after an absolute block timeout. // funds back to themselves after an absolute block timeout.
func commitSpendTimeout(commitScript []byte, outputAmt btcutil.Amount, func CommitSpendTimeout(signer Signer, signDesc *SignDescriptor,
blockTimeout uint32, selfKey *btcec.PrivateKey, 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 // In order to properly spend the transaction, we need to set the
// sequence number. We do this by convering the relative block delay // sequence number. We do this by convering the relative block delay
// into a sequence number value able to be interpeted by // into a sequence number value able to be interpeted by
// OP_CHECKSEQUENCEVERIFY. // 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 // Additionally, OP_CSV requires that the version of the transaction
// spending a pkscript with OP_CSV within it *must* be >= 2. // 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 // With the sequence number in place, we're now able to properly sign
// off on the sweep transaction. // off on the sweep transaction.
hashCache := txscript.NewTxSigHashes(sweepTx) sweepSig, err := signer.SignOutputRaw(sweepTx, signDesc)
sweepSig, err := txscript.RawTxInWitnessSignature(
sweepTx, hashCache, 0, int64(outputAmt), commitScript,
txscript.SigHashAll, selfKey)
if err != nil { if err != nil {
return nil, err 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 // Place a zero as the first item in the evaluated witness stack to
// force script execution to the timeout spend clause. // force script execution to the timeout spend clause.
witnessStack := wire.TxWitness(make([][]byte, 3)) witnessStack := wire.TxWitness(make([][]byte, 3))
witnessStack[0] = sweepSig witnessStack[0] = append(sweepSig, byte(txscript.SigHashAll))
witnessStack[1] = []byte{0} witnessStack[1] = []byte{0}
witnessStack[2] = commitScript witnessStack[2] = signDesc.RedeemScript
return witnessStack, nil return witnessStack, nil
} }

View File

@ -43,6 +43,8 @@ func TestCommitmentSpendValidation(t *testing.T) {
revocationPreimage := testHdSeed[:] revocationPreimage := testHdSeed[:]
revokePubKey := DeriveRevocationPubkey(bobKeyPub, revocationPreimage) revokePubKey := DeriveRevocationPubkey(bobKeyPub, revocationPreimage)
aliceSelfOutputSigner := &mockSigner{aliceKeyPriv}
// With all the test data set up, we create the commitment transaction. // 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 // We only focus on a single party's transactions, as the scripts are
// identical with the roles reversed. // identical with the roles reversed.
@ -77,15 +79,24 @@ 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")
} }
aliceWitnessSpend, err := commitSpendTimeout(delayScript, channelBalance, signDesc := &SignDescriptor{
csvTimeout, aliceKeyPriv, sweepTx) 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 { 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,
nil, int64(channelBalance)) signDesc.SigHashes, int64(channelBalance))
if err != nil { if err != nil {
t.Fatalf("unable to create engine: %v", err) t.Fatalf("unable to create engine: %v", err)
} }