lnwallet: move pubKey comparison for funding output spend into spendMultiSig func

* This change makes the spendMultiSig function testable independent of
the reservation workflow.
This commit is contained in:
Olaoluwa Osuntokun 2016-02-05 12:33:22 -08:00
parent 4632894562
commit fa05ee9a22
3 changed files with 19 additions and 24 deletions

@ -189,16 +189,10 @@ func (c *ChannelUpdate) VerifyNewCommitmentSigs(ourSig, theirSig []byte) error {
// public keys in descending order. So we do a quick comparison in order
// ensure the signatures appear on the Script Virual Machine stack in
// the correct order.
// TODO(roasbeef): func
redeemScript := channelState.FundingRedeemScript
ourKey := channelState.OurCommitKey.PubKey().SerializeCompressed()
theirKey := channelState.TheirCommitKey.SerializeCompressed()
if bytes.Compare(ourKey, theirKey) == -1 {
scriptSig, err = spendMultiSig(redeemScript, theirSig, ourSig)
} else {
scriptSig, err = spendMultiSig(redeemScript, ourSig, theirSig)
}
scriptSig, err = spendMultiSig(redeemScript, ourKey, ourSig, theirKey, theirSig)
if err != nil {
return err
}

@ -67,6 +67,7 @@ func fundMultiSigOut(aPub, bPub []byte, amt int64) ([]byte, *wire.TxOut, error)
if err != nil {
return nil, nil, err
}
pkScript, err := scriptHashPkScript(redeemScript)
if err != nil {
return nil, nil, err
@ -77,16 +78,23 @@ func fundMultiSigOut(aPub, bPub []byte, amt int64) ([]byte, *wire.TxOut, error)
// spendMultiSig generates the scriptSig required to redeem the 2-of-2 p2sh
// multi-sig output.
func spendMultiSig(redeemScript, sigA, sigB []byte) ([]byte, error) {
func spendMultiSig(redeemScript, pubA, sigA, pubB, sigB []byte) ([]byte, error) {
bldr := txscript.NewScriptBuilder()
// add a 0 for some multisig fun
bldr.AddOp(txscript.OP_0)
// add sigA
bldr.AddData(sigA)
// add sigB
bldr.AddData(sigB)
// When initially generating the redeemScript, we sorted the serialized
// public keys in descending order. So we do a quick comparison in order
// ensure the signatures appear on the Script Virual Machine stack in
// the correct order.
if bytes.Compare(pubA, pubB) == -1 {
bldr.AddData(sigB)
bldr.AddData(sigA)
} else {
bldr.AddData(sigA)
bldr.AddData(sigB)
}
// preimage goes on AT THE ENDDDD
bldr.AddData(redeemScript)

@ -1,7 +1,6 @@
package lnwallet
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
@ -934,18 +933,12 @@ func (l *LightningWallet) handleFundingCounterPartySigs(msg *addCounterPartySigs
// Next, create the spending scriptSig, and then verify that the script
// is complete, allowing us to spend from the funding transaction.
//
// When initially generating the redeemScript, we sorted the serialized
// public keys in descending order. So we do a quick comparison in order
// ensure the signatures appear on the Script Virual Machine stack in
// the correct order.
var scriptSig []byte
theirCommitSig := msg.theirCommitmentSig
if bytes.Compare(ourKey.PubKey().SerializeCompressed(), theirKey.SerializeCompressed()) == -1 {
scriptSig, err = spendMultiSig(redeemScript, theirCommitSig, ourCommitSig)
} else {
scriptSig, err = spendMultiSig(redeemScript, ourCommitSig, theirCommitSig)
}
ourKeySer := ourKey.PubKey().SerializeCompressed()
theirKeySer := theirKey.SerializeCompressed()
scriptSig, err := spendMultiSig(redeemScript, ourKeySer, ourCommitSig,
theirKeySer,
theirCommitSig)
if err != nil {
msg.err <- err
return