diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 72e51762..791b38d8 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -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 } diff --git a/lnwallet/script_utils.go b/lnwallet/script_utils.go index a854767e..406cbd1a 100644 --- a/lnwallet/script_utils.go +++ b/lnwallet/script_utils.go @@ -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) diff --git a/lnwallet/wallet.go b/lnwallet/wallet.go index f7af9107..372b6cf3 100644 --- a/lnwallet/wallet.go +++ b/lnwallet/wallet.go @@ -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