lnwallet/channel: use genHtlcScript to create scripts

It takes into account the necessary variables and will prepare us for
doing commitment type dependent script generation later.
This commit is contained in:
Johan T. Halseth 2020-03-06 16:11:44 +01:00
parent 02dd8d23fd
commit 865776c776
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -2157,11 +2157,6 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
// remote commitment transaction. // remote commitment transaction.
htlcRetributions := make([]HtlcRetribution, 0, len(revokedSnapshot.Htlcs)) htlcRetributions := make([]HtlcRetribution, 0, len(revokedSnapshot.Htlcs))
for _, htlc := range revokedSnapshot.Htlcs { for _, htlc := range revokedSnapshot.Htlcs {
var (
htlcWitnessScript []byte
err error
)
// If the HTLC is dust, then we'll skip it as it doesn't have // If the HTLC is dust, then we'll skip it as it doesn't have
// an output on the commitment transaction. // an output on the commitment transaction.
if htlcIsDust( if htlcIsDust(
@ -2185,31 +2180,13 @@ func NewBreachRetribution(chanState *channeldb.OpenChannel, stateNum uint64,
// If this is an incoming HTLC, then this means that they were // If this is an incoming HTLC, then this means that they were
// the sender of the HTLC (relative to us). So we'll // the sender of the HTLC (relative to us). So we'll
// re-generate the sender HTLC script. // re-generate the sender HTLC script. Otherwise, is this was
if htlc.Incoming { // an outgoing HTLC that we sent, then from the PoV of the
htlcWitnessScript, err = input.SenderHTLCScript( // remote commitment state, they're the receiver of this HTLC.
keyRing.RemoteHtlcKey, keyRing.LocalHtlcKey, htlcPkScript, htlcWitnessScript, err := genHtlcScript(
keyRing.RevocationKey, htlc.RHash[:], htlc.Incoming, false, htlc.RefundTimeout,
) htlc.RHash, keyRing,
if err != nil { )
return nil, err
}
} else {
// Otherwise, is this was an outgoing HTLC that we
// sent, then from the PoV of the remote commitment
// state, they're the receiver of this HTLC.
htlcWitnessScript, err = input.ReceiverHTLCScript(
htlc.RefundTimeout, keyRing.LocalHtlcKey,
keyRing.RemoteHtlcKey, keyRing.RevocationKey,
htlc.RHash[:],
)
if err != nil {
return nil, err
}
}
htlcPkScript, err := input.WitnessScriptHash(htlcWitnessScript)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -5325,24 +5302,19 @@ func newOutgoingHtlcResolution(signer input.Signer,
Index: uint32(htlc.OutputIndex), Index: uint32(htlc.OutputIndex),
} }
// First, we'll re-generate the script used to send the HTLC to
// the remote party within their commitment transaction.
htlcScriptHash, htlcScript, err := genHtlcScript(
false, localCommit, htlc.RefundTimeout, htlc.RHash, keyRing,
)
if err != nil {
return nil, err
}
// If we're spending this HTLC output from the remote node's // If we're spending this HTLC output from the remote node's
// commitment, then we won't need to go to the second level as our // commitment, then we won't need to go to the second level as our
// outputs don't have a CSV delay. // outputs don't have a CSV delay.
if !localCommit { if !localCommit {
// First, we'll re-generate the script used to send the HTLC to
// the remote party within their commitment transaction.
htlcReceiverScript, err := input.ReceiverHTLCScript(htlc.RefundTimeout,
keyRing.LocalHtlcKey, keyRing.RemoteHtlcKey,
keyRing.RevocationKey, htlc.RHash[:],
)
if err != nil {
return nil, err
}
htlcScriptHash, err := input.WitnessScriptHash(htlcReceiverScript)
if err != nil {
return nil, err
}
// With the script generated, we can completely populated the // With the script generated, we can completely populated the
// SignDescriptor needed to sweep the output. // SignDescriptor needed to sweep the output.
return &OutgoingHtlcResolution{ return &OutgoingHtlcResolution{
@ -5351,7 +5323,7 @@ func newOutgoingHtlcResolution(signer input.Signer,
SweepSignDesc: input.SignDescriptor{ SweepSignDesc: input.SignDescriptor{
KeyDesc: localChanCfg.HtlcBasePoint, KeyDesc: localChanCfg.HtlcBasePoint,
SingleTweak: keyRing.LocalHtlcKeyTweak, SingleTweak: keyRing.LocalHtlcKeyTweak,
WitnessScript: htlcReceiverScript, WitnessScript: htlcScript,
Output: &wire.TxOut{ Output: &wire.TxOut{
PkScript: htlcScriptHash, PkScript: htlcScriptHash,
Value: int64(htlc.Amt.ToSatoshis()), Value: int64(htlc.Amt.ToSatoshis()),
@ -5383,15 +5355,10 @@ func newOutgoingHtlcResolution(signer input.Signer,
// With the transaction created, we can generate a sign descriptor // With the transaction created, we can generate a sign descriptor
// that's capable of generating the signature required to spend the // that's capable of generating the signature required to spend the
// HTLC output using the timeout transaction. // HTLC output using the timeout transaction.
htlcCreationScript, err := input.SenderHTLCScript(keyRing.LocalHtlcKey,
keyRing.RemoteHtlcKey, keyRing.RevocationKey, htlc.RHash[:])
if err != nil {
return nil, err
}
timeoutSignDesc := input.SignDescriptor{ timeoutSignDesc := input.SignDescriptor{
KeyDesc: localChanCfg.HtlcBasePoint, KeyDesc: localChanCfg.HtlcBasePoint,
SingleTweak: keyRing.LocalHtlcKeyTweak, SingleTweak: keyRing.LocalHtlcKeyTweak,
WitnessScript: htlcCreationScript, WitnessScript: htlcScript,
Output: &wire.TxOut{ Output: &wire.TxOut{
Value: int64(htlc.Amt.ToSatoshis()), Value: int64(htlc.Amt.ToSatoshis()),
}, },
@ -5419,7 +5386,7 @@ func newOutgoingHtlcResolution(signer input.Signer,
if err != nil { if err != nil {
return nil, err return nil, err
} }
htlcScriptHash, err := input.WitnessScriptHash(htlcSweepScript) htlcSweepScriptHash, err := input.WitnessScriptHash(htlcSweepScript)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -5440,7 +5407,7 @@ func newOutgoingHtlcResolution(signer input.Signer,
SingleTweak: localDelayTweak, SingleTweak: localDelayTweak,
WitnessScript: htlcSweepScript, WitnessScript: htlcSweepScript,
Output: &wire.TxOut{ Output: &wire.TxOut{
PkScript: htlcScriptHash, PkScript: htlcSweepScriptHash,
Value: int64(secondLevelOutputAmt), Value: int64(secondLevelOutputAmt),
}, },
HashType: txscript.SigHashAll, HashType: txscript.SigHashAll,
@ -5465,23 +5432,18 @@ func newIncomingHtlcResolution(signer input.Signer, localChanCfg *channeldb.Chan
Index: uint32(htlc.OutputIndex), Index: uint32(htlc.OutputIndex),
} }
// First, we'll re-generate the script the remote party used to
// send the HTLC to us in their commitment transaction.
htlcScriptHash, htlcScript, err := genHtlcScript(
true, localCommit, htlc.RefundTimeout, htlc.RHash, keyRing,
)
if err != nil {
return nil, err
}
// If we're spending this output from the remote node's commitment, // If we're spending this output from the remote node's commitment,
// then we can skip the second layer and spend the output directly. // then we can skip the second layer and spend the output directly.
if !localCommit { if !localCommit {
// First, we'll re-generate the script the remote party used to
// send the HTLC to us in their commitment transaction.
htlcSenderScript, err := input.SenderHTLCScript(
keyRing.RemoteHtlcKey, keyRing.LocalHtlcKey,
keyRing.RevocationKey, htlc.RHash[:],
)
if err != nil {
return nil, err
}
htlcScriptHash, err := input.WitnessScriptHash(htlcSenderScript)
if err != nil {
return nil, err
}
// With the script generated, we can completely populated the // With the script generated, we can completely populated the
// SignDescriptor needed to sweep the output. // SignDescriptor needed to sweep the output.
return &IncomingHtlcResolution{ return &IncomingHtlcResolution{
@ -5490,7 +5452,7 @@ func newIncomingHtlcResolution(signer input.Signer, localChanCfg *channeldb.Chan
SweepSignDesc: input.SignDescriptor{ SweepSignDesc: input.SignDescriptor{
KeyDesc: localChanCfg.HtlcBasePoint, KeyDesc: localChanCfg.HtlcBasePoint,
SingleTweak: keyRing.LocalHtlcKeyTweak, SingleTweak: keyRing.LocalHtlcKeyTweak,
WitnessScript: htlcSenderScript, WitnessScript: htlcScript,
Output: &wire.TxOut{ Output: &wire.TxOut{
PkScript: htlcScriptHash, PkScript: htlcScriptHash,
Value: int64(htlc.Amt.ToSatoshis()), Value: int64(htlc.Amt.ToSatoshis()),
@ -5516,17 +5478,10 @@ func newIncomingHtlcResolution(signer input.Signer, localChanCfg *channeldb.Chan
// Once we've created the second-level transaction, we'll generate the // Once we've created the second-level transaction, we'll generate the
// SignDesc needed spend the HTLC output using the success transaction. // SignDesc needed spend the HTLC output using the success transaction.
htlcCreationScript, err := input.ReceiverHTLCScript(htlc.RefundTimeout,
keyRing.RemoteHtlcKey, keyRing.LocalHtlcKey,
keyRing.RevocationKey, htlc.RHash[:],
)
if err != nil {
return nil, err
}
successSignDesc := input.SignDescriptor{ successSignDesc := input.SignDescriptor{
KeyDesc: localChanCfg.HtlcBasePoint, KeyDesc: localChanCfg.HtlcBasePoint,
SingleTweak: keyRing.LocalHtlcKeyTweak, SingleTweak: keyRing.LocalHtlcKeyTweak,
WitnessScript: htlcCreationScript, WitnessScript: htlcScript,
Output: &wire.TxOut{ Output: &wire.TxOut{
Value: int64(htlc.Amt.ToSatoshis()), Value: int64(htlc.Amt.ToSatoshis()),
}, },
@ -5556,7 +5511,7 @@ func newIncomingHtlcResolution(signer input.Signer, localChanCfg *channeldb.Chan
if err != nil { if err != nil {
return nil, err return nil, err
} }
htlcScriptHash, err := input.WitnessScriptHash(htlcSweepScript) htlcSweepScriptHash, err := input.WitnessScriptHash(htlcSweepScript)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -5576,7 +5531,7 @@ func newIncomingHtlcResolution(signer input.Signer, localChanCfg *channeldb.Chan
SingleTweak: localDelayTweak, SingleTweak: localDelayTweak,
WitnessScript: htlcSweepScript, WitnessScript: htlcSweepScript,
Output: &wire.TxOut{ Output: &wire.TxOut{
PkScript: htlcScriptHash, PkScript: htlcSweepScriptHash,
Value: int64(secondLevelOutputAmt), Value: int64(secondLevelOutputAmt),
}, },
HashType: txscript.SigHashAll, HashType: txscript.SigHashAll,