2020-01-06 11:42:03 +01:00
|
|
|
package lnwallet
|
|
|
|
|
|
|
|
import (
|
2022-04-07 23:37:46 +02:00
|
|
|
"bytes"
|
2020-01-06 11:42:03 +01:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2022-04-07 23:37:46 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2020-03-06 16:11:47 +01:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
2020-01-06 11:42:03 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2020-01-06 11:42:03 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2020-01-06 11:42:03 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
2020-03-06 16:11:46 +01:00
|
|
|
// anchorSize is the constant anchor output size.
|
|
|
|
const anchorSize = btcutil.Amount(330)
|
|
|
|
|
2020-12-10 14:16:53 +01:00
|
|
|
// DefaultAnchorsCommitMaxFeeRateSatPerVByte is the default max fee rate in
|
|
|
|
// sat/vbyte the initiator will use for anchor channels. This should be enough
|
|
|
|
// to ensure propagation before anchoring down the commitment transaction.
|
|
|
|
const DefaultAnchorsCommitMaxFeeRateSatPerVByte = 10
|
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
// CommitmentKeyRing holds all derived keys needed to construct commitment and
|
|
|
|
// HTLC transactions. The keys are derived differently depending whether the
|
|
|
|
// commitment transaction is ours or the remote peer's. Private keys associated
|
|
|
|
// with each key may belong to the commitment owner or the "other party" which
|
|
|
|
// is referred to in the field comments, regardless of which is local and which
|
|
|
|
// is remote.
|
|
|
|
type CommitmentKeyRing struct {
|
2020-01-06 11:42:03 +01:00
|
|
|
// CommitPoint is the "per commitment point" used to derive the tweak
|
2020-01-06 11:42:03 +01:00
|
|
|
// for each base point.
|
|
|
|
CommitPoint *btcec.PublicKey
|
|
|
|
|
|
|
|
// LocalCommitKeyTweak is the tweak used to derive the local public key
|
|
|
|
// from the local payment base point or the local private key from the
|
|
|
|
// base point secret. This may be included in a SignDescriptor to
|
|
|
|
// generate signatures for the local payment key.
|
2020-01-06 11:42:03 +01:00
|
|
|
//
|
|
|
|
// NOTE: This will always refer to "our" local key, regardless of
|
|
|
|
// whether this is our commit or not.
|
2020-01-06 11:42:03 +01:00
|
|
|
LocalCommitKeyTweak []byte
|
|
|
|
|
|
|
|
// TODO(roasbeef): need delay tweak as well?
|
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
// LocalHtlcKeyTweak is the tweak used to derive the local HTLC key
|
|
|
|
// from the local HTLC base point. This value is needed in order to
|
2020-01-06 11:42:03 +01:00
|
|
|
// derive the final key used within the HTLC scripts in the commitment
|
|
|
|
// transaction.
|
2020-01-06 11:42:03 +01:00
|
|
|
//
|
|
|
|
// NOTE: This will always refer to "our" local HTLC key, regardless of
|
|
|
|
// whether this is our commit or not.
|
2020-01-06 11:42:03 +01:00
|
|
|
LocalHtlcKeyTweak []byte
|
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
// LocalHtlcKey is the key that will be used in any clause paying to
|
|
|
|
// our node of any HTLC scripts within the commitment transaction for
|
|
|
|
// this key ring set.
|
|
|
|
//
|
|
|
|
// NOTE: This will always refer to "our" local HTLC key, regardless of
|
|
|
|
// whether this is our commit or not.
|
2020-01-06 11:42:03 +01:00
|
|
|
LocalHtlcKey *btcec.PublicKey
|
|
|
|
|
|
|
|
// RemoteHtlcKey is the key that will be used in clauses within the
|
|
|
|
// HTLC script that send money to the remote party.
|
2020-01-06 11:42:03 +01:00
|
|
|
//
|
|
|
|
// NOTE: This will always refer to "their" remote HTLC key, regardless
|
|
|
|
// of whether this is our commit or not.
|
2020-01-06 11:42:03 +01:00
|
|
|
RemoteHtlcKey *btcec.PublicKey
|
|
|
|
|
2020-01-06 11:42:04 +01:00
|
|
|
// ToLocalKey is the commitment transaction owner's key which is
|
|
|
|
// included in HTLC success and timeout transaction scripts. This is
|
|
|
|
// the public key used for the to_local output of the commitment
|
|
|
|
// transaction.
|
|
|
|
//
|
|
|
|
// NOTE: Who's key this is depends on the current perspective. If this
|
|
|
|
// is our commitment this will be our key.
|
|
|
|
ToLocalKey *btcec.PublicKey
|
2020-01-06 11:42:03 +01:00
|
|
|
|
2020-01-06 11:42:04 +01:00
|
|
|
// ToRemoteKey is the non-owner's payment key in the commitment tx.
|
|
|
|
// This is the key used to generate the to_remote output within the
|
2020-01-06 11:42:03 +01:00
|
|
|
// commitment transaction.
|
2020-01-06 11:42:04 +01:00
|
|
|
//
|
|
|
|
// NOTE: Who's key this is depends on the current perspective. If this
|
|
|
|
// is our commitment this will be their key.
|
|
|
|
ToRemoteKey *btcec.PublicKey
|
2020-01-06 11:42:03 +01:00
|
|
|
|
|
|
|
// RevocationKey is the key that can be used by the other party to
|
|
|
|
// redeem outputs from a revoked commitment transaction if it were to
|
|
|
|
// be published.
|
2020-01-06 11:42:04 +01:00
|
|
|
//
|
|
|
|
// NOTE: Who can sign for this key depends on the current perspective.
|
|
|
|
// If this is our commitment, it means the remote node can sign for
|
|
|
|
// this key in case of a breach.
|
2020-01-06 11:42:03 +01:00
|
|
|
RevocationKey *btcec.PublicKey
|
|
|
|
}
|
|
|
|
|
2020-01-06 11:42:04 +01:00
|
|
|
// DeriveCommitmentKeys generates a new commitment key set using the base points
|
|
|
|
// and commitment point. The keys are derived differently depending on the type
|
|
|
|
// of channel, and whether the commitment transaction is ours or the remote
|
|
|
|
// peer's.
|
2020-01-06 11:42:03 +01:00
|
|
|
func DeriveCommitmentKeys(commitPoint *btcec.PublicKey,
|
2020-01-06 11:42:04 +01:00
|
|
|
isOurCommit bool, chanType channeldb.ChannelType,
|
2020-01-06 11:42:03 +01:00
|
|
|
localChanCfg, remoteChanCfg *channeldb.ChannelConfig) *CommitmentKeyRing {
|
|
|
|
|
2020-01-06 11:42:04 +01:00
|
|
|
tweaklessCommit := chanType.IsTweakless()
|
|
|
|
|
2020-01-06 11:42:05 +01:00
|
|
|
// Depending on if this is our commit or not, we'll choose the correct
|
|
|
|
// base point.
|
|
|
|
localBasePoint := localChanCfg.PaymentBasePoint
|
|
|
|
if isOurCommit {
|
|
|
|
localBasePoint = localChanCfg.DelayBasePoint
|
|
|
|
}
|
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
// First, we'll derive all the keys that don't depend on the context of
|
|
|
|
// whose commitment transaction this is.
|
|
|
|
keyRing := &CommitmentKeyRing{
|
|
|
|
CommitPoint: commitPoint,
|
|
|
|
|
|
|
|
LocalCommitKeyTweak: input.SingleTweakBytes(
|
2020-01-06 11:42:05 +01:00
|
|
|
commitPoint, localBasePoint.PubKey,
|
2020-01-06 11:42:03 +01:00
|
|
|
),
|
|
|
|
LocalHtlcKeyTweak: input.SingleTweakBytes(
|
|
|
|
commitPoint, localChanCfg.HtlcBasePoint.PubKey,
|
|
|
|
),
|
|
|
|
LocalHtlcKey: input.TweakPubKey(
|
|
|
|
localChanCfg.HtlcBasePoint.PubKey, commitPoint,
|
|
|
|
),
|
|
|
|
RemoteHtlcKey: input.TweakPubKey(
|
|
|
|
remoteChanCfg.HtlcBasePoint.PubKey, commitPoint,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2020-01-06 11:42:04 +01:00
|
|
|
// We'll now compute the to_local, to_remote, and revocation key based
|
|
|
|
// on the current commitment point. All keys are tweaked each state in
|
2020-01-06 11:42:03 +01:00
|
|
|
// order to ensure the keys from each state are unlinkable. To create
|
|
|
|
// the revocation key, we take the opposite party's revocation base
|
|
|
|
// point and combine that with the current commitment point.
|
|
|
|
var (
|
2020-01-06 11:42:04 +01:00
|
|
|
toLocalBasePoint *btcec.PublicKey
|
|
|
|
toRemoteBasePoint *btcec.PublicKey
|
2020-01-06 11:42:03 +01:00
|
|
|
revocationBasePoint *btcec.PublicKey
|
|
|
|
)
|
|
|
|
if isOurCommit {
|
2020-01-06 11:42:04 +01:00
|
|
|
toLocalBasePoint = localChanCfg.DelayBasePoint.PubKey
|
|
|
|
toRemoteBasePoint = remoteChanCfg.PaymentBasePoint.PubKey
|
2020-01-06 11:42:03 +01:00
|
|
|
revocationBasePoint = remoteChanCfg.RevocationBasePoint.PubKey
|
|
|
|
} else {
|
2020-01-06 11:42:04 +01:00
|
|
|
toLocalBasePoint = remoteChanCfg.DelayBasePoint.PubKey
|
|
|
|
toRemoteBasePoint = localChanCfg.PaymentBasePoint.PubKey
|
2020-01-06 11:42:03 +01:00
|
|
|
revocationBasePoint = localChanCfg.RevocationBasePoint.PubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the base points assigned, we can now derive the actual keys
|
|
|
|
// using the base point, and the current commitment tweak.
|
2020-01-06 11:42:04 +01:00
|
|
|
keyRing.ToLocalKey = input.TweakPubKey(toLocalBasePoint, commitPoint)
|
2020-01-06 11:42:03 +01:00
|
|
|
keyRing.RevocationKey = input.DeriveRevocationPubkey(
|
|
|
|
revocationBasePoint, commitPoint,
|
|
|
|
)
|
|
|
|
|
|
|
|
// If this commitment should omit the tweak for the remote point, then
|
|
|
|
// we'll use that directly, and ignore the commitPoint tweak.
|
|
|
|
if tweaklessCommit {
|
2020-01-06 11:42:04 +01:00
|
|
|
keyRing.ToRemoteKey = toRemoteBasePoint
|
2020-01-06 11:42:04 +01:00
|
|
|
|
|
|
|
// If this is not our commitment, the above ToRemoteKey will be
|
|
|
|
// ours, and we blank out the local commitment tweak to
|
|
|
|
// indicate that the key should not be tweaked when signing.
|
|
|
|
if !isOurCommit {
|
|
|
|
keyRing.LocalCommitKeyTweak = nil
|
|
|
|
}
|
2020-01-06 11:42:03 +01:00
|
|
|
} else {
|
2020-01-06 11:42:04 +01:00
|
|
|
keyRing.ToRemoteKey = input.TweakPubKey(
|
|
|
|
toRemoteBasePoint, commitPoint,
|
2020-01-06 11:42:03 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyRing
|
|
|
|
}
|
|
|
|
|
2023-08-08 06:09:58 +02:00
|
|
|
// WitnessScriptDesc holds the output script and the witness script for p2wsh
|
|
|
|
// outputs.
|
|
|
|
type WitnessScriptDesc struct {
|
|
|
|
// OutputScript is the output's PkScript.
|
|
|
|
OutputScript []byte
|
2020-01-06 11:42:04 +01:00
|
|
|
|
|
|
|
// WitnessScript is the full script required to properly redeem the
|
|
|
|
// output. This field should be set to the full script if a p2wsh
|
|
|
|
// output is being signed. For p2wkh it should be set equal to the
|
|
|
|
// PkScript.
|
|
|
|
WitnessScript []byte
|
2023-08-08 06:09:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PkScript is the public key script that commits to the final
|
|
|
|
// contract.
|
|
|
|
func (w *WitnessScriptDesc) PkScript() []byte {
|
|
|
|
return w.OutputScript
|
|
|
|
}
|
|
|
|
|
|
|
|
// WitnessScript returns the witness script that we'll use when signing for the
|
|
|
|
// remote party, and also verifying signatures on our transactions. As an
|
|
|
|
// example, when we create an outgoing HTLC for the remote party, we want to
|
|
|
|
// sign their success path.
|
|
|
|
func (w *WitnessScriptDesc) WitnessScriptToSign() []byte {
|
|
|
|
return w.WitnessScript
|
|
|
|
}
|
|
|
|
|
|
|
|
// WitnessScriptForPath returns the witness script for the given spending path.
|
|
|
|
// An error is returned if the path is unknown. This is useful as when
|
|
|
|
// constructing a contrl block for a given path, one also needs witness script
|
|
|
|
// being signed.
|
|
|
|
func (w *WitnessScriptDesc) WitnessScriptForPath(path input.ScriptPath,
|
|
|
|
) ([]byte, error) {
|
2023-03-02 06:38:53 +01:00
|
|
|
|
2023-08-08 06:09:58 +02:00
|
|
|
return w.WitnessScript, nil
|
2020-01-06 11:42:04 +01:00
|
|
|
}
|
|
|
|
|
2021-07-15 02:06:13 +02:00
|
|
|
// CommitScriptToSelf constructs the public key script for the output on the
|
|
|
|
// commitment transaction paying to the "owner" of said commitment transaction.
|
2021-07-15 02:16:13 +02:00
|
|
|
// The `initiator` argument should correspond to the owner of the commitment
|
2022-01-13 17:29:43 +01:00
|
|
|
// transaction which we are generating the to_local script for. If the other
|
2021-07-15 02:16:13 +02:00
|
|
|
// party learns of the preimage to the revocation hash, then they can claim all
|
|
|
|
// the settled funds in the channel, plus the unsettled funds.
|
|
|
|
func CommitScriptToSelf(chanType channeldb.ChannelType, initiator bool,
|
2023-08-08 06:09:58 +02:00
|
|
|
selfKey, revokeKey *btcec.PublicKey, csvDelay, leaseExpiry uint32,
|
|
|
|
) (
|
|
|
|
input.ScriptDescriptor, error) {
|
2021-07-15 02:06:13 +02:00
|
|
|
|
2021-07-15 02:16:13 +02:00
|
|
|
switch {
|
2023-01-20 02:05:39 +01:00
|
|
|
// For taproot scripts, we'll need to make a slightly modified script
|
|
|
|
// where a NUMS key is used to force a script path reveal of either the
|
|
|
|
// revocation or the CSV timeout.
|
|
|
|
//
|
|
|
|
// Our "redeem" script here is just the taproot witness program.
|
|
|
|
case chanType.IsTaproot():
|
2023-08-08 06:09:58 +02:00
|
|
|
return input.NewLocalCommitScriptTree(
|
2023-01-20 02:05:39 +01:00
|
|
|
csvDelay, selfKey, revokeKey,
|
|
|
|
)
|
|
|
|
|
2021-07-15 02:16:13 +02:00
|
|
|
// If we are the initiator of a leased channel, then we have an
|
2023-01-20 02:05:39 +01:00
|
|
|
// additional CLTV requirement in addition to the usual CSV
|
|
|
|
// requirement.
|
2021-07-15 02:16:13 +02:00
|
|
|
case initiator && chanType.HasLeaseExpiration():
|
2023-01-20 02:05:39 +01:00
|
|
|
toLocalRedeemScript, err := input.LeaseCommitScriptToSelf(
|
2021-07-15 02:16:13 +02:00
|
|
|
selfKey, revokeKey, csvDelay, leaseExpiry,
|
|
|
|
)
|
2023-01-20 02:05:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
toLocalScriptHash, err := input.WitnessScriptHash(
|
|
|
|
toLocalRedeemScript,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-08 06:09:58 +02:00
|
|
|
return &WitnessScriptDesc{
|
|
|
|
OutputScript: toLocalScriptHash,
|
2023-01-20 02:05:39 +01:00
|
|
|
WitnessScript: toLocalRedeemScript,
|
|
|
|
}, nil
|
2021-07-15 02:16:13 +02:00
|
|
|
|
|
|
|
default:
|
2023-01-20 02:05:39 +01:00
|
|
|
toLocalRedeemScript, err := input.CommitScriptToSelf(
|
2021-07-15 02:16:13 +02:00
|
|
|
csvDelay, selfKey, revokeKey,
|
|
|
|
)
|
2023-01-20 02:05:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-15 02:06:13 +02:00
|
|
|
|
2023-01-20 02:05:39 +01:00
|
|
|
toLocalScriptHash, err := input.WitnessScriptHash(
|
|
|
|
toLocalRedeemScript,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-15 02:06:13 +02:00
|
|
|
|
2023-08-08 06:09:58 +02:00
|
|
|
return &WitnessScriptDesc{
|
|
|
|
OutputScript: toLocalScriptHash,
|
2023-01-20 02:05:39 +01:00
|
|
|
WitnessScript: toLocalRedeemScript,
|
|
|
|
}, nil
|
|
|
|
}
|
2021-07-15 02:06:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CommitScriptToRemote derives the appropriate to_remote script based on the
|
2021-07-15 02:16:13 +02:00
|
|
|
// channel's commitment type. The `initiator` argument should correspond to the
|
2022-01-13 17:29:43 +01:00
|
|
|
// owner of the commitment transaction which we are generating the to_remote
|
2021-07-15 02:16:13 +02:00
|
|
|
// script for. The second return value is the CSV delay of the output script,
|
|
|
|
// what must be satisfied in order to spend the output.
|
|
|
|
func CommitScriptToRemote(chanType channeldb.ChannelType, initiator bool,
|
2023-01-20 02:05:58 +01:00
|
|
|
remoteKey *btcec.PublicKey,
|
2023-08-08 06:09:58 +02:00
|
|
|
leaseExpiry uint32) (input.ScriptDescriptor, uint32, error) {
|
2021-07-15 02:16:13 +02:00
|
|
|
|
|
|
|
switch {
|
|
|
|
// If we are not the initiator of a leased channel, then the remote
|
|
|
|
// party has an additional CLTV requirement in addition to the 1 block
|
|
|
|
// CSV requirement.
|
|
|
|
case chanType.HasLeaseExpiration() && !initiator:
|
|
|
|
script, err := input.LeaseCommitScriptToRemoteConfirmed(
|
2023-01-20 02:05:58 +01:00
|
|
|
remoteKey, leaseExpiry,
|
2021-07-15 02:16:13 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p2wsh, err := input.WitnessScriptHash(script)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2023-08-08 06:09:58 +02:00
|
|
|
return &WitnessScriptDesc{
|
|
|
|
OutputScript: p2wsh,
|
2021-07-15 02:16:13 +02:00
|
|
|
WitnessScript: script,
|
|
|
|
}, 1, nil
|
2020-03-06 16:11:46 +01:00
|
|
|
|
2023-01-20 02:05:58 +01:00
|
|
|
// For taproot channels, we'll use a slightly different format, where
|
|
|
|
// we use a NUMS key to force the remote party to take a script path,
|
|
|
|
// with the sole tap leaf enforcing the 1 CSV delay.
|
|
|
|
case chanType.IsTaproot():
|
2023-03-02 06:38:53 +01:00
|
|
|
toRemoteScriptTree, err := input.NewRemoteCommitScriptTree(
|
2023-01-20 02:05:58 +01:00
|
|
|
remoteKey,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2023-08-08 06:09:58 +02:00
|
|
|
return toRemoteScriptTree, 1, nil
|
2023-01-20 02:05:58 +01:00
|
|
|
|
2020-03-06 16:11:46 +01:00
|
|
|
// If this channel type has anchors, we derive the delayed to_remote
|
|
|
|
// script.
|
2021-07-15 02:16:13 +02:00
|
|
|
case chanType.HasAnchors():
|
2023-01-20 02:05:58 +01:00
|
|
|
script, err := input.CommitScriptToRemoteConfirmed(remoteKey)
|
2020-03-06 16:11:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p2wsh, err := input.WitnessScriptHash(script)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2020-01-06 11:42:04 +01:00
|
|
|
|
2023-08-08 06:09:58 +02:00
|
|
|
return &WitnessScriptDesc{
|
|
|
|
OutputScript: p2wsh,
|
2020-03-06 16:11:46 +01:00
|
|
|
WitnessScript: script,
|
|
|
|
}, 1, nil
|
|
|
|
|
2021-07-15 02:16:13 +02:00
|
|
|
default:
|
|
|
|
// Otherwise the to_remote will be a simple p2wkh.
|
2023-01-20 02:05:58 +01:00
|
|
|
p2wkh, err := input.CommitScriptUnencumbered(remoteKey)
|
2021-07-15 02:16:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2020-01-06 11:42:04 +01:00
|
|
|
|
2021-07-15 02:16:13 +02:00
|
|
|
// Since this is a regular P2WKH, the WitnessScipt and PkScript
|
|
|
|
// should both be set to the script hash.
|
2023-08-08 06:09:58 +02:00
|
|
|
return &WitnessScriptDesc{
|
|
|
|
OutputScript: p2wkh,
|
2021-07-15 02:16:13 +02:00
|
|
|
WitnessScript: p2wkh,
|
|
|
|
}, 0, nil
|
|
|
|
}
|
2020-03-06 16:11:46 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:11:47 +01:00
|
|
|
// HtlcSigHashType returns the sighash type to use for HTLC success and timeout
|
|
|
|
// transactions given the channel type.
|
|
|
|
func HtlcSigHashType(chanType channeldb.ChannelType) txscript.SigHashType {
|
|
|
|
if chanType.HasAnchors() {
|
|
|
|
return txscript.SigHashSingle | txscript.SigHashAnyOneCanPay
|
|
|
|
}
|
|
|
|
|
|
|
|
return txscript.SigHashAll
|
|
|
|
}
|
|
|
|
|
2020-12-09 12:24:01 +01:00
|
|
|
// HtlcSignDetails converts the passed parameters to a SignDetails valid for
|
|
|
|
// this channel type. For non-anchor channels this will return nil.
|
|
|
|
func HtlcSignDetails(chanType channeldb.ChannelType, signDesc input.SignDescriptor,
|
|
|
|
sigHash txscript.SigHashType, peerSig input.Signature) *input.SignDetails {
|
|
|
|
|
|
|
|
// Non-anchor channels don't need sign details, as the HTLC second
|
|
|
|
// level cannot be altered.
|
|
|
|
if !chanType.HasAnchors() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &input.SignDetails{
|
|
|
|
SignDesc: signDesc,
|
|
|
|
SigHashType: sigHash,
|
|
|
|
PeerSig: peerSig,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 16:11:47 +01:00
|
|
|
// HtlcSecondLevelInputSequence dictates the sequence number we must use on the
|
|
|
|
// input to a second level HTLC transaction.
|
|
|
|
func HtlcSecondLevelInputSequence(chanType channeldb.ChannelType) uint32 {
|
|
|
|
if chanType.HasAnchors() {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-07-15 02:06:13 +02:00
|
|
|
// SecondLevelHtlcScript derives the appropriate second level HTLC script based
|
|
|
|
// on the channel's commitment type. It is the uniform script that's used as the
|
|
|
|
// output for the second-level HTLC transactions. The second level transaction
|
|
|
|
// act as a sort of covenant, ensuring that a 2-of-2 multi-sig output can only
|
2021-07-15 02:16:13 +02:00
|
|
|
// be spent in a particular way, and to a particular output. The `initiator`
|
2022-01-13 17:29:43 +01:00
|
|
|
// argument should correspond to the owner of the commitment transaction which
|
2021-07-15 02:16:13 +02:00
|
|
|
// we are generating the to_local script for.
|
|
|
|
func SecondLevelHtlcScript(chanType channeldb.ChannelType, initiator bool,
|
|
|
|
revocationKey, delayKey *btcec.PublicKey,
|
2023-08-08 06:09:58 +02:00
|
|
|
csvDelay, leaseExpiry uint32) (input.ScriptDescriptor, error) {
|
2021-07-15 02:06:13 +02:00
|
|
|
|
2021-07-15 02:16:13 +02:00
|
|
|
switch {
|
2023-01-20 03:31:21 +01:00
|
|
|
// For taproot channels, the pkScript is a segwit v1 p2tr output.
|
|
|
|
case chanType.IsTaproot():
|
2023-08-08 06:09:58 +02:00
|
|
|
return input.TaprootSecondLevelScriptTree(
|
2023-01-20 03:31:21 +01:00
|
|
|
revocationKey, delayKey, csvDelay,
|
|
|
|
)
|
|
|
|
|
2021-07-15 02:16:13 +02:00
|
|
|
// If we are the initiator of a leased channel, then we have an
|
2023-01-20 03:31:21 +01:00
|
|
|
// additional CLTV requirement in addition to the usual CSV
|
|
|
|
// requirement.
|
2021-07-15 02:16:13 +02:00
|
|
|
case initiator && chanType.HasLeaseExpiration():
|
2023-08-08 06:09:58 +02:00
|
|
|
witnessScript, err := input.LeaseSecondLevelHtlcScript(
|
2021-07-15 02:16:13 +02:00
|
|
|
revocationKey, delayKey, csvDelay, leaseExpiry,
|
|
|
|
)
|
2023-08-09 07:22:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-15 02:16:13 +02:00
|
|
|
|
2023-08-08 06:09:58 +02:00
|
|
|
pkScript, err := input.WitnessScriptHash(witnessScript)
|
2023-01-20 03:31:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-08 06:09:58 +02:00
|
|
|
return &WitnessScriptDesc{
|
|
|
|
OutputScript: pkScript,
|
|
|
|
WitnessScript: witnessScript,
|
|
|
|
}, nil
|
|
|
|
|
2021-07-15 02:16:13 +02:00
|
|
|
default:
|
2023-08-08 06:09:58 +02:00
|
|
|
witnessScript, err := input.SecondLevelHtlcScript(
|
2021-07-15 02:16:13 +02:00
|
|
|
revocationKey, delayKey, csvDelay,
|
|
|
|
)
|
2023-08-09 07:22:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-15 02:06:13 +02:00
|
|
|
|
2023-08-08 06:09:58 +02:00
|
|
|
pkScript, err := input.WitnessScriptHash(witnessScript)
|
2023-01-20 03:31:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-15 02:06:13 +02:00
|
|
|
|
2023-08-08 06:09:58 +02:00
|
|
|
return &WitnessScriptDesc{
|
|
|
|
OutputScript: pkScript,
|
|
|
|
WitnessScript: witnessScript,
|
|
|
|
}, nil
|
|
|
|
}
|
2021-07-15 02:06:13 +02:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:11:46 +01:00
|
|
|
// CommitWeight returns the base commitment weight before adding HTLCs.
|
|
|
|
func CommitWeight(chanType channeldb.ChannelType) int64 {
|
2023-03-02 06:30:57 +01:00
|
|
|
switch {
|
|
|
|
case chanType.IsTaproot():
|
|
|
|
return input.TaprootCommitWeight
|
|
|
|
|
2020-03-06 16:11:46 +01:00
|
|
|
// If this commitment has anchors, it will be slightly heavier.
|
2023-03-02 06:30:57 +01:00
|
|
|
case chanType.HasAnchors():
|
2020-03-06 16:11:46 +01:00
|
|
|
return input.AnchorCommitWeight
|
|
|
|
|
2023-03-02 06:30:57 +01:00
|
|
|
default:
|
|
|
|
return input.CommitWeight
|
|
|
|
}
|
2020-03-06 16:11:46 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:11:49 +01:00
|
|
|
// HtlcTimeoutFee returns the fee in satoshis required for an HTLC timeout
|
|
|
|
// transaction based on the current fee rate.
|
|
|
|
func HtlcTimeoutFee(chanType channeldb.ChannelType,
|
|
|
|
feePerKw chainfee.SatPerKWeight) btcutil.Amount {
|
|
|
|
|
2023-03-02 06:31:43 +01:00
|
|
|
switch {
|
2020-12-07 14:14:20 +01:00
|
|
|
// For zero-fee HTLC channels, this will always be zero, regardless of
|
|
|
|
// feerate.
|
2023-03-02 06:31:43 +01:00
|
|
|
case chanType.ZeroHtlcTxFee() || chanType.IsTaproot():
|
2020-12-07 14:14:20 +01:00
|
|
|
return 0
|
|
|
|
|
2023-03-02 06:31:43 +01:00
|
|
|
case chanType.HasAnchors():
|
2020-03-06 16:11:49 +01:00
|
|
|
return feePerKw.FeeForWeight(input.HtlcTimeoutWeightConfirmed)
|
|
|
|
|
2023-03-02 06:31:43 +01:00
|
|
|
default:
|
|
|
|
return feePerKw.FeeForWeight(input.HtlcTimeoutWeight)
|
|
|
|
}
|
2020-03-06 16:11:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// HtlcSuccessFee returns the fee in satoshis required for an HTLC success
|
|
|
|
// transaction based on the current fee rate.
|
|
|
|
func HtlcSuccessFee(chanType channeldb.ChannelType,
|
|
|
|
feePerKw chainfee.SatPerKWeight) btcutil.Amount {
|
|
|
|
|
2023-03-02 06:31:43 +01:00
|
|
|
switch {
|
2020-12-07 14:14:20 +01:00
|
|
|
// For zero-fee HTLC channels, this will always be zero, regardless of
|
|
|
|
// feerate.
|
2023-03-02 06:31:43 +01:00
|
|
|
case chanType.ZeroHtlcTxFee() || chanType.IsTaproot():
|
2020-12-07 14:14:20 +01:00
|
|
|
return 0
|
2023-01-20 02:05:58 +01:00
|
|
|
|
2023-03-02 06:31:43 +01:00
|
|
|
case chanType.HasAnchors():
|
2020-03-06 16:11:49 +01:00
|
|
|
return feePerKw.FeeForWeight(input.HtlcSuccessWeightConfirmed)
|
2023-01-20 02:05:58 +01:00
|
|
|
|
2023-03-02 06:31:43 +01:00
|
|
|
default:
|
|
|
|
return feePerKw.FeeForWeight(input.HtlcSuccessWeight)
|
|
|
|
}
|
2020-03-06 16:11:49 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 16:11:46 +01:00
|
|
|
// CommitScriptAnchors return the scripts to use for the local and remote
|
|
|
|
// anchor.
|
2023-01-20 02:06:38 +01:00
|
|
|
func CommitScriptAnchors(chanType channeldb.ChannelType,
|
|
|
|
localChanCfg, remoteChanCfg *channeldb.ChannelConfig,
|
2023-08-08 06:09:58 +02:00
|
|
|
keyRing *CommitmentKeyRing) (
|
|
|
|
input.ScriptDescriptor, input.ScriptDescriptor, error) {
|
2020-03-06 16:11:46 +01:00
|
|
|
|
2023-01-20 02:06:38 +01:00
|
|
|
var (
|
2023-08-08 06:09:58 +02:00
|
|
|
anchorScript func(key *btcec.PublicKey) (
|
|
|
|
input.ScriptDescriptor, error)
|
|
|
|
|
|
|
|
keySelector func(*channeldb.ChannelConfig,
|
2023-01-20 02:06:38 +01:00
|
|
|
bool) *btcec.PublicKey
|
|
|
|
)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
// For taproot channels, the anchor is slightly different: the top
|
|
|
|
// level key is now the (relative) local delay and remote public key,
|
|
|
|
// since these are fully revealed once the commitment hits the chain.
|
|
|
|
case chanType.IsTaproot():
|
2023-08-08 06:09:58 +02:00
|
|
|
anchorScript = func(key *btcec.PublicKey,
|
|
|
|
) (input.ScriptDescriptor, error) {
|
2023-08-09 07:22:12 +02:00
|
|
|
|
2023-08-08 06:09:58 +02:00
|
|
|
return input.NewAnchorScriptTree(
|
2023-03-02 06:38:53 +01:00
|
|
|
key,
|
|
|
|
)
|
2020-03-06 16:11:46 +01:00
|
|
|
}
|
|
|
|
|
2023-01-20 02:06:38 +01:00
|
|
|
keySelector = func(cfg *channeldb.ChannelConfig,
|
|
|
|
local bool) *btcec.PublicKey {
|
|
|
|
|
|
|
|
if local {
|
|
|
|
return keyRing.ToLocalKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyRing.ToRemoteKey
|
2020-03-06 16:11:46 +01:00
|
|
|
}
|
|
|
|
|
2023-01-20 02:06:38 +01:00
|
|
|
// For normal channels we'll use the multi-sig keys since those are
|
|
|
|
// revealed when the channel closes
|
|
|
|
default:
|
|
|
|
// For normal channels, we'll create a p2wsh script based on
|
|
|
|
// the target key.
|
2023-08-08 06:09:58 +02:00
|
|
|
anchorScript = func(key *btcec.PublicKey,
|
|
|
|
) (input.ScriptDescriptor, error) {
|
|
|
|
|
2023-01-20 02:06:38 +01:00
|
|
|
script, err := input.CommitScriptAnchor(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
scriptHash, err := input.WitnessScriptHash(script)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-08 06:09:58 +02:00
|
|
|
return &WitnessScriptDesc{
|
|
|
|
OutputScript: scriptHash,
|
2023-01-20 02:06:38 +01:00
|
|
|
WitnessScript: script,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the existing channels, we'll always select the multi-sig
|
|
|
|
// key from the party's channel config.
|
|
|
|
keySelector = func(cfg *channeldb.ChannelConfig,
|
|
|
|
_ bool) *btcec.PublicKey {
|
|
|
|
|
|
|
|
return cfg.MultiSigKey.PubKey
|
|
|
|
}
|
2020-03-06 16:11:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the script used for the anchor output spendable by the local
|
|
|
|
// node.
|
2023-01-20 02:06:38 +01:00
|
|
|
localAnchor, err := anchorScript(keySelector(localChanCfg, true))
|
2020-03-06 16:11:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-14 02:50:45 +01:00
|
|
|
// And the anchor spendable by the remote node.
|
2023-01-20 02:06:38 +01:00
|
|
|
remoteAnchor, err := anchorScript(keySelector(remoteChanCfg, false))
|
2020-03-06 16:11:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return localAnchor, remoteAnchor, nil
|
2020-01-06 11:42:04 +01:00
|
|
|
}
|
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
// CommitmentBuilder is a type that wraps the type of channel we are dealing
|
|
|
|
// with, and abstracts the various ways of constructing commitment
|
|
|
|
// transactions.
|
|
|
|
type CommitmentBuilder struct {
|
|
|
|
// chanState is the underlying channels's state struct, used to
|
|
|
|
// determine the type of channel we are dealing with, and relevant
|
|
|
|
// parameters.
|
|
|
|
chanState *channeldb.OpenChannel
|
|
|
|
|
|
|
|
// obfuscator is a 48-bit state hint that's used to obfuscate the
|
|
|
|
// current state number on the commitment transactions.
|
|
|
|
obfuscator [StateHintSize]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCommitmentBuilder creates a new CommitmentBuilder from chanState.
|
|
|
|
func NewCommitmentBuilder(chanState *channeldb.OpenChannel) *CommitmentBuilder {
|
2020-03-06 16:11:46 +01:00
|
|
|
// The anchor channel type MUST be tweakless.
|
|
|
|
if chanState.ChanType.HasAnchors() && !chanState.ChanType.IsTweakless() {
|
|
|
|
panic("invalid channel type combination")
|
|
|
|
}
|
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
return &CommitmentBuilder{
|
|
|
|
chanState: chanState,
|
|
|
|
obfuscator: createStateHintObfuscator(chanState),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
// createStateHintObfuscator derives and assigns the state hint obfuscator for
|
|
|
|
// the channel, which is used to encode the commitment height in the sequence
|
|
|
|
// number of commitment transaction inputs.
|
2020-01-06 11:42:03 +01:00
|
|
|
func createStateHintObfuscator(state *channeldb.OpenChannel) [StateHintSize]byte {
|
2020-01-06 11:42:03 +01:00
|
|
|
if state.IsInitiator {
|
2020-01-06 11:42:03 +01:00
|
|
|
return DeriveStateHintObfuscator(
|
2020-01-06 11:42:03 +01:00
|
|
|
state.LocalChanCfg.PaymentBasePoint.PubKey,
|
|
|
|
state.RemoteChanCfg.PaymentBasePoint.PubKey,
|
|
|
|
)
|
|
|
|
}
|
2020-01-06 11:42:03 +01:00
|
|
|
|
|
|
|
return DeriveStateHintObfuscator(
|
|
|
|
state.RemoteChanCfg.PaymentBasePoint.PubKey,
|
|
|
|
state.LocalChanCfg.PaymentBasePoint.PubKey,
|
|
|
|
)
|
2020-01-06 11:42:03 +01:00
|
|
|
}
|
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
// unsignedCommitmentTx is the final commitment created from evaluating an HTLC
|
|
|
|
// view at a given height, along with some meta data.
|
|
|
|
type unsignedCommitmentTx struct {
|
|
|
|
// txn is the final, unsigned commitment transaction for this view.
|
|
|
|
txn *wire.MsgTx
|
2020-01-06 11:42:03 +01:00
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
// fee is the total fee of the commitment transaction.
|
|
|
|
fee btcutil.Amount
|
|
|
|
|
2020-03-31 00:49:43 +02:00
|
|
|
// ourBalance is our balance on this commitment *after* subtracting
|
|
|
|
// commitment fees and anchor outputs. This can be different than the
|
|
|
|
// balances before creating the commitment transaction as one party must
|
|
|
|
// pay the commitment fee.
|
|
|
|
ourBalance lnwire.MilliSatoshi
|
|
|
|
|
|
|
|
// theirBalance is their balance of this commitment *after* subtracting
|
|
|
|
// commitment fees and anchor outputs. This can be different than the
|
|
|
|
// balances before creating the commitment transaction as one party must
|
|
|
|
// pay the commitment fee.
|
2020-01-06 11:42:03 +01:00
|
|
|
theirBalance lnwire.MilliSatoshi
|
2020-03-31 00:50:10 +02:00
|
|
|
|
|
|
|
// cltvs is a sorted list of CLTV deltas for each HTLC on the commitment
|
|
|
|
// transaction. Any non-htlc outputs will have a CLTV delay of zero.
|
|
|
|
cltvs []uint32
|
2020-01-06 11:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// createUnsignedCommitmentTx generates the unsigned commitment transaction for
|
|
|
|
// a commitment view and returns it as part of the unsignedCommitmentTx. The
|
|
|
|
// passed in balances should be balances *before* subtracting any commitment
|
2020-03-06 16:11:46 +01:00
|
|
|
// fees, but after anchor outputs.
|
2020-01-06 11:42:03 +01:00
|
|
|
func (cb *CommitmentBuilder) createUnsignedCommitmentTx(ourBalance,
|
|
|
|
theirBalance lnwire.MilliSatoshi, isOurs bool,
|
|
|
|
feePerKw chainfee.SatPerKWeight, height uint64,
|
|
|
|
filteredHTLCView *htlcView,
|
|
|
|
keyRing *CommitmentKeyRing) (*unsignedCommitmentTx, error) {
|
|
|
|
|
|
|
|
dustLimit := cb.chanState.LocalChanCfg.DustLimit
|
|
|
|
if !isOurs {
|
|
|
|
dustLimit = cb.chanState.RemoteChanCfg.DustLimit
|
|
|
|
}
|
2020-01-06 11:42:03 +01:00
|
|
|
|
|
|
|
numHTLCs := int64(0)
|
|
|
|
for _, htlc := range filteredHTLCView.ourUpdates {
|
2021-09-28 17:34:10 +02:00
|
|
|
if HtlcIsDust(
|
2020-03-06 16:11:49 +01:00
|
|
|
cb.chanState.ChanType, false, isOurs, feePerKw,
|
|
|
|
htlc.Amount.ToSatoshis(), dustLimit,
|
|
|
|
) {
|
2022-02-07 13:58:28 +01:00
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
numHTLCs++
|
|
|
|
}
|
|
|
|
for _, htlc := range filteredHTLCView.theirUpdates {
|
2021-09-28 17:34:10 +02:00
|
|
|
if HtlcIsDust(
|
2020-03-06 16:11:49 +01:00
|
|
|
cb.chanState.ChanType, true, isOurs, feePerKw,
|
|
|
|
htlc.Amount.ToSatoshis(), dustLimit,
|
|
|
|
) {
|
2022-02-07 13:58:28 +01:00
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
numHTLCs++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll calculate the fee for the commitment transaction based
|
|
|
|
// on its total weight. Once we have the total weight, we'll multiply
|
|
|
|
// by the current fee-per-kw, then divide by 1000 to get the proper
|
|
|
|
// fee.
|
2020-03-06 16:11:46 +01:00
|
|
|
totalCommitWeight := CommitWeight(cb.chanState.ChanType) +
|
|
|
|
input.HTLCWeight*numHTLCs
|
2020-01-06 11:42:03 +01:00
|
|
|
|
|
|
|
// With the weight known, we can now calculate the commitment fee,
|
|
|
|
// ensuring that we account for any dust outputs trimmed above.
|
2020-01-06 11:42:03 +01:00
|
|
|
commitFee := feePerKw.FeeForWeight(totalCommitWeight)
|
2020-01-06 11:42:03 +01:00
|
|
|
commitFeeMSat := lnwire.NewMSatFromSatoshis(commitFee)
|
|
|
|
|
|
|
|
// Currently, within the protocol, the initiator always pays the fees.
|
|
|
|
// So we'll subtract the fee amount from the balance of the current
|
|
|
|
// initiator. If the initiator is unable to pay the fee fully, then
|
|
|
|
// their entire output is consumed.
|
|
|
|
switch {
|
2020-01-06 11:42:03 +01:00
|
|
|
case cb.chanState.IsInitiator && commitFee > ourBalance.ToSatoshis():
|
2020-01-06 11:42:03 +01:00
|
|
|
ourBalance = 0
|
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
case cb.chanState.IsInitiator:
|
2020-01-06 11:42:03 +01:00
|
|
|
ourBalance -= commitFeeMSat
|
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
case !cb.chanState.IsInitiator && commitFee > theirBalance.ToSatoshis():
|
2020-01-06 11:42:03 +01:00
|
|
|
theirBalance = 0
|
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
case !cb.chanState.IsInitiator:
|
2020-01-06 11:42:03 +01:00
|
|
|
theirBalance -= commitFeeMSat
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
commitTx *wire.MsgTx
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
// Depending on whether the transaction is ours or not, we call
|
2020-01-06 11:42:03 +01:00
|
|
|
// CreateCommitTx with parameters matching the perspective, to generate
|
2020-01-06 11:42:03 +01:00
|
|
|
// a new commitment transaction with all the latest unsettled/un-timed
|
|
|
|
// out HTLCs.
|
2021-07-15 02:16:13 +02:00
|
|
|
var leaseExpiry uint32
|
|
|
|
if cb.chanState.ChanType.HasLeaseExpiration() {
|
|
|
|
leaseExpiry = cb.chanState.ThawHeight
|
|
|
|
}
|
2020-01-06 11:42:03 +01:00
|
|
|
if isOurs {
|
2020-01-06 11:42:03 +01:00
|
|
|
commitTx, err = CreateCommitTx(
|
2020-01-06 11:42:04 +01:00
|
|
|
cb.chanState.ChanType, fundingTxIn(cb.chanState), keyRing,
|
|
|
|
&cb.chanState.LocalChanCfg, &cb.chanState.RemoteChanCfg,
|
|
|
|
ourBalance.ToSatoshis(), theirBalance.ToSatoshis(),
|
2021-07-15 02:16:13 +02:00
|
|
|
numHTLCs, cb.chanState.IsInitiator, leaseExpiry,
|
2020-01-06 11:42:03 +01:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
commitTx, err = CreateCommitTx(
|
2020-01-06 11:42:04 +01:00
|
|
|
cb.chanState.ChanType, fundingTxIn(cb.chanState), keyRing,
|
|
|
|
&cb.chanState.RemoteChanCfg, &cb.chanState.LocalChanCfg,
|
|
|
|
theirBalance.ToSatoshis(), ourBalance.ToSatoshis(),
|
2021-07-15 02:16:13 +02:00
|
|
|
numHTLCs, !cb.chanState.IsInitiator, leaseExpiry,
|
2020-01-06 11:42:03 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-01-06 11:42:03 +01:00
|
|
|
return nil, err
|
2020-01-06 11:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now add all the HTLC outputs to the commitment transaction.
|
|
|
|
// Each output includes an off-chain 2-of-2 covenant clause, so we'll
|
|
|
|
// need the objective local/remote keys for this particular commitment
|
|
|
|
// as well. For any non-dust HTLCs that are manifested on the commitment
|
|
|
|
// transaction, we'll also record its CLTV which is required to sort the
|
|
|
|
// commitment transaction below. The slice is initially sized to the
|
|
|
|
// number of existing outputs, since any outputs already added are
|
|
|
|
// commitment outputs and should correspond to zero values for the
|
|
|
|
// purposes of sorting.
|
|
|
|
cltvs := make([]uint32, len(commitTx.TxOut))
|
|
|
|
for _, htlc := range filteredHTLCView.ourUpdates {
|
2021-09-28 17:34:10 +02:00
|
|
|
if HtlcIsDust(
|
2020-03-06 16:11:49 +01:00
|
|
|
cb.chanState.ChanType, false, isOurs, feePerKw,
|
|
|
|
htlc.Amount.ToSatoshis(), dustLimit,
|
|
|
|
) {
|
2022-02-07 13:58:28 +01:00
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-06 16:11:45 +01:00
|
|
|
err := addHTLC(
|
|
|
|
commitTx, isOurs, false, htlc, keyRing,
|
|
|
|
cb.chanState.ChanType,
|
|
|
|
)
|
2020-01-06 11:42:03 +01:00
|
|
|
if err != nil {
|
2020-01-06 11:42:03 +01:00
|
|
|
return nil, err
|
2020-01-06 11:42:03 +01:00
|
|
|
}
|
2022-02-07 13:58:25 +01:00
|
|
|
cltvs = append(cltvs, htlc.Timeout) // nolint:makezero
|
2020-01-06 11:42:03 +01:00
|
|
|
}
|
|
|
|
for _, htlc := range filteredHTLCView.theirUpdates {
|
2021-09-28 17:34:10 +02:00
|
|
|
if HtlcIsDust(
|
2020-03-06 16:11:49 +01:00
|
|
|
cb.chanState.ChanType, true, isOurs, feePerKw,
|
|
|
|
htlc.Amount.ToSatoshis(), dustLimit,
|
|
|
|
) {
|
2022-02-07 13:58:28 +01:00
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-06 16:11:45 +01:00
|
|
|
err := addHTLC(
|
|
|
|
commitTx, isOurs, true, htlc, keyRing,
|
|
|
|
cb.chanState.ChanType,
|
|
|
|
)
|
2020-01-06 11:42:03 +01:00
|
|
|
if err != nil {
|
2020-01-06 11:42:03 +01:00
|
|
|
return nil, err
|
2020-01-06 11:42:03 +01:00
|
|
|
}
|
2022-02-07 13:58:25 +01:00
|
|
|
cltvs = append(cltvs, htlc.Timeout) // nolint:makezero
|
2020-01-06 11:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the state hint of the commitment transaction to facilitate
|
|
|
|
// quickly recovering the necessary penalty state in the case of an
|
|
|
|
// uncooperative broadcast.
|
2020-01-06 11:42:03 +01:00
|
|
|
err = SetStateNumHint(commitTx, height, cb.obfuscator)
|
2020-01-06 11:42:03 +01:00
|
|
|
if err != nil {
|
2020-01-06 11:42:03 +01:00
|
|
|
return nil, err
|
2020-01-06 11:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the transactions according to the agreed upon canonical
|
|
|
|
// ordering. This lets us skip sending the entire transaction over,
|
|
|
|
// instead we'll just send signatures.
|
|
|
|
InPlaceCommitSort(commitTx, cltvs)
|
|
|
|
|
|
|
|
// Next, we'll ensure that we don't accidentally create a commitment
|
|
|
|
// transaction which would be invalid by consensus.
|
|
|
|
uTx := btcutil.NewTx(commitTx)
|
|
|
|
if err := blockchain.CheckTransactionSanity(uTx); err != nil {
|
2020-01-06 11:42:03 +01:00
|
|
|
return nil, err
|
2020-01-06 11:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we'll assert that were not attempting to draw more out of
|
|
|
|
// the channel that was originally placed within it.
|
|
|
|
var totalOut btcutil.Amount
|
|
|
|
for _, txOut := range commitTx.TxOut {
|
|
|
|
totalOut += btcutil.Amount(txOut.Value)
|
|
|
|
}
|
2021-02-22 18:07:21 +01:00
|
|
|
if totalOut+commitFee > cb.chanState.Capacity {
|
2020-01-06 11:42:03 +01:00
|
|
|
return nil, fmt.Errorf("height=%v, for ChannelPoint(%v) "+
|
|
|
|
"attempts to consume %v while channel capacity is %v",
|
|
|
|
height, cb.chanState.FundingOutpoint,
|
2021-02-22 18:07:21 +01:00
|
|
|
totalOut+commitFee, cb.chanState.Capacity)
|
2020-01-06 11:42:03 +01:00
|
|
|
}
|
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
return &unsignedCommitmentTx{
|
|
|
|
txn: commitTx,
|
|
|
|
fee: commitFee,
|
|
|
|
ourBalance: ourBalance,
|
|
|
|
theirBalance: theirBalance,
|
2020-03-31 00:50:10 +02:00
|
|
|
cltvs: cltvs,
|
2020-01-06 11:42:03 +01:00
|
|
|
}, nil
|
2020-01-06 11:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateCommitTx creates a commitment transaction, spending from specified
|
|
|
|
// funding output. The commitment transaction contains two outputs: one local
|
|
|
|
// output paying to the "owner" of the commitment transaction which can be
|
|
|
|
// spent after a relative block delay or revocation event, and a remote output
|
|
|
|
// paying the counterparty within the channel, which can be spent immediately
|
2021-07-15 02:16:13 +02:00
|
|
|
// or after a delay depending on the commitment type. The `initiator` argument
|
2022-01-13 17:29:43 +01:00
|
|
|
// should correspond to the owner of the commitment transaction we are creating.
|
2020-01-06 11:42:04 +01:00
|
|
|
func CreateCommitTx(chanType channeldb.ChannelType,
|
|
|
|
fundingOutput wire.TxIn, keyRing *CommitmentKeyRing,
|
2020-01-06 11:42:03 +01:00
|
|
|
localChanCfg, remoteChanCfg *channeldb.ChannelConfig,
|
2020-03-06 16:11:46 +01:00
|
|
|
amountToLocal, amountToRemote btcutil.Amount,
|
2021-07-15 02:16:13 +02:00
|
|
|
numHTLCs int64, initiator bool, leaseExpiry uint32) (*wire.MsgTx, error) {
|
2020-01-06 11:42:03 +01:00
|
|
|
|
|
|
|
// First, we create the script for the delayed "pay-to-self" output.
|
|
|
|
// This output has 2 main redemption clauses: either we can redeem the
|
|
|
|
// output after a relative block delay, or the remote node can claim
|
|
|
|
// the funds with the revocation key if we broadcast a revoked
|
|
|
|
// commitment transaction.
|
2021-07-15 02:06:13 +02:00
|
|
|
toLocalScript, err := CommitScriptToSelf(
|
2021-07-15 02:16:13 +02:00
|
|
|
chanType, initiator, keyRing.ToLocalKey, keyRing.RevocationKey,
|
|
|
|
uint32(localChanCfg.CsvDelay), leaseExpiry,
|
2020-01-06 11:42:03 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-06 11:42:04 +01:00
|
|
|
// Next, we create the script paying to the remote.
|
2020-03-06 16:11:46 +01:00
|
|
|
toRemoteScript, _, err := CommitScriptToRemote(
|
2021-07-15 02:16:13 +02:00
|
|
|
chanType, initiator, keyRing.ToRemoteKey, leaseExpiry,
|
2020-01-06 11:42:03 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that both output scripts have been created, we can finally create
|
|
|
|
// the transaction itself. We use a transaction version of 2 since CSV
|
|
|
|
// will fail unless the tx version is >= 2.
|
|
|
|
commitTx := wire.NewMsgTx(2)
|
|
|
|
commitTx.AddTxIn(&fundingOutput)
|
|
|
|
|
|
|
|
// Avoid creating dust outputs within the commitment transaction.
|
2020-03-06 16:11:46 +01:00
|
|
|
localOutput := amountToLocal >= localChanCfg.DustLimit
|
|
|
|
if localOutput {
|
2020-01-06 11:42:03 +01:00
|
|
|
commitTx.AddTxOut(&wire.TxOut{
|
2023-08-08 06:09:58 +02:00
|
|
|
PkScript: toLocalScript.PkScript(),
|
2020-01-06 11:42:03 +01:00
|
|
|
Value: int64(amountToLocal),
|
|
|
|
})
|
|
|
|
}
|
2020-03-06 16:11:46 +01:00
|
|
|
|
|
|
|
remoteOutput := amountToRemote >= localChanCfg.DustLimit
|
|
|
|
if remoteOutput {
|
2020-01-06 11:42:03 +01:00
|
|
|
commitTx.AddTxOut(&wire.TxOut{
|
2023-08-08 06:09:58 +02:00
|
|
|
PkScript: toRemoteScript.PkScript(),
|
2020-01-06 11:42:03 +01:00
|
|
|
Value: int64(amountToRemote),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-06 16:11:46 +01:00
|
|
|
// If this channel type has anchors, we'll also add those.
|
|
|
|
if chanType.HasAnchors() {
|
|
|
|
localAnchor, remoteAnchor, err := CommitScriptAnchors(
|
2023-01-20 02:06:38 +01:00
|
|
|
chanType, localChanCfg, remoteChanCfg, keyRing,
|
2020-03-06 16:11:46 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add local anchor output only if we have a commitment output
|
|
|
|
// or there are HTLCs.
|
|
|
|
if localOutput || numHTLCs > 0 {
|
|
|
|
commitTx.AddTxOut(&wire.TxOut{
|
2023-08-08 06:09:58 +02:00
|
|
|
PkScript: localAnchor.PkScript(),
|
2020-03-06 16:11:46 +01:00
|
|
|
Value: int64(anchorSize),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add anchor output to remote only if they have a commitment
|
|
|
|
// output or there are HTLCs.
|
|
|
|
if remoteOutput || numHTLCs > 0 {
|
|
|
|
commitTx.AddTxOut(&wire.TxOut{
|
2023-08-08 06:09:58 +02:00
|
|
|
PkScript: remoteAnchor.PkScript(),
|
2020-03-06 16:11:46 +01:00
|
|
|
Value: int64(anchorSize),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
return commitTx, nil
|
|
|
|
}
|
|
|
|
|
2020-08-24 15:26:06 +02:00
|
|
|
// CoopCloseBalance returns the final balances that should be used to create
|
|
|
|
// the cooperative close tx, given the channel type and transaction fee.
|
|
|
|
func CoopCloseBalance(chanType channeldb.ChannelType, isInitiator bool,
|
|
|
|
coopCloseFee btcutil.Amount, localCommit channeldb.ChannelCommitment) (
|
2020-08-24 15:44:13 +02:00
|
|
|
btcutil.Amount, btcutil.Amount, error) {
|
2020-08-24 15:26:06 +02:00
|
|
|
|
|
|
|
// Get both parties' balances from the latest commitment.
|
|
|
|
ourBalance := localCommit.LocalBalance.ToSatoshis()
|
|
|
|
theirBalance := localCommit.RemoteBalance.ToSatoshis()
|
|
|
|
|
|
|
|
// We'll make sure we account for the complete balance by adding the
|
|
|
|
// current dangling commitment fee to the balance of the initiator.
|
2020-08-21 13:38:36 +02:00
|
|
|
initiatorDelta := localCommit.CommitFee
|
|
|
|
|
|
|
|
// Since the initiator's balance also is stored after subtracting the
|
|
|
|
// anchor values, add that back in case this was an anchor commitment.
|
|
|
|
if chanType.HasAnchors() {
|
|
|
|
initiatorDelta += 2 * anchorSize
|
|
|
|
}
|
|
|
|
|
|
|
|
// The initiator will pay the full coop close fee, subtract that value
|
|
|
|
// from their balance.
|
|
|
|
initiatorDelta -= coopCloseFee
|
|
|
|
|
2020-08-24 15:26:06 +02:00
|
|
|
if isInitiator {
|
2020-08-21 13:38:36 +02:00
|
|
|
ourBalance += initiatorDelta
|
2020-08-24 15:26:06 +02:00
|
|
|
} else {
|
2020-08-21 13:38:36 +02:00
|
|
|
theirBalance += initiatorDelta
|
2020-08-24 15:26:06 +02:00
|
|
|
}
|
|
|
|
|
2020-08-24 15:44:13 +02:00
|
|
|
// During fee negotiation it should always be verified that the
|
|
|
|
// initiator can pay the proposed fee, but we do a sanity check just to
|
|
|
|
// be sure here.
|
|
|
|
if ourBalance < 0 || theirBalance < 0 {
|
|
|
|
return 0, 0, fmt.Errorf("initiator cannot afford proposed " +
|
|
|
|
"coop close fee")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ourBalance, theirBalance, nil
|
2020-08-24 15:26:06 +02:00
|
|
|
}
|
|
|
|
|
2023-01-20 04:02:28 +01:00
|
|
|
// genSegwitV0HtlcScript generates the HTLC scripts for a normal segwit v0
|
|
|
|
// channel.
|
|
|
|
func genSegwitV0HtlcScript(chanType channeldb.ChannelType,
|
|
|
|
isIncoming, ourCommit bool, timeout uint32, rHash [32]byte,
|
2023-08-08 06:09:58 +02:00
|
|
|
keyRing *CommitmentKeyRing) (*WitnessScriptDesc, error) {
|
2020-01-06 11:42:03 +01:00
|
|
|
|
|
|
|
var (
|
|
|
|
witnessScript []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2020-03-06 16:11:47 +01:00
|
|
|
// Choose scripts based on channel type.
|
|
|
|
confirmedHtlcSpends := false
|
|
|
|
if chanType.HasAnchors() {
|
|
|
|
confirmedHtlcSpends = true
|
|
|
|
}
|
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
// Generate the proper redeem scripts for the HTLC output modified by
|
|
|
|
// two-bits denoting if this is an incoming HTLC, and if the HTLC is
|
|
|
|
// being applied to their commitment transaction or ours.
|
|
|
|
switch {
|
|
|
|
// The HTLC is paying to us, and being applied to our commitment
|
|
|
|
// transaction. So we need to use the receiver's version of HTLC the
|
|
|
|
// script.
|
|
|
|
case isIncoming && ourCommit:
|
2020-03-06 16:11:47 +01:00
|
|
|
witnessScript, err = input.ReceiverHTLCScript(
|
|
|
|
timeout, keyRing.RemoteHtlcKey, keyRing.LocalHtlcKey,
|
|
|
|
keyRing.RevocationKey, rHash[:], confirmedHtlcSpends,
|
|
|
|
)
|
2020-01-06 11:42:03 +01:00
|
|
|
|
|
|
|
// We're being paid via an HTLC by the remote party, and the HTLC is
|
|
|
|
// being added to their commitment transaction, so we use the sender's
|
|
|
|
// version of the HTLC script.
|
|
|
|
case isIncoming && !ourCommit:
|
2020-03-06 16:11:47 +01:00
|
|
|
witnessScript, err = input.SenderHTLCScript(
|
|
|
|
keyRing.RemoteHtlcKey, keyRing.LocalHtlcKey,
|
|
|
|
keyRing.RevocationKey, rHash[:], confirmedHtlcSpends,
|
|
|
|
)
|
2020-01-06 11:42:03 +01:00
|
|
|
|
|
|
|
// We're sending an HTLC which is being added to our commitment
|
|
|
|
// transaction. Therefore, we need to use the sender's version of the
|
|
|
|
// HTLC script.
|
|
|
|
case !isIncoming && ourCommit:
|
2020-03-06 16:11:47 +01:00
|
|
|
witnessScript, err = input.SenderHTLCScript(
|
|
|
|
keyRing.LocalHtlcKey, keyRing.RemoteHtlcKey,
|
|
|
|
keyRing.RevocationKey, rHash[:], confirmedHtlcSpends,
|
|
|
|
)
|
2020-01-06 11:42:03 +01:00
|
|
|
|
|
|
|
// Finally, we're paying the remote party via an HTLC, which is being
|
|
|
|
// added to their commitment transaction. Therefore, we use the
|
|
|
|
// receiver's version of the HTLC script.
|
|
|
|
case !isIncoming && !ourCommit:
|
2020-03-06 16:11:47 +01:00
|
|
|
witnessScript, err = input.ReceiverHTLCScript(
|
|
|
|
timeout, keyRing.LocalHtlcKey, keyRing.RemoteHtlcKey,
|
|
|
|
keyRing.RevocationKey, rHash[:], confirmedHtlcSpends,
|
|
|
|
)
|
2020-01-06 11:42:03 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
2023-01-20 04:02:28 +01:00
|
|
|
return nil, err
|
2020-01-06 11:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have the redeem scripts, create the P2WSH public key
|
|
|
|
// script for the output itself.
|
|
|
|
htlcP2WSH, err := input.WitnessScriptHash(witnessScript)
|
2023-01-20 04:02:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-08 06:09:58 +02:00
|
|
|
return &WitnessScriptDesc{
|
|
|
|
OutputScript: htlcP2WSH,
|
2023-01-20 04:02:28 +01:00
|
|
|
WitnessScript: witnessScript,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// genTaprootHtlcScript generates the HTLC scripts for a taproot+musig2
|
|
|
|
// channel.
|
|
|
|
func genTaprootHtlcScript(isIncoming, ourCommit bool, timeout uint32,
|
2023-08-08 06:09:58 +02:00
|
|
|
rHash [32]byte,
|
|
|
|
keyRing *CommitmentKeyRing) (*input.HtlcScriptTree, error) {
|
2023-01-20 04:02:28 +01:00
|
|
|
|
|
|
|
var (
|
2023-08-08 06:09:58 +02:00
|
|
|
htlcScriptTree *input.HtlcScriptTree
|
|
|
|
err error
|
2023-01-20 04:02:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Generate the proper redeem scripts for the HTLC output modified by
|
|
|
|
// two-bits denoting if this is an incoming HTLC, and if the HTLC is
|
|
|
|
// being applied to their commitment transaction or ours.
|
|
|
|
switch {
|
|
|
|
// The HTLC is paying to us, and being applied to our commitment
|
|
|
|
// transaction. So we need to use the receiver's version of HTLC the
|
|
|
|
// script.
|
|
|
|
case isIncoming && ourCommit:
|
2023-08-08 06:09:58 +02:00
|
|
|
htlcScriptTree, err = input.ReceiverHTLCScriptTaproot(
|
2023-01-20 04:02:28 +01:00
|
|
|
timeout, keyRing.RemoteHtlcKey, keyRing.LocalHtlcKey,
|
2023-08-08 06:09:58 +02:00
|
|
|
keyRing.RevocationKey, rHash[:], ourCommit,
|
2023-01-20 04:02:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// We're being paid via an HTLC by the remote party, and the HTLC is
|
|
|
|
// being added to their commitment transaction, so we use the sender's
|
|
|
|
// version of the HTLC script.
|
|
|
|
case isIncoming && !ourCommit:
|
2023-08-08 06:09:58 +02:00
|
|
|
htlcScriptTree, err = input.SenderHTLCScriptTaproot(
|
2023-01-20 04:02:28 +01:00
|
|
|
keyRing.RemoteHtlcKey, keyRing.LocalHtlcKey,
|
2023-08-08 06:09:58 +02:00
|
|
|
keyRing.RevocationKey, rHash[:], ourCommit,
|
2023-01-20 04:02:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// We're sending an HTLC which is being added to our commitment
|
|
|
|
// transaction. Therefore, we need to use the sender's version of the
|
|
|
|
// HTLC script.
|
|
|
|
case !isIncoming && ourCommit:
|
2023-08-08 06:09:58 +02:00
|
|
|
htlcScriptTree, err = input.SenderHTLCScriptTaproot(
|
2023-01-20 04:02:28 +01:00
|
|
|
keyRing.LocalHtlcKey, keyRing.RemoteHtlcKey,
|
2023-08-08 06:09:58 +02:00
|
|
|
keyRing.RevocationKey, rHash[:], ourCommit,
|
2023-01-20 04:02:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Finally, we're paying the remote party via an HTLC, which is being
|
|
|
|
// added to their commitment transaction. Therefore, we use the
|
|
|
|
// receiver's version of the HTLC script.
|
|
|
|
case !isIncoming && !ourCommit:
|
2023-08-08 06:09:58 +02:00
|
|
|
htlcScriptTree, err = input.ReceiverHTLCScriptTaproot(
|
2023-01-20 04:02:28 +01:00
|
|
|
timeout, keyRing.LocalHtlcKey, keyRing.RemoteHtlcKey,
|
2023-08-08 06:09:58 +02:00
|
|
|
keyRing.RevocationKey, rHash[:], ourCommit,
|
2023-01-20 04:02:28 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-09 07:22:12 +02:00
|
|
|
return htlcScriptTree, err
|
2023-01-20 04:02:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// genHtlcScript generates the proper P2WSH public key scripts for the HTLC
|
|
|
|
// output modified by two-bits denoting if this is an incoming HTLC, and if the
|
2023-08-08 06:09:58 +02:00
|
|
|
// HTLC is being applied to their commitment transaction or ours. A script
|
|
|
|
// multiplexer for the various spending paths is returned. The script path that
|
|
|
|
// we need to sign for the remote party (2nd level HTLCs) is also returned
|
|
|
|
// along side the multiplexer.
|
2023-01-20 04:02:28 +01:00
|
|
|
func genHtlcScript(chanType channeldb.ChannelType, isIncoming, ourCommit bool,
|
2023-08-08 06:09:58 +02:00
|
|
|
timeout uint32, rHash [32]byte, keyRing *CommitmentKeyRing,
|
|
|
|
) (input.ScriptDescriptor, error) {
|
2023-01-20 04:02:28 +01:00
|
|
|
|
|
|
|
if !chanType.IsTaproot() {
|
2023-08-08 06:09:58 +02:00
|
|
|
return genSegwitV0HtlcScript(
|
2023-01-20 04:02:28 +01:00
|
|
|
chanType, isIncoming, ourCommit, timeout, rHash,
|
|
|
|
keyRing,
|
|
|
|
)
|
|
|
|
}
|
2023-08-09 07:22:12 +02:00
|
|
|
|
|
|
|
return genTaprootHtlcScript(
|
|
|
|
isIncoming, ourCommit, timeout, rHash, keyRing,
|
|
|
|
)
|
2020-01-06 11:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// addHTLC adds a new HTLC to the passed commitment transaction. One of four
|
|
|
|
// full scripts will be generated for the HTLC output depending on if the HTLC
|
|
|
|
// is incoming and if it's being applied to our commitment transaction or that
|
|
|
|
// of the remote node's. Additionally, in order to be able to efficiently
|
|
|
|
// locate the added HTLC on the commitment transaction from the
|
|
|
|
// PaymentDescriptor that generated it, the generated script is stored within
|
|
|
|
// the descriptor itself.
|
|
|
|
func addHTLC(commitTx *wire.MsgTx, ourCommit bool,
|
|
|
|
isIncoming bool, paymentDesc *PaymentDescriptor,
|
2020-03-06 16:11:45 +01:00
|
|
|
keyRing *CommitmentKeyRing, chanType channeldb.ChannelType) error {
|
2020-01-06 11:42:03 +01:00
|
|
|
|
|
|
|
timeout := paymentDesc.Timeout
|
|
|
|
rHash := paymentDesc.RHash
|
|
|
|
|
2023-03-02 06:38:53 +01:00
|
|
|
scriptInfo, err := genHtlcScript(
|
2020-03-06 16:11:45 +01:00
|
|
|
chanType, isIncoming, ourCommit, timeout, rHash, keyRing,
|
|
|
|
)
|
2020-01-06 11:42:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-08 06:09:58 +02:00
|
|
|
pkScript := scriptInfo.PkScript()
|
2023-03-02 06:38:53 +01:00
|
|
|
|
2020-01-06 11:42:03 +01:00
|
|
|
// Add the new HTLC outputs to the respective commitment transactions.
|
|
|
|
amountPending := int64(paymentDesc.Amount.ToSatoshis())
|
2023-08-08 06:09:58 +02:00
|
|
|
commitTx.AddTxOut(wire.NewTxOut(amountPending, pkScript))
|
2020-01-06 11:42:03 +01:00
|
|
|
|
|
|
|
// Store the pkScript of this particular PaymentDescriptor so we can
|
|
|
|
// quickly locate it within the commitment transaction later.
|
|
|
|
if ourCommit {
|
2023-08-08 06:09:58 +02:00
|
|
|
paymentDesc.ourPkScript = pkScript
|
|
|
|
|
|
|
|
paymentDesc.ourWitnessScript = scriptInfo.WitnessScriptToSign()
|
2020-01-06 11:42:03 +01:00
|
|
|
} else {
|
2023-08-08 06:09:58 +02:00
|
|
|
paymentDesc.theirPkScript = pkScript
|
|
|
|
|
2023-08-09 07:22:12 +02:00
|
|
|
//nolint:lll
|
2023-08-08 06:09:58 +02:00
|
|
|
paymentDesc.theirWitnessScript = scriptInfo.WitnessScriptToSign()
|
2020-01-06 11:42:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-07 23:37:46 +02:00
|
|
|
|
|
|
|
// findOutputIndexesFromRemote finds the index of our and their outputs from
|
|
|
|
// the remote commitment transaction. It derives the key ring to compute the
|
|
|
|
// output scripts and compares them against the outputs inside the commitment
|
|
|
|
// to find the match.
|
|
|
|
func findOutputIndexesFromRemote(revocationPreimage *chainhash.Hash,
|
|
|
|
chanState *channeldb.OpenChannel) (uint32, uint32, error) {
|
|
|
|
|
|
|
|
// Init the output indexes as empty.
|
|
|
|
ourIndex := uint32(channeldb.OutputIndexEmpty)
|
|
|
|
theirIndex := uint32(channeldb.OutputIndexEmpty)
|
|
|
|
|
|
|
|
chanCommit := chanState.RemoteCommitment
|
|
|
|
_, commitmentPoint := btcec.PrivKeyFromBytes(revocationPreimage[:])
|
|
|
|
|
|
|
|
// With the commitment point generated, we can now derive the king ring
|
|
|
|
// which will be used to generate the output scripts.
|
|
|
|
keyRing := DeriveCommitmentKeys(
|
|
|
|
commitmentPoint, false, chanState.ChanType,
|
|
|
|
&chanState.LocalChanCfg, &chanState.RemoteChanCfg,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Since it's remote commitment chain, we'd used the mirrored values.
|
|
|
|
//
|
|
|
|
// We use the remote's channel config for the csv delay.
|
|
|
|
theirDelay := uint32(chanState.RemoteChanCfg.CsvDelay)
|
|
|
|
|
|
|
|
// If we are the initiator of this channel, then it's be false from the
|
|
|
|
// remote's PoV.
|
|
|
|
isRemoteInitiator := !chanState.IsInitiator
|
|
|
|
|
|
|
|
var leaseExpiry uint32
|
|
|
|
if chanState.ChanType.HasLeaseExpiration() {
|
|
|
|
leaseExpiry = chanState.ThawHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map the scripts from our PoV. When facing a local commitment, the to
|
|
|
|
// local output belongs to us and the to remote output belongs to them.
|
|
|
|
// When facing a remote commitment, the to local output belongs to them
|
|
|
|
// and the to remote output belongs to us.
|
|
|
|
|
|
|
|
// Compute the to local script. From our PoV, when facing a remote
|
|
|
|
// commitment, the to local output belongs to them.
|
|
|
|
theirScript, err := CommitScriptToSelf(
|
|
|
|
chanState.ChanType, isRemoteInitiator, keyRing.ToLocalKey,
|
|
|
|
keyRing.RevocationKey, theirDelay, leaseExpiry,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return ourIndex, theirIndex, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the to remote script. From our PoV, when facing a remote
|
|
|
|
// commitment, the to remote output belongs to us.
|
|
|
|
ourScript, _, err := CommitScriptToRemote(
|
|
|
|
chanState.ChanType, isRemoteInitiator, keyRing.ToRemoteKey,
|
|
|
|
leaseExpiry,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return ourIndex, theirIndex, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now compare the scripts to find our/their output index.
|
|
|
|
for i, txOut := range chanCommit.CommitTx.TxOut {
|
|
|
|
switch {
|
2023-08-08 06:09:58 +02:00
|
|
|
case bytes.Equal(txOut.PkScript, ourScript.PkScript()):
|
2022-04-07 23:37:46 +02:00
|
|
|
ourIndex = uint32(i)
|
2023-08-08 06:09:58 +02:00
|
|
|
case bytes.Equal(txOut.PkScript, theirScript.PkScript()):
|
2022-04-07 23:37:46 +02:00
|
|
|
theirIndex = uint32(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ourIndex, theirIndex, nil
|
|
|
|
}
|