multi: use witnessScript everywhere instead of redeemScript

This commit consists of a mass variable renaming to call the pkScript being executed for segwit outputs the `witnessScript` instead of `redeemScript`. The latter naming convention is generally considered to be reserved for the context of BIP 16 execution. With segwit to be deployed soon, we should be using the correct terminology uniformly through the codebase. 

In addition some minor typos throughout the codebase has been fixed.
This commit is contained in:
andrew.shvv 2016-10-16 03:02:09 +04:00 committed by Olaoluwa Osuntokun
parent e038491f8a
commit e515710a7d
13 changed files with 77 additions and 76 deletions

@ -127,7 +127,7 @@ type OpenChannel struct {
OurMultiSigKey *btcec.PublicKey OurMultiSigKey *btcec.PublicKey
TheirMultiSigKey *btcec.PublicKey TheirMultiSigKey *btcec.PublicKey
FundingRedeemScript []byte FundingWitnessScript []byte
// In blocks // In blocks
LocalCsvDelay uint32 LocalCsvDelay uint32
@ -1095,7 +1095,7 @@ func putChanFundingInfo(nodeChanBucket *bolt.Bucket, channel *OpenChannel) error
return err return err
} }
if err := wire.WriteVarBytes(&b, 0, channel.FundingRedeemScript[:]); err != nil { if err := wire.WriteVarBytes(&b, 0, channel.FundingWitnessScript[:]); err != nil {
return err return err
} }
@ -1151,7 +1151,7 @@ func fetchChanFundingInfo(nodeChanBucket *bolt.Bucket, channel *OpenChannel) err
return err return err
} }
channel.FundingRedeemScript, err = wire.ReadVarBytes(infoBytes, 0, 520, "") channel.FundingWitnessScript, err = wire.ReadVarBytes(infoBytes, 0, 520, "")
if err != nil { if err != nil {
return err return err
} }

@ -148,7 +148,7 @@ func createTestChannelState(cdb *DB) (*OpenChannel, error) {
FundingOutpoint: testOutpoint, FundingOutpoint: testOutpoint,
OurMultiSigKey: privKey.PubKey(), OurMultiSigKey: privKey.PubKey(),
TheirMultiSigKey: privKey.PubKey(), TheirMultiSigKey: privKey.PubKey(),
FundingRedeemScript: script, FundingWitnessScript: script,
TheirCurrentRevocation: privKey.PubKey(), TheirCurrentRevocation: privKey.PubKey(),
TheirCurrentRevocationHash: key, TheirCurrentRevocationHash: key,
OurDeliveryScript: script, OurDeliveryScript: script,
@ -257,7 +257,7 @@ func TestOpenChannelPutGetDelete(t *testing.T) {
newState.TheirMultiSigKey.SerializeCompressed()) { newState.TheirMultiSigKey.SerializeCompressed()) {
t.Fatalf("their multisig key doesn't match") t.Fatalf("their multisig key doesn't match")
} }
if !bytes.Equal(state.FundingRedeemScript, newState.FundingRedeemScript) { if !bytes.Equal(state.FundingWitnessScript, newState.FundingWitnessScript) {
t.Fatalf("redeem script doesn't match") t.Fatalf("redeem script doesn't match")
} }

@ -57,7 +57,7 @@ type BtcWallet struct {
var _ lnwallet.WalletController = (*BtcWallet)(nil) var _ lnwallet.WalletController = (*BtcWallet)(nil)
// New returns a new fully initialized instance of BtcWallet given a valid // New returns a new fully initialized instance of BtcWallet given a valid
// confirguration struct. // configuration struct.
func New(cfg *Config) (*BtcWallet, error) { func New(cfg *Config) (*BtcWallet, error) {
// Ensure the wallet exists or create it when the create flag is set. // Ensure the wallet exists or create it when the create flag is set.
netDir := networkDir(cfg.DataDir, cfg.NetParams) netDir := networkDir(cfg.DataDir, cfg.NetParams)

@ -96,7 +96,7 @@ func (b *BtcWallet) fetchPrivKey(pub *btcec.PublicKey) (*btcec.PrivateKey, error
// //
// This is a part of the WalletController interface. // This is a part of the WalletController interface.
func (b *BtcWallet) SignOutputRaw(tx *wire.MsgTx, signDesc *lnwallet.SignDescriptor) ([]byte, error) { func (b *BtcWallet) SignOutputRaw(tx *wire.MsgTx, signDesc *lnwallet.SignDescriptor) ([]byte, error) {
redeemScript := signDesc.RedeemScript witnessScript := signDesc.WitnessScript
privKey, err := b.fetchPrivKey(signDesc.PubKey) privKey, err := b.fetchPrivKey(signDesc.PubKey)
if err != nil { if err != nil {
@ -105,7 +105,7 @@ func (b *BtcWallet) SignOutputRaw(tx *wire.MsgTx, signDesc *lnwallet.SignDescrip
amt := signDesc.Output.Value amt := signDesc.Output.Value
sig, err := txscript.RawTxInWitnessSignature(tx, signDesc.SigHashes, 0, sig, err := txscript.RawTxInWitnessSignature(tx, signDesc.SigHashes, 0,
amt, redeemScript, txscript.SigHashAll, privKey) amt, witnessScript, txscript.SigHashAll, privKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -385,7 +385,7 @@ type LightningChannel struct {
LocalDeliveryScript []byte LocalDeliveryScript []byte
RemoteDeliveryScript []byte RemoteDeliveryScript []byte
FundingRedeemScript []byte FundingWitnessScript []byte
fundingTxIn *wire.TxIn fundingTxIn *wire.TxIn
fundingP2WSH []byte fundingP2WSH []byte
@ -432,7 +432,7 @@ func NewLightningChannel(signer Signer, bio BlockChainIO,
Capacity: state.Capacity, Capacity: state.Capacity,
LocalDeliveryScript: state.OurDeliveryScript, LocalDeliveryScript: state.OurDeliveryScript,
RemoteDeliveryScript: state.TheirDeliveryScript, RemoteDeliveryScript: state.TheirDeliveryScript,
FundingRedeemScript: state.FundingRedeemScript, FundingWitnessScript: state.FundingWitnessScript,
ForceCloseSignal: make(chan struct{}), ForceCloseSignal: make(chan struct{}),
UnilateralCloseSignal: make(chan struct{}), UnilateralCloseSignal: make(chan struct{}),
} }
@ -460,7 +460,7 @@ func NewLightningChannel(signer Signer, bio BlockChainIO,
// Create the sign descriptor which we'll be using very frequently to // Create the sign descriptor which we'll be using very frequently to
// request a signature for the 2-of-2 multi-sig from the signer in // request a signature for the 2-of-2 multi-sig from the signer in
// order to complete channel state transitions. // order to complete channel state transitions.
fundingPkScript, err := witnessScriptHash(state.FundingRedeemScript) fundingPkScript, err := witnessScriptHash(state.FundingWitnessScript)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -468,7 +468,7 @@ func NewLightningChannel(signer Signer, bio BlockChainIO,
lc.fundingP2WSH = fundingPkScript lc.fundingP2WSH = fundingPkScript
lc.signDesc = &SignDescriptor{ lc.signDesc = &SignDescriptor{
PubKey: lc.channelState.OurMultiSigKey, PubKey: lc.channelState.OurMultiSigKey,
RedeemScript: lc.channelState.FundingRedeemScript, WitnessScript: lc.channelState.FundingWitnessScript,
Output: &wire.TxOut{ Output: &wire.TxOut{
PkScript: lc.fundingP2WSH, PkScript: lc.fundingP2WSH,
Value: int64(lc.channelState.Capacity), Value: int64(lc.channelState.Capacity),
@ -955,7 +955,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(rawSig []byte,
// Construct the sighash of the commitment transaction corresponding to // Construct the sighash of the commitment transaction corresponding to
// this newly proposed state update. // this newly proposed state update.
localCommitTx := localCommitmentView.txn localCommitTx := localCommitmentView.txn
multiSigScript := lc.channelState.FundingRedeemScript multiSigScript := lc.channelState.FundingWitnessScript
hashCache := txscript.NewTxSigHashes(localCommitTx) hashCache := txscript.NewTxSigHashes(localCommitTx)
sigHash, err := txscript.CalcWitnessSigHash(multiSigScript, hashCache, sigHash, err := txscript.CalcWitnessSigHash(multiSigScript, hashCache,
txscript.SigHashAll, localCommitTx, 0, int64(lc.channelState.Capacity)) txscript.SigHashAll, localCommitTx, 0, int64(lc.channelState.Capacity))
@ -1485,7 +1485,7 @@ func (lc *LightningChannel) ForceClose() (*ForceCloseSummary, error) {
// required to spend from the multi-sig output. // required to spend from the multi-sig output.
ourKey := lc.channelState.OurMultiSigKey.SerializeCompressed() ourKey := lc.channelState.OurMultiSigKey.SerializeCompressed()
theirKey := lc.channelState.TheirMultiSigKey.SerializeCompressed() theirKey := lc.channelState.TheirMultiSigKey.SerializeCompressed()
witness := SpendMultiSig(lc.FundingRedeemScript, ourKey, ourSig, witness := SpendMultiSig(lc.FundingWitnessScript, ourKey, ourSig,
theirKey, theirSig) theirKey, theirSig)
commitTx.TxIn[0].Witness = witness commitTx.TxIn[0].Witness = witness
@ -1529,7 +1529,7 @@ func (lc *LightningChannel) ForceClose() (*ForceCloseSummary, error) {
// set as the caller will decide these values once sweeping the output. // set as the caller will decide these values once sweeping the output.
selfSignDesc := &SignDescriptor{ selfSignDesc := &SignDescriptor{
PubKey: selfKey, PubKey: selfKey,
RedeemScript: selfScript, WitnessScript: selfScript,
Output: &wire.TxOut{ Output: &wire.TxOut{
PkScript: delayScript, PkScript: delayScript,
Value: int64(lc.channelState.OurBalance), Value: int64(lc.channelState.OurBalance),
@ -1643,7 +1643,7 @@ func (lc *LightningChannel) CompleteCooperativeClose(remoteSig []byte) (*wire.Ms
ourKey := lc.channelState.OurMultiSigKey.SerializeCompressed() ourKey := lc.channelState.OurMultiSigKey.SerializeCompressed()
theirKey := lc.channelState.TheirMultiSigKey.SerializeCompressed() theirKey := lc.channelState.TheirMultiSigKey.SerializeCompressed()
ourSig := append(closeSig, byte(txscript.SigHashAll)) ourSig := append(closeSig, byte(txscript.SigHashAll))
witness := SpendMultiSig(lc.signDesc.RedeemScript, ourKey, ourSig, witness := SpendMultiSig(lc.signDesc.WitnessScript, ourKey, ourSig,
theirKey, remoteSig) theirKey, remoteSig)
closeTx.TxIn[0].Witness = witness closeTx.TxIn[0].Witness = witness

@ -58,11 +58,11 @@ type mockSigner struct {
func (m *mockSigner) SignOutputRaw(tx *wire.MsgTx, signDesc *SignDescriptor) ([]byte, error) { func (m *mockSigner) SignOutputRaw(tx *wire.MsgTx, signDesc *SignDescriptor) ([]byte, error) {
amt := signDesc.Output.Value amt := signDesc.Output.Value
redeemScript := signDesc.RedeemScript witnessScript := signDesc.WitnessScript
privKey := m.key privKey := m.key
sig, err := txscript.RawTxInWitnessSignature(tx, signDesc.SigHashes, sig, err := txscript.RawTxInWitnessSignature(tx, signDesc.SigHashes,
signDesc.InputIndex, amt, redeemScript, txscript.SigHashAll, privKey) signDesc.InputIndex, amt, witnessScript, txscript.SigHashAll, privKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -179,7 +179,7 @@ func createTestChannels(revocationWindow int) (*LightningChannel, *LightningChan
csvTimeoutAlice := uint32(5) csvTimeoutAlice := uint32(5)
csvTimeoutBob := uint32(4) csvTimeoutBob := uint32(4)
redeemScript, _, err := GenFundingPkScript(aliceKeyPub.SerializeCompressed(), witnessScript, _, err := GenFundingPkScript(aliceKeyPub.SerializeCompressed(),
bobKeyPub.SerializeCompressed(), int64(channelCapacity)) bobKeyPub.SerializeCompressed(), int64(channelCapacity))
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
@ -240,7 +240,7 @@ func createTestChannels(revocationWindow int) (*LightningChannel, *LightningChan
FundingOutpoint: prevOut, FundingOutpoint: prevOut,
OurMultiSigKey: aliceKeyPub, OurMultiSigKey: aliceKeyPub,
TheirMultiSigKey: bobKeyPub, TheirMultiSigKey: bobKeyPub,
FundingRedeemScript: redeemScript, FundingWitnessScript: witnessScript,
LocalCsvDelay: csvTimeoutAlice, LocalCsvDelay: csvTimeoutAlice,
RemoteCsvDelay: csvTimeoutBob, RemoteCsvDelay: csvTimeoutBob,
TheirCurrentRevocation: bobRevokeKey, TheirCurrentRevocation: bobRevokeKey,
@ -260,7 +260,7 @@ func createTestChannels(revocationWindow int) (*LightningChannel, *LightningChan
FundingOutpoint: prevOut, FundingOutpoint: prevOut,
OurMultiSigKey: bobKeyPub, OurMultiSigKey: bobKeyPub,
TheirMultiSigKey: aliceKeyPub, TheirMultiSigKey: aliceKeyPub,
FundingRedeemScript: redeemScript, FundingWitnessScript: witnessScript,
LocalCsvDelay: csvTimeoutBob, LocalCsvDelay: csvTimeoutBob,
RemoteCsvDelay: csvTimeoutAlice, RemoteCsvDelay: csvTimeoutAlice,
TheirCurrentRevocation: aliceRevokeKey, TheirCurrentRevocation: aliceRevokeKey,

@ -210,19 +210,19 @@ type BlockChainIO interface {
GetTransaction(txid *wire.ShaHash) (*wire.MsgTx, error) GetTransaction(txid *wire.ShaHash) (*wire.MsgTx, error)
} }
// SignDescriptor houses the necessary information required to succesfully sign // SignDescriptor houses the necessary information required to successfully sign
// a given output. This struct is used by the Signer interface in order to gain // a given output. This struct is used by the Signer interface in order to gain
// access to critial data needed to generate a valid signature. // access to critical data needed to generate a valid signature.
type SignDescriptor struct { type SignDescriptor struct {
// Pubkey is the public key to which the signature should be generated // Pubkey is the public key to which the signature should be generated
// over. The Signer should then generate a signature with the private // over. The Signer should then generate a signature with the private
// key corresponding to this public key. // key corresponding to this public key.
PubKey *btcec.PublicKey PubKey *btcec.PublicKey
// RedeemScript is the full script required to properly redeem the // WitnessScript is the full script required to properly redeem the
// output. This field will only be populated if a p2wsh or a p2sh // output. This field will only be populated if a p2wsh or a p2sh
// output is being signed. // output is being signed.
RedeemScript []byte WitnessScript []byte
// Output is the target output which should be signed. The PkScript and // Output is the target output which should be signed. The PkScript and
// Value fields within the output should be properly populated, // Value fields within the output should be properly populated,

@ -481,14 +481,14 @@ func testDualFundingReservationWorkflow(miner *rpctest.Harness, wallet *lnwallet
chanInfo := lnc.StateSnapshot() chanInfo := lnc.StateSnapshot()
// Obtain bob's signature for the closure transaction. // Obtain bob's signature for the closure transaction.
redeemScript := lnc.FundingRedeemScript witnessScript := lnc.FundingWitnessScript
fundingOut := lnc.ChannelPoint() fundingOut := lnc.ChannelPoint()
fundingTxIn := wire.NewTxIn(fundingOut, nil, nil) fundingTxIn := wire.NewTxIn(fundingOut, nil, nil)
bobCloseTx := lnwallet.CreateCooperativeCloseTx(fundingTxIn, bobCloseTx := lnwallet.CreateCooperativeCloseTx(fundingTxIn,
chanInfo.RemoteBalance, chanInfo.LocalBalance, chanInfo.RemoteBalance, chanInfo.LocalBalance,
lnc.RemoteDeliveryScript, lnc.LocalDeliveryScript, lnc.RemoteDeliveryScript, lnc.LocalDeliveryScript,
false) false)
bobSig, err := bobNode.signCommitTx(bobCloseTx, redeemScript, int64(lnc.Capacity)) bobSig, err := bobNode.signCommitTx(bobCloseTx, witnessScript, int64(lnc.Capacity))
if err != nil { if err != nil {
t.Fatalf("unable to generate bob's signature for closing tx: %v", err) t.Fatalf("unable to generate bob's signature for closing tx: %v", err)
} }
@ -497,7 +497,7 @@ func testDualFundingReservationWorkflow(miner *rpctest.Harness, wallet *lnwallet
// be accepted, and found in the next mined block. // be accepted, and found in the next mined block.
ourKey := chanReservation.OurContribution().MultiSigKey.SerializeCompressed() ourKey := chanReservation.OurContribution().MultiSigKey.SerializeCompressed()
theirKey := chanReservation.TheirContribution().MultiSigKey.SerializeCompressed() theirKey := chanReservation.TheirContribution().MultiSigKey.SerializeCompressed()
witness := lnwallet.SpendMultiSig(redeemScript, ourKey, aliceCloseSig, witness := lnwallet.SpendMultiSig(witnessScript, ourKey, aliceCloseSig,
theirKey, bobSig) theirKey, bobSig)
bobCloseTx.TxIn[0].Witness = witness bobCloseTx.TxIn[0].Witness = witness
if err := wallet.PublishTransaction(bobCloseTx); err != nil { if err := wallet.PublishTransaction(bobCloseTx); err != nil {

@ -24,7 +24,7 @@ type ChannelContribution struct {
Inputs []*wire.TxIn Inputs []*wire.TxIn
// ChangeOutputs are the Outputs to be used in the case that the total // ChangeOutputs are the Outputs to be used in the case that the total
// value of the fund ing inputs is greater than the total potential // value of the funding inputs is greater than the total potential
// channel capacity. // channel capacity.
ChangeOutputs []*wire.TxOut ChangeOutputs []*wire.TxOut
@ -52,28 +52,29 @@ type ChannelContribution struct {
// InputScripts represents any script inputs required to redeem a previous // InputScripts represents any script inputs required to redeem a previous
// output. This struct is used rather than just a witness, or scripSig in // output. This struct is used rather than just a witness, or scripSig in
// order to accomdate nested p2sh which utilizes both types of input scripts. // order to accommodate nested p2sh which utilizes both types of input scripts.
type InputScript struct { type InputScript struct {
Witness [][]byte Witness [][]byte
ScriptSig []byte ScriptSig []byte
} }
// ChannelReservation represents an intent to open a lightning payment channel // ChannelReservation represents an intent to open a lightning payment channel
// a counterpaty. The funding proceses from reservation to channel opening is a // a counterparty. The funding processes from reservation to channel opening
// 3-step process. In order to allow for full concurrency during the reservation // is a 3-step process. In order to allow for full concurrency during the
// workflow, resources consumed by a contribution are "locked" themselves. This // reservation workflow, resources consumed by a contribution are "locked"
// prevents a number of race conditions such as two funding transactions // themselves. This prevents a number of race conditions such as two funding
// double-spending the same input. A reservation can also be cancelled, which // transactions double-spending the same input. A reservation can also be
// removes the resources from limbo, allowing another reservation to claim them. // cancelled, which removes the resources from limbo, allowing another
// reservation to claim them.
// //
// The reservation workflow consists of the following three steps: // The reservation workflow consists of the following three steps:
// 1. lnwallet.InitChannelReservation // 1. lnwallet.InitChannelReservation
// * One requests the wallet to allocate the neccessary resources for a // * One requests the wallet to allocate the necessary resources for a
// channel reservation. These resources a put in limbo for the lifetime // channel reservation. These resources a put in limbo for the lifetime
// of a reservation. // of a reservation.
// * Once completed the reservation will have the wallet's contribution // * Once completed the reservation will have the wallet's contribution
// accessible via the .OurContribution() method. This contribution // accessible via the .OurContribution() method. This contribution
// contains the neccessary items to allow the remote party to build both // contains the necessary items to allow the remote party to build both
// the funding, and commitment transactions. // the funding, and commitment transactions.
// 2. ChannelReservation.ProcessContribution/ChannelReservation.ProcessSingleContribution // 2. ChannelReservation.ProcessContribution/ChannelReservation.ProcessSingleContribution
// * The counterparty presents their contribution to the payment channel. // * The counterparty presents their contribution to the payment channel.
@ -84,7 +85,7 @@ type InputScript struct {
// * All signatures crafted by us, are now available via .OurSignatures(). // * All signatures crafted by us, are now available via .OurSignatures().
// 3. ChannelReservation.CompleteReservation/ChannelReservation.CompleteReservationSingle // 3. ChannelReservation.CompleteReservation/ChannelReservation.CompleteReservationSingle
// * The final step in the workflow. The counterparty presents the // * The final step in the workflow. The counterparty presents the
// signatures for all their inputs to the funding transation, as well // signatures for all their inputs to the funding transaction, as well
// as a signature to our version of the commitment transaction. // as a signature to our version of the commitment transaction.
// * We then verify the validity of all signatures before considering the // * We then verify the validity of all signatures before considering the
// channel "open". // channel "open".
@ -328,7 +329,7 @@ func (r *ChannelReservation) FinalFundingTx() *wire.MsgTx {
func (r *ChannelReservation) FundingRedeemScript() []byte { func (r *ChannelReservation) FundingRedeemScript() []byte {
r.RLock() r.RLock()
defer r.RUnlock() defer r.RUnlock()
return r.partialState.FundingRedeemScript return r.partialState.FundingWitnessScript
} }
// LocalCommitTx returns the commitment transaction for the local node involved // LocalCommitTx returns the commitment transaction for the local node involved

@ -25,11 +25,11 @@ var (
// witnessScriptHash generates a pay-to-witness-script-hash public key script // witnessScriptHash generates a pay-to-witness-script-hash public key script
// paying to a version 0 witness program paying to the passed redeem script. // paying to a version 0 witness program paying to the passed redeem script.
func witnessScriptHash(redeemScript []byte) ([]byte, error) { func witnessScriptHash(witnessScript []byte) ([]byte, error) {
bldr := txscript.NewScriptBuilder() bldr := txscript.NewScriptBuilder()
bldr.AddOp(txscript.OP_0) bldr.AddOp(txscript.OP_0)
scriptHash := fastsha256.Sum256(redeemScript) scriptHash := fastsha256.Sum256(witnessScript)
bldr.AddData(scriptHash[:]) bldr.AddData(scriptHash[:])
return bldr.Script() return bldr.Script()
} }
@ -68,33 +68,33 @@ func GenFundingPkScript(aPub, bPub []byte, amt int64) ([]byte, *wire.TxOut, erro
} }
// First, create the 2-of-2 multi-sig script itself. // First, create the 2-of-2 multi-sig script itself.
redeemScript, err := genMultiSigScript(aPub, bPub) witnessScript, err := genMultiSigScript(aPub, bPub)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
// With the 2-of-2 script in had, generate a p2wsh script which pays // With the 2-of-2 script in had, generate a p2wsh script which pays
// to the funding script. // to the funding script.
pkScript, err := witnessScriptHash(redeemScript) pkScript, err := witnessScriptHash(witnessScript)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
return redeemScript, wire.NewTxOut(amt, pkScript), nil return witnessScript, 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(witnessScript, pubA, sigA, pubB, sigB []byte) [][]byte {
witness := make([][]byte, 4) witness := make([][]byte, 4)
// When spending a p2wsh multi-sig script, rather than an OP_0, we add // When spending a p2wsh multi-sig script, rather than an OP_0, we add
// a nil stack element to eat the extra pop. // a nil stack element to eat the extra pop.
witness[0] = nil witness[0] = nil
// When initially generating the redeemScript, we sorted the serialized // When initially generating the witnessScript, we sorted the serialized
// public keys in descending order. So we do a quick comparison in order // public keys in descending order. So we do a quick comparison in order
// ensure the signatures appear on the Script Virual Machine stack in // ensure the signatures appear on the Script Virtual Machine stack in
// the correct order. // the correct order.
if bytes.Compare(pubA, pubB) == -1 { if bytes.Compare(pubA, pubB) == -1 {
witness[1] = sigB witness[1] = sigB
@ -105,7 +105,7 @@ func SpendMultiSig(redeemScript, pubA, sigA, pubB, sigB []byte) [][]byte {
} }
// Finally, add the pre-image as the last witness element. // Finally, add the pre-image as the last witness element.
witness[3] = redeemScript witness[3] = witnessScript
return witness return witness
} }
@ -556,7 +556,7 @@ func commitScriptToSelf(csvTimeout uint32, selfKey, revokeKey *btcec.PublicKey)
// signature with the revocation public key. The revocation public key // signature with the revocation public key. The revocation public key
// will *only* be known to the other party if we have divulged the // will *only* be known to the other party if we have divulged the
// revocation hash, allowing them to homomorphically derive the proper // revocation hash, allowing them to homomorphically derive the proper
// private key which coresponds to the revoke public key. // private key which corresponds to the revoke public key.
builder := txscript.NewScriptBuilder() builder := txscript.NewScriptBuilder()
builder.AddOp(txscript.OP_IF) builder.AddOp(txscript.OP_IF)
@ -621,7 +621,7 @@ func CommitSpendTimeout(signer Signer, signDesc *SignDescriptor,
witnessStack := wire.TxWitness(make([][]byte, 3)) witnessStack := wire.TxWitness(make([][]byte, 3))
witnessStack[0] = append(sweepSig, byte(txscript.SigHashAll)) witnessStack[0] = append(sweepSig, byte(txscript.SigHashAll))
witnessStack[1] = []byte{0} witnessStack[1] = []byte{0}
witnessStack[2] = signDesc.RedeemScript witnessStack[2] = signDesc.WitnessScript
return witnessStack, nil return witnessStack, nil
} }

@ -16,13 +16,13 @@ import (
// the commitment transaction. // the commitment transaction.
// //
// The following spending cases are covered by this test: // The following spending cases are covered by this test:
// * Alice's spend from the delayed output on her commitment transaciton. // * Alice's spend from the delayed output on her commitment transaction.
// * Bob's spend from Alice's delayed output when she broadcasts a revoked // * Bob's spend from Alice's delayed output when she broadcasts a revoked
// commitment transaction. // commitment transaction.
// * Bob's spend from his unencumbered output within Alice's commitment // * Bob's spend from his unencumbered output within Alice's commitment
// transaction. // transaction.
func TestCommitmentSpendValidation(t *testing.T) { func TestCommitmentSpendValidation(t *testing.T) {
// We generate a fake output, and the coresponding txin. This output // We generate a fake output, and the corresponding txin. This output
// doesn't need to exist, as we'll only be validating spending from the // doesn't need to exist, as we'll only be validating spending from the
// transaction that references this. // transaction that references this.
fundingOut := &wire.OutPoint{ fundingOut := &wire.OutPoint{
@ -82,7 +82,7 @@ func TestCommitmentSpendValidation(t *testing.T) {
} }
sweepTx.TxIn[0].Sequence = lockTimeToSequence(false, csvTimeout) sweepTx.TxIn[0].Sequence = lockTimeToSequence(false, csvTimeout)
signDesc := &SignDescriptor{ signDesc := &SignDescriptor{
RedeemScript: delayScript, WitnessScript: delayScript,
SigHashes: txscript.NewTxSigHashes(sweepTx), SigHashes: txscript.NewTxSigHashes(sweepTx),
Output: &wire.TxOut{ Output: &wire.TxOut{
Value: int64(channelBalance), Value: int64(channelBalance),

@ -660,15 +660,15 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
// Finally, add the 2-of-2 multi-sig output which will set up the lightning // Finally, add the 2-of-2 multi-sig output which will set up the lightning
// channel. // channel.
channelCapacity := int64(pendingReservation.partialState.Capacity) channelCapacity := int64(pendingReservation.partialState.Capacity)
redeemScript, multiSigOut, err := GenFundingPkScript(ourKey.SerializeCompressed(), witnessScript, multiSigOut, err := GenFundingPkScript(ourKey.SerializeCompressed(),
theirKey.SerializeCompressed(), channelCapacity) theirKey.SerializeCompressed(), channelCapacity)
if err != nil { if err != nil {
req.err <- err req.err <- err
return return
} }
pendingReservation.partialState.FundingRedeemScript = redeemScript pendingReservation.partialState.FundingWitnessScript = witnessScript
// Sort the transaction. Since both side agree to a cannonical // Sort the transaction. Since both side agree to a canonical
// ordering, by sorting we no longer need to send the entire // ordering, by sorting we no longer need to send the entire
// transaction. Only signatures will be exchanged. // transaction. Only signatures will be exchanged.
fundingTx.AddTxOut(multiSigOut) fundingTx.AddTxOut(multiSigOut)
@ -708,7 +708,7 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
} }
// Locate the index of the multi-sig outpoint in order to record it // Locate the index of the multi-sig outpoint in order to record it
// since the outputs are cannonically sorted. If this is a single funder // since the outputs are canonically sorted. If this is a single funder
// workflow, then we'll also need to send this to the remote node. // workflow, then we'll also need to send this to the remote node.
fundingTxID := fundingTx.TxSha() fundingTxID := fundingTx.TxSha()
_, multiSigIndex := FindScriptOutputIndex(fundingTx, multiSigOut.PkScript) _, multiSigIndex := FindScriptOutputIndex(fundingTx, multiSigOut.PkScript)
@ -792,7 +792,7 @@ func (l *LightningWallet) handleContributionMsg(req *addContributionMsg) {
// Generate a signature for their version of the initial commitment // Generate a signature for their version of the initial commitment
// transaction. // transaction.
signDesc = SignDescriptor{ signDesc = SignDescriptor{
RedeemScript: redeemScript, WitnessScript: witnessScript,
PubKey: ourKey, PubKey: ourKey,
Output: multiSigOut, Output: multiSigOut,
HashType: txscript.SigHashAll, HashType: txscript.SigHashAll,
@ -837,13 +837,13 @@ func (l *LightningWallet) handleSingleContribution(req *addSingleContributionMsg
ourKey := pendingReservation.partialState.OurMultiSigKey ourKey := pendingReservation.partialState.OurMultiSigKey
theirKey := theirContribution.MultiSigKey theirKey := theirContribution.MultiSigKey
channelCapacity := int64(pendingReservation.partialState.Capacity) channelCapacity := int64(pendingReservation.partialState.Capacity)
redeemScript, _, err := GenFundingPkScript(ourKey.SerializeCompressed(), witnessScript, _, err := GenFundingPkScript(ourKey.SerializeCompressed(),
theirKey.SerializeCompressed(), channelCapacity) theirKey.SerializeCompressed(), channelCapacity)
if err != nil { if err != nil {
req.err <- err req.err <- err
return return
} }
pendingReservation.partialState.FundingRedeemScript = redeemScript pendingReservation.partialState.FundingWitnessScript = witnessScript
masterElkremRoot, err := l.deriveMasterElkremRoot() masterElkremRoot, err := l.deriveMasterElkremRoot()
if err != nil { if err != nil {
@ -888,7 +888,7 @@ func (l *LightningWallet) handleSingleContribution(req *addSingleContributionMsg
} }
// handleFundingCounterPartySigs is the final step in the channel reservation // handleFundingCounterPartySigs is the final step in the channel reservation
// workflow. During this setp, we validate *all* the received signatures for // workflow. During this step, we validate *all* the received signatures for
// inputs to the funding transaction. If any of these are invalid, we bail, // inputs to the funding transaction. If any of these are invalid, we bail,
// and forcibly cancel this funding request. Additionally, we ensure that the // and forcibly cancel this funding request. Additionally, we ensure that the
// signature we received from the counterparty for our version of the commitment // signature we received from the counterparty for our version of the commitment
@ -953,17 +953,17 @@ func (l *LightningWallet) handleFundingCounterPartySigs(msg *addCounterPartySigs
commitTx := pendingReservation.partialState.OurCommitTx commitTx := pendingReservation.partialState.OurCommitTx
theirKey := pendingReservation.theirContribution.MultiSigKey theirKey := pendingReservation.theirContribution.MultiSigKey
// Re-generate both the redeemScript and p2sh output. We sign the // Re-generate both the witnessScript and p2sh output. We sign the
// redeemScript script, but include the p2sh output as the subscript // witnessScript script, but include the p2sh output as the subscript
// for verification. // for verification.
redeemScript := pendingReservation.partialState.FundingRedeemScript witnessScript := pendingReservation.partialState.FundingWitnessScript
// Next, create the spending scriptSig, and then verify that the script // Next, create the spending scriptSig, and then verify that the script
// is complete, allowing us to spend from the funding transaction. // is complete, allowing us to spend from the funding transaction.
theirCommitSig := msg.theirCommitmentSig theirCommitSig := msg.theirCommitmentSig
channelValue := int64(pendingReservation.partialState.Capacity) channelValue := int64(pendingReservation.partialState.Capacity)
hashCache := txscript.NewTxSigHashes(commitTx) hashCache := txscript.NewTxSigHashes(commitTx)
sigHash, err := txscript.CalcWitnessSigHash(redeemScript, hashCache, sigHash, err := txscript.CalcWitnessSigHash(witnessScript, hashCache,
txscript.SigHashAll, commitTx, 0, channelValue) txscript.SigHashAll, commitTx, 0, channelValue)
if err != nil { if err != nil {
msg.err <- fmt.Errorf("counterparty's commitment signature is invalid: %v", err) msg.err <- fmt.Errorf("counterparty's commitment signature is invalid: %v", err)
@ -1066,13 +1066,13 @@ func (l *LightningWallet) handleSingleFunderSigs(req *addSingleFunderSigsMsg) {
pendingReservation.partialState.OurCommitTx = ourCommitTx pendingReservation.partialState.OurCommitTx = ourCommitTx
txsort.InPlaceSort(theirCommitTx) txsort.InPlaceSort(theirCommitTx)
redeemScript := pendingReservation.partialState.FundingRedeemScript witnessScript := pendingReservation.partialState.FundingWitnessScript
channelValue := int64(pendingReservation.partialState.Capacity) channelValue := int64(pendingReservation.partialState.Capacity)
hashCache := txscript.NewTxSigHashes(ourCommitTx) hashCache := txscript.NewTxSigHashes(ourCommitTx)
theirKey := pendingReservation.theirContribution.MultiSigKey theirKey := pendingReservation.theirContribution.MultiSigKey
ourKey := pendingReservation.partialState.OurMultiSigKey ourKey := pendingReservation.partialState.OurMultiSigKey
sigHash, err := txscript.CalcWitnessSigHash(redeemScript, hashCache, sigHash, err := txscript.CalcWitnessSigHash(witnessScript, hashCache,
txscript.SigHashAll, ourCommitTx, 0, channelValue) txscript.SigHashAll, ourCommitTx, 0, channelValue)
if err != nil { if err != nil {
req.err <- err req.err <- err
@ -1094,13 +1094,13 @@ func (l *LightningWallet) handleSingleFunderSigs(req *addSingleFunderSigsMsg) {
// With their signature for our version of the commitment transactions // With their signature for our version of the commitment transactions
// verified, we can now generate a signature for their version, // verified, we can now generate a signature for their version,
// allowing the funding transaction to be safely broadcast. // allowing the funding transaction to be safely broadcast.
p2wsh, err := witnessScriptHash(redeemScript) p2wsh, err := witnessScriptHash(witnessScript)
if err != nil { if err != nil {
req.err <- err req.err <- err
return return
} }
signDesc := SignDescriptor{ signDesc := SignDescriptor{
RedeemScript: redeemScript, WitnessScript: witnessScript,
PubKey: ourKey, PubKey: ourKey,
Output: &wire.TxOut{ Output: &wire.TxOut{
PkScript: p2wsh, PkScript: p2wsh,
@ -1201,7 +1201,7 @@ out:
// selectCoinsAndChange performs coin selection in order to obtain witness // selectCoinsAndChange performs coin selection in order to obtain witness
// outputs which sum to at least 'numCoins' amount of satoshis. If coin // outputs which sum to at least 'numCoins' amount of satoshis. If coin
// selection is succesful/possible, then the selected coins are available // selection is successful/possible, then the selected coins are available
// within the passed contribution's inputs. If necessary, a change address will // within the passed contribution's inputs. If necessary, a change address will
// also be generated. // also be generated.
// TODO(roasbeef): remove hardcoded fees and req'd confs for outputs. // TODO(roasbeef): remove hardcoded fees and req'd confs for outputs.
@ -1210,7 +1210,7 @@ func (l *LightningWallet) selectCoinsAndChange(feeRate uint64, amt btcutil.Amoun
// We hold the coin select mutex while querying for outputs, and // We hold the coin select mutex while querying for outputs, and
// performing coin selection in order to avoid inadvertent double // performing coin selection in order to avoid inadvertent double
// spends accross funding transactions. // spends across funding transactions.
l.coinSelectMtx.Lock() l.coinSelectMtx.Lock()
defer l.coinSelectMtx.Unlock() defer l.coinSelectMtx.Unlock()
@ -1222,7 +1222,7 @@ func (l *LightningWallet) selectCoinsAndChange(feeRate uint64, amt btcutil.Amoun
return err return err
} }
// Peform coin selection over our available, unlocked unspent outputs // Perform coin selection over our available, unlocked unspent outputs
// in order to find enough coins to meet the funding amount // in order to find enough coins to meet the funding amount
// requirements. // requirements.
selectedCoins, changeAmt, err := coinSelect(feeRate, amt, coins) selectedCoins, changeAmt, err := coinSelect(feeRate, amt, coins)

@ -655,8 +655,8 @@ func (p *peer) executeForceClose(channel *lnwallet.LightningChannel) (*wire.ShaH
return nil, err return nil, err
} }
// Send the closed channel sumary over to the utxoNursery in order to // Send the closed channel summary over to the utxoNursery in order to
// have its outputs sweeped back into the wallet once they're mature. // have its outputs swept back into the wallet once they're mature.
p.server.utxoNursery.incubateOutputs(closeSummary) p.server.utxoNursery.incubateOutputs(closeSummary)
return &txid, nil return &txid, nil